Branch data Line data Source code
1 : : /* Intrinsic translation
2 : : Copyright (C) 2002-2025 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 "dependency.h" /* For CAF array alias analysis. */
43 : : #include "attribs.h"
44 : : #include "realmpfr.h"
45 : : #include "constructor.h"
46 : :
47 : : /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
48 : :
49 : : /* This maps Fortran intrinsic math functions to external library or GCC
50 : : builtin functions. */
51 : : typedef struct GTY(()) gfc_intrinsic_map_t {
52 : : /* The explicit enum is required to work around inadequacies in the
53 : : garbage collection/gengtype parsing mechanism. */
54 : : enum gfc_isym_id id;
55 : :
56 : : /* Enum value from the "language-independent", aka C-centric, part
57 : : of gcc, or END_BUILTINS of no such value set. */
58 : : enum built_in_function float_built_in;
59 : : enum built_in_function double_built_in;
60 : : enum built_in_function long_double_built_in;
61 : : enum built_in_function complex_float_built_in;
62 : : enum built_in_function complex_double_built_in;
63 : : enum built_in_function complex_long_double_built_in;
64 : :
65 : : /* True if the naming pattern is to prepend "c" for complex and
66 : : append "f" for kind=4. False if the naming pattern is to
67 : : prepend "_gfortran_" and append "[rc](4|8|10|16)". */
68 : : bool libm_name;
69 : :
70 : : /* True if a complex version of the function exists. */
71 : : bool complex_available;
72 : :
73 : : /* True if the function should be marked const. */
74 : : bool is_constant;
75 : :
76 : : /* The base library name of this function. */
77 : : const char *name;
78 : :
79 : : /* Cache decls created for the various operand types. */
80 : : tree real4_decl;
81 : : tree real8_decl;
82 : : tree real10_decl;
83 : : tree real16_decl;
84 : : tree complex4_decl;
85 : : tree complex8_decl;
86 : : tree complex10_decl;
87 : : tree complex16_decl;
88 : : }
89 : : gfc_intrinsic_map_t;
90 : :
91 : : /* ??? The NARGS==1 hack here is based on the fact that (c99 at least)
92 : : defines complex variants of all of the entries in mathbuiltins.def
93 : : except for atan2. */
94 : : #define DEFINE_MATH_BUILTIN(ID, NAME, ARGTYPE) \
95 : : { GFC_ISYM_ ## ID, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
96 : : BUILT_IN_ ## ID ## L, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
97 : : true, false, true, NAME, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, \
98 : : NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
99 : :
100 : : #define DEFINE_MATH_BUILTIN_C(ID, NAME, ARGTYPE) \
101 : : { GFC_ISYM_ ## ID, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
102 : : BUILT_IN_ ## ID ## L, BUILT_IN_C ## ID ## F, BUILT_IN_C ## ID, \
103 : : BUILT_IN_C ## ID ## L, true, true, true, NAME, NULL_TREE, NULL_TREE, \
104 : : NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
105 : :
106 : : #define LIB_FUNCTION(ID, NAME, HAVE_COMPLEX) \
107 : : { GFC_ISYM_ ## ID, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
108 : : END_BUILTINS, END_BUILTINS, END_BUILTINS, \
109 : : false, HAVE_COMPLEX, true, NAME, NULL_TREE, NULL_TREE, NULL_TREE, \
110 : : NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE }
111 : :
112 : : #define OTHER_BUILTIN(ID, NAME, TYPE, CONST) \
113 : : { GFC_ISYM_NONE, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
114 : : BUILT_IN_ ## ID ## L, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
115 : : true, false, CONST, NAME, NULL_TREE, NULL_TREE, \
116 : : NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
117 : :
118 : : static GTY(()) gfc_intrinsic_map_t gfc_intrinsic_map[] =
119 : : {
120 : : /* Functions built into gcc itself (DEFINE_MATH_BUILTIN and
121 : : DEFINE_MATH_BUILTIN_C), then the built-ins that don't correspond
122 : : to any GFC_ISYM id directly, which use the OTHER_BUILTIN macro. */
123 : : #include "mathbuiltins.def"
124 : :
125 : : /* Functions in libgfortran. */
126 : : LIB_FUNCTION (ERFC_SCALED, "erfc_scaled", false),
127 : : LIB_FUNCTION (SIND, "sind", false),
128 : : LIB_FUNCTION (COSD, "cosd", false),
129 : : LIB_FUNCTION (TAND, "tand", false),
130 : :
131 : : /* End the list. */
132 : : LIB_FUNCTION (NONE, NULL, false)
133 : :
134 : : };
135 : : #undef OTHER_BUILTIN
136 : : #undef LIB_FUNCTION
137 : : #undef DEFINE_MATH_BUILTIN
138 : : #undef DEFINE_MATH_BUILTIN_C
139 : :
140 : :
141 : : enum rounding_mode { RND_ROUND, RND_TRUNC, RND_CEIL, RND_FLOOR };
142 : :
143 : :
144 : : /* Find the correct variant of a given builtin from its argument. */
145 : : static tree
146 : 11419 : builtin_decl_for_precision (enum built_in_function base_built_in,
147 : : int precision)
148 : : {
149 : 11419 : enum built_in_function i = END_BUILTINS;
150 : :
151 : 11419 : gfc_intrinsic_map_t *m;
152 : 489197 : for (m = gfc_intrinsic_map; m->double_built_in != base_built_in ; m++)
153 : : ;
154 : :
155 : 11419 : if (precision == TYPE_PRECISION (float_type_node))
156 : 5801 : i = m->float_built_in;
157 : 5618 : else if (precision == TYPE_PRECISION (double_type_node))
158 : : i = m->double_built_in;
159 : 1695 : else if (precision == TYPE_PRECISION (long_double_type_node)
160 : 1695 : && (!gfc_real16_is_float128
161 : 1571 : || long_double_type_node != gfc_float128_type_node))
162 : 1571 : i = m->long_double_built_in;
163 : 124 : else if (precision == TYPE_PRECISION (gfc_float128_type_node))
164 : : {
165 : : /* Special treatment, because it is not exactly a built-in, but
166 : : a library function. */
167 : 124 : return m->real16_decl;
168 : : }
169 : :
170 : 11295 : return (i == END_BUILTINS ? NULL_TREE : builtin_decl_explicit (i));
171 : : }
172 : :
173 : :
174 : : tree
175 : 10380 : gfc_builtin_decl_for_float_kind (enum built_in_function double_built_in,
176 : : int kind)
177 : : {
178 : 10380 : int i = gfc_validate_kind (BT_REAL, kind, false);
179 : :
180 : 10380 : if (gfc_real_kinds[i].c_float128)
181 : : {
182 : : /* For _Float128, the story is a bit different, because we return
183 : : a decl to a library function rather than a built-in. */
184 : : gfc_intrinsic_map_t *m;
185 : 36328 : for (m = gfc_intrinsic_map; m->double_built_in != double_built_in ; m++)
186 : : ;
187 : :
188 : 905 : return m->real16_decl;
189 : : }
190 : :
191 : 9475 : return builtin_decl_for_precision (double_built_in,
192 : 9475 : gfc_real_kinds[i].mode_precision);
193 : : }
194 : :
195 : :
196 : : /* Evaluate the arguments to an intrinsic function. The value
197 : : of NARGS may be less than the actual number of arguments in EXPR
198 : : to allow optional "KIND" arguments that are not included in the
199 : : generated code to be ignored. */
200 : :
201 : : static void
202 : 77879 : gfc_conv_intrinsic_function_args (gfc_se *se, gfc_expr *expr,
203 : : tree *argarray, int nargs)
204 : : {
205 : 77879 : gfc_actual_arglist *actual;
206 : 77879 : gfc_expr *e;
207 : 77879 : gfc_intrinsic_arg *formal;
208 : 77879 : gfc_se argse;
209 : 77879 : int curr_arg;
210 : :
211 : 77879 : formal = expr->value.function.isym->formal;
212 : 77879 : actual = expr->value.function.actual;
213 : :
214 : 175644 : for (curr_arg = 0; curr_arg < nargs; curr_arg++,
215 : 61285 : actual = actual->next,
216 : 97765 : formal = formal ? formal->next : NULL)
217 : : {
218 : 97765 : gcc_assert (actual);
219 : 97765 : e = actual->expr;
220 : : /* Skip omitted optional arguments. */
221 : 97765 : if (!e)
222 : : {
223 : 31 : --curr_arg;
224 : 31 : continue;
225 : : }
226 : :
227 : : /* Evaluate the parameter. This will substitute scalarized
228 : : references automatically. */
229 : 97734 : gfc_init_se (&argse, se);
230 : :
231 : 97734 : if (e->ts.type == BT_CHARACTER)
232 : : {
233 : 9525 : gfc_conv_expr (&argse, e);
234 : 9525 : gfc_conv_string_parameter (&argse);
235 : 9525 : argarray[curr_arg++] = argse.string_length;
236 : 9525 : gcc_assert (curr_arg < nargs);
237 : : }
238 : : else
239 : 88209 : gfc_conv_expr_val (&argse, e);
240 : :
241 : : /* If an optional argument is itself an optional dummy argument,
242 : : check its presence and substitute a null if absent. */
243 : 97734 : if (e->expr_type == EXPR_VARIABLE
244 : 51062 : && e->symtree->n.sym->attr.optional
245 : 203 : && formal
246 : 153 : && formal->optional)
247 : 80 : gfc_conv_missing_dummy (&argse, e, formal->ts, 0);
248 : :
249 : 97734 : gfc_add_block_to_block (&se->pre, &argse.pre);
250 : 97734 : gfc_add_block_to_block (&se->post, &argse.post);
251 : 97734 : argarray[curr_arg] = argse.expr;
252 : : }
253 : 77879 : }
254 : :
255 : : /* Count the number of actual arguments to the intrinsic function EXPR
256 : : including any "hidden" string length arguments. */
257 : :
258 : : static unsigned int
259 : 53692 : gfc_intrinsic_argument_list_length (gfc_expr *expr)
260 : : {
261 : 53692 : int n = 0;
262 : 53692 : gfc_actual_arglist *actual;
263 : :
264 : 122416 : for (actual = expr->value.function.actual; actual; actual = actual->next)
265 : : {
266 : 68724 : if (!actual->expr)
267 : 6313 : continue;
268 : :
269 : 62411 : if (actual->expr->ts.type == BT_CHARACTER)
270 : 4505 : n += 2;
271 : : else
272 : 57906 : n++;
273 : : }
274 : :
275 : 53692 : return n;
276 : : }
277 : :
278 : :
279 : : /* Conversions between different types are output by the frontend as
280 : : intrinsic functions. We implement these directly with inline code. */
281 : :
282 : : static void
283 : 37840 : gfc_conv_intrinsic_conversion (gfc_se * se, gfc_expr * expr)
284 : : {
285 : 37840 : tree type;
286 : 37840 : tree *args;
287 : 37840 : int nargs;
288 : :
289 : 37840 : nargs = gfc_intrinsic_argument_list_length (expr);
290 : 37840 : args = XALLOCAVEC (tree, nargs);
291 : :
292 : : /* Evaluate all the arguments passed. Whilst we're only interested in the
293 : : first one here, there are other parts of the front-end that assume this
294 : : and will trigger an ICE if it's not the case. */
295 : 37840 : type = gfc_typenode_for_spec (&expr->ts);
296 : 37840 : gcc_assert (expr->value.function.actual->expr);
297 : 37840 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
298 : :
299 : : /* Conversion between character kinds involves a call to a library
300 : : function. */
301 : 37840 : if (expr->ts.type == BT_CHARACTER)
302 : : {
303 : 208 : tree fndecl, var, addr, tmp;
304 : :
305 : 208 : if (expr->ts.kind == 1
306 : 77 : && expr->value.function.actual->expr->ts.kind == 4)
307 : 77 : fndecl = gfor_fndecl_convert_char4_to_char1;
308 : 131 : else if (expr->ts.kind == 4
309 : 131 : && expr->value.function.actual->expr->ts.kind == 1)
310 : 131 : fndecl = gfor_fndecl_convert_char1_to_char4;
311 : : else
312 : 0 : gcc_unreachable ();
313 : :
314 : : /* Create the variable storing the converted value. */
315 : 208 : type = gfc_get_pchar_type (expr->ts.kind);
316 : 208 : var = gfc_create_var (type, "str");
317 : 208 : addr = gfc_build_addr_expr (build_pointer_type (type), var);
318 : :
319 : : /* Call the library function that will perform the conversion. */
320 : 208 : gcc_assert (nargs >= 2);
321 : 208 : tmp = build_call_expr_loc (input_location,
322 : : fndecl, 3, addr, args[0], args[1]);
323 : 208 : gfc_add_expr_to_block (&se->pre, tmp);
324 : :
325 : : /* Free the temporary afterwards. */
326 : 208 : tmp = gfc_call_free (var);
327 : 208 : gfc_add_expr_to_block (&se->post, tmp);
328 : :
329 : 208 : se->expr = var;
330 : 208 : se->string_length = args[0];
331 : :
332 : 208 : return;
333 : : }
334 : :
335 : : /* Conversion from complex to non-complex involves taking the real
336 : : component of the value. */
337 : 37632 : if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
338 : 37632 : && expr->ts.type != BT_COMPLEX)
339 : : {
340 : 577 : tree artype;
341 : :
342 : 577 : artype = TREE_TYPE (TREE_TYPE (args[0]));
343 : 577 : args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
344 : : args[0]);
345 : : }
346 : :
347 : 37632 : se->expr = convert (type, args[0]);
348 : : }
349 : :
350 : : /* This is needed because the gcc backend only implements
351 : : FIX_TRUNC_EXPR, which is the same as INT() in Fortran.
352 : : FLOOR(x) = INT(x) <= x ? INT(x) : INT(x) - 1
353 : : Similarly for CEILING. */
354 : :
355 : : static tree
356 : 132 : build_fixbound_expr (stmtblock_t * pblock, tree arg, tree type, int up)
357 : : {
358 : 132 : tree tmp;
359 : 132 : tree cond;
360 : 132 : tree argtype;
361 : 132 : tree intval;
362 : :
363 : 132 : argtype = TREE_TYPE (arg);
364 : 132 : arg = gfc_evaluate_now (arg, pblock);
365 : :
366 : 132 : intval = convert (type, arg);
367 : 132 : intval = gfc_evaluate_now (intval, pblock);
368 : :
369 : 132 : tmp = convert (argtype, intval);
370 : 248 : cond = fold_build2_loc (input_location, up ? GE_EXPR : LE_EXPR,
371 : : logical_type_node, tmp, arg);
372 : :
373 : 248 : tmp = fold_build2_loc (input_location, up ? PLUS_EXPR : MINUS_EXPR, type,
374 : : intval, build_int_cst (type, 1));
375 : 132 : tmp = fold_build3_loc (input_location, COND_EXPR, type, cond, intval, tmp);
376 : 132 : return tmp;
377 : : }
378 : :
379 : :
380 : : /* Round to nearest integer, away from zero. */
381 : :
382 : : static tree
383 : 516 : build_round_expr (tree arg, tree restype)
384 : : {
385 : 516 : tree argtype;
386 : 516 : tree fn;
387 : 516 : int argprec, resprec;
388 : :
389 : 516 : argtype = TREE_TYPE (arg);
390 : 516 : argprec = TYPE_PRECISION (argtype);
391 : 516 : resprec = TYPE_PRECISION (restype);
392 : :
393 : : /* Depending on the type of the result, choose the int intrinsic (iround,
394 : : available only as a builtin, therefore cannot use it for _Float128), long
395 : : int intrinsic (lround family) or long long intrinsic (llround). If we
396 : : don't have an appropriate function that converts directly to the integer
397 : : type (such as kind == 16), just use ROUND, and then convert the result to
398 : : an integer. We might also need to convert the result afterwards. */
399 : 516 : if (resprec <= INT_TYPE_SIZE
400 : 516 : && argprec <= TYPE_PRECISION (long_double_type_node))
401 : 458 : fn = builtin_decl_for_precision (BUILT_IN_IROUND, argprec);
402 : 62 : else if (resprec <= LONG_TYPE_SIZE)
403 : 46 : fn = builtin_decl_for_precision (BUILT_IN_LROUND, argprec);
404 : 12 : else if (resprec <= LONG_LONG_TYPE_SIZE)
405 : 0 : fn = builtin_decl_for_precision (BUILT_IN_LLROUND, argprec);
406 : 12 : else if (resprec >= argprec)
407 : 12 : fn = builtin_decl_for_precision (BUILT_IN_ROUND, argprec);
408 : : else
409 : 0 : gcc_unreachable ();
410 : :
411 : 516 : return convert (restype, build_call_expr_loc (input_location,
412 : 516 : fn, 1, arg));
413 : : }
414 : :
415 : :
416 : : /* Convert a real to an integer using a specific rounding mode.
417 : : Ideally we would just build the corresponding GENERIC node,
418 : : however the RTL expander only actually supports FIX_TRUNC_EXPR. */
419 : :
420 : : static tree
421 : 1489 : build_fix_expr (stmtblock_t * pblock, tree arg, tree type,
422 : : enum rounding_mode op)
423 : : {
424 : 1489 : switch (op)
425 : : {
426 : 116 : case RND_FLOOR:
427 : 116 : return build_fixbound_expr (pblock, arg, type, 0);
428 : :
429 : 16 : case RND_CEIL:
430 : 16 : return build_fixbound_expr (pblock, arg, type, 1);
431 : :
432 : 162 : case RND_ROUND:
433 : 162 : return build_round_expr (arg, type);
434 : :
435 : 1195 : case RND_TRUNC:
436 : 1195 : return fold_build1_loc (input_location, FIX_TRUNC_EXPR, type, arg);
437 : :
438 : 0 : default:
439 : 0 : gcc_unreachable ();
440 : : }
441 : : }
442 : :
443 : :
444 : : /* Round a real value using the specified rounding mode.
445 : : We use a temporary integer of that same kind size as the result.
446 : : Values larger than those that can be represented by this kind are
447 : : unchanged, as they will not be accurate enough to represent the
448 : : rounding.
449 : : huge = HUGE (KIND (a))
450 : : aint (a) = ((a > huge) || (a < -huge)) ? a : (real)(int)a
451 : : */
452 : :
453 : : static void
454 : 220 : gfc_conv_intrinsic_aint (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
455 : : {
456 : 220 : tree type;
457 : 220 : tree itype;
458 : 220 : tree arg[2];
459 : 220 : tree tmp;
460 : 220 : tree cond;
461 : 220 : tree decl;
462 : 220 : mpfr_t huge;
463 : 220 : int n, nargs;
464 : 220 : int kind;
465 : :
466 : 220 : kind = expr->ts.kind;
467 : 220 : nargs = gfc_intrinsic_argument_list_length (expr);
468 : :
469 : 220 : decl = NULL_TREE;
470 : : /* We have builtin functions for some cases. */
471 : 220 : switch (op)
472 : : {
473 : 74 : case RND_ROUND:
474 : 74 : decl = gfc_builtin_decl_for_float_kind (BUILT_IN_ROUND, kind);
475 : 74 : break;
476 : :
477 : 146 : case RND_TRUNC:
478 : 146 : decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, kind);
479 : 146 : break;
480 : :
481 : 0 : default:
482 : 0 : gcc_unreachable ();
483 : : }
484 : :
485 : : /* Evaluate the argument. */
486 : 220 : gcc_assert (expr->value.function.actual->expr);
487 : 220 : gfc_conv_intrinsic_function_args (se, expr, arg, nargs);
488 : :
489 : : /* Use a builtin function if one exists. */
490 : 220 : if (decl != NULL_TREE)
491 : : {
492 : 220 : se->expr = build_call_expr_loc (input_location, decl, 1, arg[0]);
493 : 220 : return;
494 : : }
495 : :
496 : : /* This code is probably redundant, but we'll keep it lying around just
497 : : in case. */
498 : 0 : type = gfc_typenode_for_spec (&expr->ts);
499 : 0 : arg[0] = gfc_evaluate_now (arg[0], &se->pre);
500 : :
501 : : /* Test if the value is too large to handle sensibly. */
502 : 0 : gfc_set_model_kind (kind);
503 : 0 : mpfr_init (huge);
504 : 0 : n = gfc_validate_kind (BT_INTEGER, kind, false);
505 : 0 : mpfr_set_z (huge, gfc_integer_kinds[n].huge, GFC_RND_MODE);
506 : 0 : tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
507 : 0 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, arg[0],
508 : : tmp);
509 : :
510 : 0 : mpfr_neg (huge, huge, GFC_RND_MODE);
511 : 0 : tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
512 : 0 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, arg[0],
513 : : tmp);
514 : 0 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
515 : : cond, tmp);
516 : 0 : itype = gfc_get_int_type (kind);
517 : :
518 : 0 : tmp = build_fix_expr (&se->pre, arg[0], itype, op);
519 : 0 : tmp = convert (type, tmp);
520 : 0 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
521 : : arg[0]);
522 : 0 : mpfr_clear (huge);
523 : : }
524 : :
525 : :
526 : : /* Convert to an integer using the specified rounding mode. */
527 : :
528 : : static void
529 : 2970 : gfc_conv_intrinsic_int (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
530 : : {
531 : 2970 : tree type;
532 : 2970 : tree *args;
533 : 2970 : int nargs;
534 : :
535 : 2970 : nargs = gfc_intrinsic_argument_list_length (expr);
536 : 2970 : args = XALLOCAVEC (tree, nargs);
537 : :
538 : : /* Evaluate the argument, we process all arguments even though we only
539 : : use the first one for code generation purposes. */
540 : 2970 : type = gfc_typenode_for_spec (&expr->ts);
541 : 2970 : gcc_assert (expr->value.function.actual->expr);
542 : 2970 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
543 : :
544 : 2970 : if (TREE_CODE (TREE_TYPE (args[0])) == INTEGER_TYPE)
545 : : {
546 : : /* Conversion to a different integer kind. */
547 : 1481 : se->expr = convert (type, args[0]);
548 : : }
549 : : else
550 : : {
551 : : /* Conversion from complex to non-complex involves taking the real
552 : : component of the value. */
553 : 1489 : if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
554 : 1489 : && expr->ts.type != BT_COMPLEX)
555 : : {
556 : 192 : tree artype;
557 : :
558 : 192 : artype = TREE_TYPE (TREE_TYPE (args[0]));
559 : 192 : args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
560 : : args[0]);
561 : : }
562 : :
563 : 1489 : se->expr = build_fix_expr (&se->pre, args[0], type, op);
564 : : }
565 : 2970 : }
566 : :
567 : :
568 : : /* Get the imaginary component of a value. */
569 : :
570 : : static void
571 : 428 : gfc_conv_intrinsic_imagpart (gfc_se * se, gfc_expr * expr)
572 : : {
573 : 428 : tree arg;
574 : :
575 : 428 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
576 : 428 : se->expr = fold_build1_loc (input_location, IMAGPART_EXPR,
577 : 428 : TREE_TYPE (TREE_TYPE (arg)), arg);
578 : 428 : }
579 : :
580 : :
581 : : /* Get the complex conjugate of a value. */
582 : :
583 : : static void
584 : 255 : gfc_conv_intrinsic_conjg (gfc_se * se, gfc_expr * expr)
585 : : {
586 : 255 : tree arg;
587 : :
588 : 255 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
589 : 255 : se->expr = fold_build1_loc (input_location, CONJ_EXPR, TREE_TYPE (arg), arg);
590 : 255 : }
591 : :
592 : :
593 : :
594 : : static tree
595 : 642075 : define_quad_builtin (const char *name, tree type, bool is_const)
596 : : {
597 : 642075 : tree fndecl;
598 : 642075 : fndecl = build_decl (input_location, FUNCTION_DECL, get_identifier (name),
599 : : type);
600 : :
601 : : /* Mark the decl as external. */
602 : 642075 : DECL_EXTERNAL (fndecl) = 1;
603 : 642075 : TREE_PUBLIC (fndecl) = 1;
604 : :
605 : : /* Mark it __attribute__((const)). */
606 : 642075 : TREE_READONLY (fndecl) = is_const;
607 : :
608 : 642075 : rest_of_decl_compilation (fndecl, 1, 0);
609 : :
610 : 642075 : return fndecl;
611 : : }
612 : :
613 : : /* Add SIMD attribute for FNDECL built-in if the built-in
614 : : name is in VECTORIZED_BUILTINS. */
615 : :
616 : : static void
617 : 43693630 : add_simd_flag_for_built_in (tree fndecl)
618 : : {
619 : 43693630 : if (gfc_vectorized_builtins == NULL
620 : 17627430 : || fndecl == NULL_TREE)
621 : 36074995 : return;
622 : :
623 : 7618635 : const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
624 : 7618635 : int *clauses = gfc_vectorized_builtins->get (name);
625 : 7618635 : if (clauses)
626 : : {
627 : 4779708 : for (unsigned i = 0; i < 3; i++)
628 : 3584781 : if (*clauses & (1 << i))
629 : : {
630 : 1194932 : gfc_simd_clause simd_type = (gfc_simd_clause)*clauses;
631 : 1194932 : tree omp_clause = NULL_TREE;
632 : 1194932 : if (simd_type == SIMD_NONE)
633 : : ; /* No SIMD clause. */
634 : : else
635 : : {
636 : 1194932 : omp_clause_code code
637 : : = (simd_type == SIMD_INBRANCH
638 : 1194932 : ? OMP_CLAUSE_INBRANCH : OMP_CLAUSE_NOTINBRANCH);
639 : 1194932 : omp_clause = build_omp_clause (UNKNOWN_LOCATION, code);
640 : 1194932 : omp_clause = build_tree_list (NULL_TREE, omp_clause);
641 : : }
642 : :
643 : 1194932 : DECL_ATTRIBUTES (fndecl)
644 : 2389864 : = tree_cons (get_identifier ("omp declare simd"), omp_clause,
645 : 1194932 : DECL_ATTRIBUTES (fndecl));
646 : : }
647 : : }
648 : : }
649 : :
650 : : /* Set SIMD attribute to all built-in functions that are mentioned
651 : : in gfc_vectorized_builtins vector. */
652 : :
653 : : void
654 : 74057 : gfc_adjust_builtins (void)
655 : : {
656 : 74057 : gfc_intrinsic_map_t *m;
657 : 4443420 : for (m = gfc_intrinsic_map;
658 : 4443420 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
659 : : {
660 : 4369363 : add_simd_flag_for_built_in (m->real4_decl);
661 : 4369363 : add_simd_flag_for_built_in (m->complex4_decl);
662 : 4369363 : add_simd_flag_for_built_in (m->real8_decl);
663 : 4369363 : add_simd_flag_for_built_in (m->complex8_decl);
664 : 4369363 : add_simd_flag_for_built_in (m->real10_decl);
665 : 4369363 : add_simd_flag_for_built_in (m->complex10_decl);
666 : 4369363 : add_simd_flag_for_built_in (m->real16_decl);
667 : 4369363 : add_simd_flag_for_built_in (m->complex16_decl);
668 : 4369363 : add_simd_flag_for_built_in (m->real16_decl);
669 : 4369363 : add_simd_flag_for_built_in (m->complex16_decl);
670 : : }
671 : :
672 : : /* Release all strings. */
673 : 74057 : if (gfc_vectorized_builtins != NULL)
674 : : {
675 : 1643026 : for (hash_map<nofree_string_hash, int>::iterator it
676 : 29877 : = gfc_vectorized_builtins->begin ();
677 : 1643026 : it != gfc_vectorized_builtins->end (); ++it)
678 : 1613149 : free (CONST_CAST (char *, (*it).first));
679 : :
680 : 59754 : delete gfc_vectorized_builtins;
681 : 29877 : gfc_vectorized_builtins = NULL;
682 : : }
683 : 74057 : }
684 : :
685 : : /* Initialize function decls for library functions. The external functions
686 : : are created as required. Builtin functions are added here. */
687 : :
688 : : void
689 : 30575 : gfc_build_intrinsic_lib_fndecls (void)
690 : : {
691 : 30575 : gfc_intrinsic_map_t *m;
692 : 30575 : tree quad_decls[END_BUILTINS + 1];
693 : :
694 : 30575 : if (gfc_real16_is_float128)
695 : : {
696 : : /* If we have soft-float types, we create the decls for their
697 : : C99-like library functions. For now, we only handle _Float128
698 : : q-suffixed or IEC 60559 f128-suffixed functions. */
699 : :
700 : 30575 : tree type, complex_type, func_1, func_2, func_3, func_cabs, func_frexp;
701 : 30575 : tree func_iround, func_lround, func_llround, func_scalbn, func_cpow;
702 : :
703 : 30575 : memset (quad_decls, 0, sizeof(tree) * (END_BUILTINS + 1));
704 : :
705 : 30575 : type = gfc_float128_type_node;
706 : 30575 : complex_type = gfc_complex_float128_type_node;
707 : : /* type (*) (type) */
708 : 30575 : func_1 = build_function_type_list (type, type, NULL_TREE);
709 : : /* int (*) (type) */
710 : 30575 : func_iround = build_function_type_list (integer_type_node,
711 : : type, NULL_TREE);
712 : : /* long (*) (type) */
713 : 30575 : func_lround = build_function_type_list (long_integer_type_node,
714 : : type, NULL_TREE);
715 : : /* long long (*) (type) */
716 : 30575 : func_llround = build_function_type_list (long_long_integer_type_node,
717 : : type, NULL_TREE);
718 : : /* type (*) (type, type) */
719 : 30575 : func_2 = build_function_type_list (type, type, type, NULL_TREE);
720 : : /* type (*) (type, type, type) */
721 : 30575 : func_3 = build_function_type_list (type, type, type, type, NULL_TREE);
722 : : /* type (*) (type, &int) */
723 : 30575 : func_frexp
724 : 30575 : = build_function_type_list (type,
725 : : type,
726 : : build_pointer_type (integer_type_node),
727 : : NULL_TREE);
728 : : /* type (*) (type, int) */
729 : 30575 : func_scalbn = build_function_type_list (type,
730 : : type, integer_type_node, NULL_TREE);
731 : : /* type (*) (complex type) */
732 : 30575 : func_cabs = build_function_type_list (type, complex_type, NULL_TREE);
733 : : /* complex type (*) (complex type, complex type) */
734 : 30575 : func_cpow
735 : 30575 : = build_function_type_list (complex_type,
736 : : complex_type, complex_type, NULL_TREE);
737 : :
738 : : #define DEFINE_MATH_BUILTIN(ID, NAME, ARGTYPE)
739 : : #define DEFINE_MATH_BUILTIN_C(ID, NAME, ARGTYPE)
740 : : #define LIB_FUNCTION(ID, NAME, HAVE_COMPLEX)
741 : :
742 : : /* Only these built-ins are actually needed here. These are used directly
743 : : from the code, when calling builtin_decl_for_precision() or
744 : : builtin_decl_for_float_type(). The others are all constructed by
745 : : gfc_get_intrinsic_lib_fndecl(). */
746 : : #define OTHER_BUILTIN(ID, NAME, TYPE, CONST) \
747 : : quad_decls[BUILT_IN_ ## ID] \
748 : : = define_quad_builtin (gfc_real16_use_iec_60559 \
749 : : ? NAME "f128" : NAME "q", func_ ## TYPE, \
750 : : CONST);
751 : :
752 : : #include "mathbuiltins.def"
753 : :
754 : : #undef OTHER_BUILTIN
755 : : #undef LIB_FUNCTION
756 : : #undef DEFINE_MATH_BUILTIN
757 : : #undef DEFINE_MATH_BUILTIN_C
758 : :
759 : : /* There is one built-in we defined manually, because it gets called
760 : : with builtin_decl_for_precision() or builtin_decl_for_float_type()
761 : : even though it is not an OTHER_BUILTIN: it is SQRT. */
762 : 30575 : quad_decls[BUILT_IN_SQRT]
763 : 30575 : = define_quad_builtin (gfc_real16_use_iec_60559
764 : : ? "sqrtf128" : "sqrtq", func_1, true);
765 : : }
766 : :
767 : : /* Add GCC builtin functions. */
768 : 1803925 : for (m = gfc_intrinsic_map;
769 : 1834500 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
770 : : {
771 : 1803925 : if (m->float_built_in != END_BUILTINS)
772 : 1681625 : m->real4_decl = builtin_decl_explicit (m->float_built_in);
773 : 1803925 : if (m->complex_float_built_in != END_BUILTINS)
774 : 489200 : m->complex4_decl = builtin_decl_explicit (m->complex_float_built_in);
775 : 1803925 : if (m->double_built_in != END_BUILTINS)
776 : 1681625 : m->real8_decl = builtin_decl_explicit (m->double_built_in);
777 : 1803925 : if (m->complex_double_built_in != END_BUILTINS)
778 : 489200 : m->complex8_decl = builtin_decl_explicit (m->complex_double_built_in);
779 : :
780 : : /* If real(kind=10) exists, it is always long double. */
781 : 1803925 : if (m->long_double_built_in != END_BUILTINS)
782 : 1681625 : m->real10_decl = builtin_decl_explicit (m->long_double_built_in);
783 : 1803925 : if (m->complex_long_double_built_in != END_BUILTINS)
784 : 489200 : m->complex10_decl
785 : 489200 : = builtin_decl_explicit (m->complex_long_double_built_in);
786 : :
787 : 1803925 : if (!gfc_real16_is_float128)
788 : : {
789 : 0 : if (m->long_double_built_in != END_BUILTINS)
790 : 0 : m->real16_decl = builtin_decl_explicit (m->long_double_built_in);
791 : 0 : if (m->complex_long_double_built_in != END_BUILTINS)
792 : 0 : m->complex16_decl
793 : 0 : = builtin_decl_explicit (m->complex_long_double_built_in);
794 : : }
795 : 1803925 : else if (quad_decls[m->double_built_in] != NULL_TREE)
796 : : {
797 : : /* Quad-precision function calls are constructed when first
798 : : needed by builtin_decl_for_precision(), except for those
799 : : that will be used directly (define by OTHER_BUILTIN). */
800 : 642075 : m->real16_decl = quad_decls[m->double_built_in];
801 : : }
802 : 1161850 : else if (quad_decls[m->complex_double_built_in] != NULL_TREE)
803 : : {
804 : : /* Same thing for the complex ones. */
805 : 0 : m->complex16_decl = quad_decls[m->double_built_in];
806 : : }
807 : : }
808 : 30575 : }
809 : :
810 : :
811 : : /* Create a fndecl for a simple intrinsic library function. */
812 : :
813 : : static tree
814 : 4394 : gfc_get_intrinsic_lib_fndecl (gfc_intrinsic_map_t * m, gfc_expr * expr)
815 : : {
816 : 4394 : tree type;
817 : 4394 : vec<tree, va_gc> *argtypes;
818 : 4394 : tree fndecl;
819 : 4394 : gfc_actual_arglist *actual;
820 : 4394 : tree *pdecl;
821 : 4394 : gfc_typespec *ts;
822 : 4394 : char name[GFC_MAX_SYMBOL_LEN + 3];
823 : :
824 : 4394 : ts = &expr->ts;
825 : 4394 : if (ts->type == BT_REAL)
826 : : {
827 : 3550 : switch (ts->kind)
828 : : {
829 : 1265 : case 4:
830 : 1265 : pdecl = &m->real4_decl;
831 : 1265 : break;
832 : 1272 : case 8:
833 : 1272 : pdecl = &m->real8_decl;
834 : 1272 : break;
835 : 570 : case 10:
836 : 570 : pdecl = &m->real10_decl;
837 : 570 : break;
838 : 443 : case 16:
839 : 443 : pdecl = &m->real16_decl;
840 : 443 : break;
841 : 0 : default:
842 : 0 : gcc_unreachable ();
843 : : }
844 : : }
845 : 844 : else if (ts->type == BT_COMPLEX)
846 : : {
847 : 844 : gcc_assert (m->complex_available);
848 : :
849 : 844 : switch (ts->kind)
850 : : {
851 : 386 : case 4:
852 : 386 : pdecl = &m->complex4_decl;
853 : 386 : break;
854 : 387 : case 8:
855 : 387 : pdecl = &m->complex8_decl;
856 : 387 : break;
857 : 51 : case 10:
858 : 51 : pdecl = &m->complex10_decl;
859 : 51 : break;
860 : 20 : case 16:
861 : 20 : pdecl = &m->complex16_decl;
862 : 20 : break;
863 : 0 : default:
864 : 0 : gcc_unreachable ();
865 : : }
866 : : }
867 : : else
868 : 0 : gcc_unreachable ();
869 : :
870 : 4394 : if (*pdecl)
871 : 4054 : return *pdecl;
872 : :
873 : 340 : if (m->libm_name)
874 : : {
875 : 163 : int n = gfc_validate_kind (BT_REAL, ts->kind, false);
876 : 163 : if (gfc_real_kinds[n].c_float)
877 : 0 : snprintf (name, sizeof (name), "%s%s%s",
878 : 0 : ts->type == BT_COMPLEX ? "c" : "", m->name, "f");
879 : 163 : else if (gfc_real_kinds[n].c_double)
880 : 0 : snprintf (name, sizeof (name), "%s%s",
881 : 0 : ts->type == BT_COMPLEX ? "c" : "", m->name);
882 : 163 : else if (gfc_real_kinds[n].c_long_double)
883 : 0 : snprintf (name, sizeof (name), "%s%s%s",
884 : 0 : ts->type == BT_COMPLEX ? "c" : "", m->name, "l");
885 : 163 : else if (gfc_real_kinds[n].c_float128)
886 : 163 : snprintf (name, sizeof (name), "%s%s%s",
887 : 163 : ts->type == BT_COMPLEX ? "c" : "", m->name,
888 : 163 : gfc_real_kinds[n].use_iec_60559 ? "f128" : "q");
889 : : else
890 : 0 : gcc_unreachable ();
891 : : }
892 : : else
893 : : {
894 : 354 : snprintf (name, sizeof (name), PREFIX ("%s_%c%d"), m->name,
895 : 177 : ts->type == BT_COMPLEX ? 'c' : 'r',
896 : : gfc_type_abi_kind (ts));
897 : : }
898 : :
899 : 340 : argtypes = NULL;
900 : 696 : for (actual = expr->value.function.actual; actual; actual = actual->next)
901 : : {
902 : 356 : type = gfc_typenode_for_spec (&actual->expr->ts);
903 : 356 : vec_safe_push (argtypes, type);
904 : : }
905 : 1020 : type = build_function_type_vec (gfc_typenode_for_spec (ts), argtypes);
906 : 340 : fndecl = build_decl (input_location,
907 : : FUNCTION_DECL, get_identifier (name), type);
908 : :
909 : : /* Mark the decl as external. */
910 : 340 : DECL_EXTERNAL (fndecl) = 1;
911 : 340 : TREE_PUBLIC (fndecl) = 1;
912 : :
913 : : /* Mark it __attribute__((const)), if possible. */
914 : 340 : TREE_READONLY (fndecl) = m->is_constant;
915 : :
916 : 340 : rest_of_decl_compilation (fndecl, 1, 0);
917 : :
918 : 340 : (*pdecl) = fndecl;
919 : 340 : return fndecl;
920 : : }
921 : :
922 : :
923 : : /* Convert an intrinsic function into an external or builtin call. */
924 : :
925 : : static void
926 : 3848 : gfc_conv_intrinsic_lib_function (gfc_se * se, gfc_expr * expr)
927 : : {
928 : 3848 : gfc_intrinsic_map_t *m;
929 : 3848 : tree fndecl;
930 : 3848 : tree rettype;
931 : 3848 : tree *args;
932 : 3848 : unsigned int num_args;
933 : 3848 : gfc_isym_id id;
934 : :
935 : 3848 : id = expr->value.function.isym->id;
936 : : /* Find the entry for this function. */
937 : 79049 : for (m = gfc_intrinsic_map;
938 : 79049 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
939 : : {
940 : 79049 : if (id == m->id)
941 : : break;
942 : : }
943 : :
944 : 3848 : if (m->id == GFC_ISYM_NONE)
945 : : {
946 : 0 : gfc_internal_error ("Intrinsic function %qs (%d) not recognized",
947 : : expr->value.function.name, id);
948 : : }
949 : :
950 : : /* Get the decl and generate the call. */
951 : 3848 : num_args = gfc_intrinsic_argument_list_length (expr);
952 : 3848 : args = XALLOCAVEC (tree, num_args);
953 : :
954 : 3848 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
955 : 3848 : fndecl = gfc_get_intrinsic_lib_fndecl (m, expr);
956 : 3848 : rettype = TREE_TYPE (TREE_TYPE (fndecl));
957 : :
958 : 3848 : fndecl = build_addr (fndecl);
959 : 3848 : se->expr = build_call_array_loc (input_location, rettype, fndecl, num_args, args);
960 : 3848 : }
961 : :
962 : :
963 : : /* If bounds-checking is enabled, create code to verify at runtime that the
964 : : string lengths for both expressions are the same (needed for e.g. MERGE).
965 : : If bounds-checking is not enabled, does nothing. */
966 : :
967 : : void
968 : 1470 : gfc_trans_same_strlen_check (const char* intr_name, locus* where,
969 : : tree a, tree b, stmtblock_t* target)
970 : : {
971 : 1470 : tree cond;
972 : 1470 : tree name;
973 : :
974 : : /* If bounds-checking is disabled, do nothing. */
975 : 1470 : if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
976 : : return;
977 : :
978 : : /* Compare the two string lengths. */
979 : 94 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, a, b);
980 : :
981 : : /* Output the runtime-check. */
982 : 94 : name = gfc_build_cstring_const (intr_name);
983 : 94 : name = gfc_build_addr_expr (pchar_type_node, name);
984 : 94 : gfc_trans_runtime_check (true, false, cond, target, where,
985 : : "Unequal character lengths (%ld/%ld) in %s",
986 : : fold_convert (long_integer_type_node, a),
987 : : fold_convert (long_integer_type_node, b), name);
988 : : }
989 : :
990 : :
991 : : /* The EXPONENT(X) intrinsic function is translated into
992 : : int ret;
993 : : return isfinite(X) ? (frexp (X, &ret) , ret) : huge
994 : : so that if X is a NaN or infinity, the result is HUGE(0).
995 : : */
996 : :
997 : : static void
998 : 228 : gfc_conv_intrinsic_exponent (gfc_se *se, gfc_expr *expr)
999 : : {
1000 : 228 : tree arg, type, res, tmp, frexp, cond, huge;
1001 : 228 : int i;
1002 : :
1003 : 456 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP,
1004 : 228 : expr->value.function.actual->expr->ts.kind);
1005 : :
1006 : 228 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
1007 : 228 : arg = gfc_evaluate_now (arg, &se->pre);
1008 : :
1009 : 228 : i = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
1010 : 228 : huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[i].huge, gfc_c_int_kind);
1011 : 228 : cond = build_call_expr_loc (input_location,
1012 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
1013 : : 1, arg);
1014 : :
1015 : 228 : res = gfc_create_var (integer_type_node, NULL);
1016 : 228 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
1017 : : gfc_build_addr_expr (NULL_TREE, res));
1018 : 228 : tmp = fold_build2_loc (input_location, COMPOUND_EXPR, integer_type_node,
1019 : : tmp, res);
1020 : 228 : se->expr = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
1021 : : cond, tmp, huge);
1022 : :
1023 : 228 : type = gfc_typenode_for_spec (&expr->ts);
1024 : 228 : se->expr = fold_convert (type, se->expr);
1025 : 228 : }
1026 : :
1027 : :
1028 : : static int caf_call_cnt = 0;
1029 : :
1030 : : static tree
1031 : 1161 : conv_caf_func_index (stmtblock_t *block, gfc_namespace *ns, const char *pat,
1032 : : gfc_expr *hash)
1033 : : {
1034 : 1161 : char *name;
1035 : 1161 : gfc_se argse;
1036 : 1161 : gfc_expr func_index;
1037 : 1161 : gfc_symtree *index_st;
1038 : 1161 : tree func_index_tree;
1039 : 1161 : stmtblock_t blk;
1040 : :
1041 : : /* Need to get namespace where static variables are possible. */
1042 : 1161 : while (ns && ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
1043 : 0 : ns = ns->parent;
1044 : 1161 : gcc_assert (ns);
1045 : :
1046 : 1161 : name = xasprintf (pat, caf_call_cnt);
1047 : 1161 : gcc_assert (!gfc_get_sym_tree (name, ns, &index_st, false));
1048 : 1161 : free (name);
1049 : :
1050 : 1161 : index_st->n.sym->attr.flavor = FL_VARIABLE;
1051 : 1161 : index_st->n.sym->attr.save = SAVE_EXPLICIT;
1052 : 1161 : index_st->n.sym->value
1053 : 1161 : = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
1054 : : &gfc_current_locus);
1055 : 1161 : mpz_init_set_si (index_st->n.sym->value->value.integer, -1);
1056 : 1161 : index_st->n.sym->ts.type = BT_INTEGER;
1057 : 1161 : index_st->n.sym->ts.kind = gfc_default_integer_kind;
1058 : 1161 : gfc_set_sym_referenced (index_st->n.sym);
1059 : 1161 : memset (&func_index, 0, sizeof (gfc_expr));
1060 : 1161 : gfc_clear_ts (&func_index.ts);
1061 : 1161 : func_index.expr_type = EXPR_VARIABLE;
1062 : 1161 : func_index.symtree = index_st;
1063 : 1161 : func_index.ts = index_st->n.sym->ts;
1064 : 1161 : gfc_commit_symbol (index_st->n.sym);
1065 : :
1066 : 1161 : gfc_init_se (&argse, NULL);
1067 : 1161 : gfc_conv_expr (&argse, &func_index);
1068 : 1161 : gfc_add_block_to_block (block, &argse.pre);
1069 : 1161 : func_index_tree = argse.expr;
1070 : :
1071 : 1161 : gfc_init_se (&argse, NULL);
1072 : 1161 : gfc_conv_expr (&argse, hash);
1073 : :
1074 : 1161 : gfc_init_block (&blk);
1075 : 1161 : gfc_add_modify (&blk, func_index_tree,
1076 : : build_call_expr (gfor_fndecl_caf_get_remote_function_index, 1,
1077 : : argse.expr));
1078 : 1161 : gfc_add_expr_to_block (
1079 : : block,
1080 : : build3 (COND_EXPR, void_type_node,
1081 : : gfc_likely (build2 (EQ_EXPR, logical_type_node, func_index_tree,
1082 : : build_int_cst (integer_type_node, -1)),
1083 : : PRED_FIRST_MATCH),
1084 : : gfc_finish_block (&blk), NULL_TREE));
1085 : :
1086 : 1161 : return func_index_tree;
1087 : : }
1088 : :
1089 : : static tree
1090 : 1161 : conv_caf_add_call_data (stmtblock_t *blk, gfc_namespace *ns, const char *pat,
1091 : : gfc_symbol *data_sym, tree *data_size)
1092 : : {
1093 : 1161 : char *name;
1094 : 1161 : gfc_symtree *data_st;
1095 : 1161 : gfc_constructor *con;
1096 : 1161 : gfc_expr data, data_init;
1097 : 1161 : gfc_se argse;
1098 : 1161 : tree data_tree;
1099 : :
1100 : 1161 : memset (&data, 0, sizeof (gfc_expr));
1101 : 1161 : gfc_clear_ts (&data.ts);
1102 : 1161 : data.expr_type = EXPR_VARIABLE;
1103 : 1161 : name = xasprintf (pat, caf_call_cnt);
1104 : 1161 : gcc_assert (!gfc_get_sym_tree (name, ns, &data_st, false));
1105 : 1161 : free (name);
1106 : 1161 : data_st->n.sym->attr.flavor = FL_VARIABLE;
1107 : 1161 : data_st->n.sym->ts = data_sym->ts;
1108 : 1161 : data.symtree = data_st;
1109 : 1161 : gfc_set_sym_referenced (data.symtree->n.sym);
1110 : 1161 : data.ts = data_st->n.sym->ts;
1111 : 1161 : gfc_commit_symbol (data_st->n.sym);
1112 : :
1113 : 1161 : memset (&data_init, 0, sizeof (gfc_expr));
1114 : 1161 : gfc_clear_ts (&data_init.ts);
1115 : 1161 : data_init.expr_type = EXPR_STRUCTURE;
1116 : 1161 : data_init.ts = data.ts;
1117 : 1359 : for (gfc_component *comp = data.ts.u.derived->components; comp;
1118 : 198 : comp = comp->next)
1119 : : {
1120 : 198 : con = gfc_constructor_get ();
1121 : 198 : con->expr = comp->initializer;
1122 : 198 : comp->initializer = NULL;
1123 : 198 : gfc_constructor_append (&data_init.value.constructor, con);
1124 : : }
1125 : :
1126 : 1161 : if (data.ts.u.derived->components)
1127 : : {
1128 : 67 : gfc_init_se (&argse, NULL);
1129 : 67 : gfc_conv_expr (&argse, &data);
1130 : 67 : data_tree = argse.expr;
1131 : 67 : gfc_add_expr_to_block (blk,
1132 : : gfc_trans_structure_assign (data_tree, &data_init,
1133 : : true, true));
1134 : 67 : gfc_constructor_free (data_init.value.constructor);
1135 : 67 : *data_size = TREE_TYPE (data_tree)->type_common.size_unit;
1136 : 67 : data_tree = gfc_build_addr_expr (pvoid_type_node, data_tree);
1137 : : }
1138 : : else
1139 : : {
1140 : 1094 : data_tree = build_zero_cst (pvoid_type_node);
1141 : 1094 : *data_size = build_zero_cst (size_type_node);
1142 : : }
1143 : :
1144 : 1161 : return data_tree;
1145 : : }
1146 : :
1147 : : static tree
1148 : 282 : conv_shape_to_cst (gfc_expr *e)
1149 : : {
1150 : 282 : tree tmp = NULL;
1151 : 760 : for (int d = 0; d < e->rank; ++d)
1152 : : {
1153 : 478 : if (!tmp)
1154 : 282 : tmp = gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind);
1155 : : else
1156 : 196 : tmp = fold_build2 (MULT_EXPR, TREE_TYPE (tmp), tmp,
1157 : : gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind));
1158 : : }
1159 : 282 : return fold_convert (size_type_node, tmp);
1160 : : }
1161 : :
1162 : : static void
1163 : 1029 : conv_stat_and_team (stmtblock_t *block, gfc_expr *expr, tree *stat, tree *team,
1164 : : tree *team_no)
1165 : : {
1166 : 1029 : gfc_expr *stat_e, *team_e;
1167 : :
1168 : 1029 : stat_e = gfc_find_stat_co (expr);
1169 : 1029 : if (stat_e)
1170 : : {
1171 : 22 : gfc_se stat_se;
1172 : 22 : gfc_init_se (&stat_se, NULL);
1173 : 22 : gfc_conv_expr_reference (&stat_se, stat_e);
1174 : 22 : *stat = stat_se.expr;
1175 : 22 : gfc_add_block_to_block (block, &stat_se.pre);
1176 : 22 : gfc_add_block_to_block (block, &stat_se.post);
1177 : : }
1178 : : else
1179 : 1007 : *stat = null_pointer_node;
1180 : :
1181 : 1029 : team_e = gfc_find_team_co (expr, TEAM_TEAM);
1182 : 1029 : if (team_e)
1183 : : {
1184 : 9 : gfc_se team_se;
1185 : 9 : gfc_init_se (&team_se, NULL);
1186 : 9 : gfc_conv_expr (&team_se, team_e);
1187 : 9 : *team
1188 : 9 : = gfc_build_addr_expr (NULL_TREE, gfc_trans_force_lval (&team_se.pre,
1189 : : team_se.expr));
1190 : 9 : gfc_add_block_to_block (block, &team_se.pre);
1191 : 9 : gfc_add_block_to_block (block, &team_se.post);
1192 : : }
1193 : : else
1194 : 1020 : *team = null_pointer_node;
1195 : :
1196 : 1029 : team_e = gfc_find_team_co (expr, TEAM_NUMBER);
1197 : 1029 : if (team_e)
1198 : : {
1199 : 15 : gfc_se team_se;
1200 : 15 : gfc_init_se (&team_se, NULL);
1201 : 15 : gfc_conv_expr (&team_se, team_e);
1202 : 15 : *team_no = gfc_build_addr_expr (
1203 : : NULL_TREE,
1204 : : gfc_trans_force_lval (&team_se.pre,
1205 : : fold_convert (integer_type_node, team_se.expr)));
1206 : 15 : gfc_add_block_to_block (block, &team_se.pre);
1207 : 15 : gfc_add_block_to_block (block, &team_se.post);
1208 : : }
1209 : : else
1210 : 1014 : *team_no = null_pointer_node;
1211 : 1029 : }
1212 : :
1213 : : /* Get data from a remote coarray. */
1214 : :
1215 : : static void
1216 : 894 : gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs,
1217 : : bool may_realloc, symbol_attribute *caf_attr)
1218 : : {
1219 : 894 : gfc_expr *array_expr;
1220 : 894 : tree caf_decl, token, image_index, tmp, res_var, type, stat, dest_size,
1221 : : dest_data, opt_dest_desc, get_fn_index_tree, add_data_tree, add_data_size,
1222 : : opt_src_desc, opt_src_charlen, opt_dest_charlen, team, team_no;
1223 : 894 : symbol_attribute caf_attr_store;
1224 : 894 : gfc_namespace *ns;
1225 : 894 : gfc_expr *get_fn_hash = expr->value.function.actual->next->expr,
1226 : 894 : *get_fn_expr = expr->value.function.actual->next->next->expr;
1227 : 894 : gfc_symbol *add_data_sym = get_fn_expr->symtree->n.sym->formal->sym;
1228 : :
1229 : 894 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1230 : :
1231 : 894 : if (se->ss && se->ss->info->useflags)
1232 : : {
1233 : : /* Access the previously obtained result. */
1234 : 352 : gfc_conv_tmp_array_ref (se);
1235 : 352 : return;
1236 : : }
1237 : :
1238 : 542 : array_expr = expr->value.function.actual->expr;
1239 : 542 : ns = array_expr->expr_type == EXPR_VARIABLE
1240 : 542 : && !array_expr->symtree->n.sym->attr.associate_var
1241 : 542 : ? array_expr->symtree->n.sym->ns
1242 : : : gfc_current_ns;
1243 : 542 : type = gfc_typenode_for_spec (&array_expr->ts);
1244 : :
1245 : 542 : if (caf_attr == NULL)
1246 : : {
1247 : 542 : caf_attr_store = gfc_caf_attr (array_expr);
1248 : 542 : caf_attr = &caf_attr_store;
1249 : : }
1250 : :
1251 : 542 : res_var = lhs;
1252 : :
1253 : 542 : conv_stat_and_team (&se->pre, expr, &stat, &team, &team_no);
1254 : :
1255 : 542 : get_fn_index_tree
1256 : 542 : = conv_caf_func_index (&se->pre, ns, "__caf_get_from_remote_fn_index_%d",
1257 : : get_fn_hash);
1258 : 542 : add_data_tree
1259 : 542 : = conv_caf_add_call_data (&se->pre, ns, "__caf_get_from_remote_add_data_%d",
1260 : : add_data_sym, &add_data_size);
1261 : 542 : ++caf_call_cnt;
1262 : :
1263 : 542 : if (array_expr->rank == 0)
1264 : : {
1265 : 189 : res_var = gfc_create_var (type, "caf_res");
1266 : 189 : if (array_expr->ts.type == BT_CHARACTER)
1267 : : {
1268 : 17 : gfc_conv_string_length (array_expr->ts.u.cl, array_expr, &se->pre);
1269 : 17 : se->string_length = array_expr->ts.u.cl->backend_decl;
1270 : 17 : opt_src_charlen = gfc_build_addr_expr (
1271 : : NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1272 : 17 : dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1273 : : }
1274 : : else
1275 : : {
1276 : 172 : dest_size = res_var->typed.type->type_common.size_unit;
1277 : 172 : opt_src_charlen
1278 : 172 : = build_zero_cst (build_pointer_type (size_type_node));
1279 : : }
1280 : 189 : dest_data
1281 : 189 : = gfc_evaluate_now (gfc_build_addr_expr (NULL_TREE, res_var), &se->pre);
1282 : 189 : res_var = build_fold_indirect_ref (dest_data);
1283 : 189 : dest_data = gfc_build_addr_expr (pvoid_type_node, dest_data);
1284 : 189 : opt_dest_desc = build_zero_cst (pvoid_type_node);
1285 : : }
1286 : : else
1287 : : {
1288 : : /* Create temporary. */
1289 : 353 : 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 : 353 : res_var = se->ss->info->data.array.descriptor;
1294 : 353 : if (array_expr->ts.type == BT_CHARACTER)
1295 : : {
1296 : 8 : se->string_length = array_expr->ts.u.cl->backend_decl;
1297 : 8 : opt_src_charlen = gfc_build_addr_expr (
1298 : : NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1299 : 8 : dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1300 : : }
1301 : : else
1302 : : {
1303 : 345 : opt_src_charlen
1304 : 345 : = build_zero_cst (build_pointer_type (size_type_node));
1305 : 345 : 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 : 353 : opt_dest_desc = res_var;
1316 : 353 : dest_data = gfc_conv_descriptor_data_get (res_var);
1317 : 353 : opt_dest_desc = gfc_build_addr_expr (NULL_TREE, opt_dest_desc);
1318 : 353 : if (may_realloc)
1319 : : {
1320 : 52 : tmp = gfc_conv_descriptor_data_get (res_var);
1321 : 52 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
1322 : : NULL_TREE, NULL_TREE, true, NULL,
1323 : : GFC_CAF_COARRAY_NOCOARRAY);
1324 : 52 : gfc_add_expr_to_block (&se->post, tmp);
1325 : : }
1326 : 353 : dest_data
1327 : 353 : = gfc_build_addr_expr (NULL_TREE,
1328 : : gfc_trans_force_lval (&se->pre, dest_data));
1329 : : }
1330 : :
1331 : 542 : opt_dest_charlen = opt_src_charlen;
1332 : 542 : caf_decl = gfc_get_tree_for_caf_expr (array_expr);
1333 : 542 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1334 : 1 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1335 : :
1336 : 542 : if (!TYPE_LANG_SPECIFIC (TREE_TYPE (caf_decl))->rank
1337 : 542 : || GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl)))
1338 : 473 : opt_src_desc = build_zero_cst (pvoid_type_node);
1339 : : else
1340 : 69 : opt_src_desc = gfc_build_addr_expr (pvoid_type_node, caf_decl);
1341 : :
1342 : 542 : image_index = gfc_caf_get_image_index (&se->pre, array_expr, caf_decl);
1343 : 542 : gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, array_expr);
1344 : :
1345 : : /* It guarantees memory consistency within the same segment. */
1346 : 542 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1347 : 542 : 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 : 542 : ASM_VOLATILE_P (tmp) = 1;
1351 : 542 : gfc_add_expr_to_block (&se->pre, tmp);
1352 : :
1353 : 542 : 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 : 542 : gfc_add_expr_to_block (&se->pre, tmp);
1360 : :
1361 : 542 : if (se->ss)
1362 : 353 : gfc_advance_se_ss_chain (se);
1363 : :
1364 : 542 : se->expr = res_var;
1365 : :
1366 : 542 : return;
1367 : : }
1368 : :
1369 : : /* Generate call to caf_is_present_on_remote for allocated (coarrary[...])
1370 : : calls. */
1371 : :
1372 : : static void
1373 : 132 : gfc_conv_intrinsic_caf_is_present_remote (gfc_se *se, gfc_expr *e)
1374 : : {
1375 : 132 : gfc_expr *caf_expr, *hash, *present_fn;
1376 : 132 : gfc_symbol *add_data_sym;
1377 : 132 : tree fn_index, add_data_tree, add_data_size, caf_decl, image_index, token;
1378 : :
1379 : 132 : gcc_assert (e->expr_type == EXPR_FUNCTION
1380 : : && e->value.function.isym->id
1381 : : == GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE);
1382 : 132 : caf_expr = e->value.function.actual->expr;
1383 : 132 : hash = e->value.function.actual->next->expr;
1384 : 132 : present_fn = e->value.function.actual->next->next->expr;
1385 : 132 : add_data_sym = present_fn->symtree->n.sym->formal->sym;
1386 : :
1387 : 132 : fn_index = conv_caf_func_index (&se->pre, e->symtree->n.sym->ns,
1388 : : "__caf_present_on_remote_fn_index_%d", hash);
1389 : 132 : 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 : 132 : ++caf_call_cnt;
1393 : :
1394 : 132 : caf_decl = gfc_get_tree_for_caf_expr (caf_expr);
1395 : 132 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1396 : 2 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1397 : :
1398 : 132 : image_index = gfc_caf_get_image_index (&se->pre, caf_expr, caf_decl);
1399 : 132 : gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, caf_expr);
1400 : :
1401 : 132 : se->expr
1402 : 132 : = 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 : 132 : }
1408 : :
1409 : : static tree
1410 : 293 : conv_caf_send_to_remote (gfc_code *code)
1411 : : {
1412 : 293 : gfc_expr *lhs_expr, *rhs_expr, *lhs_hash, *receiver_fn_expr;
1413 : 293 : gfc_symbol *add_data_sym;
1414 : 293 : gfc_se lhs_se, rhs_se;
1415 : 293 : stmtblock_t block;
1416 : 293 : gfc_namespace *ns;
1417 : 293 : tree caf_decl, token, rhs_size, image_index, tmp, rhs_data;
1418 : 293 : tree lhs_stat, lhs_team, lhs_team_no, opt_lhs_charlen, opt_rhs_charlen;
1419 : 293 : tree opt_lhs_desc = NULL_TREE, opt_rhs_desc = NULL_TREE;
1420 : 293 : tree receiver_fn_index_tree, add_data_tree, add_data_size;
1421 : :
1422 : 293 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1423 : 293 : gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SEND);
1424 : :
1425 : 293 : lhs_expr = code->ext.actual->expr;
1426 : 293 : rhs_expr = code->ext.actual->next->expr;
1427 : 293 : lhs_hash = code->ext.actual->next->next->expr;
1428 : 293 : receiver_fn_expr = code->ext.actual->next->next->next->expr;
1429 : 293 : add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1430 : :
1431 : 293 : ns = lhs_expr->expr_type == EXPR_VARIABLE
1432 : 293 : && !lhs_expr->symtree->n.sym->attr.associate_var
1433 : 293 : ? lhs_expr->symtree->n.sym->ns
1434 : : : gfc_current_ns;
1435 : :
1436 : 293 : gfc_init_block (&block);
1437 : :
1438 : : /* LHS. */
1439 : 293 : gfc_init_se (&lhs_se, NULL);
1440 : 293 : caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1441 : 293 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1442 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1443 : 293 : if (lhs_expr->rank == 0)
1444 : : {
1445 : 241 : if (lhs_expr->ts.type == BT_CHARACTER)
1446 : : {
1447 : 12 : gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1448 : 12 : lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1449 : 12 : opt_lhs_charlen = gfc_build_addr_expr (
1450 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1451 : : }
1452 : : else
1453 : 229 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1454 : 241 : opt_lhs_desc = null_pointer_node;
1455 : : }
1456 : : else
1457 : : {
1458 : 52 : gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1459 : 52 : gfc_add_block_to_block (&block, &lhs_se.pre);
1460 : 52 : opt_lhs_desc = lhs_se.expr;
1461 : 52 : if (lhs_expr->ts.type == BT_CHARACTER)
1462 : 22 : opt_lhs_charlen = gfc_build_addr_expr (
1463 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1464 : : else
1465 : 30 : 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 : 52 : tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1472 : : TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1473 : 52 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1474 : 28 : opt_lhs_desc = null_pointer_node;
1475 : : else
1476 : 24 : opt_lhs_desc
1477 : 24 : = 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 : 293 : image_index = gfc_caf_get_image_index (&block, lhs_expr, caf_decl);
1483 : 293 : gfc_get_caf_token_offset (&lhs_se, &token, NULL, caf_decl, NULL, lhs_expr);
1484 : :
1485 : : /* RHS. */
1486 : 293 : gfc_init_se (&rhs_se, NULL);
1487 : 293 : if (rhs_expr->rank == 0)
1488 : : {
1489 : 171 : rhs_se.want_pointer = rhs_expr->ts.type == BT_CHARACTER;
1490 : 171 : gfc_conv_expr (&rhs_se, rhs_expr);
1491 : 171 : gfc_add_block_to_block (&block, &rhs_se.pre);
1492 : 171 : opt_rhs_desc = null_pointer_node;
1493 : 171 : if (rhs_expr->ts.type == BT_CHARACTER)
1494 : : {
1495 : 20 : rhs_data
1496 : 20 : = rhs_expr->expr_type == EXPR_CONSTANT
1497 : 20 : ? gfc_build_addr_expr (NULL_TREE,
1498 : : gfc_trans_force_lval (&block,
1499 : : rhs_se.expr))
1500 : : : rhs_se.expr;
1501 : 20 : opt_rhs_charlen = gfc_build_addr_expr (
1502 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1503 : 20 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1504 : : }
1505 : : else
1506 : : {
1507 : 151 : rhs_data
1508 : 151 : = gfc_build_addr_expr (NULL_TREE,
1509 : : gfc_trans_force_lval (&block, rhs_se.expr));
1510 : 151 : opt_rhs_charlen
1511 : 151 : = build_zero_cst (build_pointer_type (size_type_node));
1512 : 151 : rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1513 : : }
1514 : : }
1515 : : else
1516 : : {
1517 : 244 : rhs_se.force_tmp = rhs_expr->shape == NULL
1518 : 122 : || !gfc_is_simply_contiguous (rhs_expr, false, false);
1519 : 122 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1520 : 122 : gfc_add_block_to_block (&block, &rhs_se.pre);
1521 : 122 : opt_rhs_desc = rhs_se.expr;
1522 : 122 : if (rhs_expr->ts.type == BT_CHARACTER)
1523 : : {
1524 : 14 : opt_rhs_charlen = gfc_build_addr_expr (
1525 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1526 : 14 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1527 : : }
1528 : : else
1529 : : {
1530 : 108 : opt_rhs_charlen
1531 : 108 : = build_zero_cst (build_pointer_type (size_type_node));
1532 : 108 : rhs_size = fold_build2 (
1533 : : MULT_EXPR, size_type_node,
1534 : : fold_convert (size_type_node,
1535 : : rhs_expr->shape
1536 : : ? conv_shape_to_cst (rhs_expr)
1537 : : : gfc_conv_descriptor_size (rhs_se.expr,
1538 : : rhs_expr->rank)),
1539 : : fold_convert (size_type_node,
1540 : : gfc_conv_descriptor_span_get (rhs_se.expr)));
1541 : : }
1542 : :
1543 : 122 : rhs_data = gfc_build_addr_expr (
1544 : : NULL_TREE, gfc_trans_force_lval (&block, gfc_conv_descriptor_data_get (
1545 : : opt_rhs_desc)));
1546 : 122 : opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1547 : : }
1548 : 293 : gfc_add_block_to_block (&block, &rhs_se.pre);
1549 : :
1550 : 293 : conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1551 : :
1552 : 293 : receiver_fn_index_tree
1553 : 293 : = conv_caf_func_index (&block, ns, "__caf_send_to_remote_fn_index_%d",
1554 : : lhs_hash);
1555 : 293 : add_data_tree
1556 : 293 : = conv_caf_add_call_data (&block, ns, "__caf_send_to_remote_add_data_%d",
1557 : : add_data_sym, &add_data_size);
1558 : 293 : ++caf_call_cnt;
1559 : :
1560 : 293 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_send_to_remote, 14,
1561 : : token, opt_lhs_desc, opt_lhs_charlen, image_index,
1562 : : rhs_size, rhs_data, opt_rhs_charlen, opt_rhs_desc,
1563 : : receiver_fn_index_tree, add_data_tree,
1564 : : add_data_size, lhs_stat, lhs_team, lhs_team_no);
1565 : :
1566 : 293 : gfc_add_expr_to_block (&block, tmp);
1567 : 293 : gfc_add_block_to_block (&block, &lhs_se.post);
1568 : 293 : gfc_add_block_to_block (&block, &rhs_se.post);
1569 : :
1570 : : /* It guarantees memory consistency within the same segment. */
1571 : 293 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1572 : 293 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1573 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1574 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1575 : 293 : ASM_VOLATILE_P (tmp) = 1;
1576 : 293 : gfc_add_expr_to_block (&block, tmp);
1577 : :
1578 : 293 : return gfc_finish_block (&block);
1579 : : }
1580 : :
1581 : : /* Send-get data to a remote coarray. */
1582 : :
1583 : : static tree
1584 : 97 : conv_caf_sendget (gfc_code *code)
1585 : : {
1586 : : /* lhs stuff */
1587 : 97 : gfc_expr *lhs_expr, *lhs_hash, *receiver_fn_expr;
1588 : 97 : gfc_symbol *lhs_add_data_sym;
1589 : 97 : gfc_se lhs_se;
1590 : 97 : tree lhs_caf_decl, lhs_token, opt_lhs_charlen,
1591 : 97 : opt_lhs_desc = NULL_TREE, receiver_fn_index_tree, lhs_image_index,
1592 : : lhs_add_data_tree, lhs_add_data_size, lhs_stat, lhs_team, lhs_team_no;
1593 : 97 : int transfer_rank;
1594 : :
1595 : : /* rhs stuff */
1596 : 97 : gfc_expr *rhs_expr, *rhs_hash, *sender_fn_expr;
1597 : 97 : gfc_symbol *rhs_add_data_sym;
1598 : 97 : gfc_se rhs_se;
1599 : 97 : tree rhs_caf_decl, rhs_token, opt_rhs_charlen,
1600 : 97 : opt_rhs_desc = NULL_TREE, sender_fn_index_tree, rhs_image_index,
1601 : : rhs_add_data_tree, rhs_add_data_size, rhs_stat, rhs_team, rhs_team_no;
1602 : :
1603 : : /* shared */
1604 : 97 : stmtblock_t block;
1605 : 97 : gfc_namespace *ns;
1606 : 97 : tree tmp, rhs_size;
1607 : :
1608 : 97 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1609 : 97 : gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SENDGET);
1610 : :
1611 : 97 : lhs_expr = code->ext.actual->expr;
1612 : 97 : rhs_expr = code->ext.actual->next->expr;
1613 : 97 : lhs_hash = code->ext.actual->next->next->expr;
1614 : 97 : receiver_fn_expr = code->ext.actual->next->next->next->expr;
1615 : 97 : rhs_hash = code->ext.actual->next->next->next->next->expr;
1616 : 97 : sender_fn_expr = code->ext.actual->next->next->next->next->next->expr;
1617 : :
1618 : 97 : lhs_add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1619 : 97 : rhs_add_data_sym = sender_fn_expr->symtree->n.sym->formal->sym;
1620 : :
1621 : 97 : ns = lhs_expr->expr_type == EXPR_VARIABLE
1622 : 97 : && !lhs_expr->symtree->n.sym->attr.associate_var
1623 : 97 : ? lhs_expr->symtree->n.sym->ns
1624 : : : gfc_current_ns;
1625 : :
1626 : 97 : gfc_init_block (&block);
1627 : :
1628 : 97 : lhs_stat = null_pointer_node;
1629 : 97 : lhs_team = null_pointer_node;
1630 : 97 : rhs_stat = null_pointer_node;
1631 : 97 : rhs_team = null_pointer_node;
1632 : :
1633 : : /* LHS. */
1634 : 97 : gfc_init_se (&lhs_se, NULL);
1635 : 97 : lhs_caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1636 : 97 : if (TREE_CODE (TREE_TYPE (lhs_caf_decl)) == REFERENCE_TYPE)
1637 : 0 : lhs_caf_decl = build_fold_indirect_ref_loc (input_location, lhs_caf_decl);
1638 : 97 : if (lhs_expr->rank == 0)
1639 : : {
1640 : 63 : if (lhs_expr->ts.type == BT_CHARACTER)
1641 : : {
1642 : 8 : gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1643 : 8 : lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1644 : 8 : opt_lhs_charlen = gfc_build_addr_expr (
1645 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1646 : : }
1647 : : else
1648 : 55 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1649 : 63 : opt_lhs_desc = null_pointer_node;
1650 : : }
1651 : : else
1652 : : {
1653 : 34 : gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1654 : 34 : gfc_add_block_to_block (&block, &lhs_se.pre);
1655 : 34 : opt_lhs_desc = lhs_se.expr;
1656 : 34 : if (lhs_expr->ts.type == BT_CHARACTER)
1657 : 16 : opt_lhs_charlen = gfc_build_addr_expr (
1658 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1659 : : else
1660 : 18 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1661 : : /* Get the third formal argument of the receiver function. (This is the
1662 : : location where to put the data on the remote image.) Need to look at
1663 : : the argument in the function decl, because in the gfc_symbol's formal
1664 : : argument an array may have no descriptor while in the generated
1665 : : function decl it has. */
1666 : 34 : tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1667 : : TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1668 : 34 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1669 : 30 : opt_lhs_desc = null_pointer_node;
1670 : : else
1671 : 4 : opt_lhs_desc
1672 : 4 : = gfc_build_addr_expr (NULL_TREE,
1673 : : gfc_trans_force_lval (&block, opt_lhs_desc));
1674 : : }
1675 : :
1676 : : /* Obtain token, offset and image index for the LHS. */
1677 : 97 : lhs_image_index = gfc_caf_get_image_index (&block, lhs_expr, lhs_caf_decl);
1678 : 97 : gfc_get_caf_token_offset (&lhs_se, &lhs_token, NULL, lhs_caf_decl, NULL,
1679 : : lhs_expr);
1680 : :
1681 : : /* RHS. */
1682 : 97 : rhs_caf_decl = gfc_get_tree_for_caf_expr (rhs_expr);
1683 : 97 : if (TREE_CODE (TREE_TYPE (rhs_caf_decl)) == REFERENCE_TYPE)
1684 : 0 : rhs_caf_decl = build_fold_indirect_ref_loc (input_location, rhs_caf_decl);
1685 : 97 : transfer_rank = rhs_expr->rank;
1686 : 97 : gfc_expression_rank (rhs_expr);
1687 : 97 : gfc_init_se (&rhs_se, NULL);
1688 : 97 : if (rhs_expr->rank == 0)
1689 : : {
1690 : 64 : opt_rhs_desc = null_pointer_node;
1691 : 64 : if (rhs_expr->ts.type == BT_CHARACTER)
1692 : : {
1693 : 16 : gfc_conv_expr (&rhs_se, rhs_expr);
1694 : 16 : gfc_add_block_to_block (&block, &rhs_se.pre);
1695 : 16 : opt_rhs_charlen = gfc_build_addr_expr (
1696 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1697 : 16 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1698 : : }
1699 : : else
1700 : : {
1701 : 48 : gfc_typespec *ts
1702 : 48 : = &sender_fn_expr->symtree->n.sym->formal->next->next->sym->ts;
1703 : :
1704 : 48 : opt_rhs_charlen
1705 : 48 : = build_zero_cst (build_pointer_type (size_type_node));
1706 : 48 : rhs_size = gfc_typenode_for_spec (ts)->type_common.size_unit;
1707 : : }
1708 : : }
1709 : : /* Get the fifth formal argument of the getter function. This is the argument
1710 : : pointing to the data to get on the remote image. Need to look at the
1711 : : argument in the function decl, because in the gfc_symbol's formal argument
1712 : : an array may have no descriptor while in the generated function decl it
1713 : : has. */
1714 : 33 : else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_VALUE (
1715 : : TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1716 : : TREE_TYPE (sender_fn_expr->symtree->n.sym->backend_decl))))))))))
1717 : : {
1718 : 29 : rhs_se.data_not_needed = 1;
1719 : 29 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1720 : 29 : gfc_add_block_to_block (&block, &rhs_se.pre);
1721 : 29 : if (rhs_expr->ts.type == BT_CHARACTER)
1722 : : {
1723 : 8 : opt_rhs_charlen = gfc_build_addr_expr (
1724 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1725 : 8 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1726 : : }
1727 : : else
1728 : : {
1729 : 21 : opt_rhs_charlen
1730 : 21 : = build_zero_cst (build_pointer_type (size_type_node));
1731 : 21 : rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1732 : : }
1733 : 29 : opt_rhs_desc = null_pointer_node;
1734 : : }
1735 : : else
1736 : : {
1737 : 4 : gfc_ref *arr_ref = rhs_expr->ref;
1738 : 4 : while (arr_ref && arr_ref->type != REF_ARRAY)
1739 : 0 : arr_ref = arr_ref->next;
1740 : 4 : rhs_se.force_tmp
1741 : 8 : = (rhs_expr->shape == NULL
1742 : 4 : && (!arr_ref || !gfc_full_array_ref_p (arr_ref, nullptr)))
1743 : 8 : || !gfc_is_simply_contiguous (rhs_expr, false, false);
1744 : 4 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1745 : 4 : gfc_add_block_to_block (&block, &rhs_se.pre);
1746 : 4 : opt_rhs_desc = rhs_se.expr;
1747 : 4 : if (rhs_expr->ts.type == BT_CHARACTER)
1748 : : {
1749 : 0 : opt_rhs_charlen = gfc_build_addr_expr (
1750 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1751 : 0 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1752 : : }
1753 : : else
1754 : : {
1755 : 4 : opt_rhs_charlen
1756 : 4 : = build_zero_cst (build_pointer_type (size_type_node));
1757 : 4 : rhs_size = fold_build2 (
1758 : : MULT_EXPR, size_type_node,
1759 : : fold_convert (size_type_node,
1760 : : rhs_expr->shape
1761 : : ? conv_shape_to_cst (rhs_expr)
1762 : : : gfc_conv_descriptor_size (rhs_se.expr,
1763 : : rhs_expr->rank)),
1764 : : fold_convert (size_type_node,
1765 : : gfc_conv_descriptor_span_get (rhs_se.expr)));
1766 : : }
1767 : :
1768 : 4 : opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1769 : : }
1770 : 97 : gfc_add_block_to_block (&block, &rhs_se.pre);
1771 : :
1772 : : /* Obtain token, offset and image index for the RHS. */
1773 : 97 : rhs_image_index = gfc_caf_get_image_index (&block, rhs_expr, rhs_caf_decl);
1774 : 97 : gfc_get_caf_token_offset (&rhs_se, &rhs_token, NULL, rhs_caf_decl, NULL,
1775 : : rhs_expr);
1776 : :
1777 : : /* stat and team. */
1778 : 97 : conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1779 : 97 : conv_stat_and_team (&block, rhs_expr, &rhs_stat, &rhs_team, &rhs_team_no);
1780 : :
1781 : 97 : sender_fn_index_tree
1782 : 97 : = conv_caf_func_index (&block, ns, "__caf_transfer_from_fn_index_%d",
1783 : : rhs_hash);
1784 : 97 : rhs_add_data_tree
1785 : 97 : = conv_caf_add_call_data (&block, ns,
1786 : : "__caf_transfer_from_remote_add_data_%d",
1787 : : rhs_add_data_sym, &rhs_add_data_size);
1788 : 97 : receiver_fn_index_tree
1789 : 97 : = conv_caf_func_index (&block, ns, "__caf_transfer_to_remote_fn_index_%d",
1790 : : lhs_hash);
1791 : 97 : lhs_add_data_tree
1792 : 97 : = conv_caf_add_call_data (&block, ns,
1793 : : "__caf_transfer_to_remote_add_data_%d",
1794 : : lhs_add_data_sym, &lhs_add_data_size);
1795 : 97 : ++caf_call_cnt;
1796 : :
1797 : 97 : tmp = build_call_expr_loc (
1798 : : input_location, gfor_fndecl_caf_transfer_between_remotes, 22, lhs_token,
1799 : : opt_lhs_desc, opt_lhs_charlen, lhs_image_index, receiver_fn_index_tree,
1800 : : lhs_add_data_tree, lhs_add_data_size, rhs_token, opt_rhs_desc,
1801 : : opt_rhs_charlen, rhs_image_index, sender_fn_index_tree, rhs_add_data_tree,
1802 : : rhs_add_data_size, rhs_size,
1803 : : transfer_rank == 0 ? boolean_true_node : boolean_false_node, lhs_stat,
1804 : : rhs_stat, lhs_team, lhs_team_no, rhs_team, rhs_team_no);
1805 : :
1806 : 97 : gfc_add_expr_to_block (&block, tmp);
1807 : 97 : gfc_add_block_to_block (&block, &lhs_se.post);
1808 : 97 : gfc_add_block_to_block (&block, &rhs_se.post);
1809 : :
1810 : : /* It guarantees memory consistency within the same segment. */
1811 : 97 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1812 : 97 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1813 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1814 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1815 : 97 : ASM_VOLATILE_P (tmp) = 1;
1816 : 97 : gfc_add_expr_to_block (&block, tmp);
1817 : :
1818 : 97 : return gfc_finish_block (&block);
1819 : : }
1820 : :
1821 : :
1822 : : static void
1823 : 727 : trans_this_image (gfc_se * se, gfc_expr *expr)
1824 : : {
1825 : 727 : stmtblock_t loop;
1826 : 727 : tree type, desc, dim_arg, cond, tmp, m, loop_var, exit_label, min_var, lbound,
1827 : : ubound, extent, ml, team;
1828 : 727 : gfc_se argse;
1829 : 727 : int rank, corank;
1830 : :
1831 : : /* The case -fcoarray=single is handled elsewhere. */
1832 : 727 : gcc_assert (flag_coarray != GFC_FCOARRAY_SINGLE);
1833 : :
1834 : : /* Translate team, if present. */
1835 : 727 : if (expr->value.function.actual->next->next->expr)
1836 : : {
1837 : 18 : gfc_init_se (&argse, NULL);
1838 : 18 : gfc_conv_expr_val (&argse, expr->value.function.actual->next->next->expr);
1839 : 18 : gfc_add_block_to_block (&se->pre, &argse.pre);
1840 : 18 : gfc_add_block_to_block (&se->post, &argse.post);
1841 : 18 : team = fold_convert (pvoid_type_node, argse.expr);
1842 : : }
1843 : : else
1844 : 709 : team = null_pointer_node;
1845 : :
1846 : : /* Argument-free version: THIS_IMAGE(). */
1847 : 727 : if (expr->value.function.actual->expr == NULL)
1848 : : {
1849 : 556 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
1850 : : team);
1851 : 556 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
1852 : : tmp);
1853 : 560 : return;
1854 : : }
1855 : :
1856 : : /* Coarray-argument version: THIS_IMAGE(coarray [, dim]). */
1857 : :
1858 : 171 : type = gfc_get_int_type (gfc_default_integer_kind);
1859 : 171 : corank = expr->value.function.actual->expr->corank;
1860 : 171 : rank = expr->value.function.actual->expr->rank;
1861 : :
1862 : : /* Obtain the descriptor of the COARRAY. */
1863 : 171 : gfc_init_se (&argse, NULL);
1864 : 171 : argse.want_coarray = 1;
1865 : 171 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
1866 : 171 : gfc_add_block_to_block (&se->pre, &argse.pre);
1867 : 171 : gfc_add_block_to_block (&se->post, &argse.post);
1868 : 171 : desc = argse.expr;
1869 : :
1870 : 171 : if (se->ss)
1871 : : {
1872 : : /* Create an implicit second parameter from the loop variable. */
1873 : 41 : gcc_assert (!expr->value.function.actual->next->expr);
1874 : 41 : gcc_assert (corank > 0);
1875 : 41 : gcc_assert (se->loop->dimen == 1);
1876 : 41 : gcc_assert (se->ss->info->expr == expr);
1877 : :
1878 : 41 : dim_arg = se->loop->loopvar[0];
1879 : 41 : dim_arg = fold_build2_loc (input_location, PLUS_EXPR,
1880 : : gfc_array_index_type, dim_arg,
1881 : 41 : build_int_cst (TREE_TYPE (dim_arg), 1));
1882 : 41 : gfc_advance_se_ss_chain (se);
1883 : : }
1884 : : else
1885 : : {
1886 : : /* Use the passed DIM= argument. */
1887 : 130 : gcc_assert (expr->value.function.actual->next->expr);
1888 : 130 : gfc_init_se (&argse, NULL);
1889 : 130 : gfc_conv_expr_type (&argse, expr->value.function.actual->next->expr,
1890 : : gfc_array_index_type);
1891 : 130 : gfc_add_block_to_block (&se->pre, &argse.pre);
1892 : 130 : dim_arg = argse.expr;
1893 : :
1894 : 130 : if (INTEGER_CST_P (dim_arg))
1895 : : {
1896 : 72 : if (wi::ltu_p (wi::to_wide (dim_arg), 1)
1897 : 144 : || wi::gtu_p (wi::to_wide (dim_arg),
1898 : 72 : GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
1899 : 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
1900 : 0 : "dimension index", expr->value.function.isym->name,
1901 : : &expr->where);
1902 : : }
1903 : 58 : else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1904 : : {
1905 : 0 : dim_arg = gfc_evaluate_now (dim_arg, &se->pre);
1906 : 0 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1907 : : dim_arg,
1908 : 0 : build_int_cst (TREE_TYPE (dim_arg), 1));
1909 : 0 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
1910 : 0 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
1911 : : dim_arg, tmp);
1912 : 0 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
1913 : : logical_type_node, cond, tmp);
1914 : 0 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
1915 : : gfc_msg_fault);
1916 : : }
1917 : : }
1918 : :
1919 : : /* Used algorithm; cf. Fortran 2008, C.10. Note, due to the scalarizer,
1920 : : one always has a dim_arg argument.
1921 : :
1922 : : m = this_image() - 1
1923 : : if (corank == 1)
1924 : : {
1925 : : sub(1) = m + lcobound(corank)
1926 : : return;
1927 : : }
1928 : : i = rank
1929 : : min_var = min (rank + corank - 2, rank + dim_arg - 1)
1930 : : for (;;)
1931 : : {
1932 : : extent = gfc_extent(i)
1933 : : ml = m
1934 : : m = m/extent
1935 : : if (i >= min_var)
1936 : : goto exit_label
1937 : : i++
1938 : : }
1939 : : exit_label:
1940 : : sub(dim_arg) = (dim_arg < corank) ? ml - m*extent + lcobound(dim_arg)
1941 : : : m + lcobound(corank)
1942 : : */
1943 : :
1944 : : /* this_image () - 1. */
1945 : 171 : tmp
1946 : 171 : = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1, team);
1947 : 171 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
1948 : : fold_convert (type, tmp), build_int_cst (type, 1));
1949 : 171 : if (corank == 1)
1950 : : {
1951 : : /* sub(1) = m + lcobound(corank). */
1952 : 4 : lbound = gfc_conv_descriptor_lbound_get (desc,
1953 : 4 : build_int_cst (TREE_TYPE (gfc_array_index_type),
1954 : 4 : corank+rank-1));
1955 : 4 : lbound = fold_convert (type, lbound);
1956 : 4 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
1957 : :
1958 : 4 : se->expr = tmp;
1959 : 4 : return;
1960 : : }
1961 : :
1962 : 167 : m = gfc_create_var (type, NULL);
1963 : 167 : ml = gfc_create_var (type, NULL);
1964 : 167 : loop_var = gfc_create_var (integer_type_node, NULL);
1965 : 167 : min_var = gfc_create_var (integer_type_node, NULL);
1966 : :
1967 : : /* m = this_image () - 1. */
1968 : 167 : gfc_add_modify (&se->pre, m, tmp);
1969 : :
1970 : : /* min_var = min (rank + corank-2, rank + dim_arg - 1). */
1971 : 167 : tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1972 : : fold_convert (integer_type_node, dim_arg),
1973 : 167 : build_int_cst (integer_type_node, rank - 1));
1974 : 167 : tmp = fold_build2_loc (input_location, MIN_EXPR, integer_type_node,
1975 : 167 : build_int_cst (integer_type_node, rank + corank - 2),
1976 : : tmp);
1977 : 167 : gfc_add_modify (&se->pre, min_var, tmp);
1978 : :
1979 : : /* i = rank. */
1980 : 167 : tmp = build_int_cst (integer_type_node, rank);
1981 : 167 : gfc_add_modify (&se->pre, loop_var, tmp);
1982 : :
1983 : 167 : exit_label = gfc_build_label_decl (NULL_TREE);
1984 : 167 : TREE_USED (exit_label) = 1;
1985 : :
1986 : : /* Loop body. */
1987 : 167 : gfc_init_block (&loop);
1988 : :
1989 : : /* ml = m. */
1990 : 167 : gfc_add_modify (&loop, ml, m);
1991 : :
1992 : : /* extent = ... */
1993 : 167 : lbound = gfc_conv_descriptor_lbound_get (desc, loop_var);
1994 : 167 : ubound = gfc_conv_descriptor_ubound_get (desc, loop_var);
1995 : 167 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1996 : 167 : extent = fold_convert (type, extent);
1997 : :
1998 : : /* m = m/extent. */
1999 : 167 : gfc_add_modify (&loop, m,
2000 : : fold_build2_loc (input_location, TRUNC_DIV_EXPR, type,
2001 : : m, extent));
2002 : :
2003 : : /* Exit condition: if (i >= min_var) goto exit_label. */
2004 : 167 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, loop_var,
2005 : : min_var);
2006 : 167 : tmp = build1_v (GOTO_EXPR, exit_label);
2007 : 167 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
2008 : : build_empty_stmt (input_location));
2009 : 167 : gfc_add_expr_to_block (&loop, tmp);
2010 : :
2011 : : /* Increment loop variable: i++. */
2012 : 167 : gfc_add_modify (&loop, loop_var,
2013 : : fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
2014 : : loop_var,
2015 : : integer_one_node));
2016 : :
2017 : : /* Making the loop... actually loop! */
2018 : 167 : tmp = gfc_finish_block (&loop);
2019 : 167 : tmp = build1_v (LOOP_EXPR, tmp);
2020 : 167 : gfc_add_expr_to_block (&se->pre, tmp);
2021 : :
2022 : : /* The exit label. */
2023 : 167 : tmp = build1_v (LABEL_EXPR, exit_label);
2024 : 167 : gfc_add_expr_to_block (&se->pre, tmp);
2025 : :
2026 : : /* sub(co_dim) = (co_dim < corank) ? ml - m*extent + lcobound(dim_arg)
2027 : : : m + lcobound(corank) */
2028 : :
2029 : 167 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, dim_arg,
2030 : 167 : build_int_cst (TREE_TYPE (dim_arg), corank));
2031 : :
2032 : 167 : lbound = gfc_conv_descriptor_lbound_get (desc,
2033 : : fold_build2_loc (input_location, PLUS_EXPR,
2034 : : gfc_array_index_type, dim_arg,
2035 : 167 : build_int_cst (TREE_TYPE (dim_arg), rank-1)));
2036 : 167 : lbound = fold_convert (type, lbound);
2037 : :
2038 : 167 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type, ml,
2039 : : fold_build2_loc (input_location, MULT_EXPR, type,
2040 : : m, extent));
2041 : 167 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
2042 : :
2043 : 167 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
2044 : : fold_build2_loc (input_location, PLUS_EXPR, type,
2045 : : m, lbound));
2046 : : }
2047 : :
2048 : :
2049 : : /* Convert a call to image_status. */
2050 : :
2051 : : static void
2052 : 16 : conv_intrinsic_image_status (gfc_se *se, gfc_expr *expr)
2053 : : {
2054 : 16 : unsigned int num_args;
2055 : 16 : tree *args, tmp;
2056 : :
2057 : 16 : num_args = gfc_intrinsic_argument_list_length (expr);
2058 : 16 : args = XALLOCAVEC (tree, num_args);
2059 : 16 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2060 : : /* In args[0] the number of the image the status is desired for has to be
2061 : : given. */
2062 : :
2063 : 16 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
2064 : : {
2065 : 0 : tree arg;
2066 : 0 : arg = gfc_evaluate_now (args[0], &se->pre);
2067 : 0 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2068 : : fold_convert (integer_type_node, arg),
2069 : : integer_one_node);
2070 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
2071 : : tmp, integer_zero_node,
2072 : : build_int_cst (integer_type_node,
2073 : : GFC_STAT_STOPPED_IMAGE));
2074 : : }
2075 : 16 : else if (flag_coarray == GFC_FCOARRAY_LIB)
2076 : 16 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_image_status, 2,
2077 : : args[0],
2078 : : num_args < 2 ? null_pointer_node : args[1]);
2079 : : else
2080 : 0 : gcc_unreachable ();
2081 : :
2082 : 16 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2083 : 16 : }
2084 : :
2085 : : static void
2086 : 19 : conv_intrinsic_team_number (gfc_se *se, gfc_expr *expr)
2087 : : {
2088 : 19 : unsigned int num_args;
2089 : :
2090 : 19 : tree *args, tmp;
2091 : :
2092 : 19 : num_args = gfc_intrinsic_argument_list_length (expr);
2093 : 19 : args = XALLOCAVEC (tree, num_args);
2094 : 19 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2095 : :
2096 : 19 : if (flag_coarray ==
2097 : 18 : GFC_FCOARRAY_SINGLE && expr->value.function.actual->expr)
2098 : 0 : tmp = gfc_evaluate_now (args[0], &se->pre);
2099 : 19 : else if (flag_coarray == GFC_FCOARRAY_SINGLE)
2100 : : {
2101 : : // the value -1 represents that no team has been created yet
2102 : 18 : tmp = build_int_cst (integer_type_node, -1);
2103 : : }
2104 : 1 : else if (flag_coarray == GFC_FCOARRAY_LIB && expr->value.function.actual->expr)
2105 : 0 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
2106 : : args[0]);
2107 : 1 : else if (flag_coarray == GFC_FCOARRAY_LIB)
2108 : 1 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
2109 : : null_pointer_node);
2110 : : else
2111 : 0 : gcc_unreachable ();
2112 : :
2113 : 19 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2114 : 19 : }
2115 : :
2116 : :
2117 : : static void
2118 : 152 : trans_image_index (gfc_se * se, gfc_expr *expr)
2119 : : {
2120 : 152 : tree num_images, cond, coindex, type, lbound, ubound, desc, subdesc, tmp,
2121 : 152 : invalid_bound, team = null_pointer_node, team_number = null_pointer_node;
2122 : 152 : gfc_se argse, subse;
2123 : 152 : int rank, corank, codim;
2124 : :
2125 : 152 : type = gfc_get_int_type (gfc_default_integer_kind);
2126 : 152 : corank = expr->value.function.actual->expr->corank;
2127 : 152 : rank = expr->value.function.actual->expr->rank;
2128 : :
2129 : : /* Obtain the descriptor of the COARRAY. */
2130 : 152 : gfc_init_se (&argse, NULL);
2131 : 152 : argse.want_coarray = 1;
2132 : 152 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2133 : 152 : gfc_add_block_to_block (&se->pre, &argse.pre);
2134 : 152 : gfc_add_block_to_block (&se->post, &argse.post);
2135 : 152 : desc = argse.expr;
2136 : :
2137 : : /* Obtain a handle to the SUB argument. */
2138 : 152 : gfc_init_se (&subse, NULL);
2139 : 152 : gfc_conv_expr_descriptor (&subse, expr->value.function.actual->next->expr);
2140 : 152 : gfc_add_block_to_block (&se->pre, &subse.pre);
2141 : 152 : gfc_add_block_to_block (&se->post, &subse.post);
2142 : 152 : subdesc = build_fold_indirect_ref_loc (input_location,
2143 : : gfc_conv_descriptor_data_get (subse.expr));
2144 : :
2145 : 152 : if (expr->value.function.actual->next->next->expr)
2146 : : {
2147 : 0 : gfc_init_se (&argse, NULL);
2148 : 0 : gfc_conv_expr_descriptor (&argse,
2149 : 0 : expr->value.function.actual->next->next->expr);
2150 : 0 : if (expr->value.function.actual->next->next->expr->ts.type == BT_DERIVED)
2151 : 0 : team = argse.expr;
2152 : : else
2153 : 0 : team_number = gfc_build_addr_expr (
2154 : : NULL_TREE,
2155 : : gfc_trans_force_lval (&argse.pre,
2156 : : fold_convert (integer_type_node, argse.expr)));
2157 : 0 : gfc_add_block_to_block (&se->pre, &argse.pre);
2158 : 0 : gfc_add_block_to_block (&se->post, &argse.post);
2159 : : }
2160 : :
2161 : : /* Fortran 2008 does not require that the values remain in the cobounds,
2162 : : thus we need explicitly check this - and return 0 if they are exceeded. */
2163 : :
2164 : 152 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2165 : 152 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1], NULL);
2166 : 152 : invalid_bound = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2167 : : fold_convert (gfc_array_index_type, tmp),
2168 : : lbound);
2169 : :
2170 : 352 : for (codim = corank + rank - 2; codim >= rank; codim--)
2171 : : {
2172 : 200 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2173 : 200 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2174 : 200 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2175 : 200 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2176 : : fold_convert (gfc_array_index_type, tmp),
2177 : : lbound);
2178 : 200 : invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2179 : : logical_type_node, invalid_bound, cond);
2180 : 200 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2181 : : fold_convert (gfc_array_index_type, tmp),
2182 : : ubound);
2183 : 200 : invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2184 : : logical_type_node, invalid_bound, cond);
2185 : : }
2186 : :
2187 : 152 : invalid_bound = gfc_unlikely (invalid_bound, PRED_FORTRAN_INVALID_BOUND);
2188 : :
2189 : : /* See Fortran 2008, C.10 for the following algorithm. */
2190 : :
2191 : : /* coindex = sub(corank) - lcobound(n). */
2192 : 152 : coindex = fold_convert (gfc_array_index_type,
2193 : : gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1],
2194 : : NULL));
2195 : 152 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2196 : 152 : coindex = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2197 : : fold_convert (gfc_array_index_type, coindex),
2198 : : lbound);
2199 : :
2200 : 352 : for (codim = corank + rank - 2; codim >= rank; codim--)
2201 : : {
2202 : 200 : tree extent, ubound;
2203 : :
2204 : : /* coindex = coindex*extent(codim) + sub(codim) - lcobound(codim). */
2205 : 200 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2206 : 200 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2207 : 200 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2208 : :
2209 : : /* coindex *= extent. */
2210 : 200 : coindex = fold_build2_loc (input_location, MULT_EXPR,
2211 : : gfc_array_index_type, coindex, extent);
2212 : :
2213 : : /* coindex += sub(codim). */
2214 : 200 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2215 : 200 : coindex = fold_build2_loc (input_location, PLUS_EXPR,
2216 : : gfc_array_index_type, coindex,
2217 : : fold_convert (gfc_array_index_type, tmp));
2218 : :
2219 : : /* coindex -= lbound(codim). */
2220 : 200 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2221 : 200 : coindex = fold_build2_loc (input_location, MINUS_EXPR,
2222 : : gfc_array_index_type, coindex, lbound);
2223 : : }
2224 : :
2225 : 152 : coindex = fold_build2_loc (input_location, PLUS_EXPR, type,
2226 : : fold_convert(type, coindex),
2227 : : build_int_cst (type, 1));
2228 : :
2229 : : /* Return 0 if "coindex" exceeds num_images(). */
2230 : :
2231 : 152 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
2232 : 108 : num_images = build_int_cst (type, 1);
2233 : : else
2234 : : {
2235 : 44 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2236 : : team, team_number);
2237 : 44 : num_images = fold_convert (type, tmp);
2238 : : }
2239 : :
2240 : 152 : tmp = gfc_create_var (type, NULL);
2241 : 152 : gfc_add_modify (&se->pre, tmp, coindex);
2242 : :
2243 : 152 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, tmp,
2244 : : num_images);
2245 : 152 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
2246 : : cond,
2247 : : fold_convert (logical_type_node, invalid_bound));
2248 : 152 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
2249 : : build_int_cst (type, 0), tmp);
2250 : 152 : }
2251 : :
2252 : : static void
2253 : 429 : trans_num_images (gfc_se * se, gfc_expr *expr)
2254 : : {
2255 : 429 : tree tmp, team = null_pointer_node, team_number = null_pointer_node;
2256 : 429 : gfc_se argse;
2257 : :
2258 : 429 : if (expr->value.function.actual->expr)
2259 : : {
2260 : 18 : gfc_init_se (&argse, NULL);
2261 : 18 : gfc_conv_expr_val (&argse, expr->value.function.actual->expr);
2262 : 18 : if (expr->value.function.actual->expr->ts.type == BT_DERIVED)
2263 : 6 : team = argse.expr;
2264 : : else
2265 : 12 : team_number = gfc_build_addr_expr (
2266 : : NULL_TREE,
2267 : : gfc_trans_force_lval (&se->pre,
2268 : : fold_convert (integer_type_node, argse.expr)));
2269 : 18 : gfc_add_block_to_block (&se->pre, &argse.pre);
2270 : 18 : gfc_add_block_to_block (&se->post, &argse.post);
2271 : : }
2272 : :
2273 : 429 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2274 : : team, team_number);
2275 : 429 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2276 : 429 : }
2277 : :
2278 : :
2279 : : static void
2280 : 10512 : gfc_conv_intrinsic_rank (gfc_se *se, gfc_expr *expr)
2281 : : {
2282 : 10512 : gfc_se argse;
2283 : :
2284 : 10512 : gfc_init_se (&argse, NULL);
2285 : 10512 : argse.data_not_needed = 1;
2286 : 10512 : argse.descriptor_only = 1;
2287 : :
2288 : 10512 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2289 : 10512 : gfc_add_block_to_block (&se->pre, &argse.pre);
2290 : 10512 : gfc_add_block_to_block (&se->post, &argse.post);
2291 : :
2292 : 10512 : se->expr = gfc_conv_descriptor_rank (argse.expr);
2293 : 10512 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
2294 : : se->expr);
2295 : 10512 : }
2296 : :
2297 : :
2298 : : static void
2299 : 621 : gfc_conv_intrinsic_is_contiguous (gfc_se * se, gfc_expr * expr)
2300 : : {
2301 : 621 : gfc_expr *arg;
2302 : 621 : arg = expr->value.function.actual->expr;
2303 : 621 : gfc_conv_is_contiguous_expr (se, arg);
2304 : 621 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
2305 : 621 : }
2306 : :
2307 : : /* This function does the work for gfc_conv_intrinsic_is_contiguous,
2308 : : plus it can be called directly. */
2309 : :
2310 : : void
2311 : 1964 : gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
2312 : : {
2313 : 1964 : gfc_ss *ss;
2314 : 1964 : gfc_se argse;
2315 : 1964 : tree desc, tmp, stride, extent, cond;
2316 : 1964 : int i;
2317 : 1964 : tree fncall0;
2318 : 1964 : gfc_array_spec *as;
2319 : :
2320 : 1964 : if (arg->ts.type == BT_CLASS)
2321 : 36 : gfc_add_class_array_ref (arg);
2322 : :
2323 : 1964 : ss = gfc_walk_expr (arg);
2324 : 1964 : gcc_assert (ss != gfc_ss_terminator);
2325 : 1964 : gfc_init_se (&argse, NULL);
2326 : 1964 : argse.data_not_needed = 1;
2327 : 1964 : gfc_conv_expr_descriptor (&argse, arg);
2328 : :
2329 : 1964 : as = gfc_get_full_arrayspec_from_expr (arg);
2330 : :
2331 : : /* Create: stride[0] == 1 && stride[1] == extend[0]*stride[0] && ...
2332 : : Note in addition that zero-sized arrays don't count as contiguous. */
2333 : :
2334 : 1964 : if (as && as->type == AS_ASSUMED_RANK)
2335 : : {
2336 : : /* Build the call to is_contiguous0. */
2337 : 243 : argse.want_pointer = 1;
2338 : 243 : gfc_conv_expr_descriptor (&argse, arg);
2339 : 243 : gfc_add_block_to_block (&se->pre, &argse.pre);
2340 : 243 : gfc_add_block_to_block (&se->post, &argse.post);
2341 : 243 : desc = gfc_evaluate_now (argse.expr, &se->pre);
2342 : 243 : fncall0 = build_call_expr_loc (input_location,
2343 : : gfor_fndecl_is_contiguous0, 1, desc);
2344 : 243 : se->expr = fncall0;
2345 : 243 : se->expr = convert (logical_type_node, se->expr);
2346 : : }
2347 : : else
2348 : : {
2349 : 1721 : gfc_add_block_to_block (&se->pre, &argse.pre);
2350 : 1721 : gfc_add_block_to_block (&se->post, &argse.post);
2351 : 1721 : desc = gfc_evaluate_now (argse.expr, &se->pre);
2352 : :
2353 : 1721 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[0]);
2354 : 1721 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2355 : 1721 : stride, build_int_cst (TREE_TYPE (stride), 1));
2356 : :
2357 : 2047 : for (i = 0; i < arg->rank - 1; i++)
2358 : : {
2359 : 326 : tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2360 : 326 : extent = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2361 : 326 : extent = fold_build2_loc (input_location, MINUS_EXPR,
2362 : : gfc_array_index_type, extent, tmp);
2363 : 326 : extent = fold_build2_loc (input_location, PLUS_EXPR,
2364 : : gfc_array_index_type, extent,
2365 : : gfc_index_one_node);
2366 : 326 : tmp = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i]);
2367 : 326 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2368 : : tmp, extent);
2369 : 326 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i+1]);
2370 : 326 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2371 : : stride, tmp);
2372 : 326 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2373 : : boolean_type_node, cond, tmp);
2374 : : }
2375 : 1721 : se->expr = cond;
2376 : : }
2377 : 1964 : }
2378 : :
2379 : :
2380 : : /* Evaluate a single upper or lower bound. */
2381 : : /* TODO: bound intrinsic generates way too much unnecessary code. */
2382 : :
2383 : : static void
2384 : 15894 : gfc_conv_intrinsic_bound (gfc_se * se, gfc_expr * expr, enum gfc_isym_id op)
2385 : : {
2386 : 15894 : gfc_actual_arglist *arg;
2387 : 15894 : gfc_actual_arglist *arg2;
2388 : 15894 : tree desc;
2389 : 15894 : tree type;
2390 : 15894 : tree bound;
2391 : 15894 : tree tmp;
2392 : 15894 : tree cond, cond1;
2393 : 15894 : tree ubound;
2394 : 15894 : tree lbound;
2395 : 15894 : tree size;
2396 : 15894 : gfc_se argse;
2397 : 15894 : gfc_array_spec * as;
2398 : 15894 : bool assumed_rank_lb_one;
2399 : :
2400 : 15894 : arg = expr->value.function.actual;
2401 : 15894 : arg2 = arg->next;
2402 : :
2403 : 15894 : if (se->ss)
2404 : : {
2405 : : /* Create an implicit second parameter from the loop variable. */
2406 : 7755 : gcc_assert (!arg2->expr || op == GFC_ISYM_SHAPE);
2407 : 7755 : gcc_assert (se->loop->dimen == 1);
2408 : 7755 : gcc_assert (se->ss->info->expr == expr);
2409 : 7755 : gfc_advance_se_ss_chain (se);
2410 : 7755 : bound = se->loop->loopvar[0];
2411 : 7755 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2412 : : gfc_array_index_type, bound,
2413 : : se->loop->from[0]);
2414 : : }
2415 : : else
2416 : : {
2417 : : /* use the passed argument. */
2418 : 8139 : gcc_assert (arg2->expr);
2419 : 8139 : gfc_init_se (&argse, NULL);
2420 : 8139 : gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
2421 : 8139 : gfc_add_block_to_block (&se->pre, &argse.pre);
2422 : 8139 : bound = argse.expr;
2423 : : /* Convert from one based to zero based. */
2424 : 8139 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2425 : : gfc_array_index_type, bound,
2426 : : gfc_index_one_node);
2427 : : }
2428 : :
2429 : : /* TODO: don't re-evaluate the descriptor on each iteration. */
2430 : : /* Get a descriptor for the first parameter. */
2431 : 15894 : gfc_init_se (&argse, NULL);
2432 : 15894 : gfc_conv_expr_descriptor (&argse, arg->expr);
2433 : 15894 : gfc_add_block_to_block (&se->pre, &argse.pre);
2434 : 15894 : gfc_add_block_to_block (&se->post, &argse.post);
2435 : :
2436 : 15894 : desc = argse.expr;
2437 : :
2438 : 15894 : as = gfc_get_full_arrayspec_from_expr (arg->expr);
2439 : :
2440 : 15894 : if (INTEGER_CST_P (bound))
2441 : : {
2442 : 8019 : gcc_assert (op != GFC_ISYM_SHAPE);
2443 : 7782 : if (((!as || as->type != AS_ASSUMED_RANK)
2444 : 7159 : && wi::geu_p (wi::to_wide (bound),
2445 : 7159 : GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))))
2446 : 16038 : || wi::gtu_p (wi::to_wide (bound), GFC_MAX_DIMENSIONS))
2447 : 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2448 : : "dimension index",
2449 : : (op == GFC_ISYM_UBOUND) ? "UBOUND" : "LBOUND",
2450 : : &expr->where);
2451 : : }
2452 : :
2453 : 15894 : if (!INTEGER_CST_P (bound) || (as && as->type == AS_ASSUMED_RANK))
2454 : : {
2455 : 8735 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2456 : : {
2457 : 651 : bound = gfc_evaluate_now (bound, &se->pre);
2458 : 651 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2459 : 651 : bound, build_int_cst (TREE_TYPE (bound), 0));
2460 : 651 : if (as && as->type == AS_ASSUMED_RANK)
2461 : 546 : tmp = gfc_conv_descriptor_rank (desc);
2462 : : else
2463 : 105 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))];
2464 : 651 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
2465 : 651 : bound, fold_convert(TREE_TYPE (bound), tmp));
2466 : 651 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2467 : : logical_type_node, cond, tmp);
2468 : 651 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2469 : : gfc_msg_fault);
2470 : : }
2471 : : }
2472 : :
2473 : : /* Take care of the lbound shift for assumed-rank arrays that are
2474 : : nonallocatable and nonpointers. Those have a lbound of 1. */
2475 : 15406 : assumed_rank_lb_one = as && as->type == AS_ASSUMED_RANK
2476 : 10854 : && ((arg->expr->ts.type != BT_CLASS
2477 : 1945 : && !arg->expr->symtree->n.sym->attr.allocatable
2478 : 1602 : && !arg->expr->symtree->n.sym->attr.pointer)
2479 : 896 : || (arg->expr->ts.type == BT_CLASS
2480 : 174 : && !CLASS_DATA (arg->expr)->attr.allocatable
2481 : 138 : && !CLASS_DATA (arg->expr)->attr.class_pointer));
2482 : :
2483 : 15894 : ubound = gfc_conv_descriptor_ubound_get (desc, bound);
2484 : 15894 : lbound = gfc_conv_descriptor_lbound_get (desc, bound);
2485 : 15894 : size = fold_build2_loc (input_location, MINUS_EXPR,
2486 : : gfc_array_index_type, ubound, lbound);
2487 : 15894 : size = fold_build2_loc (input_location, PLUS_EXPR,
2488 : : gfc_array_index_type, size, gfc_index_one_node);
2489 : :
2490 : : /* 13.14.53: Result value for LBOUND
2491 : :
2492 : : Case (i): For an array section or for an array expression other than a
2493 : : whole array or array structure component, LBOUND(ARRAY, DIM)
2494 : : has the value 1. For a whole array or array structure
2495 : : component, LBOUND(ARRAY, DIM) has the value:
2496 : : (a) equal to the lower bound for subscript DIM of ARRAY if
2497 : : dimension DIM of ARRAY does not have extent zero
2498 : : or if ARRAY is an assumed-size array of rank DIM,
2499 : : or (b) 1 otherwise.
2500 : :
2501 : : 13.14.113: Result value for UBOUND
2502 : :
2503 : : Case (i): For an array section or for an array expression other than a
2504 : : whole array or array structure component, UBOUND(ARRAY, DIM)
2505 : : has the value equal to the number of elements in the given
2506 : : dimension; otherwise, it has a value equal to the upper bound
2507 : : for subscript DIM of ARRAY if dimension DIM of ARRAY does
2508 : : not have size zero and has value zero if dimension DIM has
2509 : : size zero. */
2510 : :
2511 : 15894 : if (op == GFC_ISYM_LBOUND && assumed_rank_lb_one)
2512 : 532 : se->expr = gfc_index_one_node;
2513 : 15362 : else if (as)
2514 : : {
2515 : 14874 : if (op == GFC_ISYM_UBOUND)
2516 : : {
2517 : 5308 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2518 : : size, gfc_index_zero_node);
2519 : 10012 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2520 : : gfc_array_index_type, cond,
2521 : : (assumed_rank_lb_one ? size : ubound),
2522 : : gfc_index_zero_node);
2523 : : }
2524 : 9566 : else if (op == GFC_ISYM_LBOUND)
2525 : : {
2526 : 4836 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2527 : : size, gfc_index_zero_node);
2528 : 4836 : if (as->type == AS_ASSUMED_SIZE)
2529 : : {
2530 : 98 : cond1 = fold_build2_loc (input_location, EQ_EXPR,
2531 : : logical_type_node, bound,
2532 : 98 : build_int_cst (TREE_TYPE (bound),
2533 : 98 : arg->expr->rank - 1));
2534 : 98 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2535 : : logical_type_node, cond, cond1);
2536 : : }
2537 : 4836 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2538 : : gfc_array_index_type, cond,
2539 : : lbound, gfc_index_one_node);
2540 : : }
2541 : 4730 : else if (op == GFC_ISYM_SHAPE)
2542 : 4730 : se->expr = fold_build2_loc (input_location, MAX_EXPR,
2543 : : gfc_array_index_type, size,
2544 : : gfc_index_zero_node);
2545 : : else
2546 : 0 : gcc_unreachable ();
2547 : :
2548 : : /* According to F2018 16.9.172, para 5, an assumed rank object,
2549 : : argument associated with and assumed size array, has the ubound
2550 : : of the final dimension set to -1 and UBOUND must return this.
2551 : : Similarly for the SHAPE intrinsic. */
2552 : 14874 : if (op != GFC_ISYM_LBOUND && assumed_rank_lb_one)
2553 : : {
2554 : 793 : tree minus_one = build_int_cst (gfc_array_index_type, -1);
2555 : 793 : tree rank = fold_convert (gfc_array_index_type,
2556 : : gfc_conv_descriptor_rank (desc));
2557 : 793 : rank = fold_build2_loc (input_location, PLUS_EXPR,
2558 : : gfc_array_index_type, rank, minus_one);
2559 : :
2560 : : /* Fix the expression to stop it from becoming even more
2561 : : complicated. */
2562 : 793 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
2563 : :
2564 : : /* Descriptors for assumed-size arrays have ubound = -1
2565 : : in the last dimension. */
2566 : 793 : cond1 = fold_build2_loc (input_location, EQ_EXPR,
2567 : : logical_type_node, ubound, minus_one);
2568 : 793 : cond = fold_build2_loc (input_location, EQ_EXPR,
2569 : : logical_type_node, bound, rank);
2570 : 793 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2571 : : logical_type_node, cond, cond1);
2572 : 793 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2573 : : gfc_array_index_type, cond,
2574 : : minus_one, se->expr);
2575 : : }
2576 : : }
2577 : : else /* as is null; this is an old-fashioned 1-based array. */
2578 : : {
2579 : 488 : if (op != GFC_ISYM_LBOUND)
2580 : : {
2581 : 386 : se->expr = fold_build2_loc (input_location, MAX_EXPR,
2582 : : gfc_array_index_type, size,
2583 : : gfc_index_zero_node);
2584 : : }
2585 : : else
2586 : 102 : se->expr = gfc_index_one_node;
2587 : : }
2588 : :
2589 : :
2590 : 15894 : type = gfc_typenode_for_spec (&expr->ts);
2591 : 15894 : se->expr = convert (type, se->expr);
2592 : 15894 : }
2593 : :
2594 : :
2595 : : static void
2596 : 577 : conv_intrinsic_cobound (gfc_se * se, gfc_expr * expr)
2597 : : {
2598 : 577 : gfc_actual_arglist *arg;
2599 : 577 : gfc_actual_arglist *arg2;
2600 : 577 : gfc_se argse;
2601 : 577 : tree bound, resbound, resbound2, desc, cond, tmp;
2602 : 577 : tree type;
2603 : 577 : int corank;
2604 : :
2605 : 577 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_LCOBOUND
2606 : : || expr->value.function.isym->id == GFC_ISYM_UCOBOUND
2607 : : || expr->value.function.isym->id == GFC_ISYM_THIS_IMAGE);
2608 : :
2609 : 577 : arg = expr->value.function.actual;
2610 : 577 : arg2 = arg->next;
2611 : :
2612 : 577 : gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
2613 : 577 : corank = arg->expr->corank;
2614 : :
2615 : 577 : gfc_init_se (&argse, NULL);
2616 : 577 : argse.want_coarray = 1;
2617 : :
2618 : 577 : gfc_conv_expr_descriptor (&argse, arg->expr);
2619 : 577 : gfc_add_block_to_block (&se->pre, &argse.pre);
2620 : 577 : gfc_add_block_to_block (&se->post, &argse.post);
2621 : 577 : desc = argse.expr;
2622 : :
2623 : 577 : if (se->ss)
2624 : : {
2625 : : /* Create an implicit second parameter from the loop variable. */
2626 : 184 : gcc_assert (!arg2->expr);
2627 : 184 : gcc_assert (corank > 0);
2628 : 184 : gcc_assert (se->loop->dimen == 1);
2629 : 184 : gcc_assert (se->ss->info->expr == expr);
2630 : :
2631 : 184 : bound = se->loop->loopvar[0];
2632 : 368 : bound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2633 : 184 : bound, gfc_rank_cst[arg->expr->rank]);
2634 : 184 : gfc_advance_se_ss_chain (se);
2635 : : }
2636 : : else
2637 : : {
2638 : : /* use the passed argument. */
2639 : 393 : gcc_assert (arg2->expr);
2640 : 393 : gfc_init_se (&argse, NULL);
2641 : 393 : gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
2642 : 393 : gfc_add_block_to_block (&se->pre, &argse.pre);
2643 : 393 : bound = argse.expr;
2644 : :
2645 : 393 : if (INTEGER_CST_P (bound))
2646 : : {
2647 : 299 : if (wi::ltu_p (wi::to_wide (bound), 1)
2648 : 598 : || wi::gtu_p (wi::to_wide (bound),
2649 : 299 : GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
2650 : 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2651 : 0 : "dimension index", expr->value.function.isym->name,
2652 : : &expr->where);
2653 : : }
2654 : 94 : else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2655 : : {
2656 : 36 : bound = gfc_evaluate_now (bound, &se->pre);
2657 : 36 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2658 : 36 : bound, build_int_cst (TREE_TYPE (bound), 1));
2659 : 36 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
2660 : 36 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2661 : : bound, tmp);
2662 : 36 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2663 : : logical_type_node, cond, tmp);
2664 : 36 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2665 : : gfc_msg_fault);
2666 : : }
2667 : :
2668 : :
2669 : : /* Subtract 1 to get to zero based and add dimensions. */
2670 : 393 : switch (arg->expr->rank)
2671 : : {
2672 : 51 : case 0:
2673 : 51 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2674 : : gfc_array_index_type, bound,
2675 : : gfc_index_one_node);
2676 : : case 1:
2677 : : break;
2678 : 36 : default:
2679 : 36 : bound = fold_build2_loc (input_location, PLUS_EXPR,
2680 : : gfc_array_index_type, bound,
2681 : 36 : gfc_rank_cst[arg->expr->rank - 1]);
2682 : : }
2683 : : }
2684 : :
2685 : 577 : resbound = gfc_conv_descriptor_lbound_get (desc, bound);
2686 : :
2687 : : /* Handle UCOBOUND with special handling of the last codimension. */
2688 : 577 : if (expr->value.function.isym->id == GFC_ISYM_UCOBOUND)
2689 : : {
2690 : : /* Last codimension: For -fcoarray=single just return
2691 : : the lcobound - otherwise add
2692 : : ceiling (real (num_images ()) / real (size)) - 1
2693 : : = (num_images () + size - 1) / size - 1
2694 : : = (num_images - 1) / size(),
2695 : : where size is the product of the extent of all but the last
2696 : : codimension. */
2697 : :
2698 : 191 : if (flag_coarray != GFC_FCOARRAY_SINGLE && corank > 1)
2699 : : {
2700 : 30 : tree cosize;
2701 : :
2702 : 30 : cosize = gfc_conv_descriptor_cosize (desc, arg->expr->rank, corank);
2703 : 30 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
2704 : : 2, null_pointer_node, null_pointer_node);
2705 : 30 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2706 : : gfc_array_index_type,
2707 : : fold_convert (gfc_array_index_type, tmp),
2708 : : build_int_cst (gfc_array_index_type, 1));
2709 : 30 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
2710 : : gfc_array_index_type, tmp,
2711 : : fold_convert (gfc_array_index_type, cosize));
2712 : 30 : resbound = fold_build2_loc (input_location, PLUS_EXPR,
2713 : : gfc_array_index_type, resbound, tmp);
2714 : 30 : }
2715 : 161 : else if (flag_coarray != GFC_FCOARRAY_SINGLE)
2716 : : {
2717 : : /* ubound = lbound + num_images() - 1. */
2718 : 21 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
2719 : : 2, null_pointer_node, null_pointer_node);
2720 : 21 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2721 : : gfc_array_index_type,
2722 : : fold_convert (gfc_array_index_type, tmp),
2723 : : build_int_cst (gfc_array_index_type, 1));
2724 : 21 : resbound = fold_build2_loc (input_location, PLUS_EXPR,
2725 : : gfc_array_index_type, resbound, tmp);
2726 : : }
2727 : :
2728 : 191 : if (corank > 1)
2729 : : {
2730 : 137 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2731 : : bound,
2732 : 137 : build_int_cst (TREE_TYPE (bound),
2733 : 137 : arg->expr->rank + corank - 1));
2734 : :
2735 : 137 : resbound2 = gfc_conv_descriptor_ubound_get (desc, bound);
2736 : 137 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2737 : : gfc_array_index_type, cond,
2738 : : resbound, resbound2);
2739 : : }
2740 : : else
2741 : 54 : se->expr = resbound;
2742 : : }
2743 : : else
2744 : 386 : se->expr = resbound;
2745 : :
2746 : 577 : type = gfc_typenode_for_spec (&expr->ts);
2747 : 577 : se->expr = convert (type, se->expr);
2748 : 577 : }
2749 : :
2750 : :
2751 : : static void
2752 : 1877 : conv_intrinsic_stride (gfc_se * se, gfc_expr * expr)
2753 : : {
2754 : 1877 : gfc_actual_arglist *array_arg;
2755 : 1877 : gfc_actual_arglist *dim_arg;
2756 : 1877 : gfc_se argse;
2757 : 1877 : tree desc, tmp;
2758 : :
2759 : 1877 : array_arg = expr->value.function.actual;
2760 : 1877 : dim_arg = array_arg->next;
2761 : :
2762 : 1877 : gcc_assert (array_arg->expr->expr_type == EXPR_VARIABLE);
2763 : :
2764 : 1877 : gfc_init_se (&argse, NULL);
2765 : 1877 : gfc_conv_expr_descriptor (&argse, array_arg->expr);
2766 : 1877 : gfc_add_block_to_block (&se->pre, &argse.pre);
2767 : 1877 : gfc_add_block_to_block (&se->post, &argse.post);
2768 : 1877 : desc = argse.expr;
2769 : :
2770 : 1877 : gcc_assert (dim_arg->expr);
2771 : 1877 : gfc_init_se (&argse, NULL);
2772 : 1877 : gfc_conv_expr_type (&argse, dim_arg->expr, gfc_array_index_type);
2773 : 1877 : gfc_add_block_to_block (&se->pre, &argse.pre);
2774 : 1877 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2775 : : argse.expr, gfc_index_one_node);
2776 : 1877 : se->expr = gfc_conv_descriptor_stride_get (desc, tmp);
2777 : 1877 : }
2778 : :
2779 : : static void
2780 : 7723 : gfc_conv_intrinsic_abs (gfc_se * se, gfc_expr * expr)
2781 : : {
2782 : 7723 : tree arg, cabs;
2783 : :
2784 : 7723 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
2785 : :
2786 : 7723 : switch (expr->value.function.actual->expr->ts.type)
2787 : : {
2788 : 6747 : case BT_INTEGER:
2789 : 6747 : case BT_REAL:
2790 : 6747 : se->expr = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (arg),
2791 : : arg);
2792 : 6747 : break;
2793 : :
2794 : 976 : case BT_COMPLEX:
2795 : 976 : cabs = gfc_builtin_decl_for_float_kind (BUILT_IN_CABS, expr->ts.kind);
2796 : 976 : se->expr = build_call_expr_loc (input_location, cabs, 1, arg);
2797 : 976 : break;
2798 : :
2799 : 0 : default:
2800 : 0 : gcc_unreachable ();
2801 : : }
2802 : 7723 : }
2803 : :
2804 : :
2805 : : /* Create a complex value from one or two real components. */
2806 : :
2807 : : static void
2808 : 485 : gfc_conv_intrinsic_cmplx (gfc_se * se, gfc_expr * expr, int both)
2809 : : {
2810 : 485 : tree real;
2811 : 485 : tree imag;
2812 : 485 : tree type;
2813 : 485 : tree *args;
2814 : 485 : unsigned int num_args;
2815 : :
2816 : 485 : num_args = gfc_intrinsic_argument_list_length (expr);
2817 : 485 : args = XALLOCAVEC (tree, num_args);
2818 : :
2819 : 485 : type = gfc_typenode_for_spec (&expr->ts);
2820 : 485 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2821 : 485 : real = convert (TREE_TYPE (type), args[0]);
2822 : 485 : if (both)
2823 : 441 : imag = convert (TREE_TYPE (type), args[1]);
2824 : 44 : else if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE)
2825 : : {
2826 : 30 : imag = fold_build1_loc (input_location, IMAGPART_EXPR,
2827 : 30 : TREE_TYPE (TREE_TYPE (args[0])), args[0]);
2828 : 30 : imag = convert (TREE_TYPE (type), imag);
2829 : : }
2830 : : else
2831 : 14 : imag = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
2832 : :
2833 : 485 : se->expr = fold_build2_loc (input_location, COMPLEX_EXPR, type, real, imag);
2834 : 485 : }
2835 : :
2836 : :
2837 : : /* Remainder function MOD(A, P) = A - INT(A / P) * P
2838 : : MODULO(A, P) = A - FLOOR (A / P) * P
2839 : :
2840 : : The obvious algorithms above are numerically instable for large
2841 : : arguments, hence these intrinsics are instead implemented via calls
2842 : : to the fmod family of functions. It is the responsibility of the
2843 : : user to ensure that the second argument is non-zero. */
2844 : :
2845 : : static void
2846 : 3165 : gfc_conv_intrinsic_mod (gfc_se * se, gfc_expr * expr, int modulo)
2847 : : {
2848 : 3165 : tree type;
2849 : 3165 : tree tmp;
2850 : 3165 : tree test;
2851 : 3165 : tree test2;
2852 : 3165 : tree fmod;
2853 : 3165 : tree zero;
2854 : 3165 : tree args[2];
2855 : :
2856 : 3165 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
2857 : :
2858 : 3165 : switch (expr->ts.type)
2859 : : {
2860 : 3012 : case BT_INTEGER:
2861 : : /* Integer case is easy, we've got a builtin op. */
2862 : 3012 : type = TREE_TYPE (args[0]);
2863 : :
2864 : 3012 : if (modulo)
2865 : 403 : se->expr = fold_build2_loc (input_location, FLOOR_MOD_EXPR, type,
2866 : : args[0], args[1]);
2867 : : else
2868 : 2609 : se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2869 : : args[0], args[1]);
2870 : : break;
2871 : :
2872 : 30 : case BT_UNSIGNED:
2873 : : /* Even easier, we only need one. */
2874 : 30 : type = TREE_TYPE (args[0]);
2875 : 30 : se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2876 : : args[0], args[1]);
2877 : 30 : break;
2878 : :
2879 : 123 : case BT_REAL:
2880 : 123 : fmod = NULL_TREE;
2881 : : /* Check if we have a builtin fmod. */
2882 : 123 : fmod = gfc_builtin_decl_for_float_kind (BUILT_IN_FMOD, expr->ts.kind);
2883 : :
2884 : : /* The builtin should always be available. */
2885 : 123 : gcc_assert (fmod != NULL_TREE);
2886 : :
2887 : 123 : tmp = build_addr (fmod);
2888 : 123 : se->expr = build_call_array_loc (input_location,
2889 : 123 : TREE_TYPE (TREE_TYPE (fmod)),
2890 : : tmp, 2, args);
2891 : 123 : if (modulo == 0)
2892 : 123 : return;
2893 : :
2894 : 25 : type = TREE_TYPE (args[0]);
2895 : :
2896 : 25 : args[0] = gfc_evaluate_now (args[0], &se->pre);
2897 : 25 : args[1] = gfc_evaluate_now (args[1], &se->pre);
2898 : :
2899 : : /* Definition:
2900 : : modulo = arg - floor (arg/arg2) * arg2
2901 : :
2902 : : In order to calculate the result accurately, we use the fmod
2903 : : function as follows.
2904 : :
2905 : : res = fmod (arg, arg2);
2906 : : if (res)
2907 : : {
2908 : : if ((arg < 0) xor (arg2 < 0))
2909 : : res += arg2;
2910 : : }
2911 : : else
2912 : : res = copysign (0., arg2);
2913 : :
2914 : : => As two nested ternary exprs:
2915 : :
2916 : : res = res ? (((arg < 0) xor (arg2 < 0)) ? res + arg2 : res)
2917 : : : copysign (0., arg2);
2918 : :
2919 : : */
2920 : :
2921 : 25 : zero = gfc_build_const (type, integer_zero_node);
2922 : 25 : tmp = gfc_evaluate_now (se->expr, &se->pre);
2923 : 25 : if (!flag_signed_zeros)
2924 : : {
2925 : 1 : test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2926 : : args[0], zero);
2927 : 1 : test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2928 : : args[1], zero);
2929 : 1 : test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
2930 : : logical_type_node, test, test2);
2931 : 1 : test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2932 : : tmp, zero);
2933 : 1 : test = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2934 : : logical_type_node, test, test2);
2935 : 1 : test = gfc_evaluate_now (test, &se->pre);
2936 : 1 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
2937 : : fold_build2_loc (input_location,
2938 : : PLUS_EXPR,
2939 : : type, tmp, args[1]),
2940 : : tmp);
2941 : : }
2942 : : else
2943 : : {
2944 : 24 : tree expr1, copysign, cscall;
2945 : 24 : copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN,
2946 : : expr->ts.kind);
2947 : 24 : test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2948 : : args[0], zero);
2949 : 24 : test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2950 : : args[1], zero);
2951 : 24 : test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
2952 : : logical_type_node, test, test2);
2953 : 24 : expr1 = fold_build3_loc (input_location, COND_EXPR, type, test2,
2954 : : fold_build2_loc (input_location,
2955 : : PLUS_EXPR,
2956 : : type, tmp, args[1]),
2957 : : tmp);
2958 : 24 : test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2959 : : tmp, zero);
2960 : 24 : cscall = build_call_expr_loc (input_location, copysign, 2, zero,
2961 : : args[1]);
2962 : 24 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
2963 : : expr1, cscall);
2964 : : }
2965 : : return;
2966 : :
2967 : 0 : default:
2968 : 0 : gcc_unreachable ();
2969 : : }
2970 : : }
2971 : :
2972 : : /* DSHIFTL(I,J,S) = (I << S) | (J >> (BITSIZE(J) - S))
2973 : : DSHIFTR(I,J,S) = (I << (BITSIZE(I) - S)) | (J >> S)
2974 : : where the right shifts are logical (i.e. 0's are shifted in).
2975 : : Because SHIFT_EXPR's want shifts strictly smaller than the integral
2976 : : type width, we have to special-case both S == 0 and S == BITSIZE(J):
2977 : : DSHIFTL(I,J,0) = I
2978 : : DSHIFTL(I,J,BITSIZE) = J
2979 : : DSHIFTR(I,J,0) = J
2980 : : DSHIFTR(I,J,BITSIZE) = I. */
2981 : :
2982 : : static void
2983 : 132 : gfc_conv_intrinsic_dshift (gfc_se * se, gfc_expr * expr, bool dshiftl)
2984 : : {
2985 : 132 : tree type, utype, stype, arg1, arg2, shift, res, left, right;
2986 : 132 : tree args[3], cond, tmp;
2987 : 132 : int bitsize;
2988 : :
2989 : 132 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
2990 : :
2991 : 132 : gcc_assert (TREE_TYPE (args[0]) == TREE_TYPE (args[1]));
2992 : 132 : type = TREE_TYPE (args[0]);
2993 : 132 : bitsize = TYPE_PRECISION (type);
2994 : 132 : utype = unsigned_type_for (type);
2995 : 132 : stype = TREE_TYPE (args[2]);
2996 : :
2997 : 132 : arg1 = gfc_evaluate_now (args[0], &se->pre);
2998 : 132 : arg2 = gfc_evaluate_now (args[1], &se->pre);
2999 : 132 : shift = gfc_evaluate_now (args[2], &se->pre);
3000 : :
3001 : : /* The generic case. */
3002 : 132 : tmp = fold_build2_loc (input_location, MINUS_EXPR, stype,
3003 : 132 : build_int_cst (stype, bitsize), shift);
3004 : 198 : left = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3005 : : arg1, dshiftl ? shift : tmp);
3006 : :
3007 : 198 : right = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
3008 : : fold_convert (utype, arg2), dshiftl ? tmp : shift);
3009 : 132 : right = fold_convert (type, right);
3010 : :
3011 : 132 : res = fold_build2_loc (input_location, BIT_IOR_EXPR, type, left, right);
3012 : :
3013 : : /* Special cases. */
3014 : 132 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3015 : : build_int_cst (stype, 0));
3016 : 198 : res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3017 : : dshiftl ? arg1 : arg2, res);
3018 : :
3019 : 132 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3020 : 132 : build_int_cst (stype, bitsize));
3021 : 198 : res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3022 : : dshiftl ? arg2 : arg1, res);
3023 : :
3024 : 132 : se->expr = res;
3025 : 132 : }
3026 : :
3027 : :
3028 : : /* Positive difference DIM (x, y) = ((x - y) < 0) ? 0 : x - y. */
3029 : :
3030 : : static void
3031 : 96 : gfc_conv_intrinsic_dim (gfc_se * se, gfc_expr * expr)
3032 : : {
3033 : 96 : tree val;
3034 : 96 : tree tmp;
3035 : 96 : tree type;
3036 : 96 : tree zero;
3037 : 96 : tree args[2];
3038 : :
3039 : 96 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3040 : 96 : type = TREE_TYPE (args[0]);
3041 : :
3042 : 96 : val = fold_build2_loc (input_location, MINUS_EXPR, type, args[0], args[1]);
3043 : 96 : val = gfc_evaluate_now (val, &se->pre);
3044 : :
3045 : 96 : zero = gfc_build_const (type, integer_zero_node);
3046 : 96 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node, val, zero);
3047 : 96 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, zero, val);
3048 : 96 : }
3049 : :
3050 : :
3051 : : /* SIGN(A, B) is absolute value of A times sign of B.
3052 : : The real value versions use library functions to ensure the correct
3053 : : handling of negative zero. Integer case implemented as:
3054 : : SIGN(A, B) = { tmp = (A ^ B) >> C; (A + tmp) ^ tmp }
3055 : : */
3056 : :
3057 : : static void
3058 : 424 : gfc_conv_intrinsic_sign (gfc_se * se, gfc_expr * expr)
3059 : : {
3060 : 424 : tree tmp;
3061 : 424 : tree type;
3062 : 424 : tree args[2];
3063 : :
3064 : 424 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3065 : 424 : if (expr->ts.type == BT_REAL)
3066 : : {
3067 : 162 : tree abs;
3068 : :
3069 : 162 : tmp = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
3070 : 162 : abs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
3071 : :
3072 : : /* We explicitly have to ignore the minus sign. We do so by using
3073 : : result = (arg1 == 0) ? abs(arg0) : copysign(arg0, arg1). */
3074 : 162 : if (!flag_sign_zero
3075 : 198 : && MODE_HAS_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (args[1]))))
3076 : : {
3077 : 12 : tree cond, zero;
3078 : 12 : zero = build_real_from_int_cst (TREE_TYPE (args[1]), integer_zero_node);
3079 : 12 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3080 : : args[1], zero);
3081 : 24 : se->expr = fold_build3_loc (input_location, COND_EXPR,
3082 : 12 : TREE_TYPE (args[0]), cond,
3083 : : build_call_expr_loc (input_location, abs, 1,
3084 : : args[0]),
3085 : : build_call_expr_loc (input_location, tmp, 2,
3086 : : args[0], args[1]));
3087 : : }
3088 : : else
3089 : 150 : se->expr = build_call_expr_loc (input_location, tmp, 2,
3090 : : args[0], args[1]);
3091 : 162 : return;
3092 : : }
3093 : :
3094 : : /* Having excluded floating point types, we know we are now dealing
3095 : : with signed integer types. */
3096 : 262 : type = TREE_TYPE (args[0]);
3097 : :
3098 : : /* Args[0] is used multiple times below. */
3099 : 262 : args[0] = gfc_evaluate_now (args[0], &se->pre);
3100 : :
3101 : : /* Construct (A ^ B) >> 31, which generates a bit mask of all zeros if
3102 : : the signs of A and B are the same, and of all ones if they differ. */
3103 : 262 : tmp = fold_build2_loc (input_location, BIT_XOR_EXPR, type, args[0], args[1]);
3104 : 262 : tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, tmp,
3105 : 262 : build_int_cst (type, TYPE_PRECISION (type) - 1));
3106 : 262 : tmp = gfc_evaluate_now (tmp, &se->pre);
3107 : :
3108 : : /* Construct (A + tmp) ^ tmp, which is A if tmp is zero, and -A if tmp]
3109 : : is all ones (i.e. -1). */
3110 : 262 : se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR, type,
3111 : : fold_build2_loc (input_location, PLUS_EXPR,
3112 : : type, args[0], tmp), tmp);
3113 : : }
3114 : :
3115 : :
3116 : : /* Test for the presence of an optional argument. */
3117 : :
3118 : : static void
3119 : 5045 : gfc_conv_intrinsic_present (gfc_se * se, gfc_expr * expr)
3120 : : {
3121 : 5045 : gfc_expr *arg;
3122 : :
3123 : 5045 : arg = expr->value.function.actual->expr;
3124 : 5045 : gcc_assert (arg->expr_type == EXPR_VARIABLE);
3125 : 5045 : se->expr = gfc_conv_expr_present (arg->symtree->n.sym);
3126 : 5045 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
3127 : 5045 : }
3128 : :
3129 : :
3130 : : /* Calculate the double precision product of two single precision values. */
3131 : :
3132 : : static void
3133 : 13 : gfc_conv_intrinsic_dprod (gfc_se * se, gfc_expr * expr)
3134 : : {
3135 : 13 : tree type;
3136 : 13 : tree args[2];
3137 : :
3138 : 13 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3139 : :
3140 : : /* Convert the args to double precision before multiplying. */
3141 : 13 : type = gfc_typenode_for_spec (&expr->ts);
3142 : 13 : args[0] = convert (type, args[0]);
3143 : 13 : args[1] = convert (type, args[1]);
3144 : 13 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, args[0],
3145 : : args[1]);
3146 : 13 : }
3147 : :
3148 : :
3149 : : /* Return a length one character string containing an ascii character. */
3150 : :
3151 : : static void
3152 : 2020 : gfc_conv_intrinsic_char (gfc_se * se, gfc_expr * expr)
3153 : : {
3154 : 2020 : tree arg[2];
3155 : 2020 : tree var;
3156 : 2020 : tree type;
3157 : 2020 : unsigned int num_args;
3158 : :
3159 : 2020 : num_args = gfc_intrinsic_argument_list_length (expr);
3160 : 2020 : gfc_conv_intrinsic_function_args (se, expr, arg, num_args);
3161 : :
3162 : 2020 : type = gfc_get_char_type (expr->ts.kind);
3163 : 2020 : var = gfc_create_var (type, "char");
3164 : :
3165 : 2020 : arg[0] = fold_build1_loc (input_location, NOP_EXPR, type, arg[0]);
3166 : 2020 : gfc_add_modify (&se->pre, var, arg[0]);
3167 : 2020 : se->expr = gfc_build_addr_expr (build_pointer_type (type), var);
3168 : 2020 : se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3169 : 2020 : }
3170 : :
3171 : :
3172 : : static void
3173 : 0 : gfc_conv_intrinsic_ctime (gfc_se * se, gfc_expr * expr)
3174 : : {
3175 : 0 : tree var;
3176 : 0 : tree len;
3177 : 0 : tree tmp;
3178 : 0 : tree cond;
3179 : 0 : tree fndecl;
3180 : 0 : tree *args;
3181 : 0 : unsigned int num_args;
3182 : :
3183 : 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3184 : 0 : args = XALLOCAVEC (tree, num_args);
3185 : :
3186 : 0 : var = gfc_create_var (pchar_type_node, "pstr");
3187 : 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3188 : :
3189 : 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3190 : 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3191 : 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3192 : :
3193 : 0 : fndecl = build_addr (gfor_fndecl_ctime);
3194 : 0 : tmp = build_call_array_loc (input_location,
3195 : 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_ctime)),
3196 : : fndecl, num_args, args);
3197 : 0 : gfc_add_expr_to_block (&se->pre, tmp);
3198 : :
3199 : : /* Free the temporary afterwards, if necessary. */
3200 : 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3201 : 0 : len, build_int_cst (TREE_TYPE (len), 0));
3202 : 0 : tmp = gfc_call_free (var);
3203 : 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3204 : 0 : gfc_add_expr_to_block (&se->post, tmp);
3205 : :
3206 : 0 : se->expr = var;
3207 : 0 : se->string_length = len;
3208 : 0 : }
3209 : :
3210 : :
3211 : : static void
3212 : 0 : gfc_conv_intrinsic_fdate (gfc_se * se, gfc_expr * expr)
3213 : : {
3214 : 0 : tree var;
3215 : 0 : tree len;
3216 : 0 : tree tmp;
3217 : 0 : tree cond;
3218 : 0 : tree fndecl;
3219 : 0 : tree *args;
3220 : 0 : unsigned int num_args;
3221 : :
3222 : 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3223 : 0 : args = XALLOCAVEC (tree, num_args);
3224 : :
3225 : 0 : var = gfc_create_var (pchar_type_node, "pstr");
3226 : 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3227 : :
3228 : 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3229 : 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3230 : 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3231 : :
3232 : 0 : fndecl = build_addr (gfor_fndecl_fdate);
3233 : 0 : tmp = build_call_array_loc (input_location,
3234 : 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_fdate)),
3235 : : fndecl, num_args, args);
3236 : 0 : gfc_add_expr_to_block (&se->pre, tmp);
3237 : :
3238 : : /* Free the temporary afterwards, if necessary. */
3239 : 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3240 : 0 : len, build_int_cst (TREE_TYPE (len), 0));
3241 : 0 : tmp = gfc_call_free (var);
3242 : 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3243 : 0 : gfc_add_expr_to_block (&se->post, tmp);
3244 : :
3245 : 0 : se->expr = var;
3246 : 0 : se->string_length = len;
3247 : 0 : }
3248 : :
3249 : :
3250 : : /* Generate a direct call to free() for the FREE subroutine. */
3251 : :
3252 : : static tree
3253 : 10 : conv_intrinsic_free (gfc_code *code)
3254 : : {
3255 : 10 : stmtblock_t block;
3256 : 10 : gfc_se argse;
3257 : 10 : tree arg, call;
3258 : :
3259 : 10 : gfc_init_se (&argse, NULL);
3260 : 10 : gfc_conv_expr (&argse, code->ext.actual->expr);
3261 : 10 : arg = fold_convert (ptr_type_node, argse.expr);
3262 : :
3263 : 10 : gfc_init_block (&block);
3264 : 10 : call = build_call_expr_loc (input_location,
3265 : : builtin_decl_explicit (BUILT_IN_FREE), 1, arg);
3266 : 10 : gfc_add_expr_to_block (&block, call);
3267 : 10 : return gfc_finish_block (&block);
3268 : : }
3269 : :
3270 : :
3271 : : /* Call the RANDOM_INIT library subroutine with a hidden argument for
3272 : : handling seeding on coarray images. */
3273 : :
3274 : : static tree
3275 : 90 : conv_intrinsic_random_init (gfc_code *code)
3276 : : {
3277 : 90 : stmtblock_t block;
3278 : 90 : gfc_se se;
3279 : 90 : tree arg1, arg2, tmp;
3280 : : /* On none coarray == lib compiles use LOGICAL(4) else regular LOGICAL. */
3281 : 90 : tree used_bool_type_node = flag_coarray == GFC_FCOARRAY_LIB
3282 : 90 : ? logical_type_node
3283 : 90 : : gfc_get_logical_type (4);
3284 : :
3285 : : /* Make the function call. */
3286 : 90 : gfc_init_block (&block);
3287 : 90 : gfc_init_se (&se, NULL);
3288 : :
3289 : : /* Convert REPEATABLE to the desired LOGICAL entity. */
3290 : 90 : gfc_conv_expr (&se, code->ext.actual->expr);
3291 : 90 : gfc_add_block_to_block (&block, &se.pre);
3292 : 90 : arg1 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3293 : 90 : gfc_add_block_to_block (&block, &se.post);
3294 : :
3295 : : /* Convert IMAGE_DISTINCT to the desired LOGICAL entity. */
3296 : 90 : gfc_conv_expr (&se, code->ext.actual->next->expr);
3297 : 90 : gfc_add_block_to_block (&block, &se.pre);
3298 : 90 : arg2 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3299 : 90 : gfc_add_block_to_block (&block, &se.post);
3300 : :
3301 : 90 : if (flag_coarray == GFC_FCOARRAY_LIB)
3302 : : {
3303 : 0 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
3304 : : 2, arg1, arg2);
3305 : : }
3306 : : else
3307 : : {
3308 : : /* The ABI for libgfortran needs to be maintained, so a hidden
3309 : : argument must be include if code is compiled with -fcoarray=single
3310 : : or without the option. Set to 0. */
3311 : 90 : tree arg3 = build_int_cst (gfc_get_int_type (4), 0);
3312 : 90 : tmp = build_call_expr_loc (input_location, gfor_fndecl_random_init,
3313 : : 3, arg1, arg2, arg3);
3314 : : }
3315 : :
3316 : 90 : gfc_add_expr_to_block (&block, tmp);
3317 : :
3318 : 90 : return gfc_finish_block (&block);
3319 : : }
3320 : :
3321 : :
3322 : : /* Call the SYSTEM_CLOCK library functions, handling the type and kind
3323 : : conversions. */
3324 : :
3325 : : static tree
3326 : 194 : conv_intrinsic_system_clock (gfc_code *code)
3327 : : {
3328 : 194 : stmtblock_t block;
3329 : 194 : gfc_se count_se, count_rate_se, count_max_se;
3330 : 194 : tree arg1 = NULL_TREE, arg2 = NULL_TREE, arg3 = NULL_TREE;
3331 : 194 : tree tmp;
3332 : 194 : int least;
3333 : :
3334 : 194 : gfc_expr *count = code->ext.actual->expr;
3335 : 194 : gfc_expr *count_rate = code->ext.actual->next->expr;
3336 : 194 : gfc_expr *count_max = code->ext.actual->next->next->expr;
3337 : :
3338 : : /* Evaluate our arguments. */
3339 : 194 : if (count)
3340 : : {
3341 : 194 : gfc_init_se (&count_se, NULL);
3342 : 194 : gfc_conv_expr (&count_se, count);
3343 : : }
3344 : :
3345 : 194 : if (count_rate)
3346 : : {
3347 : 181 : gfc_init_se (&count_rate_se, NULL);
3348 : 181 : gfc_conv_expr (&count_rate_se, count_rate);
3349 : : }
3350 : :
3351 : 194 : if (count_max)
3352 : : {
3353 : 180 : gfc_init_se (&count_max_se, NULL);
3354 : 180 : gfc_conv_expr (&count_max_se, count_max);
3355 : : }
3356 : :
3357 : : /* Find the smallest kind found of the arguments. */
3358 : 194 : least = 16;
3359 : 194 : least = (count && count->ts.kind < least) ? count->ts.kind : least;
3360 : 194 : least = (count_rate && count_rate->ts.kind < least) ? count_rate->ts.kind
3361 : : : least;
3362 : 194 : least = (count_max && count_max->ts.kind < least) ? count_max->ts.kind
3363 : : : least;
3364 : :
3365 : : /* Prepare temporary variables. */
3366 : :
3367 : 194 : if (count)
3368 : : {
3369 : 194 : if (least >= 8)
3370 : 18 : arg1 = gfc_create_var (gfc_get_int_type (8), "count");
3371 : 176 : else if (least == 4)
3372 : 152 : arg1 = gfc_create_var (gfc_get_int_type (4), "count");
3373 : 24 : else if (count->ts.kind == 1)
3374 : 12 : arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[0].pedantic_min_int,
3375 : : count->ts.kind);
3376 : : else
3377 : 12 : arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[1].pedantic_min_int,
3378 : : count->ts.kind);
3379 : : }
3380 : :
3381 : 194 : if (count_rate)
3382 : : {
3383 : 181 : if (least >= 8)
3384 : 18 : arg2 = gfc_create_var (gfc_get_int_type (8), "count_rate");
3385 : 163 : else if (least == 4)
3386 : 139 : arg2 = gfc_create_var (gfc_get_int_type (4), "count_rate");
3387 : : else
3388 : 24 : arg2 = integer_zero_node;
3389 : : }
3390 : :
3391 : 194 : if (count_max)
3392 : : {
3393 : 180 : if (least >= 8)
3394 : 18 : arg3 = gfc_create_var (gfc_get_int_type (8), "count_max");
3395 : 162 : else if (least == 4)
3396 : 138 : arg3 = gfc_create_var (gfc_get_int_type (4), "count_max");
3397 : : else
3398 : 24 : arg3 = integer_zero_node;
3399 : : }
3400 : :
3401 : : /* Make the function call. */
3402 : 194 : gfc_init_block (&block);
3403 : :
3404 : 194 : if (least <= 2)
3405 : : {
3406 : 24 : if (least == 1)
3407 : : {
3408 : 12 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3409 : : : null_pointer_node;
3410 : 12 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3411 : : : null_pointer_node;
3412 : 12 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3413 : : : null_pointer_node;
3414 : : }
3415 : :
3416 : 24 : if (least == 2)
3417 : : {
3418 : 12 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3419 : : : null_pointer_node;
3420 : 12 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3421 : : : null_pointer_node;
3422 : 12 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3423 : : : null_pointer_node;
3424 : : }
3425 : : }
3426 : : else
3427 : : {
3428 : 170 : if (least == 4)
3429 : : {
3430 : 581 : tmp = build_call_expr_loc (input_location,
3431 : : gfor_fndecl_system_clock4, 3,
3432 : 152 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3433 : : : null_pointer_node,
3434 : 139 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3435 : : : null_pointer_node,
3436 : 138 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3437 : : : null_pointer_node);
3438 : 152 : gfc_add_expr_to_block (&block, tmp);
3439 : : }
3440 : : /* Handle kind>=8, 10, or 16 arguments */
3441 : 170 : if (least >= 8)
3442 : : {
3443 : 72 : tmp = build_call_expr_loc (input_location,
3444 : : gfor_fndecl_system_clock8, 3,
3445 : 18 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3446 : : : null_pointer_node,
3447 : 18 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3448 : : : null_pointer_node,
3449 : 18 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3450 : : : null_pointer_node);
3451 : 18 : gfc_add_expr_to_block (&block, tmp);
3452 : : }
3453 : : }
3454 : :
3455 : : /* And store values back if needed. */
3456 : 194 : if (arg1 && arg1 != count_se.expr)
3457 : 194 : gfc_add_modify (&block, count_se.expr,
3458 : 194 : fold_convert (TREE_TYPE (count_se.expr), arg1));
3459 : 194 : if (arg2 && arg2 != count_rate_se.expr)
3460 : 181 : gfc_add_modify (&block, count_rate_se.expr,
3461 : 181 : fold_convert (TREE_TYPE (count_rate_se.expr), arg2));
3462 : 194 : if (arg3 && arg3 != count_max_se.expr)
3463 : 180 : gfc_add_modify (&block, count_max_se.expr,
3464 : 180 : fold_convert (TREE_TYPE (count_max_se.expr), arg3));
3465 : :
3466 : 194 : return gfc_finish_block (&block);
3467 : : }
3468 : :
3469 : :
3470 : : /* Return a character string containing the tty name. */
3471 : :
3472 : : static void
3473 : 0 : gfc_conv_intrinsic_ttynam (gfc_se * se, gfc_expr * expr)
3474 : : {
3475 : 0 : tree var;
3476 : 0 : tree len;
3477 : 0 : tree tmp;
3478 : 0 : tree cond;
3479 : 0 : tree fndecl;
3480 : 0 : tree *args;
3481 : 0 : unsigned int num_args;
3482 : :
3483 : 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3484 : 0 : args = XALLOCAVEC (tree, num_args);
3485 : :
3486 : 0 : var = gfc_create_var (pchar_type_node, "pstr");
3487 : 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3488 : :
3489 : 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3490 : 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3491 : 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3492 : :
3493 : 0 : fndecl = build_addr (gfor_fndecl_ttynam);
3494 : 0 : tmp = build_call_array_loc (input_location,
3495 : 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_ttynam)),
3496 : : fndecl, num_args, args);
3497 : 0 : gfc_add_expr_to_block (&se->pre, tmp);
3498 : :
3499 : : /* Free the temporary afterwards, if necessary. */
3500 : 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3501 : 0 : len, build_int_cst (TREE_TYPE (len), 0));
3502 : 0 : tmp = gfc_call_free (var);
3503 : 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3504 : 0 : gfc_add_expr_to_block (&se->post, tmp);
3505 : :
3506 : 0 : se->expr = var;
3507 : 0 : se->string_length = len;
3508 : 0 : }
3509 : :
3510 : :
3511 : : /* Get the minimum/maximum value of all the parameters.
3512 : : minmax (a1, a2, a3, ...)
3513 : : {
3514 : : mvar = a1;
3515 : : mvar = COMP (mvar, a2)
3516 : : mvar = COMP (mvar, a3)
3517 : : ...
3518 : : return mvar;
3519 : : }
3520 : : Where COMP is MIN/MAX_EXPR for integral types or when we don't
3521 : : care about NaNs, or IFN_FMIN/MAX when the target has support for
3522 : : fast NaN-honouring min/max. When neither holds expand a sequence
3523 : : of explicit comparisons. */
3524 : :
3525 : : /* TODO: Mismatching types can occur when specific names are used.
3526 : : These should be handled during resolution. */
3527 : : static void
3528 : 1302 : gfc_conv_intrinsic_minmax (gfc_se * se, gfc_expr * expr, enum tree_code op)
3529 : : {
3530 : 1302 : tree tmp;
3531 : 1302 : tree mvar;
3532 : 1302 : tree val;
3533 : 1302 : tree *args;
3534 : 1302 : tree type;
3535 : 1302 : tree argtype;
3536 : 1302 : gfc_actual_arglist *argexpr;
3537 : 1302 : unsigned int i, nargs;
3538 : :
3539 : 1302 : nargs = gfc_intrinsic_argument_list_length (expr);
3540 : 1302 : args = XALLOCAVEC (tree, nargs);
3541 : :
3542 : 1302 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
3543 : 1302 : type = gfc_typenode_for_spec (&expr->ts);
3544 : :
3545 : : /* Only evaluate the argument once. */
3546 : 1302 : if (!VAR_P (args[0]) && !TREE_CONSTANT (args[0]))
3547 : 303 : args[0] = gfc_evaluate_now (args[0], &se->pre);
3548 : :
3549 : : /* Determine suitable type of temporary, as a GNU extension allows
3550 : : different argument kinds. */
3551 : 1302 : argtype = TREE_TYPE (args[0]);
3552 : 1302 : argexpr = expr->value.function.actual;
3553 : 2823 : for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3554 : : {
3555 : 1521 : tree tmptype = TREE_TYPE (args[i]);
3556 : 1521 : if (TYPE_PRECISION (tmptype) > TYPE_PRECISION (argtype))
3557 : 1 : argtype = tmptype;
3558 : : }
3559 : 1302 : mvar = gfc_create_var (argtype, "M");
3560 : 1302 : gfc_add_modify (&se->pre, mvar, convert (argtype, args[0]));
3561 : :
3562 : 1302 : argexpr = expr->value.function.actual;
3563 : 2823 : for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3564 : : {
3565 : 1521 : tree cond = NULL_TREE;
3566 : 1521 : val = args[i];
3567 : :
3568 : : /* Handle absent optional arguments by ignoring the comparison. */
3569 : 1521 : if (argexpr->expr->expr_type == EXPR_VARIABLE
3570 : 902 : && argexpr->expr->symtree->n.sym->attr.optional
3571 : 45 : && INDIRECT_REF_P (val))
3572 : : {
3573 : 84 : cond = fold_build2_loc (input_location,
3574 : : NE_EXPR, logical_type_node,
3575 : 42 : TREE_OPERAND (val, 0),
3576 : 42 : build_int_cst (TREE_TYPE (TREE_OPERAND (val, 0)), 0));
3577 : : }
3578 : 1479 : else if (!VAR_P (val) && !TREE_CONSTANT (val))
3579 : : /* Only evaluate the argument once. */
3580 : 573 : val = gfc_evaluate_now (val, &se->pre);
3581 : :
3582 : 1521 : tree calc;
3583 : : /* For floating point types, the question is what MAX(a, NaN) or
3584 : : MIN(a, NaN) should return (where "a" is a normal number).
3585 : : There are valid use case for returning either one, but the
3586 : : Fortran standard doesn't specify which one should be chosen.
3587 : : Also, there is no consensus among other tested compilers. In
3588 : : short, it's a mess. So lets just do whatever is fastest. */
3589 : 1521 : tree_code code = op == GT_EXPR ? MAX_EXPR : MIN_EXPR;
3590 : 1521 : calc = fold_build2_loc (input_location, code, argtype,
3591 : : convert (argtype, val), mvar);
3592 : 1521 : tmp = build2_v (MODIFY_EXPR, mvar, calc);
3593 : :
3594 : 1521 : if (cond != NULL_TREE)
3595 : 42 : tmp = build3_v (COND_EXPR, cond, tmp,
3596 : : build_empty_stmt (input_location));
3597 : 1521 : gfc_add_expr_to_block (&se->pre, tmp);
3598 : : }
3599 : 1302 : se->expr = convert (type, mvar);
3600 : 1302 : }
3601 : :
3602 : :
3603 : : /* Generate library calls for MIN and MAX intrinsics for character
3604 : : variables. */
3605 : : static void
3606 : 282 : gfc_conv_intrinsic_minmax_char (gfc_se * se, gfc_expr * expr, int op)
3607 : : {
3608 : 282 : tree *args;
3609 : 282 : tree var, len, fndecl, tmp, cond, function;
3610 : 282 : unsigned int nargs;
3611 : :
3612 : 282 : nargs = gfc_intrinsic_argument_list_length (expr);
3613 : 282 : args = XALLOCAVEC (tree, nargs + 4);
3614 : 282 : gfc_conv_intrinsic_function_args (se, expr, &args[4], nargs);
3615 : :
3616 : : /* Create the result variables. */
3617 : 282 : len = gfc_create_var (gfc_charlen_type_node, "len");
3618 : 282 : args[0] = gfc_build_addr_expr (NULL_TREE, len);
3619 : 282 : var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
3620 : 282 : args[1] = gfc_build_addr_expr (ppvoid_type_node, var);
3621 : 282 : args[2] = build_int_cst (integer_type_node, op);
3622 : 282 : args[3] = build_int_cst (integer_type_node, nargs / 2);
3623 : :
3624 : 282 : if (expr->ts.kind == 1)
3625 : 210 : function = gfor_fndecl_string_minmax;
3626 : 72 : else if (expr->ts.kind == 4)
3627 : 72 : function = gfor_fndecl_string_minmax_char4;
3628 : : else
3629 : 0 : gcc_unreachable ();
3630 : :
3631 : : /* Make the function call. */
3632 : 282 : fndecl = build_addr (function);
3633 : 282 : tmp = build_call_array_loc (input_location,
3634 : 282 : TREE_TYPE (TREE_TYPE (function)), fndecl,
3635 : : nargs + 4, args);
3636 : 282 : gfc_add_expr_to_block (&se->pre, tmp);
3637 : :
3638 : : /* Free the temporary afterwards, if necessary. */
3639 : 282 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3640 : 282 : len, build_int_cst (TREE_TYPE (len), 0));
3641 : 282 : tmp = gfc_call_free (var);
3642 : 282 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3643 : 282 : gfc_add_expr_to_block (&se->post, tmp);
3644 : :
3645 : 282 : se->expr = var;
3646 : 282 : se->string_length = len;
3647 : 282 : }
3648 : :
3649 : :
3650 : : /* Create a symbol node for this intrinsic. The symbol from the frontend
3651 : : has the generic name. */
3652 : :
3653 : : static gfc_symbol *
3654 : 11309 : gfc_get_symbol_for_expr (gfc_expr * expr, bool ignore_optional)
3655 : : {
3656 : 11309 : gfc_symbol *sym;
3657 : :
3658 : : /* TODO: Add symbols for intrinsic function to the global namespace. */
3659 : 11309 : gcc_assert (strlen (expr->value.function.name) <= GFC_MAX_SYMBOL_LEN - 5);
3660 : 11309 : sym = gfc_new_symbol (expr->value.function.name, NULL);
3661 : :
3662 : 11309 : sym->ts = expr->ts;
3663 : 11309 : if (sym->ts.type == BT_CHARACTER)
3664 : 1781 : sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3665 : 11309 : sym->attr.external = 1;
3666 : 11309 : sym->attr.function = 1;
3667 : 11309 : sym->attr.always_explicit = 1;
3668 : 11309 : sym->attr.proc = PROC_INTRINSIC;
3669 : 11309 : sym->attr.flavor = FL_PROCEDURE;
3670 : 11309 : sym->result = sym;
3671 : 11309 : if (expr->rank > 0)
3672 : : {
3673 : 9960 : sym->attr.dimension = 1;
3674 : 9960 : sym->as = gfc_get_array_spec ();
3675 : 9960 : sym->as->type = AS_ASSUMED_SHAPE;
3676 : 9960 : sym->as->rank = expr->rank;
3677 : : }
3678 : :
3679 : 11309 : gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3680 : : ignore_optional ? expr->value.function.actual
3681 : : : NULL);
3682 : :
3683 : 11309 : return sym;
3684 : : }
3685 : :
3686 : : /* Remove empty actual arguments. */
3687 : :
3688 : : static void
3689 : 8277 : remove_empty_actual_arguments (gfc_actual_arglist **ap)
3690 : : {
3691 : 44456 : while (*ap)
3692 : : {
3693 : 36179 : if ((*ap)->expr == NULL)
3694 : : {
3695 : 11076 : gfc_actual_arglist *r = *ap;
3696 : 11076 : *ap = r->next;
3697 : 11076 : r->next = NULL;
3698 : 11076 : gfc_free_actual_arglist (r);
3699 : : }
3700 : : else
3701 : 25103 : ap = &((*ap)->next);
3702 : : }
3703 : 8277 : }
3704 : :
3705 : : #define MAX_SPEC_ARG 12
3706 : :
3707 : : /* Make up an fn spec that's right for intrinsic functions that we
3708 : : want to call. */
3709 : :
3710 : : static char *
3711 : 1939 : intrinsic_fnspec (gfc_expr *expr)
3712 : : {
3713 : 1939 : static char fnspec_buf[MAX_SPEC_ARG*2+1];
3714 : 1939 : char *fp;
3715 : 1939 : int i;
3716 : 1939 : int num_char_args;
3717 : :
3718 : : #define ADD_CHAR(c) do { *fp++ = c; *fp++ = ' '; } while(0)
3719 : :
3720 : : /* Set the fndecl. */
3721 : 1939 : fp = fnspec_buf;
3722 : : /* Function return value. FIXME: Check if the second letter could
3723 : : be something other than a space, for further optimization. */
3724 : 1939 : ADD_CHAR ('.');
3725 : 1939 : if (expr->rank == 0)
3726 : : {
3727 : 238 : if (expr->ts.type == BT_CHARACTER)
3728 : : {
3729 : 84 : ADD_CHAR ('w'); /* Address of character. */
3730 : 84 : ADD_CHAR ('.'); /* Length of character. */
3731 : : }
3732 : : }
3733 : : else
3734 : 1701 : ADD_CHAR ('w'); /* Return value is a descriptor. */
3735 : :
3736 : 1939 : num_char_args = 0;
3737 : 10224 : for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
3738 : : {
3739 : 8285 : if (a->expr == NULL)
3740 : 2565 : continue;
3741 : :
3742 : 5720 : if (a->name && strcmp (a->name,"%VAL") == 0)
3743 : 1300 : ADD_CHAR ('.');
3744 : : else
3745 : : {
3746 : 4420 : if (a->expr->rank > 0)
3747 : 2575 : ADD_CHAR ('r');
3748 : : else
3749 : 1845 : ADD_CHAR ('R');
3750 : : }
3751 : 5720 : num_char_args += a->expr->ts.type == BT_CHARACTER;
3752 : 5720 : gcc_assert (fp - fnspec_buf + num_char_args <= MAX_SPEC_ARG*2);
3753 : : }
3754 : :
3755 : 2743 : for (i = 0; i < num_char_args; i++)
3756 : 804 : ADD_CHAR ('.');
3757 : :
3758 : 1939 : *fp = '\0';
3759 : 1939 : return fnspec_buf;
3760 : : }
3761 : :
3762 : : #undef MAX_SPEC_ARG
3763 : : #undef ADD_CHAR
3764 : :
3765 : : /* Generate the right symbol for the specific intrinsic function and
3766 : : modify the expr accordingly. This assumes that absent optional
3767 : : arguments should be removed. */
3768 : :
3769 : : gfc_symbol *
3770 : 8277 : specific_intrinsic_symbol (gfc_expr *expr)
3771 : : {
3772 : 8277 : gfc_symbol *sym;
3773 : :
3774 : 8277 : sym = gfc_find_intrinsic_symbol (expr);
3775 : 8277 : if (sym == NULL)
3776 : : {
3777 : 1939 : sym = gfc_get_intrinsic_function_symbol (expr);
3778 : 1939 : sym->ts = expr->ts;
3779 : 1939 : if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl)
3780 : 240 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
3781 : :
3782 : 1939 : gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3783 : : expr->value.function.actual, true);
3784 : 1939 : sym->backend_decl
3785 : 1939 : = gfc_get_extern_function_decl (sym, expr->value.function.actual,
3786 : 1939 : intrinsic_fnspec (expr));
3787 : : }
3788 : :
3789 : 8277 : remove_empty_actual_arguments (&(expr->value.function.actual));
3790 : :
3791 : 8277 : return sym;
3792 : : }
3793 : :
3794 : : /* Generate a call to an external intrinsic function. FIXME: So far,
3795 : : this only works for functions which are called with well-defined
3796 : : types; CSHIFT and friends will come later. */
3797 : :
3798 : : static void
3799 : 13512 : gfc_conv_intrinsic_funcall (gfc_se * se, gfc_expr * expr)
3800 : : {
3801 : 13512 : gfc_symbol *sym;
3802 : 13512 : vec<tree, va_gc> *append_args;
3803 : 13512 : bool specific_symbol;
3804 : :
3805 : 13512 : gcc_assert (!se->ss || se->ss->info->expr == expr);
3806 : :
3807 : 13512 : if (se->ss)
3808 : 11601 : gcc_assert (expr->rank > 0);
3809 : : else
3810 : 1911 : gcc_assert (expr->rank == 0);
3811 : :
3812 : 13512 : switch (expr->value.function.isym->id)
3813 : : {
3814 : : case GFC_ISYM_ANY:
3815 : : case GFC_ISYM_ALL:
3816 : : case GFC_ISYM_FINDLOC:
3817 : : case GFC_ISYM_MAXLOC:
3818 : : case GFC_ISYM_MINLOC:
3819 : : case GFC_ISYM_MAXVAL:
3820 : : case GFC_ISYM_MINVAL:
3821 : : case GFC_ISYM_NORM2:
3822 : : case GFC_ISYM_PRODUCT:
3823 : : case GFC_ISYM_SUM:
3824 : : specific_symbol = true;
3825 : : break;
3826 : 5235 : default:
3827 : 5235 : specific_symbol = false;
3828 : : }
3829 : :
3830 : 13512 : if (specific_symbol)
3831 : : {
3832 : : /* Need to copy here because specific_intrinsic_symbol modifies
3833 : : expr to omit the absent optional arguments. */
3834 : 8277 : expr = gfc_copy_expr (expr);
3835 : 8277 : sym = specific_intrinsic_symbol (expr);
3836 : : }
3837 : : else
3838 : 5235 : sym = gfc_get_symbol_for_expr (expr, se->ignore_optional);
3839 : :
3840 : : /* Calls to libgfortran_matmul need to be appended special arguments,
3841 : : to be able to call the BLAS ?gemm functions if required and possible. */
3842 : 13512 : append_args = NULL;
3843 : 13512 : if (expr->value.function.isym->id == GFC_ISYM_MATMUL
3844 : 862 : && !expr->external_blas
3845 : 825 : && sym->ts.type != BT_LOGICAL)
3846 : : {
3847 : 809 : tree cint = gfc_get_int_type (gfc_c_int_kind);
3848 : :
3849 : 809 : if (flag_external_blas
3850 : 0 : && (sym->ts.type == BT_REAL || sym->ts.type == BT_COMPLEX)
3851 : 0 : && (sym->ts.kind == 4 || sym->ts.kind == 8))
3852 : : {
3853 : 0 : tree gemm_fndecl;
3854 : :
3855 : 0 : if (sym->ts.type == BT_REAL)
3856 : : {
3857 : 0 : if (sym->ts.kind == 4)
3858 : 0 : gemm_fndecl = gfor_fndecl_sgemm;
3859 : : else
3860 : 0 : gemm_fndecl = gfor_fndecl_dgemm;
3861 : : }
3862 : : else
3863 : : {
3864 : 0 : if (sym->ts.kind == 4)
3865 : 0 : gemm_fndecl = gfor_fndecl_cgemm;
3866 : : else
3867 : 0 : gemm_fndecl = gfor_fndecl_zgemm;
3868 : : }
3869 : :
3870 : 0 : vec_alloc (append_args, 3);
3871 : 0 : append_args->quick_push (build_int_cst (cint, 1));
3872 : 0 : append_args->quick_push (build_int_cst (cint,
3873 : 0 : flag_blas_matmul_limit));
3874 : 0 : append_args->quick_push (gfc_build_addr_expr (NULL_TREE,
3875 : : gemm_fndecl));
3876 : 0 : }
3877 : : else
3878 : : {
3879 : 809 : vec_alloc (append_args, 3);
3880 : 809 : append_args->quick_push (build_int_cst (cint, 0));
3881 : 809 : append_args->quick_push (build_int_cst (cint, 0));
3882 : 809 : append_args->quick_push (null_pointer_node);
3883 : : }
3884 : : }
3885 : : /* Non-character scalar reduce returns a pointer to a result of size set by
3886 : : the element size of 'array'. Setting 'sym' allocatable ensures that the
3887 : : result is deallocated at the appropriate time. */
3888 : 12703 : else if (expr->value.function.isym->id == GFC_ISYM_REDUCE
3889 : 102 : && expr->rank == 0 && expr->ts.type != BT_CHARACTER)
3890 : 96 : sym->attr.allocatable = 1;
3891 : :
3892 : :
3893 : 13512 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
3894 : : append_args);
3895 : :
3896 : 13512 : if (specific_symbol)
3897 : 8277 : gfc_free_expr (expr);
3898 : : else
3899 : 5235 : gfc_free_symbol (sym);
3900 : 13512 : }
3901 : :
3902 : : /* ANY and ALL intrinsics. ANY->op == NE_EXPR, ALL->op == EQ_EXPR.
3903 : : Implemented as
3904 : : any(a)
3905 : : {
3906 : : forall (i=...)
3907 : : if (a[i] != 0)
3908 : : return 1
3909 : : end forall
3910 : : return 0
3911 : : }
3912 : : all(a)
3913 : : {
3914 : : forall (i=...)
3915 : : if (a[i] == 0)
3916 : : return 0
3917 : : end forall
3918 : : return 1
3919 : : }
3920 : : */
3921 : : static void
3922 : 36952 : gfc_conv_intrinsic_anyall (gfc_se * se, gfc_expr * expr, enum tree_code op)
3923 : : {
3924 : 36952 : tree resvar;
3925 : 36952 : stmtblock_t block;
3926 : 36952 : stmtblock_t body;
3927 : 36952 : tree type;
3928 : 36952 : tree tmp;
3929 : 36952 : tree found;
3930 : 36952 : gfc_loopinfo loop;
3931 : 36952 : gfc_actual_arglist *actual;
3932 : 36952 : gfc_ss *arrayss;
3933 : 36952 : gfc_se arrayse;
3934 : 36952 : tree exit_label;
3935 : :
3936 : 36952 : if (se->ss)
3937 : : {
3938 : 0 : gfc_conv_intrinsic_funcall (se, expr);
3939 : 0 : return;
3940 : : }
3941 : :
3942 : 36952 : actual = expr->value.function.actual;
3943 : 36952 : type = gfc_typenode_for_spec (&expr->ts);
3944 : : /* Initialize the result. */
3945 : 36952 : resvar = gfc_create_var (type, "test");
3946 : 36952 : if (op == EQ_EXPR)
3947 : 419 : tmp = convert (type, boolean_true_node);
3948 : : else
3949 : 36533 : tmp = convert (type, boolean_false_node);
3950 : 36952 : gfc_add_modify (&se->pre, resvar, tmp);
3951 : :
3952 : : /* Walk the arguments. */
3953 : 36952 : arrayss = gfc_walk_expr (actual->expr);
3954 : 36952 : gcc_assert (arrayss != gfc_ss_terminator);
3955 : :
3956 : : /* Initialize the scalarizer. */
3957 : 36952 : gfc_init_loopinfo (&loop);
3958 : 36952 : exit_label = gfc_build_label_decl (NULL_TREE);
3959 : 36952 : TREE_USED (exit_label) = 1;
3960 : 36952 : gfc_add_ss_to_loop (&loop, arrayss);
3961 : :
3962 : : /* Initialize the loop. */
3963 : 36952 : gfc_conv_ss_startstride (&loop);
3964 : 36952 : gfc_conv_loop_setup (&loop, &expr->where);
3965 : :
3966 : 36952 : gfc_mark_ss_chain_used (arrayss, 1);
3967 : : /* Generate the loop body. */
3968 : 36952 : gfc_start_scalarized_body (&loop, &body);
3969 : :
3970 : : /* If the condition matches then set the return value. */
3971 : 36952 : gfc_start_block (&block);
3972 : 36952 : if (op == EQ_EXPR)
3973 : 419 : tmp = convert (type, boolean_false_node);
3974 : : else
3975 : 36533 : tmp = convert (type, boolean_true_node);
3976 : 36952 : gfc_add_modify (&block, resvar, tmp);
3977 : :
3978 : : /* And break out of the loop. */
3979 : 36952 : tmp = build1_v (GOTO_EXPR, exit_label);
3980 : 36952 : gfc_add_expr_to_block (&block, tmp);
3981 : :
3982 : 36952 : found = gfc_finish_block (&block);
3983 : :
3984 : : /* Check this element. */
3985 : 36952 : gfc_init_se (&arrayse, NULL);
3986 : 36952 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
3987 : 36952 : arrayse.ss = arrayss;
3988 : 36952 : gfc_conv_expr_val (&arrayse, actual->expr);
3989 : :
3990 : 36952 : gfc_add_block_to_block (&body, &arrayse.pre);
3991 : 36952 : tmp = fold_build2_loc (input_location, op, logical_type_node, arrayse.expr,
3992 : 36952 : build_int_cst (TREE_TYPE (arrayse.expr), 0));
3993 : 36952 : tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
3994 : 36952 : gfc_add_expr_to_block (&body, tmp);
3995 : 36952 : gfc_add_block_to_block (&body, &arrayse.post);
3996 : :
3997 : 36952 : gfc_trans_scalarizing_loops (&loop, &body);
3998 : :
3999 : : /* Add the exit label. */
4000 : 36952 : tmp = build1_v (LABEL_EXPR, exit_label);
4001 : 36952 : gfc_add_expr_to_block (&loop.pre, tmp);
4002 : :
4003 : 36952 : gfc_add_block_to_block (&se->pre, &loop.pre);
4004 : 36952 : gfc_add_block_to_block (&se->pre, &loop.post);
4005 : 36952 : gfc_cleanup_loop (&loop);
4006 : :
4007 : 36952 : se->expr = resvar;
4008 : : }
4009 : :
4010 : :
4011 : : /* Generate the constant 180 / pi, which is used in the conversion
4012 : : of acosd(), asind(), atand(), atan2d(). */
4013 : :
4014 : : static tree
4015 : 336 : rad2deg (int kind)
4016 : : {
4017 : 336 : tree retval;
4018 : 336 : mpfr_t pi, t0;
4019 : :
4020 : 336 : gfc_set_model_kind (kind);
4021 : 336 : mpfr_init (pi);
4022 : 336 : mpfr_init (t0);
4023 : 336 : mpfr_set_si (t0, 180, GFC_RND_MODE);
4024 : 336 : mpfr_const_pi (pi, GFC_RND_MODE);
4025 : 336 : mpfr_div (t0, t0, pi, GFC_RND_MODE);
4026 : 336 : retval = gfc_conv_mpfr_to_tree (t0, kind, 0);
4027 : 336 : mpfr_clear (t0);
4028 : 336 : mpfr_clear (pi);
4029 : 336 : return retval;
4030 : : }
4031 : :
4032 : :
4033 : : static gfc_intrinsic_map_t *
4034 : 546 : gfc_lookup_intrinsic (gfc_isym_id id)
4035 : : {
4036 : 546 : gfc_intrinsic_map_t *m = gfc_intrinsic_map;
4037 : 11154 : for (; m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
4038 : 11154 : if (id == m->id)
4039 : : break;
4040 : 546 : gcc_assert (id == m->id);
4041 : 546 : return m;
4042 : : }
4043 : :
4044 : :
4045 : : /* ACOSD(x) is translated into ACOS(x) * 180 / pi.
4046 : : ASIND(x) is translated into ASIN(x) * 180 / pi.
4047 : : ATAND(x) is translated into ATAN(x) * 180 / pi. */
4048 : :
4049 : : static void
4050 : 216 : gfc_conv_intrinsic_atrigd (gfc_se * se, gfc_expr * expr, gfc_isym_id id)
4051 : : {
4052 : 216 : tree arg;
4053 : 216 : tree atrigd;
4054 : 216 : tree type;
4055 : 216 : gfc_intrinsic_map_t *m;
4056 : :
4057 : 216 : type = gfc_typenode_for_spec (&expr->ts);
4058 : :
4059 : 216 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4060 : :
4061 : 216 : switch (id)
4062 : : {
4063 : 72 : case GFC_ISYM_ACOSD:
4064 : 72 : m = gfc_lookup_intrinsic (GFC_ISYM_ACOS);
4065 : 72 : break;
4066 : 72 : case GFC_ISYM_ASIND:
4067 : 72 : m = gfc_lookup_intrinsic (GFC_ISYM_ASIN);
4068 : 72 : break;
4069 : 72 : case GFC_ISYM_ATAND:
4070 : 72 : m = gfc_lookup_intrinsic (GFC_ISYM_ATAN);
4071 : 72 : break;
4072 : 0 : default:
4073 : 0 : gcc_unreachable ();
4074 : : }
4075 : 216 : atrigd = gfc_get_intrinsic_lib_fndecl (m, expr);
4076 : 216 : atrigd = build_call_expr_loc (input_location, atrigd, 1, arg);
4077 : :
4078 : 216 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atrigd,
4079 : : fold_convert (type, rad2deg (expr->ts.kind)));
4080 : 216 : }
4081 : :
4082 : :
4083 : : /* COTAN(X) is translated into -TAN(X+PI/2) for REAL argument and
4084 : : COS(X) / SIN(X) for COMPLEX argument. */
4085 : :
4086 : : static void
4087 : 102 : gfc_conv_intrinsic_cotan (gfc_se *se, gfc_expr *expr)
4088 : : {
4089 : 102 : gfc_intrinsic_map_t *m;
4090 : 102 : tree arg;
4091 : 102 : tree type;
4092 : :
4093 : 102 : type = gfc_typenode_for_spec (&expr->ts);
4094 : 102 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4095 : :
4096 : 102 : if (expr->ts.type == BT_REAL)
4097 : : {
4098 : 102 : tree tan;
4099 : 102 : tree tmp;
4100 : 102 : mpfr_t pio2;
4101 : :
4102 : : /* Create pi/2. */
4103 : 102 : gfc_set_model_kind (expr->ts.kind);
4104 : 102 : mpfr_init (pio2);
4105 : 102 : mpfr_const_pi (pio2, GFC_RND_MODE);
4106 : 102 : mpfr_div_ui (pio2, pio2, 2, GFC_RND_MODE);
4107 : 102 : tmp = gfc_conv_mpfr_to_tree (pio2, expr->ts.kind, 0);
4108 : 102 : mpfr_clear (pio2);
4109 : :
4110 : : /* Find tan builtin function. */
4111 : 102 : m = gfc_lookup_intrinsic (GFC_ISYM_TAN);
4112 : 102 : tan = gfc_get_intrinsic_lib_fndecl (m, expr);
4113 : 102 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, arg, tmp);
4114 : 102 : tan = build_call_expr_loc (input_location, tan, 1, tmp);
4115 : 102 : se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tan);
4116 : : }
4117 : : else
4118 : : {
4119 : 0 : tree sin;
4120 : 0 : tree cos;
4121 : :
4122 : : /* Find cos builtin function. */
4123 : 0 : m = gfc_lookup_intrinsic (GFC_ISYM_COS);
4124 : 0 : cos = gfc_get_intrinsic_lib_fndecl (m, expr);
4125 : 0 : cos = build_call_expr_loc (input_location, cos, 1, arg);
4126 : :
4127 : : /* Find sin builtin function. */
4128 : 0 : m = gfc_lookup_intrinsic (GFC_ISYM_SIN);
4129 : 0 : sin = gfc_get_intrinsic_lib_fndecl (m, expr);
4130 : 0 : sin = build_call_expr_loc (input_location, sin, 1, arg);
4131 : :
4132 : : /* Divide cos by sin. */
4133 : 0 : se->expr = fold_build2_loc (input_location, RDIV_EXPR, type, cos, sin);
4134 : : }
4135 : 102 : }
4136 : :
4137 : :
4138 : : /* COTAND(X) is translated into -TAND(X+90) for REAL argument. */
4139 : :
4140 : : static void
4141 : 108 : gfc_conv_intrinsic_cotand (gfc_se *se, gfc_expr *expr)
4142 : : {
4143 : 108 : tree arg;
4144 : 108 : tree type;
4145 : 108 : tree ninety_tree;
4146 : 108 : mpfr_t ninety;
4147 : :
4148 : 108 : type = gfc_typenode_for_spec (&expr->ts);
4149 : 108 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4150 : :
4151 : 108 : gfc_set_model_kind (expr->ts.kind);
4152 : :
4153 : : /* Build the tree for x + 90. */
4154 : 108 : mpfr_init_set_ui (ninety, 90, GFC_RND_MODE);
4155 : 108 : ninety_tree = gfc_conv_mpfr_to_tree (ninety, expr->ts.kind, 0);
4156 : 108 : arg = fold_build2_loc (input_location, PLUS_EXPR, type, arg, ninety_tree);
4157 : 108 : mpfr_clear (ninety);
4158 : :
4159 : : /* Find tand. */
4160 : 108 : gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_TAND);
4161 : 108 : tree tand = gfc_get_intrinsic_lib_fndecl (m, expr);
4162 : 108 : tand = build_call_expr_loc (input_location, tand, 1, arg);
4163 : :
4164 : 108 : se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tand);
4165 : 108 : }
4166 : :
4167 : :
4168 : : /* ATAN2D(Y,X) is translated into ATAN2(Y,X) * 180 / PI. */
4169 : :
4170 : : static void
4171 : 120 : gfc_conv_intrinsic_atan2d (gfc_se *se, gfc_expr *expr)
4172 : : {
4173 : 120 : tree args[2];
4174 : 120 : tree atan2d;
4175 : 120 : tree type;
4176 : :
4177 : 120 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
4178 : 120 : type = TREE_TYPE (args[0]);
4179 : :
4180 : 120 : gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_ATAN2);
4181 : 120 : atan2d = gfc_get_intrinsic_lib_fndecl (m, expr);
4182 : 120 : atan2d = build_call_expr_loc (input_location, atan2d, 2, args[0], args[1]);
4183 : :
4184 : 120 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atan2d,
4185 : : rad2deg (expr->ts.kind));
4186 : 120 : }
4187 : :
4188 : :
4189 : : /* COUNT(A) = Number of true elements in A. */
4190 : : static void
4191 : 143 : gfc_conv_intrinsic_count (gfc_se * se, gfc_expr * expr)
4192 : : {
4193 : 143 : tree resvar;
4194 : 143 : tree type;
4195 : 143 : stmtblock_t body;
4196 : 143 : tree tmp;
4197 : 143 : gfc_loopinfo loop;
4198 : 143 : gfc_actual_arglist *actual;
4199 : 143 : gfc_ss *arrayss;
4200 : 143 : gfc_se arrayse;
4201 : :
4202 : 143 : if (se->ss)
4203 : : {
4204 : 0 : gfc_conv_intrinsic_funcall (se, expr);
4205 : 0 : return;
4206 : : }
4207 : :
4208 : 143 : actual = expr->value.function.actual;
4209 : :
4210 : 143 : type = gfc_typenode_for_spec (&expr->ts);
4211 : : /* Initialize the result. */
4212 : 143 : resvar = gfc_create_var (type, "count");
4213 : 143 : gfc_add_modify (&se->pre, resvar, build_int_cst (type, 0));
4214 : :
4215 : : /* Walk the arguments. */
4216 : 143 : arrayss = gfc_walk_expr (actual->expr);
4217 : 143 : gcc_assert (arrayss != gfc_ss_terminator);
4218 : :
4219 : : /* Initialize the scalarizer. */
4220 : 143 : gfc_init_loopinfo (&loop);
4221 : 143 : gfc_add_ss_to_loop (&loop, arrayss);
4222 : :
4223 : : /* Initialize the loop. */
4224 : 143 : gfc_conv_ss_startstride (&loop);
4225 : 143 : gfc_conv_loop_setup (&loop, &expr->where);
4226 : :
4227 : 143 : gfc_mark_ss_chain_used (arrayss, 1);
4228 : : /* Generate the loop body. */
4229 : 143 : gfc_start_scalarized_body (&loop, &body);
4230 : :
4231 : 143 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (resvar),
4232 : 143 : resvar, build_int_cst (TREE_TYPE (resvar), 1));
4233 : 143 : tmp = build2_v (MODIFY_EXPR, resvar, tmp);
4234 : :
4235 : 143 : gfc_init_se (&arrayse, NULL);
4236 : 143 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
4237 : 143 : arrayse.ss = arrayss;
4238 : 143 : gfc_conv_expr_val (&arrayse, actual->expr);
4239 : 143 : tmp = build3_v (COND_EXPR, arrayse.expr, tmp,
4240 : : build_empty_stmt (input_location));
4241 : :
4242 : 143 : gfc_add_block_to_block (&body, &arrayse.pre);
4243 : 143 : gfc_add_expr_to_block (&body, tmp);
4244 : 143 : gfc_add_block_to_block (&body, &arrayse.post);
4245 : :
4246 : 143 : gfc_trans_scalarizing_loops (&loop, &body);
4247 : :
4248 : 143 : gfc_add_block_to_block (&se->pre, &loop.pre);
4249 : 143 : gfc_add_block_to_block (&se->pre, &loop.post);
4250 : 143 : gfc_cleanup_loop (&loop);
4251 : :
4252 : 143 : se->expr = resvar;
4253 : : }
4254 : :
4255 : :
4256 : : /* Update given gfc_se to have ss component pointing to the nested gfc_ss
4257 : : struct and return the corresponding loopinfo. */
4258 : :
4259 : : static gfc_loopinfo *
4260 : 3374 : enter_nested_loop (gfc_se *se)
4261 : : {
4262 : 3374 : se->ss = se->ss->nested_ss;
4263 : 3374 : gcc_assert (se->ss == se->ss->loop->ss);
4264 : :
4265 : 3374 : return se->ss->loop;
4266 : : }
4267 : :
4268 : : /* Build the condition for a mask, which may be optional. */
4269 : :
4270 : : static tree
4271 : 12763 : conv_mask_condition (gfc_se *maskse, gfc_expr *maskexpr,
4272 : : bool optional_mask)
4273 : : {
4274 : 12763 : tree present;
4275 : 12763 : tree type;
4276 : :
4277 : 12763 : if (optional_mask)
4278 : : {
4279 : 206 : type = TREE_TYPE (maskse->expr);
4280 : 206 : present = gfc_conv_expr_present (maskexpr->symtree->n.sym);
4281 : 206 : present = convert (type, present);
4282 : 206 : present = fold_build1_loc (input_location, TRUTH_NOT_EXPR, type,
4283 : : present);
4284 : 206 : return fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
4285 : 206 : type, present, maskse->expr);
4286 : : }
4287 : : else
4288 : 12557 : return maskse->expr;
4289 : : }
4290 : :
4291 : : /* Inline implementation of the sum and product intrinsics. */
4292 : : static void
4293 : 2463 : gfc_conv_intrinsic_arith (gfc_se * se, gfc_expr * expr, enum tree_code op,
4294 : : bool norm2)
4295 : : {
4296 : 2463 : tree resvar;
4297 : 2463 : tree scale = NULL_TREE;
4298 : 2463 : tree type;
4299 : 2463 : stmtblock_t body;
4300 : 2463 : stmtblock_t block;
4301 : 2463 : tree tmp;
4302 : 2463 : gfc_loopinfo loop, *ploop;
4303 : 2463 : gfc_actual_arglist *arg_array, *arg_mask;
4304 : 2463 : gfc_ss *arrayss = NULL;
4305 : 2463 : gfc_ss *maskss = NULL;
4306 : 2463 : gfc_se arrayse;
4307 : 2463 : gfc_se maskse;
4308 : 2463 : gfc_se *parent_se;
4309 : 2463 : gfc_expr *arrayexpr;
4310 : 2463 : gfc_expr *maskexpr;
4311 : 2463 : bool optional_mask;
4312 : :
4313 : 2463 : if (expr->rank > 0)
4314 : : {
4315 : 578 : gcc_assert (gfc_inline_intrinsic_function_p (expr));
4316 : : parent_se = se;
4317 : : }
4318 : : else
4319 : : parent_se = NULL;
4320 : :
4321 : 2463 : type = gfc_typenode_for_spec (&expr->ts);
4322 : : /* Initialize the result. */
4323 : 2463 : resvar = gfc_create_var (type, "val");
4324 : 2463 : if (norm2)
4325 : : {
4326 : : /* result = 0.0;
4327 : : scale = 1.0. */
4328 : 68 : scale = gfc_create_var (type, "scale");
4329 : 68 : gfc_add_modify (&se->pre, scale,
4330 : : gfc_build_const (type, integer_one_node));
4331 : 68 : tmp = gfc_build_const (type, integer_zero_node);
4332 : : }
4333 : 2395 : else if (op == PLUS_EXPR || op == BIT_IOR_EXPR || op == BIT_XOR_EXPR)
4334 : 1991 : tmp = gfc_build_const (type, integer_zero_node);
4335 : 404 : else if (op == NE_EXPR)
4336 : : /* PARITY. */
4337 : 36 : tmp = convert (type, boolean_false_node);
4338 : 368 : else if (op == BIT_AND_EXPR)
4339 : 24 : tmp = gfc_build_const (type, fold_build1_loc (input_location, NEGATE_EXPR,
4340 : : type, integer_one_node));
4341 : : else
4342 : 344 : tmp = gfc_build_const (type, integer_one_node);
4343 : :
4344 : 2463 : gfc_add_modify (&se->pre, resvar, tmp);
4345 : :
4346 : 2463 : arg_array = expr->value.function.actual;
4347 : :
4348 : 2463 : arrayexpr = arg_array->expr;
4349 : :
4350 : 2463 : if (op == NE_EXPR || norm2)
4351 : : {
4352 : : /* PARITY and NORM2. */
4353 : : maskexpr = NULL;
4354 : : optional_mask = false;
4355 : : }
4356 : : else
4357 : : {
4358 : 2359 : arg_mask = arg_array->next->next;
4359 : 2359 : gcc_assert (arg_mask != NULL);
4360 : 2359 : maskexpr = arg_mask->expr;
4361 : 371 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
4362 : 266 : && maskexpr->symtree->n.sym->attr.dummy
4363 : 2377 : && maskexpr->symtree->n.sym->attr.optional;
4364 : : }
4365 : :
4366 : 2463 : if (expr->rank == 0)
4367 : : {
4368 : : /* Walk the arguments. */
4369 : 1885 : arrayss = gfc_walk_expr (arrayexpr);
4370 : 1885 : gcc_assert (arrayss != gfc_ss_terminator);
4371 : :
4372 : 1885 : if (maskexpr && maskexpr->rank > 0)
4373 : : {
4374 : 223 : maskss = gfc_walk_expr (maskexpr);
4375 : 223 : gcc_assert (maskss != gfc_ss_terminator);
4376 : : }
4377 : : else
4378 : : maskss = NULL;
4379 : :
4380 : : /* Initialize the scalarizer. */
4381 : 1885 : gfc_init_loopinfo (&loop);
4382 : :
4383 : : /* We add the mask first because the number of iterations is
4384 : : taken from the last ss, and this breaks if an absent
4385 : : optional argument is used for mask. */
4386 : :
4387 : 1885 : if (maskexpr && maskexpr->rank > 0)
4388 : 223 : gfc_add_ss_to_loop (&loop, maskss);
4389 : 1885 : gfc_add_ss_to_loop (&loop, arrayss);
4390 : :
4391 : : /* Initialize the loop. */
4392 : 1885 : gfc_conv_ss_startstride (&loop);
4393 : 1885 : gfc_conv_loop_setup (&loop, &expr->where);
4394 : :
4395 : 1885 : if (maskexpr && maskexpr->rank > 0)
4396 : 223 : gfc_mark_ss_chain_used (maskss, 1);
4397 : 1885 : gfc_mark_ss_chain_used (arrayss, 1);
4398 : :
4399 : 1885 : ploop = &loop;
4400 : : }
4401 : : else
4402 : : /* All the work has been done in the parent loops. */
4403 : 578 : ploop = enter_nested_loop (se);
4404 : :
4405 : 2463 : gcc_assert (ploop);
4406 : :
4407 : : /* Generate the loop body. */
4408 : 2463 : gfc_start_scalarized_body (ploop, &body);
4409 : :
4410 : : /* If we have a mask, only add this element if the mask is set. */
4411 : 2463 : if (maskexpr && maskexpr->rank > 0)
4412 : : {
4413 : 307 : gfc_init_se (&maskse, parent_se);
4414 : 307 : gfc_copy_loopinfo_to_se (&maskse, ploop);
4415 : 307 : if (expr->rank == 0)
4416 : 223 : maskse.ss = maskss;
4417 : 307 : gfc_conv_expr_val (&maskse, maskexpr);
4418 : 307 : gfc_add_block_to_block (&body, &maskse.pre);
4419 : :
4420 : 307 : gfc_start_block (&block);
4421 : : }
4422 : : else
4423 : 2156 : gfc_init_block (&block);
4424 : :
4425 : : /* Do the actual summation/product. */
4426 : 2463 : gfc_init_se (&arrayse, parent_se);
4427 : 2463 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
4428 : 2463 : if (expr->rank == 0)
4429 : 1885 : arrayse.ss = arrayss;
4430 : 2463 : gfc_conv_expr_val (&arrayse, arrayexpr);
4431 : 2463 : gfc_add_block_to_block (&block, &arrayse.pre);
4432 : :
4433 : 2463 : if (norm2)
4434 : : {
4435 : : /* if (x (i) != 0.0)
4436 : : {
4437 : : absX = abs(x(i))
4438 : : if (absX > scale)
4439 : : {
4440 : : val = scale/absX;
4441 : : result = 1.0 + result * val * val;
4442 : : scale = absX;
4443 : : }
4444 : : else
4445 : : {
4446 : : val = absX/scale;
4447 : : result += val * val;
4448 : : }
4449 : : } */
4450 : 68 : tree res1, res2, cond, absX, val;
4451 : 68 : stmtblock_t ifblock1, ifblock2, ifblock3;
4452 : :
4453 : 68 : gfc_init_block (&ifblock1);
4454 : :
4455 : 68 : absX = gfc_create_var (type, "absX");
4456 : 68 : gfc_add_modify (&ifblock1, absX,
4457 : : fold_build1_loc (input_location, ABS_EXPR, type,
4458 : : arrayse.expr));
4459 : 68 : val = gfc_create_var (type, "val");
4460 : 68 : gfc_add_expr_to_block (&ifblock1, val);
4461 : :
4462 : 68 : gfc_init_block (&ifblock2);
4463 : 68 : gfc_add_modify (&ifblock2, val,
4464 : : fold_build2_loc (input_location, RDIV_EXPR, type, scale,
4465 : : absX));
4466 : 68 : res1 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4467 : 68 : res1 = fold_build2_loc (input_location, MULT_EXPR, type, resvar, res1);
4468 : 68 : res1 = fold_build2_loc (input_location, PLUS_EXPR, type, res1,
4469 : : gfc_build_const (type, integer_one_node));
4470 : 68 : gfc_add_modify (&ifblock2, resvar, res1);
4471 : 68 : gfc_add_modify (&ifblock2, scale, absX);
4472 : 68 : res1 = gfc_finish_block (&ifblock2);
4473 : :
4474 : 68 : gfc_init_block (&ifblock3);
4475 : 68 : gfc_add_modify (&ifblock3, val,
4476 : : fold_build2_loc (input_location, RDIV_EXPR, type, absX,
4477 : : scale));
4478 : 68 : res2 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4479 : 68 : res2 = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, res2);
4480 : 68 : gfc_add_modify (&ifblock3, resvar, res2);
4481 : 68 : res2 = gfc_finish_block (&ifblock3);
4482 : :
4483 : 68 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
4484 : : absX, scale);
4485 : 68 : tmp = build3_v (COND_EXPR, cond, res1, res2);
4486 : 68 : gfc_add_expr_to_block (&ifblock1, tmp);
4487 : 68 : tmp = gfc_finish_block (&ifblock1);
4488 : :
4489 : 68 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
4490 : : arrayse.expr,
4491 : : gfc_build_const (type, integer_zero_node));
4492 : :
4493 : 68 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
4494 : 68 : gfc_add_expr_to_block (&block, tmp);
4495 : : }
4496 : : else
4497 : : {
4498 : 2395 : tmp = fold_build2_loc (input_location, op, type, resvar, arrayse.expr);
4499 : 2395 : gfc_add_modify (&block, resvar, tmp);
4500 : : }
4501 : :
4502 : 2463 : gfc_add_block_to_block (&block, &arrayse.post);
4503 : :
4504 : 2463 : if (maskexpr && maskexpr->rank > 0)
4505 : : {
4506 : : /* We enclose the above in if (mask) {...} . If the mask is an
4507 : : optional argument, generate
4508 : : IF (.NOT. PRESENT(MASK) .OR. MASK(I)). */
4509 : 307 : tree ifmask;
4510 : 307 : tmp = gfc_finish_block (&block);
4511 : 307 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4512 : 307 : tmp = build3_v (COND_EXPR, ifmask, tmp,
4513 : : build_empty_stmt (input_location));
4514 : 307 : }
4515 : : else
4516 : 2156 : tmp = gfc_finish_block (&block);
4517 : 2463 : gfc_add_expr_to_block (&body, tmp);
4518 : :
4519 : 2463 : gfc_trans_scalarizing_loops (ploop, &body);
4520 : :
4521 : : /* For a scalar mask, enclose the loop in an if statement. */
4522 : 2463 : if (maskexpr && maskexpr->rank == 0)
4523 : : {
4524 : 64 : gfc_init_block (&block);
4525 : 64 : gfc_add_block_to_block (&block, &ploop->pre);
4526 : 64 : gfc_add_block_to_block (&block, &ploop->post);
4527 : 64 : tmp = gfc_finish_block (&block);
4528 : :
4529 : 64 : if (expr->rank > 0)
4530 : : {
4531 : 34 : tmp = build3_v (COND_EXPR, se->ss->info->data.scalar.value, tmp,
4532 : : build_empty_stmt (input_location));
4533 : 34 : gfc_advance_se_ss_chain (se);
4534 : : }
4535 : : else
4536 : : {
4537 : 30 : tree ifmask;
4538 : :
4539 : 30 : gcc_assert (expr->rank == 0);
4540 : 30 : gfc_init_se (&maskse, NULL);
4541 : 30 : gfc_conv_expr_val (&maskse, maskexpr);
4542 : 30 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4543 : 30 : tmp = build3_v (COND_EXPR, ifmask, tmp,
4544 : : build_empty_stmt (input_location));
4545 : : }
4546 : :
4547 : 64 : gfc_add_expr_to_block (&block, tmp);
4548 : 64 : gfc_add_block_to_block (&se->pre, &block);
4549 : 64 : gcc_assert (se->post.head == NULL);
4550 : : }
4551 : : else
4552 : : {
4553 : 2399 : gfc_add_block_to_block (&se->pre, &ploop->pre);
4554 : 2399 : gfc_add_block_to_block (&se->pre, &ploop->post);
4555 : : }
4556 : :
4557 : 2463 : if (expr->rank == 0)
4558 : 1885 : gfc_cleanup_loop (ploop);
4559 : :
4560 : 2463 : if (norm2)
4561 : : {
4562 : : /* result = scale * sqrt(result). */
4563 : 68 : tree sqrt;
4564 : 68 : sqrt = gfc_builtin_decl_for_float_kind (BUILT_IN_SQRT, expr->ts.kind);
4565 : 68 : resvar = build_call_expr_loc (input_location,
4566 : : sqrt, 1, resvar);
4567 : 68 : resvar = fold_build2_loc (input_location, MULT_EXPR, type, scale, resvar);
4568 : : }
4569 : :
4570 : 2463 : se->expr = resvar;
4571 : 2463 : }
4572 : :
4573 : :
4574 : : /* Inline implementation of the dot_product intrinsic. This function
4575 : : is based on gfc_conv_intrinsic_arith (the previous function). */
4576 : : static void
4577 : 113 : gfc_conv_intrinsic_dot_product (gfc_se * se, gfc_expr * expr)
4578 : : {
4579 : 113 : tree resvar;
4580 : 113 : tree type;
4581 : 113 : stmtblock_t body;
4582 : 113 : stmtblock_t block;
4583 : 113 : tree tmp;
4584 : 113 : gfc_loopinfo loop;
4585 : 113 : gfc_actual_arglist *actual;
4586 : 113 : gfc_ss *arrayss1, *arrayss2;
4587 : 113 : gfc_se arrayse1, arrayse2;
4588 : 113 : gfc_expr *arrayexpr1, *arrayexpr2;
4589 : :
4590 : 113 : type = gfc_typenode_for_spec (&expr->ts);
4591 : :
4592 : : /* Initialize the result. */
4593 : 113 : resvar = gfc_create_var (type, "val");
4594 : 113 : if (expr->ts.type == BT_LOGICAL)
4595 : 30 : tmp = build_int_cst (type, 0);
4596 : : else
4597 : 83 : tmp = gfc_build_const (type, integer_zero_node);
4598 : :
4599 : 113 : gfc_add_modify (&se->pre, resvar, tmp);
4600 : :
4601 : : /* Walk argument #1. */
4602 : 113 : actual = expr->value.function.actual;
4603 : 113 : arrayexpr1 = actual->expr;
4604 : 113 : arrayss1 = gfc_walk_expr (arrayexpr1);
4605 : 113 : gcc_assert (arrayss1 != gfc_ss_terminator);
4606 : :
4607 : : /* Walk argument #2. */
4608 : 113 : actual = actual->next;
4609 : 113 : arrayexpr2 = actual->expr;
4610 : 113 : arrayss2 = gfc_walk_expr (arrayexpr2);
4611 : 113 : gcc_assert (arrayss2 != gfc_ss_terminator);
4612 : :
4613 : : /* Initialize the scalarizer. */
4614 : 113 : gfc_init_loopinfo (&loop);
4615 : 113 : gfc_add_ss_to_loop (&loop, arrayss1);
4616 : 113 : gfc_add_ss_to_loop (&loop, arrayss2);
4617 : :
4618 : : /* Initialize the loop. */
4619 : 113 : gfc_conv_ss_startstride (&loop);
4620 : 113 : gfc_conv_loop_setup (&loop, &expr->where);
4621 : :
4622 : 113 : gfc_mark_ss_chain_used (arrayss1, 1);
4623 : 113 : gfc_mark_ss_chain_used (arrayss2, 1);
4624 : :
4625 : : /* Generate the loop body. */
4626 : 113 : gfc_start_scalarized_body (&loop, &body);
4627 : 113 : gfc_init_block (&block);
4628 : :
4629 : : /* Make the tree expression for [conjg(]array1[)]. */
4630 : 113 : gfc_init_se (&arrayse1, NULL);
4631 : 113 : gfc_copy_loopinfo_to_se (&arrayse1, &loop);
4632 : 113 : arrayse1.ss = arrayss1;
4633 : 113 : gfc_conv_expr_val (&arrayse1, arrayexpr1);
4634 : 113 : if (expr->ts.type == BT_COMPLEX)
4635 : 9 : arrayse1.expr = fold_build1_loc (input_location, CONJ_EXPR, type,
4636 : : arrayse1.expr);
4637 : 113 : gfc_add_block_to_block (&block, &arrayse1.pre);
4638 : :
4639 : : /* Make the tree expression for array2. */
4640 : 113 : gfc_init_se (&arrayse2, NULL);
4641 : 113 : gfc_copy_loopinfo_to_se (&arrayse2, &loop);
4642 : 113 : arrayse2.ss = arrayss2;
4643 : 113 : gfc_conv_expr_val (&arrayse2, arrayexpr2);
4644 : 113 : gfc_add_block_to_block (&block, &arrayse2.pre);
4645 : :
4646 : : /* Do the actual product and sum. */
4647 : 113 : if (expr->ts.type == BT_LOGICAL)
4648 : : {
4649 : 30 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, type,
4650 : : arrayse1.expr, arrayse2.expr);
4651 : 30 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, type, resvar, tmp);
4652 : : }
4653 : : else
4654 : : {
4655 : 83 : tmp = fold_build2_loc (input_location, MULT_EXPR, type, arrayse1.expr,
4656 : : arrayse2.expr);
4657 : 83 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, tmp);
4658 : : }
4659 : 113 : gfc_add_modify (&block, resvar, tmp);
4660 : :
4661 : : /* Finish up the loop block and the loop. */
4662 : 113 : tmp = gfc_finish_block (&block);
4663 : 113 : gfc_add_expr_to_block (&body, tmp);
4664 : :
4665 : 113 : gfc_trans_scalarizing_loops (&loop, &body);
4666 : 113 : gfc_add_block_to_block (&se->pre, &loop.pre);
4667 : 113 : gfc_add_block_to_block (&se->pre, &loop.post);
4668 : 113 : gfc_cleanup_loop (&loop);
4669 : :
4670 : 113 : se->expr = resvar;
4671 : 113 : }
4672 : :
4673 : :
4674 : : /* Tells whether the expression E is a reference to an optional variable whose
4675 : : presence is not known at compile time. Those are variable references without
4676 : : subreference; if there is a subreference, we can assume the variable is
4677 : : present. We have to special case full arrays, which we represent with a fake
4678 : : "full" reference, and class descriptors for which a reference to data is not
4679 : : really a subreference. */
4680 : :
4681 : : bool
4682 : 14613 : maybe_absent_optional_variable (gfc_expr *e)
4683 : : {
4684 : 14613 : if (!(e && e->expr_type == EXPR_VARIABLE))
4685 : : return false;
4686 : :
4687 : 1716 : gfc_symbol *sym = e->symtree->n.sym;
4688 : 1716 : if (!sym->attr.optional)
4689 : : return false;
4690 : :
4691 : 224 : gfc_ref *ref = e->ref;
4692 : 224 : if (ref == nullptr)
4693 : : return true;
4694 : :
4695 : 20 : if (ref->type == REF_ARRAY
4696 : 20 : && ref->u.ar.type == AR_FULL
4697 : 20 : && ref->next == nullptr)
4698 : : return true;
4699 : :
4700 : 0 : if (!(sym->ts.type == BT_CLASS
4701 : 0 : && ref->type == REF_COMPONENT
4702 : 0 : && ref->u.c.component == CLASS_DATA (sym)))
4703 : : return false;
4704 : :
4705 : 0 : gfc_ref *next_ref = ref->next;
4706 : 0 : if (next_ref == nullptr)
4707 : : return true;
4708 : :
4709 : 0 : if (next_ref->type == REF_ARRAY
4710 : 0 : && next_ref->u.ar.type == AR_FULL
4711 : 0 : && next_ref->next == nullptr)
4712 : 0 : return true;
4713 : :
4714 : : return false;
4715 : : }
4716 : :
4717 : :
4718 : : /* Emit code for minloc or maxloc intrinsic. There are many different cases
4719 : : we need to handle. For performance reasons we sometimes create two
4720 : : loops instead of one, where the second one is much simpler.
4721 : : Examples for minloc intrinsic:
4722 : : A: Result is scalar.
4723 : : 1) Array mask is used and NaNs need to be supported:
4724 : : limit = Infinity;
4725 : : pos = 0;
4726 : : S = from;
4727 : : while (S <= to) {
4728 : : if (mask[S]) {
4729 : : if (pos == 0) pos = S + (1 - from);
4730 : : if (a[S] <= limit) {
4731 : : limit = a[S];
4732 : : pos = S + (1 - from);
4733 : : goto lab1;
4734 : : }
4735 : : }
4736 : : S++;
4737 : : }
4738 : : goto lab2;
4739 : : lab1:;
4740 : : while (S <= to) {
4741 : : if (mask[S])
4742 : : if (a[S] < limit) {
4743 : : limit = a[S];
4744 : : pos = S + (1 - from);
4745 : : }
4746 : : S++;
4747 : : }
4748 : : lab2:;
4749 : : 2) NaNs need to be supported, but it is known at compile time or cheaply
4750 : : at runtime whether array is nonempty or not:
4751 : : limit = Infinity;
4752 : : pos = 0;
4753 : : S = from;
4754 : : while (S <= to) {
4755 : : if (a[S] <= limit) {
4756 : : limit = a[S];
4757 : : pos = S + (1 - from);
4758 : : goto lab1;
4759 : : }
4760 : : S++;
4761 : : }
4762 : : if (from <= to) pos = 1;
4763 : : goto lab2;
4764 : : lab1:;
4765 : : while (S <= to) {
4766 : : if (a[S] < limit) {
4767 : : limit = a[S];
4768 : : pos = S + (1 - from);
4769 : : }
4770 : : S++;
4771 : : }
4772 : : lab2:;
4773 : : 3) NaNs aren't supported, array mask is used:
4774 : : limit = infinities_supported ? Infinity : huge (limit);
4775 : : pos = 0;
4776 : : S = from;
4777 : : while (S <= to) {
4778 : : if (mask[S]) {
4779 : : limit = a[S];
4780 : : pos = S + (1 - from);
4781 : : goto lab1;
4782 : : }
4783 : : S++;
4784 : : }
4785 : : goto lab2;
4786 : : lab1:;
4787 : : while (S <= to) {
4788 : : if (mask[S])
4789 : : if (a[S] < limit) {
4790 : : limit = a[S];
4791 : : pos = S + (1 - from);
4792 : : }
4793 : : S++;
4794 : : }
4795 : : lab2:;
4796 : : 4) Same without array mask:
4797 : : limit = infinities_supported ? Infinity : huge (limit);
4798 : : pos = (from <= to) ? 1 : 0;
4799 : : S = from;
4800 : : while (S <= to) {
4801 : : if (a[S] < limit) {
4802 : : limit = a[S];
4803 : : pos = S + (1 - from);
4804 : : }
4805 : : S++;
4806 : : }
4807 : : B: Array result, non-CHARACTER type, DIM absent
4808 : : Generate similar code as in the scalar case, using a collection of
4809 : : variables (one per dimension) instead of a single variable as result.
4810 : : Picking only cases 1) and 4) with ARRAY of rank 2, the generated code
4811 : : becomes:
4812 : : 1) Array mask is used and NaNs need to be supported:
4813 : : limit = Infinity;
4814 : : pos0 = 0;
4815 : : pos1 = 0;
4816 : : S1 = from1;
4817 : : second_loop_entry = false;
4818 : : while (S1 <= to1) {
4819 : : S0 = from0;
4820 : : while (s0 <= to0 {
4821 : : if (mask[S1][S0]) {
4822 : : if (pos0 == 0) {
4823 : : pos0 = S0 + (1 - from0);
4824 : : pos1 = S1 + (1 - from1);
4825 : : }
4826 : : if (a[S1][S0] <= limit) {
4827 : : limit = a[S1][S0];
4828 : : pos0 = S0 + (1 - from0);
4829 : : pos1 = S1 + (1 - from1);
4830 : : second_loop_entry = true;
4831 : : goto lab1;
4832 : : }
4833 : : }
4834 : : S0++;
4835 : : }
4836 : : S1++;
4837 : : }
4838 : : goto lab2;
4839 : : lab1:;
4840 : : S1 = second_loop_entry ? S1 : from1;
4841 : : while (S1 <= to1) {
4842 : : S0 = second_loop_entry ? S0 : from0;
4843 : : while (S0 <= to0) {
4844 : : if (mask[S1][S0])
4845 : : if (a[S1][S0] < limit) {
4846 : : limit = a[S1][S0];
4847 : : pos0 = S + (1 - from0);
4848 : : pos1 = S + (1 - from1);
4849 : : }
4850 : : second_loop_entry = false;
4851 : : S0++;
4852 : : }
4853 : : S1++;
4854 : : }
4855 : : lab2:;
4856 : : result = { pos0, pos1 };
4857 : : ...
4858 : : 4) NANs aren't supported, no array mask.
4859 : : limit = infinities_supported ? Infinity : huge (limit);
4860 : : pos0 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4861 : : pos1 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4862 : : S1 = from1;
4863 : : while (S1 <= to1) {
4864 : : S0 = from0;
4865 : : while (S0 <= to0) {
4866 : : if (a[S1][S0] < limit) {
4867 : : limit = a[S1][S0];
4868 : : pos0 = S + (1 - from0);
4869 : : pos1 = S + (1 - from1);
4870 : : }
4871 : : S0++;
4872 : : }
4873 : : S1++;
4874 : : }
4875 : : result = { pos0, pos1 };
4876 : : C: Otherwise, a call is generated.
4877 : : For 2) and 4), if mask is scalar, this all goes into a conditional,
4878 : : setting pos = 0; in the else branch.
4879 : :
4880 : : Since we now also support the BACK argument, instead of using
4881 : : if (a[S] < limit), we now use
4882 : :
4883 : : if (back)
4884 : : cond = a[S] <= limit;
4885 : : else
4886 : : cond = a[S] < limit;
4887 : : if (cond) {
4888 : : ....
4889 : :
4890 : : The optimizer is smart enough to move the condition out of the loop.
4891 : : They are now marked as unlikely too for further speedup. */
4892 : :
4893 : : static void
4894 : 18898 : gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op)
4895 : : {
4896 : 18898 : stmtblock_t body;
4897 : 18898 : stmtblock_t block;
4898 : 18898 : stmtblock_t ifblock;
4899 : 18898 : stmtblock_t elseblock;
4900 : 18898 : tree limit;
4901 : 18898 : tree type;
4902 : 18898 : tree tmp;
4903 : 18898 : tree cond;
4904 : 18898 : tree elsetmp;
4905 : 18898 : tree ifbody;
4906 : 18898 : tree offset[GFC_MAX_DIMENSIONS];
4907 : 18898 : tree nonempty;
4908 : 18898 : tree lab1, lab2;
4909 : 18898 : tree b_if, b_else;
4910 : 18898 : tree back;
4911 : 18898 : gfc_loopinfo loop, *ploop;
4912 : 18898 : gfc_actual_arglist *array_arg, *dim_arg, *mask_arg, *kind_arg;
4913 : 18898 : gfc_actual_arglist *back_arg;
4914 : 18898 : gfc_ss *arrayss = nullptr;
4915 : 18898 : gfc_ss *maskss = nullptr;
4916 : 18898 : gfc_ss *orig_ss = nullptr;
4917 : 18898 : gfc_se arrayse;
4918 : 18898 : gfc_se maskse;
4919 : 18898 : gfc_se nested_se;
4920 : 18898 : gfc_se *base_se;
4921 : 18898 : gfc_expr *arrayexpr;
4922 : 18898 : gfc_expr *maskexpr;
4923 : 18898 : gfc_expr *backexpr;
4924 : 18898 : gfc_se backse;
4925 : 18898 : tree pos[GFC_MAX_DIMENSIONS];
4926 : 18898 : tree idx[GFC_MAX_DIMENSIONS];
4927 : 18898 : tree result_var = NULL_TREE;
4928 : 18898 : int n;
4929 : 18898 : bool optional_mask;
4930 : :
4931 : 18898 : array_arg = expr->value.function.actual;
4932 : 18898 : dim_arg = array_arg->next;
4933 : 18898 : mask_arg = dim_arg->next;
4934 : 18898 : kind_arg = mask_arg->next;
4935 : 18898 : back_arg = kind_arg->next;
4936 : :
4937 : 18898 : bool dim_present = dim_arg->expr != nullptr;
4938 : 18898 : bool nested_loop = dim_present && expr->rank > 0;
4939 : :
4940 : : /* Remove kind. */
4941 : 18898 : if (kind_arg->expr)
4942 : : {
4943 : 2240 : gfc_free_expr (kind_arg->expr);
4944 : 2240 : kind_arg->expr = NULL;
4945 : : }
4946 : :
4947 : : /* Pass BACK argument by value. */
4948 : 18898 : back_arg->name = "%VAL";
4949 : :
4950 : 18898 : if (se->ss)
4951 : : {
4952 : 14732 : if (se->ss->info->useflags)
4953 : : {
4954 : 7671 : if (!dim_present || !gfc_inline_intrinsic_function_p (expr))
4955 : : {
4956 : : /* The code generating and initializing the result array has been
4957 : : generated already before the scalarization loop, either with a
4958 : : library function call or with inline code; now we can just use
4959 : : the result. */
4960 : 4875 : gfc_conv_tmp_array_ref (se);
4961 : 13822 : return;
4962 : : }
4963 : : }
4964 : 7061 : else if (!gfc_inline_intrinsic_function_p (expr))
4965 : : {
4966 : 3780 : gfc_conv_intrinsic_funcall (se, expr);
4967 : 3780 : return;
4968 : : }
4969 : : }
4970 : :
4971 : 10243 : arrayexpr = array_arg->expr;
4972 : :
4973 : : /* Special case for character maxloc. Remove unneeded "dim" actual
4974 : : argument, then call a library function. */
4975 : :
4976 : 10243 : if (arrayexpr->ts.type == BT_CHARACTER)
4977 : : {
4978 : 292 : gcc_assert (expr->rank == 0);
4979 : :
4980 : 292 : if (dim_arg->expr)
4981 : : {
4982 : 292 : gfc_free_expr (dim_arg->expr);
4983 : 292 : dim_arg->expr = NULL;
4984 : : }
4985 : 292 : gfc_conv_intrinsic_funcall (se, expr);
4986 : 292 : return;
4987 : : }
4988 : :
4989 : 9951 : type = gfc_typenode_for_spec (&expr->ts);
4990 : :
4991 : 9951 : if (expr->rank > 0 && !dim_present)
4992 : : {
4993 : 3281 : gfc_array_spec as;
4994 : 3281 : memset (&as, 0, sizeof (as));
4995 : :
4996 : 3281 : as.rank = 1;
4997 : 3281 : as.lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
4998 : : &arrayexpr->where,
4999 : : HOST_WIDE_INT_1);
5000 : 6562 : as.upper[0] = gfc_get_int_expr (gfc_index_integer_kind,
5001 : : &arrayexpr->where,
5002 : 3281 : arrayexpr->rank);
5003 : :
5004 : 3281 : tree array = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
5005 : :
5006 : 3281 : result_var = gfc_create_var (array, "loc_result");
5007 : : }
5008 : :
5009 : 7155 : const int reduction_dimensions = dim_present ? 1 : arrayexpr->rank;
5010 : :
5011 : : /* Initialize the result. */
5012 : 22177 : for (int i = 0; i < reduction_dimensions; i++)
5013 : : {
5014 : 12226 : pos[i] = gfc_create_var (gfc_array_index_type,
5015 : : gfc_get_string ("pos%d", i));
5016 : 12226 : offset[i] = gfc_create_var (gfc_array_index_type,
5017 : : gfc_get_string ("offset%d", i));
5018 : 12226 : idx[i] = gfc_create_var (gfc_array_index_type,
5019 : : gfc_get_string ("idx%d", i));
5020 : : }
5021 : :
5022 : 9951 : maskexpr = mask_arg->expr;
5023 : 6518 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5024 : 5329 : && maskexpr->symtree->n.sym->attr.dummy
5025 : 10116 : && maskexpr->symtree->n.sym->attr.optional;
5026 : 9951 : backexpr = back_arg->expr;
5027 : :
5028 : 17106 : gfc_init_se (&backse, nested_loop ? se : nullptr);
5029 : 9951 : if (backexpr == nullptr)
5030 : 0 : back = logical_false_node;
5031 : 9951 : else if (maybe_absent_optional_variable (backexpr))
5032 : : {
5033 : : /* This should have been checked already by
5034 : : maybe_absent_optional_variable. */
5035 : 184 : gcc_checking_assert (backexpr->expr_type == EXPR_VARIABLE);
5036 : :
5037 : 184 : gfc_conv_expr (&backse, backexpr);
5038 : 184 : tree present = gfc_conv_expr_present (backexpr->symtree->n.sym, false);
5039 : 184 : back = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5040 : : logical_type_node, present, backse.expr);
5041 : : }
5042 : : else
5043 : : {
5044 : 9767 : gfc_conv_expr (&backse, backexpr);
5045 : 9767 : back = backse.expr;
5046 : : }
5047 : 9951 : gfc_add_block_to_block (&se->pre, &backse.pre);
5048 : 9951 : back = gfc_evaluate_now_loc (input_location, back, &se->pre);
5049 : 9951 : gfc_add_block_to_block (&se->pre, &backse.post);
5050 : :
5051 : 9951 : if (nested_loop)
5052 : : {
5053 : 2796 : gfc_init_se (&nested_se, se);
5054 : 2796 : base_se = &nested_se;
5055 : : }
5056 : : else
5057 : : {
5058 : : /* Walk the arguments. */
5059 : 7155 : arrayss = gfc_walk_expr (arrayexpr);
5060 : 7155 : gcc_assert (arrayss != gfc_ss_terminator);
5061 : :
5062 : 7155 : if (maskexpr && maskexpr->rank != 0)
5063 : : {
5064 : 2700 : maskss = gfc_walk_expr (maskexpr);
5065 : 2700 : gcc_assert (maskss != gfc_ss_terminator);
5066 : : }
5067 : :
5068 : : base_se = nullptr;
5069 : : }
5070 : :
5071 : 18091 : nonempty = nullptr;
5072 : 7448 : if (!(maskexpr && maskexpr->rank > 0))
5073 : : {
5074 : 6077 : mpz_t asize;
5075 : 6077 : bool reduction_size_known;
5076 : :
5077 : 6077 : if (dim_present)
5078 : : {
5079 : 4032 : int reduction_dim;
5080 : 4032 : if (dim_arg->expr->expr_type == EXPR_CONSTANT)
5081 : 4030 : reduction_dim = mpz_get_si (dim_arg->expr->value.integer) - 1;
5082 : 2 : else if (arrayexpr->rank == 1)
5083 : : reduction_dim = 0;
5084 : : else
5085 : 0 : gcc_unreachable ();
5086 : 4032 : reduction_size_known = gfc_array_dimen_size (arrayexpr, reduction_dim,
5087 : : &asize);
5088 : : }
5089 : : else
5090 : 2045 : reduction_size_known = gfc_array_size (arrayexpr, &asize);
5091 : :
5092 : 6077 : if (reduction_size_known)
5093 : : {
5094 : 4482 : nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
5095 : 4482 : mpz_clear (asize);
5096 : 4482 : nonempty = fold_build2_loc (input_location, GT_EXPR,
5097 : : logical_type_node, nonempty,
5098 : : gfc_index_zero_node);
5099 : : }
5100 : 6077 : maskss = NULL;
5101 : : }
5102 : :
5103 : 9951 : limit = gfc_create_var (gfc_typenode_for_spec (&arrayexpr->ts), "limit");
5104 : 9951 : switch (arrayexpr->ts.type)
5105 : : {
5106 : 3898 : case BT_REAL:
5107 : 3898 : tmp = gfc_build_inf_or_huge (TREE_TYPE (limit), arrayexpr->ts.kind);
5108 : 3898 : break;
5109 : :
5110 : 6029 : case BT_INTEGER:
5111 : 6029 : n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5112 : 6029 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
5113 : : arrayexpr->ts.kind);
5114 : 6029 : break;
5115 : :
5116 : 24 : case BT_UNSIGNED:
5117 : : /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
5118 : 24 : if (op == GT_EXPR)
5119 : : {
5120 : 12 : tmp = gfc_get_unsigned_type (arrayexpr->ts.kind);
5121 : 12 : tmp = build_int_cst (tmp, 0);
5122 : : }
5123 : : else
5124 : : {
5125 : 12 : n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5126 : 12 : tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
5127 : : expr->ts.kind);
5128 : : }
5129 : : break;
5130 : :
5131 : 0 : default:
5132 : 0 : gcc_unreachable ();
5133 : : }
5134 : :
5135 : : /* We start with the most negative possible value for MAXLOC, and the most
5136 : : positive possible value for MINLOC. The most negative possible value is
5137 : : -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
5138 : : possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
5139 : : with above. */
5140 : 9951 : if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
5141 : 4724 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
5142 : 4724 : if (op == GT_EXPR && arrayexpr->ts.type == BT_INTEGER)
5143 : 2914 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp), tmp,
5144 : 2914 : build_int_cst (TREE_TYPE (tmp), 1));
5145 : :
5146 : 9951 : gfc_add_modify (&se->pre, limit, tmp);
5147 : :
5148 : : /* If we are in a case where we generate two sets of loops, the second one
5149 : : should continue where the first stopped instead of restarting from the
5150 : : beginning. So nested loops in the second set should have a partial range
5151 : : on the first iteration, but they should start from the beginning and span
5152 : : their full range on the following iterations. So we use conditionals in
5153 : : the loops lower bounds, and use the following variable in those
5154 : : conditionals to decide whether to use the original loop bound or to use
5155 : : the index at which the loop from the first set stopped. */
5156 : 9951 : tree second_loop_entry = gfc_create_var (logical_type_node,
5157 : : "second_loop_entry");
5158 : 9951 : gfc_add_modify (&se->pre, second_loop_entry, logical_false_node);
5159 : :
5160 : 9951 : if (nested_loop)
5161 : : {
5162 : 2796 : ploop = enter_nested_loop (&nested_se);
5163 : 2796 : orig_ss = nested_se.ss;
5164 : 2796 : ploop->temp_dim = 1;
5165 : : }
5166 : : else
5167 : : {
5168 : : /* Initialize the scalarizer. */
5169 : 7155 : gfc_init_loopinfo (&loop);
5170 : :
5171 : : /* We add the mask first because the number of iterations is taken
5172 : : from the last ss, and this breaks if an absent optional argument
5173 : : is used for mask. */
5174 : :
5175 : 7155 : if (maskss)
5176 : 2700 : gfc_add_ss_to_loop (&loop, maskss);
5177 : :
5178 : 7155 : gfc_add_ss_to_loop (&loop, arrayss);
5179 : :
5180 : : /* Initialize the loop. */
5181 : 7155 : gfc_conv_ss_startstride (&loop);
5182 : :
5183 : : /* The code generated can have more than one loop in sequence (see the
5184 : : comment at the function header). This doesn't work well with the
5185 : : scalarizer, which changes arrays' offset when the scalarization loops
5186 : : are generated (see gfc_trans_preloop_setup). Fortunately, we can use
5187 : : the scalarizer temporary code to handle multiple loops. Thus, we set
5188 : : temp_dim here, we call gfc_mark_ss_chain_used with flag=3 later, and
5189 : : we use gfc_trans_scalarized_loop_boundary even later to restore
5190 : : offset. */
5191 : 7155 : loop.temp_dim = loop.dimen;
5192 : 7155 : gfc_conv_loop_setup (&loop, &expr->where);
5193 : :
5194 : 7155 : ploop = &loop;
5195 : : }
5196 : :
5197 : 9951 : gcc_assert (reduction_dimensions == ploop->dimen);
5198 : :
5199 : 9951 : if (nonempty == NULL && !(maskexpr && maskexpr->rank > 0))
5200 : : {
5201 : 1595 : nonempty = logical_true_node;
5202 : :
5203 : 3697 : for (int i = 0; i < ploop->dimen; i++)
5204 : : {
5205 : 2102 : if (!(ploop->from[i] && ploop->to[i]))
5206 : : {
5207 : : nonempty = NULL;
5208 : : break;
5209 : : }
5210 : :
5211 : 2102 : tree tmp = fold_build2_loc (input_location, LE_EXPR,
5212 : : logical_type_node, ploop->from[i],
5213 : : ploop->to[i]);
5214 : :
5215 : 2102 : nonempty = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5216 : : logical_type_node, nonempty, tmp);
5217 : : }
5218 : : }
5219 : :
5220 : 11546 : lab1 = NULL;
5221 : 11546 : lab2 = NULL;
5222 : : /* Initialize the position to zero, following Fortran 2003. We are free
5223 : : to do this because Fortran 95 allows the result of an entirely false
5224 : : mask to be processor dependent. If we know at compile time the array
5225 : : is non-empty and no MASK is used, we can initialize to 1 to simplify
5226 : : the inner loop. */
5227 : 9951 : if (nonempty != NULL && !HONOR_NANS (DECL_MODE (limit)))
5228 : : {
5229 : 3748 : tree init = fold_build3_loc (input_location, COND_EXPR,
5230 : : gfc_array_index_type, nonempty,
5231 : : gfc_index_one_node,
5232 : : gfc_index_zero_node);
5233 : 8430 : for (int i = 0; i < ploop->dimen; i++)
5234 : 4682 : gfc_add_modify (&ploop->pre, pos[i], init);
5235 : : }
5236 : : else
5237 : : {
5238 : 13747 : for (int i = 0; i < ploop->dimen; i++)
5239 : 7544 : gfc_add_modify (&ploop->pre, pos[i], gfc_index_zero_node);
5240 : 6203 : lab1 = gfc_build_label_decl (NULL_TREE);
5241 : 6203 : TREE_USED (lab1) = 1;
5242 : 6203 : lab2 = gfc_build_label_decl (NULL_TREE);
5243 : 6203 : TREE_USED (lab2) = 1;
5244 : : }
5245 : :
5246 : : /* An offset must be added to the loop
5247 : : counter to obtain the required position. */
5248 : 22177 : for (int i = 0; i < ploop->dimen; i++)
5249 : : {
5250 : 12226 : gcc_assert (ploop->from[i]);
5251 : :
5252 : 12226 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5253 : : gfc_index_one_node, ploop->from[i]);
5254 : 12226 : gfc_add_modify (&ploop->pre, offset[i], tmp);
5255 : : }
5256 : :
5257 : 9951 : if (!nested_loop)
5258 : : {
5259 : 9965 : gfc_mark_ss_chain_used (arrayss, lab1 ? 3 : 1);
5260 : 7155 : if (maskss)
5261 : 2700 : gfc_mark_ss_chain_used (maskss, lab1 ? 3 : 1);
5262 : : }
5263 : :
5264 : : /* Generate the loop body. */
5265 : 9951 : gfc_start_scalarized_body (ploop, &body);
5266 : :
5267 : : /* If we have a mask, only check this element if the mask is set. */
5268 : 9951 : if (maskexpr && maskexpr->rank > 0)
5269 : : {
5270 : 3874 : gfc_init_se (&maskse, base_se);
5271 : 3874 : gfc_copy_loopinfo_to_se (&maskse, ploop);
5272 : 3874 : if (!nested_loop)
5273 : 2700 : maskse.ss = maskss;
5274 : 3874 : gfc_conv_expr_val (&maskse, maskexpr);
5275 : 3874 : gfc_add_block_to_block (&body, &maskse.pre);
5276 : :
5277 : 3874 : gfc_start_block (&block);
5278 : : }
5279 : : else
5280 : 6077 : gfc_init_block (&block);
5281 : :
5282 : : /* Compare with the current limit. */
5283 : 9951 : gfc_init_se (&arrayse, base_se);
5284 : 9951 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
5285 : 9951 : if (!nested_loop)
5286 : 7155 : arrayse.ss = arrayss;
5287 : 9951 : gfc_conv_expr_val (&arrayse, arrayexpr);
5288 : 9951 : gfc_add_block_to_block (&block, &arrayse.pre);
5289 : :
5290 : : /* We do the following if this is a more extreme value. */
5291 : 9951 : gfc_start_block (&ifblock);
5292 : :
5293 : : /* Assign the value to the limit... */
5294 : 9951 : gfc_add_modify (&ifblock, limit, arrayse.expr);
5295 : :
5296 : 9951 : if (nonempty == NULL && HONOR_NANS (DECL_MODE (limit)))
5297 : : {
5298 : 1569 : stmtblock_t ifblock2;
5299 : 1569 : tree ifbody2;
5300 : :
5301 : 1569 : gfc_start_block (&ifblock2);
5302 : 3439 : for (int i = 0; i < ploop->dimen; i++)
5303 : : {
5304 : 1870 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5305 : : ploop->loopvar[i], offset[i]);
5306 : 1870 : gfc_add_modify (&ifblock2, pos[i], tmp);
5307 : : }
5308 : 1569 : ifbody2 = gfc_finish_block (&ifblock2);
5309 : :
5310 : 1569 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5311 : : pos[0], gfc_index_zero_node);
5312 : 1569 : tmp = build3_v (COND_EXPR, cond, ifbody2,
5313 : : build_empty_stmt (input_location));
5314 : 1569 : gfc_add_expr_to_block (&block, tmp);
5315 : : }
5316 : :
5317 : 22177 : for (int i = 0; i < ploop->dimen; i++)
5318 : : {
5319 : 12226 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5320 : : ploop->loopvar[i], offset[i]);
5321 : 12226 : gfc_add_modify (&ifblock, pos[i], tmp);
5322 : 12226 : gfc_add_modify (&ifblock, idx[i], ploop->loopvar[i]);
5323 : : }
5324 : :
5325 : 9951 : gfc_add_modify (&ifblock, second_loop_entry, logical_true_node);
5326 : :
5327 : 9951 : if (lab1)
5328 : 6203 : gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab1));
5329 : :
5330 : 9951 : ifbody = gfc_finish_block (&ifblock);
5331 : :
5332 : 9951 : if (!lab1 || HONOR_NANS (DECL_MODE (limit)))
5333 : : {
5334 : 7646 : if (lab1)
5335 : 5998 : cond = fold_build2_loc (input_location,
5336 : : op == GT_EXPR ? GE_EXPR : LE_EXPR,
5337 : : logical_type_node, arrayse.expr, limit);
5338 : : else
5339 : : {
5340 : 3748 : tree ifbody2, elsebody2;
5341 : :
5342 : : /* We switch to > or >= depending on the value of the BACK argument. */
5343 : 3748 : cond = gfc_create_var (logical_type_node, "cond");
5344 : :
5345 : 3748 : gfc_start_block (&ifblock);
5346 : 5641 : b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5347 : : logical_type_node, arrayse.expr, limit);
5348 : :
5349 : 3748 : gfc_add_modify (&ifblock, cond, b_if);
5350 : 3748 : ifbody2 = gfc_finish_block (&ifblock);
5351 : :
5352 : 3748 : gfc_start_block (&elseblock);
5353 : 3748 : b_else = fold_build2_loc (input_location, op, logical_type_node,
5354 : : arrayse.expr, limit);
5355 : :
5356 : 3748 : gfc_add_modify (&elseblock, cond, b_else);
5357 : 3748 : elsebody2 = gfc_finish_block (&elseblock);
5358 : :
5359 : 3748 : tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5360 : : back, ifbody2, elsebody2);
5361 : :
5362 : 3748 : gfc_add_expr_to_block (&block, tmp);
5363 : : }
5364 : :
5365 : 7646 : cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5366 : 7646 : ifbody = build3_v (COND_EXPR, cond, ifbody,
5367 : : build_empty_stmt (input_location));
5368 : : }
5369 : 9951 : gfc_add_expr_to_block (&block, ifbody);
5370 : :
5371 : 9951 : if (maskexpr && maskexpr->rank > 0)
5372 : : {
5373 : : /* We enclose the above in if (mask) {...}. If the mask is an
5374 : : optional argument, generate IF (.NOT. PRESENT(MASK)
5375 : : .OR. MASK(I)). */
5376 : :
5377 : 3874 : tree ifmask;
5378 : 3874 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5379 : 3874 : tmp = gfc_finish_block (&block);
5380 : 3874 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5381 : : build_empty_stmt (input_location));
5382 : 3874 : }
5383 : : else
5384 : 6077 : tmp = gfc_finish_block (&block);
5385 : 9951 : gfc_add_expr_to_block (&body, tmp);
5386 : :
5387 : 9951 : if (lab1)
5388 : : {
5389 : 13747 : for (int i = 0; i < ploop->dimen; i++)
5390 : 7544 : ploop->from[i] = fold_build3_loc (input_location, COND_EXPR,
5391 : 7544 : TREE_TYPE (ploop->from[i]),
5392 : : second_loop_entry, idx[i],
5393 : : ploop->from[i]);
5394 : :
5395 : 6203 : gfc_trans_scalarized_loop_boundary (ploop, &body);
5396 : :
5397 : 6203 : if (nested_loop)
5398 : : {
5399 : : /* The first loop already advanced the parent se'ss chain, so clear
5400 : : the parent now to avoid doing it a second time, making the chain
5401 : : out of sync. */
5402 : 1858 : nested_se.parent = nullptr;
5403 : 1858 : nested_se.ss = orig_ss;
5404 : : }
5405 : :
5406 : 6203 : stmtblock_t * const outer_block = &ploop->code[ploop->dimen - 1];
5407 : :
5408 : 6203 : if (HONOR_NANS (DECL_MODE (limit)))
5409 : : {
5410 : 3898 : if (nonempty != NULL)
5411 : : {
5412 : 2329 : stmtblock_t init_block;
5413 : 2329 : gfc_init_block (&init_block);
5414 : :
5415 : 5229 : for (int i = 0; i < ploop->dimen; i++)
5416 : 2900 : gfc_add_modify (&init_block, pos[i], gfc_index_one_node);
5417 : :
5418 : 2329 : tree ifbody = gfc_finish_block (&init_block);
5419 : 2329 : tmp = build3_v (COND_EXPR, nonempty, ifbody,
5420 : : build_empty_stmt (input_location));
5421 : 2329 : gfc_add_expr_to_block (outer_block, tmp);
5422 : : }
5423 : : }
5424 : :
5425 : 6203 : gfc_add_expr_to_block (outer_block, build1_v (GOTO_EXPR, lab2));
5426 : 6203 : gfc_add_expr_to_block (outer_block, build1_v (LABEL_EXPR, lab1));
5427 : :
5428 : : /* If we have a mask, only check this element if the mask is set. */
5429 : 6203 : if (maskexpr && maskexpr->rank > 0)
5430 : : {
5431 : 3874 : gfc_init_se (&maskse, base_se);
5432 : 3874 : gfc_copy_loopinfo_to_se (&maskse, ploop);
5433 : 3874 : if (!nested_loop)
5434 : 2700 : maskse.ss = maskss;
5435 : 3874 : gfc_conv_expr_val (&maskse, maskexpr);
5436 : 3874 : gfc_add_block_to_block (&body, &maskse.pre);
5437 : :
5438 : 3874 : gfc_start_block (&block);
5439 : : }
5440 : : else
5441 : 2329 : gfc_init_block (&block);
5442 : :
5443 : : /* Compare with the current limit. */
5444 : 6203 : gfc_init_se (&arrayse, base_se);
5445 : 6203 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
5446 : 6203 : if (!nested_loop)
5447 : 4345 : arrayse.ss = arrayss;
5448 : 6203 : gfc_conv_expr_val (&arrayse, arrayexpr);
5449 : 6203 : gfc_add_block_to_block (&block, &arrayse.pre);
5450 : :
5451 : : /* We do the following if this is a more extreme value. */
5452 : 6203 : gfc_start_block (&ifblock);
5453 : :
5454 : : /* Assign the value to the limit... */
5455 : 6203 : gfc_add_modify (&ifblock, limit, arrayse.expr);
5456 : :
5457 : 13747 : for (int i = 0; i < ploop->dimen; i++)
5458 : : {
5459 : 7544 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5460 : : ploop->loopvar[i], offset[i]);
5461 : 7544 : gfc_add_modify (&ifblock, pos[i], tmp);
5462 : : }
5463 : :
5464 : 6203 : ifbody = gfc_finish_block (&ifblock);
5465 : :
5466 : : /* We switch to > or >= depending on the value of the BACK argument. */
5467 : 6203 : {
5468 : 6203 : tree ifbody2, elsebody2;
5469 : :
5470 : 6203 : cond = gfc_create_var (logical_type_node, "cond");
5471 : :
5472 : 6203 : gfc_start_block (&ifblock);
5473 : 9537 : b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5474 : : logical_type_node, arrayse.expr, limit);
5475 : :
5476 : 6203 : gfc_add_modify (&ifblock, cond, b_if);
5477 : 6203 : ifbody2 = gfc_finish_block (&ifblock);
5478 : :
5479 : 6203 : gfc_start_block (&elseblock);
5480 : 6203 : b_else = fold_build2_loc (input_location, op, logical_type_node,
5481 : : arrayse.expr, limit);
5482 : :
5483 : 6203 : gfc_add_modify (&elseblock, cond, b_else);
5484 : 6203 : elsebody2 = gfc_finish_block (&elseblock);
5485 : :
5486 : 6203 : tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5487 : : back, ifbody2, elsebody2);
5488 : : }
5489 : :
5490 : 6203 : gfc_add_expr_to_block (&block, tmp);
5491 : 6203 : cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5492 : 6203 : tmp = build3_v (COND_EXPR, cond, ifbody,
5493 : : build_empty_stmt (input_location));
5494 : :
5495 : 6203 : gfc_add_expr_to_block (&block, tmp);
5496 : :
5497 : 6203 : if (maskexpr && maskexpr->rank > 0)
5498 : : {
5499 : : /* We enclose the above in if (mask) {...}. If the mask is
5500 : : an optional argument, generate IF (.NOT. PRESENT(MASK)
5501 : : .OR. MASK(I)).*/
5502 : :
5503 : 3874 : tree ifmask;
5504 : 3874 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5505 : 3874 : tmp = gfc_finish_block (&block);
5506 : 3874 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5507 : : build_empty_stmt (input_location));
5508 : 3874 : }
5509 : : else
5510 : 2329 : tmp = gfc_finish_block (&block);
5511 : :
5512 : 6203 : gfc_add_expr_to_block (&body, tmp);
5513 : 6203 : gfc_add_modify (&body, second_loop_entry, logical_false_node);
5514 : : }
5515 : :
5516 : 9951 : gfc_trans_scalarizing_loops (ploop, &body);
5517 : :
5518 : 9951 : if (lab2)
5519 : 6203 : gfc_add_expr_to_block (&ploop->pre, build1_v (LABEL_EXPR, lab2));
5520 : :
5521 : : /* For a scalar mask, enclose the loop in an if statement. */
5522 : 9951 : if (maskexpr && maskexpr->rank == 0)
5523 : : {
5524 : 2644 : tree ifmask;
5525 : :
5526 : 2644 : gfc_init_se (&maskse, nested_loop ? se : nullptr);
5527 : 2644 : gfc_conv_expr_val (&maskse, maskexpr);
5528 : 2644 : gfc_add_block_to_block (&se->pre, &maskse.pre);
5529 : 2644 : gfc_init_block (&block);
5530 : 2644 : gfc_add_block_to_block (&block, &ploop->pre);
5531 : 2644 : gfc_add_block_to_block (&block, &ploop->post);
5532 : 2644 : tmp = gfc_finish_block (&block);
5533 : :
5534 : : /* For the else part of the scalar mask, just initialize
5535 : : the pos variable the same way as above. */
5536 : :
5537 : 2644 : gfc_init_block (&elseblock);
5538 : 5580 : for (int i = 0; i < ploop->dimen; i++)
5539 : 2936 : gfc_add_modify (&elseblock, pos[i], gfc_index_zero_node);
5540 : 2644 : elsetmp = gfc_finish_block (&elseblock);
5541 : 2644 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5542 : 2644 : tmp = build3_v (COND_EXPR, ifmask, tmp, elsetmp);
5543 : 2644 : gfc_add_expr_to_block (&block, tmp);
5544 : 2644 : gfc_add_block_to_block (&se->pre, &block);
5545 : 2644 : }
5546 : : else
5547 : : {
5548 : 7307 : gfc_add_block_to_block (&se->pre, &ploop->pre);
5549 : 7307 : gfc_add_block_to_block (&se->pre, &ploop->post);
5550 : : }
5551 : :
5552 : 9951 : if (!nested_loop)
5553 : 7155 : gfc_cleanup_loop (&loop);
5554 : :
5555 : 9951 : if (!dim_present)
5556 : : {
5557 : 8837 : for (int i = 0; i < arrayexpr->rank; i++)
5558 : : {
5559 : 5556 : tree res_idx = build_int_cst (gfc_array_index_type, i);
5560 : 5556 : tree res_arr_ref = gfc_build_array_ref (result_var, res_idx,
5561 : : NULL_TREE, true);
5562 : :
5563 : 5556 : tree value = convert (type, pos[i]);
5564 : 5556 : gfc_add_modify (&se->pre, res_arr_ref, value);
5565 : : }
5566 : :
5567 : 3281 : se->expr = result_var;
5568 : : }
5569 : : else
5570 : 6670 : se->expr = convert (type, pos[0]);
5571 : : }
5572 : :
5573 : : /* Emit code for findloc. */
5574 : :
5575 : : static void
5576 : 1332 : gfc_conv_intrinsic_findloc (gfc_se *se, gfc_expr *expr)
5577 : : {
5578 : 1332 : gfc_actual_arglist *array_arg, *value_arg, *dim_arg, *mask_arg,
5579 : : *kind_arg, *back_arg;
5580 : 1332 : gfc_expr *value_expr;
5581 : 1332 : int ikind;
5582 : 1332 : tree resvar;
5583 : 1332 : stmtblock_t block;
5584 : 1332 : stmtblock_t body;
5585 : 1332 : stmtblock_t loopblock;
5586 : 1332 : tree type;
5587 : 1332 : tree tmp;
5588 : 1332 : tree found;
5589 : 1332 : tree forward_branch = NULL_TREE;
5590 : 1332 : tree back_branch;
5591 : 1332 : gfc_loopinfo loop;
5592 : 1332 : gfc_ss *arrayss;
5593 : 1332 : gfc_ss *maskss;
5594 : 1332 : gfc_se arrayse;
5595 : 1332 : gfc_se valuese;
5596 : 1332 : gfc_se maskse;
5597 : 1332 : gfc_se backse;
5598 : 1332 : tree exit_label;
5599 : 1332 : gfc_expr *maskexpr;
5600 : 1332 : tree offset;
5601 : 1332 : int i;
5602 : 1332 : bool optional_mask;
5603 : :
5604 : 1332 : array_arg = expr->value.function.actual;
5605 : 1332 : value_arg = array_arg->next;
5606 : 1332 : dim_arg = value_arg->next;
5607 : 1332 : mask_arg = dim_arg->next;
5608 : 1332 : kind_arg = mask_arg->next;
5609 : 1332 : back_arg = kind_arg->next;
5610 : :
5611 : : /* Remove kind and set ikind. */
5612 : 1332 : if (kind_arg->expr)
5613 : : {
5614 : 0 : ikind = mpz_get_si (kind_arg->expr->value.integer);
5615 : 0 : gfc_free_expr (kind_arg->expr);
5616 : 0 : kind_arg->expr = NULL;
5617 : : }
5618 : : else
5619 : 1332 : ikind = gfc_default_integer_kind;
5620 : :
5621 : 1332 : value_expr = value_arg->expr;
5622 : :
5623 : : /* Unless it's a string, pass VALUE by value. */
5624 : 1332 : if (value_expr->ts.type != BT_CHARACTER)
5625 : 732 : value_arg->name = "%VAL";
5626 : :
5627 : : /* Pass BACK argument by value. */
5628 : 1332 : back_arg->name = "%VAL";
5629 : :
5630 : : /* Call the library if we have a character function or if
5631 : : rank > 0. */
5632 : 1332 : if (se->ss || array_arg->expr->ts.type == BT_CHARACTER)
5633 : : {
5634 : 1200 : se->ignore_optional = 1;
5635 : 1200 : if (expr->rank == 0)
5636 : : {
5637 : : /* Remove dim argument. */
5638 : 84 : gfc_free_expr (dim_arg->expr);
5639 : 84 : dim_arg->expr = NULL;
5640 : : }
5641 : 1200 : gfc_conv_intrinsic_funcall (se, expr);
5642 : 1200 : return;
5643 : : }
5644 : :
5645 : 132 : type = gfc_get_int_type (ikind);
5646 : :
5647 : : /* Initialize the result. */
5648 : 132 : resvar = gfc_create_var (gfc_array_index_type, "pos");
5649 : 132 : gfc_add_modify (&se->pre, resvar, build_int_cst (gfc_array_index_type, 0));
5650 : 132 : offset = gfc_create_var (gfc_array_index_type, "offset");
5651 : :
5652 : 132 : maskexpr = mask_arg->expr;
5653 : 72 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5654 : 60 : && maskexpr->symtree->n.sym->attr.dummy
5655 : 144 : && maskexpr->symtree->n.sym->attr.optional;
5656 : :
5657 : : /* Generate two loops, one for BACK=.true. and one for BACK=.false. */
5658 : :
5659 : 396 : for (i = 0 ; i < 2; i++)
5660 : : {
5661 : : /* Walk the arguments. */
5662 : 264 : arrayss = gfc_walk_expr (array_arg->expr);
5663 : 264 : gcc_assert (arrayss != gfc_ss_terminator);
5664 : :
5665 : 264 : if (maskexpr && maskexpr->rank != 0)
5666 : : {
5667 : 84 : maskss = gfc_walk_expr (maskexpr);
5668 : 84 : gcc_assert (maskss != gfc_ss_terminator);
5669 : : }
5670 : : else
5671 : : maskss = NULL;
5672 : :
5673 : : /* Initialize the scalarizer. */
5674 : 264 : gfc_init_loopinfo (&loop);
5675 : 264 : exit_label = gfc_build_label_decl (NULL_TREE);
5676 : 264 : TREE_USED (exit_label) = 1;
5677 : :
5678 : : /* We add the mask first because the number of iterations is
5679 : : taken from the last ss, and this breaks if an absent
5680 : : optional argument is used for mask. */
5681 : :
5682 : 264 : if (maskss)
5683 : 84 : gfc_add_ss_to_loop (&loop, maskss);
5684 : 264 : gfc_add_ss_to_loop (&loop, arrayss);
5685 : :
5686 : : /* Initialize the loop. */
5687 : 264 : gfc_conv_ss_startstride (&loop);
5688 : 264 : gfc_conv_loop_setup (&loop, &expr->where);
5689 : :
5690 : : /* Calculate the offset. */
5691 : 264 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5692 : : gfc_index_one_node, loop.from[0]);
5693 : 264 : gfc_add_modify (&loop.pre, offset, tmp);
5694 : :
5695 : 264 : gfc_mark_ss_chain_used (arrayss, 1);
5696 : 264 : if (maskss)
5697 : 84 : gfc_mark_ss_chain_used (maskss, 1);
5698 : :
5699 : : /* The first loop is for BACK=.true. */
5700 : 264 : if (i == 0)
5701 : 132 : loop.reverse[0] = GFC_REVERSE_SET;
5702 : :
5703 : : /* Generate the loop body. */
5704 : 264 : gfc_start_scalarized_body (&loop, &body);
5705 : :
5706 : : /* If we have an array mask, only add the element if it is
5707 : : set. */
5708 : 264 : if (maskss)
5709 : : {
5710 : 84 : gfc_init_se (&maskse, NULL);
5711 : 84 : gfc_copy_loopinfo_to_se (&maskse, &loop);
5712 : 84 : maskse.ss = maskss;
5713 : 84 : gfc_conv_expr_val (&maskse, maskexpr);
5714 : 84 : gfc_add_block_to_block (&body, &maskse.pre);
5715 : : }
5716 : :
5717 : : /* If the condition matches then set the return value. */
5718 : 264 : gfc_start_block (&block);
5719 : :
5720 : : /* Add the offset. */
5721 : 264 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5722 : 264 : TREE_TYPE (resvar),
5723 : : loop.loopvar[0], offset);
5724 : 264 : gfc_add_modify (&block, resvar, tmp);
5725 : : /* And break out of the loop. */
5726 : 264 : tmp = build1_v (GOTO_EXPR, exit_label);
5727 : 264 : gfc_add_expr_to_block (&block, tmp);
5728 : :
5729 : 264 : found = gfc_finish_block (&block);
5730 : :
5731 : : /* Check this element. */
5732 : 264 : gfc_init_se (&arrayse, NULL);
5733 : 264 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
5734 : 264 : arrayse.ss = arrayss;
5735 : 264 : gfc_conv_expr_val (&arrayse, array_arg->expr);
5736 : 264 : gfc_add_block_to_block (&body, &arrayse.pre);
5737 : :
5738 : 264 : gfc_init_se (&valuese, NULL);
5739 : 264 : gfc_conv_expr_val (&valuese, value_arg->expr);
5740 : 264 : gfc_add_block_to_block (&body, &valuese.pre);
5741 : :
5742 : 264 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5743 : : arrayse.expr, valuese.expr);
5744 : :
5745 : 264 : tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
5746 : 264 : if (maskss)
5747 : : {
5748 : : /* We enclose the above in if (mask) {...}. If the mask is
5749 : : an optional argument, generate IF (.NOT. PRESENT(MASK)
5750 : : .OR. MASK(I)). */
5751 : :
5752 : 84 : tree ifmask;
5753 : 84 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5754 : 84 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5755 : : build_empty_stmt (input_location));
5756 : : }
5757 : :
5758 : 264 : gfc_add_expr_to_block (&body, tmp);
5759 : 264 : gfc_add_block_to_block (&body, &arrayse.post);
5760 : :
5761 : 264 : gfc_trans_scalarizing_loops (&loop, &body);
5762 : :
5763 : : /* Add the exit label. */
5764 : 264 : tmp = build1_v (LABEL_EXPR, exit_label);
5765 : 264 : gfc_add_expr_to_block (&loop.pre, tmp);
5766 : 264 : gfc_start_block (&loopblock);
5767 : 264 : gfc_add_block_to_block (&loopblock, &loop.pre);
5768 : 264 : gfc_add_block_to_block (&loopblock, &loop.post);
5769 : 264 : if (i == 0)
5770 : 132 : forward_branch = gfc_finish_block (&loopblock);
5771 : : else
5772 : 132 : back_branch = gfc_finish_block (&loopblock);
5773 : :
5774 : 264 : gfc_cleanup_loop (&loop);
5775 : : }
5776 : :
5777 : : /* Enclose the two loops in an IF statement. */
5778 : :
5779 : 132 : gfc_init_se (&backse, NULL);
5780 : 132 : gfc_conv_expr_val (&backse, back_arg->expr);
5781 : 132 : gfc_add_block_to_block (&se->pre, &backse.pre);
5782 : 132 : tmp = build3_v (COND_EXPR, backse.expr, forward_branch, back_branch);
5783 : :
5784 : : /* For a scalar mask, enclose the loop in an if statement. */
5785 : 132 : if (maskexpr && maskss == NULL)
5786 : : {
5787 : 30 : tree ifmask;
5788 : 30 : tree if_stmt;
5789 : :
5790 : 30 : gfc_init_se (&maskse, NULL);
5791 : 30 : gfc_conv_expr_val (&maskse, maskexpr);
5792 : 30 : gfc_init_block (&block);
5793 : 30 : gfc_add_expr_to_block (&block, maskse.expr);
5794 : 30 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5795 : 30 : if_stmt = build3_v (COND_EXPR, ifmask, tmp,
5796 : : build_empty_stmt (input_location));
5797 : 30 : gfc_add_expr_to_block (&block, if_stmt);
5798 : 30 : tmp = gfc_finish_block (&block);
5799 : : }
5800 : :
5801 : 132 : gfc_add_expr_to_block (&se->pre, tmp);
5802 : 132 : se->expr = convert (type, resvar);
5803 : :
5804 : : }
5805 : :
5806 : : /* Emit code for minval or maxval intrinsic. There are many different cases
5807 : : we need to handle. For performance reasons we sometimes create two
5808 : : loops instead of one, where the second one is much simpler.
5809 : : Examples for minval intrinsic:
5810 : : 1) Result is an array, a call is generated
5811 : : 2) Array mask is used and NaNs need to be supported, rank 1:
5812 : : limit = Infinity;
5813 : : nonempty = false;
5814 : : S = from;
5815 : : while (S <= to) {
5816 : : if (mask[S]) {
5817 : : nonempty = true;
5818 : : if (a[S] <= limit) {
5819 : : limit = a[S];
5820 : : S++;
5821 : : goto lab;
5822 : : }
5823 : : else
5824 : : S++;
5825 : : }
5826 : : }
5827 : : limit = nonempty ? NaN : huge (limit);
5828 : : lab:
5829 : : while (S <= to) { if(mask[S]) limit = min (a[S], limit); S++; }
5830 : : 3) NaNs need to be supported, but it is known at compile time or cheaply
5831 : : at runtime whether array is nonempty or not, rank 1:
5832 : : limit = Infinity;
5833 : : S = from;
5834 : : while (S <= to) {
5835 : : if (a[S] <= limit) {
5836 : : limit = a[S];
5837 : : S++;
5838 : : goto lab;
5839 : : }
5840 : : else
5841 : : S++;
5842 : : }
5843 : : limit = (from <= to) ? NaN : huge (limit);
5844 : : lab:
5845 : : while (S <= to) { limit = min (a[S], limit); S++; }
5846 : : 4) Array mask is used and NaNs need to be supported, rank > 1:
5847 : : limit = Infinity;
5848 : : nonempty = false;
5849 : : fast = false;
5850 : : S1 = from1;
5851 : : while (S1 <= to1) {
5852 : : S2 = from2;
5853 : : while (S2 <= to2) {
5854 : : if (mask[S1][S2]) {
5855 : : if (fast) limit = min (a[S1][S2], limit);
5856 : : else {
5857 : : nonempty = true;
5858 : : if (a[S1][S2] <= limit) {
5859 : : limit = a[S1][S2];
5860 : : fast = true;
5861 : : }
5862 : : }
5863 : : }
5864 : : S2++;
5865 : : }
5866 : : S1++;
5867 : : }
5868 : : if (!fast)
5869 : : limit = nonempty ? NaN : huge (limit);
5870 : : 5) NaNs need to be supported, but it is known at compile time or cheaply
5871 : : at runtime whether array is nonempty or not, rank > 1:
5872 : : limit = Infinity;
5873 : : fast = false;
5874 : : S1 = from1;
5875 : : while (S1 <= to1) {
5876 : : S2 = from2;
5877 : : while (S2 <= to2) {
5878 : : if (fast) limit = min (a[S1][S2], limit);
5879 : : else {
5880 : : if (a[S1][S2] <= limit) {
5881 : : limit = a[S1][S2];
5882 : : fast = true;
5883 : : }
5884 : : }
5885 : : S2++;
5886 : : }
5887 : : S1++;
5888 : : }
5889 : : if (!fast)
5890 : : limit = (nonempty_array) ? NaN : huge (limit);
5891 : : 6) NaNs aren't supported, but infinities are. Array mask is used:
5892 : : limit = Infinity;
5893 : : nonempty = false;
5894 : : S = from;
5895 : : while (S <= to) {
5896 : : if (mask[S]) { nonempty = true; limit = min (a[S], limit); }
5897 : : S++;
5898 : : }
5899 : : limit = nonempty ? limit : huge (limit);
5900 : : 7) Same without array mask:
5901 : : limit = Infinity;
5902 : : S = from;
5903 : : while (S <= to) { limit = min (a[S], limit); S++; }
5904 : : limit = (from <= to) ? limit : huge (limit);
5905 : : 8) Neither NaNs nor infinities are supported (-ffast-math or BT_INTEGER):
5906 : : limit = huge (limit);
5907 : : S = from;
5908 : : while (S <= to) { limit = min (a[S], limit); S++); }
5909 : : (or
5910 : : while (S <= to) { if (mask[S]) limit = min (a[S], limit); S++; }
5911 : : with array mask instead).
5912 : : For 3), 5), 7) and 8), if mask is scalar, this all goes into a conditional,
5913 : : setting limit = huge (limit); in the else branch. */
5914 : :
5915 : : static void
5916 : 2415 : gfc_conv_intrinsic_minmaxval (gfc_se * se, gfc_expr * expr, enum tree_code op)
5917 : : {
5918 : 2415 : tree limit;
5919 : 2415 : tree type;
5920 : 2415 : tree tmp;
5921 : 2415 : tree ifbody;
5922 : 2415 : tree nonempty;
5923 : 2415 : tree nonempty_var;
5924 : 2415 : tree lab;
5925 : 2415 : tree fast;
5926 : 2415 : tree huge_cst = NULL, nan_cst = NULL;
5927 : 2415 : stmtblock_t body;
5928 : 2415 : stmtblock_t block, block2;
5929 : 2415 : gfc_loopinfo loop;
5930 : 2415 : gfc_actual_arglist *actual;
5931 : 2415 : gfc_ss *arrayss;
5932 : 2415 : gfc_ss *maskss;
5933 : 2415 : gfc_se arrayse;
5934 : 2415 : gfc_se maskse;
5935 : 2415 : gfc_expr *arrayexpr;
5936 : 2415 : gfc_expr *maskexpr;
5937 : 2415 : int n;
5938 : 2415 : bool optional_mask;
5939 : :
5940 : 2415 : if (se->ss)
5941 : : {
5942 : 0 : gfc_conv_intrinsic_funcall (se, expr);
5943 : 186 : return;
5944 : : }
5945 : :
5946 : 2415 : actual = expr->value.function.actual;
5947 : 2415 : arrayexpr = actual->expr;
5948 : :
5949 : 2415 : if (arrayexpr->ts.type == BT_CHARACTER)
5950 : : {
5951 : 186 : gfc_actual_arglist *dim = actual->next;
5952 : 186 : if (expr->rank == 0 && dim->expr != 0)
5953 : : {
5954 : 6 : gfc_free_expr (dim->expr);
5955 : 6 : dim->expr = NULL;
5956 : : }
5957 : 186 : gfc_conv_intrinsic_funcall (se, expr);
5958 : 186 : return;
5959 : : }
5960 : :
5961 : 2229 : type = gfc_typenode_for_spec (&expr->ts);
5962 : : /* Initialize the result. */
5963 : 2229 : limit = gfc_create_var (type, "limit");
5964 : 2229 : n = gfc_validate_kind (expr->ts.type, expr->ts.kind, false);
5965 : 2229 : switch (expr->ts.type)
5966 : : {
5967 : 1244 : case BT_REAL:
5968 : 1244 : huge_cst = gfc_conv_mpfr_to_tree (gfc_real_kinds[n].huge,
5969 : : expr->ts.kind, 0);
5970 : 1244 : if (HONOR_INFINITIES (DECL_MODE (limit)))
5971 : : {
5972 : 1240 : REAL_VALUE_TYPE real;
5973 : 1240 : real_inf (&real);
5974 : 1240 : tmp = build_real (type, real);
5975 : : }
5976 : : else
5977 : : tmp = huge_cst;
5978 : 1244 : if (HONOR_NANS (DECL_MODE (limit)))
5979 : 1240 : nan_cst = gfc_build_nan (type, "");
5980 : : break;
5981 : :
5982 : 955 : case BT_INTEGER:
5983 : 955 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge, expr->ts.kind);
5984 : 955 : break;
5985 : :
5986 : 30 : case BT_UNSIGNED:
5987 : : /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
5988 : 30 : if (op == GT_EXPR)
5989 : 18 : tmp = build_int_cst (type, 0);
5990 : : else
5991 : 12 : tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
5992 : : expr->ts.kind);
5993 : : break;
5994 : :
5995 : 0 : default:
5996 : 0 : gcc_unreachable ();
5997 : : }
5998 : :
5999 : : /* We start with the most negative possible value for MAXVAL, and the most
6000 : : positive possible value for MINVAL. The most negative possible value is
6001 : : -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
6002 : : possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
6003 : : with above. */
6004 : 2229 : if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
6005 : : {
6006 : 985 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
6007 : 985 : if (huge_cst)
6008 : 559 : huge_cst = fold_build1_loc (input_location, NEGATE_EXPR,
6009 : 559 : TREE_TYPE (huge_cst), huge_cst);
6010 : : }
6011 : :
6012 : 1003 : if (op == GT_EXPR && expr->ts.type == BT_INTEGER)
6013 : 426 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
6014 : : tmp, build_int_cst (type, 1));
6015 : :
6016 : 2229 : gfc_add_modify (&se->pre, limit, tmp);
6017 : :
6018 : : /* Walk the arguments. */
6019 : 2229 : arrayss = gfc_walk_expr (arrayexpr);
6020 : 2229 : gcc_assert (arrayss != gfc_ss_terminator);
6021 : :
6022 : 2229 : actual = actual->next->next;
6023 : 2229 : gcc_assert (actual);
6024 : 2229 : maskexpr = actual->expr;
6025 : 1572 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
6026 : 1560 : && maskexpr->symtree->n.sym->attr.dummy
6027 : 2241 : && maskexpr->symtree->n.sym->attr.optional;
6028 : 1560 : nonempty = NULL;
6029 : 1572 : if (maskexpr && maskexpr->rank != 0)
6030 : : {
6031 : 1026 : maskss = gfc_walk_expr (maskexpr);
6032 : 1026 : gcc_assert (maskss != gfc_ss_terminator);
6033 : : }
6034 : : else
6035 : : {
6036 : 1203 : mpz_t asize;
6037 : 1203 : if (gfc_array_size (arrayexpr, &asize))
6038 : : {
6039 : 678 : nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
6040 : 678 : mpz_clear (asize);
6041 : 678 : nonempty = fold_build2_loc (input_location, GT_EXPR,
6042 : : logical_type_node, nonempty,
6043 : : gfc_index_zero_node);
6044 : : }
6045 : 1203 : maskss = NULL;
6046 : : }
6047 : :
6048 : : /* Initialize the scalarizer. */
6049 : 2229 : gfc_init_loopinfo (&loop);
6050 : :
6051 : : /* We add the mask first because the number of iterations is taken
6052 : : from the last ss, and this breaks if an absent optional argument
6053 : : is used for mask. */
6054 : :
6055 : 2229 : if (maskss)
6056 : 1026 : gfc_add_ss_to_loop (&loop, maskss);
6057 : 2229 : gfc_add_ss_to_loop (&loop, arrayss);
6058 : :
6059 : : /* Initialize the loop. */
6060 : 2229 : gfc_conv_ss_startstride (&loop);
6061 : :
6062 : : /* The code generated can have more than one loop in sequence (see the
6063 : : comment at the function header). This doesn't work well with the
6064 : : scalarizer, which changes arrays' offset when the scalarization loops
6065 : : are generated (see gfc_trans_preloop_setup). Fortunately, {min,max}val
6066 : : are currently inlined in the scalar case only. As there is no dependency
6067 : : to care about in that case, there is no temporary, so that we can use the
6068 : : scalarizer temporary code to handle multiple loops. Thus, we set temp_dim
6069 : : here, we call gfc_mark_ss_chain_used with flag=3 later, and we use
6070 : : gfc_trans_scalarized_loop_boundary even later to restore offset.
6071 : : TODO: this prevents inlining of rank > 0 minmaxval calls, so this
6072 : : should eventually go away. We could either create two loops properly,
6073 : : or find another way to save/restore the array offsets between the two
6074 : : loops (without conflicting with temporary management), or use a single
6075 : : loop minmaxval implementation. See PR 31067. */
6076 : 2229 : loop.temp_dim = loop.dimen;
6077 : 2229 : gfc_conv_loop_setup (&loop, &expr->where);
6078 : :
6079 : 2229 : if (nonempty == NULL && maskss == NULL
6080 : 525 : && loop.dimen == 1 && loop.from[0] && loop.to[0])
6081 : 489 : nonempty = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
6082 : : loop.from[0], loop.to[0]);
6083 : 2229 : nonempty_var = NULL;
6084 : 2229 : if (nonempty == NULL
6085 : 2229 : && (HONOR_INFINITIES (DECL_MODE (limit))
6086 : 480 : || HONOR_NANS (DECL_MODE (limit))))
6087 : : {
6088 : 582 : nonempty_var = gfc_create_var (logical_type_node, "nonempty");
6089 : 582 : gfc_add_modify (&se->pre, nonempty_var, logical_false_node);
6090 : 582 : nonempty = nonempty_var;
6091 : : }
6092 : 2229 : lab = NULL;
6093 : 2229 : fast = NULL;
6094 : 2229 : if (HONOR_NANS (DECL_MODE (limit)))
6095 : : {
6096 : 1240 : if (loop.dimen == 1)
6097 : : {
6098 : 820 : lab = gfc_build_label_decl (NULL_TREE);
6099 : 820 : TREE_USED (lab) = 1;
6100 : : }
6101 : : else
6102 : : {
6103 : 420 : fast = gfc_create_var (logical_type_node, "fast");
6104 : 420 : gfc_add_modify (&se->pre, fast, logical_false_node);
6105 : : }
6106 : : }
6107 : :
6108 : 2229 : gfc_mark_ss_chain_used (arrayss, lab ? 3 : 1);
6109 : 2229 : if (maskss)
6110 : 1704 : gfc_mark_ss_chain_used (maskss, lab ? 3 : 1);
6111 : : /* Generate the loop body. */
6112 : 2229 : gfc_start_scalarized_body (&loop, &body);
6113 : :
6114 : : /* If we have a mask, only add this element if the mask is set. */
6115 : 2229 : if (maskss)
6116 : : {
6117 : 1026 : gfc_init_se (&maskse, NULL);
6118 : 1026 : gfc_copy_loopinfo_to_se (&maskse, &loop);
6119 : 1026 : maskse.ss = maskss;
6120 : 1026 : gfc_conv_expr_val (&maskse, maskexpr);
6121 : 1026 : gfc_add_block_to_block (&body, &maskse.pre);
6122 : :
6123 : 1026 : gfc_start_block (&block);
6124 : : }
6125 : : else
6126 : 1203 : gfc_init_block (&block);
6127 : :
6128 : : /* Compare with the current limit. */
6129 : 2229 : gfc_init_se (&arrayse, NULL);
6130 : 2229 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
6131 : 2229 : arrayse.ss = arrayss;
6132 : 2229 : gfc_conv_expr_val (&arrayse, arrayexpr);
6133 : 2229 : arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6134 : 2229 : gfc_add_block_to_block (&block, &arrayse.pre);
6135 : :
6136 : 2229 : gfc_init_block (&block2);
6137 : :
6138 : 2229 : if (nonempty_var)
6139 : 582 : gfc_add_modify (&block2, nonempty_var, logical_true_node);
6140 : :
6141 : 2229 : if (HONOR_NANS (DECL_MODE (limit)))
6142 : : {
6143 : 1921 : tmp = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
6144 : : logical_type_node, arrayse.expr, limit);
6145 : 1240 : if (lab)
6146 : : {
6147 : 820 : stmtblock_t ifblock;
6148 : 820 : tree inc_loop;
6149 : 820 : inc_loop = fold_build2_loc (input_location, PLUS_EXPR,
6150 : 820 : TREE_TYPE (loop.loopvar[0]),
6151 : : loop.loopvar[0], gfc_index_one_node);
6152 : 820 : gfc_init_block (&ifblock);
6153 : 820 : gfc_add_modify (&ifblock, limit, arrayse.expr);
6154 : 820 : gfc_add_modify (&ifblock, loop.loopvar[0], inc_loop);
6155 : 820 : gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab));
6156 : 820 : ifbody = gfc_finish_block (&ifblock);
6157 : : }
6158 : : else
6159 : : {
6160 : 420 : stmtblock_t ifblock;
6161 : :
6162 : 420 : gfc_init_block (&ifblock);
6163 : 420 : gfc_add_modify (&ifblock, limit, arrayse.expr);
6164 : 420 : gfc_add_modify (&ifblock, fast, logical_true_node);
6165 : 420 : ifbody = gfc_finish_block (&ifblock);
6166 : : }
6167 : 1240 : tmp = build3_v (COND_EXPR, tmp, ifbody,
6168 : : build_empty_stmt (input_location));
6169 : 1240 : gfc_add_expr_to_block (&block2, tmp);
6170 : : }
6171 : : else
6172 : : {
6173 : : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6174 : : signed zeros. */
6175 : 1534 : tmp = fold_build2_loc (input_location,
6176 : : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6177 : : type, arrayse.expr, limit);
6178 : 989 : gfc_add_modify (&block2, limit, tmp);
6179 : : }
6180 : :
6181 : 2229 : if (fast)
6182 : : {
6183 : 420 : tree elsebody = gfc_finish_block (&block2);
6184 : :
6185 : : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6186 : : signed zeros. */
6187 : 420 : if (HONOR_NANS (DECL_MODE (limit)))
6188 : : {
6189 : 420 : tmp = fold_build2_loc (input_location, op, logical_type_node,
6190 : : arrayse.expr, limit);
6191 : 420 : ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6192 : 420 : ifbody = build3_v (COND_EXPR, tmp, ifbody,
6193 : : build_empty_stmt (input_location));
6194 : : }
6195 : : else
6196 : : {
6197 : 0 : tmp = fold_build2_loc (input_location,
6198 : : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6199 : : type, arrayse.expr, limit);
6200 : 0 : ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6201 : : }
6202 : 420 : tmp = build3_v (COND_EXPR, fast, ifbody, elsebody);
6203 : 420 : gfc_add_expr_to_block (&block, tmp);
6204 : : }
6205 : : else
6206 : 1809 : gfc_add_block_to_block (&block, &block2);
6207 : :
6208 : 2229 : gfc_add_block_to_block (&block, &arrayse.post);
6209 : :
6210 : 2229 : tmp = gfc_finish_block (&block);
6211 : 2229 : if (maskss)
6212 : : {
6213 : : /* We enclose the above in if (mask) {...}. If the mask is an
6214 : : optional argument, generate IF (.NOT. PRESENT(MASK)
6215 : : .OR. MASK(I)). */
6216 : 1026 : tree ifmask;
6217 : 1026 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6218 : 1026 : tmp = build3_v (COND_EXPR, ifmask, tmp,
6219 : : build_empty_stmt (input_location));
6220 : : }
6221 : 2229 : gfc_add_expr_to_block (&body, tmp);
6222 : :
6223 : 2229 : if (lab)
6224 : : {
6225 : 820 : gfc_trans_scalarized_loop_boundary (&loop, &body);
6226 : :
6227 : 820 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6228 : : nan_cst, huge_cst);
6229 : 820 : gfc_add_modify (&loop.code[0], limit, tmp);
6230 : 820 : gfc_add_expr_to_block (&loop.code[0], build1_v (LABEL_EXPR, lab));
6231 : :
6232 : : /* If we have a mask, only add this element if the mask is set. */
6233 : 820 : if (maskss)
6234 : : {
6235 : 348 : gfc_init_se (&maskse, NULL);
6236 : 348 : gfc_copy_loopinfo_to_se (&maskse, &loop);
6237 : 348 : maskse.ss = maskss;
6238 : 348 : gfc_conv_expr_val (&maskse, maskexpr);
6239 : 348 : gfc_add_block_to_block (&body, &maskse.pre);
6240 : :
6241 : 348 : gfc_start_block (&block);
6242 : : }
6243 : : else
6244 : 472 : gfc_init_block (&block);
6245 : :
6246 : : /* Compare with the current limit. */
6247 : 820 : gfc_init_se (&arrayse, NULL);
6248 : 820 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
6249 : 820 : arrayse.ss = arrayss;
6250 : 820 : gfc_conv_expr_val (&arrayse, arrayexpr);
6251 : 820 : arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6252 : 820 : gfc_add_block_to_block (&block, &arrayse.pre);
6253 : :
6254 : : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6255 : : signed zeros. */
6256 : 820 : if (HONOR_NANS (DECL_MODE (limit)))
6257 : : {
6258 : 820 : tmp = fold_build2_loc (input_location, op, logical_type_node,
6259 : : arrayse.expr, limit);
6260 : 820 : ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6261 : 820 : tmp = build3_v (COND_EXPR, tmp, ifbody,
6262 : : build_empty_stmt (input_location));
6263 : 820 : gfc_add_expr_to_block (&block, tmp);
6264 : : }
6265 : : else
6266 : : {
6267 : 0 : tmp = fold_build2_loc (input_location,
6268 : : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6269 : : type, arrayse.expr, limit);
6270 : 0 : gfc_add_modify (&block, limit, tmp);
6271 : : }
6272 : :
6273 : 820 : gfc_add_block_to_block (&block, &arrayse.post);
6274 : :
6275 : 820 : tmp = gfc_finish_block (&block);
6276 : 820 : if (maskss)
6277 : : /* We enclose the above in if (mask) {...}. */
6278 : : {
6279 : 348 : tree ifmask;
6280 : 348 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6281 : 348 : tmp = build3_v (COND_EXPR, ifmask, tmp,
6282 : : build_empty_stmt (input_location));
6283 : : }
6284 : :
6285 : 820 : gfc_add_expr_to_block (&body, tmp);
6286 : : /* Avoid initializing loopvar[0] again, it should be left where
6287 : : it finished by the first loop. */
6288 : 820 : loop.from[0] = loop.loopvar[0];
6289 : : }
6290 : 2229 : gfc_trans_scalarizing_loops (&loop, &body);
6291 : :
6292 : 2229 : if (fast)
6293 : : {
6294 : 420 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6295 : : nan_cst, huge_cst);
6296 : 420 : ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6297 : 420 : tmp = build3_v (COND_EXPR, fast, build_empty_stmt (input_location),
6298 : : ifbody);
6299 : 420 : gfc_add_expr_to_block (&loop.pre, tmp);
6300 : : }
6301 : 1809 : else if (HONOR_INFINITIES (DECL_MODE (limit)) && !lab)
6302 : : {
6303 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty, limit,
6304 : : huge_cst);
6305 : 0 : gfc_add_modify (&loop.pre, limit, tmp);
6306 : : }
6307 : :
6308 : : /* For a scalar mask, enclose the loop in an if statement. */
6309 : 2229 : if (maskexpr && maskss == NULL)
6310 : : {
6311 : 546 : tree else_stmt;
6312 : 546 : tree ifmask;
6313 : :
6314 : 546 : gfc_init_se (&maskse, NULL);
6315 : 546 : gfc_conv_expr_val (&maskse, maskexpr);
6316 : 546 : gfc_init_block (&block);
6317 : 546 : gfc_add_block_to_block (&block, &loop.pre);
6318 : 546 : gfc_add_block_to_block (&block, &loop.post);
6319 : 546 : tmp = gfc_finish_block (&block);
6320 : :
6321 : 546 : if (HONOR_INFINITIES (DECL_MODE (limit)))
6322 : 354 : else_stmt = build2_v (MODIFY_EXPR, limit, huge_cst);
6323 : : else
6324 : 192 : else_stmt = build_empty_stmt (input_location);
6325 : :
6326 : 546 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6327 : 546 : tmp = build3_v (COND_EXPR, ifmask, tmp, else_stmt);
6328 : 546 : gfc_add_expr_to_block (&block, tmp);
6329 : 546 : gfc_add_block_to_block (&se->pre, &block);
6330 : : }
6331 : : else
6332 : : {
6333 : 1683 : gfc_add_block_to_block (&se->pre, &loop.pre);
6334 : 1683 : gfc_add_block_to_block (&se->pre, &loop.post);
6335 : : }
6336 : :
6337 : 2229 : gfc_cleanup_loop (&loop);
6338 : :
6339 : 2229 : se->expr = limit;
6340 : : }
6341 : :
6342 : : /* BTEST (i, pos) = (i & (1 << pos)) != 0. */
6343 : : static void
6344 : 145 : gfc_conv_intrinsic_btest (gfc_se * se, gfc_expr * expr)
6345 : : {
6346 : 145 : tree args[2];
6347 : 145 : tree type;
6348 : 145 : tree tmp;
6349 : :
6350 : 145 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6351 : 145 : type = TREE_TYPE (args[0]);
6352 : :
6353 : : /* Optionally generate code for runtime argument check. */
6354 : 145 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6355 : : {
6356 : 6 : tree below = fold_build2_loc (input_location, LT_EXPR,
6357 : : logical_type_node, args[1],
6358 : 6 : build_int_cst (TREE_TYPE (args[1]), 0));
6359 : 6 : tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6360 : 6 : tree above = fold_build2_loc (input_location, GE_EXPR,
6361 : : logical_type_node, args[1], nbits);
6362 : 6 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6363 : : logical_type_node, below, above);
6364 : 6 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6365 : : "POS argument (%ld) out of range 0:%ld "
6366 : : "in intrinsic BTEST",
6367 : : fold_convert (long_integer_type_node, args[1]),
6368 : : fold_convert (long_integer_type_node, nbits));
6369 : : }
6370 : :
6371 : 145 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6372 : : build_int_cst (type, 1), args[1]);
6373 : 145 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], tmp);
6374 : 145 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
6375 : : build_int_cst (type, 0));
6376 : 145 : type = gfc_typenode_for_spec (&expr->ts);
6377 : 145 : se->expr = convert (type, tmp);
6378 : 145 : }
6379 : :
6380 : :
6381 : : /* Generate code for BGE, BGT, BLE and BLT intrinsics. */
6382 : : static void
6383 : 216 : gfc_conv_intrinsic_bitcomp (gfc_se * se, gfc_expr * expr, enum tree_code op)
6384 : : {
6385 : 216 : tree args[2];
6386 : :
6387 : 216 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6388 : :
6389 : : /* Convert both arguments to the unsigned type of the same size. */
6390 : 216 : args[0] = fold_convert (unsigned_type_for (TREE_TYPE (args[0])), args[0]);
6391 : 216 : args[1] = fold_convert (unsigned_type_for (TREE_TYPE (args[1])), args[1]);
6392 : :
6393 : : /* If they have unequal type size, convert to the larger one. */
6394 : 216 : if (TYPE_PRECISION (TREE_TYPE (args[0]))
6395 : 216 : > TYPE_PRECISION (TREE_TYPE (args[1])))
6396 : 0 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
6397 : 216 : else if (TYPE_PRECISION (TREE_TYPE (args[1]))
6398 : 216 : > TYPE_PRECISION (TREE_TYPE (args[0])))
6399 : 0 : args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
6400 : :
6401 : : /* Now, we compare them. */
6402 : 216 : se->expr = fold_build2_loc (input_location, op, logical_type_node,
6403 : : args[0], args[1]);
6404 : 216 : }
6405 : :
6406 : :
6407 : : /* Generate code to perform the specified operation. */
6408 : : static void
6409 : 1891 : gfc_conv_intrinsic_bitop (gfc_se * se, gfc_expr * expr, enum tree_code op)
6410 : : {
6411 : 1891 : tree args[2];
6412 : :
6413 : 1891 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6414 : 1891 : se->expr = fold_build2_loc (input_location, op, TREE_TYPE (args[0]),
6415 : : args[0], args[1]);
6416 : 1891 : }
6417 : :
6418 : : /* Bitwise not. */
6419 : : static void
6420 : 228 : gfc_conv_intrinsic_not (gfc_se * se, gfc_expr * expr)
6421 : : {
6422 : 228 : tree arg;
6423 : :
6424 : 228 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
6425 : 228 : se->expr = fold_build1_loc (input_location, BIT_NOT_EXPR,
6426 : 228 : TREE_TYPE (arg), arg);
6427 : 228 : }
6428 : :
6429 : :
6430 : : /* Generate code for OUT_OF_RANGE. */
6431 : : static void
6432 : 468 : gfc_conv_intrinsic_out_of_range (gfc_se * se, gfc_expr * expr)
6433 : : {
6434 : 468 : tree *args;
6435 : 468 : tree type;
6436 : 468 : tree tmp = NULL_TREE, tmp1, tmp2;
6437 : 468 : unsigned int num_args;
6438 : 468 : int k;
6439 : 468 : gfc_se rnd_se;
6440 : 468 : gfc_actual_arglist *arg = expr->value.function.actual;
6441 : 468 : gfc_expr *x = arg->expr;
6442 : 468 : gfc_expr *mold = arg->next->expr;
6443 : :
6444 : 468 : num_args = gfc_intrinsic_argument_list_length (expr);
6445 : 468 : args = XALLOCAVEC (tree, num_args);
6446 : :
6447 : 468 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
6448 : :
6449 : 468 : gfc_init_se (&rnd_se, NULL);
6450 : :
6451 : 468 : if (num_args == 3)
6452 : : {
6453 : : /* The ROUND argument is optional and shall appear only if X is
6454 : : of type real and MOLD is of type integer (see edit F23/004). */
6455 : 270 : gfc_expr *round = arg->next->next->expr;
6456 : 270 : gfc_conv_expr (&rnd_se, round);
6457 : :
6458 : 270 : if (round->expr_type == EXPR_VARIABLE
6459 : 198 : && round->symtree->n.sym->attr.dummy
6460 : 30 : && round->symtree->n.sym->attr.optional)
6461 : : {
6462 : 30 : tree present = gfc_conv_expr_present (round->symtree->n.sym);
6463 : 30 : rnd_se.expr = build3_loc (input_location, COND_EXPR,
6464 : : logical_type_node, present,
6465 : : rnd_se.expr, logical_false_node);
6466 : 30 : gfc_add_block_to_block (&se->pre, &rnd_se.pre);
6467 : : }
6468 : : }
6469 : : else
6470 : : {
6471 : : /* If ROUND is absent, it is equivalent to having the value false. */
6472 : 198 : rnd_se.expr = logical_false_node;
6473 : : }
6474 : :
6475 : 468 : type = TREE_TYPE (args[0]);
6476 : 468 : k = gfc_validate_kind (mold->ts.type, mold->ts.kind, false);
6477 : :
6478 : 468 : switch (x->ts.type)
6479 : : {
6480 : 378 : case BT_REAL:
6481 : : /* X may be IEEE infinity or NaN, but the representation of MOLD may not
6482 : : support infinity or NaN. */
6483 : 378 : tree finite;
6484 : 378 : finite = build_call_expr_loc (input_location,
6485 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
6486 : : 1, args[0]);
6487 : 378 : finite = convert (logical_type_node, finite);
6488 : :
6489 : 378 : if (mold->ts.type == BT_REAL)
6490 : : {
6491 : 24 : tmp1 = build1 (ABS_EXPR, type, args[0]);
6492 : 24 : tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6493 : : mold->ts.kind, 0);
6494 : 24 : tmp = build2 (GT_EXPR, logical_type_node, tmp1,
6495 : : convert (type, tmp2));
6496 : :
6497 : : /* Check if MOLD representation supports infinity or NaN. */
6498 : 24 : bool infnan = (HONOR_INFINITIES (TREE_TYPE (args[1]))
6499 : 24 : || HONOR_NANS (TREE_TYPE (args[1])));
6500 : 24 : tmp = build3 (COND_EXPR, logical_type_node, finite, tmp,
6501 : : infnan ? logical_false_node : logical_true_node);
6502 : : }
6503 : : else
6504 : : {
6505 : 354 : tree rounded;
6506 : 354 : tree decl;
6507 : :
6508 : 354 : decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, x->ts.kind);
6509 : 354 : gcc_assert (decl != NULL_TREE);
6510 : :
6511 : : /* Round or truncate argument X, depending on the optional argument
6512 : : ROUND (default: .false.). */
6513 : 354 : tmp1 = build_round_expr (args[0], type);
6514 : 354 : tmp2 = build_call_expr_loc (input_location, decl, 1, args[0]);
6515 : 354 : rounded = build3 (COND_EXPR, type, rnd_se.expr, tmp1, tmp2);
6516 : :
6517 : 354 : if (mold->ts.type == BT_INTEGER)
6518 : : {
6519 : 180 : tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6520 : : x->ts.kind);
6521 : 180 : tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6522 : : x->ts.kind);
6523 : : }
6524 : 174 : else if (mold->ts.type == BT_UNSIGNED)
6525 : : {
6526 : 174 : tmp1 = build_real_from_int_cst (type, integer_zero_node);
6527 : 174 : tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6528 : : x->ts.kind);
6529 : : }
6530 : : else
6531 : 0 : gcc_unreachable ();
6532 : :
6533 : 354 : tmp1 = build2 (LT_EXPR, logical_type_node, rounded,
6534 : : convert (type, tmp1));
6535 : 354 : tmp2 = build2 (GT_EXPR, logical_type_node, rounded,
6536 : : convert (type, tmp2));
6537 : 354 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6538 : 354 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node,
6539 : : build1 (TRUTH_NOT_EXPR, logical_type_node, finite),
6540 : : tmp);
6541 : : }
6542 : : break;
6543 : :
6544 : 48 : case BT_INTEGER:
6545 : 48 : if (mold->ts.type == BT_INTEGER)
6546 : : {
6547 : 12 : tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6548 : : x->ts.kind);
6549 : 12 : tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6550 : : x->ts.kind);
6551 : 12 : tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6552 : : convert (type, tmp1));
6553 : 12 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6554 : : convert (type, tmp2));
6555 : 12 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6556 : : }
6557 : 36 : else if (mold->ts.type == BT_UNSIGNED)
6558 : : {
6559 : 36 : int i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
6560 : 36 : tmp = build_int_cst (type, 0);
6561 : 36 : tmp = build2 (LT_EXPR, logical_type_node, args[0], tmp);
6562 : 36 : if (mpz_cmp (gfc_integer_kinds[i].huge,
6563 : 36 : gfc_unsigned_kinds[k].huge) > 0)
6564 : : {
6565 : 0 : tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6566 : : x->ts.kind);
6567 : 0 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6568 : : convert (type, tmp2));
6569 : 0 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp, tmp2);
6570 : : }
6571 : : }
6572 : 0 : else if (mold->ts.type == BT_REAL)
6573 : : {
6574 : 0 : tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6575 : : mold->ts.kind, 0);
6576 : 0 : tmp1 = build1 (NEGATE_EXPR, TREE_TYPE (tmp2), tmp2);
6577 : 0 : tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6578 : : convert (type, tmp1));
6579 : 0 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6580 : : convert (type, tmp2));
6581 : 0 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6582 : : }
6583 : : else
6584 : 0 : gcc_unreachable ();
6585 : : break;
6586 : :
6587 : 42 : case BT_UNSIGNED:
6588 : 42 : if (mold->ts.type == BT_UNSIGNED)
6589 : : {
6590 : 12 : tmp = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6591 : : x->ts.kind);
6592 : 12 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6593 : : convert (type, tmp));
6594 : : }
6595 : 30 : else if (mold->ts.type == BT_INTEGER)
6596 : : {
6597 : 18 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6598 : : x->ts.kind);
6599 : 18 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6600 : : convert (type, tmp));
6601 : : }
6602 : 12 : else if (mold->ts.type == BT_REAL)
6603 : : {
6604 : 12 : tmp = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6605 : : mold->ts.kind, 0);
6606 : 12 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6607 : : convert (type, tmp));
6608 : : }
6609 : : else
6610 : 0 : gcc_unreachable ();
6611 : : break;
6612 : :
6613 : 0 : default:
6614 : 0 : gcc_unreachable ();
6615 : : }
6616 : :
6617 : 468 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
6618 : 468 : }
6619 : :
6620 : :
6621 : : /* Set or clear a single bit. */
6622 : : static void
6623 : 306 : gfc_conv_intrinsic_singlebitop (gfc_se * se, gfc_expr * expr, int set)
6624 : : {
6625 : 306 : tree args[2];
6626 : 306 : tree type;
6627 : 306 : tree tmp;
6628 : 306 : enum tree_code op;
6629 : :
6630 : 306 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6631 : 306 : type = TREE_TYPE (args[0]);
6632 : :
6633 : : /* Optionally generate code for runtime argument check. */
6634 : 306 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6635 : : {
6636 : 12 : tree below = fold_build2_loc (input_location, LT_EXPR,
6637 : : logical_type_node, args[1],
6638 : 12 : build_int_cst (TREE_TYPE (args[1]), 0));
6639 : 12 : tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6640 : 12 : tree above = fold_build2_loc (input_location, GE_EXPR,
6641 : : logical_type_node, args[1], nbits);
6642 : 12 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6643 : : logical_type_node, below, above);
6644 : 12 : size_t len_name = strlen (expr->value.function.isym->name);
6645 : 12 : char *name = XALLOCAVEC (char, len_name + 1);
6646 : 72 : for (size_t i = 0; i < len_name; i++)
6647 : 60 : name[i] = TOUPPER (expr->value.function.isym->name[i]);
6648 : 12 : name[len_name] = '\0';
6649 : 12 : tree iname = gfc_build_addr_expr (pchar_type_node,
6650 : : gfc_build_cstring_const (name));
6651 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6652 : : "POS argument (%ld) out of range 0:%ld "
6653 : : "in intrinsic %s",
6654 : : fold_convert (long_integer_type_node, args[1]),
6655 : : fold_convert (long_integer_type_node, nbits),
6656 : : iname);
6657 : : }
6658 : :
6659 : 306 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6660 : : build_int_cst (type, 1), args[1]);
6661 : 306 : if (set)
6662 : : op = BIT_IOR_EXPR;
6663 : : else
6664 : : {
6665 : 168 : op = BIT_AND_EXPR;
6666 : 168 : tmp = fold_build1_loc (input_location, BIT_NOT_EXPR, type, tmp);
6667 : : }
6668 : 306 : se->expr = fold_build2_loc (input_location, op, type, args[0], tmp);
6669 : 306 : }
6670 : :
6671 : : /* Extract a sequence of bits.
6672 : : IBITS(I, POS, LEN) = (I >> POS) & ~((~0) << LEN). */
6673 : : static void
6674 : 27 : gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr)
6675 : : {
6676 : 27 : tree args[3];
6677 : 27 : tree type;
6678 : 27 : tree tmp;
6679 : 27 : tree mask;
6680 : 27 : tree num_bits, cond;
6681 : :
6682 : 27 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
6683 : 27 : type = TREE_TYPE (args[0]);
6684 : :
6685 : : /* Optionally generate code for runtime argument check. */
6686 : 27 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6687 : : {
6688 : 12 : tree tmp1 = fold_convert (long_integer_type_node, args[1]);
6689 : 12 : tree tmp2 = fold_convert (long_integer_type_node, args[2]);
6690 : 12 : tree nbits = build_int_cst (long_integer_type_node,
6691 : 12 : TYPE_PRECISION (type));
6692 : 12 : tree below = fold_build2_loc (input_location, LT_EXPR,
6693 : : logical_type_node, args[1],
6694 : 12 : build_int_cst (TREE_TYPE (args[1]), 0));
6695 : 12 : tree above = fold_build2_loc (input_location, GT_EXPR,
6696 : : logical_type_node, tmp1, nbits);
6697 : 12 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6698 : : logical_type_node, below, above);
6699 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6700 : : "POS argument (%ld) out of range 0:%ld "
6701 : : "in intrinsic IBITS", tmp1, nbits);
6702 : 12 : below = fold_build2_loc (input_location, LT_EXPR,
6703 : : logical_type_node, args[2],
6704 : 12 : build_int_cst (TREE_TYPE (args[2]), 0));
6705 : 12 : above = fold_build2_loc (input_location, GT_EXPR,
6706 : : logical_type_node, tmp2, nbits);
6707 : 12 : scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6708 : : logical_type_node, below, above);
6709 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6710 : : "LEN argument (%ld) out of range 0:%ld "
6711 : : "in intrinsic IBITS", tmp2, nbits);
6712 : 12 : above = fold_build2_loc (input_location, PLUS_EXPR,
6713 : : long_integer_type_node, tmp1, tmp2);
6714 : 12 : scond = fold_build2_loc (input_location, GT_EXPR,
6715 : : logical_type_node, above, nbits);
6716 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6717 : : "POS(%ld)+LEN(%ld)>BIT_SIZE(%ld) "
6718 : : "in intrinsic IBITS", tmp1, tmp2, nbits);
6719 : : }
6720 : :
6721 : : /* The Fortran standard allows (shift width) LEN <= BIT_SIZE(I), whereas
6722 : : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6723 : : special case. See also gfc_conv_intrinsic_ishft (). */
6724 : 27 : num_bits = build_int_cst (TREE_TYPE (args[2]), TYPE_PRECISION (type));
6725 : :
6726 : 27 : mask = build_int_cst (type, -1);
6727 : 27 : mask = fold_build2_loc (input_location, LSHIFT_EXPR, type, mask, args[2]);
6728 : 27 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[2],
6729 : : num_bits);
6730 : 27 : mask = fold_build3_loc (input_location, COND_EXPR, type, cond,
6731 : : build_int_cst (type, 0), mask);
6732 : 27 : mask = fold_build1_loc (input_location, BIT_NOT_EXPR, type, mask);
6733 : :
6734 : 27 : tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, args[0], args[1]);
6735 : :
6736 : 27 : se->expr = fold_build2_loc (input_location, BIT_AND_EXPR, type, tmp, mask);
6737 : 27 : }
6738 : :
6739 : : static void
6740 : 441 : gfc_conv_intrinsic_shift (gfc_se * se, gfc_expr * expr, bool right_shift,
6741 : : bool arithmetic)
6742 : : {
6743 : 441 : tree args[2], type, num_bits, cond;
6744 : 441 : tree bigshift;
6745 : 441 : bool do_convert = false;
6746 : :
6747 : 441 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6748 : :
6749 : 441 : args[0] = gfc_evaluate_now (args[0], &se->pre);
6750 : 441 : args[1] = gfc_evaluate_now (args[1], &se->pre);
6751 : 441 : type = TREE_TYPE (args[0]);
6752 : :
6753 : 441 : if (!arithmetic)
6754 : : {
6755 : 339 : args[0] = fold_convert (unsigned_type_for (type), args[0]);
6756 : 339 : do_convert = true;
6757 : : }
6758 : : else
6759 : 102 : gcc_assert (right_shift);
6760 : :
6761 : 441 : if (flag_unsigned && arithmetic && expr->ts.type == BT_UNSIGNED)
6762 : : {
6763 : 30 : do_convert = true;
6764 : 30 : args[0] = fold_convert (signed_type_for (type), args[0]);
6765 : : }
6766 : :
6767 : 714 : se->expr = fold_build2_loc (input_location,
6768 : : right_shift ? RSHIFT_EXPR : LSHIFT_EXPR,
6769 : 441 : TREE_TYPE (args[0]), args[0], args[1]);
6770 : :
6771 : 441 : if (do_convert)
6772 : 369 : se->expr = fold_convert (type, se->expr);
6773 : :
6774 : 441 : if (!arithmetic)
6775 : 339 : bigshift = build_int_cst (type, 0);
6776 : : else
6777 : : {
6778 : 102 : tree nonneg = fold_build2_loc (input_location, GE_EXPR,
6779 : : logical_type_node, args[0],
6780 : 102 : build_int_cst (TREE_TYPE (args[0]), 0));
6781 : 102 : bigshift = fold_build3_loc (input_location, COND_EXPR, type, nonneg,
6782 : : build_int_cst (type, 0),
6783 : : build_int_cst (type, -1));
6784 : : }
6785 : :
6786 : : /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
6787 : : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6788 : : special case. */
6789 : 441 : num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6790 : :
6791 : : /* Optionally generate code for runtime argument check. */
6792 : 441 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6793 : : {
6794 : 30 : tree below = fold_build2_loc (input_location, LT_EXPR,
6795 : : logical_type_node, args[1],
6796 : 30 : build_int_cst (TREE_TYPE (args[1]), 0));
6797 : 30 : tree above = fold_build2_loc (input_location, GT_EXPR,
6798 : : logical_type_node, args[1], num_bits);
6799 : 30 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6800 : : logical_type_node, below, above);
6801 : 30 : size_t len_name = strlen (expr->value.function.isym->name);
6802 : 30 : char *name = XALLOCAVEC (char, len_name + 1);
6803 : 210 : for (size_t i = 0; i < len_name; i++)
6804 : 180 : name[i] = TOUPPER (expr->value.function.isym->name[i]);
6805 : 30 : name[len_name] = '\0';
6806 : 30 : tree iname = gfc_build_addr_expr (pchar_type_node,
6807 : : gfc_build_cstring_const (name));
6808 : 30 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6809 : : "SHIFT argument (%ld) out of range 0:%ld "
6810 : : "in intrinsic %s",
6811 : : fold_convert (long_integer_type_node, args[1]),
6812 : : fold_convert (long_integer_type_node, num_bits),
6813 : : iname);
6814 : : }
6815 : :
6816 : 441 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
6817 : : args[1], num_bits);
6818 : :
6819 : 441 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
6820 : : bigshift, se->expr);
6821 : 441 : }
6822 : :
6823 : : /* ISHFT (I, SHIFT) = (abs (shift) >= BIT_SIZE (i))
6824 : : ? 0
6825 : : : ((shift >= 0) ? i << shift : i >> -shift)
6826 : : where all shifts are logical shifts. */
6827 : : static void
6828 : 318 : gfc_conv_intrinsic_ishft (gfc_se * se, gfc_expr * expr)
6829 : : {
6830 : 318 : tree args[2];
6831 : 318 : tree type;
6832 : 318 : tree utype;
6833 : 318 : tree tmp;
6834 : 318 : tree width;
6835 : 318 : tree num_bits;
6836 : 318 : tree cond;
6837 : 318 : tree lshift;
6838 : 318 : tree rshift;
6839 : :
6840 : 318 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6841 : :
6842 : 318 : args[0] = gfc_evaluate_now (args[0], &se->pre);
6843 : 318 : args[1] = gfc_evaluate_now (args[1], &se->pre);
6844 : :
6845 : 318 : type = TREE_TYPE (args[0]);
6846 : 318 : utype = unsigned_type_for (type);
6847 : :
6848 : 318 : width = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (args[1]),
6849 : : args[1]);
6850 : :
6851 : : /* Left shift if positive. */
6852 : 318 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type, args[0], width);
6853 : :
6854 : : /* Right shift if negative.
6855 : : We convert to an unsigned type because we want a logical shift.
6856 : : The standard doesn't define the case of shifting negative
6857 : : numbers, and we try to be compatible with other compilers, most
6858 : : notably g77, here. */
6859 : 318 : rshift = fold_convert (type, fold_build2_loc (input_location, RSHIFT_EXPR,
6860 : : utype, convert (utype, args[0]), width));
6861 : :
6862 : 318 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[1],
6863 : 318 : build_int_cst (TREE_TYPE (args[1]), 0));
6864 : 318 : tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp, lshift, rshift);
6865 : :
6866 : : /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
6867 : : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6868 : : special case. */
6869 : 318 : num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6870 : :
6871 : : /* Optionally generate code for runtime argument check. */
6872 : 318 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6873 : : {
6874 : 24 : tree outside = fold_build2_loc (input_location, GT_EXPR,
6875 : : logical_type_node, width, num_bits);
6876 : 24 : gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
6877 : : "SHIFT argument (%ld) out of range -%ld:%ld "
6878 : : "in intrinsic ISHFT",
6879 : : fold_convert (long_integer_type_node, args[1]),
6880 : : fold_convert (long_integer_type_node, num_bits),
6881 : : fold_convert (long_integer_type_node, num_bits));
6882 : : }
6883 : :
6884 : 318 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, width,
6885 : : num_bits);
6886 : 318 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
6887 : : build_int_cst (type, 0), tmp);
6888 : 318 : }
6889 : :
6890 : :
6891 : : /* Circular shift. AKA rotate or barrel shift. */
6892 : :
6893 : : static void
6894 : 658 : gfc_conv_intrinsic_ishftc (gfc_se * se, gfc_expr * expr)
6895 : : {
6896 : 658 : tree *args;
6897 : 658 : tree type;
6898 : 658 : tree tmp;
6899 : 658 : tree lrot;
6900 : 658 : tree rrot;
6901 : 658 : tree zero;
6902 : 658 : tree nbits;
6903 : 658 : unsigned int num_args;
6904 : :
6905 : 658 : num_args = gfc_intrinsic_argument_list_length (expr);
6906 : 658 : args = XALLOCAVEC (tree, num_args);
6907 : :
6908 : 658 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
6909 : :
6910 : 658 : type = TREE_TYPE (args[0]);
6911 : 658 : nbits = build_int_cst (long_integer_type_node, TYPE_PRECISION (type));
6912 : :
6913 : 658 : if (num_args == 3)
6914 : : {
6915 : 550 : gfc_expr *size = expr->value.function.actual->next->next->expr;
6916 : :
6917 : : /* Use a library function for the 3 parameter version. */
6918 : 550 : tree int4type = gfc_get_int_type (4);
6919 : :
6920 : : /* Treat optional SIZE argument when it is passed as an optional
6921 : : dummy. If SIZE is absent, the default value is BIT_SIZE(I). */
6922 : 550 : if (size->expr_type == EXPR_VARIABLE
6923 : 438 : && size->symtree->n.sym->attr.dummy
6924 : 36 : && size->symtree->n.sym->attr.optional)
6925 : : {
6926 : 36 : tree type_of_size = TREE_TYPE (args[2]);
6927 : 72 : args[2] = build3_loc (input_location, COND_EXPR, type_of_size,
6928 : 36 : gfc_conv_expr_present (size->symtree->n.sym),
6929 : : args[2], fold_convert (type_of_size, nbits));
6930 : : }
6931 : :
6932 : : /* We convert the first argument to at least 4 bytes, and
6933 : : convert back afterwards. This removes the need for library
6934 : : functions for all argument sizes, and function will be
6935 : : aligned to at least 32 bits, so there's no loss. */
6936 : 550 : if (expr->ts.kind < 4)
6937 : 242 : args[0] = convert (int4type, args[0]);
6938 : :
6939 : : /* Convert the SHIFT and SIZE args to INTEGER*4 otherwise we would
6940 : : need loads of library functions. They cannot have values >
6941 : : BIT_SIZE (I) so the conversion is safe. */
6942 : 550 : args[1] = convert (int4type, args[1]);
6943 : 550 : args[2] = convert (int4type, args[2]);
6944 : :
6945 : : /* Optionally generate code for runtime argument check. */
6946 : 550 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6947 : : {
6948 : 18 : tree size = fold_convert (long_integer_type_node, args[2]);
6949 : 18 : tree below = fold_build2_loc (input_location, LE_EXPR,
6950 : : logical_type_node, size,
6951 : 18 : build_int_cst (TREE_TYPE (args[1]), 0));
6952 : 18 : tree above = fold_build2_loc (input_location, GT_EXPR,
6953 : : logical_type_node, size, nbits);
6954 : 18 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6955 : : logical_type_node, below, above);
6956 : 18 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6957 : : "SIZE argument (%ld) out of range 1:%ld "
6958 : : "in intrinsic ISHFTC", size, nbits);
6959 : 18 : tree width = fold_convert (long_integer_type_node, args[1]);
6960 : 18 : width = fold_build1_loc (input_location, ABS_EXPR,
6961 : : long_integer_type_node, width);
6962 : 18 : scond = fold_build2_loc (input_location, GT_EXPR,
6963 : : logical_type_node, width, size);
6964 : 18 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6965 : : "SHIFT argument (%ld) out of range -%ld:%ld "
6966 : : "in intrinsic ISHFTC",
6967 : : fold_convert (long_integer_type_node, args[1]),
6968 : : size, size);
6969 : : }
6970 : :
6971 : 550 : switch (expr->ts.kind)
6972 : : {
6973 : 426 : case 1:
6974 : 426 : case 2:
6975 : 426 : case 4:
6976 : 426 : tmp = gfor_fndecl_math_ishftc4;
6977 : 426 : break;
6978 : 124 : case 8:
6979 : 124 : tmp = gfor_fndecl_math_ishftc8;
6980 : 124 : break;
6981 : 0 : case 16:
6982 : 0 : tmp = gfor_fndecl_math_ishftc16;
6983 : 0 : break;
6984 : 0 : default:
6985 : 0 : gcc_unreachable ();
6986 : : }
6987 : 550 : se->expr = build_call_expr_loc (input_location,
6988 : : tmp, 3, args[0], args[1], args[2]);
6989 : : /* Convert the result back to the original type, if we extended
6990 : : the first argument's width above. */
6991 : 550 : if (expr->ts.kind < 4)
6992 : 242 : se->expr = convert (type, se->expr);
6993 : :
6994 : 550 : return;
6995 : : }
6996 : :
6997 : : /* Evaluate arguments only once. */
6998 : 108 : args[0] = gfc_evaluate_now (args[0], &se->pre);
6999 : 108 : args[1] = gfc_evaluate_now (args[1], &se->pre);
7000 : :
7001 : : /* Optionally generate code for runtime argument check. */
7002 : 108 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7003 : : {
7004 : 12 : tree width = fold_convert (long_integer_type_node, args[1]);
7005 : 12 : width = fold_build1_loc (input_location, ABS_EXPR,
7006 : : long_integer_type_node, width);
7007 : 12 : tree outside = fold_build2_loc (input_location, GT_EXPR,
7008 : : logical_type_node, width, nbits);
7009 : 12 : gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
7010 : : "SHIFT argument (%ld) out of range -%ld:%ld "
7011 : : "in intrinsic ISHFTC",
7012 : : fold_convert (long_integer_type_node, args[1]),
7013 : : nbits, nbits);
7014 : : }
7015 : :
7016 : : /* Rotate left if positive. */
7017 : 108 : lrot = fold_build2_loc (input_location, LROTATE_EXPR, type, args[0], args[1]);
7018 : :
7019 : : /* Rotate right if negative. */
7020 : 108 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (args[1]),
7021 : : args[1]);
7022 : 108 : rrot = fold_build2_loc (input_location,RROTATE_EXPR, type, args[0], tmp);
7023 : :
7024 : 108 : zero = build_int_cst (TREE_TYPE (args[1]), 0);
7025 : 108 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, args[1],
7026 : : zero);
7027 : 108 : rrot = fold_build3_loc (input_location, COND_EXPR, type, tmp, lrot, rrot);
7028 : :
7029 : : /* Do nothing if shift == 0. */
7030 : 108 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, args[1],
7031 : : zero);
7032 : 108 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, args[0],
7033 : : rrot);
7034 : : }
7035 : :
7036 : :
7037 : : /* LEADZ (i) = (i == 0) ? BIT_SIZE (i)
7038 : : : __builtin_clz(i) - (BIT_SIZE('int') - BIT_SIZE(i))
7039 : :
7040 : : The conditional expression is necessary because the result of LEADZ(0)
7041 : : is defined, but the result of __builtin_clz(0) is undefined for most
7042 : : targets.
7043 : :
7044 : : For INTEGER kinds smaller than the C 'int' type, we have to subtract the
7045 : : difference in bit size between the argument of LEADZ and the C int. */
7046 : :
7047 : : static void
7048 : 270 : gfc_conv_intrinsic_leadz (gfc_se * se, gfc_expr * expr)
7049 : : {
7050 : 270 : tree arg;
7051 : 270 : tree arg_type;
7052 : 270 : tree cond;
7053 : 270 : tree result_type;
7054 : 270 : tree leadz;
7055 : 270 : tree bit_size;
7056 : 270 : tree tmp;
7057 : 270 : tree func;
7058 : 270 : int s, argsize;
7059 : :
7060 : 270 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7061 : 270 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7062 : :
7063 : : /* Which variant of __builtin_clz* should we call? */
7064 : 270 : if (argsize <= INT_TYPE_SIZE)
7065 : : {
7066 : 183 : arg_type = unsigned_type_node;
7067 : 183 : func = builtin_decl_explicit (BUILT_IN_CLZ);
7068 : : }
7069 : 87 : else if (argsize <= LONG_TYPE_SIZE)
7070 : : {
7071 : 57 : arg_type = long_unsigned_type_node;
7072 : 57 : func = builtin_decl_explicit (BUILT_IN_CLZL);
7073 : : }
7074 : 30 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7075 : : {
7076 : 0 : arg_type = long_long_unsigned_type_node;
7077 : 0 : func = builtin_decl_explicit (BUILT_IN_CLZLL);
7078 : : }
7079 : : else
7080 : : {
7081 : 30 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7082 : 30 : arg_type = gfc_build_uint_type (argsize);
7083 : 30 : func = NULL_TREE;
7084 : : }
7085 : :
7086 : : /* Convert the actual argument twice: first, to the unsigned type of the
7087 : : same size; then, to the proper argument type for the built-in
7088 : : function. But the return type is of the default INTEGER kind. */
7089 : 270 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7090 : 270 : arg = fold_convert (arg_type, arg);
7091 : 270 : arg = gfc_evaluate_now (arg, &se->pre);
7092 : 270 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7093 : :
7094 : : /* Compute LEADZ for the case i .ne. 0. */
7095 : 270 : if (func)
7096 : : {
7097 : 240 : s = TYPE_PRECISION (arg_type) - argsize;
7098 : 240 : tmp = fold_convert (result_type,
7099 : : build_call_expr_loc (input_location, func,
7100 : : 1, arg));
7101 : 240 : leadz = fold_build2_loc (input_location, MINUS_EXPR, result_type,
7102 : 240 : tmp, build_int_cst (result_type, s));
7103 : : }
7104 : : else
7105 : : {
7106 : : /* We end up here if the argument type is larger than 'long long'.
7107 : : We generate this code:
7108 : :
7109 : : if (x & (ULL_MAX << ULL_SIZE) != 0)
7110 : : return clzll ((unsigned long long) (x >> ULLSIZE));
7111 : : else
7112 : : return ULL_SIZE + clzll ((unsigned long long) x);
7113 : : where ULL_MAX is the largest value that a ULL_MAX can hold
7114 : : (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7115 : : is the bit-size of the long long type (64 in this example). */
7116 : 30 : tree ullsize, ullmax, tmp1, tmp2, btmp;
7117 : :
7118 : 30 : ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7119 : 30 : ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7120 : : long_long_unsigned_type_node,
7121 : : build_int_cst (long_long_unsigned_type_node,
7122 : : 0));
7123 : :
7124 : 30 : cond = fold_build2_loc (input_location, LSHIFT_EXPR, arg_type,
7125 : : fold_convert (arg_type, ullmax), ullsize);
7126 : 30 : cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type,
7127 : : arg, cond);
7128 : 30 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
7129 : : cond, build_int_cst (arg_type, 0));
7130 : :
7131 : 30 : tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7132 : : arg, ullsize);
7133 : 30 : tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7134 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7135 : 30 : tmp1 = fold_convert (result_type,
7136 : : build_call_expr_loc (input_location, btmp, 1, tmp1));
7137 : :
7138 : 30 : tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7139 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7140 : 30 : tmp2 = fold_convert (result_type,
7141 : : build_call_expr_loc (input_location, btmp, 1, tmp2));
7142 : 30 : tmp2 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7143 : : tmp2, ullsize);
7144 : :
7145 : 30 : leadz = fold_build3_loc (input_location, COND_EXPR, result_type,
7146 : : cond, tmp1, tmp2);
7147 : : }
7148 : :
7149 : : /* Build BIT_SIZE. */
7150 : 270 : bit_size = build_int_cst (result_type, argsize);
7151 : :
7152 : 270 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7153 : : arg, build_int_cst (arg_type, 0));
7154 : 270 : se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7155 : : bit_size, leadz);
7156 : 270 : }
7157 : :
7158 : :
7159 : : /* TRAILZ(i) = (i == 0) ? BIT_SIZE (i) : __builtin_ctz(i)
7160 : :
7161 : : The conditional expression is necessary because the result of TRAILZ(0)
7162 : : is defined, but the result of __builtin_ctz(0) is undefined for most
7163 : : targets. */
7164 : :
7165 : : static void
7166 : 282 : gfc_conv_intrinsic_trailz (gfc_se * se, gfc_expr *expr)
7167 : : {
7168 : 282 : tree arg;
7169 : 282 : tree arg_type;
7170 : 282 : tree cond;
7171 : 282 : tree result_type;
7172 : 282 : tree trailz;
7173 : 282 : tree bit_size;
7174 : 282 : tree func;
7175 : 282 : int argsize;
7176 : :
7177 : 282 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7178 : 282 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7179 : :
7180 : : /* Which variant of __builtin_ctz* should we call? */
7181 : 282 : if (argsize <= INT_TYPE_SIZE)
7182 : : {
7183 : 195 : arg_type = unsigned_type_node;
7184 : 195 : func = builtin_decl_explicit (BUILT_IN_CTZ);
7185 : : }
7186 : 87 : else if (argsize <= LONG_TYPE_SIZE)
7187 : : {
7188 : 57 : arg_type = long_unsigned_type_node;
7189 : 57 : func = builtin_decl_explicit (BUILT_IN_CTZL);
7190 : : }
7191 : 30 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7192 : : {
7193 : 0 : arg_type = long_long_unsigned_type_node;
7194 : 0 : func = builtin_decl_explicit (BUILT_IN_CTZLL);
7195 : : }
7196 : : else
7197 : : {
7198 : 30 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7199 : 30 : arg_type = gfc_build_uint_type (argsize);
7200 : 30 : func = NULL_TREE;
7201 : : }
7202 : :
7203 : : /* Convert the actual argument twice: first, to the unsigned type of the
7204 : : same size; then, to the proper argument type for the built-in
7205 : : function. But the return type is of the default INTEGER kind. */
7206 : 282 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7207 : 282 : arg = fold_convert (arg_type, arg);
7208 : 282 : arg = gfc_evaluate_now (arg, &se->pre);
7209 : 282 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7210 : :
7211 : : /* Compute TRAILZ for the case i .ne. 0. */
7212 : 282 : if (func)
7213 : 252 : trailz = fold_convert (result_type, build_call_expr_loc (input_location,
7214 : : func, 1, arg));
7215 : : else
7216 : : {
7217 : : /* We end up here if the argument type is larger than 'long long'.
7218 : : We generate this code:
7219 : :
7220 : : if ((x & ULL_MAX) == 0)
7221 : : return ULL_SIZE + ctzll ((unsigned long long) (x >> ULLSIZE));
7222 : : else
7223 : : return ctzll ((unsigned long long) x);
7224 : :
7225 : : where ULL_MAX is the largest value that a ULL_MAX can hold
7226 : : (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7227 : : is the bit-size of the long long type (64 in this example). */
7228 : 30 : tree ullsize, ullmax, tmp1, tmp2, btmp;
7229 : :
7230 : 30 : ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7231 : 30 : ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7232 : : long_long_unsigned_type_node,
7233 : : build_int_cst (long_long_unsigned_type_node, 0));
7234 : :
7235 : 30 : cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type, arg,
7236 : : fold_convert (arg_type, ullmax));
7237 : 30 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, cond,
7238 : : build_int_cst (arg_type, 0));
7239 : :
7240 : 30 : tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7241 : : arg, ullsize);
7242 : 30 : tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7243 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7244 : 30 : tmp1 = fold_convert (result_type,
7245 : : build_call_expr_loc (input_location, btmp, 1, tmp1));
7246 : 30 : tmp1 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7247 : : tmp1, ullsize);
7248 : :
7249 : 30 : tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7250 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7251 : 30 : tmp2 = fold_convert (result_type,
7252 : : build_call_expr_loc (input_location, btmp, 1, tmp2));
7253 : :
7254 : 30 : trailz = fold_build3_loc (input_location, COND_EXPR, result_type,
7255 : : cond, tmp1, tmp2);
7256 : : }
7257 : :
7258 : : /* Build BIT_SIZE. */
7259 : 282 : bit_size = build_int_cst (result_type, argsize);
7260 : :
7261 : 282 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7262 : : arg, build_int_cst (arg_type, 0));
7263 : 282 : se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7264 : : bit_size, trailz);
7265 : 282 : }
7266 : :
7267 : : /* Using __builtin_popcount for POPCNT and __builtin_parity for POPPAR;
7268 : : for types larger than "long long", we call the long long built-in for
7269 : : the lower and higher bits and combine the result. */
7270 : :
7271 : : static void
7272 : 134 : gfc_conv_intrinsic_popcnt_poppar (gfc_se * se, gfc_expr *expr, int parity)
7273 : : {
7274 : 134 : tree arg;
7275 : 134 : tree arg_type;
7276 : 134 : tree result_type;
7277 : 134 : tree func;
7278 : 134 : int argsize;
7279 : :
7280 : 134 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7281 : 134 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7282 : 134 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7283 : :
7284 : : /* Which variant of the builtin should we call? */
7285 : 134 : if (argsize <= INT_TYPE_SIZE)
7286 : : {
7287 : 108 : arg_type = unsigned_type_node;
7288 : 198 : func = builtin_decl_explicit (parity
7289 : : ? BUILT_IN_PARITY
7290 : : : BUILT_IN_POPCOUNT);
7291 : : }
7292 : 26 : else if (argsize <= LONG_TYPE_SIZE)
7293 : : {
7294 : 12 : arg_type = long_unsigned_type_node;
7295 : 18 : func = builtin_decl_explicit (parity
7296 : : ? BUILT_IN_PARITYL
7297 : : : BUILT_IN_POPCOUNTL);
7298 : : }
7299 : 14 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7300 : : {
7301 : 0 : arg_type = long_long_unsigned_type_node;
7302 : 0 : func = builtin_decl_explicit (parity
7303 : : ? BUILT_IN_PARITYLL
7304 : : : BUILT_IN_POPCOUNTLL);
7305 : : }
7306 : : else
7307 : : {
7308 : : /* Our argument type is larger than 'long long', which mean none
7309 : : of the POPCOUNT builtins covers it. We thus call the 'long long'
7310 : : variant multiple times, and add the results. */
7311 : 14 : tree utype, arg2, call1, call2;
7312 : :
7313 : : /* For now, we only cover the case where argsize is twice as large
7314 : : as 'long long'. */
7315 : 14 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7316 : :
7317 : 21 : func = builtin_decl_explicit (parity
7318 : : ? BUILT_IN_PARITYLL
7319 : : : BUILT_IN_POPCOUNTLL);
7320 : :
7321 : : /* Convert it to an integer, and store into a variable. */
7322 : 14 : utype = gfc_build_uint_type (argsize);
7323 : 14 : arg = fold_convert (utype, arg);
7324 : 14 : arg = gfc_evaluate_now (arg, &se->pre);
7325 : :
7326 : : /* Call the builtin twice. */
7327 : 14 : call1 = build_call_expr_loc (input_location, func, 1,
7328 : : fold_convert (long_long_unsigned_type_node,
7329 : : arg));
7330 : :
7331 : 14 : arg2 = fold_build2_loc (input_location, RSHIFT_EXPR, utype, arg,
7332 : : build_int_cst (utype, LONG_LONG_TYPE_SIZE));
7333 : 14 : call2 = build_call_expr_loc (input_location, func, 1,
7334 : : fold_convert (long_long_unsigned_type_node,
7335 : : arg2));
7336 : :
7337 : : /* Combine the results. */
7338 : 14 : if (parity)
7339 : 7 : se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR,
7340 : : integer_type_node, call1, call2);
7341 : : else
7342 : 7 : se->expr = fold_build2_loc (input_location, PLUS_EXPR,
7343 : : integer_type_node, call1, call2);
7344 : :
7345 : 14 : se->expr = convert (result_type, se->expr);
7346 : 14 : return;
7347 : : }
7348 : :
7349 : : /* Convert the actual argument twice: first, to the unsigned type of the
7350 : : same size; then, to the proper argument type for the built-in
7351 : : function. */
7352 : 120 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7353 : 120 : arg = fold_convert (arg_type, arg);
7354 : :
7355 : 120 : se->expr = fold_convert (result_type,
7356 : : build_call_expr_loc (input_location, func, 1, arg));
7357 : : }
7358 : :
7359 : :
7360 : : /* Process an intrinsic with unspecified argument-types that has an optional
7361 : : argument (which could be of type character), e.g. EOSHIFT. For those, we
7362 : : need to append the string length of the optional argument if it is not
7363 : : present and the type is really character.
7364 : : primary specifies the position (starting at 1) of the non-optional argument
7365 : : specifying the type and optional gives the position of the optional
7366 : : argument in the arglist. */
7367 : :
7368 : : static void
7369 : 6074 : conv_generic_with_optional_char_arg (gfc_se* se, gfc_expr* expr,
7370 : : unsigned primary, unsigned optional)
7371 : : {
7372 : 6074 : gfc_actual_arglist* prim_arg;
7373 : 6074 : gfc_actual_arglist* opt_arg;
7374 : 6074 : unsigned cur_pos;
7375 : 6074 : gfc_actual_arglist* arg;
7376 : 6074 : gfc_symbol* sym;
7377 : 6074 : vec<tree, va_gc> *append_args;
7378 : :
7379 : : /* Find the two arguments given as position. */
7380 : 6074 : cur_pos = 0;
7381 : 6074 : prim_arg = NULL;
7382 : 6074 : opt_arg = NULL;
7383 : 18222 : for (arg = expr->value.function.actual; arg; arg = arg->next)
7384 : : {
7385 : 18222 : ++cur_pos;
7386 : :
7387 : 18222 : if (cur_pos == primary)
7388 : 6074 : prim_arg = arg;
7389 : 18222 : if (cur_pos == optional)
7390 : 6074 : opt_arg = arg;
7391 : :
7392 : 18222 : if (cur_pos >= primary && cur_pos >= optional)
7393 : : break;
7394 : : }
7395 : 6074 : gcc_assert (prim_arg);
7396 : 6074 : gcc_assert (prim_arg->expr);
7397 : 6074 : gcc_assert (opt_arg);
7398 : :
7399 : : /* If we do have type CHARACTER and the optional argument is really absent,
7400 : : append a dummy 0 as string length. */
7401 : 6074 : append_args = NULL;
7402 : 6074 : if (prim_arg->expr->ts.type == BT_CHARACTER && !opt_arg->expr)
7403 : : {
7404 : 608 : tree dummy;
7405 : :
7406 : 608 : dummy = build_int_cst (gfc_charlen_type_node, 0);
7407 : 608 : vec_alloc (append_args, 1);
7408 : 608 : append_args->quick_push (dummy);
7409 : : }
7410 : :
7411 : : /* Build the call itself. */
7412 : 6074 : gcc_assert (!se->ignore_optional);
7413 : 6074 : sym = gfc_get_symbol_for_expr (expr, false);
7414 : 6074 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
7415 : : append_args);
7416 : 6074 : gfc_free_symbol (sym);
7417 : 6074 : }
7418 : :
7419 : : /* The length of a character string. */
7420 : : static void
7421 : 5621 : gfc_conv_intrinsic_len (gfc_se * se, gfc_expr * expr)
7422 : : {
7423 : 5621 : tree len;
7424 : 5621 : tree type;
7425 : 5621 : tree decl;
7426 : 5621 : gfc_symbol *sym;
7427 : 5621 : gfc_se argse;
7428 : 5621 : gfc_expr *arg;
7429 : :
7430 : 5621 : gcc_assert (!se->ss);
7431 : :
7432 : 5621 : arg = expr->value.function.actual->expr;
7433 : :
7434 : 5621 : type = gfc_typenode_for_spec (&expr->ts);
7435 : 5621 : switch (arg->expr_type)
7436 : : {
7437 : 0 : case EXPR_CONSTANT:
7438 : 0 : len = build_int_cst (gfc_charlen_type_node, arg->value.character.length);
7439 : 0 : break;
7440 : :
7441 : 2 : case EXPR_ARRAY:
7442 : : /* Obtain the string length from the function used by
7443 : : trans-array.cc(gfc_trans_array_constructor). */
7444 : 2 : len = NULL_TREE;
7445 : 2 : get_array_ctor_strlen (&se->pre, arg->value.constructor, &len);
7446 : 2 : break;
7447 : :
7448 : 5036 : case EXPR_VARIABLE:
7449 : 5036 : if (arg->ref == NULL
7450 : 2276 : || (arg->ref->next == NULL && arg->ref->type == REF_ARRAY))
7451 : : {
7452 : : /* This doesn't catch all cases.
7453 : : See http://gcc.gnu.org/ml/fortran/2004-06/msg00165.html
7454 : : and the surrounding thread. */
7455 : 4504 : sym = arg->symtree->n.sym;
7456 : 4504 : decl = gfc_get_symbol_decl (sym);
7457 : 4504 : if (decl == current_function_decl && sym->attr.function
7458 : 53 : && (sym->result == sym))
7459 : 53 : decl = gfc_get_fake_result_decl (sym, 0);
7460 : :
7461 : 4504 : len = sym->ts.u.cl->backend_decl;
7462 : 4504 : gcc_assert (len);
7463 : : break;
7464 : : }
7465 : :
7466 : : /* Fall through. */
7467 : :
7468 : 1115 : default:
7469 : 1115 : gfc_init_se (&argse, se);
7470 : 1115 : if (arg->rank == 0)
7471 : 993 : gfc_conv_expr (&argse, arg);
7472 : : else
7473 : 122 : gfc_conv_expr_descriptor (&argse, arg);
7474 : 1115 : gfc_add_block_to_block (&se->pre, &argse.pre);
7475 : 1115 : gfc_add_block_to_block (&se->post, &argse.post);
7476 : 1115 : len = argse.string_length;
7477 : 1115 : break;
7478 : : }
7479 : 5621 : se->expr = convert (type, len);
7480 : 5621 : }
7481 : :
7482 : : /* The length of a character string not including trailing blanks. */
7483 : : static void
7484 : 2313 : gfc_conv_intrinsic_len_trim (gfc_se * se, gfc_expr * expr)
7485 : : {
7486 : 2313 : int kind = expr->value.function.actual->expr->ts.kind;
7487 : 2313 : tree args[2], type, fndecl;
7488 : :
7489 : 2313 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7490 : 2313 : type = gfc_typenode_for_spec (&expr->ts);
7491 : :
7492 : 2313 : if (kind == 1)
7493 : 1911 : fndecl = gfor_fndecl_string_len_trim;
7494 : 402 : else if (kind == 4)
7495 : 402 : fndecl = gfor_fndecl_string_len_trim_char4;
7496 : : else
7497 : 0 : gcc_unreachable ();
7498 : :
7499 : 2313 : se->expr = build_call_expr_loc (input_location,
7500 : : fndecl, 2, args[0], args[1]);
7501 : 2313 : se->expr = convert (type, se->expr);
7502 : 2313 : }
7503 : :
7504 : :
7505 : : /* Returns the starting position of a substring within a string. */
7506 : :
7507 : : static void
7508 : 738 : gfc_conv_intrinsic_index_scan_verify (gfc_se * se, gfc_expr * expr,
7509 : : tree function)
7510 : : {
7511 : 738 : tree logical4_type_node = gfc_get_logical_type (4);
7512 : 738 : tree type;
7513 : 738 : tree fndecl;
7514 : 738 : tree *args;
7515 : 738 : unsigned int num_args;
7516 : :
7517 : 738 : args = XALLOCAVEC (tree, 5);
7518 : :
7519 : : /* Get number of arguments; characters count double due to the
7520 : : string length argument. Kind= is not passed to the library
7521 : : and thus ignored. */
7522 : 738 : if (expr->value.function.actual->next->next->expr == NULL)
7523 : : num_args = 4;
7524 : : else
7525 : 304 : num_args = 5;
7526 : :
7527 : 738 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7528 : 738 : type = gfc_typenode_for_spec (&expr->ts);
7529 : :
7530 : 738 : if (num_args == 4)
7531 : 434 : args[4] = build_int_cst (logical4_type_node, 0);
7532 : : else
7533 : 304 : args[4] = convert (logical4_type_node, args[4]);
7534 : :
7535 : 738 : fndecl = build_addr (function);
7536 : 738 : se->expr = build_call_array_loc (input_location,
7537 : 738 : TREE_TYPE (TREE_TYPE (function)), fndecl,
7538 : : 5, args);
7539 : 738 : se->expr = convert (type, se->expr);
7540 : :
7541 : 738 : }
7542 : :
7543 : : /* The ascii value for a single character. */
7544 : : static void
7545 : 2033 : gfc_conv_intrinsic_ichar (gfc_se * se, gfc_expr * expr)
7546 : : {
7547 : 2033 : tree args[3], type, pchartype;
7548 : 2033 : int nargs;
7549 : :
7550 : 2033 : nargs = gfc_intrinsic_argument_list_length (expr);
7551 : 2033 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
7552 : 2033 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (args[1])));
7553 : 2033 : pchartype = gfc_get_pchar_type (expr->value.function.actual->expr->ts.kind);
7554 : 2033 : args[1] = fold_build1_loc (input_location, NOP_EXPR, pchartype, args[1]);
7555 : 2033 : type = gfc_typenode_for_spec (&expr->ts);
7556 : :
7557 : 2033 : se->expr = build_fold_indirect_ref_loc (input_location,
7558 : : args[1]);
7559 : 2033 : se->expr = convert (type, se->expr);
7560 : 2033 : }
7561 : :
7562 : :
7563 : : /* Intrinsic ISNAN calls __builtin_isnan. */
7564 : :
7565 : : static void
7566 : 432 : gfc_conv_intrinsic_isnan (gfc_se * se, gfc_expr * expr)
7567 : : {
7568 : 432 : tree arg;
7569 : :
7570 : 432 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7571 : 432 : se->expr = build_call_expr_loc (input_location,
7572 : : builtin_decl_explicit (BUILT_IN_ISNAN),
7573 : : 1, arg);
7574 : 864 : STRIP_TYPE_NOPS (se->expr);
7575 : 432 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
7576 : 432 : }
7577 : :
7578 : :
7579 : : /* Intrinsics IS_IOSTAT_END and IS_IOSTAT_EOR just need to compare
7580 : : their argument against a constant integer value. */
7581 : :
7582 : : static void
7583 : 24 : gfc_conv_has_intvalue (gfc_se * se, gfc_expr * expr, const int value)
7584 : : {
7585 : 24 : tree arg;
7586 : :
7587 : 24 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7588 : 24 : se->expr = fold_build2_loc (input_location, EQ_EXPR,
7589 : : gfc_typenode_for_spec (&expr->ts),
7590 : 24 : arg, build_int_cst (TREE_TYPE (arg), value));
7591 : 24 : }
7592 : :
7593 : :
7594 : :
7595 : : /* MERGE (tsource, fsource, mask) = mask ? tsource : fsource. */
7596 : :
7597 : : static void
7598 : 949 : gfc_conv_intrinsic_merge (gfc_se * se, gfc_expr * expr)
7599 : : {
7600 : 949 : tree tsource;
7601 : 949 : tree fsource;
7602 : 949 : tree mask;
7603 : 949 : tree type;
7604 : 949 : tree len, len2;
7605 : 949 : tree *args;
7606 : 949 : unsigned int num_args;
7607 : :
7608 : 949 : num_args = gfc_intrinsic_argument_list_length (expr);
7609 : 949 : args = XALLOCAVEC (tree, num_args);
7610 : :
7611 : 949 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7612 : 949 : if (expr->ts.type != BT_CHARACTER)
7613 : : {
7614 : 422 : tsource = args[0];
7615 : 422 : fsource = args[1];
7616 : 422 : mask = args[2];
7617 : : }
7618 : : else
7619 : : {
7620 : : /* We do the same as in the non-character case, but the argument
7621 : : list is different because of the string length arguments. We
7622 : : also have to set the string length for the result. */
7623 : 527 : len = args[0];
7624 : 527 : tsource = args[1];
7625 : 527 : len2 = args[2];
7626 : 527 : fsource = args[3];
7627 : 527 : mask = args[4];
7628 : :
7629 : 527 : gfc_trans_same_strlen_check ("MERGE intrinsic", &expr->where, len, len2,
7630 : : &se->pre);
7631 : 527 : se->string_length = len;
7632 : : }
7633 : 949 : tsource = gfc_evaluate_now (tsource, &se->pre);
7634 : 949 : fsource = gfc_evaluate_now (fsource, &se->pre);
7635 : 949 : mask = gfc_evaluate_now (mask, &se->pre);
7636 : 949 : type = TREE_TYPE (tsource);
7637 : 949 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, mask, tsource,
7638 : : fold_convert (type, fsource));
7639 : 949 : }
7640 : :
7641 : :
7642 : : /* MERGE_BITS (I, J, MASK) = (I & MASK) | (I & (~MASK)). */
7643 : :
7644 : : static void
7645 : 42 : gfc_conv_intrinsic_merge_bits (gfc_se * se, gfc_expr * expr)
7646 : : {
7647 : 42 : tree args[3], mask, type;
7648 : :
7649 : 42 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
7650 : 42 : mask = gfc_evaluate_now (args[2], &se->pre);
7651 : :
7652 : 42 : type = TREE_TYPE (args[0]);
7653 : 42 : gcc_assert (TREE_TYPE (args[1]) == type);
7654 : 42 : gcc_assert (TREE_TYPE (mask) == type);
7655 : :
7656 : 42 : args[0] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], mask);
7657 : 42 : args[1] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[1],
7658 : : fold_build1_loc (input_location, BIT_NOT_EXPR,
7659 : : type, mask));
7660 : 42 : se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
7661 : : args[0], args[1]);
7662 : 42 : }
7663 : :
7664 : :
7665 : : /* MASKL(n) = n == 0 ? 0 : (~0) << (BIT_SIZE - n)
7666 : : MASKR(n) = n == BIT_SIZE ? ~0 : ~((~0) << n) */
7667 : :
7668 : : static void
7669 : 64 : gfc_conv_intrinsic_mask (gfc_se * se, gfc_expr * expr, int left)
7670 : : {
7671 : 64 : tree arg, allones, type, utype, res, cond, bitsize;
7672 : 64 : int i;
7673 : :
7674 : 64 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7675 : 64 : arg = gfc_evaluate_now (arg, &se->pre);
7676 : :
7677 : 64 : type = gfc_get_int_type (expr->ts.kind);
7678 : 64 : utype = unsigned_type_for (type);
7679 : :
7680 : 64 : i = gfc_validate_kind (BT_INTEGER, expr->ts.kind, false);
7681 : 64 : bitsize = build_int_cst (TREE_TYPE (arg), gfc_integer_kinds[i].bit_size);
7682 : :
7683 : 64 : allones = fold_build1_loc (input_location, BIT_NOT_EXPR, utype,
7684 : : build_int_cst (utype, 0));
7685 : :
7686 : 64 : if (left)
7687 : : {
7688 : : /* Left-justified mask. */
7689 : 32 : res = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (arg),
7690 : : bitsize, arg);
7691 : 32 : res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7692 : : fold_convert (utype, res));
7693 : :
7694 : : /* Special case arg == 0, because SHIFT_EXPR wants a shift strictly
7695 : : smaller than type width. */
7696 : 32 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
7697 : 32 : build_int_cst (TREE_TYPE (arg), 0));
7698 : 32 : res = fold_build3_loc (input_location, COND_EXPR, utype, cond,
7699 : : build_int_cst (utype, 0), res);
7700 : : }
7701 : : else
7702 : : {
7703 : : /* Right-justified mask. */
7704 : 32 : res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7705 : : fold_convert (utype, arg));
7706 : 32 : res = fold_build1_loc (input_location, BIT_NOT_EXPR, utype, res);
7707 : :
7708 : : /* Special case agr == bit_size, because SHIFT_EXPR wants a shift
7709 : : strictly smaller than type width. */
7710 : 32 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7711 : : arg, bitsize);
7712 : 32 : res = fold_build3_loc (input_location, COND_EXPR, utype,
7713 : : cond, allones, res);
7714 : : }
7715 : :
7716 : 64 : se->expr = fold_convert (type, res);
7717 : 64 : }
7718 : :
7719 : :
7720 : : /* FRACTION (s) is translated into:
7721 : : isfinite (s) ? frexp (s, &dummy_int) : NaN */
7722 : : static void
7723 : 60 : gfc_conv_intrinsic_fraction (gfc_se * se, gfc_expr * expr)
7724 : : {
7725 : 60 : tree arg, type, tmp, res, frexp, cond;
7726 : :
7727 : 60 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7728 : :
7729 : 60 : type = gfc_typenode_for_spec (&expr->ts);
7730 : 60 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7731 : 60 : arg = gfc_evaluate_now (arg, &se->pre);
7732 : :
7733 : 60 : cond = build_call_expr_loc (input_location,
7734 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
7735 : : 1, arg);
7736 : :
7737 : 60 : tmp = gfc_create_var (integer_type_node, NULL);
7738 : 60 : res = build_call_expr_loc (input_location, frexp, 2,
7739 : : fold_convert (type, arg),
7740 : : gfc_build_addr_expr (NULL_TREE, tmp));
7741 : 60 : res = fold_convert (type, res);
7742 : :
7743 : 60 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
7744 : : cond, res, gfc_build_nan (type, ""));
7745 : 60 : }
7746 : :
7747 : :
7748 : : /* NEAREST (s, dir) is translated into
7749 : : tmp = copysign (HUGE_VAL, dir);
7750 : : return nextafter (s, tmp);
7751 : : */
7752 : : static void
7753 : 1595 : gfc_conv_intrinsic_nearest (gfc_se * se, gfc_expr * expr)
7754 : : {
7755 : 1595 : tree args[2], type, tmp, nextafter, copysign, huge_val;
7756 : :
7757 : 1595 : nextafter = gfc_builtin_decl_for_float_kind (BUILT_IN_NEXTAFTER, expr->ts.kind);
7758 : 1595 : copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
7759 : :
7760 : 1595 : type = gfc_typenode_for_spec (&expr->ts);
7761 : 1595 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7762 : :
7763 : 1595 : huge_val = gfc_build_inf_or_huge (type, expr->ts.kind);
7764 : 1595 : tmp = build_call_expr_loc (input_location, copysign, 2, huge_val,
7765 : : fold_convert (type, args[1]));
7766 : 1595 : se->expr = build_call_expr_loc (input_location, nextafter, 2,
7767 : : fold_convert (type, args[0]), tmp);
7768 : 1595 : se->expr = fold_convert (type, se->expr);
7769 : 1595 : }
7770 : :
7771 : :
7772 : : /* SPACING (s) is translated into
7773 : : int e;
7774 : : if (!isfinite (s))
7775 : : res = NaN;
7776 : : else if (s == 0)
7777 : : res = tiny;
7778 : : else
7779 : : {
7780 : : frexp (s, &e);
7781 : : e = e - prec;
7782 : : e = MAX_EXPR (e, emin);
7783 : : res = scalbn (1., e);
7784 : : }
7785 : : return res;
7786 : :
7787 : : where prec is the precision of s, gfc_real_kinds[k].digits,
7788 : : emin is min_exponent - 1, gfc_real_kinds[k].min_exponent - 1,
7789 : : and tiny is tiny(s), gfc_real_kinds[k].tiny. */
7790 : :
7791 : : static void
7792 : 70 : gfc_conv_intrinsic_spacing (gfc_se * se, gfc_expr * expr)
7793 : : {
7794 : 70 : tree arg, type, prec, emin, tiny, res, e;
7795 : 70 : tree cond, nan, tmp, frexp, scalbn;
7796 : 70 : int k;
7797 : 70 : stmtblock_t block;
7798 : :
7799 : 70 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
7800 : 70 : prec = build_int_cst (integer_type_node, gfc_real_kinds[k].digits);
7801 : 70 : emin = build_int_cst (integer_type_node, gfc_real_kinds[k].min_exponent - 1);
7802 : 70 : tiny = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].tiny, expr->ts.kind, 0);
7803 : :
7804 : 70 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7805 : 70 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7806 : :
7807 : 70 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7808 : 70 : arg = gfc_evaluate_now (arg, &se->pre);
7809 : :
7810 : 70 : type = gfc_typenode_for_spec (&expr->ts);
7811 : 70 : e = gfc_create_var (integer_type_node, NULL);
7812 : 70 : res = gfc_create_var (type, NULL);
7813 : :
7814 : :
7815 : : /* Build the block for s /= 0. */
7816 : 70 : gfc_start_block (&block);
7817 : 70 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
7818 : : gfc_build_addr_expr (NULL_TREE, e));
7819 : 70 : gfc_add_expr_to_block (&block, tmp);
7820 : :
7821 : 70 : tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node, e,
7822 : : prec);
7823 : 70 : gfc_add_modify (&block, e, fold_build2_loc (input_location, MAX_EXPR,
7824 : : integer_type_node, tmp, emin));
7825 : :
7826 : 70 : tmp = build_call_expr_loc (input_location, scalbn, 2,
7827 : 70 : build_real_from_int_cst (type, integer_one_node), e);
7828 : 70 : gfc_add_modify (&block, res, tmp);
7829 : :
7830 : : /* Finish by building the IF statement for value zero. */
7831 : 70 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
7832 : 70 : build_real_from_int_cst (type, integer_zero_node));
7833 : 70 : tmp = build3_v (COND_EXPR, cond, build2_v (MODIFY_EXPR, res, tiny),
7834 : : gfc_finish_block (&block));
7835 : :
7836 : : /* And deal with infinities and NaNs. */
7837 : 70 : cond = build_call_expr_loc (input_location,
7838 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
7839 : : 1, arg);
7840 : 70 : nan = gfc_build_nan (type, "");
7841 : 70 : tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, res, nan));
7842 : :
7843 : 70 : gfc_add_expr_to_block (&se->pre, tmp);
7844 : 70 : se->expr = res;
7845 : 70 : }
7846 : :
7847 : :
7848 : : /* RRSPACING (s) is translated into
7849 : : int e;
7850 : : real x;
7851 : : x = fabs (s);
7852 : : if (isfinite (x))
7853 : : {
7854 : : if (x != 0)
7855 : : {
7856 : : frexp (s, &e);
7857 : : x = scalbn (x, precision - e);
7858 : : }
7859 : : }
7860 : : else
7861 : : x = NaN;
7862 : : return x;
7863 : :
7864 : : where precision is gfc_real_kinds[k].digits. */
7865 : :
7866 : : static void
7867 : 48 : gfc_conv_intrinsic_rrspacing (gfc_se * se, gfc_expr * expr)
7868 : : {
7869 : 48 : tree arg, type, e, x, cond, nan, stmt, tmp, frexp, scalbn, fabs;
7870 : 48 : int prec, k;
7871 : 48 : stmtblock_t block;
7872 : :
7873 : 48 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
7874 : 48 : prec = gfc_real_kinds[k].digits;
7875 : :
7876 : 48 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7877 : 48 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7878 : 48 : fabs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
7879 : :
7880 : 48 : type = gfc_typenode_for_spec (&expr->ts);
7881 : 48 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7882 : 48 : arg = gfc_evaluate_now (arg, &se->pre);
7883 : :
7884 : 48 : e = gfc_create_var (integer_type_node, NULL);
7885 : 48 : x = gfc_create_var (type, NULL);
7886 : 48 : gfc_add_modify (&se->pre, x,
7887 : : build_call_expr_loc (input_location, fabs, 1, arg));
7888 : :
7889 : :
7890 : 48 : gfc_start_block (&block);
7891 : 48 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
7892 : : gfc_build_addr_expr (NULL_TREE, e));
7893 : 48 : gfc_add_expr_to_block (&block, tmp);
7894 : :
7895 : 48 : tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
7896 : 48 : build_int_cst (integer_type_node, prec), e);
7897 : 48 : tmp = build_call_expr_loc (input_location, scalbn, 2, x, tmp);
7898 : 48 : gfc_add_modify (&block, x, tmp);
7899 : 48 : stmt = gfc_finish_block (&block);
7900 : :
7901 : : /* if (x != 0) */
7902 : 48 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, x,
7903 : 48 : build_real_from_int_cst (type, integer_zero_node));
7904 : 48 : tmp = build3_v (COND_EXPR, cond, stmt, build_empty_stmt (input_location));
7905 : :
7906 : : /* And deal with infinities and NaNs. */
7907 : 48 : cond = build_call_expr_loc (input_location,
7908 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
7909 : : 1, x);
7910 : 48 : nan = gfc_build_nan (type, "");
7911 : 48 : tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, x, nan));
7912 : :
7913 : 48 : gfc_add_expr_to_block (&se->pre, tmp);
7914 : 48 : se->expr = fold_convert (type, x);
7915 : 48 : }
7916 : :
7917 : :
7918 : : /* SCALE (s, i) is translated into scalbn (s, i). */
7919 : : static void
7920 : 72 : gfc_conv_intrinsic_scale (gfc_se * se, gfc_expr * expr)
7921 : : {
7922 : 72 : tree args[2], type, scalbn;
7923 : :
7924 : 72 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7925 : :
7926 : 72 : type = gfc_typenode_for_spec (&expr->ts);
7927 : 72 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7928 : 72 : se->expr = build_call_expr_loc (input_location, scalbn, 2,
7929 : : fold_convert (type, args[0]),
7930 : : fold_convert (integer_type_node, args[1]));
7931 : 72 : se->expr = fold_convert (type, se->expr);
7932 : 72 : }
7933 : :
7934 : :
7935 : : /* SET_EXPONENT (s, i) is translated into
7936 : : isfinite(s) ? scalbn (frexp (s, &dummy_int), i) : NaN */
7937 : : static void
7938 : 262 : gfc_conv_intrinsic_set_exponent (gfc_se * se, gfc_expr * expr)
7939 : : {
7940 : 262 : tree args[2], type, tmp, frexp, scalbn, cond, nan, res;
7941 : :
7942 : 262 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7943 : 262 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7944 : :
7945 : 262 : type = gfc_typenode_for_spec (&expr->ts);
7946 : 262 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7947 : 262 : args[0] = gfc_evaluate_now (args[0], &se->pre);
7948 : :
7949 : 262 : tmp = gfc_create_var (integer_type_node, NULL);
7950 : 262 : tmp = build_call_expr_loc (input_location, frexp, 2,
7951 : : fold_convert (type, args[0]),
7952 : : gfc_build_addr_expr (NULL_TREE, tmp));
7953 : 262 : res = build_call_expr_loc (input_location, scalbn, 2, tmp,
7954 : : fold_convert (integer_type_node, args[1]));
7955 : 262 : res = fold_convert (type, res);
7956 : :
7957 : : /* Call to isfinite */
7958 : 262 : cond = build_call_expr_loc (input_location,
7959 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
7960 : : 1, args[0]);
7961 : 262 : nan = gfc_build_nan (type, "");
7962 : :
7963 : 262 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
7964 : : res, nan);
7965 : 262 : }
7966 : :
7967 : :
7968 : : static void
7969 : 14561 : gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
7970 : : {
7971 : 14561 : gfc_actual_arglist *actual;
7972 : 14561 : tree arg1;
7973 : 14561 : tree type;
7974 : 14561 : tree size;
7975 : 14561 : gfc_se argse;
7976 : 14561 : gfc_expr *e;
7977 : 14561 : gfc_symbol *sym = NULL;
7978 : :
7979 : 14561 : gfc_init_se (&argse, NULL);
7980 : 14561 : actual = expr->value.function.actual;
7981 : :
7982 : 14561 : if (actual->expr->ts.type == BT_CLASS)
7983 : 578 : gfc_add_class_array_ref (actual->expr);
7984 : :
7985 : 14561 : e = actual->expr;
7986 : :
7987 : : /* These are emerging from the interface mapping, when a class valued
7988 : : function appears as the rhs in a realloc on assign statement, where
7989 : : the size of the result is that of one of the actual arguments. */
7990 : 14561 : if (e->expr_type == EXPR_VARIABLE
7991 : 14107 : && e->symtree->n.sym->ns == NULL /* This is distinctive! */
7992 : 555 : && e->symtree->n.sym->ts.type == BT_CLASS
7993 : 44 : && e->ref && e->ref->type == REF_COMPONENT
7994 : 26 : && strcmp (e->ref->u.c.component->name, "_data") == 0)
7995 : 14561 : sym = e->symtree->n.sym;
7996 : :
7997 : 14561 : if ((gfc_option.rtcheck & GFC_RTCHECK_POINTER)
7998 : : && e
7999 : 854 : && (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION))
8000 : : {
8001 : 854 : symbol_attribute attr;
8002 : 854 : char *msg;
8003 : 854 : tree temp;
8004 : 854 : tree cond;
8005 : :
8006 : 854 : if (e->symtree->n.sym && IS_CLASS_ARRAY (e->symtree->n.sym))
8007 : : {
8008 : 33 : attr = CLASS_DATA (e->symtree->n.sym)->attr;
8009 : 33 : attr.pointer = attr.class_pointer;
8010 : : }
8011 : : else
8012 : 821 : attr = gfc_expr_attr (e);
8013 : :
8014 : 854 : if (attr.allocatable)
8015 : 100 : msg = xasprintf ("Allocatable argument '%s' is not allocated",
8016 : 100 : e->symtree->n.sym->name);
8017 : 754 : else if (attr.pointer)
8018 : 46 : msg = xasprintf ("Pointer argument '%s' is not associated",
8019 : 46 : e->symtree->n.sym->name);
8020 : : else
8021 : 708 : goto end_arg_check;
8022 : :
8023 : 146 : if (sym)
8024 : : {
8025 : 0 : temp = gfc_class_data_get (sym->backend_decl);
8026 : 0 : temp = gfc_conv_descriptor_data_get (temp);
8027 : : }
8028 : : else
8029 : : {
8030 : 146 : argse.descriptor_only = 1;
8031 : 146 : gfc_conv_expr_descriptor (&argse, actual->expr);
8032 : 146 : temp = gfc_conv_descriptor_data_get (argse.expr);
8033 : : }
8034 : :
8035 : 146 : cond = fold_build2_loc (input_location, EQ_EXPR,
8036 : : logical_type_node, temp,
8037 : 146 : fold_convert (TREE_TYPE (temp),
8038 : : null_pointer_node));
8039 : 146 : gfc_trans_runtime_check (true, false, cond, &argse.pre, &e->where, msg);
8040 : :
8041 : 146 : free (msg);
8042 : : }
8043 : 13707 : end_arg_check:
8044 : :
8045 : 14561 : argse.data_not_needed = 1;
8046 : 14561 : if (gfc_is_class_array_function (e))
8047 : : {
8048 : : /* For functions that return a class array conv_expr_descriptor is not
8049 : : able to get the descriptor right. Therefore this special case. */
8050 : 6 : gfc_conv_expr_reference (&argse, e);
8051 : 6 : argse.expr = gfc_class_data_get (argse.expr);
8052 : : }
8053 : 14555 : else if (sym && sym->backend_decl)
8054 : : {
8055 : 14 : gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (sym->backend_decl)));
8056 : 14 : argse.expr = gfc_class_data_get (sym->backend_decl);
8057 : : }
8058 : : else
8059 : 14541 : gfc_conv_expr_descriptor (&argse, actual->expr);
8060 : 14561 : gfc_add_block_to_block (&se->pre, &argse.pre);
8061 : 14561 : gfc_add_block_to_block (&se->post, &argse.post);
8062 : 14561 : arg1 = argse.expr;
8063 : :
8064 : 14561 : actual = actual->next;
8065 : 14561 : if (actual->expr)
8066 : : {
8067 : 8632 : stmtblock_t block;
8068 : 8632 : gfc_init_block (&block);
8069 : 8632 : gfc_init_se (&argse, NULL);
8070 : 8632 : gfc_conv_expr_type (&argse, actual->expr,
8071 : : gfc_array_index_type);
8072 : 8632 : gfc_add_block_to_block (&block, &argse.pre);
8073 : 8632 : tree tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8074 : : argse.expr, gfc_index_one_node);
8075 : 8632 : size = gfc_tree_array_size (&block, arg1, e, tmp);
8076 : :
8077 : : /* Unusually, for an intrinsic, size does not exclude
8078 : : an optional arg2, so we must test for it. */
8079 : 8632 : if (actual->expr->expr_type == EXPR_VARIABLE
8080 : 2019 : && actual->expr->symtree->n.sym->attr.dummy
8081 : 31 : && actual->expr->symtree->n.sym->attr.optional)
8082 : : {
8083 : 31 : tree cond;
8084 : 31 : stmtblock_t block2;
8085 : 31 : gfc_init_block (&block2);
8086 : 31 : gfc_init_se (&argse, NULL);
8087 : 31 : argse.want_pointer = 1;
8088 : 31 : argse.data_not_needed = 1;
8089 : 31 : gfc_conv_expr (&argse, actual->expr);
8090 : 31 : gfc_add_block_to_block (&se->pre, &argse.pre);
8091 : : /* 'block2' contains the arg2 absent case, 'block' the arg2 present
8092 : : case; size_var can be used in both blocks. */
8093 : 31 : tree size_var = gfc_create_var (TREE_TYPE (size), "size");
8094 : 31 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8095 : 31 : TREE_TYPE (size_var), size_var, size);
8096 : 31 : gfc_add_expr_to_block (&block, tmp);
8097 : 31 : size = gfc_tree_array_size (&block2, arg1, e, NULL_TREE);
8098 : 31 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8099 : 31 : TREE_TYPE (size_var), size_var, size);
8100 : 31 : gfc_add_expr_to_block (&block2, tmp);
8101 : 31 : cond = gfc_conv_expr_present (actual->expr->symtree->n.sym);
8102 : 31 : tmp = build3_v (COND_EXPR, cond, gfc_finish_block (&block),
8103 : : gfc_finish_block (&block2));
8104 : 31 : gfc_add_expr_to_block (&se->pre, tmp);
8105 : 31 : size = size_var;
8106 : 31 : }
8107 : : else
8108 : 8601 : gfc_add_block_to_block (&se->pre, &block);
8109 : : }
8110 : : else
8111 : 5929 : size = gfc_tree_array_size (&se->pre, arg1, e, NULL_TREE);
8112 : 14561 : type = gfc_typenode_for_spec (&expr->ts);
8113 : 14561 : se->expr = convert (type, size);
8114 : 14561 : }
8115 : :
8116 : :
8117 : : /* Helper function to compute the size of a character variable,
8118 : : excluding the terminating null characters. The result has
8119 : : gfc_array_index_type type. */
8120 : :
8121 : : tree
8122 : 1778 : size_of_string_in_bytes (int kind, tree string_length)
8123 : : {
8124 : 1778 : tree bytesize;
8125 : 1778 : int i = gfc_validate_kind (BT_CHARACTER, kind, false);
8126 : :
8127 : 3556 : bytesize = build_int_cst (gfc_array_index_type,
8128 : 1778 : gfc_character_kinds[i].bit_size / 8);
8129 : :
8130 : 1778 : return fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8131 : : bytesize,
8132 : 1778 : fold_convert (gfc_array_index_type, string_length));
8133 : : }
8134 : :
8135 : :
8136 : : static void
8137 : 1296 : gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr)
8138 : : {
8139 : 1296 : gfc_expr *arg;
8140 : 1296 : gfc_se argse;
8141 : 1296 : tree source_bytes;
8142 : 1296 : tree tmp;
8143 : 1296 : tree lower;
8144 : 1296 : tree upper;
8145 : 1296 : tree byte_size;
8146 : 1296 : tree field;
8147 : 1296 : int n;
8148 : :
8149 : 1296 : gfc_init_se (&argse, NULL);
8150 : 1296 : arg = expr->value.function.actual->expr;
8151 : :
8152 : 1296 : if (arg->rank || arg->ts.type == BT_ASSUMED)
8153 : 1004 : gfc_conv_expr_descriptor (&argse, arg);
8154 : : else
8155 : 292 : gfc_conv_expr_reference (&argse, arg);
8156 : :
8157 : 1296 : if (arg->ts.type == BT_ASSUMED)
8158 : : {
8159 : : /* This only works if an array descriptor has been passed; thus, extract
8160 : : the size from the descriptor. */
8161 : 164 : gcc_assert (TYPE_PRECISION (gfc_array_index_type)
8162 : : == TYPE_PRECISION (size_type_node));
8163 : 164 : tmp = arg->symtree->n.sym->backend_decl;
8164 : 164 : tmp = DECL_LANG_SPECIFIC (tmp)
8165 : 60 : && GFC_DECL_SAVED_DESCRIPTOR (tmp) != NULL_TREE
8166 : 218 : ? GFC_DECL_SAVED_DESCRIPTOR (tmp) : tmp;
8167 : 164 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
8168 : 164 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8169 : :
8170 : 164 : tmp = gfc_conv_descriptor_dtype (tmp);
8171 : 164 : field = gfc_advance_chain (TYPE_FIELDS (get_dtype_type_node ()),
8172 : : GFC_DTYPE_ELEM_LEN);
8173 : 164 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
8174 : : tmp, field, NULL_TREE);
8175 : :
8176 : 164 : byte_size = fold_convert (gfc_array_index_type, tmp);
8177 : : }
8178 : 1132 : else if (arg->ts.type == BT_CLASS)
8179 : : {
8180 : : /* Conv_expr_descriptor returns a component_ref to _data component of the
8181 : : class object. The class object may be a non-pointer object, e.g.
8182 : : located on the stack, or a memory location pointed to, e.g. a
8183 : : parameter, i.e., an indirect_ref. */
8184 : 954 : if (POINTER_TYPE_P (TREE_TYPE (argse.expr))
8185 : 584 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (argse.expr))))
8186 : 198 : byte_size
8187 : 198 : = gfc_class_vtab_size_get (build_fold_indirect_ref (argse.expr));
8188 : 386 : else if (GFC_CLASS_TYPE_P (TREE_TYPE (argse.expr)))
8189 : 0 : byte_size = gfc_class_vtab_size_get (argse.expr);
8190 : 386 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (argse.expr))
8191 : 386 : && TREE_CODE (argse.expr) == COMPONENT_REF)
8192 : 328 : byte_size = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8193 : 58 : else if (arg->rank > 0
8194 : 16 : || (arg->rank == 0
8195 : 16 : && arg->ref && arg->ref->type == REF_COMPONENT))
8196 : : {
8197 : : /* The scalarizer added an additional temp. To get the class' vptr
8198 : : one has to look at the original backend_decl. */
8199 : 58 : if (argse.class_container)
8200 : 16 : byte_size = gfc_class_vtab_size_get (argse.class_container);
8201 : 42 : else if (DECL_LANG_SPECIFIC (arg->symtree->n.sym->backend_decl))
8202 : 84 : byte_size = gfc_class_vtab_size_get (
8203 : 42 : GFC_DECL_SAVED_DESCRIPTOR (arg->symtree->n.sym->backend_decl));
8204 : : else
8205 : 0 : gcc_unreachable ();
8206 : : }
8207 : : else
8208 : 0 : gcc_unreachable ();
8209 : : }
8210 : : else
8211 : : {
8212 : 548 : if (arg->ts.type == BT_CHARACTER)
8213 : 84 : byte_size = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8214 : : else
8215 : : {
8216 : 464 : if (arg->rank == 0)
8217 : 0 : byte_size = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8218 : : argse.expr));
8219 : : else
8220 : 464 : byte_size = gfc_get_element_type (TREE_TYPE (argse.expr));
8221 : 464 : byte_size = fold_convert (gfc_array_index_type,
8222 : : size_in_bytes (byte_size));
8223 : : }
8224 : : }
8225 : :
8226 : 1296 : if (arg->rank == 0)
8227 : 292 : se->expr = byte_size;
8228 : : else
8229 : : {
8230 : 1004 : source_bytes = gfc_create_var (gfc_array_index_type, "bytes");
8231 : 1004 : gfc_add_modify (&argse.pre, source_bytes, byte_size);
8232 : :
8233 : 1004 : if (arg->rank == -1)
8234 : : {
8235 : 357 : tree cond, loop_var, exit_label;
8236 : 357 : stmtblock_t body;
8237 : :
8238 : 357 : tmp = fold_convert (gfc_array_index_type,
8239 : : gfc_conv_descriptor_rank (argse.expr));
8240 : 357 : loop_var = gfc_create_var (gfc_array_index_type, "i");
8241 : 357 : gfc_add_modify (&argse.pre, loop_var, gfc_index_zero_node);
8242 : 357 : exit_label = gfc_build_label_decl (NULL_TREE);
8243 : :
8244 : : /* Create loop:
8245 : : for (;;)
8246 : : {
8247 : : if (i >= rank)
8248 : : goto exit;
8249 : : source_bytes = source_bytes * array.dim[i].extent;
8250 : : i = i + 1;
8251 : : }
8252 : : exit: */
8253 : 357 : gfc_start_block (&body);
8254 : 357 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
8255 : : loop_var, tmp);
8256 : 357 : tmp = build1_v (GOTO_EXPR, exit_label);
8257 : 357 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
8258 : : cond, tmp, build_empty_stmt (input_location));
8259 : 357 : gfc_add_expr_to_block (&body, tmp);
8260 : :
8261 : 357 : lower = gfc_conv_descriptor_lbound_get (argse.expr, loop_var);
8262 : 357 : upper = gfc_conv_descriptor_ubound_get (argse.expr, loop_var);
8263 : 357 : tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8264 : 357 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8265 : : gfc_array_index_type, tmp, source_bytes);
8266 : 357 : gfc_add_modify (&body, source_bytes, tmp);
8267 : :
8268 : 357 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
8269 : : gfc_array_index_type, loop_var,
8270 : : gfc_index_one_node);
8271 : 357 : gfc_add_modify_loc (input_location, &body, loop_var, tmp);
8272 : :
8273 : 357 : tmp = gfc_finish_block (&body);
8274 : :
8275 : 357 : tmp = fold_build1_loc (input_location, LOOP_EXPR, void_type_node,
8276 : : tmp);
8277 : 357 : gfc_add_expr_to_block (&argse.pre, tmp);
8278 : :
8279 : 357 : tmp = build1_v (LABEL_EXPR, exit_label);
8280 : 357 : gfc_add_expr_to_block (&argse.pre, tmp);
8281 : : }
8282 : : else
8283 : : {
8284 : : /* Obtain the size of the array in bytes. */
8285 : 1834 : for (n = 0; n < arg->rank; n++)
8286 : : {
8287 : 1187 : tree idx;
8288 : 1187 : idx = gfc_rank_cst[n];
8289 : 1187 : lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8290 : 1187 : upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8291 : 1187 : tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8292 : 1187 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8293 : : gfc_array_index_type, tmp, source_bytes);
8294 : 1187 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8295 : : }
8296 : : }
8297 : 1004 : se->expr = source_bytes;
8298 : : }
8299 : :
8300 : 1296 : gfc_add_block_to_block (&se->pre, &argse.pre);
8301 : 1296 : }
8302 : :
8303 : :
8304 : : static void
8305 : 802 : gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
8306 : : {
8307 : 802 : gfc_expr *arg;
8308 : 802 : gfc_se argse;
8309 : 802 : tree type, result_type, tmp, class_decl = NULL;
8310 : 802 : gfc_symbol *sym;
8311 : 802 : bool unlimited = false;
8312 : :
8313 : 802 : arg = expr->value.function.actual->expr;
8314 : :
8315 : 802 : gfc_init_se (&argse, NULL);
8316 : 802 : result_type = gfc_get_int_type (expr->ts.kind);
8317 : :
8318 : 802 : if (arg->rank == 0)
8319 : : {
8320 : 211 : if (arg->ts.type == BT_CLASS)
8321 : : {
8322 : 86 : unlimited = UNLIMITED_POLY (arg);
8323 : 86 : gfc_add_vptr_component (arg);
8324 : 86 : gfc_add_size_component (arg);
8325 : 86 : gfc_conv_expr (&argse, arg);
8326 : 86 : tmp = fold_convert (result_type, argse.expr);
8327 : 86 : class_decl = gfc_get_class_from_expr (argse.expr);
8328 : 86 : goto done;
8329 : : }
8330 : :
8331 : 125 : gfc_conv_expr_reference (&argse, arg);
8332 : 125 : type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8333 : : argse.expr));
8334 : : }
8335 : : else
8336 : : {
8337 : 591 : argse.want_pointer = 0;
8338 : 591 : gfc_conv_expr_descriptor (&argse, arg);
8339 : 591 : sym = arg->expr_type == EXPR_VARIABLE ? arg->symtree->n.sym : NULL;
8340 : 591 : if (arg->ts.type == BT_CLASS)
8341 : : {
8342 : 60 : unlimited = UNLIMITED_POLY (arg);
8343 : 60 : if (TREE_CODE (argse.expr) == COMPONENT_REF)
8344 : 54 : tmp = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8345 : 6 : else if (arg->rank > 0 && sym
8346 : 12 : && DECL_LANG_SPECIFIC (sym->backend_decl))
8347 : 12 : tmp = gfc_class_vtab_size_get (
8348 : 6 : GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl));
8349 : : else
8350 : 0 : gcc_unreachable ();
8351 : 60 : tmp = fold_convert (result_type, tmp);
8352 : 60 : class_decl = gfc_get_class_from_expr (argse.expr);
8353 : 60 : goto done;
8354 : : }
8355 : 531 : type = gfc_get_element_type (TREE_TYPE (argse.expr));
8356 : : }
8357 : :
8358 : : /* Obtain the argument's word length. */
8359 : 656 : if (arg->ts.type == BT_CHARACTER)
8360 : 241 : tmp = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8361 : : else
8362 : 415 : tmp = size_in_bytes (type);
8363 : 656 : tmp = fold_convert (result_type, tmp);
8364 : :
8365 : 802 : done:
8366 : 802 : if (unlimited && class_decl)
8367 : 68 : tmp = gfc_resize_class_size_with_len (NULL, class_decl, tmp);
8368 : :
8369 : 802 : se->expr = fold_build2_loc (input_location, MULT_EXPR, result_type, tmp,
8370 : : build_int_cst (result_type, BITS_PER_UNIT));
8371 : 802 : gfc_add_block_to_block (&se->pre, &argse.pre);
8372 : 802 : }
8373 : :
8374 : :
8375 : : /* Intrinsic string comparison functions. */
8376 : :
8377 : : static void
8378 : 99 : gfc_conv_intrinsic_strcmp (gfc_se * se, gfc_expr * expr, enum tree_code op)
8379 : : {
8380 : 99 : tree args[4];
8381 : :
8382 : 99 : gfc_conv_intrinsic_function_args (se, expr, args, 4);
8383 : :
8384 : 99 : se->expr
8385 : 198 : = gfc_build_compare_string (args[0], args[1], args[2], args[3],
8386 : 99 : expr->value.function.actual->expr->ts.kind,
8387 : : op);
8388 : 99 : se->expr = fold_build2_loc (input_location, op,
8389 : : gfc_typenode_for_spec (&expr->ts), se->expr,
8390 : 99 : build_int_cst (TREE_TYPE (se->expr), 0));
8391 : 99 : }
8392 : :
8393 : : /* Generate a call to the adjustl/adjustr library function. */
8394 : : static void
8395 : 462 : gfc_conv_intrinsic_adjust (gfc_se * se, gfc_expr * expr, tree fndecl)
8396 : : {
8397 : 462 : tree args[3];
8398 : 462 : tree len;
8399 : 462 : tree type;
8400 : 462 : tree var;
8401 : 462 : tree tmp;
8402 : :
8403 : 462 : gfc_conv_intrinsic_function_args (se, expr, &args[1], 2);
8404 : 462 : len = args[1];
8405 : :
8406 : 462 : type = TREE_TYPE (args[2]);
8407 : 462 : var = gfc_conv_string_tmp (se, type, len);
8408 : 462 : args[0] = var;
8409 : :
8410 : 462 : tmp = build_call_expr_loc (input_location,
8411 : : fndecl, 3, args[0], args[1], args[2]);
8412 : 462 : gfc_add_expr_to_block (&se->pre, tmp);
8413 : 462 : se->expr = var;
8414 : 462 : se->string_length = len;
8415 : 462 : }
8416 : :
8417 : :
8418 : : /* Generate code for the TRANSFER intrinsic:
8419 : : For scalar results:
8420 : : DEST = TRANSFER (SOURCE, MOLD)
8421 : : where:
8422 : : typeof<DEST> = typeof<MOLD>
8423 : : and:
8424 : : MOLD is scalar.
8425 : :
8426 : : For array results:
8427 : : DEST(1:N) = TRANSFER (SOURCE, MOLD[, SIZE])
8428 : : where:
8429 : : typeof<DEST> = typeof<MOLD>
8430 : : and:
8431 : : N = min (sizeof (SOURCE(:)), sizeof (DEST(:)),
8432 : : sizeof (DEST(0) * SIZE). */
8433 : : static void
8434 : 3286 : gfc_conv_intrinsic_transfer (gfc_se * se, gfc_expr * expr)
8435 : : {
8436 : 3286 : tree tmp;
8437 : 3286 : tree tmpdecl;
8438 : 3286 : tree ptr;
8439 : 3286 : tree extent;
8440 : 3286 : tree source;
8441 : 3286 : tree source_type;
8442 : 3286 : tree source_bytes;
8443 : 3286 : tree mold_type;
8444 : 3286 : tree dest_word_len;
8445 : 3286 : tree size_words;
8446 : 3286 : tree size_bytes;
8447 : 3286 : tree upper;
8448 : 3286 : tree lower;
8449 : 3286 : tree stmt;
8450 : 3286 : tree class_ref = NULL_TREE;
8451 : 3286 : gfc_actual_arglist *arg;
8452 : 3286 : gfc_se argse;
8453 : 3286 : gfc_array_info *info;
8454 : 3286 : stmtblock_t block;
8455 : 3286 : int n;
8456 : 3286 : bool scalar_mold;
8457 : 3286 : gfc_expr *source_expr, *mold_expr, *class_expr;
8458 : :
8459 : 3286 : info = NULL;
8460 : 3286 : if (se->loop)
8461 : 438 : info = &se->ss->info->data.array;
8462 : :
8463 : : /* Convert SOURCE. The output from this stage is:-
8464 : : source_bytes = length of the source in bytes
8465 : : source = pointer to the source data. */
8466 : 3286 : arg = expr->value.function.actual;
8467 : 3286 : source_expr = arg->expr;
8468 : :
8469 : : /* Ensure double transfer through LOGICAL preserves all
8470 : : the needed bits. */
8471 : 3286 : if (arg->expr->expr_type == EXPR_FUNCTION
8472 : 2337 : && arg->expr->value.function.esym == NULL
8473 : 2319 : && arg->expr->value.function.isym != NULL
8474 : 2319 : && arg->expr->value.function.isym->id == GFC_ISYM_TRANSFER
8475 : 12 : && arg->expr->ts.type == BT_LOGICAL
8476 : 12 : && expr->ts.type != arg->expr->ts.type)
8477 : 12 : arg->expr->value.function.name = "__transfer_in_transfer";
8478 : :
8479 : 3286 : gfc_init_se (&argse, NULL);
8480 : :
8481 : 3286 : source_bytes = gfc_create_var (gfc_array_index_type, NULL);
8482 : :
8483 : : /* Obtain the pointer to source and the length of source in bytes. */
8484 : 3286 : if (arg->expr->rank == 0)
8485 : : {
8486 : 2964 : gfc_conv_expr_reference (&argse, arg->expr);
8487 : 2964 : if (arg->expr->ts.type == BT_CLASS)
8488 : : {
8489 : 31 : tmp = build_fold_indirect_ref_loc (input_location, argse.expr);
8490 : 31 : if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
8491 : : {
8492 : 19 : source = gfc_class_data_get (tmp);
8493 : 19 : class_ref = tmp;
8494 : : }
8495 : : else
8496 : : {
8497 : : /* Array elements are evaluated as a reference to the data.
8498 : : To obtain the vptr for the element size, the argument
8499 : : expression must be stripped to the class reference and
8500 : : re-evaluated. The pre and post blocks are not needed. */
8501 : 12 : gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
8502 : 12 : source = argse.expr;
8503 : 12 : class_expr = gfc_find_and_cut_at_last_class_ref (arg->expr);
8504 : 12 : gfc_init_se (&argse, NULL);
8505 : 12 : gfc_conv_expr (&argse, class_expr);
8506 : 12 : class_ref = argse.expr;
8507 : : }
8508 : : }
8509 : : else
8510 : 2933 : source = argse.expr;
8511 : :
8512 : : /* Obtain the source word length. */
8513 : 2964 : switch (arg->expr->ts.type)
8514 : : {
8515 : 294 : case BT_CHARACTER:
8516 : 294 : tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8517 : : argse.string_length);
8518 : 294 : break;
8519 : 31 : case BT_CLASS:
8520 : 31 : if (class_ref != NULL_TREE)
8521 : : {
8522 : 31 : tmp = gfc_class_vtab_size_get (class_ref);
8523 : 31 : if (UNLIMITED_POLY (source_expr))
8524 : 24 : tmp = gfc_resize_class_size_with_len (NULL, class_ref, tmp);
8525 : : }
8526 : : else
8527 : : {
8528 : 0 : tmp = gfc_class_vtab_size_get (argse.expr);
8529 : 0 : if (UNLIMITED_POLY (source_expr))
8530 : 0 : tmp = gfc_resize_class_size_with_len (NULL, argse.expr, tmp);
8531 : : }
8532 : : break;
8533 : 2639 : default:
8534 : 2639 : source_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8535 : : source));
8536 : 2639 : tmp = fold_convert (gfc_array_index_type,
8537 : : size_in_bytes (source_type));
8538 : 2639 : break;
8539 : : }
8540 : : }
8541 : : else
8542 : : {
8543 : 322 : argse.want_pointer = 0;
8544 : 322 : gfc_conv_expr_descriptor (&argse, arg->expr);
8545 : 322 : source = gfc_conv_descriptor_data_get (argse.expr);
8546 : 322 : source_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8547 : :
8548 : : /* Repack the source if not simply contiguous. */
8549 : 322 : if (!gfc_is_simply_contiguous (arg->expr, false, true))
8550 : : {
8551 : 62 : tmp = gfc_build_addr_expr (NULL_TREE, argse.expr);
8552 : :
8553 : 62 : if (warn_array_temporaries)
8554 : 0 : gfc_warning (OPT_Warray_temporaries,
8555 : : "Creating array temporary at %L", &expr->where);
8556 : :
8557 : 62 : source = build_call_expr_loc (input_location,
8558 : : gfor_fndecl_in_pack, 1, tmp);
8559 : 62 : source = gfc_evaluate_now (source, &argse.pre);
8560 : :
8561 : : /* Free the temporary. */
8562 : 62 : gfc_start_block (&block);
8563 : 62 : tmp = gfc_call_free (source);
8564 : 62 : gfc_add_expr_to_block (&block, tmp);
8565 : 62 : stmt = gfc_finish_block (&block);
8566 : :
8567 : : /* Clean up if it was repacked. */
8568 : 62 : gfc_init_block (&block);
8569 : 62 : tmp = gfc_conv_array_data (argse.expr);
8570 : 62 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
8571 : : source, tmp);
8572 : 62 : tmp = build3_v (COND_EXPR, tmp, stmt,
8573 : : build_empty_stmt (input_location));
8574 : 62 : gfc_add_expr_to_block (&block, tmp);
8575 : 62 : gfc_add_block_to_block (&block, &se->post);
8576 : 62 : gfc_init_block (&se->post);
8577 : 62 : gfc_add_block_to_block (&se->post, &block);
8578 : : }
8579 : :
8580 : : /* Obtain the source word length. */
8581 : 322 : if (arg->expr->ts.type == BT_CHARACTER)
8582 : 144 : tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8583 : : argse.string_length);
8584 : 178 : else if (arg->expr->ts.type == BT_CLASS)
8585 : : {
8586 : 36 : class_ref = TREE_OPERAND (argse.expr, 0);
8587 : 36 : tmp = gfc_class_vtab_size_get (class_ref);
8588 : 36 : if (UNLIMITED_POLY (arg->expr))
8589 : 36 : tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8590 : : }
8591 : : else
8592 : 142 : tmp = fold_convert (gfc_array_index_type,
8593 : : size_in_bytes (source_type));
8594 : :
8595 : : /* Obtain the size of the array in bytes. */
8596 : 322 : extent = gfc_create_var (gfc_array_index_type, NULL);
8597 : 674 : for (n = 0; n < arg->expr->rank; n++)
8598 : : {
8599 : 352 : tree idx;
8600 : 352 : idx = gfc_rank_cst[n];
8601 : 352 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8602 : 352 : lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8603 : 352 : upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8604 : 352 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
8605 : : gfc_array_index_type, upper, lower);
8606 : 352 : gfc_add_modify (&argse.pre, extent, tmp);
8607 : 352 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
8608 : : gfc_array_index_type, extent,
8609 : : gfc_index_one_node);
8610 : 352 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8611 : : gfc_array_index_type, tmp, source_bytes);
8612 : : }
8613 : : }
8614 : :
8615 : 3286 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8616 : 3286 : gfc_add_block_to_block (&se->pre, &argse.pre);
8617 : 3286 : gfc_add_block_to_block (&se->post, &argse.post);
8618 : :
8619 : : /* Now convert MOLD. The outputs are:
8620 : : mold_type = the TREE type of MOLD
8621 : : dest_word_len = destination word length in bytes. */
8622 : 3286 : arg = arg->next;
8623 : 3286 : mold_expr = arg->expr;
8624 : :
8625 : 3286 : gfc_init_se (&argse, NULL);
8626 : :
8627 : 3286 : scalar_mold = arg->expr->rank == 0;
8628 : :
8629 : 3286 : if (arg->expr->rank == 0)
8630 : : {
8631 : 2997 : gfc_conv_expr_reference (&argse, mold_expr);
8632 : 2997 : mold_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8633 : : argse.expr));
8634 : : }
8635 : : else
8636 : : {
8637 : 289 : argse.want_pointer = 0;
8638 : 289 : gfc_conv_expr_descriptor (&argse, mold_expr);
8639 : 289 : mold_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8640 : : }
8641 : :
8642 : 3286 : gfc_add_block_to_block (&se->pre, &argse.pre);
8643 : 3286 : gfc_add_block_to_block (&se->post, &argse.post);
8644 : :
8645 : 3286 : if (strcmp (expr->value.function.name, "__transfer_in_transfer") == 0)
8646 : : {
8647 : : /* If this TRANSFER is nested in another TRANSFER, use a type
8648 : : that preserves all bits. */
8649 : 12 : if (mold_expr->ts.type == BT_LOGICAL)
8650 : 12 : mold_type = gfc_get_int_type (mold_expr->ts.kind);
8651 : : }
8652 : :
8653 : : /* Obtain the destination word length. */
8654 : 3286 : switch (mold_expr->ts.type)
8655 : : {
8656 : 467 : case BT_CHARACTER:
8657 : 467 : tmp = size_of_string_in_bytes (mold_expr->ts.kind, argse.string_length);
8658 : 467 : mold_type = gfc_get_character_type_len (mold_expr->ts.kind,
8659 : : argse.string_length);
8660 : 467 : break;
8661 : 6 : case BT_CLASS:
8662 : 6 : if (scalar_mold)
8663 : 6 : class_ref = argse.expr;
8664 : : else
8665 : 0 : class_ref = TREE_OPERAND (argse.expr, 0);
8666 : 6 : tmp = gfc_class_vtab_size_get (class_ref);
8667 : 6 : if (UNLIMITED_POLY (arg->expr))
8668 : 0 : tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8669 : : break;
8670 : 2813 : default:
8671 : 2813 : tmp = fold_convert (gfc_array_index_type, size_in_bytes (mold_type));
8672 : 2813 : break;
8673 : : }
8674 : :
8675 : : /* Do not fix dest_word_len if it is a variable, since the temporary can wind
8676 : : up being used before the assignment. */
8677 : 3286 : if (mold_expr->ts.type == BT_CHARACTER && mold_expr->ts.deferred)
8678 : : dest_word_len = tmp;
8679 : : else
8680 : : {
8681 : 3232 : dest_word_len = gfc_create_var (gfc_array_index_type, NULL);
8682 : 3232 : gfc_add_modify (&se->pre, dest_word_len, tmp);
8683 : : }
8684 : :
8685 : : /* Finally convert SIZE, if it is present. */
8686 : 3286 : arg = arg->next;
8687 : 3286 : size_words = gfc_create_var (gfc_array_index_type, NULL);
8688 : :
8689 : 3286 : if (arg->expr)
8690 : : {
8691 : 222 : gfc_init_se (&argse, NULL);
8692 : 222 : gfc_conv_expr_reference (&argse, arg->expr);
8693 : 222 : tmp = convert (gfc_array_index_type,
8694 : : build_fold_indirect_ref_loc (input_location,
8695 : : argse.expr));
8696 : 222 : gfc_add_block_to_block (&se->pre, &argse.pre);
8697 : 222 : gfc_add_block_to_block (&se->post, &argse.post);
8698 : : }
8699 : : else
8700 : : tmp = NULL_TREE;
8701 : :
8702 : : /* Separate array and scalar results. */
8703 : 3286 : if (scalar_mold && tmp == NULL_TREE)
8704 : 2848 : goto scalar_transfer;
8705 : :
8706 : 438 : size_bytes = gfc_create_var (gfc_array_index_type, NULL);
8707 : 438 : if (tmp != NULL_TREE)
8708 : 222 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8709 : : tmp, dest_word_len);
8710 : : else
8711 : : tmp = source_bytes;
8712 : :
8713 : 438 : gfc_add_modify (&se->pre, size_bytes, tmp);
8714 : 438 : gfc_add_modify (&se->pre, size_words,
8715 : : fold_build2_loc (input_location, CEIL_DIV_EXPR,
8716 : : gfc_array_index_type,
8717 : : size_bytes, dest_word_len));
8718 : :
8719 : : /* Evaluate the bounds of the result. If the loop range exists, we have
8720 : : to check if it is too large. If so, we modify loop->to be consistent
8721 : : with min(size, size(source)). Otherwise, size is made consistent with
8722 : : the loop range, so that the right number of bytes is transferred.*/
8723 : 438 : n = se->loop->order[0];
8724 : 438 : if (se->loop->to[n] != NULL_TREE)
8725 : : {
8726 : 201 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8727 : : se->loop->to[n], se->loop->from[n]);
8728 : 201 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8729 : : tmp, gfc_index_one_node);
8730 : 201 : tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
8731 : : tmp, size_words);
8732 : 201 : gfc_add_modify (&se->pre, size_words, tmp);
8733 : 201 : gfc_add_modify (&se->pre, size_bytes,
8734 : : fold_build2_loc (input_location, MULT_EXPR,
8735 : : gfc_array_index_type,
8736 : : size_words, dest_word_len));
8737 : 402 : upper = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8738 : 201 : size_words, se->loop->from[n]);
8739 : 201 : upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8740 : : upper, gfc_index_one_node);
8741 : : }
8742 : : else
8743 : : {
8744 : 237 : upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8745 : : size_words, gfc_index_one_node);
8746 : 237 : se->loop->from[n] = gfc_index_zero_node;
8747 : : }
8748 : :
8749 : 438 : se->loop->to[n] = upper;
8750 : :
8751 : : /* Build a destination descriptor, using the pointer, source, as the
8752 : : data field. */
8753 : 438 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss, mold_type,
8754 : : NULL_TREE, false, true, false, &expr->where);
8755 : :
8756 : : /* Cast the pointer to the result. */
8757 : 438 : tmp = gfc_conv_descriptor_data_get (info->descriptor);
8758 : 438 : tmp = fold_convert (pvoid_type_node, tmp);
8759 : :
8760 : : /* Use memcpy to do the transfer. */
8761 : 438 : tmp
8762 : 438 : = build_call_expr_loc (input_location,
8763 : : builtin_decl_explicit (BUILT_IN_MEMCPY), 3, tmp,
8764 : : fold_convert (pvoid_type_node, source),
8765 : : fold_convert (size_type_node,
8766 : : fold_build2_loc (input_location,
8767 : : MIN_EXPR,
8768 : : gfc_array_index_type,
8769 : : size_bytes,
8770 : : source_bytes)));
8771 : 438 : gfc_add_expr_to_block (&se->pre, tmp);
8772 : :
8773 : 438 : se->expr = info->descriptor;
8774 : 438 : if (expr->ts.type == BT_CHARACTER)
8775 : : {
8776 : 275 : tmp = fold_convert (gfc_charlen_type_node,
8777 : : TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
8778 : 275 : se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
8779 : : gfc_charlen_type_node,
8780 : : dest_word_len, tmp);
8781 : : }
8782 : :
8783 : 438 : return;
8784 : :
8785 : : /* Deal with scalar results. */
8786 : 2848 : scalar_transfer:
8787 : 2848 : extent = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
8788 : : dest_word_len, source_bytes);
8789 : 2848 : extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
8790 : : extent, gfc_index_zero_node);
8791 : :
8792 : 2848 : if (expr->ts.type == BT_CHARACTER)
8793 : : {
8794 : 192 : tree direct, indirect, free;
8795 : :
8796 : 192 : ptr = convert (gfc_get_pchar_type (expr->ts.kind), source);
8797 : 192 : tmpdecl = gfc_create_var (gfc_get_pchar_type (expr->ts.kind),
8798 : : "transfer");
8799 : :
8800 : : /* If source is longer than the destination, use a pointer to
8801 : : the source directly. */
8802 : 192 : gfc_init_block (&block);
8803 : 192 : gfc_add_modify (&block, tmpdecl, ptr);
8804 : 192 : direct = gfc_finish_block (&block);
8805 : :
8806 : : /* Otherwise, allocate a string with the length of the destination
8807 : : and copy the source into it. */
8808 : 192 : gfc_init_block (&block);
8809 : 192 : tmp = gfc_get_pchar_type (expr->ts.kind);
8810 : 192 : tmp = gfc_call_malloc (&block, tmp, dest_word_len);
8811 : 192 : gfc_add_modify (&block, tmpdecl,
8812 : 192 : fold_convert (TREE_TYPE (ptr), tmp));
8813 : 192 : tmp = build_call_expr_loc (input_location,
8814 : : builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
8815 : : fold_convert (pvoid_type_node, tmpdecl),
8816 : : fold_convert (pvoid_type_node, ptr),
8817 : : fold_convert (size_type_node, extent));
8818 : 192 : gfc_add_expr_to_block (&block, tmp);
8819 : 192 : indirect = gfc_finish_block (&block);
8820 : :
8821 : : /* Wrap it up with the condition. */
8822 : 192 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
8823 : : dest_word_len, source_bytes);
8824 : 192 : tmp = build3_v (COND_EXPR, tmp, direct, indirect);
8825 : 192 : gfc_add_expr_to_block (&se->pre, tmp);
8826 : :
8827 : : /* Free the temporary string, if necessary. */
8828 : 192 : free = gfc_call_free (tmpdecl);
8829 : 192 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
8830 : : dest_word_len, source_bytes);
8831 : 192 : tmp = build3_v (COND_EXPR, tmp, free, build_empty_stmt (input_location));
8832 : 192 : gfc_add_expr_to_block (&se->post, tmp);
8833 : :
8834 : 192 : se->expr = tmpdecl;
8835 : 192 : tmp = fold_convert (gfc_charlen_type_node,
8836 : : TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
8837 : 192 : se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
8838 : : gfc_charlen_type_node,
8839 : : dest_word_len, tmp);
8840 : : }
8841 : : else
8842 : : {
8843 : 2656 : tmpdecl = gfc_create_var (mold_type, "transfer");
8844 : :
8845 : 2656 : ptr = convert (build_pointer_type (mold_type), source);
8846 : :
8847 : : /* For CLASS results, allocate the needed memory first. */
8848 : 2656 : if (mold_expr->ts.type == BT_CLASS)
8849 : : {
8850 : 6 : tree cdata;
8851 : 6 : cdata = gfc_class_data_get (tmpdecl);
8852 : 6 : tmp = gfc_call_malloc (&se->pre, TREE_TYPE (cdata), dest_word_len);
8853 : 6 : gfc_add_modify (&se->pre, cdata, tmp);
8854 : : }
8855 : :
8856 : : /* Use memcpy to do the transfer. */
8857 : 2656 : if (mold_expr->ts.type == BT_CLASS)
8858 : 6 : tmp = gfc_class_data_get (tmpdecl);
8859 : : else
8860 : 2650 : tmp = gfc_build_addr_expr (NULL_TREE, tmpdecl);
8861 : :
8862 : 2656 : tmp = build_call_expr_loc (input_location,
8863 : : builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
8864 : : fold_convert (pvoid_type_node, tmp),
8865 : : fold_convert (pvoid_type_node, ptr),
8866 : : fold_convert (size_type_node, extent));
8867 : 2656 : gfc_add_expr_to_block (&se->pre, tmp);
8868 : :
8869 : : /* For CLASS results, set the _vptr. */
8870 : 2656 : if (mold_expr->ts.type == BT_CLASS)
8871 : 6 : gfc_reset_vptr (&se->pre, nullptr, tmpdecl, source_expr->ts.u.derived);
8872 : :
8873 : 2656 : se->expr = tmpdecl;
8874 : : }
8875 : : }
8876 : :
8877 : :
8878 : : /* Generate code for the ALLOCATED intrinsic.
8879 : : Generate inline code that directly check the address of the argument. */
8880 : :
8881 : : static void
8882 : 6966 : gfc_conv_allocated (gfc_se *se, gfc_expr *expr)
8883 : : {
8884 : 6966 : gfc_se arg1se;
8885 : 6966 : tree tmp;
8886 : 6966 : gfc_expr *e = expr->value.function.actual->expr;
8887 : :
8888 : 6966 : gfc_init_se (&arg1se, NULL);
8889 : 6966 : if (e->ts.type == BT_CLASS)
8890 : : {
8891 : : /* Make sure that class array expressions have both a _data
8892 : : component reference and an array reference.... */
8893 : 881 : if (CLASS_DATA (e)->attr.dimension)
8894 : 418 : gfc_add_class_array_ref (e);
8895 : : /* .... whilst scalars only need the _data component. */
8896 : : else
8897 : 463 : gfc_add_data_component (e);
8898 : : }
8899 : :
8900 : 6966 : gcc_assert (flag_coarray != GFC_FCOARRAY_LIB || !gfc_is_coindexed (e));
8901 : :
8902 : 6966 : if (e->rank == 0)
8903 : : {
8904 : : /* Allocatable scalar. */
8905 : 2772 : arg1se.want_pointer = 1;
8906 : 2772 : gfc_conv_expr (&arg1se, e);
8907 : 2772 : tmp = arg1se.expr;
8908 : : }
8909 : : else
8910 : : {
8911 : : /* Allocatable array. */
8912 : 4194 : arg1se.descriptor_only = 1;
8913 : 4194 : gfc_conv_expr_descriptor (&arg1se, e);
8914 : 4194 : tmp = gfc_conv_descriptor_data_get (arg1se.expr);
8915 : : }
8916 : :
8917 : 6966 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
8918 : 6966 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
8919 : :
8920 : : /* Components of pointer array references sometimes come back with a pre block. */
8921 : 6966 : if (arg1se.pre.head)
8922 : 6 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
8923 : :
8924 : 6966 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
8925 : 6966 : }
8926 : :
8927 : :
8928 : : /* Generate code for the ASSOCIATED intrinsic.
8929 : : If both POINTER and TARGET are arrays, generate a call to library function
8930 : : _gfor_associated, and pass descriptors of POINTER and TARGET to it.
8931 : : In other cases, generate inline code that directly compare the address of
8932 : : POINTER with the address of TARGET. */
8933 : :
8934 : : static void
8935 : 8844 : gfc_conv_associated (gfc_se *se, gfc_expr *expr)
8936 : : {
8937 : 8844 : gfc_actual_arglist *arg1;
8938 : 8844 : gfc_actual_arglist *arg2;
8939 : 8844 : gfc_se arg1se;
8940 : 8844 : gfc_se arg2se;
8941 : 8844 : tree tmp2;
8942 : 8844 : tree tmp;
8943 : 8844 : tree nonzero_arraylen = NULL_TREE;
8944 : 8844 : gfc_ss *ss;
8945 : 8844 : bool scalar;
8946 : :
8947 : 8844 : gfc_init_se (&arg1se, NULL);
8948 : 8844 : gfc_init_se (&arg2se, NULL);
8949 : 8844 : arg1 = expr->value.function.actual;
8950 : 8844 : arg2 = arg1->next;
8951 : :
8952 : : /* Check whether the expression is a scalar or not; we cannot use
8953 : : arg1->expr->rank as it can be nonzero for proc pointers. */
8954 : 8844 : ss = gfc_walk_expr (arg1->expr);
8955 : 8844 : scalar = ss == gfc_ss_terminator;
8956 : 8844 : if (!scalar)
8957 : 3846 : gfc_free_ss_chain (ss);
8958 : :
8959 : 8844 : if (!arg2->expr)
8960 : : {
8961 : : /* No optional target. */
8962 : 6513 : if (scalar)
8963 : : {
8964 : : /* A pointer to a scalar. */
8965 : 4119 : arg1se.want_pointer = 1;
8966 : 4119 : gfc_conv_expr (&arg1se, arg1->expr);
8967 : 4119 : if (arg1->expr->symtree->n.sym->attr.proc_pointer
8968 : 184 : && arg1->expr->symtree->n.sym->attr.dummy)
8969 : 78 : arg1se.expr = build_fold_indirect_ref_loc (input_location,
8970 : : arg1se.expr);
8971 : 4119 : if (arg1->expr->ts.type == BT_CLASS)
8972 : : {
8973 : 384 : tmp2 = gfc_class_data_get (arg1se.expr);
8974 : 384 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
8975 : 0 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
8976 : : }
8977 : : else
8978 : 3735 : tmp2 = arg1se.expr;
8979 : : }
8980 : : else
8981 : : {
8982 : : /* A pointer to an array. */
8983 : 2394 : gfc_conv_expr_descriptor (&arg1se, arg1->expr);
8984 : 2394 : tmp2 = gfc_conv_descriptor_data_get (arg1se.expr);
8985 : : }
8986 : 6513 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
8987 : 6513 : gfc_add_block_to_block (&se->post, &arg1se.post);
8988 : 6513 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp2,
8989 : 6513 : fold_convert (TREE_TYPE (tmp2), null_pointer_node));
8990 : 6513 : se->expr = tmp;
8991 : : }
8992 : : else
8993 : : {
8994 : : /* An optional target. */
8995 : 2331 : if (arg2->expr->ts.type == BT_CLASS
8996 : 24 : && arg2->expr->expr_type != EXPR_FUNCTION)
8997 : 18 : gfc_add_data_component (arg2->expr);
8998 : :
8999 : 2331 : if (scalar)
9000 : : {
9001 : : /* A pointer to a scalar. */
9002 : 879 : arg1se.want_pointer = 1;
9003 : 879 : gfc_conv_expr (&arg1se, arg1->expr);
9004 : 879 : if (arg1->expr->symtree->n.sym->attr.proc_pointer
9005 : 92 : && arg1->expr->symtree->n.sym->attr.dummy)
9006 : 42 : arg1se.expr = build_fold_indirect_ref_loc (input_location,
9007 : : arg1se.expr);
9008 : 879 : if (arg1->expr->ts.type == BT_CLASS)
9009 : 246 : arg1se.expr = gfc_class_data_get (arg1se.expr);
9010 : :
9011 : 879 : arg2se.want_pointer = 1;
9012 : 879 : gfc_conv_expr (&arg2se, arg2->expr);
9013 : 879 : if (arg2->expr->symtree->n.sym->attr.proc_pointer
9014 : 0 : && arg2->expr->symtree->n.sym->attr.dummy)
9015 : 0 : arg2se.expr = build_fold_indirect_ref_loc (input_location,
9016 : : arg2se.expr);
9017 : 879 : if (arg2->expr->ts.type == BT_CLASS)
9018 : : {
9019 : 6 : arg2se.expr = gfc_evaluate_now (arg2se.expr, &arg2se.pre);
9020 : 6 : arg2se.expr = gfc_class_data_get (arg2se.expr);
9021 : : }
9022 : 879 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9023 : 879 : gfc_add_block_to_block (&se->post, &arg1se.post);
9024 : 879 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9025 : 879 : gfc_add_block_to_block (&se->post, &arg2se.post);
9026 : 879 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9027 : : arg1se.expr, arg2se.expr);
9028 : 879 : tmp2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9029 : : arg1se.expr, null_pointer_node);
9030 : 879 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9031 : : logical_type_node, tmp, tmp2);
9032 : : }
9033 : : else
9034 : : {
9035 : : /* An array pointer of zero length is not associated if target is
9036 : : present. */
9037 : 1452 : arg1se.descriptor_only = 1;
9038 : 1452 : gfc_conv_expr_lhs (&arg1se, arg1->expr);
9039 : 1452 : if (arg1->expr->rank == -1)
9040 : : {
9041 : 84 : tmp = gfc_conv_descriptor_rank (arg1se.expr);
9042 : 168 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9043 : 84 : TREE_TYPE (tmp), tmp,
9044 : 84 : build_int_cst (TREE_TYPE (tmp), 1));
9045 : : }
9046 : : else
9047 : 1368 : tmp = gfc_rank_cst[arg1->expr->rank - 1];
9048 : 1452 : tmp = gfc_conv_descriptor_stride_get (arg1se.expr, tmp);
9049 : 1452 : if (arg2->expr->rank != 0)
9050 : 1422 : nonzero_arraylen = fold_build2_loc (input_location, NE_EXPR,
9051 : : logical_type_node, tmp,
9052 : 1422 : build_int_cst (TREE_TYPE (tmp), 0));
9053 : :
9054 : : /* A pointer to an array, call library function _gfor_associated. */
9055 : 1452 : arg1se.want_pointer = 1;
9056 : 1452 : gfc_conv_expr_descriptor (&arg1se, arg1->expr);
9057 : 1452 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9058 : 1452 : gfc_add_block_to_block (&se->post, &arg1se.post);
9059 : :
9060 : 1452 : arg2se.want_pointer = 1;
9061 : 1452 : arg2se.force_no_tmp = 1;
9062 : 1452 : if (arg2->expr->rank != 0)
9063 : 1422 : gfc_conv_expr_descriptor (&arg2se, arg2->expr);
9064 : : else
9065 : : {
9066 : 30 : gfc_conv_expr (&arg2se, arg2->expr);
9067 : 30 : arg2se.expr
9068 : 30 : = gfc_conv_scalar_to_descriptor (&arg2se, arg2se.expr,
9069 : 30 : gfc_expr_attr (arg2->expr));
9070 : 30 : arg2se.expr = gfc_build_addr_expr (NULL_TREE, arg2se.expr);
9071 : : }
9072 : 1452 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9073 : 1452 : gfc_add_block_to_block (&se->post, &arg2se.post);
9074 : 1452 : se->expr = build_call_expr_loc (input_location,
9075 : : gfor_fndecl_associated, 2,
9076 : : arg1se.expr, arg2se.expr);
9077 : 1452 : se->expr = convert (logical_type_node, se->expr);
9078 : 1452 : if (arg2->expr->rank != 0)
9079 : 1422 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9080 : : logical_type_node, se->expr,
9081 : : nonzero_arraylen);
9082 : : }
9083 : :
9084 : : /* If target is present zero character length pointers cannot
9085 : : be associated. */
9086 : 2331 : if (arg1->expr->ts.type == BT_CHARACTER)
9087 : : {
9088 : 630 : tmp = arg1se.string_length;
9089 : 630 : tmp = fold_build2_loc (input_location, NE_EXPR,
9090 : : logical_type_node, tmp,
9091 : 630 : build_zero_cst (TREE_TYPE (tmp)));
9092 : 630 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9093 : : logical_type_node, se->expr, tmp);
9094 : : }
9095 : : }
9096 : :
9097 : 8844 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9098 : 8844 : }
9099 : :
9100 : :
9101 : : /* Generate code for the SAME_TYPE_AS intrinsic.
9102 : : Generate inline code that directly checks the vindices. */
9103 : :
9104 : : static void
9105 : 403 : gfc_conv_same_type_as (gfc_se *se, gfc_expr *expr)
9106 : : {
9107 : 403 : gfc_expr *a, *b;
9108 : 403 : gfc_se se1, se2;
9109 : 403 : tree tmp;
9110 : 403 : tree conda = NULL_TREE, condb = NULL_TREE;
9111 : :
9112 : 403 : gfc_init_se (&se1, NULL);
9113 : 403 : gfc_init_se (&se2, NULL);
9114 : :
9115 : 403 : a = expr->value.function.actual->expr;
9116 : 403 : b = expr->value.function.actual->next->expr;
9117 : :
9118 : 403 : bool unlimited_poly_a = UNLIMITED_POLY (a);
9119 : 403 : bool unlimited_poly_b = UNLIMITED_POLY (b);
9120 : 403 : if (unlimited_poly_a)
9121 : : {
9122 : 105 : se1.want_pointer = 1;
9123 : 105 : gfc_add_vptr_component (a);
9124 : : }
9125 : 298 : else if (a->ts.type == BT_CLASS)
9126 : : {
9127 : 256 : gfc_add_vptr_component (a);
9128 : 256 : gfc_add_hash_component (a);
9129 : : }
9130 : 42 : else if (a->ts.type == BT_DERIVED)
9131 : 42 : a = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9132 : 42 : a->ts.u.derived->hash_value);
9133 : :
9134 : 403 : if (unlimited_poly_b)
9135 : : {
9136 : 66 : se2.want_pointer = 1;
9137 : 66 : gfc_add_vptr_component (b);
9138 : : }
9139 : 337 : else if (b->ts.type == BT_CLASS)
9140 : : {
9141 : 169 : gfc_add_vptr_component (b);
9142 : 169 : gfc_add_hash_component (b);
9143 : : }
9144 : 168 : else if (b->ts.type == BT_DERIVED)
9145 : 168 : b = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9146 : 168 : b->ts.u.derived->hash_value);
9147 : :
9148 : 403 : gfc_conv_expr (&se1, a);
9149 : 403 : gfc_conv_expr (&se2, b);
9150 : :
9151 : 403 : if (unlimited_poly_a)
9152 : : {
9153 : 105 : conda = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9154 : : se1.expr,
9155 : 105 : build_int_cst (TREE_TYPE (se1.expr), 0));
9156 : 105 : se1.expr = gfc_vptr_hash_get (se1.expr);
9157 : : }
9158 : :
9159 : 403 : if (unlimited_poly_b)
9160 : : {
9161 : 66 : condb = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9162 : : se2.expr,
9163 : 66 : build_int_cst (TREE_TYPE (se2.expr), 0));
9164 : 66 : se2.expr = gfc_vptr_hash_get (se2.expr);
9165 : : }
9166 : :
9167 : 403 : tmp = fold_build2_loc (input_location, EQ_EXPR,
9168 : : logical_type_node, se1.expr,
9169 : 403 : fold_convert (TREE_TYPE (se1.expr), se2.expr));
9170 : :
9171 : 403 : if (conda)
9172 : 105 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9173 : : logical_type_node, conda, tmp);
9174 : :
9175 : 403 : if (condb)
9176 : 66 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9177 : : logical_type_node, condb, tmp);
9178 : :
9179 : 403 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
9180 : 403 : }
9181 : :
9182 : :
9183 : : /* Generate code for SELECTED_CHAR_KIND (NAME) intrinsic function. */
9184 : :
9185 : : static void
9186 : 42 : gfc_conv_intrinsic_sc_kind (gfc_se *se, gfc_expr *expr)
9187 : : {
9188 : 42 : tree args[2];
9189 : :
9190 : 42 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
9191 : 42 : se->expr = build_call_expr_loc (input_location,
9192 : : gfor_fndecl_sc_kind, 2, args[0], args[1]);
9193 : 42 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9194 : 42 : }
9195 : :
9196 : :
9197 : : /* Generate code for SELECTED_INT_KIND (R) intrinsic function. */
9198 : :
9199 : : static void
9200 : 45 : gfc_conv_intrinsic_si_kind (gfc_se *se, gfc_expr *expr)
9201 : : {
9202 : 45 : tree arg, type;
9203 : :
9204 : 45 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9205 : :
9206 : : /* The argument to SELECTED_INT_KIND is INTEGER(4). */
9207 : 45 : type = gfc_get_int_type (4);
9208 : 45 : arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9209 : :
9210 : : /* Convert it to the required type. */
9211 : 45 : type = gfc_typenode_for_spec (&expr->ts);
9212 : 45 : se->expr = build_call_expr_loc (input_location,
9213 : : gfor_fndecl_si_kind, 1, arg);
9214 : 45 : se->expr = fold_convert (type, se->expr);
9215 : 45 : }
9216 : :
9217 : :
9218 : : /* Generate code for SELECTED_LOGICAL_KIND (BITS) intrinsic function. */
9219 : :
9220 : : static void
9221 : 6 : gfc_conv_intrinsic_sl_kind (gfc_se *se, gfc_expr *expr)
9222 : : {
9223 : 6 : tree arg, type;
9224 : :
9225 : 6 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9226 : :
9227 : : /* The argument to SELECTED_LOGICAL_KIND is INTEGER(4). */
9228 : 6 : type = gfc_get_int_type (4);
9229 : 6 : arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9230 : :
9231 : : /* Convert it to the required type. */
9232 : 6 : type = gfc_typenode_for_spec (&expr->ts);
9233 : 6 : se->expr = build_call_expr_loc (input_location,
9234 : : gfor_fndecl_sl_kind, 1, arg);
9235 : 6 : se->expr = fold_convert (type, se->expr);
9236 : 6 : }
9237 : :
9238 : :
9239 : : /* Generate code for SELECTED_REAL_KIND (P, R, RADIX) intrinsic function. */
9240 : :
9241 : : static void
9242 : 82 : gfc_conv_intrinsic_sr_kind (gfc_se *se, gfc_expr *expr)
9243 : : {
9244 : 82 : gfc_actual_arglist *actual;
9245 : 82 : tree type;
9246 : 82 : gfc_se argse;
9247 : 82 : vec<tree, va_gc> *args = NULL;
9248 : :
9249 : 328 : for (actual = expr->value.function.actual; actual; actual = actual->next)
9250 : : {
9251 : 246 : gfc_init_se (&argse, se);
9252 : :
9253 : : /* Pass a NULL pointer for an absent arg. */
9254 : 246 : if (actual->expr == NULL)
9255 : 96 : argse.expr = null_pointer_node;
9256 : : else
9257 : : {
9258 : 150 : gfc_typespec ts;
9259 : 150 : gfc_clear_ts (&ts);
9260 : :
9261 : 150 : if (actual->expr->ts.kind != gfc_c_int_kind)
9262 : : {
9263 : : /* The arguments to SELECTED_REAL_KIND are INTEGER(4). */
9264 : 0 : ts.type = BT_INTEGER;
9265 : 0 : ts.kind = gfc_c_int_kind;
9266 : 0 : gfc_convert_type (actual->expr, &ts, 2);
9267 : : }
9268 : 150 : gfc_conv_expr_reference (&argse, actual->expr);
9269 : : }
9270 : :
9271 : 246 : gfc_add_block_to_block (&se->pre, &argse.pre);
9272 : 246 : gfc_add_block_to_block (&se->post, &argse.post);
9273 : 246 : vec_safe_push (args, argse.expr);
9274 : : }
9275 : :
9276 : : /* Convert it to the required type. */
9277 : 82 : type = gfc_typenode_for_spec (&expr->ts);
9278 : 82 : se->expr = build_call_expr_loc_vec (input_location,
9279 : : gfor_fndecl_sr_kind, args);
9280 : 82 : se->expr = fold_convert (type, se->expr);
9281 : 82 : }
9282 : :
9283 : :
9284 : : /* Generate code for TRIM (A) intrinsic function. */
9285 : :
9286 : : static void
9287 : 574 : gfc_conv_intrinsic_trim (gfc_se * se, gfc_expr * expr)
9288 : : {
9289 : 574 : tree var;
9290 : 574 : tree len;
9291 : 574 : tree addr;
9292 : 574 : tree tmp;
9293 : 574 : tree cond;
9294 : 574 : tree fndecl;
9295 : 574 : tree function;
9296 : 574 : tree *args;
9297 : 574 : unsigned int num_args;
9298 : :
9299 : 574 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
9300 : 574 : args = XALLOCAVEC (tree, num_args);
9301 : :
9302 : 574 : var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
9303 : 574 : addr = gfc_build_addr_expr (ppvoid_type_node, var);
9304 : 574 : len = gfc_create_var (gfc_charlen_type_node, "len");
9305 : :
9306 : 574 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
9307 : 574 : args[0] = gfc_build_addr_expr (NULL_TREE, len);
9308 : 574 : args[1] = addr;
9309 : :
9310 : 574 : if (expr->ts.kind == 1)
9311 : 542 : function = gfor_fndecl_string_trim;
9312 : 32 : else if (expr->ts.kind == 4)
9313 : 32 : function = gfor_fndecl_string_trim_char4;
9314 : : else
9315 : 0 : gcc_unreachable ();
9316 : :
9317 : 574 : fndecl = build_addr (function);
9318 : 574 : tmp = build_call_array_loc (input_location,
9319 : 574 : TREE_TYPE (TREE_TYPE (function)), fndecl,
9320 : : num_args, args);
9321 : 574 : gfc_add_expr_to_block (&se->pre, tmp);
9322 : :
9323 : : /* Free the temporary afterwards, if necessary. */
9324 : 574 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9325 : 574 : len, build_int_cst (TREE_TYPE (len), 0));
9326 : 574 : tmp = gfc_call_free (var);
9327 : 574 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
9328 : 574 : gfc_add_expr_to_block (&se->post, tmp);
9329 : :
9330 : 574 : se->expr = var;
9331 : 574 : se->string_length = len;
9332 : 574 : }
9333 : :
9334 : :
9335 : : /* Generate code for REPEAT (STRING, NCOPIES) intrinsic function. */
9336 : :
9337 : : static void
9338 : 529 : gfc_conv_intrinsic_repeat (gfc_se * se, gfc_expr * expr)
9339 : : {
9340 : 529 : tree args[3], ncopies, dest, dlen, src, slen, ncopies_type;
9341 : 529 : tree type, cond, tmp, count, exit_label, n, max, largest;
9342 : 529 : tree size;
9343 : 529 : stmtblock_t block, body;
9344 : 529 : int i;
9345 : :
9346 : : /* We store in charsize the size of a character. */
9347 : 529 : i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
9348 : 529 : size = build_int_cst (sizetype, gfc_character_kinds[i].bit_size / 8);
9349 : :
9350 : : /* Get the arguments. */
9351 : 529 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
9352 : 529 : slen = fold_convert (sizetype, gfc_evaluate_now (args[0], &se->pre));
9353 : 529 : src = args[1];
9354 : 529 : ncopies = gfc_evaluate_now (args[2], &se->pre);
9355 : 529 : ncopies_type = TREE_TYPE (ncopies);
9356 : :
9357 : : /* Check that NCOPIES is not negative. */
9358 : 529 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, ncopies,
9359 : : build_int_cst (ncopies_type, 0));
9360 : 529 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9361 : : "Argument NCOPIES of REPEAT intrinsic is negative "
9362 : : "(its value is %ld)",
9363 : : fold_convert (long_integer_type_node, ncopies));
9364 : :
9365 : : /* If the source length is zero, any non negative value of NCOPIES
9366 : : is valid, and nothing happens. */
9367 : 529 : n = gfc_create_var (ncopies_type, "ncopies");
9368 : 529 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9369 : : size_zero_node);
9370 : 529 : tmp = fold_build3_loc (input_location, COND_EXPR, ncopies_type, cond,
9371 : : build_int_cst (ncopies_type, 0), ncopies);
9372 : 529 : gfc_add_modify (&se->pre, n, tmp);
9373 : 529 : ncopies = n;
9374 : :
9375 : : /* Check that ncopies is not too large: ncopies should be less than
9376 : : (or equal to) MAX / slen, where MAX is the maximal integer of
9377 : : the gfc_charlen_type_node type. If slen == 0, we need a special
9378 : : case to avoid the division by zero. */
9379 : 529 : max = fold_build2_loc (input_location, TRUNC_DIV_EXPR, sizetype,
9380 : 529 : fold_convert (sizetype,
9381 : : TYPE_MAX_VALUE (gfc_charlen_type_node)),
9382 : : slen);
9383 : 1054 : largest = TYPE_PRECISION (sizetype) > TYPE_PRECISION (ncopies_type)
9384 : 529 : ? sizetype : ncopies_type;
9385 : 529 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9386 : : fold_convert (largest, ncopies),
9387 : : fold_convert (largest, max));
9388 : 529 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9389 : : size_zero_node);
9390 : 529 : cond = fold_build3_loc (input_location, COND_EXPR, logical_type_node, tmp,
9391 : : logical_false_node, cond);
9392 : 529 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9393 : : "Argument NCOPIES of REPEAT intrinsic is too large");
9394 : :
9395 : : /* Compute the destination length. */
9396 : 529 : dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
9397 : : fold_convert (gfc_charlen_type_node, slen),
9398 : : fold_convert (gfc_charlen_type_node, ncopies));
9399 : 529 : type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
9400 : 529 : dest = gfc_conv_string_tmp (se, build_pointer_type (type), dlen);
9401 : :
9402 : : /* Generate the code to do the repeat operation:
9403 : : for (i = 0; i < ncopies; i++)
9404 : : memmove (dest + (i * slen * size), src, slen*size); */
9405 : 529 : gfc_start_block (&block);
9406 : 529 : count = gfc_create_var (sizetype, "count");
9407 : 529 : gfc_add_modify (&block, count, size_zero_node);
9408 : 529 : exit_label = gfc_build_label_decl (NULL_TREE);
9409 : :
9410 : : /* Start the loop body. */
9411 : 529 : gfc_start_block (&body);
9412 : :
9413 : : /* Exit the loop if count >= ncopies. */
9414 : 529 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, count,
9415 : : fold_convert (sizetype, ncopies));
9416 : 529 : tmp = build1_v (GOTO_EXPR, exit_label);
9417 : 529 : TREE_USED (exit_label) = 1;
9418 : 529 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9419 : : build_empty_stmt (input_location));
9420 : 529 : gfc_add_expr_to_block (&body, tmp);
9421 : :
9422 : : /* Call memmove (dest + (i*slen*size), src, slen*size). */
9423 : 529 : tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, slen,
9424 : : count);
9425 : 529 : tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, tmp,
9426 : : size);
9427 : 529 : tmp = fold_build_pointer_plus_loc (input_location,
9428 : : fold_convert (pvoid_type_node, dest), tmp);
9429 : 529 : tmp = build_call_expr_loc (input_location,
9430 : : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9431 : : 3, tmp, src,
9432 : : fold_build2_loc (input_location, MULT_EXPR,
9433 : : size_type_node, slen, size));
9434 : 529 : gfc_add_expr_to_block (&body, tmp);
9435 : :
9436 : : /* Increment count. */
9437 : 529 : tmp = fold_build2_loc (input_location, PLUS_EXPR, sizetype,
9438 : : count, size_one_node);
9439 : 529 : gfc_add_modify (&body, count, tmp);
9440 : :
9441 : : /* Build the loop. */
9442 : 529 : tmp = build1_v (LOOP_EXPR, gfc_finish_block (&body));
9443 : 529 : gfc_add_expr_to_block (&block, tmp);
9444 : :
9445 : : /* Add the exit label. */
9446 : 529 : tmp = build1_v (LABEL_EXPR, exit_label);
9447 : 529 : gfc_add_expr_to_block (&block, tmp);
9448 : :
9449 : : /* Finish the block. */
9450 : 529 : tmp = gfc_finish_block (&block);
9451 : 529 : gfc_add_expr_to_block (&se->pre, tmp);
9452 : :
9453 : : /* Set the result value. */
9454 : 529 : se->expr = dest;
9455 : 529 : se->string_length = dlen;
9456 : 529 : }
9457 : :
9458 : :
9459 : : /* Generate code for the IARGC intrinsic. */
9460 : :
9461 : : static void
9462 : 12 : gfc_conv_intrinsic_iargc (gfc_se * se, gfc_expr * expr)
9463 : : {
9464 : 12 : tree tmp;
9465 : 12 : tree fndecl;
9466 : 12 : tree type;
9467 : :
9468 : : /* Call the library function. This always returns an INTEGER(4). */
9469 : 12 : fndecl = gfor_fndecl_iargc;
9470 : 12 : tmp = build_call_expr_loc (input_location,
9471 : : fndecl, 0);
9472 : :
9473 : : /* Convert it to the required type. */
9474 : 12 : type = gfc_typenode_for_spec (&expr->ts);
9475 : 12 : tmp = fold_convert (type, tmp);
9476 : :
9477 : 12 : se->expr = tmp;
9478 : 12 : }
9479 : :
9480 : :
9481 : : /* Generate code for the KILL intrinsic. */
9482 : :
9483 : : static void
9484 : 8 : conv_intrinsic_kill (gfc_se *se, gfc_expr *expr)
9485 : : {
9486 : 8 : tree *args;
9487 : 8 : tree int4_type_node = gfc_get_int_type (4);
9488 : 8 : tree pid;
9489 : 8 : tree sig;
9490 : 8 : tree tmp;
9491 : 8 : unsigned int num_args;
9492 : :
9493 : 8 : num_args = gfc_intrinsic_argument_list_length (expr);
9494 : 8 : args = XALLOCAVEC (tree, num_args);
9495 : 8 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
9496 : :
9497 : : /* Convert PID to a INTEGER(4) entity. */
9498 : 8 : pid = convert (int4_type_node, args[0]);
9499 : :
9500 : : /* Convert SIG to a INTEGER(4) entity. */
9501 : 8 : sig = convert (int4_type_node, args[1]);
9502 : :
9503 : 8 : tmp = build_call_expr_loc (input_location, gfor_fndecl_kill, 2, pid, sig);
9504 : :
9505 : 8 : se->expr = fold_convert (TREE_TYPE (args[0]), tmp);
9506 : 8 : }
9507 : :
9508 : :
9509 : : static tree
9510 : 15 : conv_intrinsic_kill_sub (gfc_code *code)
9511 : : {
9512 : 15 : stmtblock_t block;
9513 : 15 : gfc_se se, se_stat;
9514 : 15 : tree int4_type_node = gfc_get_int_type (4);
9515 : 15 : tree pid;
9516 : 15 : tree sig;
9517 : 15 : tree statp;
9518 : 15 : tree tmp;
9519 : :
9520 : : /* Make the function call. */
9521 : 15 : gfc_init_block (&block);
9522 : 15 : gfc_init_se (&se, NULL);
9523 : :
9524 : : /* Convert PID to a INTEGER(4) entity. */
9525 : 15 : gfc_conv_expr (&se, code->ext.actual->expr);
9526 : 15 : gfc_add_block_to_block (&block, &se.pre);
9527 : 15 : pid = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9528 : 15 : gfc_add_block_to_block (&block, &se.post);
9529 : :
9530 : : /* Convert SIG to a INTEGER(4) entity. */
9531 : 15 : gfc_conv_expr (&se, code->ext.actual->next->expr);
9532 : 15 : gfc_add_block_to_block (&block, &se.pre);
9533 : 15 : sig = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9534 : 15 : gfc_add_block_to_block (&block, &se.post);
9535 : :
9536 : : /* Deal with an optional STATUS. */
9537 : 15 : if (code->ext.actual->next->next->expr)
9538 : : {
9539 : 10 : gfc_init_se (&se_stat, NULL);
9540 : 10 : gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
9541 : 10 : statp = gfc_create_var (gfc_get_int_type (4), "_statp");
9542 : : }
9543 : : else
9544 : : statp = NULL_TREE;
9545 : :
9546 : 25 : tmp = build_call_expr_loc (input_location, gfor_fndecl_kill_sub, 3, pid, sig,
9547 : 10 : statp ? gfc_build_addr_expr (NULL_TREE, statp) : null_pointer_node);
9548 : :
9549 : 15 : gfc_add_expr_to_block (&block, tmp);
9550 : :
9551 : 15 : if (statp && statp != se_stat.expr)
9552 : 10 : gfc_add_modify (&block, se_stat.expr,
9553 : 10 : fold_convert (TREE_TYPE (se_stat.expr), statp));
9554 : :
9555 : 15 : return gfc_finish_block (&block);
9556 : : }
9557 : :
9558 : :
9559 : :
9560 : : /* The loc intrinsic returns the address of its argument as
9561 : : gfc_index_integer_kind integer. */
9562 : :
9563 : : static void
9564 : 8582 : gfc_conv_intrinsic_loc (gfc_se * se, gfc_expr * expr)
9565 : : {
9566 : 8582 : tree temp_var;
9567 : 8582 : gfc_expr *arg_expr;
9568 : :
9569 : 8582 : gcc_assert (!se->ss);
9570 : :
9571 : 8582 : arg_expr = expr->value.function.actual->expr;
9572 : 8582 : if (arg_expr->rank == 0)
9573 : : {
9574 : 6182 : if (arg_expr->ts.type == BT_CLASS)
9575 : 18 : gfc_add_data_component (arg_expr);
9576 : 6182 : gfc_conv_expr_reference (se, arg_expr);
9577 : : }
9578 : : else
9579 : 2400 : gfc_conv_array_parameter (se, arg_expr, true, NULL, NULL, NULL);
9580 : 8582 : se->expr = convert (gfc_get_int_type (gfc_index_integer_kind), se->expr);
9581 : :
9582 : : /* Create a temporary variable for loc return value. Without this,
9583 : : we get an error an ICE in gcc/expr.cc(expand_expr_addr_expr_1). */
9584 : 8582 : temp_var = gfc_create_var (gfc_get_int_type (gfc_index_integer_kind), NULL);
9585 : 8582 : gfc_add_modify (&se->pre, temp_var, se->expr);
9586 : 8582 : se->expr = temp_var;
9587 : 8582 : }
9588 : :
9589 : :
9590 : : /* Specialized trim for f_c_string. */
9591 : :
9592 : : static void
9593 : 42 : conv_trim (gfc_se *tse, gfc_se *str)
9594 : : {
9595 : 42 : tree cond, plen, pvar, tlen, ttmp, tvar;
9596 : :
9597 : 42 : tlen = gfc_create_var (gfc_charlen_type_node, "tlen");
9598 : 42 : plen = gfc_build_addr_expr (NULL_TREE, tlen);
9599 : :
9600 : 42 : tvar = gfc_create_var (pchar_type_node, "tstr");
9601 : 42 : pvar = gfc_build_addr_expr (ppvoid_type_node, tvar);
9602 : :
9603 : 42 : ttmp = build_call_expr_loc (input_location, gfor_fndecl_string_trim, 4,
9604 : : plen, pvar, str->string_length, str->expr);
9605 : :
9606 : 42 : gfc_add_expr_to_block (&tse->pre, ttmp);
9607 : :
9608 : : /* Free the temporary afterwards, if necessary. */
9609 : 42 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9610 : 42 : tlen, build_int_cst (TREE_TYPE (tlen), 0));
9611 : 42 : ttmp = gfc_call_free (tvar);
9612 : 42 : ttmp = build3_v (COND_EXPR, cond, ttmp, build_empty_stmt (input_location));
9613 : 42 : gfc_add_expr_to_block (&tse->post, ttmp);
9614 : :
9615 : 42 : tse->expr = tvar;
9616 : 42 : tse->string_length = tlen;
9617 : 42 : }
9618 : :
9619 : :
9620 : : /* The following routine generates code for the intrinsic functions from
9621 : : the ISO_C_BINDING module: C_LOC, C_FUNLOC, C_ASSOCIATED, and
9622 : : F_C_STRING. */
9623 : :
9624 : : static void
9625 : 8953 : conv_isocbinding_function (gfc_se *se, gfc_expr *expr)
9626 : : {
9627 : 8953 : gfc_actual_arglist *arg = expr->value.function.actual;
9628 : :
9629 : 8953 : if (expr->value.function.isym->id == GFC_ISYM_C_LOC)
9630 : : {
9631 : 6650 : if (arg->expr->rank == 0)
9632 : 1918 : gfc_conv_expr_reference (se, arg->expr);
9633 : 4732 : else if (gfc_is_simply_contiguous (arg->expr, false, false))
9634 : 3696 : gfc_conv_array_parameter (se, arg->expr, true, NULL, NULL, NULL);
9635 : : else
9636 : : {
9637 : 1036 : gfc_conv_expr_descriptor (se, arg->expr);
9638 : 1036 : se->expr = gfc_conv_descriptor_data_get (se->expr);
9639 : : }
9640 : :
9641 : : /* TODO -- the following two lines shouldn't be necessary, but if
9642 : : they're removed, a bug is exposed later in the code path.
9643 : : This workaround was thus introduced, but will have to be
9644 : : removed; please see PR 35150 for details about the issue. */
9645 : 6650 : se->expr = convert (pvoid_type_node, se->expr);
9646 : 6650 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
9647 : : }
9648 : 2303 : else if (expr->value.function.isym->id == GFC_ISYM_C_FUNLOC)
9649 : 231 : gfc_conv_expr_reference (se, arg->expr);
9650 : 2072 : else if (expr->value.function.isym->id == GFC_ISYM_C_ASSOCIATED)
9651 : : {
9652 : 2030 : gfc_se arg1se;
9653 : 2030 : gfc_se arg2se;
9654 : :
9655 : : /* Build the addr_expr for the first argument. The argument is
9656 : : already an *address* so we don't need to set want_pointer in
9657 : : the gfc_se. */
9658 : 2030 : gfc_init_se (&arg1se, NULL);
9659 : 2030 : gfc_conv_expr (&arg1se, arg->expr);
9660 : 2030 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9661 : 2030 : gfc_add_block_to_block (&se->post, &arg1se.post);
9662 : :
9663 : : /* See if we were given two arguments. */
9664 : 2030 : if (arg->next->expr == NULL)
9665 : : /* Only given one arg so generate a null and do a
9666 : : not-equal comparison against the first arg. */
9667 : 1675 : se->expr = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9668 : : arg1se.expr,
9669 : 1675 : fold_convert (TREE_TYPE (arg1se.expr),
9670 : : null_pointer_node));
9671 : : else
9672 : : {
9673 : 355 : tree eq_expr;
9674 : 355 : tree not_null_expr;
9675 : :
9676 : : /* Given two arguments so build the arg2se from second arg. */
9677 : 355 : gfc_init_se (&arg2se, NULL);
9678 : 355 : gfc_conv_expr (&arg2se, arg->next->expr);
9679 : 355 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9680 : 355 : gfc_add_block_to_block (&se->post, &arg2se.post);
9681 : :
9682 : : /* Generate test to compare that the two args are equal. */
9683 : 355 : eq_expr = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9684 : : arg1se.expr, arg2se.expr);
9685 : : /* Generate test to ensure that the first arg is not null. */
9686 : 355 : not_null_expr = fold_build2_loc (input_location, NE_EXPR,
9687 : : logical_type_node,
9688 : : arg1se.expr, null_pointer_node);
9689 : :
9690 : : /* Finally, the generated test must check that both arg1 is not
9691 : : NULL and that it is equal to the second arg. */
9692 : 355 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9693 : : logical_type_node,
9694 : : not_null_expr, eq_expr);
9695 : : }
9696 : : }
9697 : 42 : else if (expr->value.function.isym->id == GFC_ISYM_F_C_STRING)
9698 : : {
9699 : : /* There are three cases:
9700 : : f_c_string(string) -> trim(string) // c_null_char
9701 : : f_c_string(string, .false.) -> trim(string) // c_null_char
9702 : : f_c_string(string, .true.) -> string // c_null_char */
9703 : :
9704 : 42 : gfc_se lse, rse, tse;
9705 : 42 : tree len, tmp, var;
9706 : 42 : gfc_expr *string = arg->expr;
9707 : 42 : gfc_expr *asis = arg->next->expr;
9708 : 42 : gfc_expr *cnc;
9709 : :
9710 : : /* Convert string. */
9711 : 42 : gfc_init_se (&lse, se);
9712 : 42 : gfc_conv_expr (&lse, string);
9713 : 42 : gfc_conv_string_parameter (&lse);
9714 : :
9715 : : /* Create a string for C_NULL_CHAR and convert it. */
9716 : 42 : cnc = gfc_get_character_expr (gfc_default_character_kind,
9717 : : &string->where, "\0", 1);
9718 : 42 : gfc_init_se (&rse, se);
9719 : 42 : gfc_conv_expr (&rse, cnc);
9720 : 42 : gfc_conv_string_parameter (&rse);
9721 : 42 : gfc_free_expr (cnc);
9722 : :
9723 : : #ifdef cnode
9724 : : #undef cnode
9725 : : #endif
9726 : : #define cnode gfc_charlen_type_node
9727 : 42 : if (asis)
9728 : : {
9729 : 30 : stmtblock_t block;
9730 : 30 : gfc_se asis_se, vse;
9731 : 30 : tree elen, evar, tlen, tvar;
9732 : 30 : tree else_branch, then_branch;
9733 : :
9734 : 30 : elen = evar = tlen = tvar = NULL_TREE;
9735 : :
9736 : : /* f_c_string(string, .true.) -> string // c_null_char */
9737 : :
9738 : 30 : gfc_init_block (&block);
9739 : :
9740 : 30 : gfc_add_block_to_block (&block, &lse.pre);
9741 : 30 : gfc_add_block_to_block (&block, &rse.pre);
9742 : :
9743 : 30 : tlen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9744 : : fold_convert (cnode, lse.string_length),
9745 : : fold_convert (cnode, rse.string_length));
9746 : :
9747 : 30 : gfc_init_se (&vse, se);
9748 : 30 : tvar = gfc_conv_string_tmp (&vse, pchar_type_node, tlen);
9749 : 30 : gfc_add_block_to_block (&block, &vse.pre);
9750 : :
9751 : 30 : tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9752 : : 6, tlen, tvar,
9753 : : lse.string_length, lse.expr,
9754 : : rse.string_length, rse.expr);
9755 : 30 : gfc_add_expr_to_block (&block, tmp);
9756 : :
9757 : 30 : then_branch = gfc_finish_block (&block);
9758 : :
9759 : : /* f_c_string(string, .false.) = trim(string) // c_null_char */
9760 : :
9761 : 30 : gfc_init_block (&block);
9762 : :
9763 : 30 : gfc_init_se (&tse, se);
9764 : 30 : conv_trim (&tse, &lse);
9765 : 30 : gfc_add_block_to_block (&block, &tse.pre);
9766 : 30 : gfc_add_block_to_block (&block, &rse.pre);
9767 : :
9768 : 30 : elen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9769 : : fold_convert (cnode, tse.string_length),
9770 : : fold_convert (cnode, rse.string_length));
9771 : :
9772 : 30 : gfc_init_se (&vse, se);
9773 : 30 : evar = gfc_conv_string_tmp (&vse, pchar_type_node, elen);
9774 : 30 : gfc_add_block_to_block (&block, &vse.pre);
9775 : :
9776 : 30 : tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9777 : : 6, elen, evar,
9778 : : tse.string_length, tse.expr,
9779 : : rse.string_length, rse.expr);
9780 : 30 : gfc_add_expr_to_block (&block, tmp);
9781 : :
9782 : 30 : else_branch = gfc_finish_block (&block);
9783 : :
9784 : 30 : gfc_init_se (&asis_se, se);
9785 : 30 : gfc_conv_expr (&asis_se, asis);
9786 : 30 : if (asis->expr_type == EXPR_VARIABLE
9787 : 18 : && asis->symtree->n.sym->attr.dummy
9788 : 6 : && asis->symtree->n.sym->attr.optional)
9789 : : {
9790 : 6 : tree present = gfc_conv_expr_present (asis->symtree->n.sym);
9791 : 6 : asis_se.expr = build3_loc (input_location, COND_EXPR,
9792 : : logical_type_node, present,
9793 : : asis_se.expr,
9794 : : build_int_cst (logical_type_node, 0));
9795 : : }
9796 : 30 : gfc_add_block_to_block (&se->pre, &asis_se.pre);
9797 : 30 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
9798 : : asis_se.expr, then_branch, else_branch);
9799 : :
9800 : 30 : gfc_add_expr_to_block (&se->pre, tmp);
9801 : :
9802 : 30 : var = fold_build3_loc (input_location, COND_EXPR, pchar_type_node,
9803 : : asis_se.expr, tvar, evar);
9804 : 30 : gfc_add_expr_to_block (&se->pre, var);
9805 : :
9806 : 30 : len = fold_build3_loc (input_location, COND_EXPR, cnode,
9807 : : asis_se.expr, tlen, elen);
9808 : 30 : gfc_add_expr_to_block (&se->pre, len);
9809 : : }
9810 : : else
9811 : : {
9812 : : /* f_c_string(string) = trim(string) // c_null_char */
9813 : :
9814 : 12 : gfc_add_block_to_block (&se->pre, &lse.pre);
9815 : 12 : gfc_add_block_to_block (&se->pre, &rse.pre);
9816 : :
9817 : 12 : gfc_init_se (&tse, se);
9818 : 12 : conv_trim (&tse, &lse);
9819 : 12 : gfc_add_block_to_block (&se->pre, &tse.pre);
9820 : 12 : gfc_add_block_to_block (&se->post, &tse.post);
9821 : :
9822 : 12 : len = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9823 : : fold_convert (cnode, tse.string_length),
9824 : : fold_convert (cnode, rse.string_length));
9825 : :
9826 : 12 : var = gfc_conv_string_tmp (se, pchar_type_node, len);
9827 : :
9828 : 12 : tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9829 : : 6, len, var,
9830 : : tse.string_length, tse.expr,
9831 : : rse.string_length, rse.expr);
9832 : 12 : gfc_add_expr_to_block (&se->pre, tmp);
9833 : : }
9834 : :
9835 : 42 : se->expr = var;
9836 : 42 : se->string_length = len;
9837 : :
9838 : : #undef cnode
9839 : : }
9840 : : else
9841 : 0 : gcc_unreachable ();
9842 : 8953 : }
9843 : :
9844 : :
9845 : : /* The following routine generates code for the intrinsic
9846 : : subroutines from the ISO_C_BINDING module:
9847 : : * C_F_POINTER
9848 : : * C_F_PROCPOINTER. */
9849 : :
9850 : : static tree
9851 : 2758 : conv_isocbinding_subroutine (gfc_code *code)
9852 : : {
9853 : 2758 : gfc_se se;
9854 : 2758 : gfc_se cptrse;
9855 : 2758 : gfc_se fptrse;
9856 : 2758 : gfc_se shapese;
9857 : 2758 : gfc_ss *shape_ss;
9858 : 2758 : tree desc, dim, tmp, stride, offset;
9859 : 2758 : stmtblock_t body, block;
9860 : 2758 : gfc_loopinfo loop;
9861 : 2758 : gfc_actual_arglist *arg = code->ext.actual;
9862 : :
9863 : 2758 : gfc_init_se (&se, NULL);
9864 : 2758 : gfc_init_se (&cptrse, NULL);
9865 : 2758 : gfc_conv_expr (&cptrse, arg->expr);
9866 : 2758 : gfc_add_block_to_block (&se.pre, &cptrse.pre);
9867 : 2758 : gfc_add_block_to_block (&se.post, &cptrse.post);
9868 : :
9869 : 2758 : gfc_init_se (&fptrse, NULL);
9870 : 2758 : if (arg->next->expr->rank == 0)
9871 : : {
9872 : 2285 : fptrse.want_pointer = 1;
9873 : 2285 : gfc_conv_expr (&fptrse, arg->next->expr);
9874 : 2285 : gfc_add_block_to_block (&se.pre, &fptrse.pre);
9875 : 2285 : gfc_add_block_to_block (&se.post, &fptrse.post);
9876 : 2285 : if (arg->next->expr->symtree->n.sym->attr.proc_pointer
9877 : 57 : && arg->next->expr->symtree->n.sym->attr.dummy)
9878 : 7 : fptrse.expr = build_fold_indirect_ref_loc (input_location,
9879 : : fptrse.expr);
9880 : 4570 : se.expr = fold_build2_loc (input_location, MODIFY_EXPR,
9881 : 2285 : TREE_TYPE (fptrse.expr),
9882 : : fptrse.expr,
9883 : 2285 : fold_convert (TREE_TYPE (fptrse.expr),
9884 : : cptrse.expr));
9885 : 2285 : gfc_add_expr_to_block (&se.pre, se.expr);
9886 : 2285 : gfc_add_block_to_block (&se.pre, &se.post);
9887 : 2285 : return gfc_finish_block (&se.pre);
9888 : : }
9889 : :
9890 : 473 : gfc_start_block (&block);
9891 : :
9892 : : /* Get the descriptor of the Fortran pointer. */
9893 : 473 : fptrse.descriptor_only = 1;
9894 : 473 : gfc_conv_expr_descriptor (&fptrse, arg->next->expr);
9895 : 473 : gfc_add_block_to_block (&block, &fptrse.pre);
9896 : 473 : desc = fptrse.expr;
9897 : :
9898 : : /* Set the span field. */
9899 : 473 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
9900 : 473 : tmp = fold_convert (gfc_array_index_type, tmp);
9901 : 473 : gfc_conv_descriptor_span_set (&block, desc, tmp);
9902 : :
9903 : : /* Set data value, dtype, and offset. */
9904 : 473 : tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc));
9905 : 473 : gfc_conv_descriptor_data_set (&block, desc, fold_convert (tmp, cptrse.expr));
9906 : 473 : gfc_add_modify (&block, gfc_conv_descriptor_dtype (desc),
9907 : 473 : gfc_get_dtype (TREE_TYPE (desc)));
9908 : :
9909 : : /* Start scalarization of the bounds, using the shape argument. */
9910 : :
9911 : 473 : shape_ss = gfc_walk_expr (arg->next->next->expr);
9912 : 473 : gcc_assert (shape_ss != gfc_ss_terminator);
9913 : 473 : gfc_init_se (&shapese, NULL);
9914 : :
9915 : 473 : gfc_init_loopinfo (&loop);
9916 : 473 : gfc_add_ss_to_loop (&loop, shape_ss);
9917 : 473 : gfc_conv_ss_startstride (&loop);
9918 : 473 : gfc_conv_loop_setup (&loop, &arg->next->expr->where);
9919 : 473 : gfc_mark_ss_chain_used (shape_ss, 1);
9920 : :
9921 : 473 : gfc_copy_loopinfo_to_se (&shapese, &loop);
9922 : 473 : shapese.ss = shape_ss;
9923 : :
9924 : 473 : stride = gfc_create_var (gfc_array_index_type, "stride");
9925 : 473 : offset = gfc_create_var (gfc_array_index_type, "offset");
9926 : 473 : gfc_add_modify (&block, stride, gfc_index_one_node);
9927 : 473 : gfc_add_modify (&block, offset, gfc_index_zero_node);
9928 : :
9929 : : /* Loop body. */
9930 : 473 : gfc_start_scalarized_body (&loop, &body);
9931 : :
9932 : 473 : dim = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9933 : : loop.loopvar[0], loop.from[0]);
9934 : :
9935 : : /* Set bounds and stride. */
9936 : 473 : gfc_conv_descriptor_lbound_set (&body, desc, dim, gfc_index_one_node);
9937 : 473 : gfc_conv_descriptor_stride_set (&body, desc, dim, stride);
9938 : :
9939 : 473 : gfc_conv_expr (&shapese, arg->next->next->expr);
9940 : 473 : gfc_add_block_to_block (&body, &shapese.pre);
9941 : 473 : gfc_conv_descriptor_ubound_set (&body, desc, dim, shapese.expr);
9942 : 473 : gfc_add_block_to_block (&body, &shapese.post);
9943 : :
9944 : : /* Calculate offset. */
9945 : 473 : gfc_add_modify (&body, offset,
9946 : : fold_build2_loc (input_location, PLUS_EXPR,
9947 : : gfc_array_index_type, offset, stride));
9948 : : /* Update stride. */
9949 : 473 : gfc_add_modify (&body, stride,
9950 : : fold_build2_loc (input_location, MULT_EXPR,
9951 : : gfc_array_index_type, stride,
9952 : : fold_convert (gfc_array_index_type,
9953 : : shapese.expr)));
9954 : : /* Finish scalarization loop. */
9955 : 473 : gfc_trans_scalarizing_loops (&loop, &body);
9956 : 473 : gfc_add_block_to_block (&block, &loop.pre);
9957 : 473 : gfc_add_block_to_block (&block, &loop.post);
9958 : 473 : gfc_add_block_to_block (&block, &fptrse.post);
9959 : 473 : gfc_cleanup_loop (&loop);
9960 : :
9961 : 473 : gfc_add_modify (&block, offset,
9962 : : fold_build1_loc (input_location, NEGATE_EXPR,
9963 : : gfc_array_index_type, offset));
9964 : 473 : gfc_conv_descriptor_offset_set (&block, desc, offset);
9965 : :
9966 : 473 : gfc_add_expr_to_block (&se.pre, gfc_finish_block (&block));
9967 : 473 : gfc_add_block_to_block (&se.pre, &se.post);
9968 : 473 : return gfc_finish_block (&se.pre);
9969 : : }
9970 : :
9971 : :
9972 : : /* Save and restore floating-point state. */
9973 : :
9974 : : tree
9975 : 944 : gfc_save_fp_state (stmtblock_t *block)
9976 : : {
9977 : 944 : tree type, fpstate, tmp;
9978 : :
9979 : 944 : type = build_array_type (char_type_node,
9980 : : build_range_type (size_type_node, size_zero_node,
9981 : : size_int (GFC_FPE_STATE_BUFFER_SIZE)));
9982 : 944 : fpstate = gfc_create_var (type, "fpstate");
9983 : 944 : fpstate = gfc_build_addr_expr (pvoid_type_node, fpstate);
9984 : :
9985 : 944 : tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_entry,
9986 : : 1, fpstate);
9987 : 944 : gfc_add_expr_to_block (block, tmp);
9988 : :
9989 : 944 : return fpstate;
9990 : : }
9991 : :
9992 : :
9993 : : void
9994 : 944 : gfc_restore_fp_state (stmtblock_t *block, tree fpstate)
9995 : : {
9996 : 944 : tree tmp;
9997 : :
9998 : 944 : tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_exit,
9999 : : 1, fpstate);
10000 : 944 : gfc_add_expr_to_block (block, tmp);
10001 : 944 : }
10002 : :
10003 : :
10004 : : /* Generate code for arguments of IEEE functions. */
10005 : :
10006 : : static void
10007 : 12457 : conv_ieee_function_args (gfc_se *se, gfc_expr *expr, tree *argarray,
10008 : : int nargs)
10009 : : {
10010 : 12457 : gfc_actual_arglist *actual;
10011 : 12457 : gfc_expr *e;
10012 : 12457 : gfc_se argse;
10013 : 12457 : int arg;
10014 : :
10015 : 12457 : actual = expr->value.function.actual;
10016 : 34461 : for (arg = 0; arg < nargs; arg++, actual = actual->next)
10017 : : {
10018 : 22004 : gcc_assert (actual);
10019 : 22004 : e = actual->expr;
10020 : :
10021 : 22004 : gfc_init_se (&argse, se);
10022 : 22004 : gfc_conv_expr_val (&argse, e);
10023 : :
10024 : 22004 : gfc_add_block_to_block (&se->pre, &argse.pre);
10025 : 22004 : gfc_add_block_to_block (&se->post, &argse.post);
10026 : 22004 : argarray[arg] = argse.expr;
10027 : : }
10028 : 12457 : }
10029 : :
10030 : :
10031 : : /* Generate code for intrinsics IEEE_IS_NAN, IEEE_IS_FINITE
10032 : : and IEEE_UNORDERED, which translate directly to GCC type-generic
10033 : : built-ins. */
10034 : :
10035 : : static void
10036 : 1062 : conv_intrinsic_ieee_builtin (gfc_se * se, gfc_expr * expr,
10037 : : enum built_in_function code, int nargs)
10038 : : {
10039 : 1062 : tree args[2];
10040 : 1062 : gcc_assert ((unsigned) nargs <= ARRAY_SIZE (args));
10041 : :
10042 : 1062 : conv_ieee_function_args (se, expr, args, nargs);
10043 : 1062 : se->expr = build_call_expr_loc_array (input_location,
10044 : : builtin_decl_explicit (code),
10045 : : nargs, args);
10046 : 2388 : STRIP_TYPE_NOPS (se->expr);
10047 : 1062 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10048 : 1062 : }
10049 : :
10050 : :
10051 : : /* Generate code for intrinsics IEEE_SIGNBIT. */
10052 : :
10053 : : static void
10054 : 624 : conv_intrinsic_ieee_signbit (gfc_se * se, gfc_expr * expr)
10055 : : {
10056 : 624 : tree arg, signbit;
10057 : :
10058 : 624 : conv_ieee_function_args (se, expr, &arg, 1);
10059 : 624 : signbit = build_call_expr_loc (input_location,
10060 : : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10061 : : 1, arg);
10062 : 624 : signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10063 : : signbit, integer_zero_node);
10064 : 624 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), signbit);
10065 : 624 : }
10066 : :
10067 : :
10068 : : /* Generate code for IEEE_IS_NORMAL intrinsic:
10069 : : IEEE_IS_NORMAL(x) --> (__builtin_isnormal(x) || x == 0) */
10070 : :
10071 : : static void
10072 : 312 : conv_intrinsic_ieee_is_normal (gfc_se * se, gfc_expr * expr)
10073 : : {
10074 : 312 : tree arg, isnormal, iszero;
10075 : :
10076 : : /* Convert arg, evaluate it only once. */
10077 : 312 : conv_ieee_function_args (se, expr, &arg, 1);
10078 : 312 : arg = gfc_evaluate_now (arg, &se->pre);
10079 : :
10080 : 312 : isnormal = build_call_expr_loc (input_location,
10081 : : builtin_decl_explicit (BUILT_IN_ISNORMAL),
10082 : : 1, arg);
10083 : 312 : iszero = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
10084 : 312 : build_real_from_int_cst (TREE_TYPE (arg),
10085 : 312 : integer_zero_node));
10086 : 312 : se->expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
10087 : : logical_type_node, isnormal, iszero);
10088 : 312 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10089 : 312 : }
10090 : :
10091 : :
10092 : : /* Generate code for IEEE_IS_NEGATIVE intrinsic:
10093 : : IEEE_IS_NEGATIVE(x) --> (__builtin_signbit(x) && !__builtin_isnan(x)) */
10094 : :
10095 : : static void
10096 : 312 : conv_intrinsic_ieee_is_negative (gfc_se * se, gfc_expr * expr)
10097 : : {
10098 : 312 : tree arg, signbit, isnan;
10099 : :
10100 : : /* Convert arg, evaluate it only once. */
10101 : 312 : conv_ieee_function_args (se, expr, &arg, 1);
10102 : 312 : arg = gfc_evaluate_now (arg, &se->pre);
10103 : :
10104 : 312 : isnan = build_call_expr_loc (input_location,
10105 : : builtin_decl_explicit (BUILT_IN_ISNAN),
10106 : : 1, arg);
10107 : 936 : STRIP_TYPE_NOPS (isnan);
10108 : :
10109 : 312 : signbit = build_call_expr_loc (input_location,
10110 : : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10111 : : 1, arg);
10112 : 312 : signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10113 : : signbit, integer_zero_node);
10114 : :
10115 : 312 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10116 : : logical_type_node, signbit,
10117 : : fold_build1_loc (input_location, TRUTH_NOT_EXPR,
10118 : 312 : TREE_TYPE(isnan), isnan));
10119 : :
10120 : 312 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10121 : 312 : }
10122 : :
10123 : :
10124 : : /* Generate code for IEEE_LOGB and IEEE_RINT. */
10125 : :
10126 : : static void
10127 : 240 : conv_intrinsic_ieee_logb_rint (gfc_se * se, gfc_expr * expr,
10128 : : enum built_in_function code)
10129 : : {
10130 : 240 : tree arg, decl, call, fpstate;
10131 : 240 : int argprec;
10132 : :
10133 : 240 : conv_ieee_function_args (se, expr, &arg, 1);
10134 : 240 : argprec = TYPE_PRECISION (TREE_TYPE (arg));
10135 : 240 : decl = builtin_decl_for_precision (code, argprec);
10136 : :
10137 : : /* Save floating-point state. */
10138 : 240 : fpstate = gfc_save_fp_state (&se->pre);
10139 : :
10140 : : /* Make the function call. */
10141 : 240 : call = build_call_expr_loc (input_location, decl, 1, arg);
10142 : 240 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), call);
10143 : :
10144 : : /* Restore floating-point state. */
10145 : 240 : gfc_restore_fp_state (&se->post, fpstate);
10146 : 240 : }
10147 : :
10148 : :
10149 : : /* Generate code for IEEE_REM. */
10150 : :
10151 : : static void
10152 : 84 : conv_intrinsic_ieee_rem (gfc_se * se, gfc_expr * expr)
10153 : : {
10154 : 84 : tree args[2], decl, call, fpstate;
10155 : 84 : int argprec;
10156 : :
10157 : 84 : conv_ieee_function_args (se, expr, args, 2);
10158 : :
10159 : : /* If arguments have unequal size, convert them to the larger. */
10160 : 84 : if (TYPE_PRECISION (TREE_TYPE (args[0]))
10161 : 84 : > TYPE_PRECISION (TREE_TYPE (args[1])))
10162 : 6 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10163 : 78 : else if (TYPE_PRECISION (TREE_TYPE (args[1]))
10164 : 78 : > TYPE_PRECISION (TREE_TYPE (args[0])))
10165 : 24 : args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
10166 : :
10167 : 84 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10168 : 84 : decl = builtin_decl_for_precision (BUILT_IN_REMAINDER, argprec);
10169 : :
10170 : : /* Save floating-point state. */
10171 : 84 : fpstate = gfc_save_fp_state (&se->pre);
10172 : :
10173 : : /* Make the function call. */
10174 : 84 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10175 : 84 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10176 : :
10177 : : /* Restore floating-point state. */
10178 : 84 : gfc_restore_fp_state (&se->post, fpstate);
10179 : 84 : }
10180 : :
10181 : :
10182 : : /* Generate code for IEEE_NEXT_AFTER. */
10183 : :
10184 : : static void
10185 : 180 : conv_intrinsic_ieee_next_after (gfc_se * se, gfc_expr * expr)
10186 : : {
10187 : 180 : tree args[2], decl, call, fpstate;
10188 : 180 : int argprec;
10189 : :
10190 : 180 : conv_ieee_function_args (se, expr, args, 2);
10191 : :
10192 : : /* Result has the characteristics of first argument. */
10193 : 180 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10194 : 180 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10195 : 180 : decl = builtin_decl_for_precision (BUILT_IN_NEXTAFTER, argprec);
10196 : :
10197 : : /* Save floating-point state. */
10198 : 180 : fpstate = gfc_save_fp_state (&se->pre);
10199 : :
10200 : : /* Make the function call. */
10201 : 180 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10202 : 180 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10203 : :
10204 : : /* Restore floating-point state. */
10205 : 180 : gfc_restore_fp_state (&se->post, fpstate);
10206 : 180 : }
10207 : :
10208 : :
10209 : : /* Generate code for IEEE_SCALB. */
10210 : :
10211 : : static void
10212 : 228 : conv_intrinsic_ieee_scalb (gfc_se * se, gfc_expr * expr)
10213 : : {
10214 : 228 : tree args[2], decl, call, huge, type;
10215 : 228 : int argprec, n;
10216 : :
10217 : 228 : conv_ieee_function_args (se, expr, args, 2);
10218 : :
10219 : : /* Result has the characteristics of first argument. */
10220 : 228 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10221 : 228 : decl = builtin_decl_for_precision (BUILT_IN_SCALBN, argprec);
10222 : :
10223 : 228 : if (TYPE_PRECISION (TREE_TYPE (args[1])) > TYPE_PRECISION (integer_type_node))
10224 : : {
10225 : : /* We need to fold the integer into the range of a C int. */
10226 : 18 : args[1] = gfc_evaluate_now (args[1], &se->pre);
10227 : 18 : type = TREE_TYPE (args[1]);
10228 : :
10229 : 18 : n = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
10230 : 18 : huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
10231 : : gfc_c_int_kind);
10232 : 18 : huge = fold_convert (type, huge);
10233 : 18 : args[1] = fold_build2_loc (input_location, MIN_EXPR, type, args[1],
10234 : : huge);
10235 : 18 : args[1] = fold_build2_loc (input_location, MAX_EXPR, type, args[1],
10236 : : fold_build1_loc (input_location, NEGATE_EXPR,
10237 : : type, huge));
10238 : : }
10239 : :
10240 : 228 : args[1] = fold_convert (integer_type_node, args[1]);
10241 : :
10242 : : /* Make the function call. */
10243 : 228 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10244 : 228 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10245 : 228 : }
10246 : :
10247 : :
10248 : : /* Generate code for IEEE_COPY_SIGN. */
10249 : :
10250 : : static void
10251 : 576 : conv_intrinsic_ieee_copy_sign (gfc_se * se, gfc_expr * expr)
10252 : : {
10253 : 576 : tree args[2], decl, sign;
10254 : 576 : int argprec;
10255 : :
10256 : 576 : conv_ieee_function_args (se, expr, args, 2);
10257 : :
10258 : : /* Get the sign of the second argument. */
10259 : 576 : sign = build_call_expr_loc (input_location,
10260 : : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10261 : : 1, args[1]);
10262 : 576 : sign = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10263 : : sign, integer_zero_node);
10264 : :
10265 : : /* Create a value of one, with the right sign. */
10266 : 576 : sign = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
10267 : : sign,
10268 : : fold_build1_loc (input_location, NEGATE_EXPR,
10269 : : integer_type_node,
10270 : : integer_one_node),
10271 : : integer_one_node);
10272 : 576 : args[1] = fold_convert (TREE_TYPE (args[0]), sign);
10273 : :
10274 : 576 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10275 : 576 : decl = builtin_decl_for_precision (BUILT_IN_COPYSIGN, argprec);
10276 : :
10277 : 576 : se->expr = build_call_expr_loc_array (input_location, decl, 2, args);
10278 : 576 : }
10279 : :
10280 : :
10281 : : /* Generate code for IEEE_CLASS. */
10282 : :
10283 : : static void
10284 : 648 : conv_intrinsic_ieee_class (gfc_se *se, gfc_expr *expr)
10285 : : {
10286 : 648 : tree arg, c, t1, t2, t3, t4;
10287 : :
10288 : : /* Convert arg, evaluate it only once. */
10289 : 648 : conv_ieee_function_args (se, expr, &arg, 1);
10290 : 648 : arg = gfc_evaluate_now (arg, &se->pre);
10291 : :
10292 : 648 : c = build_call_expr_loc (input_location,
10293 : : builtin_decl_explicit (BUILT_IN_FPCLASSIFY), 6,
10294 : : build_int_cst (integer_type_node, IEEE_QUIET_NAN),
10295 : : build_int_cst (integer_type_node,
10296 : : IEEE_POSITIVE_INF),
10297 : : build_int_cst (integer_type_node,
10298 : : IEEE_POSITIVE_NORMAL),
10299 : : build_int_cst (integer_type_node,
10300 : : IEEE_POSITIVE_DENORMAL),
10301 : : build_int_cst (integer_type_node,
10302 : : IEEE_POSITIVE_ZERO),
10303 : : arg);
10304 : 648 : c = gfc_evaluate_now (c, &se->pre);
10305 : 648 : t1 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10306 : : c, build_int_cst (integer_type_node,
10307 : : IEEE_QUIET_NAN));
10308 : 648 : t2 = build_call_expr_loc (input_location,
10309 : : builtin_decl_explicit (BUILT_IN_ISSIGNALING), 1,
10310 : : arg);
10311 : 648 : t2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10312 : 648 : t2, build_zero_cst (TREE_TYPE (t2)));
10313 : 648 : t1 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10314 : : logical_type_node, t1, t2);
10315 : 648 : t3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10316 : : c, build_int_cst (integer_type_node,
10317 : : IEEE_POSITIVE_ZERO));
10318 : 648 : t4 = build_call_expr_loc (input_location,
10319 : : builtin_decl_explicit (BUILT_IN_SIGNBIT), 1,
10320 : : arg);
10321 : 648 : t4 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10322 : 648 : t4, build_zero_cst (TREE_TYPE (t4)));
10323 : 648 : t3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10324 : : logical_type_node, t3, t4);
10325 : 648 : int s = IEEE_NEGATIVE_ZERO + IEEE_POSITIVE_ZERO;
10326 : 648 : gcc_assert (IEEE_NEGATIVE_INF == s - IEEE_POSITIVE_INF);
10327 : 648 : gcc_assert (IEEE_NEGATIVE_NORMAL == s - IEEE_POSITIVE_NORMAL);
10328 : 648 : gcc_assert (IEEE_NEGATIVE_DENORMAL == s - IEEE_POSITIVE_DENORMAL);
10329 : 648 : gcc_assert (IEEE_NEGATIVE_SUBNORMAL == s - IEEE_POSITIVE_SUBNORMAL);
10330 : 648 : gcc_assert (IEEE_NEGATIVE_ZERO == s - IEEE_POSITIVE_ZERO);
10331 : 648 : t4 = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (c),
10332 : 648 : build_int_cst (TREE_TYPE (c), s), c);
10333 : 648 : t3 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c),
10334 : : t3, t4, c);
10335 : 648 : t1 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c), t1,
10336 : 648 : build_int_cst (TREE_TYPE (c), IEEE_SIGNALING_NAN),
10337 : : t3);
10338 : 648 : tree type = gfc_typenode_for_spec (&expr->ts);
10339 : : /* Perform a quick sanity check that the return type is
10340 : : IEEE_CLASS_TYPE derived type defined in
10341 : : libgfortran/ieee/ieee_arithmetic.F90
10342 : : Primarily check that it is a derived type with a single
10343 : : member in it. */
10344 : 648 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10345 : 648 : tree field = NULL_TREE;
10346 : 1296 : for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10347 : 648 : if (TREE_CODE (f) == FIELD_DECL)
10348 : : {
10349 : 648 : gcc_assert (field == NULL_TREE);
10350 : : field = f;
10351 : : }
10352 : 648 : gcc_assert (field);
10353 : 648 : t1 = fold_convert (TREE_TYPE (field), t1);
10354 : 648 : se->expr = build_constructor_single (type, field, t1);
10355 : 648 : }
10356 : :
10357 : :
10358 : : /* Generate code for IEEE_VALUE. */
10359 : :
10360 : : static void
10361 : 1111 : conv_intrinsic_ieee_value (gfc_se *se, gfc_expr *expr)
10362 : : {
10363 : 1111 : tree args[2], arg, ret, tmp;
10364 : 1111 : stmtblock_t body;
10365 : :
10366 : : /* Convert args, evaluate the second one only once. */
10367 : 1111 : conv_ieee_function_args (se, expr, args, 2);
10368 : 1111 : arg = gfc_evaluate_now (args[1], &se->pre);
10369 : :
10370 : 1111 : tree type = TREE_TYPE (arg);
10371 : : /* Perform a quick sanity check that the second argument's type is
10372 : : IEEE_CLASS_TYPE derived type defined in
10373 : : libgfortran/ieee/ieee_arithmetic.F90
10374 : : Primarily check that it is a derived type with a single
10375 : : member in it. */
10376 : 1111 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10377 : 1111 : tree field = NULL_TREE;
10378 : 2222 : for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10379 : 1111 : if (TREE_CODE (f) == FIELD_DECL)
10380 : : {
10381 : 1111 : gcc_assert (field == NULL_TREE);
10382 : : field = f;
10383 : : }
10384 : 1111 : gcc_assert (field);
10385 : 1111 : arg = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
10386 : : arg, field, NULL_TREE);
10387 : 1111 : arg = gfc_evaluate_now (arg, &se->pre);
10388 : :
10389 : 1111 : type = gfc_typenode_for_spec (&expr->ts);
10390 : 1111 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
10391 : 1111 : ret = gfc_create_var (type, NULL);
10392 : :
10393 : 1111 : gfc_init_block (&body);
10394 : :
10395 : 1111 : tree end_label = gfc_build_label_decl (NULL_TREE);
10396 : 12221 : for (int c = IEEE_SIGNALING_NAN; c <= IEEE_POSITIVE_INF; ++c)
10397 : : {
10398 : 11110 : tree label = gfc_build_label_decl (NULL_TREE);
10399 : 11110 : tree low = build_int_cst (TREE_TYPE (arg), c);
10400 : 11110 : tmp = build_case_label (low, low, label);
10401 : 11110 : gfc_add_expr_to_block (&body, tmp);
10402 : :
10403 : 11110 : REAL_VALUE_TYPE real;
10404 : 11110 : int k;
10405 : 11110 : switch (c)
10406 : : {
10407 : 1111 : case IEEE_SIGNALING_NAN:
10408 : 1111 : real_nan (&real, "", 0, TYPE_MODE (type));
10409 : 1111 : break;
10410 : 1111 : case IEEE_QUIET_NAN:
10411 : 1111 : real_nan (&real, "", 1, TYPE_MODE (type));
10412 : 1111 : break;
10413 : 1111 : case IEEE_NEGATIVE_INF:
10414 : 1111 : real_inf (&real);
10415 : 1111 : real = real_value_negate (&real);
10416 : 1111 : break;
10417 : 1111 : case IEEE_NEGATIVE_NORMAL:
10418 : 1111 : real_from_integer (&real, TYPE_MODE (type), -42, SIGNED);
10419 : 1111 : break;
10420 : 1111 : case IEEE_NEGATIVE_DENORMAL:
10421 : 1111 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10422 : 1111 : real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10423 : : type, GFC_RND_MODE);
10424 : 1111 : real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10425 : 1111 : real = real_value_negate (&real);
10426 : 1111 : break;
10427 : 1111 : case IEEE_NEGATIVE_ZERO:
10428 : 1111 : real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10429 : 1111 : real = real_value_negate (&real);
10430 : 1111 : break;
10431 : 1111 : case IEEE_POSITIVE_ZERO:
10432 : : /* Make this also the default: label. The other possibility
10433 : : would be to add a separate default: label followed by
10434 : : __builtin_unreachable (). */
10435 : 1111 : label = gfc_build_label_decl (NULL_TREE);
10436 : 1111 : tmp = build_case_label (NULL_TREE, NULL_TREE, label);
10437 : 1111 : gfc_add_expr_to_block (&body, tmp);
10438 : 1111 : real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10439 : 1111 : break;
10440 : 1111 : case IEEE_POSITIVE_DENORMAL:
10441 : 1111 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10442 : 1111 : real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10443 : : type, GFC_RND_MODE);
10444 : 1111 : real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10445 : 1111 : break;
10446 : 1111 : case IEEE_POSITIVE_NORMAL:
10447 : 1111 : real_from_integer (&real, TYPE_MODE (type), 42, SIGNED);
10448 : 1111 : break;
10449 : 1111 : case IEEE_POSITIVE_INF:
10450 : 1111 : real_inf (&real);
10451 : 1111 : break;
10452 : : default:
10453 : : gcc_unreachable ();
10454 : : }
10455 : :
10456 : 11110 : tree val = build_real (type, real);
10457 : 11110 : gfc_add_modify (&body, ret, val);
10458 : :
10459 : 11110 : tmp = build1_v (GOTO_EXPR, end_label);
10460 : 11110 : gfc_add_expr_to_block (&body, tmp);
10461 : : }
10462 : :
10463 : 1111 : tmp = gfc_finish_block (&body);
10464 : 1111 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, arg, tmp);
10465 : 1111 : gfc_add_expr_to_block (&se->pre, tmp);
10466 : :
10467 : 1111 : tmp = build1_v (LABEL_EXPR, end_label);
10468 : 1111 : gfc_add_expr_to_block (&se->pre, tmp);
10469 : :
10470 : 1111 : se->expr = ret;
10471 : 1111 : }
10472 : :
10473 : :
10474 : : /* Generate code for IEEE_FMA. */
10475 : :
10476 : : static void
10477 : 120 : conv_intrinsic_ieee_fma (gfc_se * se, gfc_expr * expr)
10478 : : {
10479 : 120 : tree args[3], decl, call;
10480 : 120 : int argprec;
10481 : :
10482 : 120 : conv_ieee_function_args (se, expr, args, 3);
10483 : :
10484 : : /* All three arguments should have the same type. */
10485 : 120 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10486 : 120 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[2])));
10487 : :
10488 : : /* Call the type-generic FMA built-in. */
10489 : 120 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10490 : 120 : decl = builtin_decl_for_precision (BUILT_IN_FMA, argprec);
10491 : 120 : call = build_call_expr_loc_array (input_location, decl, 3, args);
10492 : :
10493 : : /* Convert to the final type. */
10494 : 120 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10495 : 120 : }
10496 : :
10497 : :
10498 : : /* Generate code for IEEE_{MIN,MAX}_NUM{,_MAG}. */
10499 : :
10500 : : static void
10501 : 3072 : conv_intrinsic_ieee_minmax (gfc_se * se, gfc_expr * expr, int max,
10502 : : const char *name)
10503 : : {
10504 : 3072 : tree args[2], func;
10505 : 3072 : built_in_function fn;
10506 : :
10507 : 3072 : conv_ieee_function_args (se, expr, args, 2);
10508 : 3072 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10509 : 3072 : args[0] = gfc_evaluate_now (args[0], &se->pre);
10510 : 3072 : args[1] = gfc_evaluate_now (args[1], &se->pre);
10511 : :
10512 : 3072 : if (startswith (name, "mag"))
10513 : : {
10514 : : /* IEEE_MIN_NUM_MAG and IEEE_MAX_NUM_MAG translate to C functions
10515 : : fminmag() and fmaxmag(), which do not exist as built-ins.
10516 : :
10517 : : Following glibc, we emit this:
10518 : :
10519 : : fminmag (x, y) {
10520 : : ax = ABS (x);
10521 : : ay = ABS (y);
10522 : : if (isless (ax, ay))
10523 : : return x;
10524 : : else if (isgreater (ax, ay))
10525 : : return y;
10526 : : else if (ax == ay)
10527 : : return x < y ? x : y;
10528 : : else if (issignaling (x) || issignaling (y))
10529 : : return x + y;
10530 : : else
10531 : : return isnan (y) ? x : y;
10532 : : }
10533 : :
10534 : : fmaxmag (x, y) {
10535 : : ax = ABS (x);
10536 : : ay = ABS (y);
10537 : : if (isgreater (ax, ay))
10538 : : return x;
10539 : : else if (isless (ax, ay))
10540 : : return y;
10541 : : else if (ax == ay)
10542 : : return x > y ? x : y;
10543 : : else if (issignaling (x) || issignaling (y))
10544 : : return x + y;
10545 : : else
10546 : : return isnan (y) ? x : y;
10547 : : }
10548 : :
10549 : : */
10550 : :
10551 : 1536 : tree abs0, abs1, sig0, sig1;
10552 : 1536 : tree cond1, cond2, cond3, cond4, cond5;
10553 : 1536 : tree res;
10554 : 1536 : tree type = TREE_TYPE (args[0]);
10555 : :
10556 : 1536 : func = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
10557 : 1536 : abs0 = build_call_expr_loc (input_location, func, 1, args[0]);
10558 : 1536 : abs1 = build_call_expr_loc (input_location, func, 1, args[1]);
10559 : 1536 : abs0 = gfc_evaluate_now (abs0, &se->pre);
10560 : 1536 : abs1 = gfc_evaluate_now (abs1, &se->pre);
10561 : :
10562 : 1536 : cond5 = build_call_expr_loc (input_location,
10563 : : builtin_decl_explicit (BUILT_IN_ISNAN),
10564 : : 1, args[1]);
10565 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond5,
10566 : : args[0], args[1]);
10567 : :
10568 : 1536 : sig0 = build_call_expr_loc (input_location,
10569 : : builtin_decl_explicit (BUILT_IN_ISSIGNALING),
10570 : : 1, args[0]);
10571 : 1536 : sig1 = build_call_expr_loc (input_location,
10572 : : builtin_decl_explicit (BUILT_IN_ISSIGNALING),
10573 : : 1, args[1]);
10574 : 1536 : cond4 = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
10575 : : logical_type_node, sig0, sig1);
10576 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond4,
10577 : : fold_build2_loc (input_location, PLUS_EXPR,
10578 : : type, args[0], args[1]),
10579 : : res);
10580 : :
10581 : 1536 : cond3 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10582 : : abs0, abs1);
10583 : 2304 : res = fold_build3_loc (input_location, COND_EXPR, type, cond3,
10584 : : fold_build2_loc (input_location,
10585 : : max ? MAX_EXPR : MIN_EXPR,
10586 : : type, args[0], args[1]),
10587 : : res);
10588 : :
10589 : 2304 : func = builtin_decl_explicit (max ? BUILT_IN_ISLESS : BUILT_IN_ISGREATER);
10590 : 1536 : cond2 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
10591 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond2,
10592 : : args[1], res);
10593 : :
10594 : 2304 : func = builtin_decl_explicit (max ? BUILT_IN_ISGREATER : BUILT_IN_ISLESS);
10595 : 1536 : cond1 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
10596 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond1,
10597 : : args[0], res);
10598 : :
10599 : 1536 : se->expr = res;
10600 : : }
10601 : : else
10602 : : {
10603 : : /* IEEE_MIN_NUM and IEEE_MAX_NUM translate to fmin() and fmax(). */
10604 : 1536 : fn = max ? BUILT_IN_FMAX : BUILT_IN_FMIN;
10605 : 1536 : func = gfc_builtin_decl_for_float_kind (fn, expr->ts.kind);
10606 : 1536 : se->expr = build_call_expr_loc_array (input_location, func, 2, args);
10607 : : }
10608 : 3072 : }
10609 : :
10610 : :
10611 : : /* Generate code for comparison functions IEEE_QUIET_* and
10612 : : IEEE_SIGNALING_*. */
10613 : :
10614 : : static void
10615 : 3888 : conv_intrinsic_ieee_comparison (gfc_se * se, gfc_expr * expr, int signaling,
10616 : : const char *name)
10617 : : {
10618 : 3888 : tree args[2];
10619 : 3888 : tree arg1, arg2, res;
10620 : :
10621 : : /* Evaluate arguments only once. */
10622 : 3888 : conv_ieee_function_args (se, expr, args, 2);
10623 : 3888 : arg1 = gfc_evaluate_now (args[0], &se->pre);
10624 : 3888 : arg2 = gfc_evaluate_now (args[1], &se->pre);
10625 : :
10626 : 3888 : if (startswith (name, "eq"))
10627 : : {
10628 : 648 : if (signaling)
10629 : 324 : res = build_call_expr_loc (input_location,
10630 : : builtin_decl_explicit (BUILT_IN_ISEQSIG),
10631 : : 2, arg1, arg2);
10632 : : else
10633 : 324 : res = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10634 : : arg1, arg2);
10635 : : }
10636 : 3240 : else if (startswith (name, "ne"))
10637 : : {
10638 : 648 : if (signaling)
10639 : : {
10640 : 324 : res = build_call_expr_loc (input_location,
10641 : : builtin_decl_explicit (BUILT_IN_ISEQSIG),
10642 : : 2, arg1, arg2);
10643 : 324 : res = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
10644 : : logical_type_node, res);
10645 : : }
10646 : : else
10647 : 324 : res = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10648 : : arg1, arg2);
10649 : : }
10650 : 2592 : else if (startswith (name, "ge"))
10651 : : {
10652 : 648 : if (signaling)
10653 : 324 : res = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10654 : : arg1, arg2);
10655 : : else
10656 : 324 : res = build_call_expr_loc (input_location,
10657 : : builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL),
10658 : : 2, arg1, arg2);
10659 : : }
10660 : 1944 : else if (startswith (name, "gt"))
10661 : : {
10662 : 648 : if (signaling)
10663 : 324 : res = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
10664 : : arg1, arg2);
10665 : : else
10666 : 324 : res = build_call_expr_loc (input_location,
10667 : : builtin_decl_explicit (BUILT_IN_ISGREATER),
10668 : : 2, arg1, arg2);
10669 : : }
10670 : 1296 : else if (startswith (name, "le"))
10671 : : {
10672 : 648 : if (signaling)
10673 : 324 : res = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
10674 : : arg1, arg2);
10675 : : else
10676 : 324 : res = build_call_expr_loc (input_location,
10677 : : builtin_decl_explicit (BUILT_IN_ISLESSEQUAL),
10678 : : 2, arg1, arg2);
10679 : : }
10680 : 648 : else if (startswith (name, "lt"))
10681 : : {
10682 : 648 : if (signaling)
10683 : 324 : res = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
10684 : : arg1, arg2);
10685 : : else
10686 : 324 : res = build_call_expr_loc (input_location,
10687 : : builtin_decl_explicit (BUILT_IN_ISLESS),
10688 : : 2, arg1, arg2);
10689 : : }
10690 : : else
10691 : 0 : gcc_unreachable ();
10692 : :
10693 : 3888 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), res);
10694 : 3888 : }
10695 : :
10696 : :
10697 : : /* Generate code for an intrinsic function from the IEEE_ARITHMETIC
10698 : : module. */
10699 : :
10700 : : bool
10701 : 13939 : gfc_conv_ieee_arithmetic_function (gfc_se * se, gfc_expr * expr)
10702 : : {
10703 : 13939 : const char *name = expr->value.function.name;
10704 : :
10705 : 13939 : if (startswith (name, "_gfortran_ieee_is_nan"))
10706 : 522 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISNAN, 1);
10707 : 13417 : else if (startswith (name, "_gfortran_ieee_is_finite"))
10708 : 372 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISFINITE, 1);
10709 : 13045 : else if (startswith (name, "_gfortran_ieee_unordered"))
10710 : 168 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISUNORDERED, 2);
10711 : 12877 : else if (startswith (name, "_gfortran_ieee_signbit"))
10712 : 624 : conv_intrinsic_ieee_signbit (se, expr);
10713 : 12253 : else if (startswith (name, "_gfortran_ieee_is_normal"))
10714 : 312 : conv_intrinsic_ieee_is_normal (se, expr);
10715 : 11941 : else if (startswith (name, "_gfortran_ieee_is_negative"))
10716 : 312 : conv_intrinsic_ieee_is_negative (se, expr);
10717 : 11629 : else if (startswith (name, "_gfortran_ieee_copy_sign"))
10718 : 576 : conv_intrinsic_ieee_copy_sign (se, expr);
10719 : 11053 : else if (startswith (name, "_gfortran_ieee_scalb"))
10720 : 228 : conv_intrinsic_ieee_scalb (se, expr);
10721 : 10825 : else if (startswith (name, "_gfortran_ieee_next_after"))
10722 : 180 : conv_intrinsic_ieee_next_after (se, expr);
10723 : 10645 : else if (startswith (name, "_gfortran_ieee_rem"))
10724 : 84 : conv_intrinsic_ieee_rem (se, expr);
10725 : 10561 : else if (startswith (name, "_gfortran_ieee_logb"))
10726 : 144 : conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_LOGB);
10727 : 10417 : else if (startswith (name, "_gfortran_ieee_rint"))
10728 : 96 : conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_RINT);
10729 : 10321 : else if (startswith (name, "ieee_class_") && ISDIGIT (name[11]))
10730 : 648 : conv_intrinsic_ieee_class (se, expr);
10731 : 9673 : else if (startswith (name, "ieee_value_") && ISDIGIT (name[11]))
10732 : 1111 : conv_intrinsic_ieee_value (se, expr);
10733 : 8562 : else if (startswith (name, "_gfortran_ieee_fma"))
10734 : 120 : conv_intrinsic_ieee_fma (se, expr);
10735 : 8442 : else if (startswith (name, "_gfortran_ieee_min_num_"))
10736 : 1536 : conv_intrinsic_ieee_minmax (se, expr, 0, name + 23);
10737 : 6906 : else if (startswith (name, "_gfortran_ieee_max_num_"))
10738 : 1536 : conv_intrinsic_ieee_minmax (se, expr, 1, name + 23);
10739 : 5370 : else if (startswith (name, "_gfortran_ieee_quiet_"))
10740 : 1944 : conv_intrinsic_ieee_comparison (se, expr, 0, name + 21);
10741 : 3426 : else if (startswith (name, "_gfortran_ieee_signaling_"))
10742 : 1944 : conv_intrinsic_ieee_comparison (se, expr, 1, name + 25);
10743 : : else
10744 : : /* It is not among the functions we translate directly. We return
10745 : : false, so a library function call is emitted. */
10746 : : return false;
10747 : :
10748 : : return true;
10749 : : }
10750 : :
10751 : :
10752 : : /* Generate a direct call to malloc() for the MALLOC intrinsic. */
10753 : :
10754 : : static void
10755 : 16 : gfc_conv_intrinsic_malloc (gfc_se * se, gfc_expr * expr)
10756 : : {
10757 : 16 : tree arg, res, restype;
10758 : :
10759 : 16 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
10760 : 16 : arg = fold_convert (size_type_node, arg);
10761 : 16 : res = build_call_expr_loc (input_location,
10762 : : builtin_decl_explicit (BUILT_IN_MALLOC), 1, arg);
10763 : 16 : restype = gfc_typenode_for_spec (&expr->ts);
10764 : 16 : se->expr = fold_convert (restype, res);
10765 : 16 : }
10766 : :
10767 : :
10768 : : /* Generate code for an intrinsic function. Some map directly to library
10769 : : calls, others get special handling. In some cases the name of the function
10770 : : used depends on the type specifiers. */
10771 : :
10772 : : void
10773 : 251594 : gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
10774 : : {
10775 : 251594 : const char *name;
10776 : 251594 : int lib, kind;
10777 : 251594 : tree fndecl;
10778 : :
10779 : 251594 : name = &expr->value.function.name[2];
10780 : :
10781 : 251594 : if (expr->rank > 0)
10782 : : {
10783 : 50724 : lib = gfc_is_intrinsic_libcall (expr);
10784 : 50724 : if (lib != 0)
10785 : : {
10786 : 19269 : if (lib == 1)
10787 : 11672 : se->ignore_optional = 1;
10788 : :
10789 : 19269 : switch (expr->value.function.isym->id)
10790 : : {
10791 : 6074 : case GFC_ISYM_EOSHIFT:
10792 : 6074 : case GFC_ISYM_PACK:
10793 : 6074 : case GFC_ISYM_RESHAPE:
10794 : 6074 : case GFC_ISYM_REDUCE:
10795 : : /* For all of those the first argument specifies the type and the
10796 : : third is optional. */
10797 : 6074 : conv_generic_with_optional_char_arg (se, expr, 1, 3);
10798 : 6074 : break;
10799 : :
10800 : 1116 : case GFC_ISYM_FINDLOC:
10801 : 1116 : gfc_conv_intrinsic_findloc (se, expr);
10802 : 1116 : break;
10803 : :
10804 : 2935 : case GFC_ISYM_MINLOC:
10805 : 2935 : gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
10806 : 2935 : break;
10807 : :
10808 : 2439 : case GFC_ISYM_MAXLOC:
10809 : 2439 : gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
10810 : 2439 : break;
10811 : :
10812 : 6705 : default:
10813 : 6705 : gfc_conv_intrinsic_funcall (se, expr);
10814 : 6705 : break;
10815 : : }
10816 : :
10817 : 19269 : return;
10818 : : }
10819 : : }
10820 : :
10821 : 232325 : switch (expr->value.function.isym->id)
10822 : : {
10823 : 0 : case GFC_ISYM_NONE:
10824 : 0 : gcc_unreachable ();
10825 : :
10826 : 529 : case GFC_ISYM_REPEAT:
10827 : 529 : gfc_conv_intrinsic_repeat (se, expr);
10828 : 529 : break;
10829 : :
10830 : 574 : case GFC_ISYM_TRIM:
10831 : 574 : gfc_conv_intrinsic_trim (se, expr);
10832 : 574 : break;
10833 : :
10834 : 42 : case GFC_ISYM_SC_KIND:
10835 : 42 : gfc_conv_intrinsic_sc_kind (se, expr);
10836 : 42 : break;
10837 : :
10838 : 45 : case GFC_ISYM_SI_KIND:
10839 : 45 : gfc_conv_intrinsic_si_kind (se, expr);
10840 : 45 : break;
10841 : :
10842 : 6 : case GFC_ISYM_SL_KIND:
10843 : 6 : gfc_conv_intrinsic_sl_kind (se, expr);
10844 : 6 : break;
10845 : :
10846 : 82 : case GFC_ISYM_SR_KIND:
10847 : 82 : gfc_conv_intrinsic_sr_kind (se, expr);
10848 : 82 : break;
10849 : :
10850 : 228 : case GFC_ISYM_EXPONENT:
10851 : 228 : gfc_conv_intrinsic_exponent (se, expr);
10852 : 228 : break;
10853 : :
10854 : 316 : case GFC_ISYM_SCAN:
10855 : 316 : kind = expr->value.function.actual->expr->ts.kind;
10856 : 316 : if (kind == 1)
10857 : 250 : fndecl = gfor_fndecl_string_scan;
10858 : 66 : else if (kind == 4)
10859 : 66 : fndecl = gfor_fndecl_string_scan_char4;
10860 : : else
10861 : 0 : gcc_unreachable ();
10862 : :
10863 : 316 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
10864 : 316 : break;
10865 : :
10866 : 94 : case GFC_ISYM_VERIFY:
10867 : 94 : kind = expr->value.function.actual->expr->ts.kind;
10868 : 94 : if (kind == 1)
10869 : 70 : fndecl = gfor_fndecl_string_verify;
10870 : 24 : else if (kind == 4)
10871 : 24 : fndecl = gfor_fndecl_string_verify_char4;
10872 : : else
10873 : 0 : gcc_unreachable ();
10874 : :
10875 : 94 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
10876 : 94 : break;
10877 : :
10878 : 6966 : case GFC_ISYM_ALLOCATED:
10879 : 6966 : gfc_conv_allocated (se, expr);
10880 : 6966 : break;
10881 : :
10882 : 8844 : case GFC_ISYM_ASSOCIATED:
10883 : 8844 : gfc_conv_associated(se, expr);
10884 : 8844 : break;
10885 : :
10886 : 403 : case GFC_ISYM_SAME_TYPE_AS:
10887 : 403 : gfc_conv_same_type_as (se, expr);
10888 : 403 : break;
10889 : :
10890 : 7723 : case GFC_ISYM_ABS:
10891 : 7723 : gfc_conv_intrinsic_abs (se, expr);
10892 : 7723 : break;
10893 : :
10894 : 351 : case GFC_ISYM_ADJUSTL:
10895 : 351 : if (expr->ts.kind == 1)
10896 : 297 : fndecl = gfor_fndecl_adjustl;
10897 : 54 : else if (expr->ts.kind == 4)
10898 : 54 : fndecl = gfor_fndecl_adjustl_char4;
10899 : : else
10900 : 0 : gcc_unreachable ();
10901 : :
10902 : 351 : gfc_conv_intrinsic_adjust (se, expr, fndecl);
10903 : 351 : break;
10904 : :
10905 : 111 : case GFC_ISYM_ADJUSTR:
10906 : 111 : if (expr->ts.kind == 1)
10907 : 56 : fndecl = gfor_fndecl_adjustr;
10908 : 55 : else if (expr->ts.kind == 4)
10909 : 55 : fndecl = gfor_fndecl_adjustr_char4;
10910 : : else
10911 : 0 : gcc_unreachable ();
10912 : :
10913 : 111 : gfc_conv_intrinsic_adjust (se, expr, fndecl);
10914 : 111 : break;
10915 : :
10916 : 428 : case GFC_ISYM_AIMAG:
10917 : 428 : gfc_conv_intrinsic_imagpart (se, expr);
10918 : 428 : break;
10919 : :
10920 : 146 : case GFC_ISYM_AINT:
10921 : 146 : gfc_conv_intrinsic_aint (se, expr, RND_TRUNC);
10922 : 146 : break;
10923 : :
10924 : 419 : case GFC_ISYM_ALL:
10925 : 419 : gfc_conv_intrinsic_anyall (se, expr, EQ_EXPR);
10926 : 419 : break;
10927 : :
10928 : 74 : case GFC_ISYM_ANINT:
10929 : 74 : gfc_conv_intrinsic_aint (se, expr, RND_ROUND);
10930 : 74 : break;
10931 : :
10932 : 90 : case GFC_ISYM_AND:
10933 : 90 : gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
10934 : 90 : break;
10935 : :
10936 : 36533 : case GFC_ISYM_ANY:
10937 : 36533 : gfc_conv_intrinsic_anyall (se, expr, NE_EXPR);
10938 : 36533 : break;
10939 : :
10940 : 216 : case GFC_ISYM_ACOSD:
10941 : 216 : case GFC_ISYM_ASIND:
10942 : 216 : case GFC_ISYM_ATAND:
10943 : 216 : gfc_conv_intrinsic_atrigd (se, expr, expr->value.function.isym->id);
10944 : 216 : break;
10945 : :
10946 : 102 : case GFC_ISYM_COTAN:
10947 : 102 : gfc_conv_intrinsic_cotan (se, expr);
10948 : 102 : break;
10949 : :
10950 : 108 : case GFC_ISYM_COTAND:
10951 : 108 : gfc_conv_intrinsic_cotand (se, expr);
10952 : 108 : break;
10953 : :
10954 : 120 : case GFC_ISYM_ATAN2D:
10955 : 120 : gfc_conv_intrinsic_atan2d (se, expr);
10956 : 120 : break;
10957 : :
10958 : 145 : case GFC_ISYM_BTEST:
10959 : 145 : gfc_conv_intrinsic_btest (se, expr);
10960 : 145 : break;
10961 : :
10962 : 54 : case GFC_ISYM_BGE:
10963 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, GE_EXPR);
10964 : 54 : break;
10965 : :
10966 : 54 : case GFC_ISYM_BGT:
10967 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, GT_EXPR);
10968 : 54 : break;
10969 : :
10970 : 54 : case GFC_ISYM_BLE:
10971 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, LE_EXPR);
10972 : 54 : break;
10973 : :
10974 : 54 : case GFC_ISYM_BLT:
10975 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, LT_EXPR);
10976 : 54 : break;
10977 : :
10978 : 8953 : case GFC_ISYM_C_ASSOCIATED:
10979 : 8953 : case GFC_ISYM_C_FUNLOC:
10980 : 8953 : case GFC_ISYM_C_LOC:
10981 : 8953 : case GFC_ISYM_F_C_STRING:
10982 : 8953 : conv_isocbinding_function (se, expr);
10983 : 8953 : break;
10984 : :
10985 : 2020 : case GFC_ISYM_ACHAR:
10986 : 2020 : case GFC_ISYM_CHAR:
10987 : 2020 : gfc_conv_intrinsic_char (se, expr);
10988 : 2020 : break;
10989 : :
10990 : 37840 : case GFC_ISYM_CONVERSION:
10991 : 37840 : case GFC_ISYM_DBLE:
10992 : 37840 : case GFC_ISYM_DFLOAT:
10993 : 37840 : case GFC_ISYM_FLOAT:
10994 : 37840 : case GFC_ISYM_LOGICAL:
10995 : 37840 : case GFC_ISYM_REAL:
10996 : 37840 : case GFC_ISYM_REALPART:
10997 : 37840 : case GFC_ISYM_SNGL:
10998 : 37840 : gfc_conv_intrinsic_conversion (se, expr);
10999 : 37840 : break;
11000 : :
11001 : : /* Integer conversions are handled separately to make sure we get the
11002 : : correct rounding mode. */
11003 : 2676 : case GFC_ISYM_INT:
11004 : 2676 : case GFC_ISYM_INT2:
11005 : 2676 : case GFC_ISYM_INT8:
11006 : 2676 : case GFC_ISYM_LONG:
11007 : 2676 : case GFC_ISYM_UINT:
11008 : 2676 : gfc_conv_intrinsic_int (se, expr, RND_TRUNC);
11009 : 2676 : break;
11010 : :
11011 : 162 : case GFC_ISYM_NINT:
11012 : 162 : gfc_conv_intrinsic_int (se, expr, RND_ROUND);
11013 : 162 : break;
11014 : :
11015 : 16 : case GFC_ISYM_CEILING:
11016 : 16 : gfc_conv_intrinsic_int (se, expr, RND_CEIL);
11017 : 16 : break;
11018 : :
11019 : 116 : case GFC_ISYM_FLOOR:
11020 : 116 : gfc_conv_intrinsic_int (se, expr, RND_FLOOR);
11021 : 116 : break;
11022 : :
11023 : 2731 : case GFC_ISYM_MOD:
11024 : 2731 : gfc_conv_intrinsic_mod (se, expr, 0);
11025 : 2731 : break;
11026 : :
11027 : 434 : case GFC_ISYM_MODULO:
11028 : 434 : gfc_conv_intrinsic_mod (se, expr, 1);
11029 : 434 : break;
11030 : :
11031 : 894 : case GFC_ISYM_CAF_GET:
11032 : 894 : gfc_conv_intrinsic_caf_get (se, expr, NULL_TREE, false, NULL);
11033 : 894 : break;
11034 : :
11035 : 132 : case GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE:
11036 : 132 : gfc_conv_intrinsic_caf_is_present_remote (se, expr);
11037 : 132 : break;
11038 : :
11039 : 479 : case GFC_ISYM_CMPLX:
11040 : 479 : gfc_conv_intrinsic_cmplx (se, expr, name[5] == '1');
11041 : 479 : break;
11042 : :
11043 : 10 : case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
11044 : 10 : gfc_conv_intrinsic_iargc (se, expr);
11045 : 10 : break;
11046 : :
11047 : 6 : case GFC_ISYM_COMPLEX:
11048 : 6 : gfc_conv_intrinsic_cmplx (se, expr, 1);
11049 : 6 : break;
11050 : :
11051 : 255 : case GFC_ISYM_CONJG:
11052 : 255 : gfc_conv_intrinsic_conjg (se, expr);
11053 : 255 : break;
11054 : :
11055 : 143 : case GFC_ISYM_COUNT:
11056 : 143 : gfc_conv_intrinsic_count (se, expr);
11057 : 143 : break;
11058 : :
11059 : 0 : case GFC_ISYM_CTIME:
11060 : 0 : gfc_conv_intrinsic_ctime (se, expr);
11061 : 0 : break;
11062 : :
11063 : 96 : case GFC_ISYM_DIM:
11064 : 96 : gfc_conv_intrinsic_dim (se, expr);
11065 : 96 : break;
11066 : :
11067 : 113 : case GFC_ISYM_DOT_PRODUCT:
11068 : 113 : gfc_conv_intrinsic_dot_product (se, expr);
11069 : 113 : break;
11070 : :
11071 : 13 : case GFC_ISYM_DPROD:
11072 : 13 : gfc_conv_intrinsic_dprod (se, expr);
11073 : 13 : break;
11074 : :
11075 : 66 : case GFC_ISYM_DSHIFTL:
11076 : 66 : gfc_conv_intrinsic_dshift (se, expr, true);
11077 : 66 : break;
11078 : :
11079 : 66 : case GFC_ISYM_DSHIFTR:
11080 : 66 : gfc_conv_intrinsic_dshift (se, expr, false);
11081 : 66 : break;
11082 : :
11083 : 0 : case GFC_ISYM_FDATE:
11084 : 0 : gfc_conv_intrinsic_fdate (se, expr);
11085 : 0 : break;
11086 : :
11087 : 60 : case GFC_ISYM_FRACTION:
11088 : 60 : gfc_conv_intrinsic_fraction (se, expr);
11089 : 60 : break;
11090 : :
11091 : 24 : case GFC_ISYM_IALL:
11092 : 24 : gfc_conv_intrinsic_arith (se, expr, BIT_AND_EXPR, false);
11093 : 24 : break;
11094 : :
11095 : 600 : case GFC_ISYM_IAND:
11096 : 600 : gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
11097 : 600 : break;
11098 : :
11099 : 12 : case GFC_ISYM_IANY:
11100 : 12 : gfc_conv_intrinsic_arith (se, expr, BIT_IOR_EXPR, false);
11101 : 12 : break;
11102 : :
11103 : 168 : case GFC_ISYM_IBCLR:
11104 : 168 : gfc_conv_intrinsic_singlebitop (se, expr, 0);
11105 : 168 : break;
11106 : :
11107 : 27 : case GFC_ISYM_IBITS:
11108 : 27 : gfc_conv_intrinsic_ibits (se, expr);
11109 : 27 : break;
11110 : :
11111 : 138 : case GFC_ISYM_IBSET:
11112 : 138 : gfc_conv_intrinsic_singlebitop (se, expr, 1);
11113 : 138 : break;
11114 : :
11115 : 2033 : case GFC_ISYM_IACHAR:
11116 : 2033 : case GFC_ISYM_ICHAR:
11117 : : /* We assume ASCII character sequence. */
11118 : 2033 : gfc_conv_intrinsic_ichar (se, expr);
11119 : 2033 : break;
11120 : :
11121 : 2 : case GFC_ISYM_IARGC:
11122 : 2 : gfc_conv_intrinsic_iargc (se, expr);
11123 : 2 : break;
11124 : :
11125 : 688 : case GFC_ISYM_IEOR:
11126 : 688 : gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11127 : 688 : break;
11128 : :
11129 : 328 : case GFC_ISYM_INDEX:
11130 : 328 : kind = expr->value.function.actual->expr->ts.kind;
11131 : 328 : if (kind == 1)
11132 : 262 : fndecl = gfor_fndecl_string_index;
11133 : 66 : else if (kind == 4)
11134 : 66 : fndecl = gfor_fndecl_string_index_char4;
11135 : : else
11136 : 0 : gcc_unreachable ();
11137 : :
11138 : 328 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
11139 : 328 : break;
11140 : :
11141 : 483 : case GFC_ISYM_IOR:
11142 : 483 : gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11143 : 483 : break;
11144 : :
11145 : 12 : case GFC_ISYM_IPARITY:
11146 : 12 : gfc_conv_intrinsic_arith (se, expr, BIT_XOR_EXPR, false);
11147 : 12 : break;
11148 : :
11149 : 6 : case GFC_ISYM_IS_IOSTAT_END:
11150 : 6 : gfc_conv_has_intvalue (se, expr, LIBERROR_END);
11151 : 6 : break;
11152 : :
11153 : 18 : case GFC_ISYM_IS_IOSTAT_EOR:
11154 : 18 : gfc_conv_has_intvalue (se, expr, LIBERROR_EOR);
11155 : 18 : break;
11156 : :
11157 : 621 : case GFC_ISYM_IS_CONTIGUOUS:
11158 : 621 : gfc_conv_intrinsic_is_contiguous (se, expr);
11159 : 621 : break;
11160 : :
11161 : 432 : case GFC_ISYM_ISNAN:
11162 : 432 : gfc_conv_intrinsic_isnan (se, expr);
11163 : 432 : break;
11164 : :
11165 : 8 : case GFC_ISYM_KILL:
11166 : 8 : conv_intrinsic_kill (se, expr);
11167 : 8 : break;
11168 : :
11169 : 90 : case GFC_ISYM_LSHIFT:
11170 : 90 : gfc_conv_intrinsic_shift (se, expr, false, false);
11171 : 90 : break;
11172 : :
11173 : 24 : case GFC_ISYM_RSHIFT:
11174 : 24 : gfc_conv_intrinsic_shift (se, expr, true, true);
11175 : 24 : break;
11176 : :
11177 : 78 : case GFC_ISYM_SHIFTA:
11178 : 78 : gfc_conv_intrinsic_shift (se, expr, true, true);
11179 : 78 : break;
11180 : :
11181 : 183 : case GFC_ISYM_SHIFTL:
11182 : 183 : gfc_conv_intrinsic_shift (se, expr, false, false);
11183 : 183 : break;
11184 : :
11185 : 66 : case GFC_ISYM_SHIFTR:
11186 : 66 : gfc_conv_intrinsic_shift (se, expr, true, false);
11187 : 66 : break;
11188 : :
11189 : 318 : case GFC_ISYM_ISHFT:
11190 : 318 : gfc_conv_intrinsic_ishft (se, expr);
11191 : 318 : break;
11192 : :
11193 : 658 : case GFC_ISYM_ISHFTC:
11194 : 658 : gfc_conv_intrinsic_ishftc (se, expr);
11195 : 658 : break;
11196 : :
11197 : 270 : case GFC_ISYM_LEADZ:
11198 : 270 : gfc_conv_intrinsic_leadz (se, expr);
11199 : 270 : break;
11200 : :
11201 : 282 : case GFC_ISYM_TRAILZ:
11202 : 282 : gfc_conv_intrinsic_trailz (se, expr);
11203 : 282 : break;
11204 : :
11205 : 103 : case GFC_ISYM_POPCNT:
11206 : 103 : gfc_conv_intrinsic_popcnt_poppar (se, expr, 0);
11207 : 103 : break;
11208 : :
11209 : 31 : case GFC_ISYM_POPPAR:
11210 : 31 : gfc_conv_intrinsic_popcnt_poppar (se, expr, 1);
11211 : 31 : break;
11212 : :
11213 : 5470 : case GFC_ISYM_LBOUND:
11214 : 5470 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_LBOUND);
11215 : 5470 : break;
11216 : :
11217 : 178 : case GFC_ISYM_LCOBOUND:
11218 : 178 : conv_intrinsic_cobound (se, expr);
11219 : 178 : break;
11220 : :
11221 : 744 : case GFC_ISYM_TRANSPOSE:
11222 : : /* The scalarizer has already been set up for reversed dimension access
11223 : : order ; now we just get the argument value normally. */
11224 : 744 : gfc_conv_expr (se, expr->value.function.actual->expr);
11225 : 744 : break;
11226 : :
11227 : 5621 : case GFC_ISYM_LEN:
11228 : 5621 : gfc_conv_intrinsic_len (se, expr);
11229 : 5621 : break;
11230 : :
11231 : 2313 : case GFC_ISYM_LEN_TRIM:
11232 : 2313 : gfc_conv_intrinsic_len_trim (se, expr);
11233 : 2313 : break;
11234 : :
11235 : 18 : case GFC_ISYM_LGE:
11236 : 18 : gfc_conv_intrinsic_strcmp (se, expr, GE_EXPR);
11237 : 18 : break;
11238 : :
11239 : 36 : case GFC_ISYM_LGT:
11240 : 36 : gfc_conv_intrinsic_strcmp (se, expr, GT_EXPR);
11241 : 36 : break;
11242 : :
11243 : 18 : case GFC_ISYM_LLE:
11244 : 18 : gfc_conv_intrinsic_strcmp (se, expr, LE_EXPR);
11245 : 18 : break;
11246 : :
11247 : 27 : case GFC_ISYM_LLT:
11248 : 27 : gfc_conv_intrinsic_strcmp (se, expr, LT_EXPR);
11249 : 27 : break;
11250 : :
11251 : 16 : case GFC_ISYM_MALLOC:
11252 : 16 : gfc_conv_intrinsic_malloc (se, expr);
11253 : 16 : break;
11254 : :
11255 : 32 : case GFC_ISYM_MASKL:
11256 : 32 : gfc_conv_intrinsic_mask (se, expr, 1);
11257 : 32 : break;
11258 : :
11259 : 32 : case GFC_ISYM_MASKR:
11260 : 32 : gfc_conv_intrinsic_mask (se, expr, 0);
11261 : 32 : break;
11262 : :
11263 : 1033 : case GFC_ISYM_MAX:
11264 : 1033 : if (expr->ts.type == BT_CHARACTER)
11265 : 138 : gfc_conv_intrinsic_minmax_char (se, expr, 1);
11266 : : else
11267 : 895 : gfc_conv_intrinsic_minmax (se, expr, GT_EXPR);
11268 : : break;
11269 : :
11270 : 6348 : case GFC_ISYM_MAXLOC:
11271 : 6348 : gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
11272 : 6348 : break;
11273 : :
11274 : 216 : case GFC_ISYM_FINDLOC:
11275 : 216 : gfc_conv_intrinsic_findloc (se, expr);
11276 : 216 : break;
11277 : :
11278 : 1099 : case GFC_ISYM_MAXVAL:
11279 : 1099 : gfc_conv_intrinsic_minmaxval (se, expr, GT_EXPR);
11280 : 1099 : break;
11281 : :
11282 : 949 : case GFC_ISYM_MERGE:
11283 : 949 : gfc_conv_intrinsic_merge (se, expr);
11284 : 949 : break;
11285 : :
11286 : 42 : case GFC_ISYM_MERGE_BITS:
11287 : 42 : gfc_conv_intrinsic_merge_bits (se, expr);
11288 : 42 : break;
11289 : :
11290 : 551 : case GFC_ISYM_MIN:
11291 : 551 : if (expr->ts.type == BT_CHARACTER)
11292 : 144 : gfc_conv_intrinsic_minmax_char (se, expr, -1);
11293 : : else
11294 : 407 : gfc_conv_intrinsic_minmax (se, expr, LT_EXPR);
11295 : : break;
11296 : :
11297 : 7176 : case GFC_ISYM_MINLOC:
11298 : 7176 : gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
11299 : 7176 : break;
11300 : :
11301 : 1316 : case GFC_ISYM_MINVAL:
11302 : 1316 : gfc_conv_intrinsic_minmaxval (se, expr, LT_EXPR);
11303 : 1316 : break;
11304 : :
11305 : 1595 : case GFC_ISYM_NEAREST:
11306 : 1595 : gfc_conv_intrinsic_nearest (se, expr);
11307 : 1595 : break;
11308 : :
11309 : 68 : case GFC_ISYM_NORM2:
11310 : 68 : gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, true);
11311 : 68 : break;
11312 : :
11313 : 228 : case GFC_ISYM_NOT:
11314 : 228 : gfc_conv_intrinsic_not (se, expr);
11315 : 228 : break;
11316 : :
11317 : 12 : case GFC_ISYM_OR:
11318 : 12 : gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11319 : 12 : break;
11320 : :
11321 : 468 : case GFC_ISYM_OUT_OF_RANGE:
11322 : 468 : gfc_conv_intrinsic_out_of_range (se, expr);
11323 : 468 : break;
11324 : :
11325 : 36 : case GFC_ISYM_PARITY:
11326 : 36 : gfc_conv_intrinsic_arith (se, expr, NE_EXPR, false);
11327 : 36 : break;
11328 : :
11329 : 5045 : case GFC_ISYM_PRESENT:
11330 : 5045 : gfc_conv_intrinsic_present (se, expr);
11331 : 5045 : break;
11332 : :
11333 : 344 : case GFC_ISYM_PRODUCT:
11334 : 344 : gfc_conv_intrinsic_arith (se, expr, MULT_EXPR, false);
11335 : 344 : break;
11336 : :
11337 : 10512 : case GFC_ISYM_RANK:
11338 : 10512 : gfc_conv_intrinsic_rank (se, expr);
11339 : 10512 : break;
11340 : :
11341 : 48 : case GFC_ISYM_RRSPACING:
11342 : 48 : gfc_conv_intrinsic_rrspacing (se, expr);
11343 : 48 : break;
11344 : :
11345 : 262 : case GFC_ISYM_SET_EXPONENT:
11346 : 262 : gfc_conv_intrinsic_set_exponent (se, expr);
11347 : 262 : break;
11348 : :
11349 : 72 : case GFC_ISYM_SCALE:
11350 : 72 : gfc_conv_intrinsic_scale (se, expr);
11351 : 72 : break;
11352 : :
11353 : 4775 : case GFC_ISYM_SHAPE:
11354 : 4775 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_SHAPE);
11355 : 4775 : break;
11356 : :
11357 : 424 : case GFC_ISYM_SIGN:
11358 : 424 : gfc_conv_intrinsic_sign (se, expr);
11359 : 424 : break;
11360 : :
11361 : 14561 : case GFC_ISYM_SIZE:
11362 : 14561 : gfc_conv_intrinsic_size (se, expr);
11363 : 14561 : break;
11364 : :
11365 : 1296 : case GFC_ISYM_SIZEOF:
11366 : 1296 : case GFC_ISYM_C_SIZEOF:
11367 : 1296 : gfc_conv_intrinsic_sizeof (se, expr);
11368 : 1296 : break;
11369 : :
11370 : 802 : case GFC_ISYM_STORAGE_SIZE:
11371 : 802 : gfc_conv_intrinsic_storage_size (se, expr);
11372 : 802 : break;
11373 : :
11374 : 70 : case GFC_ISYM_SPACING:
11375 : 70 : gfc_conv_intrinsic_spacing (se, expr);
11376 : 70 : break;
11377 : :
11378 : 1877 : case GFC_ISYM_STRIDE:
11379 : 1877 : conv_intrinsic_stride (se, expr);
11380 : 1877 : break;
11381 : :
11382 : 1967 : case GFC_ISYM_SUM:
11383 : 1967 : gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, false);
11384 : 1967 : break;
11385 : :
11386 : 19 : case GFC_ISYM_TEAM_NUMBER:
11387 : 19 : conv_intrinsic_team_number (se, expr);
11388 : 19 : break;
11389 : :
11390 : 3545 : case GFC_ISYM_TRANSFER:
11391 : 3545 : if (se->ss && se->ss->info->useflags)
11392 : : /* Access the previously obtained result. */
11393 : 259 : gfc_conv_tmp_array_ref (se);
11394 : : else
11395 : 3286 : gfc_conv_intrinsic_transfer (se, expr);
11396 : : break;
11397 : :
11398 : 0 : case GFC_ISYM_TTYNAM:
11399 : 0 : gfc_conv_intrinsic_ttynam (se, expr);
11400 : 0 : break;
11401 : :
11402 : 5649 : case GFC_ISYM_UBOUND:
11403 : 5649 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_UBOUND);
11404 : 5649 : break;
11405 : :
11406 : 191 : case GFC_ISYM_UCOBOUND:
11407 : 191 : conv_intrinsic_cobound (se, expr);
11408 : 191 : break;
11409 : :
11410 : 18 : case GFC_ISYM_XOR:
11411 : 18 : gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11412 : 18 : break;
11413 : :
11414 : 8582 : case GFC_ISYM_LOC:
11415 : 8582 : gfc_conv_intrinsic_loc (se, expr);
11416 : 8582 : break;
11417 : :
11418 : 935 : case GFC_ISYM_THIS_IMAGE:
11419 : : /* For num_images() == 1, handle as LCOBOUND. */
11420 : 935 : if (expr->value.function.actual->expr
11421 : 379 : && flag_coarray == GFC_FCOARRAY_SINGLE)
11422 : 208 : conv_intrinsic_cobound (se, expr);
11423 : : else
11424 : 727 : trans_this_image (se, expr);
11425 : : break;
11426 : :
11427 : 152 : case GFC_ISYM_IMAGE_INDEX:
11428 : 152 : trans_image_index (se, expr);
11429 : 152 : break;
11430 : :
11431 : 16 : case GFC_ISYM_IMAGE_STATUS:
11432 : 16 : conv_intrinsic_image_status (se, expr);
11433 : 16 : break;
11434 : :
11435 : 429 : case GFC_ISYM_NUM_IMAGES:
11436 : 429 : trans_num_images (se, expr);
11437 : 429 : break;
11438 : :
11439 : 1349 : case GFC_ISYM_ACCESS:
11440 : 1349 : case GFC_ISYM_CHDIR:
11441 : 1349 : case GFC_ISYM_CHMOD:
11442 : 1349 : case GFC_ISYM_DTIME:
11443 : 1349 : case GFC_ISYM_ETIME:
11444 : 1349 : case GFC_ISYM_EXTENDS_TYPE_OF:
11445 : 1349 : case GFC_ISYM_FGET:
11446 : 1349 : case GFC_ISYM_FGETC:
11447 : 1349 : case GFC_ISYM_FNUM:
11448 : 1349 : case GFC_ISYM_FPUT:
11449 : 1349 : case GFC_ISYM_FPUTC:
11450 : 1349 : case GFC_ISYM_FSTAT:
11451 : 1349 : case GFC_ISYM_FTELL:
11452 : 1349 : case GFC_ISYM_GETCWD:
11453 : 1349 : case GFC_ISYM_GETGID:
11454 : 1349 : case GFC_ISYM_GETPID:
11455 : 1349 : case GFC_ISYM_GETUID:
11456 : 1349 : case GFC_ISYM_GET_TEAM:
11457 : 1349 : case GFC_ISYM_HOSTNM:
11458 : 1349 : case GFC_ISYM_IERRNO:
11459 : 1349 : case GFC_ISYM_IRAND:
11460 : 1349 : case GFC_ISYM_ISATTY:
11461 : 1349 : case GFC_ISYM_JN2:
11462 : 1349 : case GFC_ISYM_LINK:
11463 : 1349 : case GFC_ISYM_LSTAT:
11464 : 1349 : case GFC_ISYM_MATMUL:
11465 : 1349 : case GFC_ISYM_MCLOCK:
11466 : 1349 : case GFC_ISYM_MCLOCK8:
11467 : 1349 : case GFC_ISYM_RAND:
11468 : 1349 : case GFC_ISYM_REDUCE:
11469 : 1349 : case GFC_ISYM_RENAME:
11470 : 1349 : case GFC_ISYM_SECOND:
11471 : 1349 : case GFC_ISYM_SECNDS:
11472 : 1349 : case GFC_ISYM_SIGNAL:
11473 : 1349 : case GFC_ISYM_STAT:
11474 : 1349 : case GFC_ISYM_SYMLNK:
11475 : 1349 : case GFC_ISYM_SYSTEM:
11476 : 1349 : case GFC_ISYM_TIME:
11477 : 1349 : case GFC_ISYM_TIME8:
11478 : 1349 : case GFC_ISYM_UMASK:
11479 : 1349 : case GFC_ISYM_UNLINK:
11480 : 1349 : case GFC_ISYM_YN2:
11481 : 1349 : gfc_conv_intrinsic_funcall (se, expr);
11482 : 1349 : break;
11483 : :
11484 : 0 : case GFC_ISYM_EOSHIFT:
11485 : 0 : case GFC_ISYM_PACK:
11486 : 0 : case GFC_ISYM_RESHAPE:
11487 : : /* For those, expr->rank should always be >0 and thus the if above the
11488 : : switch should have matched. */
11489 : 0 : gcc_unreachable ();
11490 : 3848 : break;
11491 : :
11492 : 3848 : default:
11493 : 3848 : gfc_conv_intrinsic_lib_function (se, expr);
11494 : 3848 : break;
11495 : : }
11496 : : }
11497 : :
11498 : :
11499 : : static gfc_ss *
11500 : 1560 : walk_inline_intrinsic_transpose (gfc_ss *ss, gfc_expr *expr)
11501 : : {
11502 : 1560 : gfc_ss *arg_ss, *tmp_ss;
11503 : 1560 : gfc_actual_arglist *arg;
11504 : :
11505 : 1560 : arg = expr->value.function.actual;
11506 : :
11507 : 1560 : gcc_assert (arg->expr);
11508 : :
11509 : 1560 : arg_ss = gfc_walk_subexpr (gfc_ss_terminator, arg->expr);
11510 : 1560 : gcc_assert (arg_ss != gfc_ss_terminator);
11511 : :
11512 : : for (tmp_ss = arg_ss; ; tmp_ss = tmp_ss->next)
11513 : : {
11514 : 1665 : if (tmp_ss->info->type != GFC_SS_SCALAR
11515 : : && tmp_ss->info->type != GFC_SS_REFERENCE)
11516 : : {
11517 : 1628 : gcc_assert (tmp_ss->dimen == 2);
11518 : :
11519 : : /* We just invert dimensions. */
11520 : 1628 : std::swap (tmp_ss->dim[0], tmp_ss->dim[1]);
11521 : : }
11522 : :
11523 : : /* Stop when tmp_ss points to the last valid element of the chain... */
11524 : 1665 : if (tmp_ss->next == gfc_ss_terminator)
11525 : : break;
11526 : : }
11527 : :
11528 : : /* ... so that we can attach the rest of the chain to it. */
11529 : 1560 : tmp_ss->next = ss;
11530 : :
11531 : 1560 : return arg_ss;
11532 : : }
11533 : :
11534 : :
11535 : : /* Move the given dimension of the given gfc_ss list to a nested gfc_ss list.
11536 : : This has the side effect of reversing the nested list, so there is no
11537 : : need to call gfc_reverse_ss on it (the given list is assumed not to be
11538 : : reversed yet). */
11539 : :
11540 : : static gfc_ss *
11541 : 3371 : nest_loop_dimension (gfc_ss *ss, int dim)
11542 : : {
11543 : 3371 : int ss_dim, i;
11544 : 3371 : gfc_ss *new_ss, *prev_ss = gfc_ss_terminator;
11545 : 3371 : gfc_loopinfo *new_loop;
11546 : :
11547 : 3371 : gcc_assert (ss != gfc_ss_terminator);
11548 : :
11549 : 8118 : for (; ss != gfc_ss_terminator; ss = ss->next)
11550 : : {
11551 : 4747 : new_ss = gfc_get_ss ();
11552 : 4747 : new_ss->next = prev_ss;
11553 : 4747 : new_ss->parent = ss;
11554 : 4747 : new_ss->info = ss->info;
11555 : 4747 : new_ss->info->refcount++;
11556 : 4747 : if (ss->dimen != 0)
11557 : : {
11558 : 4684 : gcc_assert (ss->info->type != GFC_SS_SCALAR
11559 : : && ss->info->type != GFC_SS_REFERENCE);
11560 : :
11561 : 4684 : new_ss->dimen = 1;
11562 : 4684 : new_ss->dim[0] = ss->dim[dim];
11563 : :
11564 : 4684 : gcc_assert (dim < ss->dimen);
11565 : :
11566 : 4684 : ss_dim = --ss->dimen;
11567 : 10430 : for (i = dim; i < ss_dim; i++)
11568 : 5746 : ss->dim[i] = ss->dim[i + 1];
11569 : :
11570 : 4684 : ss->dim[ss_dim] = 0;
11571 : : }
11572 : 4747 : prev_ss = new_ss;
11573 : :
11574 : 4747 : if (ss->nested_ss)
11575 : : {
11576 : 81 : ss->nested_ss->parent = new_ss;
11577 : 81 : new_ss->nested_ss = ss->nested_ss;
11578 : : }
11579 : 4747 : ss->nested_ss = new_ss;
11580 : : }
11581 : :
11582 : 3371 : new_loop = gfc_get_loopinfo ();
11583 : 3371 : gfc_init_loopinfo (new_loop);
11584 : :
11585 : 3371 : gcc_assert (prev_ss != NULL);
11586 : 3371 : gcc_assert (prev_ss != gfc_ss_terminator);
11587 : 3371 : gfc_add_ss_to_loop (new_loop, prev_ss);
11588 : 3371 : return new_ss->parent;
11589 : : }
11590 : :
11591 : :
11592 : : /* Create the gfc_ss list for the SUM/PRODUCT arguments when the function
11593 : : is to be inlined. */
11594 : :
11595 : : static gfc_ss *
11596 : 575 : walk_inline_intrinsic_arith (gfc_ss *ss, gfc_expr *expr)
11597 : : {
11598 : 575 : gfc_ss *tmp_ss, *tail, *array_ss;
11599 : 575 : gfc_actual_arglist *arg1, *arg2, *arg3;
11600 : 575 : int sum_dim;
11601 : 575 : bool scalar_mask = false;
11602 : :
11603 : : /* The rank of the result will be determined later. */
11604 : 575 : arg1 = expr->value.function.actual;
11605 : 575 : arg2 = arg1->next;
11606 : 575 : arg3 = arg2->next;
11607 : 575 : gcc_assert (arg3 != NULL);
11608 : :
11609 : 575 : if (expr->rank == 0)
11610 : : return ss;
11611 : :
11612 : 575 : tmp_ss = gfc_ss_terminator;
11613 : :
11614 : 575 : if (arg3->expr)
11615 : : {
11616 : 118 : gfc_ss *mask_ss;
11617 : :
11618 : 118 : mask_ss = gfc_walk_subexpr (tmp_ss, arg3->expr);
11619 : 118 : if (mask_ss == tmp_ss)
11620 : 34 : scalar_mask = 1;
11621 : :
11622 : : tmp_ss = mask_ss;
11623 : : }
11624 : :
11625 : 575 : array_ss = gfc_walk_subexpr (tmp_ss, arg1->expr);
11626 : 575 : gcc_assert (array_ss != tmp_ss);
11627 : :
11628 : : /* Odd thing: If the mask is scalar, it is used by the frontend after
11629 : : the array (to make an if around the nested loop). Thus it shall
11630 : : be after array_ss once the gfc_ss list is reversed. */
11631 : 575 : if (scalar_mask)
11632 : 34 : tmp_ss = gfc_get_scalar_ss (array_ss, arg3->expr);
11633 : : else
11634 : : tmp_ss = array_ss;
11635 : :
11636 : : /* "Hide" the dimension on which we will sum in the first arg's scalarization
11637 : : chain. */
11638 : 575 : sum_dim = mpz_get_si (arg2->expr->value.integer) - 1;
11639 : 575 : tail = nest_loop_dimension (tmp_ss, sum_dim);
11640 : 575 : tail->next = ss;
11641 : :
11642 : 575 : return tmp_ss;
11643 : : }
11644 : :
11645 : :
11646 : : /* Create the gfc_ss list for the arguments to MINLOC or MAXLOC when the
11647 : : function is to be inlined. */
11648 : :
11649 : : static gfc_ss *
11650 : 6085 : walk_inline_intrinsic_minmaxloc (gfc_ss *ss, gfc_expr *expr ATTRIBUTE_UNUSED)
11651 : : {
11652 : 6085 : if (expr->rank == 0)
11653 : : return ss;
11654 : :
11655 : 6085 : gfc_actual_arglist *array_arg = expr->value.function.actual;
11656 : 6085 : gfc_actual_arglist *dim_arg = array_arg->next;
11657 : 6085 : gfc_actual_arglist *mask_arg = dim_arg->next;
11658 : 6085 : gfc_actual_arglist *kind_arg = mask_arg->next;
11659 : 6085 : gfc_actual_arglist *back_arg = kind_arg->next;
11660 : :
11661 : 6085 : gfc_expr *array = array_arg->expr;
11662 : 6085 : gfc_expr *dim = dim_arg->expr;
11663 : 6085 : gfc_expr *mask = mask_arg->expr;
11664 : 6085 : gfc_expr *back = back_arg->expr;
11665 : :
11666 : 6085 : if (dim == nullptr)
11667 : 3289 : return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
11668 : :
11669 : 2796 : gfc_ss *tmp_ss = gfc_ss_terminator;
11670 : :
11671 : 2796 : bool scalar_mask = false;
11672 : 2796 : if (mask)
11673 : : {
11674 : 1866 : gfc_ss *mask_ss = gfc_walk_subexpr (tmp_ss, mask);
11675 : 1866 : if (mask_ss == tmp_ss)
11676 : : scalar_mask = true;
11677 : 1174 : else if (maybe_absent_optional_variable (mask))
11678 : 20 : mask_ss->info->can_be_null_ref = true;
11679 : :
11680 : : tmp_ss = mask_ss;
11681 : : }
11682 : :
11683 : 2796 : gfc_ss *array_ss = gfc_walk_subexpr (tmp_ss, array);
11684 : 2796 : gcc_assert (array_ss != tmp_ss);
11685 : :
11686 : 2796 : tmp_ss = array_ss;
11687 : :
11688 : : /* Move the dimension on which we will sum to a separate nested scalarization
11689 : : chain, "hiding" that dimension from the outer scalarization. */
11690 : 2796 : int dim_val = mpz_get_si (dim->value.integer);
11691 : 2796 : gfc_ss *tail = nest_loop_dimension (tmp_ss, dim_val - 1);
11692 : :
11693 : 2796 : if (back && array->rank > 1)
11694 : : {
11695 : : /* If there are nested scalarization loops, include BACK in the
11696 : : scalarization chains to avoid evaluating it multiple times in a loop.
11697 : : Otherwise, prefer to handle it outside of scalarization. */
11698 : 2796 : gfc_ss *back_ss = gfc_get_scalar_ss (ss, back);
11699 : 2796 : back_ss->info->type = GFC_SS_REFERENCE;
11700 : 2796 : if (maybe_absent_optional_variable (back))
11701 : 16 : back_ss->info->can_be_null_ref = true;
11702 : :
11703 : 2796 : tail->next = back_ss;
11704 : 2796 : }
11705 : : else
11706 : 0 : tail->next = ss;
11707 : :
11708 : 2796 : if (scalar_mask)
11709 : : {
11710 : 692 : tmp_ss = gfc_get_scalar_ss (tmp_ss, mask);
11711 : : /* MASK can be a forwarded optional argument, so make the necessary setup
11712 : : to avoid the scalarizer generating any unguarded pointer dereference in
11713 : : that case. */
11714 : 692 : tmp_ss->info->type = GFC_SS_REFERENCE;
11715 : 692 : if (maybe_absent_optional_variable (mask))
11716 : 4 : tmp_ss->info->can_be_null_ref = true;
11717 : : }
11718 : :
11719 : : return tmp_ss;
11720 : : }
11721 : :
11722 : :
11723 : : static gfc_ss *
11724 : 8220 : walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr)
11725 : : {
11726 : :
11727 : 8220 : switch (expr->value.function.isym->id)
11728 : : {
11729 : 575 : case GFC_ISYM_PRODUCT:
11730 : 575 : case GFC_ISYM_SUM:
11731 : 575 : return walk_inline_intrinsic_arith (ss, expr);
11732 : :
11733 : 1560 : case GFC_ISYM_TRANSPOSE:
11734 : 1560 : return walk_inline_intrinsic_transpose (ss, expr);
11735 : :
11736 : 6085 : case GFC_ISYM_MAXLOC:
11737 : 6085 : case GFC_ISYM_MINLOC:
11738 : 6085 : return walk_inline_intrinsic_minmaxloc (ss, expr);
11739 : :
11740 : 0 : default:
11741 : 0 : gcc_unreachable ();
11742 : : }
11743 : : gcc_unreachable ();
11744 : : }
11745 : :
11746 : :
11747 : : /* This generates code to execute before entering the scalarization loop.
11748 : : Currently does nothing. */
11749 : :
11750 : : void
11751 : 11261 : gfc_add_intrinsic_ss_code (gfc_loopinfo * loop ATTRIBUTE_UNUSED, gfc_ss * ss)
11752 : : {
11753 : 11261 : switch (ss->info->expr->value.function.isym->id)
11754 : : {
11755 : 11261 : case GFC_ISYM_UBOUND:
11756 : 11261 : case GFC_ISYM_LBOUND:
11757 : 11261 : case GFC_ISYM_UCOBOUND:
11758 : 11261 : case GFC_ISYM_LCOBOUND:
11759 : 11261 : case GFC_ISYM_MAXLOC:
11760 : 11261 : case GFC_ISYM_MINLOC:
11761 : 11261 : case GFC_ISYM_THIS_IMAGE:
11762 : 11261 : case GFC_ISYM_SHAPE:
11763 : 11261 : break;
11764 : :
11765 : 0 : default:
11766 : 0 : gcc_unreachable ();
11767 : : }
11768 : 11261 : }
11769 : :
11770 : :
11771 : : /* The LBOUND, LCOBOUND, UBOUND, UCOBOUND, and SHAPE intrinsics with
11772 : : one parameter are expanded into code inside the scalarization loop. */
11773 : :
11774 : : static gfc_ss *
11775 : 9814 : gfc_walk_intrinsic_bound (gfc_ss * ss, gfc_expr * expr)
11776 : : {
11777 : 9814 : if (expr->value.function.actual->expr->ts.type == BT_CLASS)
11778 : 410 : gfc_add_class_array_ref (expr->value.function.actual->expr);
11779 : :
11780 : : /* The two argument version returns a scalar. */
11781 : 9814 : if (expr->value.function.isym->id != GFC_ISYM_SHAPE
11782 : 3415 : && expr->value.function.actual->next->expr)
11783 : : return ss;
11784 : :
11785 : 9814 : return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
11786 : : }
11787 : :
11788 : :
11789 : : /* Walk an intrinsic array libcall. */
11790 : :
11791 : : static gfc_ss *
11792 : 14520 : gfc_walk_intrinsic_libfunc (gfc_ss * ss, gfc_expr * expr)
11793 : : {
11794 : 14520 : gcc_assert (expr->rank > 0);
11795 : 14520 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
11796 : : }
11797 : :
11798 : :
11799 : : /* Return whether the function call expression EXPR will be expanded
11800 : : inline by gfc_conv_intrinsic_function. */
11801 : :
11802 : : bool
11803 : 293844 : gfc_inline_intrinsic_function_p (gfc_expr *expr)
11804 : : {
11805 : 293844 : gfc_actual_arglist *args, *dim_arg, *mask_arg;
11806 : 293844 : gfc_expr *maskexpr;
11807 : :
11808 : 293844 : gfc_intrinsic_sym *isym = expr->value.function.isym;
11809 : 293844 : if (!isym)
11810 : : return false;
11811 : :
11812 : 293808 : switch (isym->id)
11813 : : {
11814 : 5072 : case GFC_ISYM_PRODUCT:
11815 : 5072 : case GFC_ISYM_SUM:
11816 : : /* Disable inline expansion if code size matters. */
11817 : 5072 : if (optimize_size)
11818 : : return false;
11819 : :
11820 : 4223 : args = expr->value.function.actual;
11821 : 4223 : dim_arg = args->next;
11822 : :
11823 : : /* We need to be able to subset the SUM argument at compile-time. */
11824 : 4223 : if (dim_arg->expr && dim_arg->expr->expr_type != EXPR_CONSTANT)
11825 : : return false;
11826 : :
11827 : : /* FIXME: If MASK is optional for a more than two-dimensional
11828 : : argument, the scalarizer gets confused if the mask is
11829 : : absent. See PR 82995. For now, fall back to the library
11830 : : function. */
11831 : :
11832 : 3611 : mask_arg = dim_arg->next;
11833 : 3611 : maskexpr = mask_arg->expr;
11834 : :
11835 : 3611 : if (expr->rank > 0 && maskexpr && maskexpr->expr_type == EXPR_VARIABLE
11836 : 276 : && maskexpr->symtree->n.sym->attr.dummy
11837 : 48 : && maskexpr->symtree->n.sym->attr.optional)
11838 : : return false;
11839 : :
11840 : : return true;
11841 : :
11842 : : case GFC_ISYM_TRANSPOSE:
11843 : : return true;
11844 : :
11845 : 57188 : case GFC_ISYM_MINLOC:
11846 : 57188 : case GFC_ISYM_MAXLOC:
11847 : 57188 : {
11848 : 57188 : if ((isym->id == GFC_ISYM_MINLOC
11849 : 30521 : && (flag_inline_intrinsics
11850 : 30521 : & GFC_FLAG_INLINE_INTRINSIC_MINLOC) == 0)
11851 : 46611 : || (isym->id == GFC_ISYM_MAXLOC
11852 : 26667 : && (flag_inline_intrinsics
11853 : 26667 : & GFC_FLAG_INLINE_INTRINSIC_MAXLOC) == 0))
11854 : : return false;
11855 : :
11856 : 37638 : gfc_actual_arglist *array_arg = expr->value.function.actual;
11857 : 37638 : gfc_actual_arglist *dim_arg = array_arg->next;
11858 : :
11859 : 37638 : gfc_expr *array = array_arg->expr;
11860 : 37638 : gfc_expr *dim = dim_arg->expr;
11861 : :
11862 : 37638 : if (!(array->ts.type == BT_INTEGER
11863 : : || array->ts.type == BT_REAL))
11864 : : return false;
11865 : :
11866 : 34658 : if (array->rank == 1)
11867 : : return true;
11868 : :
11869 : 20711 : if (dim != nullptr
11870 : 13372 : && dim->expr_type != EXPR_CONSTANT)
11871 : : return false;
11872 : :
11873 : : return true;
11874 : : }
11875 : :
11876 : : default:
11877 : : return false;
11878 : : }
11879 : : }
11880 : :
11881 : :
11882 : : /* Returns nonzero if the specified intrinsic function call maps directly to
11883 : : an external library call. Should only be used for functions that return
11884 : : arrays. */
11885 : :
11886 : : int
11887 : 88058 : gfc_is_intrinsic_libcall (gfc_expr * expr)
11888 : : {
11889 : 88058 : gcc_assert (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym);
11890 : 88058 : gcc_assert (expr->rank > 0);
11891 : :
11892 : 88058 : if (gfc_inline_intrinsic_function_p (expr))
11893 : : return 0;
11894 : :
11895 : 73476 : switch (expr->value.function.isym->id)
11896 : : {
11897 : : case GFC_ISYM_ALL:
11898 : : case GFC_ISYM_ANY:
11899 : : case GFC_ISYM_COUNT:
11900 : : case GFC_ISYM_FINDLOC:
11901 : : case GFC_ISYM_JN2:
11902 : : case GFC_ISYM_IANY:
11903 : : case GFC_ISYM_IALL:
11904 : : case GFC_ISYM_IPARITY:
11905 : : case GFC_ISYM_MATMUL:
11906 : : case GFC_ISYM_MAXLOC:
11907 : : case GFC_ISYM_MAXVAL:
11908 : : case GFC_ISYM_MINLOC:
11909 : : case GFC_ISYM_MINVAL:
11910 : : case GFC_ISYM_NORM2:
11911 : : case GFC_ISYM_PARITY:
11912 : : case GFC_ISYM_PRODUCT:
11913 : : case GFC_ISYM_SUM:
11914 : : case GFC_ISYM_SPREAD:
11915 : : case GFC_ISYM_YN2:
11916 : : /* Ignore absent optional parameters. */
11917 : : return 1;
11918 : :
11919 : 16429 : case GFC_ISYM_CSHIFT:
11920 : 16429 : case GFC_ISYM_EOSHIFT:
11921 : 16429 : case GFC_ISYM_GET_TEAM:
11922 : 16429 : case GFC_ISYM_FAILED_IMAGES:
11923 : 16429 : case GFC_ISYM_STOPPED_IMAGES:
11924 : 16429 : case GFC_ISYM_PACK:
11925 : 16429 : case GFC_ISYM_REDUCE:
11926 : 16429 : case GFC_ISYM_RESHAPE:
11927 : 16429 : case GFC_ISYM_UNPACK:
11928 : : /* Pass absent optional parameters. */
11929 : 16429 : return 2;
11930 : :
11931 : : default:
11932 : : return 0;
11933 : : }
11934 : : }
11935 : :
11936 : : /* Walk an intrinsic function. */
11937 : : gfc_ss *
11938 : 55744 : gfc_walk_intrinsic_function (gfc_ss * ss, gfc_expr * expr,
11939 : : gfc_intrinsic_sym * isym)
11940 : : {
11941 : 55744 : gcc_assert (isym);
11942 : :
11943 : 55744 : if (isym->elemental)
11944 : 18828 : return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
11945 : : expr->value.function.isym,
11946 : 18828 : GFC_SS_SCALAR);
11947 : :
11948 : 36916 : if (expr->rank == 0 && expr->corank == 0)
11949 : : return ss;
11950 : :
11951 : 32554 : if (gfc_inline_intrinsic_function_p (expr))
11952 : 8220 : return walk_inline_intrinsic_function (ss, expr);
11953 : :
11954 : 24334 : if (expr->rank != 0 && gfc_is_intrinsic_libcall (expr))
11955 : 13600 : return gfc_walk_intrinsic_libfunc (ss, expr);
11956 : :
11957 : : /* Special cases. */
11958 : 10734 : switch (isym->id)
11959 : : {
11960 : 9814 : case GFC_ISYM_LBOUND:
11961 : 9814 : case GFC_ISYM_LCOBOUND:
11962 : 9814 : case GFC_ISYM_UBOUND:
11963 : 9814 : case GFC_ISYM_UCOBOUND:
11964 : 9814 : case GFC_ISYM_THIS_IMAGE:
11965 : 9814 : case GFC_ISYM_SHAPE:
11966 : 9814 : return gfc_walk_intrinsic_bound (ss, expr);
11967 : :
11968 : 920 : case GFC_ISYM_TRANSFER:
11969 : 920 : case GFC_ISYM_CAF_GET:
11970 : 920 : return gfc_walk_intrinsic_libfunc (ss, expr);
11971 : :
11972 : 0 : default:
11973 : : /* This probably meant someone forgot to add an intrinsic to the above
11974 : : list(s) when they implemented it, or something's gone horribly
11975 : : wrong. */
11976 : 0 : gcc_unreachable ();
11977 : : }
11978 : : }
11979 : :
11980 : : static tree
11981 : 63 : conv_co_collective (gfc_code *code)
11982 : : {
11983 : 63 : gfc_se argse;
11984 : 63 : stmtblock_t block, post_block;
11985 : 63 : tree fndecl, array = NULL_TREE, strlen, image_index, stat, errmsg, errmsg_len;
11986 : 63 : gfc_expr *image_idx_expr, *stat_expr, *errmsg_expr, *opr_expr;
11987 : :
11988 : 63 : gfc_start_block (&block);
11989 : 63 : gfc_init_block (&post_block);
11990 : :
11991 : 63 : if (code->resolved_isym->id == GFC_ISYM_CO_REDUCE)
11992 : : {
11993 : 7 : opr_expr = code->ext.actual->next->expr;
11994 : 7 : image_idx_expr = code->ext.actual->next->next->expr;
11995 : 7 : stat_expr = code->ext.actual->next->next->next->expr;
11996 : 7 : errmsg_expr = code->ext.actual->next->next->next->next->expr;
11997 : : }
11998 : : else
11999 : : {
12000 : 56 : opr_expr = NULL;
12001 : 56 : image_idx_expr = code->ext.actual->next->expr;
12002 : 56 : stat_expr = code->ext.actual->next->next->expr;
12003 : 56 : errmsg_expr = code->ext.actual->next->next->next->expr;
12004 : : }
12005 : :
12006 : : /* stat. */
12007 : 63 : if (stat_expr)
12008 : : {
12009 : 49 : gfc_init_se (&argse, NULL);
12010 : 49 : gfc_conv_expr (&argse, stat_expr);
12011 : 49 : gfc_add_block_to_block (&block, &argse.pre);
12012 : 49 : gfc_add_block_to_block (&post_block, &argse.post);
12013 : 49 : stat = argse.expr;
12014 : 49 : if (flag_coarray != GFC_FCOARRAY_SINGLE)
12015 : 22 : stat = gfc_build_addr_expr (NULL_TREE, stat);
12016 : : }
12017 : 14 : else if (flag_coarray == GFC_FCOARRAY_SINGLE)
12018 : : stat = NULL_TREE;
12019 : : else
12020 : 8 : stat = null_pointer_node;
12021 : :
12022 : : /* Early exit for GFC_FCOARRAY_SINGLE. */
12023 : 63 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
12024 : : {
12025 : 33 : if (stat != NULL_TREE)
12026 : : {
12027 : : /* For optional stats, check the pointer is valid before zero'ing. */
12028 : 27 : if (gfc_expr_attr (stat_expr).optional)
12029 : : {
12030 : 12 : tree tmp;
12031 : 12 : stmtblock_t ass_block;
12032 : 12 : gfc_start_block (&ass_block);
12033 : 12 : gfc_add_modify (&ass_block, stat,
12034 : 12 : fold_convert (TREE_TYPE (stat),
12035 : : integer_zero_node));
12036 : 12 : tmp = fold_build2 (NE_EXPR, logical_type_node,
12037 : : gfc_build_addr_expr (NULL_TREE, stat),
12038 : : null_pointer_node);
12039 : 12 : tmp = fold_build3 (COND_EXPR, void_type_node, tmp,
12040 : : gfc_finish_block (&ass_block),
12041 : : build_empty_stmt (input_location));
12042 : 12 : gfc_add_expr_to_block (&block, tmp);
12043 : : }
12044 : : else
12045 : 15 : gfc_add_modify (&block, stat,
12046 : 15 : fold_convert (TREE_TYPE (stat), integer_zero_node));
12047 : : }
12048 : 33 : return gfc_finish_block (&block);
12049 : : }
12050 : :
12051 : 3 : gfc_symbol *derived = code->ext.actual->expr->ts.type == BT_DERIVED
12052 : 30 : ? code->ext.actual->expr->ts.u.derived : NULL;
12053 : :
12054 : : /* Handle the array. */
12055 : 30 : gfc_init_se (&argse, NULL);
12056 : 30 : if (!derived || !derived->attr.alloc_comp
12057 : 1 : || code->resolved_isym->id != GFC_ISYM_CO_BROADCAST)
12058 : : {
12059 : 29 : if (code->ext.actual->expr->rank == 0)
12060 : : {
12061 : 16 : symbol_attribute attr;
12062 : 16 : gfc_clear_attr (&attr);
12063 : 16 : gfc_init_se (&argse, NULL);
12064 : 16 : gfc_conv_expr (&argse, code->ext.actual->expr);
12065 : 16 : gfc_add_block_to_block (&block, &argse.pre);
12066 : 16 : gfc_add_block_to_block (&post_block, &argse.post);
12067 : 16 : array = gfc_conv_scalar_to_descriptor (&argse, argse.expr, attr);
12068 : 16 : array = gfc_build_addr_expr (NULL_TREE, array);
12069 : : }
12070 : : else
12071 : : {
12072 : 13 : argse.want_pointer = 1;
12073 : 13 : gfc_conv_expr_descriptor (&argse, code->ext.actual->expr);
12074 : 13 : array = argse.expr;
12075 : : }
12076 : : }
12077 : :
12078 : 30 : gfc_add_block_to_block (&block, &argse.pre);
12079 : 30 : gfc_add_block_to_block (&post_block, &argse.post);
12080 : :
12081 : 30 : if (code->ext.actual->expr->ts.type == BT_CHARACTER)
12082 : 6 : strlen = argse.string_length;
12083 : : else
12084 : 24 : strlen = integer_zero_node;
12085 : :
12086 : : /* image_index. */
12087 : 30 : if (image_idx_expr)
12088 : : {
12089 : 22 : gfc_init_se (&argse, NULL);
12090 : 22 : gfc_conv_expr (&argse, image_idx_expr);
12091 : 22 : gfc_add_block_to_block (&block, &argse.pre);
12092 : 22 : gfc_add_block_to_block (&post_block, &argse.post);
12093 : 22 : image_index = fold_convert (integer_type_node, argse.expr);
12094 : : }
12095 : : else
12096 : 8 : image_index = integer_zero_node;
12097 : :
12098 : : /* errmsg. */
12099 : 30 : if (errmsg_expr)
12100 : : {
12101 : 17 : gfc_init_se (&argse, NULL);
12102 : 17 : gfc_conv_expr (&argse, errmsg_expr);
12103 : 17 : gfc_add_block_to_block (&block, &argse.pre);
12104 : 17 : gfc_add_block_to_block (&post_block, &argse.post);
12105 : 17 : errmsg = argse.expr;
12106 : 17 : errmsg_len = fold_convert (size_type_node, argse.string_length);
12107 : : }
12108 : : else
12109 : : {
12110 : 13 : errmsg = null_pointer_node;
12111 : 13 : errmsg_len = build_zero_cst (size_type_node);
12112 : : }
12113 : :
12114 : : /* Generate the function call. */
12115 : 30 : switch (code->resolved_isym->id)
12116 : : {
12117 : 12 : case GFC_ISYM_CO_BROADCAST:
12118 : 12 : fndecl = gfor_fndecl_co_broadcast;
12119 : 12 : break;
12120 : 5 : case GFC_ISYM_CO_MAX:
12121 : 5 : fndecl = gfor_fndecl_co_max;
12122 : 5 : break;
12123 : 4 : case GFC_ISYM_CO_MIN:
12124 : 4 : fndecl = gfor_fndecl_co_min;
12125 : 4 : break;
12126 : 5 : case GFC_ISYM_CO_REDUCE:
12127 : 5 : fndecl = gfor_fndecl_co_reduce;
12128 : 5 : break;
12129 : 4 : case GFC_ISYM_CO_SUM:
12130 : 4 : fndecl = gfor_fndecl_co_sum;
12131 : 4 : break;
12132 : 0 : default:
12133 : 0 : gcc_unreachable ();
12134 : : }
12135 : :
12136 : 30 : if (derived && derived->attr.alloc_comp
12137 : 1 : && code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12138 : : /* The derived type has the attribute 'alloc_comp'. */
12139 : : {
12140 : 2 : tree tmp = gfc_bcast_alloc_comp (derived, code->ext.actual->expr,
12141 : 1 : code->ext.actual->expr->rank,
12142 : : image_index, stat, errmsg, errmsg_len);
12143 : 1 : gfc_add_expr_to_block (&block, tmp);
12144 : 1 : }
12145 : : else
12146 : : {
12147 : 29 : if (code->resolved_isym->id == GFC_ISYM_CO_SUM
12148 : 25 : || code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12149 : 15 : fndecl = build_call_expr_loc (input_location, fndecl, 5, array,
12150 : : image_index, stat, errmsg, errmsg_len);
12151 : 14 : else if (code->resolved_isym->id != GFC_ISYM_CO_REDUCE)
12152 : 9 : fndecl = build_call_expr_loc (input_location, fndecl, 6, array,
12153 : : image_index, stat, errmsg,
12154 : : strlen, errmsg_len);
12155 : : else
12156 : : {
12157 : 5 : tree opr, opr_flags;
12158 : :
12159 : : // FIXME: Handle TS29113's bind(C) strings with descriptor.
12160 : 5 : int opr_flag_int;
12161 : 5 : if (gfc_is_proc_ptr_comp (opr_expr))
12162 : : {
12163 : 0 : gfc_symbol *sym = gfc_get_proc_ptr_comp (opr_expr)->ts.interface;
12164 : 0 : opr_flag_int = sym->attr.dimension
12165 : 0 : || (sym->ts.type == BT_CHARACTER
12166 : 0 : && !sym->attr.is_bind_c)
12167 : 0 : ? GFC_CAF_BYREF : 0;
12168 : 0 : opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12169 : 0 : && !sym->attr.is_bind_c
12170 : 0 : ? GFC_CAF_HIDDENLEN : 0;
12171 : 0 : opr_flag_int |= sym->formal->sym->attr.value
12172 : 0 : ? GFC_CAF_ARG_VALUE : 0;
12173 : : }
12174 : : else
12175 : : {
12176 : 5 : opr_flag_int = gfc_return_by_reference (opr_expr->symtree->n.sym)
12177 : 5 : ? GFC_CAF_BYREF : 0;
12178 : 10 : opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12179 : 0 : && !opr_expr->symtree->n.sym->attr.is_bind_c
12180 : 5 : ? GFC_CAF_HIDDENLEN : 0;
12181 : 5 : opr_flag_int |= opr_expr->symtree->n.sym->formal->sym->attr.value
12182 : 5 : ? GFC_CAF_ARG_VALUE : 0;
12183 : : }
12184 : 5 : opr_flags = build_int_cst (integer_type_node, opr_flag_int);
12185 : 5 : gfc_conv_expr (&argse, opr_expr);
12186 : 5 : opr = argse.expr;
12187 : 5 : fndecl = build_call_expr_loc (input_location, fndecl, 8, array, opr,
12188 : : opr_flags, image_index, stat, errmsg,
12189 : : strlen, errmsg_len);
12190 : : }
12191 : : }
12192 : :
12193 : 30 : gfc_add_expr_to_block (&block, fndecl);
12194 : 30 : gfc_add_block_to_block (&block, &post_block);
12195 : :
12196 : 30 : return gfc_finish_block (&block);
12197 : : }
12198 : :
12199 : :
12200 : : static tree
12201 : 68 : conv_intrinsic_atomic_op (gfc_code *code)
12202 : : {
12203 : 68 : gfc_se argse;
12204 : 68 : tree tmp, atom, value, old = NULL_TREE, stat = NULL_TREE;
12205 : 68 : stmtblock_t block, post_block;
12206 : 68 : gfc_expr *atom_expr = code->ext.actual->expr;
12207 : 68 : gfc_expr *stat_expr;
12208 : 68 : built_in_function fn;
12209 : :
12210 : 68 : if (atom_expr->expr_type == EXPR_FUNCTION
12211 : 0 : && atom_expr->value.function.isym
12212 : 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12213 : 0 : atom_expr = atom_expr->value.function.actual->expr;
12214 : :
12215 : 68 : gfc_start_block (&block);
12216 : 68 : gfc_init_block (&post_block);
12217 : :
12218 : 68 : gfc_init_se (&argse, NULL);
12219 : 68 : argse.want_pointer = 1;
12220 : 68 : gfc_conv_expr (&argse, atom_expr);
12221 : 68 : gfc_add_block_to_block (&block, &argse.pre);
12222 : 68 : gfc_add_block_to_block (&post_block, &argse.post);
12223 : 68 : atom = argse.expr;
12224 : :
12225 : 68 : gfc_init_se (&argse, NULL);
12226 : 68 : if (flag_coarray == GFC_FCOARRAY_LIB
12227 : 29 : && code->ext.actual->next->expr->ts.kind == atom_expr->ts.kind)
12228 : 28 : argse.want_pointer = 1;
12229 : 68 : gfc_conv_expr (&argse, code->ext.actual->next->expr);
12230 : 68 : gfc_add_block_to_block (&block, &argse.pre);
12231 : 68 : gfc_add_block_to_block (&post_block, &argse.post);
12232 : 68 : value = argse.expr;
12233 : :
12234 : 68 : switch (code->resolved_isym->id)
12235 : : {
12236 : 42 : case GFC_ISYM_ATOMIC_ADD:
12237 : 42 : case GFC_ISYM_ATOMIC_AND:
12238 : 42 : case GFC_ISYM_ATOMIC_DEF:
12239 : 42 : case GFC_ISYM_ATOMIC_OR:
12240 : 42 : case GFC_ISYM_ATOMIC_XOR:
12241 : 42 : stat_expr = code->ext.actual->next->next->expr;
12242 : 42 : if (flag_coarray == GFC_FCOARRAY_LIB)
12243 : 18 : old = null_pointer_node;
12244 : : break;
12245 : 26 : default:
12246 : 26 : gfc_init_se (&argse, NULL);
12247 : 26 : if (flag_coarray == GFC_FCOARRAY_LIB)
12248 : 11 : argse.want_pointer = 1;
12249 : 26 : gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
12250 : 26 : gfc_add_block_to_block (&block, &argse.pre);
12251 : 26 : gfc_add_block_to_block (&post_block, &argse.post);
12252 : 26 : old = argse.expr;
12253 : 26 : stat_expr = code->ext.actual->next->next->next->expr;
12254 : : }
12255 : :
12256 : : /* STAT= */
12257 : 68 : if (stat_expr != NULL)
12258 : : {
12259 : 58 : gcc_assert (stat_expr->expr_type == EXPR_VARIABLE);
12260 : 58 : gfc_init_se (&argse, NULL);
12261 : 58 : if (flag_coarray == GFC_FCOARRAY_LIB)
12262 : 24 : argse.want_pointer = 1;
12263 : 58 : gfc_conv_expr_val (&argse, stat_expr);
12264 : 58 : gfc_add_block_to_block (&block, &argse.pre);
12265 : 58 : gfc_add_block_to_block (&post_block, &argse.post);
12266 : 58 : stat = argse.expr;
12267 : : }
12268 : 10 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12269 : 5 : stat = null_pointer_node;
12270 : :
12271 : 68 : if (flag_coarray == GFC_FCOARRAY_LIB)
12272 : : {
12273 : 29 : tree image_index, caf_decl, offset, token;
12274 : 29 : int op;
12275 : :
12276 : 29 : switch (code->resolved_isym->id)
12277 : : {
12278 : : case GFC_ISYM_ATOMIC_ADD:
12279 : : case GFC_ISYM_ATOMIC_FETCH_ADD:
12280 : : op = (int) GFC_CAF_ATOMIC_ADD;
12281 : : break;
12282 : 6 : case GFC_ISYM_ATOMIC_AND:
12283 : 6 : case GFC_ISYM_ATOMIC_FETCH_AND:
12284 : 6 : op = (int) GFC_CAF_ATOMIC_AND;
12285 : 6 : break;
12286 : 6 : case GFC_ISYM_ATOMIC_OR:
12287 : 6 : case GFC_ISYM_ATOMIC_FETCH_OR:
12288 : 6 : op = (int) GFC_CAF_ATOMIC_OR;
12289 : 6 : break;
12290 : 6 : case GFC_ISYM_ATOMIC_XOR:
12291 : 6 : case GFC_ISYM_ATOMIC_FETCH_XOR:
12292 : 6 : op = (int) GFC_CAF_ATOMIC_XOR;
12293 : 6 : break;
12294 : 6 : case GFC_ISYM_ATOMIC_DEF:
12295 : 6 : op = 0; /* Unused. */
12296 : 6 : break;
12297 : 0 : default:
12298 : 0 : gcc_unreachable ();
12299 : : }
12300 : :
12301 : 29 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12302 : 29 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12303 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12304 : :
12305 : 29 : if (gfc_is_coindexed (atom_expr))
12306 : 25 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12307 : : else
12308 : 4 : image_index = integer_zero_node;
12309 : :
12310 : 29 : if (!POINTER_TYPE_P (TREE_TYPE (value)))
12311 : : {
12312 : 28 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
12313 : 28 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), value));
12314 : 28 : value = gfc_build_addr_expr (NULL_TREE, tmp);
12315 : : }
12316 : :
12317 : 29 : gfc_init_se (&argse, NULL);
12318 : 29 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12319 : : atom_expr);
12320 : :
12321 : 29 : gfc_add_block_to_block (&block, &argse.pre);
12322 : 29 : if (code->resolved_isym->id == GFC_ISYM_ATOMIC_DEF)
12323 : 6 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_def, 7,
12324 : : token, offset, image_index, value, stat,
12325 : : build_int_cst (integer_type_node,
12326 : 6 : (int) atom_expr->ts.type),
12327 : : build_int_cst (integer_type_node,
12328 : 6 : (int) atom_expr->ts.kind));
12329 : : else
12330 : 23 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_op, 9,
12331 : 23 : build_int_cst (integer_type_node, op),
12332 : : token, offset, image_index, value, old, stat,
12333 : : build_int_cst (integer_type_node,
12334 : 23 : (int) atom_expr->ts.type),
12335 : : build_int_cst (integer_type_node,
12336 : 23 : (int) atom_expr->ts.kind));
12337 : :
12338 : 29 : gfc_add_expr_to_block (&block, tmp);
12339 : 29 : gfc_add_block_to_block (&block, &argse.post);
12340 : 29 : gfc_add_block_to_block (&block, &post_block);
12341 : 29 : return gfc_finish_block (&block);
12342 : : }
12343 : :
12344 : :
12345 : 39 : switch (code->resolved_isym->id)
12346 : : {
12347 : : case GFC_ISYM_ATOMIC_ADD:
12348 : : case GFC_ISYM_ATOMIC_FETCH_ADD:
12349 : : fn = BUILT_IN_ATOMIC_FETCH_ADD_N;
12350 : : break;
12351 : 8 : case GFC_ISYM_ATOMIC_AND:
12352 : 8 : case GFC_ISYM_ATOMIC_FETCH_AND:
12353 : 8 : fn = BUILT_IN_ATOMIC_FETCH_AND_N;
12354 : 8 : break;
12355 : 9 : case GFC_ISYM_ATOMIC_DEF:
12356 : 9 : fn = BUILT_IN_ATOMIC_STORE_N;
12357 : 9 : break;
12358 : 8 : case GFC_ISYM_ATOMIC_OR:
12359 : 8 : case GFC_ISYM_ATOMIC_FETCH_OR:
12360 : 8 : fn = BUILT_IN_ATOMIC_FETCH_OR_N;
12361 : 8 : break;
12362 : 8 : case GFC_ISYM_ATOMIC_XOR:
12363 : 8 : case GFC_ISYM_ATOMIC_FETCH_XOR:
12364 : 8 : fn = BUILT_IN_ATOMIC_FETCH_XOR_N;
12365 : 8 : break;
12366 : 0 : default:
12367 : 0 : gcc_unreachable ();
12368 : : }
12369 : :
12370 : 39 : tmp = TREE_TYPE (TREE_TYPE (atom));
12371 : 78 : fn = (built_in_function) ((int) fn
12372 : 39 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12373 : 39 : + 1);
12374 : 39 : tree itype = TREE_TYPE (TREE_TYPE (atom));
12375 : 39 : tmp = builtin_decl_explicit (fn);
12376 : :
12377 : 39 : switch (code->resolved_isym->id)
12378 : : {
12379 : 24 : case GFC_ISYM_ATOMIC_ADD:
12380 : 24 : case GFC_ISYM_ATOMIC_AND:
12381 : 24 : case GFC_ISYM_ATOMIC_DEF:
12382 : 24 : case GFC_ISYM_ATOMIC_OR:
12383 : 24 : case GFC_ISYM_ATOMIC_XOR:
12384 : 24 : tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12385 : : fold_convert (itype, value),
12386 : : build_int_cst (NULL, MEMMODEL_RELAXED));
12387 : 24 : gfc_add_expr_to_block (&block, tmp);
12388 : 24 : break;
12389 : 15 : default:
12390 : 15 : tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12391 : : fold_convert (itype, value),
12392 : : build_int_cst (NULL, MEMMODEL_RELAXED));
12393 : 15 : gfc_add_modify (&block, old, fold_convert (TREE_TYPE (old), tmp));
12394 : 15 : break;
12395 : : }
12396 : :
12397 : 39 : if (stat != NULL_TREE)
12398 : 34 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12399 : 39 : gfc_add_block_to_block (&block, &post_block);
12400 : 39 : return gfc_finish_block (&block);
12401 : : }
12402 : :
12403 : :
12404 : : static tree
12405 : 119 : conv_intrinsic_atomic_ref (gfc_code *code)
12406 : : {
12407 : 119 : gfc_se argse;
12408 : 119 : tree tmp, atom, value, stat = NULL_TREE;
12409 : 119 : stmtblock_t block, post_block;
12410 : 119 : built_in_function fn;
12411 : 119 : gfc_expr *atom_expr = code->ext.actual->next->expr;
12412 : :
12413 : 119 : if (atom_expr->expr_type == EXPR_FUNCTION
12414 : 0 : && atom_expr->value.function.isym
12415 : 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12416 : 0 : atom_expr = atom_expr->value.function.actual->expr;
12417 : :
12418 : 119 : gfc_start_block (&block);
12419 : 119 : gfc_init_block (&post_block);
12420 : 119 : gfc_init_se (&argse, NULL);
12421 : 119 : argse.want_pointer = 1;
12422 : 119 : gfc_conv_expr (&argse, atom_expr);
12423 : 119 : gfc_add_block_to_block (&block, &argse.pre);
12424 : 119 : gfc_add_block_to_block (&post_block, &argse.post);
12425 : 119 : atom = argse.expr;
12426 : :
12427 : 119 : gfc_init_se (&argse, NULL);
12428 : 119 : if (flag_coarray == GFC_FCOARRAY_LIB
12429 : 58 : && code->ext.actual->expr->ts.kind == atom_expr->ts.kind)
12430 : 55 : argse.want_pointer = 1;
12431 : 119 : gfc_conv_expr (&argse, code->ext.actual->expr);
12432 : 119 : gfc_add_block_to_block (&block, &argse.pre);
12433 : 119 : gfc_add_block_to_block (&post_block, &argse.post);
12434 : 119 : value = argse.expr;
12435 : :
12436 : : /* STAT= */
12437 : 119 : if (code->ext.actual->next->next->expr != NULL)
12438 : : {
12439 : 110 : gcc_assert (code->ext.actual->next->next->expr->expr_type
12440 : : == EXPR_VARIABLE);
12441 : 110 : gfc_init_se (&argse, NULL);
12442 : 110 : if (flag_coarray == GFC_FCOARRAY_LIB)
12443 : 54 : argse.want_pointer = 1;
12444 : 110 : gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
12445 : 110 : gfc_add_block_to_block (&block, &argse.pre);
12446 : 110 : gfc_add_block_to_block (&post_block, &argse.post);
12447 : 110 : stat = argse.expr;
12448 : : }
12449 : 9 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12450 : 4 : stat = null_pointer_node;
12451 : :
12452 : 119 : if (flag_coarray == GFC_FCOARRAY_LIB)
12453 : : {
12454 : 58 : tree image_index, caf_decl, offset, token;
12455 : 58 : tree orig_value = NULL_TREE, vardecl = NULL_TREE;
12456 : :
12457 : 58 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12458 : 58 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12459 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12460 : :
12461 : 58 : if (gfc_is_coindexed (atom_expr))
12462 : 52 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12463 : : else
12464 : 6 : image_index = integer_zero_node;
12465 : :
12466 : 58 : gfc_init_se (&argse, NULL);
12467 : 58 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12468 : : atom_expr);
12469 : 58 : gfc_add_block_to_block (&block, &argse.pre);
12470 : :
12471 : : /* Different type, need type conversion. */
12472 : 58 : if (!POINTER_TYPE_P (TREE_TYPE (value)))
12473 : : {
12474 : 3 : vardecl = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
12475 : 3 : orig_value = value;
12476 : 3 : value = gfc_build_addr_expr (NULL_TREE, vardecl);
12477 : : }
12478 : :
12479 : 58 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_ref, 7,
12480 : : token, offset, image_index, value, stat,
12481 : : build_int_cst (integer_type_node,
12482 : 58 : (int) atom_expr->ts.type),
12483 : : build_int_cst (integer_type_node,
12484 : 58 : (int) atom_expr->ts.kind));
12485 : 58 : gfc_add_expr_to_block (&block, tmp);
12486 : 58 : if (vardecl != NULL_TREE)
12487 : 3 : gfc_add_modify (&block, orig_value,
12488 : 3 : fold_convert (TREE_TYPE (orig_value), vardecl));
12489 : 58 : gfc_add_block_to_block (&block, &argse.post);
12490 : 58 : gfc_add_block_to_block (&block, &post_block);
12491 : 58 : return gfc_finish_block (&block);
12492 : : }
12493 : :
12494 : 61 : tmp = TREE_TYPE (TREE_TYPE (atom));
12495 : 122 : fn = (built_in_function) ((int) BUILT_IN_ATOMIC_LOAD_N
12496 : 61 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12497 : 61 : + 1);
12498 : 61 : tmp = builtin_decl_explicit (fn);
12499 : 61 : tmp = build_call_expr_loc (input_location, tmp, 2, atom,
12500 : : build_int_cst (integer_type_node,
12501 : : MEMMODEL_RELAXED));
12502 : 61 : gfc_add_modify (&block, value, fold_convert (TREE_TYPE (value), tmp));
12503 : :
12504 : 61 : if (stat != NULL_TREE)
12505 : 56 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12506 : 61 : gfc_add_block_to_block (&block, &post_block);
12507 : 61 : return gfc_finish_block (&block);
12508 : : }
12509 : :
12510 : :
12511 : : static tree
12512 : 10 : conv_intrinsic_atomic_cas (gfc_code *code)
12513 : : {
12514 : 10 : gfc_se argse;
12515 : 10 : tree tmp, atom, old, new_val, comp, stat = NULL_TREE;
12516 : 10 : stmtblock_t block, post_block;
12517 : 10 : built_in_function fn;
12518 : 10 : gfc_expr *atom_expr = code->ext.actual->expr;
12519 : :
12520 : 10 : if (atom_expr->expr_type == EXPR_FUNCTION
12521 : 0 : && atom_expr->value.function.isym
12522 : 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12523 : 0 : atom_expr = atom_expr->value.function.actual->expr;
12524 : :
12525 : 10 : gfc_init_block (&block);
12526 : 10 : gfc_init_block (&post_block);
12527 : 10 : gfc_init_se (&argse, NULL);
12528 : 10 : argse.want_pointer = 1;
12529 : 10 : gfc_conv_expr (&argse, atom_expr);
12530 : 10 : atom = argse.expr;
12531 : :
12532 : 10 : gfc_init_se (&argse, NULL);
12533 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12534 : 4 : argse.want_pointer = 1;
12535 : 10 : gfc_conv_expr (&argse, code->ext.actual->next->expr);
12536 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12537 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12538 : 10 : old = argse.expr;
12539 : :
12540 : 10 : gfc_init_se (&argse, NULL);
12541 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12542 : 4 : argse.want_pointer = 1;
12543 : 10 : gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
12544 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12545 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12546 : 10 : comp = argse.expr;
12547 : :
12548 : 10 : gfc_init_se (&argse, NULL);
12549 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB
12550 : 4 : && code->ext.actual->next->next->next->expr->ts.kind
12551 : 4 : == atom_expr->ts.kind)
12552 : 4 : argse.want_pointer = 1;
12553 : 10 : gfc_conv_expr (&argse, code->ext.actual->next->next->next->expr);
12554 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12555 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12556 : 10 : new_val = argse.expr;
12557 : :
12558 : : /* STAT= */
12559 : 10 : if (code->ext.actual->next->next->next->next->expr != NULL)
12560 : : {
12561 : 10 : gcc_assert (code->ext.actual->next->next->next->next->expr->expr_type
12562 : : == EXPR_VARIABLE);
12563 : 10 : gfc_init_se (&argse, NULL);
12564 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12565 : 4 : argse.want_pointer = 1;
12566 : 10 : gfc_conv_expr_val (&argse,
12567 : 10 : code->ext.actual->next->next->next->next->expr);
12568 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12569 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12570 : 10 : stat = argse.expr;
12571 : : }
12572 : 0 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12573 : 0 : stat = null_pointer_node;
12574 : :
12575 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12576 : : {
12577 : 4 : tree image_index, caf_decl, offset, token;
12578 : :
12579 : 4 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12580 : 4 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12581 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12582 : :
12583 : 4 : if (gfc_is_coindexed (atom_expr))
12584 : 4 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12585 : : else
12586 : 0 : image_index = integer_zero_node;
12587 : :
12588 : 4 : if (TREE_TYPE (TREE_TYPE (new_val)) != TREE_TYPE (TREE_TYPE (old)))
12589 : : {
12590 : 4 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "new");
12591 : 4 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), new_val));
12592 : 4 : new_val = gfc_build_addr_expr (NULL_TREE, tmp);
12593 : : }
12594 : :
12595 : : /* Convert a constant to a pointer. */
12596 : 4 : if (!POINTER_TYPE_P (TREE_TYPE (comp)))
12597 : : {
12598 : 4 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "comp");
12599 : 4 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), comp));
12600 : 4 : comp = gfc_build_addr_expr (NULL_TREE, tmp);
12601 : : }
12602 : :
12603 : 4 : gfc_init_se (&argse, NULL);
12604 : 4 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12605 : : atom_expr);
12606 : 4 : gfc_add_block_to_block (&block, &argse.pre);
12607 : :
12608 : 4 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_cas, 9,
12609 : : token, offset, image_index, old, comp, new_val,
12610 : : stat, build_int_cst (integer_type_node,
12611 : 4 : (int) atom_expr->ts.type),
12612 : : build_int_cst (integer_type_node,
12613 : 4 : (int) atom_expr->ts.kind));
12614 : 4 : gfc_add_expr_to_block (&block, tmp);
12615 : 4 : gfc_add_block_to_block (&block, &argse.post);
12616 : 4 : gfc_add_block_to_block (&block, &post_block);
12617 : 4 : return gfc_finish_block (&block);
12618 : : }
12619 : :
12620 : 6 : tmp = TREE_TYPE (TREE_TYPE (atom));
12621 : 12 : fn = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
12622 : 6 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12623 : 6 : + 1);
12624 : 6 : tmp = builtin_decl_explicit (fn);
12625 : :
12626 : 6 : gfc_add_modify (&block, old, comp);
12627 : 12 : tmp = build_call_expr_loc (input_location, tmp, 6, atom,
12628 : : gfc_build_addr_expr (NULL, old),
12629 : 6 : fold_convert (TREE_TYPE (old), new_val),
12630 : : boolean_false_node,
12631 : : build_int_cst (NULL, MEMMODEL_RELAXED),
12632 : : build_int_cst (NULL, MEMMODEL_RELAXED));
12633 : 6 : gfc_add_expr_to_block (&block, tmp);
12634 : :
12635 : 6 : if (stat != NULL_TREE)
12636 : 6 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12637 : 6 : gfc_add_block_to_block (&block, &post_block);
12638 : 6 : return gfc_finish_block (&block);
12639 : : }
12640 : :
12641 : : static tree
12642 : 70 : conv_intrinsic_event_query (gfc_code *code)
12643 : : {
12644 : 70 : gfc_se se, argse;
12645 : 70 : tree stat = NULL_TREE, stat2 = NULL_TREE;
12646 : 70 : tree count = NULL_TREE, count2 = NULL_TREE;
12647 : :
12648 : 70 : gfc_expr *event_expr = code->ext.actual->expr;
12649 : :
12650 : 70 : if (code->ext.actual->next->next->expr)
12651 : : {
12652 : 12 : gcc_assert (code->ext.actual->next->next->expr->expr_type
12653 : : == EXPR_VARIABLE);
12654 : 12 : gfc_init_se (&argse, NULL);
12655 : 12 : gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
12656 : 12 : stat = argse.expr;
12657 : : }
12658 : 58 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12659 : 29 : stat = null_pointer_node;
12660 : :
12661 : 70 : if (code->ext.actual->next->expr)
12662 : : {
12663 : 70 : gcc_assert (code->ext.actual->next->expr->expr_type == EXPR_VARIABLE);
12664 : 70 : gfc_init_se (&argse, NULL);
12665 : 70 : gfc_conv_expr_val (&argse, code->ext.actual->next->expr);
12666 : 70 : count = argse.expr;
12667 : : }
12668 : :
12669 : 70 : gfc_start_block (&se.pre);
12670 : 70 : if (flag_coarray == GFC_FCOARRAY_LIB)
12671 : : {
12672 : 35 : tree tmp, token, image_index;
12673 : 35 : tree index = build_zero_cst (gfc_array_index_type);
12674 : :
12675 : 35 : if (event_expr->expr_type == EXPR_FUNCTION
12676 : 0 : && event_expr->value.function.isym
12677 : 0 : && event_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12678 : 0 : event_expr = event_expr->value.function.actual->expr;
12679 : :
12680 : 35 : tree caf_decl = gfc_get_tree_for_caf_expr (event_expr);
12681 : :
12682 : 35 : if (event_expr->symtree->n.sym->ts.type != BT_DERIVED
12683 : 35 : || event_expr->symtree->n.sym->ts.u.derived->from_intmod
12684 : : != INTMOD_ISO_FORTRAN_ENV
12685 : 35 : || event_expr->symtree->n.sym->ts.u.derived->intmod_sym_id
12686 : : != ISOFORTRAN_EVENT_TYPE)
12687 : : {
12688 : 0 : gfc_error ("Sorry, the event component of derived type at %L is not "
12689 : : "yet supported", &event_expr->where);
12690 : 0 : return NULL_TREE;
12691 : : }
12692 : :
12693 : 35 : if (gfc_is_coindexed (event_expr))
12694 : : {
12695 : 0 : gfc_error ("The event variable at %L shall not be coindexed",
12696 : : &event_expr->where);
12697 : 0 : return NULL_TREE;
12698 : : }
12699 : :
12700 : 35 : image_index = integer_zero_node;
12701 : :
12702 : 35 : gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
12703 : : event_expr);
12704 : :
12705 : : /* For arrays, obtain the array index. */
12706 : 35 : if (gfc_expr_attr (event_expr).dimension)
12707 : : {
12708 : 26 : tree desc, tmp, extent, lbound, ubound;
12709 : 26 : gfc_array_ref *ar, ar2;
12710 : 26 : int i;
12711 : :
12712 : : /* TODO: Extend this, once DT components are supported. */
12713 : 26 : ar = &event_expr->ref->u.ar;
12714 : 26 : ar2 = *ar;
12715 : 26 : memset (ar, '\0', sizeof (*ar));
12716 : 26 : ar->as = ar2.as;
12717 : 26 : ar->type = AR_FULL;
12718 : :
12719 : 26 : gfc_init_se (&argse, NULL);
12720 : 26 : argse.descriptor_only = 1;
12721 : 26 : gfc_conv_expr_descriptor (&argse, event_expr);
12722 : 26 : gfc_add_block_to_block (&se.pre, &argse.pre);
12723 : 26 : desc = argse.expr;
12724 : 26 : *ar = ar2;
12725 : :
12726 : 26 : extent = build_one_cst (gfc_array_index_type);
12727 : 78 : for (i = 0; i < ar->dimen; i++)
12728 : : {
12729 : 26 : gfc_init_se (&argse, NULL);
12730 : 26 : gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
12731 : 26 : gfc_add_block_to_block (&argse.pre, &argse.pre);
12732 : 26 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
12733 : 26 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
12734 : 26 : TREE_TYPE (lbound), argse.expr, lbound);
12735 : 26 : tmp = fold_build2_loc (input_location, MULT_EXPR,
12736 : 26 : TREE_TYPE (tmp), extent, tmp);
12737 : 26 : index = fold_build2_loc (input_location, PLUS_EXPR,
12738 : 26 : TREE_TYPE (tmp), index, tmp);
12739 : 26 : if (i < ar->dimen - 1)
12740 : : {
12741 : 0 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
12742 : 0 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
12743 : 0 : extent = fold_build2_loc (input_location, MULT_EXPR,
12744 : 0 : TREE_TYPE (tmp), extent, tmp);
12745 : : }
12746 : : }
12747 : : }
12748 : :
12749 : 35 : if (count != null_pointer_node && TREE_TYPE (count) != integer_type_node)
12750 : : {
12751 : 0 : count2 = count;
12752 : 0 : count = gfc_create_var (integer_type_node, "count");
12753 : : }
12754 : :
12755 : 35 : if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
12756 : : {
12757 : 0 : stat2 = stat;
12758 : 0 : stat = gfc_create_var (integer_type_node, "stat");
12759 : : }
12760 : :
12761 : 35 : index = fold_convert (size_type_node, index);
12762 : 70 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_query, 5,
12763 : : token, index, image_index, count
12764 : 35 : ? gfc_build_addr_expr (NULL, count) : count,
12765 : 35 : stat != null_pointer_node
12766 : 6 : ? gfc_build_addr_expr (NULL, stat) : stat);
12767 : 35 : gfc_add_expr_to_block (&se.pre, tmp);
12768 : :
12769 : 35 : if (count2 != NULL_TREE)
12770 : 0 : gfc_add_modify (&se.pre, count2,
12771 : 0 : fold_convert (TREE_TYPE (count2), count));
12772 : :
12773 : 35 : if (stat2 != NULL_TREE)
12774 : 0 : gfc_add_modify (&se.pre, stat2,
12775 : 0 : fold_convert (TREE_TYPE (stat2), stat));
12776 : :
12777 : 35 : return gfc_finish_block (&se.pre);
12778 : : }
12779 : :
12780 : 35 : gfc_init_se (&argse, NULL);
12781 : 35 : gfc_conv_expr_val (&argse, code->ext.actual->expr);
12782 : 35 : gfc_add_modify (&se.pre, count, fold_convert (TREE_TYPE (count), argse.expr));
12783 : :
12784 : 35 : if (stat != NULL_TREE)
12785 : 6 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
12786 : :
12787 : 35 : return gfc_finish_block (&se.pre);
12788 : : }
12789 : :
12790 : :
12791 : : /* This is a peculiar case because of the need to do dependency checking.
12792 : : It is called via trans-stmt.cc(gfc_trans_call), where it is picked out as
12793 : : a special case and this function called instead of
12794 : : gfc_conv_procedure_call. */
12795 : : void
12796 : 197 : gfc_conv_intrinsic_mvbits (gfc_se *se, gfc_actual_arglist *actual_args,
12797 : : gfc_loopinfo *loop)
12798 : : {
12799 : 197 : gfc_actual_arglist *actual;
12800 : 197 : gfc_se argse[5];
12801 : 197 : gfc_expr *arg[5];
12802 : 197 : gfc_ss *lss;
12803 : 197 : int n;
12804 : :
12805 : 197 : tree from, frompos, len, to, topos;
12806 : 197 : tree lenmask, oldbits, newbits, bitsize;
12807 : 197 : tree type, utype, above, mask1, mask2;
12808 : :
12809 : 197 : if (loop)
12810 : 67 : lss = loop->ss;
12811 : : else
12812 : 130 : lss = gfc_ss_terminator;
12813 : :
12814 : : actual = actual_args;
12815 : 1182 : for (n = 0; n < 5; n++, actual = actual->next)
12816 : : {
12817 : 985 : arg[n] = actual->expr;
12818 : 985 : gfc_init_se (&argse[n], NULL);
12819 : :
12820 : 985 : if (lss != gfc_ss_terminator)
12821 : : {
12822 : 335 : gfc_copy_loopinfo_to_se (&argse[n], loop);
12823 : : /* Find the ss for the expression if it is there. */
12824 : 335 : argse[n].ss = lss;
12825 : 335 : gfc_mark_ss_chain_used (lss, 1);
12826 : : }
12827 : :
12828 : 985 : gfc_conv_expr (&argse[n], arg[n]);
12829 : :
12830 : 985 : if (loop)
12831 : 335 : lss = argse[n].ss;
12832 : : }
12833 : :
12834 : 197 : from = argse[0].expr;
12835 : 197 : frompos = argse[1].expr;
12836 : 197 : len = argse[2].expr;
12837 : 197 : to = argse[3].expr;
12838 : 197 : topos = argse[4].expr;
12839 : :
12840 : : /* The type of the result (TO). */
12841 : 197 : type = TREE_TYPE (to);
12842 : 197 : bitsize = build_int_cst (integer_type_node, TYPE_PRECISION (type));
12843 : :
12844 : : /* Optionally generate code for runtime argument check. */
12845 : 197 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
12846 : : {
12847 : 18 : tree nbits, below, ccond;
12848 : 18 : tree fp = fold_convert (long_integer_type_node, frompos);
12849 : 18 : tree ln = fold_convert (long_integer_type_node, len);
12850 : 18 : tree tp = fold_convert (long_integer_type_node, topos);
12851 : 18 : below = fold_build2_loc (input_location, LT_EXPR,
12852 : : logical_type_node, frompos,
12853 : 18 : build_int_cst (TREE_TYPE (frompos), 0));
12854 : 18 : above = fold_build2_loc (input_location, GT_EXPR,
12855 : : logical_type_node, frompos,
12856 : 18 : fold_convert (TREE_TYPE (frompos), bitsize));
12857 : 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
12858 : : logical_type_node, below, above);
12859 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
12860 : 18 : &arg[1]->where,
12861 : : "FROMPOS argument (%ld) out of range 0:%d "
12862 : : "in intrinsic MVBITS", fp, bitsize);
12863 : 18 : below = fold_build2_loc (input_location, LT_EXPR,
12864 : : logical_type_node, len,
12865 : 18 : build_int_cst (TREE_TYPE (len), 0));
12866 : 18 : above = fold_build2_loc (input_location, GT_EXPR,
12867 : : logical_type_node, len,
12868 : 18 : fold_convert (TREE_TYPE (len), bitsize));
12869 : 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
12870 : : logical_type_node, below, above);
12871 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[2].pre,
12872 : 18 : &arg[2]->where,
12873 : : "LEN argument (%ld) out of range 0:%d "
12874 : : "in intrinsic MVBITS", ln, bitsize);
12875 : 18 : below = fold_build2_loc (input_location, LT_EXPR,
12876 : : logical_type_node, topos,
12877 : 18 : build_int_cst (TREE_TYPE (topos), 0));
12878 : 18 : above = fold_build2_loc (input_location, GT_EXPR,
12879 : : logical_type_node, topos,
12880 : 18 : fold_convert (TREE_TYPE (topos), bitsize));
12881 : 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
12882 : : logical_type_node, below, above);
12883 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
12884 : 18 : &arg[4]->where,
12885 : : "TOPOS argument (%ld) out of range 0:%d "
12886 : : "in intrinsic MVBITS", tp, bitsize);
12887 : :
12888 : : /* The tests above ensure that FROMPOS, LEN and TOPOS fit into short
12889 : : integers. Additions below cannot overflow. */
12890 : 18 : nbits = fold_convert (long_integer_type_node, bitsize);
12891 : 18 : above = fold_build2_loc (input_location, PLUS_EXPR,
12892 : : long_integer_type_node, fp, ln);
12893 : 18 : ccond = fold_build2_loc (input_location, GT_EXPR,
12894 : : logical_type_node, above, nbits);
12895 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
12896 : : &arg[1]->where,
12897 : : "FROMPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
12898 : : "in intrinsic MVBITS", fp, ln, bitsize);
12899 : 18 : above = fold_build2_loc (input_location, PLUS_EXPR,
12900 : : long_integer_type_node, tp, ln);
12901 : 18 : ccond = fold_build2_loc (input_location, GT_EXPR,
12902 : : logical_type_node, above, nbits);
12903 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
12904 : : &arg[4]->where,
12905 : : "TOPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
12906 : : "in intrinsic MVBITS", tp, ln, bitsize);
12907 : : }
12908 : :
12909 : 1182 : for (n = 0; n < 5; n++)
12910 : : {
12911 : 985 : gfc_add_block_to_block (&se->pre, &argse[n].pre);
12912 : 985 : gfc_add_block_to_block (&se->post, &argse[n].post);
12913 : : }
12914 : :
12915 : : /* lenmask = (LEN >= bit_size (TYPE)) ? ~(TYPE)0 : ((TYPE)1 << LEN) - 1 */
12916 : 197 : above = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
12917 : 197 : len, fold_convert (TREE_TYPE (len), bitsize));
12918 : 197 : mask1 = build_int_cst (type, -1);
12919 : 197 : mask2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
12920 : : build_int_cst (type, 1), len);
12921 : 197 : mask2 = fold_build2_loc (input_location, MINUS_EXPR, type,
12922 : : mask2, build_int_cst (type, 1));
12923 : 197 : lenmask = fold_build3_loc (input_location, COND_EXPR, type,
12924 : : above, mask1, mask2);
12925 : :
12926 : : /* newbits = (((UTYPE)(FROM) >> FROMPOS) & lenmask) << TOPOS.
12927 : : * For valid frompos+len <= bit_size(FROM) the conversion to unsigned is
12928 : : * not strictly necessary; artificial bits from rshift will be masked. */
12929 : 197 : utype = unsigned_type_for (type);
12930 : 197 : newbits = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
12931 : : fold_convert (utype, from), frompos);
12932 : 197 : newbits = fold_build2_loc (input_location, BIT_AND_EXPR, type,
12933 : : fold_convert (type, newbits), lenmask);
12934 : 197 : newbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
12935 : : newbits, topos);
12936 : :
12937 : : /* oldbits = TO & (~(lenmask << TOPOS)). */
12938 : 197 : oldbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
12939 : : lenmask, topos);
12940 : 197 : oldbits = fold_build1_loc (input_location, BIT_NOT_EXPR, type, oldbits);
12941 : 197 : oldbits = fold_build2_loc (input_location, BIT_AND_EXPR, type, oldbits, to);
12942 : :
12943 : : /* TO = newbits | oldbits. */
12944 : 197 : se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
12945 : : oldbits, newbits);
12946 : :
12947 : : /* Return the assignment. */
12948 : 197 : se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
12949 : : void_type_node, to, se->expr);
12950 : 197 : }
12951 : :
12952 : : /* Comes from trans-stmt.cc, but we don't want the whole header included. */
12953 : : extern void gfc_trans_sync_stat (struct sync_stat *sync_stat, gfc_se *se,
12954 : : tree *stat, tree *errmsg, tree *errmsg_len);
12955 : :
12956 : : static tree
12957 : 236 : conv_intrinsic_move_alloc (gfc_code *code)
12958 : : {
12959 : 236 : stmtblock_t block;
12960 : 236 : gfc_expr *from_expr, *to_expr;
12961 : 236 : gfc_se from_se, to_se;
12962 : 236 : tree tmp, to_tree, from_tree, stat, errmsg, errmsg_len, fin_label = NULL_TREE;
12963 : 236 : bool coarray, from_is_class, from_is_scalar;
12964 : 236 : gfc_actual_arglist *arg = code->ext.actual;
12965 : 236 : sync_stat tmp_sync_stat = {nullptr, nullptr};
12966 : :
12967 : 236 : gfc_start_block (&block);
12968 : :
12969 : 236 : from_expr = arg->expr;
12970 : 236 : arg = arg->next;
12971 : 236 : to_expr = arg->expr;
12972 : 236 : arg = arg->next;
12973 : :
12974 : 708 : while (arg)
12975 : : {
12976 : 472 : if (arg->expr)
12977 : : {
12978 : 0 : if (!strcmp ("stat", arg->name))
12979 : 0 : tmp_sync_stat.stat = arg->expr;
12980 : 0 : else if (!strcmp ("errmsg", arg->name))
12981 : 0 : tmp_sync_stat.errmsg = arg->expr;
12982 : : }
12983 : 472 : arg = arg->next;
12984 : : }
12985 : :
12986 : 236 : gfc_init_se (&from_se, NULL);
12987 : 236 : gfc_init_se (&to_se, NULL);
12988 : :
12989 : 236 : gfc_trans_sync_stat (&tmp_sync_stat, &from_se, &stat, &errmsg, &errmsg_len);
12990 : 236 : if (stat != null_pointer_node)
12991 : 0 : fin_label = gfc_build_label_decl (NULL_TREE);
12992 : :
12993 : 236 : gcc_assert (from_expr->ts.type != BT_CLASS || to_expr->ts.type == BT_CLASS);
12994 : 236 : coarray = from_expr->corank != 0;
12995 : :
12996 : 236 : from_is_class = from_expr->ts.type == BT_CLASS;
12997 : 236 : from_is_scalar = from_expr->rank == 0 && !coarray;
12998 : 236 : if (to_expr->ts.type == BT_CLASS || from_is_scalar)
12999 : : {
13000 : 163 : from_se.want_pointer = 1;
13001 : 163 : if (from_is_scalar)
13002 : 115 : gfc_conv_expr (&from_se, from_expr);
13003 : : else
13004 : 48 : gfc_conv_expr_descriptor (&from_se, from_expr);
13005 : 163 : if (from_is_class)
13006 : 64 : from_tree = gfc_class_data_get (from_se.expr);
13007 : : else
13008 : : {
13009 : 99 : gfc_symbol *vtab;
13010 : 99 : from_tree = from_se.expr;
13011 : :
13012 : 99 : if (to_expr->ts.type == BT_CLASS)
13013 : : {
13014 : 36 : vtab = gfc_find_vtab (&from_expr->ts);
13015 : 36 : gcc_assert (vtab);
13016 : 36 : from_se.expr = gfc_get_symbol_decl (vtab);
13017 : : }
13018 : : }
13019 : 163 : gfc_add_block_to_block (&block, &from_se.pre);
13020 : :
13021 : 163 : to_se.want_pointer = 1;
13022 : 163 : if (to_expr->rank == 0)
13023 : 115 : gfc_conv_expr (&to_se, to_expr);
13024 : : else
13025 : 48 : gfc_conv_expr_descriptor (&to_se, to_expr);
13026 : 163 : if (to_expr->ts.type == BT_CLASS)
13027 : 100 : to_tree = gfc_class_data_get (to_se.expr);
13028 : : else
13029 : 63 : to_tree = to_se.expr;
13030 : 163 : gfc_add_block_to_block (&block, &to_se.pre);
13031 : :
13032 : : /* Deallocate "to". */
13033 : 163 : if (to_expr->rank == 0)
13034 : : {
13035 : 115 : tmp = gfc_deallocate_scalar_with_status (to_tree, stat, fin_label,
13036 : : true, to_expr, to_expr->ts,
13037 : : NULL_TREE, false, true,
13038 : : errmsg, errmsg_len);
13039 : 115 : gfc_add_expr_to_block (&block, tmp);
13040 : : }
13041 : :
13042 : 163 : if (from_is_scalar)
13043 : : {
13044 : : /* Assign (_data) pointers. */
13045 : 115 : gfc_add_modify_loc (input_location, &block, to_tree,
13046 : 115 : fold_convert (TREE_TYPE (to_tree), from_tree));
13047 : :
13048 : : /* Set "from" to NULL. */
13049 : 115 : gfc_add_modify_loc (input_location, &block, from_tree,
13050 : 115 : fold_convert (TREE_TYPE (from_tree),
13051 : : null_pointer_node));
13052 : :
13053 : 115 : gfc_add_block_to_block (&block, &from_se.post);
13054 : : }
13055 : 163 : gfc_add_block_to_block (&block, &to_se.post);
13056 : :
13057 : : /* Set _vptr. */
13058 : 163 : if (to_expr->ts.type == BT_CLASS)
13059 : : {
13060 : 100 : gfc_class_set_vptr (&block, to_se.expr, from_se.expr);
13061 : 100 : if (from_is_class)
13062 : 64 : gfc_reset_vptr (&block, from_expr);
13063 : 100 : if (UNLIMITED_POLY (to_expr))
13064 : : {
13065 : 20 : tree to_len = gfc_class_len_get (to_se.class_container);
13066 : 20 : tmp = from_expr->ts.type == BT_CHARACTER && from_se.string_length
13067 : 20 : ? from_se.string_length
13068 : : : size_zero_node;
13069 : 20 : gfc_add_modify_loc (input_location, &block, to_len,
13070 : 20 : fold_convert (TREE_TYPE (to_len), tmp));
13071 : : }
13072 : : }
13073 : :
13074 : 163 : if (from_is_scalar)
13075 : : {
13076 : 115 : if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13077 : : {
13078 : 6 : gfc_add_modify_loc (input_location, &block, to_se.string_length,
13079 : 6 : fold_convert (TREE_TYPE (to_se.string_length),
13080 : : from_se.string_length));
13081 : 6 : if (from_expr->ts.deferred)
13082 : 6 : gfc_add_modify_loc (
13083 : : input_location, &block, from_se.string_length,
13084 : 6 : build_int_cst (TREE_TYPE (from_se.string_length), 0));
13085 : : }
13086 : 115 : if (UNLIMITED_POLY (from_expr))
13087 : 2 : gfc_reset_len (&block, from_expr);
13088 : :
13089 : 115 : return gfc_finish_block (&block);
13090 : : }
13091 : :
13092 : 48 : gfc_init_se (&to_se, NULL);
13093 : 48 : gfc_init_se (&from_se, NULL);
13094 : : }
13095 : :
13096 : : /* Deallocate "to". */
13097 : 121 : if (from_expr->rank == 0)
13098 : : {
13099 : 3 : to_se.want_coarray = 1;
13100 : 3 : from_se.want_coarray = 1;
13101 : : }
13102 : 121 : gfc_conv_expr_descriptor (&to_se, to_expr);
13103 : 121 : gfc_conv_expr_descriptor (&from_se, from_expr);
13104 : :
13105 : : /* For coarrays, call SYNC ALL if TO is already deallocated as MOVE_ALLOC
13106 : : is an image control "statement", cf. IR F08/0040 in 12-006A. */
13107 : 121 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
13108 : : {
13109 : 4 : tree cond;
13110 : :
13111 : 4 : tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
13112 : : fin_label, true, to_expr,
13113 : : GFC_CAF_COARRAY_DEALLOCATE_ONLY,
13114 : : NULL_TREE, NULL_TREE,
13115 : : gfc_conv_descriptor_token (to_se.expr),
13116 : : true);
13117 : 4 : gfc_add_expr_to_block (&block, tmp);
13118 : :
13119 : 4 : tmp = gfc_conv_descriptor_data_get (to_se.expr);
13120 : 4 : cond = fold_build2_loc (input_location, EQ_EXPR,
13121 : : logical_type_node, tmp,
13122 : 4 : fold_convert (TREE_TYPE (tmp),
13123 : : null_pointer_node));
13124 : 4 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
13125 : : 3, null_pointer_node, null_pointer_node,
13126 : : integer_zero_node);
13127 : :
13128 : 4 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
13129 : : tmp, build_empty_stmt (input_location));
13130 : 4 : gfc_add_expr_to_block (&block, tmp);
13131 : 4 : }
13132 : : else
13133 : : {
13134 : 117 : if (to_expr->ts.type == BT_DERIVED
13135 : 25 : && to_expr->ts.u.derived->attr.alloc_comp)
13136 : : {
13137 : 19 : tmp = gfc_deallocate_alloc_comp (to_expr->ts.u.derived,
13138 : : to_se.expr, to_expr->rank);
13139 : 19 : gfc_add_expr_to_block (&block, tmp);
13140 : : }
13141 : :
13142 : 117 : tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
13143 : : fin_label, true, to_expr,
13144 : : GFC_CAF_COARRAY_NOCOARRAY, NULL_TREE,
13145 : : NULL_TREE, NULL_TREE, true);
13146 : 117 : gfc_add_expr_to_block (&block, tmp);
13147 : : }
13148 : :
13149 : : /* Copy the array descriptor data. */
13150 : 121 : gfc_add_modify_loc (input_location, &block, to_se.expr, from_se.expr);
13151 : :
13152 : : /* Set "from" to NULL. */
13153 : 121 : tmp = gfc_conv_descriptor_data_get (from_se.expr);
13154 : 121 : gfc_add_modify_loc (input_location, &block, tmp,
13155 : 121 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
13156 : :
13157 : 121 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
13158 : : {
13159 : : /* Copy the array descriptor data has overwritten the to-token and cleared
13160 : : from.data. Now also clear the from.token. */
13161 : 4 : gfc_add_modify (&block, gfc_conv_descriptor_token (from_se.expr),
13162 : : null_pointer_node);
13163 : : }
13164 : :
13165 : 121 : if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13166 : : {
13167 : 7 : gfc_add_modify_loc (input_location, &block, to_se.string_length,
13168 : 7 : fold_convert (TREE_TYPE (to_se.string_length),
13169 : : from_se.string_length));
13170 : 7 : if (from_expr->ts.deferred)
13171 : 6 : gfc_add_modify_loc (input_location, &block, from_se.string_length,
13172 : 6 : build_int_cst (TREE_TYPE (from_se.string_length), 0));
13173 : : }
13174 : 121 : if (fin_label)
13175 : 0 : gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, fin_label));
13176 : :
13177 : 121 : return gfc_finish_block (&block);
13178 : : }
13179 : :
13180 : :
13181 : : tree
13182 : 5871 : gfc_conv_intrinsic_subroutine (gfc_code *code)
13183 : : {
13184 : 5871 : tree res;
13185 : :
13186 : 5871 : gcc_assert (code->resolved_isym);
13187 : :
13188 : 5871 : switch (code->resolved_isym->id)
13189 : : {
13190 : 236 : case GFC_ISYM_MOVE_ALLOC:
13191 : 236 : res = conv_intrinsic_move_alloc (code);
13192 : 236 : break;
13193 : :
13194 : 10 : case GFC_ISYM_ATOMIC_CAS:
13195 : 10 : res = conv_intrinsic_atomic_cas (code);
13196 : 10 : break;
13197 : :
13198 : 68 : case GFC_ISYM_ATOMIC_ADD:
13199 : 68 : case GFC_ISYM_ATOMIC_AND:
13200 : 68 : case GFC_ISYM_ATOMIC_DEF:
13201 : 68 : case GFC_ISYM_ATOMIC_OR:
13202 : 68 : case GFC_ISYM_ATOMIC_XOR:
13203 : 68 : case GFC_ISYM_ATOMIC_FETCH_ADD:
13204 : 68 : case GFC_ISYM_ATOMIC_FETCH_AND:
13205 : 68 : case GFC_ISYM_ATOMIC_FETCH_OR:
13206 : 68 : case GFC_ISYM_ATOMIC_FETCH_XOR:
13207 : 68 : res = conv_intrinsic_atomic_op (code);
13208 : 68 : break;
13209 : :
13210 : 119 : case GFC_ISYM_ATOMIC_REF:
13211 : 119 : res = conv_intrinsic_atomic_ref (code);
13212 : 119 : break;
13213 : :
13214 : 70 : case GFC_ISYM_EVENT_QUERY:
13215 : 70 : res = conv_intrinsic_event_query (code);
13216 : 70 : break;
13217 : :
13218 : 2758 : case GFC_ISYM_C_F_POINTER:
13219 : 2758 : case GFC_ISYM_C_F_PROCPOINTER:
13220 : 2758 : res = conv_isocbinding_subroutine (code);
13221 : 2758 : break;
13222 : :
13223 : 293 : case GFC_ISYM_CAF_SEND:
13224 : 293 : res = conv_caf_send_to_remote (code);
13225 : 293 : break;
13226 : :
13227 : 97 : case GFC_ISYM_CAF_SENDGET:
13228 : 97 : res = conv_caf_sendget (code);
13229 : 97 : break;
13230 : :
13231 : 63 : case GFC_ISYM_CO_BROADCAST:
13232 : 63 : case GFC_ISYM_CO_MIN:
13233 : 63 : case GFC_ISYM_CO_MAX:
13234 : 63 : case GFC_ISYM_CO_REDUCE:
13235 : 63 : case GFC_ISYM_CO_SUM:
13236 : 63 : res = conv_co_collective (code);
13237 : 63 : break;
13238 : :
13239 : 10 : case GFC_ISYM_FREE:
13240 : 10 : res = conv_intrinsic_free (code);
13241 : 10 : break;
13242 : :
13243 : 90 : case GFC_ISYM_RANDOM_INIT:
13244 : 90 : res = conv_intrinsic_random_init (code);
13245 : 90 : break;
13246 : :
13247 : 15 : case GFC_ISYM_KILL:
13248 : 15 : res = conv_intrinsic_kill_sub (code);
13249 : 15 : break;
13250 : :
13251 : : case GFC_ISYM_MVBITS:
13252 : : res = NULL_TREE;
13253 : : break;
13254 : :
13255 : 194 : case GFC_ISYM_SYSTEM_CLOCK:
13256 : 194 : res = conv_intrinsic_system_clock (code);
13257 : 194 : break;
13258 : :
13259 : : default:
13260 : : res = NULL_TREE;
13261 : : break;
13262 : : }
13263 : :
13264 : 5871 : return res;
13265 : : }
13266 : :
13267 : : #include "gt-fortran-trans-intrinsic.h"
|