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 : 408862 : 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 : 29987 : 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 : 75266 : gfc_conv_intrinsic_function_args (gfc_se *se, gfc_expr *expr,
203 : : tree *argarray, int nargs)
204 : : {
205 : 75266 : gfc_actual_arglist *actual;
206 : 75266 : gfc_expr *e;
207 : 75266 : gfc_intrinsic_arg *formal;
208 : 75266 : gfc_se argse;
209 : 75266 : int curr_arg;
210 : :
211 : 75266 : formal = expr->value.function.isym->formal;
212 : 75266 : actual = expr->value.function.actual;
213 : :
214 : 170258 : for (curr_arg = 0; curr_arg < nargs; curr_arg++,
215 : 94992 : actual = actual->next,
216 : 94992 : formal = formal ? formal->next : NULL)
217 : : {
218 : 94992 : gcc_assert (actual);
219 : 94992 : e = actual->expr;
220 : : /* Skip omitted optional arguments. */
221 : 94992 : if (!e)
222 : : {
223 : 31 : --curr_arg;
224 : 31 : continue;
225 : : }
226 : :
227 : : /* Evaluate the parameter. This will substitute scalarized
228 : : references automatically. */
229 : 94961 : gfc_init_se (&argse, se);
230 : :
231 : 94961 : if (e->ts.type == BT_CHARACTER)
232 : : {
233 : 9518 : gfc_conv_expr (&argse, e);
234 : 9518 : gfc_conv_string_parameter (&argse);
235 : 9518 : argarray[curr_arg++] = argse.string_length;
236 : 9518 : gcc_assert (curr_arg < nargs);
237 : : }
238 : : else
239 : 85443 : 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 : 94961 : if (e->expr_type == EXPR_VARIABLE
244 : 50879 : && 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 : 94961 : gfc_add_block_to_block (&se->pre, &argse.pre);
250 : 94961 : gfc_add_block_to_block (&se->post, &argse.post);
251 : 94961 : argarray[curr_arg] = argse.expr;
252 : : }
253 : 75266 : }
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 : 51345 : gfc_intrinsic_argument_list_length (gfc_expr *expr)
260 : : {
261 : 51345 : int n = 0;
262 : 51345 : gfc_actual_arglist *actual;
263 : :
264 : 117671 : for (actual = expr->value.function.actual; actual; actual = actual->next)
265 : : {
266 : 66326 : if (!actual->expr)
267 : 6325 : continue;
268 : :
269 : 60001 : if (actual->expr->ts.type == BT_CHARACTER)
270 : 4498 : n += 2;
271 : : else
272 : 55503 : n++;
273 : : }
274 : :
275 : 51345 : 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 : 35559 : gfc_conv_intrinsic_conversion (gfc_se * se, gfc_expr * expr)
284 : : {
285 : 35559 : tree type;
286 : 35559 : tree *args;
287 : 35559 : int nargs;
288 : :
289 : 35559 : nargs = gfc_intrinsic_argument_list_length (expr);
290 : 35559 : 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 : 35559 : type = gfc_typenode_for_spec (&expr->ts);
296 : 35559 : gcc_assert (expr->value.function.actual->expr);
297 : 35559 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
298 : :
299 : : /* Conversion between character kinds involves a call to a library
300 : : function. */
301 : 35559 : 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 : 35351 : if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
338 : 35351 : && 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 : 35351 : 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 : 132 : 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 : 1465 : build_fix_expr (stmtblock_t * pblock, tree arg, tree type,
422 : : enum rounding_mode op)
423 : : {
424 : 1465 : 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 : 1171 : case RND_TRUNC:
436 : 1171 : 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 : 2943 : gfc_conv_intrinsic_int (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
530 : : {
531 : 2943 : tree type;
532 : 2943 : tree *args;
533 : 2943 : int nargs;
534 : :
535 : 2943 : nargs = gfc_intrinsic_argument_list_length (expr);
536 : 2943 : 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 : 2943 : type = gfc_typenode_for_spec (&expr->ts);
541 : 2943 : gcc_assert (expr->value.function.actual->expr);
542 : 2943 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
543 : :
544 : 2943 : if (TREE_CODE (TREE_TYPE (args[0])) == INTEGER_TYPE)
545 : : {
546 : : /* Conversion to a different integer kind. */
547 : 1478 : 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 : 1465 : if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
554 : 1465 : && 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 : 1465 : se->expr = build_fix_expr (&se->pre, args[0], type, op);
564 : : }
565 : 2943 : }
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 : 638988 : define_quad_builtin (const char *name, tree type, bool is_const)
596 : : {
597 : 638988 : tree fndecl;
598 : 638988 : fndecl = build_decl (input_location, FUNCTION_DECL, get_identifier (name),
599 : : type);
600 : :
601 : : /* Mark the decl as external. */
602 : 638988 : DECL_EXTERNAL (fndecl) = 1;
603 : 638988 : TREE_PUBLIC (fndecl) = 1;
604 : :
605 : : /* Mark it __attribute__((const)). */
606 : 638988 : TREE_READONLY (fndecl) = is_const;
607 : :
608 : 638988 : rest_of_decl_compilation (fndecl, 1, 0);
609 : :
610 : 638988 : 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 : 38336480 : add_simd_flag_for_built_in (tree fndecl)
618 : : {
619 : 38336480 : if (gfc_vectorized_builtins == NULL
620 : 15461680 : || fndecl == NULL_TREE)
621 : 31378724 : return;
622 : :
623 : 6957756 : const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
624 : 6957756 : int *clauses = gfc_vectorized_builtins->get (name);
625 : 6957756 : if (clauses)
626 : : {
627 : 4756828 : for (unsigned i = 0; i < 3; i++)
628 : 3567621 : if (*clauses & (1 << i))
629 : : {
630 : 1189212 : gfc_simd_clause simd_type = (gfc_simd_clause)*clauses;
631 : 1189212 : tree omp_clause = NULL_TREE;
632 : 1189212 : if (simd_type == SIMD_NONE)
633 : : ; /* No SIMD clause. */
634 : : else
635 : : {
636 : 2378424 : omp_clause_code code
637 : : = (simd_type == SIMD_INBRANCH
638 : 1189212 : ? OMP_CLAUSE_INBRANCH : OMP_CLAUSE_NOTINBRANCH);
639 : 1189212 : omp_clause = build_omp_clause (UNKNOWN_LOCATION, code);
640 : 1189212 : omp_clause = build_tree_list (NULL_TREE, omp_clause);
641 : : }
642 : :
643 : 1189212 : DECL_ATTRIBUTES (fndecl)
644 : 2378424 : = tree_cons (get_identifier ("omp declare simd"), omp_clause,
645 : 1189212 : 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 : 73724 : gfc_adjust_builtins (void)
655 : : {
656 : 73724 : gfc_intrinsic_map_t *m;
657 : 3907372 : for (m = gfc_intrinsic_map;
658 : 3907372 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
659 : : {
660 : 3833648 : add_simd_flag_for_built_in (m->real4_decl);
661 : 3833648 : add_simd_flag_for_built_in (m->complex4_decl);
662 : 3833648 : add_simd_flag_for_built_in (m->real8_decl);
663 : 3833648 : add_simd_flag_for_built_in (m->complex8_decl);
664 : 3833648 : add_simd_flag_for_built_in (m->real10_decl);
665 : 3833648 : add_simd_flag_for_built_in (m->complex10_decl);
666 : 3833648 : add_simd_flag_for_built_in (m->real16_decl);
667 : 3833648 : add_simd_flag_for_built_in (m->complex16_decl);
668 : 3833648 : add_simd_flag_for_built_in (m->real16_decl);
669 : 3833648 : add_simd_flag_for_built_in (m->complex16_decl);
670 : : }
671 : :
672 : : /* Release all strings. */
673 : 73724 : if (gfc_vectorized_builtins != NULL)
674 : : {
675 : 1635161 : for (hash_map<nofree_string_hash, int>::iterator it
676 : 29734 : = gfc_vectorized_builtins->begin ();
677 : 1635161 : it != gfc_vectorized_builtins->end (); ++it)
678 : 1605427 : free (CONST_CAST (char *, (*it).first));
679 : :
680 : 59468 : delete gfc_vectorized_builtins;
681 : 29734 : gfc_vectorized_builtins = NULL;
682 : : }
683 : 73724 : }
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 : 30428 : gfc_build_intrinsic_lib_fndecls (void)
690 : : {
691 : 30428 : gfc_intrinsic_map_t *m;
692 : 30428 : tree quad_decls[END_BUILTINS + 1];
693 : :
694 : 30428 : 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 : 30428 : tree type, complex_type, func_1, func_2, func_3, func_cabs, func_frexp;
701 : 30428 : tree func_iround, func_lround, func_llround, func_scalbn, func_cpow;
702 : :
703 : 30428 : memset (quad_decls, 0, sizeof(tree) * (END_BUILTINS + 1));
704 : :
705 : 30428 : type = gfc_float128_type_node;
706 : 30428 : complex_type = gfc_complex_float128_type_node;
707 : : /* type (*) (type) */
708 : 30428 : func_1 = build_function_type_list (type, type, NULL_TREE);
709 : : /* int (*) (type) */
710 : 30428 : func_iround = build_function_type_list (integer_type_node,
711 : : type, NULL_TREE);
712 : : /* long (*) (type) */
713 : 30428 : func_lround = build_function_type_list (long_integer_type_node,
714 : : type, NULL_TREE);
715 : : /* long long (*) (type) */
716 : 30428 : func_llround = build_function_type_list (long_long_integer_type_node,
717 : : type, NULL_TREE);
718 : : /* type (*) (type, type) */
719 : 30428 : func_2 = build_function_type_list (type, type, type, NULL_TREE);
720 : : /* type (*) (type, type, type) */
721 : 30428 : func_3 = build_function_type_list (type, type, type, type, NULL_TREE);
722 : : /* type (*) (type, &int) */
723 : 30428 : func_frexp
724 : 30428 : = build_function_type_list (type,
725 : : type,
726 : : build_pointer_type (integer_type_node),
727 : : NULL_TREE);
728 : : /* type (*) (type, int) */
729 : 30428 : func_scalbn = build_function_type_list (type,
730 : : type, integer_type_node, NULL_TREE);
731 : : /* type (*) (complex type) */
732 : 30428 : func_cabs = build_function_type_list (type, complex_type, NULL_TREE);
733 : : /* complex type (*) (complex type, complex type) */
734 : 30428 : func_cpow
735 : 30428 : = 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 : 30428 : quad_decls[BUILT_IN_SQRT]
763 : 30428 : = define_quad_builtin (gfc_real16_use_iec_60559
764 : : ? "sqrtf128" : "sqrtq", func_1, true);
765 : : }
766 : :
767 : : /* Add GCC builtin functions. */
768 : 1582256 : for (m = gfc_intrinsic_map;
769 : 1612684 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
770 : : {
771 : 1582256 : if (m->float_built_in != END_BUILTINS)
772 : 1460544 : m->real4_decl = builtin_decl_explicit (m->float_built_in);
773 : 1582256 : if (m->complex_float_built_in != END_BUILTINS)
774 : 486848 : m->complex4_decl = builtin_decl_explicit (m->complex_float_built_in);
775 : 1582256 : if (m->double_built_in != END_BUILTINS)
776 : 1460544 : m->real8_decl = builtin_decl_explicit (m->double_built_in);
777 : 1582256 : if (m->complex_double_built_in != END_BUILTINS)
778 : 486848 : m->complex8_decl = builtin_decl_explicit (m->complex_double_built_in);
779 : :
780 : : /* If real(kind=10) exists, it is always long double. */
781 : 1582256 : if (m->long_double_built_in != END_BUILTINS)
782 : 1460544 : m->real10_decl = builtin_decl_explicit (m->long_double_built_in);
783 : 1582256 : if (m->complex_long_double_built_in != END_BUILTINS)
784 : 486848 : m->complex10_decl
785 : 486848 : = builtin_decl_explicit (m->complex_long_double_built_in);
786 : :
787 : 1582256 : 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 : 1582256 : 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 : 638988 : m->real16_decl = quad_decls[m->double_built_in];
801 : : }
802 : 943268 : 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 : 30428 : }
809 : :
810 : :
811 : : /* Create a fndecl for a simple intrinsic library function. */
812 : :
813 : : static tree
814 : 4302 : gfc_get_intrinsic_lib_fndecl (gfc_intrinsic_map_t * m, gfc_expr * expr)
815 : : {
816 : 4302 : tree type;
817 : 4302 : vec<tree, va_gc> *argtypes;
818 : 4302 : tree fndecl;
819 : 4302 : gfc_actual_arglist *actual;
820 : 4302 : tree *pdecl;
821 : 4302 : gfc_typespec *ts;
822 : 4302 : char name[GFC_MAX_SYMBOL_LEN + 3];
823 : :
824 : 4302 : ts = &expr->ts;
825 : 4302 : if (ts->type == BT_REAL)
826 : : {
827 : 3458 : switch (ts->kind)
828 : : {
829 : 1241 : case 4:
830 : 1241 : pdecl = &m->real4_decl;
831 : 1241 : break;
832 : 1248 : case 8:
833 : 1248 : pdecl = &m->real8_decl;
834 : 1248 : break;
835 : 551 : case 10:
836 : 551 : pdecl = &m->real10_decl;
837 : 551 : break;
838 : 418 : case 16:
839 : 418 : pdecl = &m->real16_decl;
840 : 418 : 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 : 4302 : if (*pdecl)
871 : 3963 : return *pdecl;
872 : :
873 : 339 : if (m->libm_name)
874 : : {
875 : 162 : int n = gfc_validate_kind (BT_REAL, ts->kind, false);
876 : 162 : 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 : 162 : 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 : 162 : 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 : 162 : else if (gfc_real_kinds[n].c_float128)
886 : 162 : snprintf (name, sizeof (name), "%s%s%s",
887 : 162 : ts->type == BT_COMPLEX ? "c" : "", m->name,
888 : 162 : 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 : 339 : argtypes = NULL;
900 : 694 : for (actual = expr->value.function.actual; actual; actual = actual->next)
901 : : {
902 : 355 : type = gfc_typenode_for_spec (&actual->expr->ts);
903 : 355 : vec_safe_push (argtypes, type);
904 : : }
905 : 1017 : type = build_function_type_vec (gfc_typenode_for_spec (ts), argtypes);
906 : 339 : fndecl = build_decl (input_location,
907 : : FUNCTION_DECL, get_identifier (name), type);
908 : :
909 : : /* Mark the decl as external. */
910 : 339 : DECL_EXTERNAL (fndecl) = 1;
911 : 339 : TREE_PUBLIC (fndecl) = 1;
912 : :
913 : : /* Mark it __attribute__((const)), if possible. */
914 : 339 : TREE_READONLY (fndecl) = m->is_constant;
915 : :
916 : 339 : rest_of_decl_compilation (fndecl, 1, 0);
917 : :
918 : 339 : (*pdecl) = fndecl;
919 : 339 : return fndecl;
920 : : }
921 : :
922 : :
923 : : /* Convert an intrinsic function into an external or builtin call. */
924 : :
925 : : static void
926 : 3804 : gfc_conv_intrinsic_lib_function (gfc_se * se, gfc_expr * expr)
927 : : {
928 : 3804 : gfc_intrinsic_map_t *m;
929 : 3804 : tree fndecl;
930 : 3804 : tree rettype;
931 : 3804 : tree *args;
932 : 3804 : unsigned int num_args;
933 : 3804 : gfc_isym_id id;
934 : :
935 : 3804 : id = expr->value.function.isym->id;
936 : : /* Find the entry for this function. */
937 : 55270 : for (m = gfc_intrinsic_map;
938 : 55270 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
939 : : {
940 : 55270 : if (id == m->id)
941 : : break;
942 : : }
943 : :
944 : 3804 : 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 : 3804 : num_args = gfc_intrinsic_argument_list_length (expr);
952 : 3804 : args = XALLOCAVEC (tree, num_args);
953 : :
954 : 3804 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
955 : 3804 : fndecl = gfc_get_intrinsic_lib_fndecl (m, expr);
956 : 3804 : rettype = TREE_TYPE (TREE_TYPE (fndecl));
957 : :
958 : 3804 : fndecl = build_addr (fndecl);
959 : 3804 : se->expr = build_call_array_loc (input_location, rettype, fndecl, num_args, args);
960 : 3804 : }
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 : 1071 : conv_caf_func_index (stmtblock_t *block, gfc_namespace *ns, const char *pat,
1032 : : gfc_expr *hash)
1033 : : {
1034 : 1071 : char *name;
1035 : 1071 : gfc_se argse;
1036 : 1071 : gfc_expr func_index;
1037 : 1071 : gfc_symtree *index_st;
1038 : 1071 : tree func_index_tree;
1039 : 1071 : stmtblock_t blk;
1040 : :
1041 : : /* Need to get namespace where static variables are possible. */
1042 : 1071 : while (ns && ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
1043 : 0 : ns = ns->parent;
1044 : 1071 : gcc_assert (ns);
1045 : :
1046 : 1071 : name = xasprintf (pat, caf_call_cnt);
1047 : 1071 : gcc_assert (!gfc_get_sym_tree (name, ns, &index_st, false));
1048 : 1071 : free (name);
1049 : :
1050 : 1071 : index_st->n.sym->attr.flavor = FL_VARIABLE;
1051 : 1071 : index_st->n.sym->attr.save = SAVE_EXPLICIT;
1052 : 1071 : index_st->n.sym->value
1053 : 1071 : = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
1054 : : &gfc_current_locus);
1055 : 1071 : mpz_init_set_si (index_st->n.sym->value->value.integer, -1);
1056 : 1071 : index_st->n.sym->ts.type = BT_INTEGER;
1057 : 1071 : index_st->n.sym->ts.kind = gfc_default_integer_kind;
1058 : 1071 : gfc_set_sym_referenced (index_st->n.sym);
1059 : 1071 : memset (&func_index, 0, sizeof (gfc_expr));
1060 : 1071 : gfc_clear_ts (&func_index.ts);
1061 : 1071 : func_index.expr_type = EXPR_VARIABLE;
1062 : 1071 : func_index.symtree = index_st;
1063 : 1071 : func_index.ts = index_st->n.sym->ts;
1064 : 1071 : gfc_commit_symbol (index_st->n.sym);
1065 : :
1066 : 1071 : gfc_init_se (&argse, NULL);
1067 : 1071 : gfc_conv_expr (&argse, &func_index);
1068 : 1071 : gfc_add_block_to_block (block, &argse.pre);
1069 : 1071 : func_index_tree = argse.expr;
1070 : :
1071 : 1071 : gfc_init_se (&argse, NULL);
1072 : 1071 : gfc_conv_expr (&argse, hash);
1073 : :
1074 : 1071 : gfc_init_block (&blk);
1075 : 1071 : gfc_add_modify (&blk, func_index_tree,
1076 : : build_call_expr (gfor_fndecl_caf_get_remote_function_index, 1,
1077 : : argse.expr));
1078 : 1071 : 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 : 1071 : build_int_cst (integer_type_node, -1)),
1083 : : PRED_FIRST_MATCH),
1084 : : gfc_finish_block (&blk), NULL_TREE));
1085 : :
1086 : 1071 : return func_index_tree;
1087 : : }
1088 : :
1089 : : static tree
1090 : 1071 : conv_caf_add_call_data (stmtblock_t *blk, gfc_namespace *ns, const char *pat,
1091 : : gfc_symbol *data_sym, tree *data_size)
1092 : : {
1093 : 1071 : char *name;
1094 : 1071 : gfc_symtree *data_st;
1095 : 1071 : gfc_constructor *con;
1096 : 1071 : gfc_expr data, data_init;
1097 : 1071 : gfc_se argse;
1098 : 1071 : tree data_tree;
1099 : :
1100 : 1071 : memset (&data, 0, sizeof (gfc_expr));
1101 : 1071 : gfc_clear_ts (&data.ts);
1102 : 1071 : data.expr_type = EXPR_VARIABLE;
1103 : 1071 : name = xasprintf (pat, caf_call_cnt);
1104 : 1071 : gcc_assert (!gfc_get_sym_tree (name, ns, &data_st, false));
1105 : 1071 : free (name);
1106 : 1071 : data_st->n.sym->attr.flavor = FL_VARIABLE;
1107 : 1071 : data_st->n.sym->ts = data_sym->ts;
1108 : 1071 : data.symtree = data_st;
1109 : 1071 : gfc_set_sym_referenced (data.symtree->n.sym);
1110 : 1071 : data.ts = data_st->n.sym->ts;
1111 : 1071 : gfc_commit_symbol (data_st->n.sym);
1112 : :
1113 : 1071 : memset (&data_init, 0, sizeof (gfc_expr));
1114 : 1071 : gfc_clear_ts (&data_init.ts);
1115 : 1071 : data_init.expr_type = EXPR_STRUCTURE;
1116 : 1071 : data_init.ts = data.ts;
1117 : 1269 : 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 : 1071 : 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 : 1004 : data_tree = build_zero_cst (pvoid_type_node);
1141 : 1004 : *data_size = build_zero_cst (size_type_node);
1142 : : }
1143 : :
1144 : 1071 : 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 : 1005 : conv_stat_and_team (stmtblock_t *block, gfc_expr *expr, tree *stat, tree *team,
1164 : : tree *team_no)
1165 : : {
1166 : 1005 : gfc_expr *stat_e, *team_e;
1167 : :
1168 : 1005 : stat_e = gfc_find_stat_co (expr);
1169 : 1005 : if (stat_e)
1170 : : {
1171 : 14 : gfc_se stat_se;
1172 : 14 : gfc_init_se (&stat_se, NULL);
1173 : 14 : gfc_conv_expr_reference (&stat_se, stat_e);
1174 : 14 : *stat = stat_se.expr;
1175 : 14 : gfc_add_block_to_block (block, &stat_se.pre);
1176 : 14 : gfc_add_block_to_block (block, &stat_se.post);
1177 : : }
1178 : : else
1179 : 991 : *stat = null_pointer_node;
1180 : :
1181 : 1005 : team_e = gfc_find_team_co (expr, TEAM_TEAM);
1182 : 1005 : if (team_e)
1183 : : {
1184 : 1 : gfc_se team_se;
1185 : 1 : gfc_init_se (&team_se, NULL);
1186 : 1 : gfc_conv_expr_reference (&team_se, team_e);
1187 : 1 : *team = team_se.expr;
1188 : 1 : gfc_add_block_to_block (block, &team_se.pre);
1189 : 1 : gfc_add_block_to_block (block, &team_se.post);
1190 : : }
1191 : : else
1192 : 1004 : *team = null_pointer_node;
1193 : :
1194 : 1005 : team_e = gfc_find_team_co (expr, TEAM_NUMBER);
1195 : 1005 : if (team_e)
1196 : : {
1197 : 1 : gfc_se team_se;
1198 : 1 : gfc_init_se (&team_se, NULL);
1199 : 1 : gfc_conv_expr_reference (&team_se, team_e);
1200 : 1 : *team_no = team_se.expr;
1201 : 1 : gfc_add_block_to_block (block, &team_se.pre);
1202 : 1 : gfc_add_block_to_block (block, &team_se.post);
1203 : : }
1204 : : else
1205 : 1004 : *team_no = null_pointer_node;
1206 : 1005 : }
1207 : :
1208 : : /* Get data from a remote coarray. */
1209 : :
1210 : : static void
1211 : 889 : gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs,
1212 : : bool may_realloc, symbol_attribute *caf_attr)
1213 : : {
1214 : 889 : gfc_expr *array_expr;
1215 : 889 : tree caf_decl, token, image_index, tmp, res_var, type, stat, dest_size,
1216 : : dest_data, opt_dest_desc, get_fn_index_tree, add_data_tree, add_data_size,
1217 : : opt_src_desc, opt_src_charlen, opt_dest_charlen, team, team_no;
1218 : 889 : symbol_attribute caf_attr_store;
1219 : 889 : gfc_namespace *ns;
1220 : 889 : gfc_expr *get_fn_hash = expr->value.function.actual->next->expr,
1221 : 889 : *get_fn_expr = expr->value.function.actual->next->next->expr;
1222 : 889 : gfc_symbol *add_data_sym = get_fn_expr->symtree->n.sym->formal->sym;
1223 : :
1224 : 889 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1225 : :
1226 : 889 : if (se->ss && se->ss->info->useflags)
1227 : : {
1228 : : /* Access the previously obtained result. */
1229 : 352 : gfc_conv_tmp_array_ref (se);
1230 : 352 : return;
1231 : : }
1232 : :
1233 : 537 : array_expr = expr->value.function.actual->expr;
1234 : 1074 : ns = array_expr->expr_type == EXPR_VARIABLE
1235 : 537 : && !array_expr->symtree->n.sym->attr.associate_var
1236 : 1074 : ? array_expr->symtree->n.sym->ns
1237 : : : gfc_current_ns;
1238 : 537 : type = gfc_typenode_for_spec (&array_expr->ts);
1239 : :
1240 : 537 : if (caf_attr == NULL)
1241 : : {
1242 : 537 : caf_attr_store = gfc_caf_attr (array_expr);
1243 : 537 : caf_attr = &caf_attr_store;
1244 : : }
1245 : :
1246 : 537 : res_var = lhs;
1247 : :
1248 : 537 : conv_stat_and_team (&se->pre, expr, &stat, &team, &team_no);
1249 : :
1250 : 537 : get_fn_index_tree
1251 : 537 : = conv_caf_func_index (&se->pre, ns, "__caf_get_from_remote_fn_index_%d",
1252 : : get_fn_hash);
1253 : 537 : add_data_tree
1254 : 537 : = conv_caf_add_call_data (&se->pre, ns, "__caf_get_from_remote_add_data_%d",
1255 : : add_data_sym, &add_data_size);
1256 : 537 : ++caf_call_cnt;
1257 : :
1258 : 537 : if (array_expr->rank == 0)
1259 : : {
1260 : 184 : res_var = gfc_create_var (type, "caf_res");
1261 : 184 : if (array_expr->ts.type == BT_CHARACTER)
1262 : : {
1263 : 17 : gfc_conv_string_length (array_expr->ts.u.cl, array_expr, &se->pre);
1264 : 17 : se->string_length = array_expr->ts.u.cl->backend_decl;
1265 : 17 : opt_src_charlen = gfc_build_addr_expr (
1266 : : NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1267 : 17 : dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1268 : : }
1269 : : else
1270 : : {
1271 : 167 : dest_size = res_var->typed.type->type_common.size_unit;
1272 : 167 : opt_src_charlen
1273 : 167 : = build_zero_cst (build_pointer_type (size_type_node));
1274 : : }
1275 : 184 : dest_data
1276 : 184 : = gfc_evaluate_now (gfc_build_addr_expr (NULL_TREE, res_var), &se->pre);
1277 : 184 : res_var = build_fold_indirect_ref (dest_data);
1278 : 184 : dest_data = gfc_build_addr_expr (pvoid_type_node, dest_data);
1279 : 184 : opt_dest_desc = build_zero_cst (pvoid_type_node);
1280 : : }
1281 : : else
1282 : : {
1283 : : /* Create temporary. */
1284 : 353 : may_realloc = gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
1285 : : type, NULL_TREE, false, false,
1286 : : false, &array_expr->where)
1287 : : == NULL_TREE;
1288 : 353 : res_var = se->ss->info->data.array.descriptor;
1289 : 353 : if (array_expr->ts.type == BT_CHARACTER)
1290 : : {
1291 : 8 : se->string_length = array_expr->ts.u.cl->backend_decl;
1292 : 8 : opt_src_charlen = gfc_build_addr_expr (
1293 : : NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1294 : 8 : dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1295 : : }
1296 : : else
1297 : : {
1298 : 345 : opt_src_charlen
1299 : 345 : = build_zero_cst (build_pointer_type (size_type_node));
1300 : 345 : dest_size = fold_build2 (
1301 : : MULT_EXPR, size_type_node,
1302 : : fold_convert (size_type_node,
1303 : : array_expr->shape
1304 : : ? conv_shape_to_cst (array_expr)
1305 : : : gfc_conv_descriptor_size (res_var,
1306 : : array_expr->rank)),
1307 : : fold_convert (size_type_node,
1308 : : gfc_conv_descriptor_span_get (res_var)));
1309 : : }
1310 : 353 : opt_dest_desc = res_var;
1311 : 353 : dest_data = gfc_conv_descriptor_data_get (res_var);
1312 : 353 : opt_dest_desc = gfc_build_addr_expr (NULL_TREE, opt_dest_desc);
1313 : 353 : if (may_realloc)
1314 : : {
1315 : 52 : tmp = gfc_conv_descriptor_data_get (res_var);
1316 : 52 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
1317 : : NULL_TREE, NULL_TREE, true, NULL,
1318 : : GFC_CAF_COARRAY_NOCOARRAY);
1319 : 52 : gfc_add_expr_to_block (&se->post, tmp);
1320 : : }
1321 : 353 : dest_data
1322 : 353 : = gfc_build_addr_expr (NULL_TREE,
1323 : : gfc_trans_force_lval (&se->pre, dest_data));
1324 : : }
1325 : :
1326 : 537 : opt_dest_charlen = opt_src_charlen;
1327 : 537 : caf_decl = gfc_get_tree_for_caf_expr (array_expr);
1328 : 537 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1329 : 1 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1330 : :
1331 : 537 : if (!TYPE_LANG_SPECIFIC (TREE_TYPE (caf_decl))->rank
1332 : 537 : || GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl)))
1333 : 468 : opt_src_desc = build_zero_cst (pvoid_type_node);
1334 : : else
1335 : 69 : opt_src_desc = gfc_build_addr_expr (pvoid_type_node, caf_decl);
1336 : :
1337 : 537 : image_index = gfc_caf_get_image_index (&se->pre, array_expr, caf_decl);
1338 : 537 : gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, array_expr);
1339 : :
1340 : : /* It guarantees memory consistency within the same segment. */
1341 : 537 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1342 : 537 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1343 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1344 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1345 : 537 : ASM_VOLATILE_P (tmp) = 1;
1346 : 537 : gfc_add_expr_to_block (&se->pre, tmp);
1347 : :
1348 : 537 : tmp = build_call_expr_loc (
1349 : : input_location, gfor_fndecl_caf_get_from_remote, 15, token, opt_src_desc,
1350 : : opt_src_charlen, image_index, dest_size, dest_data, opt_dest_charlen,
1351 : : opt_dest_desc, constant_boolean_node (may_realloc, boolean_type_node),
1352 : : get_fn_index_tree, add_data_tree, add_data_size, stat, team, team_no);
1353 : :
1354 : 537 : gfc_add_expr_to_block (&se->pre, tmp);
1355 : :
1356 : 537 : if (se->ss)
1357 : 353 : gfc_advance_se_ss_chain (se);
1358 : :
1359 : 537 : se->expr = res_var;
1360 : :
1361 : 537 : return;
1362 : : }
1363 : :
1364 : : /* Generate call to caf_is_present_on_remote for allocated (coarrary[...])
1365 : : calls. */
1366 : :
1367 : : static void
1368 : 66 : gfc_conv_intrinsic_caf_is_present_remote (gfc_se *se, gfc_expr *e)
1369 : : {
1370 : 66 : gfc_expr *caf_expr, *hash, *present_fn;
1371 : 66 : gfc_symbol *add_data_sym;
1372 : 66 : tree fn_index, add_data_tree, add_data_size, caf_decl, image_index, token;
1373 : :
1374 : 66 : gcc_assert (e->expr_type == EXPR_FUNCTION
1375 : : && e->value.function.isym->id
1376 : : == GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE);
1377 : 66 : caf_expr = e->value.function.actual->expr;
1378 : 66 : hash = e->value.function.actual->next->expr;
1379 : 66 : present_fn = e->value.function.actual->next->next->expr;
1380 : 66 : add_data_sym = present_fn->symtree->n.sym->formal->sym;
1381 : :
1382 : 66 : fn_index = conv_caf_func_index (&se->pre, gfc_current_ns,
1383 : : "__caf_present_on_remote_fn_index_%d", hash);
1384 : 66 : add_data_tree = conv_caf_add_call_data (&se->pre, gfc_current_ns,
1385 : : "__caf_present_on_remote_add_data_%d",
1386 : : add_data_sym, &add_data_size);
1387 : 66 : ++caf_call_cnt;
1388 : :
1389 : 66 : caf_decl = gfc_get_tree_for_caf_expr (caf_expr);
1390 : 66 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1391 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1392 : :
1393 : 66 : image_index = gfc_caf_get_image_index (&se->pre, caf_expr, caf_decl);
1394 : 66 : gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, caf_expr);
1395 : :
1396 : 66 : se->expr
1397 : 66 : = fold_convert (logical_type_node,
1398 : : build_call_expr_loc (input_location,
1399 : : gfor_fndecl_caf_is_present_on_remote,
1400 : : 5, token, image_index, fn_index,
1401 : : add_data_tree, add_data_size));
1402 : 66 : }
1403 : :
1404 : : static tree
1405 : 288 : conv_caf_send_to_remote (gfc_code *code)
1406 : : {
1407 : 288 : gfc_expr *lhs_expr, *rhs_expr, *lhs_hash, *receiver_fn_expr;
1408 : 288 : gfc_symbol *add_data_sym;
1409 : 288 : gfc_se lhs_se, rhs_se;
1410 : 288 : stmtblock_t block;
1411 : 288 : gfc_namespace *ns;
1412 : 288 : tree caf_decl, token, rhs_size, image_index, tmp, rhs_data;
1413 : 288 : tree lhs_stat, lhs_team, lhs_team_no, opt_lhs_charlen, opt_rhs_charlen;
1414 : 288 : tree opt_lhs_desc = NULL_TREE, opt_rhs_desc = NULL_TREE;
1415 : 288 : tree receiver_fn_index_tree, add_data_tree, add_data_size;
1416 : :
1417 : 288 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1418 : 288 : gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SEND);
1419 : :
1420 : 288 : lhs_expr = code->ext.actual->expr;
1421 : 288 : rhs_expr = code->ext.actual->next->expr;
1422 : 288 : lhs_hash = code->ext.actual->next->next->expr;
1423 : 288 : receiver_fn_expr = code->ext.actual->next->next->next->expr;
1424 : 288 : add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1425 : :
1426 : 576 : ns = lhs_expr->expr_type == EXPR_VARIABLE
1427 : 288 : && !lhs_expr->symtree->n.sym->attr.associate_var
1428 : 576 : ? lhs_expr->symtree->n.sym->ns
1429 : : : gfc_current_ns;
1430 : :
1431 : 288 : gfc_init_block (&block);
1432 : :
1433 : : /* LHS. */
1434 : 288 : gfc_init_se (&lhs_se, NULL);
1435 : 288 : caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1436 : 288 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1437 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1438 : 288 : if (lhs_expr->rank == 0)
1439 : : {
1440 : 236 : if (lhs_expr->ts.type == BT_CHARACTER)
1441 : : {
1442 : 12 : gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1443 : 12 : lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1444 : 12 : opt_lhs_charlen = gfc_build_addr_expr (
1445 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1446 : : }
1447 : : else
1448 : 224 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1449 : 236 : opt_lhs_desc = null_pointer_node;
1450 : : }
1451 : : else
1452 : : {
1453 : 52 : gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1454 : 52 : gfc_add_block_to_block (&block, &lhs_se.pre);
1455 : 52 : opt_lhs_desc = lhs_se.expr;
1456 : 52 : if (lhs_expr->ts.type == BT_CHARACTER)
1457 : 22 : opt_lhs_charlen = gfc_build_addr_expr (
1458 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1459 : : else
1460 : 30 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1461 : : /* Get the third formal argument of the receiver function. (This is the
1462 : : location where to put the data on the remote image.) Need to look at
1463 : : the argument in the function decl, because in the gfc_symbol's formal
1464 : : argument an array may have no descriptor while in the generated
1465 : : function decl it has. */
1466 : 52 : tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1467 : : TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1468 : 52 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1469 : 28 : opt_lhs_desc = null_pointer_node;
1470 : : else
1471 : 24 : opt_lhs_desc
1472 : 24 : = gfc_build_addr_expr (NULL_TREE,
1473 : : gfc_trans_force_lval (&block, opt_lhs_desc));
1474 : : }
1475 : :
1476 : : /* Obtain token, offset and image index for the LHS. */
1477 : 288 : image_index = gfc_caf_get_image_index (&block, lhs_expr, caf_decl);
1478 : 288 : gfc_get_caf_token_offset (&lhs_se, &token, NULL, caf_decl, NULL, lhs_expr);
1479 : :
1480 : : /* RHS. */
1481 : 288 : gfc_init_se (&rhs_se, NULL);
1482 : 288 : if (rhs_expr->rank == 0)
1483 : : {
1484 : 166 : rhs_se.want_pointer = rhs_expr->ts.type == BT_CHARACTER;
1485 : 166 : gfc_conv_expr (&rhs_se, rhs_expr);
1486 : 166 : gfc_add_block_to_block (&block, &rhs_se.pre);
1487 : 166 : opt_rhs_desc = null_pointer_node;
1488 : 166 : if (rhs_expr->ts.type == BT_CHARACTER)
1489 : : {
1490 : 20 : rhs_data
1491 : 40 : = rhs_expr->expr_type == EXPR_CONSTANT
1492 : 20 : ? gfc_build_addr_expr (NULL_TREE,
1493 : : gfc_trans_force_lval (&block,
1494 : : rhs_se.expr))
1495 : : : rhs_se.expr;
1496 : 20 : opt_rhs_charlen = gfc_build_addr_expr (
1497 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1498 : 20 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1499 : : }
1500 : : else
1501 : : {
1502 : 146 : rhs_data
1503 : 146 : = gfc_build_addr_expr (NULL_TREE,
1504 : : gfc_trans_force_lval (&block, rhs_se.expr));
1505 : 146 : opt_rhs_charlen
1506 : 146 : = build_zero_cst (build_pointer_type (size_type_node));
1507 : 146 : rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1508 : : }
1509 : : }
1510 : : else
1511 : : {
1512 : 244 : rhs_se.force_tmp = rhs_expr->shape == NULL
1513 : 122 : || !gfc_is_simply_contiguous (rhs_expr, false, false);
1514 : 122 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1515 : 122 : gfc_add_block_to_block (&block, &rhs_se.pre);
1516 : 122 : opt_rhs_desc = rhs_se.expr;
1517 : 122 : if (rhs_expr->ts.type == BT_CHARACTER)
1518 : : {
1519 : 14 : opt_rhs_charlen = gfc_build_addr_expr (
1520 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1521 : 14 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1522 : : }
1523 : : else
1524 : : {
1525 : 108 : opt_rhs_charlen
1526 : 108 : = build_zero_cst (build_pointer_type (size_type_node));
1527 : 108 : rhs_size = fold_build2 (
1528 : : MULT_EXPR, size_type_node,
1529 : : fold_convert (size_type_node,
1530 : : rhs_expr->shape
1531 : : ? conv_shape_to_cst (rhs_expr)
1532 : : : gfc_conv_descriptor_size (rhs_se.expr,
1533 : : rhs_expr->rank)),
1534 : : fold_convert (size_type_node,
1535 : : gfc_conv_descriptor_span_get (rhs_se.expr)));
1536 : : }
1537 : :
1538 : 122 : rhs_data = gfc_build_addr_expr (
1539 : : NULL_TREE, gfc_trans_force_lval (&block, gfc_conv_descriptor_data_get (
1540 : : opt_rhs_desc)));
1541 : 122 : opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1542 : : }
1543 : 288 : gfc_add_block_to_block (&block, &rhs_se.pre);
1544 : :
1545 : 288 : conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1546 : :
1547 : 288 : receiver_fn_index_tree
1548 : 288 : = conv_caf_func_index (&block, ns, "__caf_send_to_remote_fn_index_%d",
1549 : : lhs_hash);
1550 : 288 : add_data_tree
1551 : 288 : = conv_caf_add_call_data (&block, ns, "__caf_send_to_remote_add_data_%d",
1552 : : add_data_sym, &add_data_size);
1553 : 288 : ++caf_call_cnt;
1554 : :
1555 : 288 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_send_to_remote, 14,
1556 : : token, opt_lhs_desc, opt_lhs_charlen, image_index,
1557 : : rhs_size, rhs_data, opt_rhs_charlen, opt_rhs_desc,
1558 : : receiver_fn_index_tree, add_data_tree,
1559 : : add_data_size, lhs_stat, lhs_team, lhs_team_no);
1560 : :
1561 : 288 : gfc_add_expr_to_block (&block, tmp);
1562 : 288 : gfc_add_block_to_block (&block, &lhs_se.post);
1563 : 288 : gfc_add_block_to_block (&block, &rhs_se.post);
1564 : :
1565 : : /* It guarantees memory consistency within the same segment. */
1566 : 288 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1567 : 288 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1568 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1569 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1570 : 288 : ASM_VOLATILE_P (tmp) = 1;
1571 : 288 : gfc_add_expr_to_block (&block, tmp);
1572 : :
1573 : 288 : return gfc_finish_block (&block);
1574 : : }
1575 : :
1576 : : /* Send-get data to a remote coarray. */
1577 : :
1578 : : static tree
1579 : 90 : conv_caf_sendget (gfc_code *code)
1580 : : {
1581 : : /* lhs stuff */
1582 : 90 : gfc_expr *lhs_expr, *lhs_hash, *receiver_fn_expr;
1583 : 90 : gfc_symbol *lhs_add_data_sym;
1584 : 90 : gfc_se lhs_se;
1585 : 90 : tree lhs_caf_decl, lhs_token, opt_lhs_charlen,
1586 : 90 : opt_lhs_desc = NULL_TREE, receiver_fn_index_tree, lhs_image_index,
1587 : : lhs_add_data_tree, lhs_add_data_size, lhs_stat, lhs_team, lhs_team_no;
1588 : 90 : int transfer_rank;
1589 : :
1590 : : /* rhs stuff */
1591 : 90 : gfc_expr *rhs_expr, *rhs_hash, *sender_fn_expr;
1592 : 90 : gfc_symbol *rhs_add_data_sym;
1593 : 90 : gfc_se rhs_se;
1594 : 90 : tree rhs_caf_decl, rhs_token, opt_rhs_charlen,
1595 : 90 : opt_rhs_desc = NULL_TREE, sender_fn_index_tree, rhs_image_index,
1596 : : rhs_add_data_tree, rhs_add_data_size, rhs_stat, rhs_team, rhs_team_no;
1597 : :
1598 : : /* shared */
1599 : 90 : stmtblock_t block;
1600 : 90 : gfc_namespace *ns;
1601 : 90 : tree tmp, rhs_size;
1602 : :
1603 : 90 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1604 : 90 : gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SENDGET);
1605 : :
1606 : 90 : lhs_expr = code->ext.actual->expr;
1607 : 90 : rhs_expr = code->ext.actual->next->expr;
1608 : 90 : lhs_hash = code->ext.actual->next->next->expr;
1609 : 90 : receiver_fn_expr = code->ext.actual->next->next->next->expr;
1610 : 90 : rhs_hash = code->ext.actual->next->next->next->next->expr;
1611 : 90 : sender_fn_expr = code->ext.actual->next->next->next->next->next->expr;
1612 : :
1613 : 90 : lhs_add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1614 : 90 : rhs_add_data_sym = sender_fn_expr->symtree->n.sym->formal->sym;
1615 : :
1616 : 180 : ns = lhs_expr->expr_type == EXPR_VARIABLE
1617 : 90 : && !lhs_expr->symtree->n.sym->attr.associate_var
1618 : 180 : ? lhs_expr->symtree->n.sym->ns
1619 : : : gfc_current_ns;
1620 : :
1621 : 90 : gfc_init_block (&block);
1622 : :
1623 : 90 : lhs_stat = null_pointer_node;
1624 : 90 : lhs_team = null_pointer_node;
1625 : 90 : rhs_stat = null_pointer_node;
1626 : 90 : rhs_team = null_pointer_node;
1627 : :
1628 : : /* LHS. */
1629 : 90 : gfc_init_se (&lhs_se, NULL);
1630 : 90 : lhs_caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1631 : 90 : if (TREE_CODE (TREE_TYPE (lhs_caf_decl)) == REFERENCE_TYPE)
1632 : 0 : lhs_caf_decl = build_fold_indirect_ref_loc (input_location, lhs_caf_decl);
1633 : 90 : if (lhs_expr->rank == 0)
1634 : : {
1635 : 56 : if (lhs_expr->ts.type == BT_CHARACTER)
1636 : : {
1637 : 8 : gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1638 : 8 : lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1639 : 8 : opt_lhs_charlen = gfc_build_addr_expr (
1640 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1641 : : }
1642 : : else
1643 : 48 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1644 : 56 : opt_lhs_desc = null_pointer_node;
1645 : : }
1646 : : else
1647 : : {
1648 : 34 : gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1649 : 34 : gfc_add_block_to_block (&block, &lhs_se.pre);
1650 : 34 : opt_lhs_desc = lhs_se.expr;
1651 : 34 : if (lhs_expr->ts.type == BT_CHARACTER)
1652 : 16 : opt_lhs_charlen = gfc_build_addr_expr (
1653 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1654 : : else
1655 : 18 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1656 : : /* Get the third formal argument of the receiver function. (This is the
1657 : : location where to put the data on the remote image.) Need to look at
1658 : : the argument in the function decl, because in the gfc_symbol's formal
1659 : : argument an array may have no descriptor while in the generated
1660 : : function decl it has. */
1661 : 34 : tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1662 : : TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1663 : 34 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1664 : 30 : opt_lhs_desc = null_pointer_node;
1665 : : else
1666 : 4 : opt_lhs_desc
1667 : 4 : = gfc_build_addr_expr (NULL_TREE,
1668 : : gfc_trans_force_lval (&block, opt_lhs_desc));
1669 : : }
1670 : :
1671 : : /* Obtain token, offset and image index for the LHS. */
1672 : 90 : lhs_image_index = gfc_caf_get_image_index (&block, lhs_expr, lhs_caf_decl);
1673 : 90 : gfc_get_caf_token_offset (&lhs_se, &lhs_token, NULL, lhs_caf_decl, NULL,
1674 : : lhs_expr);
1675 : :
1676 : : /* RHS. */
1677 : 90 : rhs_caf_decl = gfc_get_tree_for_caf_expr (rhs_expr);
1678 : 90 : if (TREE_CODE (TREE_TYPE (rhs_caf_decl)) == REFERENCE_TYPE)
1679 : 0 : rhs_caf_decl = build_fold_indirect_ref_loc (input_location, rhs_caf_decl);
1680 : 90 : transfer_rank = rhs_expr->rank;
1681 : 90 : gfc_expression_rank (rhs_expr);
1682 : 90 : gfc_init_se (&rhs_se, NULL);
1683 : 90 : if (rhs_expr->rank == 0)
1684 : : {
1685 : 64 : opt_rhs_desc = null_pointer_node;
1686 : 64 : if (rhs_expr->ts.type == BT_CHARACTER)
1687 : : {
1688 : 16 : gfc_conv_expr (&rhs_se, rhs_expr);
1689 : 16 : gfc_add_block_to_block (&block, &rhs_se.pre);
1690 : 16 : opt_rhs_charlen = gfc_build_addr_expr (
1691 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1692 : 16 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1693 : : }
1694 : : else
1695 : : {
1696 : 48 : gfc_typespec *ts
1697 : 48 : = &sender_fn_expr->symtree->n.sym->formal->next->next->sym->ts;
1698 : :
1699 : 48 : opt_rhs_charlen
1700 : 48 : = build_zero_cst (build_pointer_type (size_type_node));
1701 : 48 : rhs_size = gfc_typenode_for_spec (ts)->type_common.size_unit;
1702 : : }
1703 : : }
1704 : : /* Get the fifth formal argument of the getter function. This is the argument
1705 : : pointing to the data to get on the remote image. Need to look at the
1706 : : argument in the function decl, because in the gfc_symbol's formal argument
1707 : : an array may have no descriptor while in the generated function decl it
1708 : : has. */
1709 : 26 : else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_VALUE (
1710 : : TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1711 : : TREE_TYPE (sender_fn_expr->symtree->n.sym->backend_decl))))))))))
1712 : : {
1713 : 22 : rhs_se.data_not_needed = 1;
1714 : 22 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1715 : 22 : gfc_add_block_to_block (&block, &rhs_se.pre);
1716 : 22 : if (rhs_expr->ts.type == BT_CHARACTER)
1717 : : {
1718 : 8 : opt_rhs_charlen = gfc_build_addr_expr (
1719 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1720 : 8 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1721 : : }
1722 : : else
1723 : : {
1724 : 14 : opt_rhs_charlen
1725 : 14 : = build_zero_cst (build_pointer_type (size_type_node));
1726 : 14 : rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1727 : : }
1728 : 22 : opt_rhs_desc = null_pointer_node;
1729 : : }
1730 : : else
1731 : : {
1732 : 4 : gfc_ref *arr_ref = rhs_expr->ref;
1733 : 4 : while (arr_ref && arr_ref->type != REF_ARRAY)
1734 : 0 : arr_ref = arr_ref->next;
1735 : 4 : rhs_se.force_tmp
1736 : 8 : = (rhs_expr->shape == NULL
1737 : 4 : && (!arr_ref || !gfc_full_array_ref_p (arr_ref, nullptr)))
1738 : 8 : || !gfc_is_simply_contiguous (rhs_expr, false, false);
1739 : 4 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1740 : 4 : gfc_add_block_to_block (&block, &rhs_se.pre);
1741 : 4 : opt_rhs_desc = rhs_se.expr;
1742 : 4 : if (rhs_expr->ts.type == BT_CHARACTER)
1743 : : {
1744 : 0 : opt_rhs_charlen = gfc_build_addr_expr (
1745 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1746 : 0 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1747 : : }
1748 : : else
1749 : : {
1750 : 4 : opt_rhs_charlen
1751 : 4 : = build_zero_cst (build_pointer_type (size_type_node));
1752 : 4 : rhs_size = fold_build2 (
1753 : : MULT_EXPR, size_type_node,
1754 : : fold_convert (size_type_node,
1755 : : rhs_expr->shape
1756 : : ? conv_shape_to_cst (rhs_expr)
1757 : : : gfc_conv_descriptor_size (rhs_se.expr,
1758 : : rhs_expr->rank)),
1759 : : fold_convert (size_type_node,
1760 : : gfc_conv_descriptor_span_get (rhs_se.expr)));
1761 : : }
1762 : :
1763 : 4 : opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1764 : : }
1765 : 90 : gfc_add_block_to_block (&block, &rhs_se.pre);
1766 : :
1767 : : /* Obtain token, offset and image index for the RHS. */
1768 : 90 : rhs_image_index = gfc_caf_get_image_index (&block, rhs_expr, rhs_caf_decl);
1769 : 90 : gfc_get_caf_token_offset (&rhs_se, &rhs_token, NULL, rhs_caf_decl, NULL,
1770 : : rhs_expr);
1771 : :
1772 : : /* stat and team. */
1773 : 90 : conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1774 : 90 : conv_stat_and_team (&block, rhs_expr, &rhs_stat, &rhs_team, &rhs_team_no);
1775 : :
1776 : 90 : sender_fn_index_tree
1777 : 90 : = conv_caf_func_index (&block, ns, "__caf_transfer_from_fn_index_%d",
1778 : : rhs_hash);
1779 : 90 : rhs_add_data_tree
1780 : 90 : = conv_caf_add_call_data (&block, ns,
1781 : : "__caf_transfer_from_remote_add_data_%d",
1782 : : rhs_add_data_sym, &rhs_add_data_size);
1783 : 90 : receiver_fn_index_tree
1784 : 90 : = conv_caf_func_index (&block, ns, "__caf_transfer_to_remote_fn_index_%d",
1785 : : lhs_hash);
1786 : 90 : lhs_add_data_tree
1787 : 90 : = conv_caf_add_call_data (&block, ns,
1788 : : "__caf_transfer_to_remote_add_data_%d",
1789 : : lhs_add_data_sym, &lhs_add_data_size);
1790 : 90 : ++caf_call_cnt;
1791 : :
1792 : 90 : tmp = build_call_expr_loc (
1793 : : input_location, gfor_fndecl_caf_transfer_between_remotes, 20, lhs_token,
1794 : : opt_lhs_desc, opt_lhs_charlen, lhs_image_index, receiver_fn_index_tree,
1795 : : lhs_add_data_tree, lhs_add_data_size, rhs_token, opt_rhs_desc,
1796 : : opt_rhs_charlen, rhs_image_index, sender_fn_index_tree, rhs_add_data_tree,
1797 : : rhs_add_data_size, rhs_size,
1798 : : transfer_rank == 0 ? boolean_true_node : boolean_false_node, lhs_stat,
1799 : : lhs_team, lhs_team_no, rhs_stat, rhs_team, rhs_team_no);
1800 : :
1801 : 90 : gfc_add_expr_to_block (&block, tmp);
1802 : 90 : gfc_add_block_to_block (&block, &lhs_se.post);
1803 : 90 : gfc_add_block_to_block (&block, &rhs_se.post);
1804 : :
1805 : : /* It guarantees memory consistency within the same segment. */
1806 : 90 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1807 : 90 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1808 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1809 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1810 : 90 : ASM_VOLATILE_P (tmp) = 1;
1811 : 90 : gfc_add_expr_to_block (&block, tmp);
1812 : :
1813 : 90 : return gfc_finish_block (&block);
1814 : : }
1815 : :
1816 : :
1817 : : static void
1818 : 650 : trans_this_image (gfc_se * se, gfc_expr *expr)
1819 : : {
1820 : 650 : stmtblock_t loop;
1821 : 650 : tree type, desc, dim_arg, cond, tmp, m, loop_var, exit_label, min_var,
1822 : : lbound, ubound, extent, ml;
1823 : 650 : gfc_se argse;
1824 : 650 : int rank, corank;
1825 : 650 : gfc_expr *distance = expr->value.function.actual->next->next->expr;
1826 : :
1827 : 650 : if (expr->value.function.actual->expr
1828 : 650 : && !gfc_is_coarray (expr->value.function.actual->expr))
1829 : 1 : distance = expr->value.function.actual->expr;
1830 : :
1831 : : /* The case -fcoarray=single is handled elsewhere. */
1832 : 650 : gcc_assert (flag_coarray != GFC_FCOARRAY_SINGLE);
1833 : :
1834 : : /* Argument-free version: THIS_IMAGE(). */
1835 : 650 : if (distance || expr->value.function.actual->expr == NULL)
1836 : : {
1837 : 501 : if (distance)
1838 : : {
1839 : 2 : gfc_init_se (&argse, NULL);
1840 : 2 : gfc_conv_expr_val (&argse, distance);
1841 : 2 : gfc_add_block_to_block (&se->pre, &argse.pre);
1842 : 2 : gfc_add_block_to_block (&se->post, &argse.post);
1843 : 2 : tmp = fold_convert (integer_type_node, argse.expr);
1844 : : }
1845 : : else
1846 : 501 : tmp = integer_zero_node;
1847 : 503 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
1848 : : tmp);
1849 : 503 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
1850 : : tmp);
1851 : 507 : return;
1852 : : }
1853 : :
1854 : : /* Coarray-argument version: THIS_IMAGE(coarray [, dim]). */
1855 : :
1856 : 147 : type = gfc_get_int_type (gfc_default_integer_kind);
1857 : 147 : corank = expr->value.function.actual->expr->corank;
1858 : 147 : rank = expr->value.function.actual->expr->rank;
1859 : :
1860 : : /* Obtain the descriptor of the COARRAY. */
1861 : 147 : gfc_init_se (&argse, NULL);
1862 : 147 : argse.want_coarray = 1;
1863 : 147 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
1864 : 147 : gfc_add_block_to_block (&se->pre, &argse.pre);
1865 : 147 : gfc_add_block_to_block (&se->post, &argse.post);
1866 : 147 : desc = argse.expr;
1867 : :
1868 : 147 : if (se->ss)
1869 : : {
1870 : : /* Create an implicit second parameter from the loop variable. */
1871 : 29 : gcc_assert (!expr->value.function.actual->next->expr);
1872 : 29 : gcc_assert (corank > 0);
1873 : 29 : gcc_assert (se->loop->dimen == 1);
1874 : 29 : gcc_assert (se->ss->info->expr == expr);
1875 : :
1876 : 29 : dim_arg = se->loop->loopvar[0];
1877 : 29 : dim_arg = fold_build2_loc (input_location, PLUS_EXPR,
1878 : : gfc_array_index_type, dim_arg,
1879 : 29 : build_int_cst (TREE_TYPE (dim_arg), 1));
1880 : 29 : gfc_advance_se_ss_chain (se);
1881 : : }
1882 : : else
1883 : : {
1884 : : /* Use the passed DIM= argument. */
1885 : 118 : gcc_assert (expr->value.function.actual->next->expr);
1886 : 118 : gfc_init_se (&argse, NULL);
1887 : 118 : gfc_conv_expr_type (&argse, expr->value.function.actual->next->expr,
1888 : : gfc_array_index_type);
1889 : 118 : gfc_add_block_to_block (&se->pre, &argse.pre);
1890 : 118 : dim_arg = argse.expr;
1891 : :
1892 : 118 : if (INTEGER_CST_P (dim_arg))
1893 : : {
1894 : 60 : if (wi::ltu_p (wi::to_wide (dim_arg), 1)
1895 : 120 : || wi::gtu_p (wi::to_wide (dim_arg),
1896 : 60 : GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
1897 : 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
1898 : 0 : "dimension index", expr->value.function.isym->name,
1899 : : &expr->where);
1900 : : }
1901 : 58 : else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1902 : : {
1903 : 0 : dim_arg = gfc_evaluate_now (dim_arg, &se->pre);
1904 : 0 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1905 : : dim_arg,
1906 : 0 : build_int_cst (TREE_TYPE (dim_arg), 1));
1907 : 0 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
1908 : 0 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
1909 : : dim_arg, tmp);
1910 : 0 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
1911 : : logical_type_node, cond, tmp);
1912 : 0 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
1913 : : gfc_msg_fault);
1914 : : }
1915 : : }
1916 : :
1917 : : /* Used algorithm; cf. Fortran 2008, C.10. Note, due to the scalarizer,
1918 : : one always has a dim_arg argument.
1919 : :
1920 : : m = this_image() - 1
1921 : : if (corank == 1)
1922 : : {
1923 : : sub(1) = m + lcobound(corank)
1924 : : return;
1925 : : }
1926 : : i = rank
1927 : : min_var = min (rank + corank - 2, rank + dim_arg - 1)
1928 : : for (;;)
1929 : : {
1930 : : extent = gfc_extent(i)
1931 : : ml = m
1932 : : m = m/extent
1933 : : if (i >= min_var)
1934 : : goto exit_label
1935 : : i++
1936 : : }
1937 : : exit_label:
1938 : : sub(dim_arg) = (dim_arg < corank) ? ml - m*extent + lcobound(dim_arg)
1939 : : : m + lcobound(corank)
1940 : : */
1941 : :
1942 : : /* this_image () - 1. */
1943 : 147 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
1944 : : integer_zero_node);
1945 : 147 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
1946 : 147 : fold_convert (type, tmp), build_int_cst (type, 1));
1947 : 147 : if (corank == 1)
1948 : : {
1949 : : /* sub(1) = m + lcobound(corank). */
1950 : 4 : lbound = gfc_conv_descriptor_lbound_get (desc,
1951 : 4 : build_int_cst (TREE_TYPE (gfc_array_index_type),
1952 : 4 : corank+rank-1));
1953 : 4 : lbound = fold_convert (type, lbound);
1954 : 4 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
1955 : :
1956 : 4 : se->expr = tmp;
1957 : 4 : return;
1958 : : }
1959 : :
1960 : 143 : m = gfc_create_var (type, NULL);
1961 : 143 : ml = gfc_create_var (type, NULL);
1962 : 143 : loop_var = gfc_create_var (integer_type_node, NULL);
1963 : 143 : min_var = gfc_create_var (integer_type_node, NULL);
1964 : :
1965 : : /* m = this_image () - 1. */
1966 : 143 : gfc_add_modify (&se->pre, m, tmp);
1967 : :
1968 : : /* min_var = min (rank + corank-2, rank + dim_arg - 1). */
1969 : 143 : tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1970 : : fold_convert (integer_type_node, dim_arg),
1971 : 143 : build_int_cst (integer_type_node, rank - 1));
1972 : 143 : tmp = fold_build2_loc (input_location, MIN_EXPR, integer_type_node,
1973 : 143 : build_int_cst (integer_type_node, rank + corank - 2),
1974 : : tmp);
1975 : 143 : gfc_add_modify (&se->pre, min_var, tmp);
1976 : :
1977 : : /* i = rank. */
1978 : 143 : tmp = build_int_cst (integer_type_node, rank);
1979 : 143 : gfc_add_modify (&se->pre, loop_var, tmp);
1980 : :
1981 : 143 : exit_label = gfc_build_label_decl (NULL_TREE);
1982 : 143 : TREE_USED (exit_label) = 1;
1983 : :
1984 : : /* Loop body. */
1985 : 143 : gfc_init_block (&loop);
1986 : :
1987 : : /* ml = m. */
1988 : 143 : gfc_add_modify (&loop, ml, m);
1989 : :
1990 : : /* extent = ... */
1991 : 143 : lbound = gfc_conv_descriptor_lbound_get (desc, loop_var);
1992 : 143 : ubound = gfc_conv_descriptor_ubound_get (desc, loop_var);
1993 : 143 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1994 : 143 : extent = fold_convert (type, extent);
1995 : :
1996 : : /* m = m/extent. */
1997 : 143 : gfc_add_modify (&loop, m,
1998 : : fold_build2_loc (input_location, TRUNC_DIV_EXPR, type,
1999 : : m, extent));
2000 : :
2001 : : /* Exit condition: if (i >= min_var) goto exit_label. */
2002 : 143 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, loop_var,
2003 : : min_var);
2004 : 143 : tmp = build1_v (GOTO_EXPR, exit_label);
2005 : 143 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
2006 : : build_empty_stmt (input_location));
2007 : 143 : gfc_add_expr_to_block (&loop, tmp);
2008 : :
2009 : : /* Increment loop variable: i++. */
2010 : 143 : gfc_add_modify (&loop, loop_var,
2011 : : fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
2012 : : loop_var,
2013 : : integer_one_node));
2014 : :
2015 : : /* Making the loop... actually loop! */
2016 : 143 : tmp = gfc_finish_block (&loop);
2017 : 143 : tmp = build1_v (LOOP_EXPR, tmp);
2018 : 143 : gfc_add_expr_to_block (&se->pre, tmp);
2019 : :
2020 : : /* The exit label. */
2021 : 143 : tmp = build1_v (LABEL_EXPR, exit_label);
2022 : 143 : gfc_add_expr_to_block (&se->pre, tmp);
2023 : :
2024 : : /* sub(co_dim) = (co_dim < corank) ? ml - m*extent + lcobound(dim_arg)
2025 : : : m + lcobound(corank) */
2026 : :
2027 : 143 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, dim_arg,
2028 : 143 : build_int_cst (TREE_TYPE (dim_arg), corank));
2029 : :
2030 : 143 : lbound = gfc_conv_descriptor_lbound_get (desc,
2031 : : fold_build2_loc (input_location, PLUS_EXPR,
2032 : : gfc_array_index_type, dim_arg,
2033 : 143 : build_int_cst (TREE_TYPE (dim_arg), rank-1)));
2034 : 143 : lbound = fold_convert (type, lbound);
2035 : :
2036 : 143 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type, ml,
2037 : : fold_build2_loc (input_location, MULT_EXPR, type,
2038 : : m, extent));
2039 : 143 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
2040 : :
2041 : 143 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
2042 : : fold_build2_loc (input_location, PLUS_EXPR, type,
2043 : : m, lbound));
2044 : : }
2045 : :
2046 : :
2047 : : /* Convert a call to image_status. */
2048 : :
2049 : : static void
2050 : 16 : conv_intrinsic_image_status (gfc_se *se, gfc_expr *expr)
2051 : : {
2052 : 16 : unsigned int num_args;
2053 : 16 : tree *args, tmp;
2054 : :
2055 : 16 : num_args = gfc_intrinsic_argument_list_length (expr);
2056 : 16 : args = XALLOCAVEC (tree, num_args);
2057 : 16 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2058 : : /* In args[0] the number of the image the status is desired for has to be
2059 : : given. */
2060 : :
2061 : 16 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
2062 : : {
2063 : 0 : tree arg;
2064 : 0 : arg = gfc_evaluate_now (args[0], &se->pre);
2065 : 0 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2066 : : fold_convert (integer_type_node, arg),
2067 : : integer_one_node);
2068 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
2069 : : tmp, integer_zero_node,
2070 : 0 : build_int_cst (integer_type_node,
2071 : 0 : GFC_STAT_STOPPED_IMAGE));
2072 : : }
2073 : 16 : else if (flag_coarray == GFC_FCOARRAY_LIB)
2074 : 16 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_image_status, 2,
2075 : 16 : args[0], build_int_cst (integer_type_node, -1));
2076 : : else
2077 : 0 : gcc_unreachable ();
2078 : :
2079 : 16 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2080 : 16 : }
2081 : :
2082 : : static void
2083 : 31 : conv_intrinsic_team_number (gfc_se *se, gfc_expr *expr)
2084 : : {
2085 : 31 : unsigned int num_args;
2086 : :
2087 : 31 : tree *args, tmp;
2088 : :
2089 : 31 : num_args = gfc_intrinsic_argument_list_length (expr);
2090 : 31 : args = XALLOCAVEC (tree, num_args);
2091 : 31 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2092 : :
2093 : 31 : if (flag_coarray ==
2094 : 30 : GFC_FCOARRAY_SINGLE && expr->value.function.actual->expr)
2095 : : {
2096 : 0 : tree arg;
2097 : :
2098 : 0 : arg = gfc_evaluate_now (args[0], &se->pre);
2099 : 0 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2100 : : fold_convert (integer_type_node, arg),
2101 : : integer_one_node);
2102 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
2103 : : tmp, integer_zero_node,
2104 : 0 : build_int_cst (integer_type_node,
2105 : 0 : GFC_STAT_STOPPED_IMAGE));
2106 : 0 : }
2107 : 31 : else if (flag_coarray == GFC_FCOARRAY_SINGLE)
2108 : : {
2109 : : // the value -1 represents that no team has been created yet
2110 : 30 : tmp = build_int_cst (integer_type_node, -1);
2111 : : }
2112 : 1 : else if (flag_coarray == GFC_FCOARRAY_LIB && expr->value.function.actual->expr)
2113 : 0 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
2114 : 0 : args[0], build_int_cst (integer_type_node, -1));
2115 : 1 : else if (flag_coarray == GFC_FCOARRAY_LIB)
2116 : 1 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
2117 : 1 : integer_zero_node, build_int_cst (integer_type_node, -1));
2118 : : else
2119 : 0 : gcc_unreachable ();
2120 : :
2121 : 31 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2122 : 31 : }
2123 : :
2124 : :
2125 : : static void
2126 : 152 : trans_image_index (gfc_se * se, gfc_expr *expr)
2127 : : {
2128 : 152 : tree num_images, cond, coindex, type, lbound, ubound, desc, subdesc,
2129 : : tmp, invalid_bound;
2130 : 152 : gfc_se argse, subse;
2131 : 152 : int rank, corank, codim;
2132 : :
2133 : 152 : type = gfc_get_int_type (gfc_default_integer_kind);
2134 : 152 : corank = expr->value.function.actual->expr->corank;
2135 : 152 : rank = expr->value.function.actual->expr->rank;
2136 : :
2137 : : /* Obtain the descriptor of the COARRAY. */
2138 : 152 : gfc_init_se (&argse, NULL);
2139 : 152 : argse.want_coarray = 1;
2140 : 152 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2141 : 152 : gfc_add_block_to_block (&se->pre, &argse.pre);
2142 : 152 : gfc_add_block_to_block (&se->post, &argse.post);
2143 : 152 : desc = argse.expr;
2144 : :
2145 : : /* Obtain a handle to the SUB argument. */
2146 : 152 : gfc_init_se (&subse, NULL);
2147 : 152 : gfc_conv_expr_descriptor (&subse, expr->value.function.actual->next->expr);
2148 : 152 : gfc_add_block_to_block (&se->pre, &subse.pre);
2149 : 152 : gfc_add_block_to_block (&se->post, &subse.post);
2150 : 152 : subdesc = build_fold_indirect_ref_loc (input_location,
2151 : : gfc_conv_descriptor_data_get (subse.expr));
2152 : :
2153 : : /* Fortran 2008 does not require that the values remain in the cobounds,
2154 : : thus we need explicitly check this - and return 0 if they are exceeded. */
2155 : :
2156 : 152 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2157 : 152 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1], NULL);
2158 : 152 : invalid_bound = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2159 : : fold_convert (gfc_array_index_type, tmp),
2160 : : lbound);
2161 : :
2162 : 352 : for (codim = corank + rank - 2; codim >= rank; codim--)
2163 : : {
2164 : 200 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2165 : 200 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2166 : 200 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2167 : 200 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2168 : : fold_convert (gfc_array_index_type, tmp),
2169 : : lbound);
2170 : 200 : invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2171 : : logical_type_node, invalid_bound, cond);
2172 : 200 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2173 : : fold_convert (gfc_array_index_type, tmp),
2174 : : ubound);
2175 : 200 : invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2176 : : logical_type_node, invalid_bound, cond);
2177 : : }
2178 : :
2179 : 152 : invalid_bound = gfc_unlikely (invalid_bound, PRED_FORTRAN_INVALID_BOUND);
2180 : :
2181 : : /* See Fortran 2008, C.10 for the following algorithm. */
2182 : :
2183 : : /* coindex = sub(corank) - lcobound(n). */
2184 : 152 : coindex = fold_convert (gfc_array_index_type,
2185 : : gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1],
2186 : : NULL));
2187 : 152 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2188 : 152 : coindex = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2189 : : fold_convert (gfc_array_index_type, coindex),
2190 : : lbound);
2191 : :
2192 : 352 : for (codim = corank + rank - 2; codim >= rank; codim--)
2193 : : {
2194 : 200 : tree extent, ubound;
2195 : :
2196 : : /* coindex = coindex*extent(codim) + sub(codim) - lcobound(codim). */
2197 : 200 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2198 : 200 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2199 : 200 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2200 : :
2201 : : /* coindex *= extent. */
2202 : 200 : coindex = fold_build2_loc (input_location, MULT_EXPR,
2203 : : gfc_array_index_type, coindex, extent);
2204 : :
2205 : : /* coindex += sub(codim). */
2206 : 200 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2207 : 200 : coindex = fold_build2_loc (input_location, PLUS_EXPR,
2208 : : gfc_array_index_type, coindex,
2209 : : fold_convert (gfc_array_index_type, tmp));
2210 : :
2211 : : /* coindex -= lbound(codim). */
2212 : 200 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2213 : 200 : coindex = fold_build2_loc (input_location, MINUS_EXPR,
2214 : : gfc_array_index_type, coindex, lbound);
2215 : : }
2216 : :
2217 : 152 : coindex = fold_build2_loc (input_location, PLUS_EXPR, type,
2218 : : fold_convert(type, coindex),
2219 : 152 : build_int_cst (type, 1));
2220 : :
2221 : : /* Return 0 if "coindex" exceeds num_images(). */
2222 : :
2223 : 152 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
2224 : 108 : num_images = build_int_cst (type, 1);
2225 : : else
2226 : : {
2227 : 44 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2228 : : integer_zero_node,
2229 : 44 : build_int_cst (integer_type_node, -1));
2230 : 44 : num_images = fold_convert (type, tmp);
2231 : : }
2232 : :
2233 : 152 : tmp = gfc_create_var (type, NULL);
2234 : 152 : gfc_add_modify (&se->pre, tmp, coindex);
2235 : :
2236 : 152 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, tmp,
2237 : : num_images);
2238 : 152 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
2239 : : cond,
2240 : : fold_convert (logical_type_node, invalid_bound));
2241 : 152 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
2242 : 152 : build_int_cst (type, 0), tmp);
2243 : 152 : }
2244 : :
2245 : : static void
2246 : 398 : trans_num_images (gfc_se * se, gfc_expr *expr)
2247 : : {
2248 : 398 : tree tmp, distance, failed;
2249 : 398 : gfc_se argse;
2250 : :
2251 : 398 : if (expr->value.function.actual->expr)
2252 : : {
2253 : 3 : gfc_init_se (&argse, NULL);
2254 : 3 : gfc_conv_expr_val (&argse, expr->value.function.actual->expr);
2255 : 3 : gfc_add_block_to_block (&se->pre, &argse.pre);
2256 : 3 : gfc_add_block_to_block (&se->post, &argse.post);
2257 : 3 : distance = fold_convert (integer_type_node, argse.expr);
2258 : : }
2259 : : else
2260 : 395 : distance = integer_zero_node;
2261 : :
2262 : 398 : if (expr->value.function.actual->next->expr)
2263 : : {
2264 : 2 : gfc_init_se (&argse, NULL);
2265 : 2 : gfc_conv_expr_val (&argse, expr->value.function.actual->next->expr);
2266 : 2 : gfc_add_block_to_block (&se->pre, &argse.pre);
2267 : 2 : gfc_add_block_to_block (&se->post, &argse.post);
2268 : 2 : failed = fold_convert (integer_type_node, argse.expr);
2269 : : }
2270 : : else
2271 : 396 : failed = build_int_cst (integer_type_node, -1);
2272 : 398 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2273 : : distance, failed);
2274 : 398 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2275 : 398 : }
2276 : :
2277 : :
2278 : : static void
2279 : 10467 : gfc_conv_intrinsic_rank (gfc_se *se, gfc_expr *expr)
2280 : : {
2281 : 10467 : gfc_se argse;
2282 : :
2283 : 10467 : gfc_init_se (&argse, NULL);
2284 : 10467 : argse.data_not_needed = 1;
2285 : 10467 : argse.descriptor_only = 1;
2286 : :
2287 : 10467 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2288 : 10467 : gfc_add_block_to_block (&se->pre, &argse.pre);
2289 : 10467 : gfc_add_block_to_block (&se->post, &argse.post);
2290 : :
2291 : 10467 : se->expr = gfc_conv_descriptor_rank (argse.expr);
2292 : 10467 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
2293 : : se->expr);
2294 : 10467 : }
2295 : :
2296 : :
2297 : : static void
2298 : 621 : gfc_conv_intrinsic_is_contiguous (gfc_se * se, gfc_expr * expr)
2299 : : {
2300 : 621 : gfc_expr *arg;
2301 : 621 : arg = expr->value.function.actual->expr;
2302 : 621 : gfc_conv_is_contiguous_expr (se, arg);
2303 : 621 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
2304 : 621 : }
2305 : :
2306 : : /* This function does the work for gfc_conv_intrinsic_is_contiguous,
2307 : : plus it can be called directly. */
2308 : :
2309 : : void
2310 : 1964 : gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
2311 : : {
2312 : 1964 : gfc_ss *ss;
2313 : 1964 : gfc_se argse;
2314 : 1964 : tree desc, tmp, stride, extent, cond;
2315 : 1964 : int i;
2316 : 1964 : tree fncall0;
2317 : 1964 : gfc_array_spec *as;
2318 : :
2319 : 1964 : if (arg->ts.type == BT_CLASS)
2320 : 36 : gfc_add_class_array_ref (arg);
2321 : :
2322 : 1964 : ss = gfc_walk_expr (arg);
2323 : 1964 : gcc_assert (ss != gfc_ss_terminator);
2324 : 1964 : gfc_init_se (&argse, NULL);
2325 : 1964 : argse.data_not_needed = 1;
2326 : 1964 : gfc_conv_expr_descriptor (&argse, arg);
2327 : :
2328 : 1964 : as = gfc_get_full_arrayspec_from_expr (arg);
2329 : :
2330 : : /* Create: stride[0] == 1 && stride[1] == extend[0]*stride[0] && ...
2331 : : Note in addition that zero-sized arrays don't count as contiguous. */
2332 : :
2333 : 1964 : if (as && as->type == AS_ASSUMED_RANK)
2334 : : {
2335 : : /* Build the call to is_contiguous0. */
2336 : 243 : argse.want_pointer = 1;
2337 : 243 : gfc_conv_expr_descriptor (&argse, arg);
2338 : 243 : gfc_add_block_to_block (&se->pre, &argse.pre);
2339 : 243 : gfc_add_block_to_block (&se->post, &argse.post);
2340 : 243 : desc = gfc_evaluate_now (argse.expr, &se->pre);
2341 : 243 : fncall0 = build_call_expr_loc (input_location,
2342 : : gfor_fndecl_is_contiguous0, 1, desc);
2343 : 243 : se->expr = fncall0;
2344 : 243 : se->expr = convert (logical_type_node, se->expr);
2345 : : }
2346 : : else
2347 : : {
2348 : 1721 : gfc_add_block_to_block (&se->pre, &argse.pre);
2349 : 1721 : gfc_add_block_to_block (&se->post, &argse.post);
2350 : 1721 : desc = gfc_evaluate_now (argse.expr, &se->pre);
2351 : :
2352 : 1721 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[0]);
2353 : 1721 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2354 : 1721 : stride, build_int_cst (TREE_TYPE (stride), 1));
2355 : :
2356 : 2047 : for (i = 0; i < arg->rank - 1; i++)
2357 : : {
2358 : 326 : tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2359 : 326 : extent = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2360 : 326 : extent = fold_build2_loc (input_location, MINUS_EXPR,
2361 : : gfc_array_index_type, extent, tmp);
2362 : 326 : extent = fold_build2_loc (input_location, PLUS_EXPR,
2363 : : gfc_array_index_type, extent,
2364 : : gfc_index_one_node);
2365 : 326 : tmp = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i]);
2366 : 326 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2367 : : tmp, extent);
2368 : 326 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i+1]);
2369 : 326 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2370 : : stride, tmp);
2371 : 326 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2372 : : boolean_type_node, cond, tmp);
2373 : : }
2374 : 1721 : se->expr = cond;
2375 : : }
2376 : 1964 : }
2377 : :
2378 : :
2379 : : /* Evaluate a single upper or lower bound. */
2380 : : /* TODO: bound intrinsic generates way too much unnecessary code. */
2381 : :
2382 : : static void
2383 : 15858 : gfc_conv_intrinsic_bound (gfc_se * se, gfc_expr * expr, enum gfc_isym_id op)
2384 : : {
2385 : 15858 : gfc_actual_arglist *arg;
2386 : 15858 : gfc_actual_arglist *arg2;
2387 : 15858 : tree desc;
2388 : 15858 : tree type;
2389 : 15858 : tree bound;
2390 : 15858 : tree tmp;
2391 : 15858 : tree cond, cond1;
2392 : 15858 : tree ubound;
2393 : 15858 : tree lbound;
2394 : 15858 : tree size;
2395 : 15858 : gfc_se argse;
2396 : 15858 : gfc_array_spec * as;
2397 : 15858 : bool assumed_rank_lb_one;
2398 : :
2399 : 15858 : arg = expr->value.function.actual;
2400 : 15858 : arg2 = arg->next;
2401 : :
2402 : 15858 : if (se->ss)
2403 : : {
2404 : : /* Create an implicit second parameter from the loop variable. */
2405 : 7755 : gcc_assert (!arg2->expr || op == GFC_ISYM_SHAPE);
2406 : 7755 : gcc_assert (se->loop->dimen == 1);
2407 : 7755 : gcc_assert (se->ss->info->expr == expr);
2408 : 7755 : gfc_advance_se_ss_chain (se);
2409 : 7755 : bound = se->loop->loopvar[0];
2410 : 7755 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2411 : : gfc_array_index_type, bound,
2412 : : se->loop->from[0]);
2413 : : }
2414 : : else
2415 : : {
2416 : : /* use the passed argument. */
2417 : 8103 : gcc_assert (arg2->expr);
2418 : 8103 : gfc_init_se (&argse, NULL);
2419 : 8103 : gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
2420 : 8103 : gfc_add_block_to_block (&se->pre, &argse.pre);
2421 : 8103 : bound = argse.expr;
2422 : : /* Convert from one based to zero based. */
2423 : 8103 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2424 : : gfc_array_index_type, bound,
2425 : : gfc_index_one_node);
2426 : : }
2427 : :
2428 : : /* TODO: don't re-evaluate the descriptor on each iteration. */
2429 : : /* Get a descriptor for the first parameter. */
2430 : 15858 : gfc_init_se (&argse, NULL);
2431 : 15858 : gfc_conv_expr_descriptor (&argse, arg->expr);
2432 : 15858 : gfc_add_block_to_block (&se->pre, &argse.pre);
2433 : 15858 : gfc_add_block_to_block (&se->post, &argse.post);
2434 : :
2435 : 15858 : desc = argse.expr;
2436 : :
2437 : 15858 : as = gfc_get_full_arrayspec_from_expr (arg->expr);
2438 : :
2439 : 15858 : if (INTEGER_CST_P (bound))
2440 : : {
2441 : 7983 : gcc_assert (op != GFC_ISYM_SHAPE);
2442 : 7746 : if (((!as || as->type != AS_ASSUMED_RANK)
2443 : 7123 : && wi::geu_p (wi::to_wide (bound),
2444 : 7123 : GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))))
2445 : 15966 : || wi::gtu_p (wi::to_wide (bound), GFC_MAX_DIMENSIONS))
2446 : 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2447 : : "dimension index",
2448 : : (op == GFC_ISYM_UBOUND) ? "UBOUND" : "LBOUND",
2449 : : &expr->where);
2450 : : }
2451 : :
2452 : 15858 : if (!INTEGER_CST_P (bound) || (as && as->type == AS_ASSUMED_RANK))
2453 : : {
2454 : 8735 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2455 : : {
2456 : 651 : bound = gfc_evaluate_now (bound, &se->pre);
2457 : 651 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2458 : 651 : bound, build_int_cst (TREE_TYPE (bound), 0));
2459 : 651 : if (as && as->type == AS_ASSUMED_RANK)
2460 : 546 : tmp = gfc_conv_descriptor_rank (desc);
2461 : : else
2462 : 105 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))];
2463 : 651 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
2464 : 651 : bound, fold_convert(TREE_TYPE (bound), tmp));
2465 : 651 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2466 : : logical_type_node, cond, tmp);
2467 : 651 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2468 : : gfc_msg_fault);
2469 : : }
2470 : : }
2471 : :
2472 : : /* Take care of the lbound shift for assumed-rank arrays that are
2473 : : nonallocatable and nonpointers. Those have a lbound of 1. */
2474 : 15370 : assumed_rank_lb_one = as && as->type == AS_ASSUMED_RANK
2475 : 10854 : && ((arg->expr->ts.type != BT_CLASS
2476 : 1945 : && !arg->expr->symtree->n.sym->attr.allocatable
2477 : 1945 : && !arg->expr->symtree->n.sym->attr.pointer)
2478 : 896 : || (arg->expr->ts.type == BT_CLASS
2479 : 174 : && !CLASS_DATA (arg->expr)->attr.allocatable
2480 : 174 : && !CLASS_DATA (arg->expr)->attr.class_pointer));
2481 : :
2482 : 15858 : ubound = gfc_conv_descriptor_ubound_get (desc, bound);
2483 : 15858 : lbound = gfc_conv_descriptor_lbound_get (desc, bound);
2484 : 15858 : size = fold_build2_loc (input_location, MINUS_EXPR,
2485 : : gfc_array_index_type, ubound, lbound);
2486 : 15858 : size = fold_build2_loc (input_location, PLUS_EXPR,
2487 : : gfc_array_index_type, size, gfc_index_one_node);
2488 : :
2489 : : /* 13.14.53: Result value for LBOUND
2490 : :
2491 : : Case (i): For an array section or for an array expression other than a
2492 : : whole array or array structure component, LBOUND(ARRAY, DIM)
2493 : : has the value 1. For a whole array or array structure
2494 : : component, LBOUND(ARRAY, DIM) has the value:
2495 : : (a) equal to the lower bound for subscript DIM of ARRAY if
2496 : : dimension DIM of ARRAY does not have extent zero
2497 : : or if ARRAY is an assumed-size array of rank DIM,
2498 : : or (b) 1 otherwise.
2499 : :
2500 : : 13.14.113: Result value for UBOUND
2501 : :
2502 : : Case (i): For an array section or for an array expression other than a
2503 : : whole array or array structure component, UBOUND(ARRAY, DIM)
2504 : : has the value equal to the number of elements in the given
2505 : : dimension; otherwise, it has a value equal to the upper bound
2506 : : for subscript DIM of ARRAY if dimension DIM of ARRAY does
2507 : : not have size zero and has value zero if dimension DIM has
2508 : : size zero. */
2509 : :
2510 : 15858 : if (op == GFC_ISYM_LBOUND && assumed_rank_lb_one)
2511 : 532 : se->expr = gfc_index_one_node;
2512 : 15326 : else if (as)
2513 : : {
2514 : 14838 : if (op == GFC_ISYM_UBOUND)
2515 : : {
2516 : 5290 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2517 : : size, gfc_index_zero_node);
2518 : 9976 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2519 : : gfc_array_index_type, cond,
2520 : : (assumed_rank_lb_one ? size : ubound),
2521 : : gfc_index_zero_node);
2522 : : }
2523 : 9548 : else if (op == GFC_ISYM_LBOUND)
2524 : : {
2525 : 4818 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2526 : : size, gfc_index_zero_node);
2527 : 4818 : if (as->type == AS_ASSUMED_SIZE)
2528 : : {
2529 : 98 : cond1 = fold_build2_loc (input_location, EQ_EXPR,
2530 : : logical_type_node, bound,
2531 : 98 : build_int_cst (TREE_TYPE (bound),
2532 : 98 : arg->expr->rank - 1));
2533 : 98 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2534 : : logical_type_node, cond, cond1);
2535 : : }
2536 : 4818 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2537 : : gfc_array_index_type, cond,
2538 : : lbound, gfc_index_one_node);
2539 : : }
2540 : 4730 : else if (op == GFC_ISYM_SHAPE)
2541 : 4730 : se->expr = fold_build2_loc (input_location, MAX_EXPR,
2542 : : gfc_array_index_type, size,
2543 : : gfc_index_zero_node);
2544 : : else
2545 : 0 : gcc_unreachable ();
2546 : :
2547 : : /* According to F2018 16.9.172, para 5, an assumed rank object,
2548 : : argument associated with and assumed size array, has the ubound
2549 : : of the final dimension set to -1 and UBOUND must return this.
2550 : : Similarly for the SHAPE intrinsic. */
2551 : 14838 : if (op != GFC_ISYM_LBOUND && assumed_rank_lb_one)
2552 : : {
2553 : 793 : tree minus_one = build_int_cst (gfc_array_index_type, -1);
2554 : 793 : tree rank = fold_convert (gfc_array_index_type,
2555 : : gfc_conv_descriptor_rank (desc));
2556 : 793 : rank = fold_build2_loc (input_location, PLUS_EXPR,
2557 : : gfc_array_index_type, rank, minus_one);
2558 : :
2559 : : /* Fix the expression to stop it from becoming even more
2560 : : complicated. */
2561 : 793 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
2562 : :
2563 : : /* Descriptors for assumed-size arrays have ubound = -1
2564 : : in the last dimension. */
2565 : 793 : cond1 = fold_build2_loc (input_location, EQ_EXPR,
2566 : : logical_type_node, ubound, minus_one);
2567 : 793 : cond = fold_build2_loc (input_location, EQ_EXPR,
2568 : : logical_type_node, bound, rank);
2569 : 793 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2570 : : logical_type_node, cond, cond1);
2571 : 793 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2572 : : gfc_array_index_type, cond,
2573 : : minus_one, se->expr);
2574 : : }
2575 : : }
2576 : : else /* as is null; this is an old-fashioned 1-based array. */
2577 : : {
2578 : 488 : if (op != GFC_ISYM_LBOUND)
2579 : : {
2580 : 386 : se->expr = fold_build2_loc (input_location, MAX_EXPR,
2581 : : gfc_array_index_type, size,
2582 : : gfc_index_zero_node);
2583 : : }
2584 : : else
2585 : 102 : se->expr = gfc_index_one_node;
2586 : : }
2587 : :
2588 : :
2589 : 15858 : type = gfc_typenode_for_spec (&expr->ts);
2590 : 15858 : se->expr = convert (type, se->expr);
2591 : 15858 : }
2592 : :
2593 : :
2594 : : static void
2595 : 577 : conv_intrinsic_cobound (gfc_se * se, gfc_expr * expr)
2596 : : {
2597 : 577 : gfc_actual_arglist *arg;
2598 : 577 : gfc_actual_arglist *arg2;
2599 : 577 : gfc_se argse;
2600 : 577 : tree bound, resbound, resbound2, desc, cond, tmp;
2601 : 577 : tree type;
2602 : 577 : int corank;
2603 : :
2604 : 577 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_LCOBOUND
2605 : : || expr->value.function.isym->id == GFC_ISYM_UCOBOUND
2606 : : || expr->value.function.isym->id == GFC_ISYM_THIS_IMAGE);
2607 : :
2608 : 577 : arg = expr->value.function.actual;
2609 : 577 : arg2 = arg->next;
2610 : :
2611 : 577 : gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
2612 : 577 : corank = arg->expr->corank;
2613 : :
2614 : 577 : gfc_init_se (&argse, NULL);
2615 : 577 : argse.want_coarray = 1;
2616 : :
2617 : 577 : gfc_conv_expr_descriptor (&argse, arg->expr);
2618 : 577 : gfc_add_block_to_block (&se->pre, &argse.pre);
2619 : 577 : gfc_add_block_to_block (&se->post, &argse.post);
2620 : 577 : desc = argse.expr;
2621 : :
2622 : 577 : if (se->ss)
2623 : : {
2624 : : /* Create an implicit second parameter from the loop variable. */
2625 : 184 : gcc_assert (!arg2->expr);
2626 : 184 : gcc_assert (corank > 0);
2627 : 184 : gcc_assert (se->loop->dimen == 1);
2628 : 184 : gcc_assert (se->ss->info->expr == expr);
2629 : :
2630 : 184 : bound = se->loop->loopvar[0];
2631 : 368 : bound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2632 : 184 : bound, gfc_rank_cst[arg->expr->rank]);
2633 : 184 : gfc_advance_se_ss_chain (se);
2634 : : }
2635 : : else
2636 : : {
2637 : : /* use the passed argument. */
2638 : 393 : gcc_assert (arg2->expr);
2639 : 393 : gfc_init_se (&argse, NULL);
2640 : 393 : gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
2641 : 393 : gfc_add_block_to_block (&se->pre, &argse.pre);
2642 : 393 : bound = argse.expr;
2643 : :
2644 : 393 : if (INTEGER_CST_P (bound))
2645 : : {
2646 : 299 : if (wi::ltu_p (wi::to_wide (bound), 1)
2647 : 598 : || wi::gtu_p (wi::to_wide (bound),
2648 : 299 : GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
2649 : 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2650 : 0 : "dimension index", expr->value.function.isym->name,
2651 : : &expr->where);
2652 : : }
2653 : 94 : else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2654 : : {
2655 : 36 : bound = gfc_evaluate_now (bound, &se->pre);
2656 : 36 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2657 : 36 : bound, build_int_cst (TREE_TYPE (bound), 1));
2658 : 36 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
2659 : 36 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2660 : : bound, tmp);
2661 : 36 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2662 : : logical_type_node, cond, tmp);
2663 : 36 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2664 : : gfc_msg_fault);
2665 : : }
2666 : :
2667 : :
2668 : : /* Subtract 1 to get to zero based and add dimensions. */
2669 : 393 : switch (arg->expr->rank)
2670 : : {
2671 : 51 : case 0:
2672 : 51 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2673 : : gfc_array_index_type, bound,
2674 : : gfc_index_one_node);
2675 : : case 1:
2676 : : break;
2677 : 36 : default:
2678 : 36 : bound = fold_build2_loc (input_location, PLUS_EXPR,
2679 : : gfc_array_index_type, bound,
2680 : 36 : gfc_rank_cst[arg->expr->rank - 1]);
2681 : : }
2682 : : }
2683 : :
2684 : 577 : resbound = gfc_conv_descriptor_lbound_get (desc, bound);
2685 : :
2686 : : /* Handle UCOBOUND with special handling of the last codimension. */
2687 : 577 : if (expr->value.function.isym->id == GFC_ISYM_UCOBOUND)
2688 : : {
2689 : : /* Last codimension: For -fcoarray=single just return
2690 : : the lcobound - otherwise add
2691 : : ceiling (real (num_images ()) / real (size)) - 1
2692 : : = (num_images () + size - 1) / size - 1
2693 : : = (num_images - 1) / size(),
2694 : : where size is the product of the extent of all but the last
2695 : : codimension. */
2696 : :
2697 : 191 : if (flag_coarray != GFC_FCOARRAY_SINGLE && corank > 1)
2698 : : {
2699 : 30 : tree cosize;
2700 : :
2701 : 30 : cosize = gfc_conv_descriptor_cosize (desc, arg->expr->rank, corank);
2702 : 30 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
2703 : : 2, integer_zero_node,
2704 : 30 : build_int_cst (integer_type_node, -1));
2705 : 30 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2706 : : gfc_array_index_type,
2707 : : fold_convert (gfc_array_index_type, tmp),
2708 : 30 : 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, integer_zero_node,
2720 : 21 : build_int_cst (integer_type_node, -1));
2721 : 21 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2722 : : gfc_array_index_type,
2723 : : fold_convert (gfc_array_index_type, tmp),
2724 : 21 : build_int_cst (gfc_array_index_type, 1));
2725 : 21 : resbound = fold_build2_loc (input_location, PLUS_EXPR,
2726 : : gfc_array_index_type, resbound, tmp);
2727 : : }
2728 : :
2729 : 191 : if (corank > 1)
2730 : : {
2731 : 137 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2732 : : bound,
2733 : 137 : build_int_cst (TREE_TYPE (bound),
2734 : 137 : arg->expr->rank + corank - 1));
2735 : :
2736 : 137 : resbound2 = gfc_conv_descriptor_ubound_get (desc, bound);
2737 : 137 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2738 : : gfc_array_index_type, cond,
2739 : : resbound, resbound2);
2740 : : }
2741 : : else
2742 : 54 : se->expr = resbound;
2743 : : }
2744 : : else
2745 : 386 : se->expr = resbound;
2746 : :
2747 : 577 : type = gfc_typenode_for_spec (&expr->ts);
2748 : 577 : se->expr = convert (type, se->expr);
2749 : 577 : }
2750 : :
2751 : :
2752 : : static void
2753 : 1868 : conv_intrinsic_stride (gfc_se * se, gfc_expr * expr)
2754 : : {
2755 : 1868 : gfc_actual_arglist *array_arg;
2756 : 1868 : gfc_actual_arglist *dim_arg;
2757 : 1868 : gfc_se argse;
2758 : 1868 : tree desc, tmp;
2759 : :
2760 : 1868 : array_arg = expr->value.function.actual;
2761 : 1868 : dim_arg = array_arg->next;
2762 : :
2763 : 1868 : gcc_assert (array_arg->expr->expr_type == EXPR_VARIABLE);
2764 : :
2765 : 1868 : gfc_init_se (&argse, NULL);
2766 : 1868 : gfc_conv_expr_descriptor (&argse, array_arg->expr);
2767 : 1868 : gfc_add_block_to_block (&se->pre, &argse.pre);
2768 : 1868 : gfc_add_block_to_block (&se->post, &argse.post);
2769 : 1868 : desc = argse.expr;
2770 : :
2771 : 1868 : gcc_assert (dim_arg->expr);
2772 : 1868 : gfc_init_se (&argse, NULL);
2773 : 1868 : gfc_conv_expr_type (&argse, dim_arg->expr, gfc_array_index_type);
2774 : 1868 : gfc_add_block_to_block (&se->pre, &argse.pre);
2775 : 1868 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2776 : : argse.expr, gfc_index_one_node);
2777 : 1868 : se->expr = gfc_conv_descriptor_stride_get (desc, tmp);
2778 : 1868 : }
2779 : :
2780 : : static void
2781 : 7554 : gfc_conv_intrinsic_abs (gfc_se * se, gfc_expr * expr)
2782 : : {
2783 : 7554 : tree arg, cabs;
2784 : :
2785 : 7554 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
2786 : :
2787 : 7554 : switch (expr->value.function.actual->expr->ts.type)
2788 : : {
2789 : 6578 : case BT_INTEGER:
2790 : 6578 : case BT_REAL:
2791 : 6578 : se->expr = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (arg),
2792 : : arg);
2793 : 6578 : break;
2794 : :
2795 : 976 : case BT_COMPLEX:
2796 : 976 : cabs = gfc_builtin_decl_for_float_kind (BUILT_IN_CABS, expr->ts.kind);
2797 : 976 : se->expr = build_call_expr_loc (input_location, cabs, 1, arg);
2798 : 976 : break;
2799 : :
2800 : 0 : default:
2801 : 0 : gcc_unreachable ();
2802 : : }
2803 : 7554 : }
2804 : :
2805 : :
2806 : : /* Create a complex value from one or two real components. */
2807 : :
2808 : : static void
2809 : 485 : gfc_conv_intrinsic_cmplx (gfc_se * se, gfc_expr * expr, int both)
2810 : : {
2811 : 485 : tree real;
2812 : 485 : tree imag;
2813 : 485 : tree type;
2814 : 485 : tree *args;
2815 : 485 : unsigned int num_args;
2816 : :
2817 : 485 : num_args = gfc_intrinsic_argument_list_length (expr);
2818 : 485 : args = XALLOCAVEC (tree, num_args);
2819 : :
2820 : 485 : type = gfc_typenode_for_spec (&expr->ts);
2821 : 485 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2822 : 485 : real = convert (TREE_TYPE (type), args[0]);
2823 : 485 : if (both)
2824 : 441 : imag = convert (TREE_TYPE (type), args[1]);
2825 : 44 : else if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE)
2826 : : {
2827 : 30 : imag = fold_build1_loc (input_location, IMAGPART_EXPR,
2828 : 30 : TREE_TYPE (TREE_TYPE (args[0])), args[0]);
2829 : 30 : imag = convert (TREE_TYPE (type), imag);
2830 : : }
2831 : : else
2832 : 14 : imag = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
2833 : :
2834 : 485 : se->expr = fold_build2_loc (input_location, COMPLEX_EXPR, type, real, imag);
2835 : 485 : }
2836 : :
2837 : :
2838 : : /* Remainder function MOD(A, P) = A - INT(A / P) * P
2839 : : MODULO(A, P) = A - FLOOR (A / P) * P
2840 : :
2841 : : The obvious algorithms above are numerically instable for large
2842 : : arguments, hence these intrinsics are instead implemented via calls
2843 : : to the fmod family of functions. It is the responsibility of the
2844 : : user to ensure that the second argument is non-zero. */
2845 : :
2846 : : static void
2847 : 3116 : gfc_conv_intrinsic_mod (gfc_se * se, gfc_expr * expr, int modulo)
2848 : : {
2849 : 3116 : tree type;
2850 : 3116 : tree tmp;
2851 : 3116 : tree test;
2852 : 3116 : tree test2;
2853 : 3116 : tree fmod;
2854 : 3116 : tree zero;
2855 : 3116 : tree args[2];
2856 : :
2857 : 3116 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
2858 : :
2859 : 3116 : switch (expr->ts.type)
2860 : : {
2861 : 2963 : case BT_INTEGER:
2862 : : /* Integer case is easy, we've got a builtin op. */
2863 : 2963 : type = TREE_TYPE (args[0]);
2864 : :
2865 : 2963 : if (modulo)
2866 : 403 : se->expr = fold_build2_loc (input_location, FLOOR_MOD_EXPR, type,
2867 : : args[0], args[1]);
2868 : : else
2869 : 2560 : se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2870 : : args[0], args[1]);
2871 : : break;
2872 : :
2873 : 30 : case BT_UNSIGNED:
2874 : : /* Even easier, we only need one. */
2875 : 30 : type = TREE_TYPE (args[0]);
2876 : 30 : se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2877 : : args[0], args[1]);
2878 : 30 : break;
2879 : :
2880 : 123 : case BT_REAL:
2881 : 123 : fmod = NULL_TREE;
2882 : : /* Check if we have a builtin fmod. */
2883 : 123 : fmod = gfc_builtin_decl_for_float_kind (BUILT_IN_FMOD, expr->ts.kind);
2884 : :
2885 : : /* The builtin should always be available. */
2886 : 123 : gcc_assert (fmod != NULL_TREE);
2887 : :
2888 : 123 : tmp = build_addr (fmod);
2889 : 123 : se->expr = build_call_array_loc (input_location,
2890 : 123 : TREE_TYPE (TREE_TYPE (fmod)),
2891 : : tmp, 2, args);
2892 : 123 : if (modulo == 0)
2893 : 123 : return;
2894 : :
2895 : 25 : type = TREE_TYPE (args[0]);
2896 : :
2897 : 25 : args[0] = gfc_evaluate_now (args[0], &se->pre);
2898 : 25 : args[1] = gfc_evaluate_now (args[1], &se->pre);
2899 : :
2900 : : /* Definition:
2901 : : modulo = arg - floor (arg/arg2) * arg2
2902 : :
2903 : : In order to calculate the result accurately, we use the fmod
2904 : : function as follows.
2905 : :
2906 : : res = fmod (arg, arg2);
2907 : : if (res)
2908 : : {
2909 : : if ((arg < 0) xor (arg2 < 0))
2910 : : res += arg2;
2911 : : }
2912 : : else
2913 : : res = copysign (0., arg2);
2914 : :
2915 : : => As two nested ternary exprs:
2916 : :
2917 : : res = res ? (((arg < 0) xor (arg2 < 0)) ? res + arg2 : res)
2918 : : : copysign (0., arg2);
2919 : :
2920 : : */
2921 : :
2922 : 25 : zero = gfc_build_const (type, integer_zero_node);
2923 : 25 : tmp = gfc_evaluate_now (se->expr, &se->pre);
2924 : 25 : if (!flag_signed_zeros)
2925 : : {
2926 : 1 : test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2927 : : args[0], zero);
2928 : 1 : test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2929 : : args[1], zero);
2930 : 1 : test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
2931 : : logical_type_node, test, test2);
2932 : 1 : test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2933 : : tmp, zero);
2934 : 1 : test = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2935 : : logical_type_node, test, test2);
2936 : 1 : test = gfc_evaluate_now (test, &se->pre);
2937 : 1 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
2938 : : fold_build2_loc (input_location,
2939 : : PLUS_EXPR,
2940 : : type, tmp, args[1]),
2941 : : tmp);
2942 : : }
2943 : : else
2944 : : {
2945 : 24 : tree expr1, copysign, cscall;
2946 : 24 : copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN,
2947 : : expr->ts.kind);
2948 : 24 : test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2949 : : args[0], zero);
2950 : 24 : test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2951 : : args[1], zero);
2952 : 24 : test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
2953 : : logical_type_node, test, test2);
2954 : 24 : expr1 = fold_build3_loc (input_location, COND_EXPR, type, test2,
2955 : : fold_build2_loc (input_location,
2956 : : PLUS_EXPR,
2957 : : type, tmp, args[1]),
2958 : : tmp);
2959 : 24 : test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2960 : : tmp, zero);
2961 : 24 : cscall = build_call_expr_loc (input_location, copysign, 2, zero,
2962 : : args[1]);
2963 : 24 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
2964 : : expr1, cscall);
2965 : : }
2966 : : return;
2967 : :
2968 : 0 : default:
2969 : 0 : gcc_unreachable ();
2970 : : }
2971 : : }
2972 : :
2973 : : /* DSHIFTL(I,J,S) = (I << S) | (J >> (BITSIZE(J) - S))
2974 : : DSHIFTR(I,J,S) = (I << (BITSIZE(I) - S)) | (J >> S)
2975 : : where the right shifts are logical (i.e. 0's are shifted in).
2976 : : Because SHIFT_EXPR's want shifts strictly smaller than the integral
2977 : : type width, we have to special-case both S == 0 and S == BITSIZE(J):
2978 : : DSHIFTL(I,J,0) = I
2979 : : DSHIFTL(I,J,BITSIZE) = J
2980 : : DSHIFTR(I,J,0) = J
2981 : : DSHIFTR(I,J,BITSIZE) = I. */
2982 : :
2983 : : static void
2984 : 132 : gfc_conv_intrinsic_dshift (gfc_se * se, gfc_expr * expr, bool dshiftl)
2985 : : {
2986 : 132 : tree type, utype, stype, arg1, arg2, shift, res, left, right;
2987 : 132 : tree args[3], cond, tmp;
2988 : 132 : int bitsize;
2989 : :
2990 : 132 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
2991 : :
2992 : 132 : gcc_assert (TREE_TYPE (args[0]) == TREE_TYPE (args[1]));
2993 : 132 : type = TREE_TYPE (args[0]);
2994 : 132 : bitsize = TYPE_PRECISION (type);
2995 : 132 : utype = unsigned_type_for (type);
2996 : 132 : stype = TREE_TYPE (args[2]);
2997 : :
2998 : 132 : arg1 = gfc_evaluate_now (args[0], &se->pre);
2999 : 132 : arg2 = gfc_evaluate_now (args[1], &se->pre);
3000 : 132 : shift = gfc_evaluate_now (args[2], &se->pre);
3001 : :
3002 : : /* The generic case. */
3003 : 132 : tmp = fold_build2_loc (input_location, MINUS_EXPR, stype,
3004 : 132 : build_int_cst (stype, bitsize), shift);
3005 : 198 : left = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3006 : : arg1, dshiftl ? shift : tmp);
3007 : :
3008 : 198 : right = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
3009 : : fold_convert (utype, arg2), dshiftl ? tmp : shift);
3010 : 132 : right = fold_convert (type, right);
3011 : :
3012 : 132 : res = fold_build2_loc (input_location, BIT_IOR_EXPR, type, left, right);
3013 : :
3014 : : /* Special cases. */
3015 : 132 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3016 : 132 : build_int_cst (stype, 0));
3017 : 198 : res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3018 : : dshiftl ? arg1 : arg2, res);
3019 : :
3020 : 132 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3021 : 132 : build_int_cst (stype, bitsize));
3022 : 198 : res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3023 : : dshiftl ? arg2 : arg1, res);
3024 : :
3025 : 132 : se->expr = res;
3026 : 132 : }
3027 : :
3028 : :
3029 : : /* Positive difference DIM (x, y) = ((x - y) < 0) ? 0 : x - y. */
3030 : :
3031 : : static void
3032 : 96 : gfc_conv_intrinsic_dim (gfc_se * se, gfc_expr * expr)
3033 : : {
3034 : 96 : tree val;
3035 : 96 : tree tmp;
3036 : 96 : tree type;
3037 : 96 : tree zero;
3038 : 96 : tree args[2];
3039 : :
3040 : 96 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3041 : 96 : type = TREE_TYPE (args[0]);
3042 : :
3043 : 96 : val = fold_build2_loc (input_location, MINUS_EXPR, type, args[0], args[1]);
3044 : 96 : val = gfc_evaluate_now (val, &se->pre);
3045 : :
3046 : 96 : zero = gfc_build_const (type, integer_zero_node);
3047 : 96 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node, val, zero);
3048 : 96 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, zero, val);
3049 : 96 : }
3050 : :
3051 : :
3052 : : /* SIGN(A, B) is absolute value of A times sign of B.
3053 : : The real value versions use library functions to ensure the correct
3054 : : handling of negative zero. Integer case implemented as:
3055 : : SIGN(A, B) = { tmp = (A ^ B) >> C; (A + tmp) ^ tmp }
3056 : : */
3057 : :
3058 : : static void
3059 : 424 : gfc_conv_intrinsic_sign (gfc_se * se, gfc_expr * expr)
3060 : : {
3061 : 424 : tree tmp;
3062 : 424 : tree type;
3063 : 424 : tree args[2];
3064 : :
3065 : 424 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3066 : 424 : if (expr->ts.type == BT_REAL)
3067 : : {
3068 : 162 : tree abs;
3069 : :
3070 : 162 : tmp = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
3071 : 162 : abs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
3072 : :
3073 : : /* We explicitly have to ignore the minus sign. We do so by using
3074 : : result = (arg1 == 0) ? abs(arg0) : copysign(arg0, arg1). */
3075 : 162 : if (!flag_sign_zero
3076 : 198 : && MODE_HAS_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (args[1]))))
3077 : : {
3078 : 12 : tree cond, zero;
3079 : 12 : zero = build_real_from_int_cst (TREE_TYPE (args[1]), integer_zero_node);
3080 : 12 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3081 : : args[1], zero);
3082 : 24 : se->expr = fold_build3_loc (input_location, COND_EXPR,
3083 : 12 : TREE_TYPE (args[0]), cond,
3084 : : build_call_expr_loc (input_location, abs, 1,
3085 : : args[0]),
3086 : : build_call_expr_loc (input_location, tmp, 2,
3087 : : args[0], args[1]));
3088 : : }
3089 : : else
3090 : 150 : se->expr = build_call_expr_loc (input_location, tmp, 2,
3091 : : args[0], args[1]);
3092 : 162 : return;
3093 : : }
3094 : :
3095 : : /* Having excluded floating point types, we know we are now dealing
3096 : : with signed integer types. */
3097 : 262 : type = TREE_TYPE (args[0]);
3098 : :
3099 : : /* Args[0] is used multiple times below. */
3100 : 262 : args[0] = gfc_evaluate_now (args[0], &se->pre);
3101 : :
3102 : : /* Construct (A ^ B) >> 31, which generates a bit mask of all zeros if
3103 : : the signs of A and B are the same, and of all ones if they differ. */
3104 : 262 : tmp = fold_build2_loc (input_location, BIT_XOR_EXPR, type, args[0], args[1]);
3105 : 262 : tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, tmp,
3106 : 262 : build_int_cst (type, TYPE_PRECISION (type) - 1));
3107 : 262 : tmp = gfc_evaluate_now (tmp, &se->pre);
3108 : :
3109 : : /* Construct (A + tmp) ^ tmp, which is A if tmp is zero, and -A if tmp]
3110 : : is all ones (i.e. -1). */
3111 : 262 : se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR, type,
3112 : : fold_build2_loc (input_location, PLUS_EXPR,
3113 : : type, args[0], tmp), tmp);
3114 : : }
3115 : :
3116 : :
3117 : : /* Test for the presence of an optional argument. */
3118 : :
3119 : : static void
3120 : 5045 : gfc_conv_intrinsic_present (gfc_se * se, gfc_expr * expr)
3121 : : {
3122 : 5045 : gfc_expr *arg;
3123 : :
3124 : 5045 : arg = expr->value.function.actual->expr;
3125 : 5045 : gcc_assert (arg->expr_type == EXPR_VARIABLE);
3126 : 5045 : se->expr = gfc_conv_expr_present (arg->symtree->n.sym);
3127 : 5045 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
3128 : 5045 : }
3129 : :
3130 : :
3131 : : /* Calculate the double precision product of two single precision values. */
3132 : :
3133 : : static void
3134 : 13 : gfc_conv_intrinsic_dprod (gfc_se * se, gfc_expr * expr)
3135 : : {
3136 : 13 : tree type;
3137 : 13 : tree args[2];
3138 : :
3139 : 13 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3140 : :
3141 : : /* Convert the args to double precision before multiplying. */
3142 : 13 : type = gfc_typenode_for_spec (&expr->ts);
3143 : 13 : args[0] = convert (type, args[0]);
3144 : 13 : args[1] = convert (type, args[1]);
3145 : 13 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, args[0],
3146 : : args[1]);
3147 : 13 : }
3148 : :
3149 : :
3150 : : /* Return a length one character string containing an ascii character. */
3151 : :
3152 : : static void
3153 : 2020 : gfc_conv_intrinsic_char (gfc_se * se, gfc_expr * expr)
3154 : : {
3155 : 2020 : tree arg[2];
3156 : 2020 : tree var;
3157 : 2020 : tree type;
3158 : 2020 : unsigned int num_args;
3159 : :
3160 : 2020 : num_args = gfc_intrinsic_argument_list_length (expr);
3161 : 2020 : gfc_conv_intrinsic_function_args (se, expr, arg, num_args);
3162 : :
3163 : 2020 : type = gfc_get_char_type (expr->ts.kind);
3164 : 2020 : var = gfc_create_var (type, "char");
3165 : :
3166 : 2020 : arg[0] = fold_build1_loc (input_location, NOP_EXPR, type, arg[0]);
3167 : 2020 : gfc_add_modify (&se->pre, var, arg[0]);
3168 : 2020 : se->expr = gfc_build_addr_expr (build_pointer_type (type), var);
3169 : 2020 : se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3170 : 2020 : }
3171 : :
3172 : :
3173 : : static void
3174 : 0 : gfc_conv_intrinsic_ctime (gfc_se * se, gfc_expr * expr)
3175 : : {
3176 : 0 : tree var;
3177 : 0 : tree len;
3178 : 0 : tree tmp;
3179 : 0 : tree cond;
3180 : 0 : tree fndecl;
3181 : 0 : tree *args;
3182 : 0 : unsigned int num_args;
3183 : :
3184 : 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3185 : 0 : args = XALLOCAVEC (tree, num_args);
3186 : :
3187 : 0 : var = gfc_create_var (pchar_type_node, "pstr");
3188 : 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3189 : :
3190 : 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3191 : 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3192 : 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3193 : :
3194 : 0 : fndecl = build_addr (gfor_fndecl_ctime);
3195 : 0 : tmp = build_call_array_loc (input_location,
3196 : 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_ctime)),
3197 : : fndecl, num_args, args);
3198 : 0 : gfc_add_expr_to_block (&se->pre, tmp);
3199 : :
3200 : : /* Free the temporary afterwards, if necessary. */
3201 : 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3202 : 0 : len, build_int_cst (TREE_TYPE (len), 0));
3203 : 0 : tmp = gfc_call_free (var);
3204 : 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3205 : 0 : gfc_add_expr_to_block (&se->post, tmp);
3206 : :
3207 : 0 : se->expr = var;
3208 : 0 : se->string_length = len;
3209 : 0 : }
3210 : :
3211 : :
3212 : : static void
3213 : 0 : gfc_conv_intrinsic_fdate (gfc_se * se, gfc_expr * expr)
3214 : : {
3215 : 0 : tree var;
3216 : 0 : tree len;
3217 : 0 : tree tmp;
3218 : 0 : tree cond;
3219 : 0 : tree fndecl;
3220 : 0 : tree *args;
3221 : 0 : unsigned int num_args;
3222 : :
3223 : 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3224 : 0 : args = XALLOCAVEC (tree, num_args);
3225 : :
3226 : 0 : var = gfc_create_var (pchar_type_node, "pstr");
3227 : 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3228 : :
3229 : 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3230 : 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3231 : 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3232 : :
3233 : 0 : fndecl = build_addr (gfor_fndecl_fdate);
3234 : 0 : tmp = build_call_array_loc (input_location,
3235 : 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_fdate)),
3236 : : fndecl, num_args, args);
3237 : 0 : gfc_add_expr_to_block (&se->pre, tmp);
3238 : :
3239 : : /* Free the temporary afterwards, if necessary. */
3240 : 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3241 : 0 : len, build_int_cst (TREE_TYPE (len), 0));
3242 : 0 : tmp = gfc_call_free (var);
3243 : 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3244 : 0 : gfc_add_expr_to_block (&se->post, tmp);
3245 : :
3246 : 0 : se->expr = var;
3247 : 0 : se->string_length = len;
3248 : 0 : }
3249 : :
3250 : :
3251 : : /* Generate a direct call to free() for the FREE subroutine. */
3252 : :
3253 : : static tree
3254 : 10 : conv_intrinsic_free (gfc_code *code)
3255 : : {
3256 : 10 : stmtblock_t block;
3257 : 10 : gfc_se argse;
3258 : 10 : tree arg, call;
3259 : :
3260 : 10 : gfc_init_se (&argse, NULL);
3261 : 10 : gfc_conv_expr (&argse, code->ext.actual->expr);
3262 : 10 : arg = fold_convert (ptr_type_node, argse.expr);
3263 : :
3264 : 10 : gfc_init_block (&block);
3265 : 10 : call = build_call_expr_loc (input_location,
3266 : : builtin_decl_explicit (BUILT_IN_FREE), 1, arg);
3267 : 10 : gfc_add_expr_to_block (&block, call);
3268 : 10 : return gfc_finish_block (&block);
3269 : : }
3270 : :
3271 : :
3272 : : /* Call the RANDOM_INIT library subroutine with a hidden argument for
3273 : : handling seeding on coarray images. */
3274 : :
3275 : : static tree
3276 : 90 : conv_intrinsic_random_init (gfc_code *code)
3277 : : {
3278 : 90 : stmtblock_t block;
3279 : 90 : gfc_se se;
3280 : 90 : tree arg1, arg2, tmp;
3281 : : /* On none coarray == lib compiles use LOGICAL(4) else regular LOGICAL. */
3282 : 90 : tree used_bool_type_node = flag_coarray == GFC_FCOARRAY_LIB
3283 : 90 : ? logical_type_node
3284 : 90 : : gfc_get_logical_type (4);
3285 : :
3286 : : /* Make the function call. */
3287 : 90 : gfc_init_block (&block);
3288 : 90 : gfc_init_se (&se, NULL);
3289 : :
3290 : : /* Convert REPEATABLE to the desired LOGICAL entity. */
3291 : 90 : gfc_conv_expr (&se, code->ext.actual->expr);
3292 : 90 : gfc_add_block_to_block (&block, &se.pre);
3293 : 90 : arg1 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3294 : 90 : gfc_add_block_to_block (&block, &se.post);
3295 : :
3296 : : /* Convert IMAGE_DISTINCT to the desired LOGICAL entity. */
3297 : 90 : gfc_conv_expr (&se, code->ext.actual->next->expr);
3298 : 90 : gfc_add_block_to_block (&block, &se.pre);
3299 : 90 : arg2 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3300 : 90 : gfc_add_block_to_block (&block, &se.post);
3301 : :
3302 : 90 : if (flag_coarray == GFC_FCOARRAY_LIB)
3303 : : {
3304 : 0 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
3305 : : 2, arg1, arg2);
3306 : : }
3307 : : else
3308 : : {
3309 : : /* The ABI for libgfortran needs to be maintained, so a hidden
3310 : : argument must be include if code is compiled with -fcoarray=single
3311 : : or without the option. Set to 0. */
3312 : 90 : tree arg3 = build_int_cst (gfc_get_int_type (4), 0);
3313 : 90 : tmp = build_call_expr_loc (input_location, gfor_fndecl_random_init,
3314 : : 3, arg1, arg2, arg3);
3315 : : }
3316 : :
3317 : 90 : gfc_add_expr_to_block (&block, tmp);
3318 : :
3319 : 90 : return gfc_finish_block (&block);
3320 : : }
3321 : :
3322 : :
3323 : : /* Call the SYSTEM_CLOCK library functions, handling the type and kind
3324 : : conversions. */
3325 : :
3326 : : static tree
3327 : 194 : conv_intrinsic_system_clock (gfc_code *code)
3328 : : {
3329 : 194 : stmtblock_t block;
3330 : 194 : gfc_se count_se, count_rate_se, count_max_se;
3331 : 194 : tree arg1 = NULL_TREE, arg2 = NULL_TREE, arg3 = NULL_TREE;
3332 : 194 : tree tmp;
3333 : 194 : int least;
3334 : :
3335 : 194 : gfc_expr *count = code->ext.actual->expr;
3336 : 194 : gfc_expr *count_rate = code->ext.actual->next->expr;
3337 : 194 : gfc_expr *count_max = code->ext.actual->next->next->expr;
3338 : :
3339 : : /* Evaluate our arguments. */
3340 : 194 : if (count)
3341 : : {
3342 : 194 : gfc_init_se (&count_se, NULL);
3343 : 194 : gfc_conv_expr (&count_se, count);
3344 : : }
3345 : :
3346 : 194 : if (count_rate)
3347 : : {
3348 : 181 : gfc_init_se (&count_rate_se, NULL);
3349 : 181 : gfc_conv_expr (&count_rate_se, count_rate);
3350 : : }
3351 : :
3352 : 194 : if (count_max)
3353 : : {
3354 : 180 : gfc_init_se (&count_max_se, NULL);
3355 : 180 : gfc_conv_expr (&count_max_se, count_max);
3356 : : }
3357 : :
3358 : : /* Find the smallest kind found of the arguments. */
3359 : 194 : least = 16;
3360 : 194 : least = (count && count->ts.kind < least) ? count->ts.kind : least;
3361 : 194 : least = (count_rate && count_rate->ts.kind < least) ? count_rate->ts.kind
3362 : : : least;
3363 : 194 : least = (count_max && count_max->ts.kind < least) ? count_max->ts.kind
3364 : : : least;
3365 : :
3366 : : /* Prepare temporary variables. */
3367 : :
3368 : 194 : if (count)
3369 : : {
3370 : 194 : if (least >= 8)
3371 : 18 : arg1 = gfc_create_var (gfc_get_int_type (8), "count");
3372 : 176 : else if (least == 4)
3373 : 152 : arg1 = gfc_create_var (gfc_get_int_type (4), "count");
3374 : 24 : else if (count->ts.kind == 1)
3375 : 12 : arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[0].pedantic_min_int,
3376 : : count->ts.kind);
3377 : : else
3378 : 12 : arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[1].pedantic_min_int,
3379 : : count->ts.kind);
3380 : : }
3381 : :
3382 : 194 : if (count_rate)
3383 : : {
3384 : 181 : if (least >= 8)
3385 : 18 : arg2 = gfc_create_var (gfc_get_int_type (8), "count_rate");
3386 : 163 : else if (least == 4)
3387 : 139 : arg2 = gfc_create_var (gfc_get_int_type (4), "count_rate");
3388 : : else
3389 : 24 : arg2 = integer_zero_node;
3390 : : }
3391 : :
3392 : 194 : if (count_max)
3393 : : {
3394 : 180 : if (least >= 8)
3395 : 18 : arg3 = gfc_create_var (gfc_get_int_type (8), "count_max");
3396 : 162 : else if (least == 4)
3397 : 138 : arg3 = gfc_create_var (gfc_get_int_type (4), "count_max");
3398 : : else
3399 : 24 : arg3 = integer_zero_node;
3400 : : }
3401 : :
3402 : : /* Make the function call. */
3403 : 194 : gfc_init_block (&block);
3404 : :
3405 : 194 : if (least <= 2)
3406 : : {
3407 : 24 : if (least == 1)
3408 : : {
3409 : 12 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3410 : : : null_pointer_node;
3411 : 12 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3412 : : : null_pointer_node;
3413 : 12 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3414 : : : null_pointer_node;
3415 : : }
3416 : :
3417 : 24 : if (least == 2)
3418 : : {
3419 : 12 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3420 : : : null_pointer_node;
3421 : 12 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3422 : : : null_pointer_node;
3423 : 12 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3424 : : : null_pointer_node;
3425 : : }
3426 : : }
3427 : : else
3428 : : {
3429 : 170 : if (least == 4)
3430 : : {
3431 : 581 : tmp = build_call_expr_loc (input_location,
3432 : : gfor_fndecl_system_clock4, 3,
3433 : 152 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3434 : : : null_pointer_node,
3435 : 139 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3436 : : : null_pointer_node,
3437 : 138 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3438 : : : null_pointer_node);
3439 : 152 : gfc_add_expr_to_block (&block, tmp);
3440 : : }
3441 : : /* Handle kind>=8, 10, or 16 arguments */
3442 : 170 : if (least >= 8)
3443 : : {
3444 : 72 : tmp = build_call_expr_loc (input_location,
3445 : : gfor_fndecl_system_clock8, 3,
3446 : 18 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3447 : : : null_pointer_node,
3448 : 18 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3449 : : : null_pointer_node,
3450 : 18 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3451 : : : null_pointer_node);
3452 : 18 : gfc_add_expr_to_block (&block, tmp);
3453 : : }
3454 : : }
3455 : :
3456 : : /* And store values back if needed. */
3457 : 194 : if (arg1 && arg1 != count_se.expr)
3458 : 194 : gfc_add_modify (&block, count_se.expr,
3459 : 194 : fold_convert (TREE_TYPE (count_se.expr), arg1));
3460 : 194 : if (arg2 && arg2 != count_rate_se.expr)
3461 : 181 : gfc_add_modify (&block, count_rate_se.expr,
3462 : 181 : fold_convert (TREE_TYPE (count_rate_se.expr), arg2));
3463 : 194 : if (arg3 && arg3 != count_max_se.expr)
3464 : 180 : gfc_add_modify (&block, count_max_se.expr,
3465 : 180 : fold_convert (TREE_TYPE (count_max_se.expr), arg3));
3466 : :
3467 : 194 : return gfc_finish_block (&block);
3468 : : }
3469 : :
3470 : :
3471 : : /* Return a character string containing the tty name. */
3472 : :
3473 : : static void
3474 : 0 : gfc_conv_intrinsic_ttynam (gfc_se * se, gfc_expr * expr)
3475 : : {
3476 : 0 : tree var;
3477 : 0 : tree len;
3478 : 0 : tree tmp;
3479 : 0 : tree cond;
3480 : 0 : tree fndecl;
3481 : 0 : tree *args;
3482 : 0 : unsigned int num_args;
3483 : :
3484 : 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3485 : 0 : args = XALLOCAVEC (tree, num_args);
3486 : :
3487 : 0 : var = gfc_create_var (pchar_type_node, "pstr");
3488 : 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3489 : :
3490 : 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3491 : 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3492 : 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3493 : :
3494 : 0 : fndecl = build_addr (gfor_fndecl_ttynam);
3495 : 0 : tmp = build_call_array_loc (input_location,
3496 : 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_ttynam)),
3497 : : fndecl, num_args, args);
3498 : 0 : gfc_add_expr_to_block (&se->pre, tmp);
3499 : :
3500 : : /* Free the temporary afterwards, if necessary. */
3501 : 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3502 : 0 : len, build_int_cst (TREE_TYPE (len), 0));
3503 : 0 : tmp = gfc_call_free (var);
3504 : 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3505 : 0 : gfc_add_expr_to_block (&se->post, tmp);
3506 : :
3507 : 0 : se->expr = var;
3508 : 0 : se->string_length = len;
3509 : 0 : }
3510 : :
3511 : :
3512 : : /* Get the minimum/maximum value of all the parameters.
3513 : : minmax (a1, a2, a3, ...)
3514 : : {
3515 : : mvar = a1;
3516 : : mvar = COMP (mvar, a2)
3517 : : mvar = COMP (mvar, a3)
3518 : : ...
3519 : : return mvar;
3520 : : }
3521 : : Where COMP is MIN/MAX_EXPR for integral types or when we don't
3522 : : care about NaNs, or IFN_FMIN/MAX when the target has support for
3523 : : fast NaN-honouring min/max. When neither holds expand a sequence
3524 : : of explicit comparisons. */
3525 : :
3526 : : /* TODO: Mismatching types can occur when specific names are used.
3527 : : These should be handled during resolution. */
3528 : : static void
3529 : 1302 : gfc_conv_intrinsic_minmax (gfc_se * se, gfc_expr * expr, enum tree_code op)
3530 : : {
3531 : 1302 : tree tmp;
3532 : 1302 : tree mvar;
3533 : 1302 : tree val;
3534 : 1302 : tree *args;
3535 : 1302 : tree type;
3536 : 1302 : tree argtype;
3537 : 1302 : gfc_actual_arglist *argexpr;
3538 : 1302 : unsigned int i, nargs;
3539 : :
3540 : 1302 : nargs = gfc_intrinsic_argument_list_length (expr);
3541 : 1302 : args = XALLOCAVEC (tree, nargs);
3542 : :
3543 : 1302 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
3544 : 1302 : type = gfc_typenode_for_spec (&expr->ts);
3545 : :
3546 : : /* Only evaluate the argument once. */
3547 : 1302 : if (!VAR_P (args[0]) && !TREE_CONSTANT (args[0]))
3548 : 303 : args[0] = gfc_evaluate_now (args[0], &se->pre);
3549 : :
3550 : : /* Determine suitable type of temporary, as a GNU extension allows
3551 : : different argument kinds. */
3552 : 1302 : argtype = TREE_TYPE (args[0]);
3553 : 1302 : argexpr = expr->value.function.actual;
3554 : 2823 : for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3555 : : {
3556 : 1521 : tree tmptype = TREE_TYPE (args[i]);
3557 : 1521 : if (TYPE_PRECISION (tmptype) > TYPE_PRECISION (argtype))
3558 : 1 : argtype = tmptype;
3559 : : }
3560 : 1302 : mvar = gfc_create_var (argtype, "M");
3561 : 1302 : gfc_add_modify (&se->pre, mvar, convert (argtype, args[0]));
3562 : :
3563 : 1302 : argexpr = expr->value.function.actual;
3564 : 2823 : for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3565 : : {
3566 : 1521 : tree cond = NULL_TREE;
3567 : 1521 : val = args[i];
3568 : :
3569 : : /* Handle absent optional arguments by ignoring the comparison. */
3570 : 1521 : if (argexpr->expr->expr_type == EXPR_VARIABLE
3571 : 902 : && argexpr->expr->symtree->n.sym->attr.optional
3572 : 45 : && INDIRECT_REF_P (val))
3573 : : {
3574 : 84 : cond = fold_build2_loc (input_location,
3575 : : NE_EXPR, logical_type_node,
3576 : 42 : TREE_OPERAND (val, 0),
3577 : 42 : build_int_cst (TREE_TYPE (TREE_OPERAND (val, 0)), 0));
3578 : : }
3579 : 1479 : else if (!VAR_P (val) && !TREE_CONSTANT (val))
3580 : : /* Only evaluate the argument once. */
3581 : 573 : val = gfc_evaluate_now (val, &se->pre);
3582 : :
3583 : 1521 : tree calc;
3584 : : /* For floating point types, the question is what MAX(a, NaN) or
3585 : : MIN(a, NaN) should return (where "a" is a normal number).
3586 : : There are valid use case for returning either one, but the
3587 : : Fortran standard doesn't specify which one should be chosen.
3588 : : Also, there is no consensus among other tested compilers. In
3589 : : short, it's a mess. So lets just do whatever is fastest. */
3590 : 1521 : tree_code code = op == GT_EXPR ? MAX_EXPR : MIN_EXPR;
3591 : 1521 : calc = fold_build2_loc (input_location, code, argtype,
3592 : : convert (argtype, val), mvar);
3593 : 1521 : tmp = build2_v (MODIFY_EXPR, mvar, calc);
3594 : :
3595 : 1521 : if (cond != NULL_TREE)
3596 : 42 : tmp = build3_v (COND_EXPR, cond, tmp,
3597 : : build_empty_stmt (input_location));
3598 : 1521 : gfc_add_expr_to_block (&se->pre, tmp);
3599 : : }
3600 : 1302 : se->expr = convert (type, mvar);
3601 : 1302 : }
3602 : :
3603 : :
3604 : : /* Generate library calls for MIN and MAX intrinsics for character
3605 : : variables. */
3606 : : static void
3607 : 282 : gfc_conv_intrinsic_minmax_char (gfc_se * se, gfc_expr * expr, int op)
3608 : : {
3609 : 282 : tree *args;
3610 : 282 : tree var, len, fndecl, tmp, cond, function;
3611 : 282 : unsigned int nargs;
3612 : :
3613 : 282 : nargs = gfc_intrinsic_argument_list_length (expr);
3614 : 282 : args = XALLOCAVEC (tree, nargs + 4);
3615 : 282 : gfc_conv_intrinsic_function_args (se, expr, &args[4], nargs);
3616 : :
3617 : : /* Create the result variables. */
3618 : 282 : len = gfc_create_var (gfc_charlen_type_node, "len");
3619 : 282 : args[0] = gfc_build_addr_expr (NULL_TREE, len);
3620 : 282 : var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
3621 : 282 : args[1] = gfc_build_addr_expr (ppvoid_type_node, var);
3622 : 282 : args[2] = build_int_cst (integer_type_node, op);
3623 : 282 : args[3] = build_int_cst (integer_type_node, nargs / 2);
3624 : :
3625 : 282 : if (expr->ts.kind == 1)
3626 : 210 : function = gfor_fndecl_string_minmax;
3627 : 72 : else if (expr->ts.kind == 4)
3628 : 72 : function = gfor_fndecl_string_minmax_char4;
3629 : : else
3630 : 0 : gcc_unreachable ();
3631 : :
3632 : : /* Make the function call. */
3633 : 282 : fndecl = build_addr (function);
3634 : 282 : tmp = build_call_array_loc (input_location,
3635 : 282 : TREE_TYPE (TREE_TYPE (function)), fndecl,
3636 : : nargs + 4, args);
3637 : 282 : gfc_add_expr_to_block (&se->pre, tmp);
3638 : :
3639 : : /* Free the temporary afterwards, if necessary. */
3640 : 282 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3641 : 282 : len, build_int_cst (TREE_TYPE (len), 0));
3642 : 282 : tmp = gfc_call_free (var);
3643 : 282 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3644 : 282 : gfc_add_expr_to_block (&se->post, tmp);
3645 : :
3646 : 282 : se->expr = var;
3647 : 282 : se->string_length = len;
3648 : 282 : }
3649 : :
3650 : :
3651 : : /* Create a symbol node for this intrinsic. The symbol from the frontend
3652 : : has the generic name. */
3653 : :
3654 : : static gfc_symbol *
3655 : 11277 : gfc_get_symbol_for_expr (gfc_expr * expr, bool ignore_optional)
3656 : : {
3657 : 11277 : gfc_symbol *sym;
3658 : :
3659 : : /* TODO: Add symbols for intrinsic function to the global namespace. */
3660 : 11277 : gcc_assert (strlen (expr->value.function.name) <= GFC_MAX_SYMBOL_LEN - 5);
3661 : 11277 : sym = gfc_new_symbol (expr->value.function.name, NULL);
3662 : :
3663 : 11277 : sym->ts = expr->ts;
3664 : 11277 : if (sym->ts.type == BT_CHARACTER)
3665 : 1781 : sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3666 : 11277 : sym->attr.external = 1;
3667 : 11277 : sym->attr.function = 1;
3668 : 11277 : sym->attr.always_explicit = 1;
3669 : 11277 : sym->attr.proc = PROC_INTRINSIC;
3670 : 11277 : sym->attr.flavor = FL_PROCEDURE;
3671 : 11277 : sym->result = sym;
3672 : 11277 : if (expr->rank > 0)
3673 : : {
3674 : 9959 : sym->attr.dimension = 1;
3675 : 9959 : sym->as = gfc_get_array_spec ();
3676 : 9959 : sym->as->type = AS_ASSUMED_SHAPE;
3677 : 9959 : sym->as->rank = expr->rank;
3678 : : }
3679 : :
3680 : 11277 : gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3681 : : ignore_optional ? expr->value.function.actual
3682 : : : NULL);
3683 : :
3684 : 11277 : return sym;
3685 : : }
3686 : :
3687 : : /* Remove empty actual arguments. */
3688 : :
3689 : : static void
3690 : 6999 : remove_empty_actual_arguments (gfc_actual_arglist **ap)
3691 : : {
3692 : 36764 : while (*ap)
3693 : : {
3694 : 29765 : if ((*ap)->expr == NULL)
3695 : : {
3696 : 8690 : gfc_actual_arglist *r = *ap;
3697 : 8690 : *ap = r->next;
3698 : 8690 : r->next = NULL;
3699 : 8690 : gfc_free_actual_arglist (r);
3700 : : }
3701 : : else
3702 : 21075 : ap = &((*ap)->next);
3703 : : }
3704 : 6999 : }
3705 : :
3706 : : #define MAX_SPEC_ARG 12
3707 : :
3708 : : /* Make up an fn spec that's right for intrinsic functions that we
3709 : : want to call. */
3710 : :
3711 : : static char *
3712 : 1567 : intrinsic_fnspec (gfc_expr *expr)
3713 : : {
3714 : 1567 : static char fnspec_buf[MAX_SPEC_ARG*2+1];
3715 : 1567 : char *fp;
3716 : 1567 : int i;
3717 : 1567 : int num_char_args;
3718 : :
3719 : : #define ADD_CHAR(c) do { *fp++ = c; *fp++ = ' '; } while(0)
3720 : :
3721 : : /* Set the fndecl. */
3722 : 1567 : fp = fnspec_buf;
3723 : : /* Function return value. FIXME: Check if the second letter could
3724 : : be something other than a space, for further optimization. */
3725 : 1567 : ADD_CHAR ('.');
3726 : 1567 : if (expr->rank == 0)
3727 : : {
3728 : 166 : if (expr->ts.type == BT_CHARACTER)
3729 : : {
3730 : 84 : ADD_CHAR ('w'); /* Address of character. */
3731 : 84 : ADD_CHAR ('.'); /* Length of character. */
3732 : : }
3733 : : }
3734 : : else
3735 : 1401 : ADD_CHAR ('w'); /* Return value is a descriptor. */
3736 : :
3737 : 1567 : num_char_args = 0;
3738 : 7968 : for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
3739 : : {
3740 : 6401 : if (a->expr == NULL)
3741 : 1919 : continue;
3742 : :
3743 : 4482 : if (a->name && strcmp (a->name,"%VAL") == 0)
3744 : 934 : ADD_CHAR ('.');
3745 : : else
3746 : : {
3747 : 3548 : if (a->expr->rank > 0)
3748 : 2091 : ADD_CHAR ('r');
3749 : : else
3750 : 1457 : ADD_CHAR ('R');
3751 : : }
3752 : 4482 : num_char_args += a->expr->ts.type == BT_CHARACTER;
3753 : 4482 : gcc_assert (fp - fnspec_buf + num_char_args <= MAX_SPEC_ARG*2);
3754 : : }
3755 : :
3756 : 2191 : for (i = 0; i < num_char_args; i++)
3757 : 624 : ADD_CHAR ('.');
3758 : :
3759 : 1567 : *fp = '\0';
3760 : 1567 : return fnspec_buf;
3761 : : }
3762 : :
3763 : : #undef MAX_SPEC_ARG
3764 : : #undef ADD_CHAR
3765 : :
3766 : : /* Generate the right symbol for the specific intrinsic function and
3767 : : modify the expr accordingly. This assumes that absent optional
3768 : : arguments should be removed. */
3769 : :
3770 : : gfc_symbol *
3771 : 6999 : specific_intrinsic_symbol (gfc_expr *expr)
3772 : : {
3773 : 6999 : gfc_symbol *sym;
3774 : :
3775 : 6999 : sym = gfc_find_intrinsic_symbol (expr);
3776 : 6999 : if (sym == NULL)
3777 : : {
3778 : 1567 : sym = gfc_get_intrinsic_function_symbol (expr);
3779 : 1567 : sym->ts = expr->ts;
3780 : 1567 : if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl)
3781 : 240 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
3782 : :
3783 : 1567 : gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3784 : : expr->value.function.actual, true);
3785 : 1567 : sym->backend_decl
3786 : 1567 : = gfc_get_extern_function_decl (sym, expr->value.function.actual,
3787 : 1567 : intrinsic_fnspec (expr));
3788 : : }
3789 : :
3790 : 6999 : remove_empty_actual_arguments (&(expr->value.function.actual));
3791 : :
3792 : 6999 : return sym;
3793 : : }
3794 : :
3795 : : /* Generate a call to an external intrinsic function. FIXME: So far,
3796 : : this only works for functions which are called with well-defined
3797 : : types; CSHIFT and friends will come later. */
3798 : :
3799 : : static void
3800 : 12202 : gfc_conv_intrinsic_funcall (gfc_se * se, gfc_expr * expr)
3801 : : {
3802 : 12202 : gfc_symbol *sym;
3803 : 12202 : vec<tree, va_gc> *append_args;
3804 : 12202 : bool specific_symbol;
3805 : :
3806 : 12202 : gcc_assert (!se->ss || se->ss->info->expr == expr);
3807 : :
3808 : 12202 : if (se->ss)
3809 : 10574 : gcc_assert (expr->rank > 0);
3810 : : else
3811 : 1628 : gcc_assert (expr->rank == 0);
3812 : :
3813 : 12202 : switch (expr->value.function.isym->id)
3814 : : {
3815 : : case GFC_ISYM_ANY:
3816 : : case GFC_ISYM_ALL:
3817 : : case GFC_ISYM_FINDLOC:
3818 : : case GFC_ISYM_MAXLOC:
3819 : : case GFC_ISYM_MINLOC:
3820 : : case GFC_ISYM_MAXVAL:
3821 : : case GFC_ISYM_MINVAL:
3822 : : case GFC_ISYM_NORM2:
3823 : : case GFC_ISYM_PRODUCT:
3824 : : case GFC_ISYM_SUM:
3825 : : specific_symbol = true;
3826 : : break;
3827 : 5203 : default:
3828 : 5203 : specific_symbol = false;
3829 : : }
3830 : :
3831 : 12202 : if (specific_symbol)
3832 : : {
3833 : : /* Need to copy here because specific_intrinsic_symbol modifies
3834 : : expr to omit the absent optional arguments. */
3835 : 6999 : expr = gfc_copy_expr (expr);
3836 : 6999 : sym = specific_intrinsic_symbol (expr);
3837 : : }
3838 : : else
3839 : 5203 : sym = gfc_get_symbol_for_expr (expr, se->ignore_optional);
3840 : :
3841 : : /* Calls to libgfortran_matmul need to be appended special arguments,
3842 : : to be able to call the BLAS ?gemm functions if required and possible. */
3843 : 12202 : append_args = NULL;
3844 : 12202 : if (expr->value.function.isym->id == GFC_ISYM_MATMUL
3845 : 861 : && !expr->external_blas
3846 : 824 : && sym->ts.type != BT_LOGICAL)
3847 : : {
3848 : 808 : tree cint = gfc_get_int_type (gfc_c_int_kind);
3849 : :
3850 : 808 : if (flag_external_blas
3851 : 0 : && (sym->ts.type == BT_REAL || sym->ts.type == BT_COMPLEX)
3852 : 0 : && (sym->ts.kind == 4 || sym->ts.kind == 8))
3853 : : {
3854 : 0 : tree gemm_fndecl;
3855 : :
3856 : 0 : if (sym->ts.type == BT_REAL)
3857 : : {
3858 : 0 : if (sym->ts.kind == 4)
3859 : 0 : gemm_fndecl = gfor_fndecl_sgemm;
3860 : : else
3861 : 0 : gemm_fndecl = gfor_fndecl_dgemm;
3862 : : }
3863 : : else
3864 : : {
3865 : 0 : if (sym->ts.kind == 4)
3866 : 0 : gemm_fndecl = gfor_fndecl_cgemm;
3867 : : else
3868 : 0 : gemm_fndecl = gfor_fndecl_zgemm;
3869 : : }
3870 : :
3871 : 0 : vec_alloc (append_args, 3);
3872 : 0 : append_args->quick_push (build_int_cst (cint, 1));
3873 : 0 : append_args->quick_push (build_int_cst (cint,
3874 : : flag_blas_matmul_limit));
3875 : 0 : append_args->quick_push (gfc_build_addr_expr (NULL_TREE,
3876 : : gemm_fndecl));
3877 : 0 : }
3878 : : else
3879 : : {
3880 : 808 : vec_alloc (append_args, 3);
3881 : 808 : append_args->quick_push (build_int_cst (cint, 0));
3882 : 808 : append_args->quick_push (build_int_cst (cint, 0));
3883 : 808 : append_args->quick_push (null_pointer_node);
3884 : : }
3885 : : }
3886 : : /* Non-character scalar reduce returns a pointer to a result of size set by
3887 : : the element size of 'array'. Setting 'sym' allocatable ensures that the
3888 : : result is deallocated at the appropriate time. */
3889 : 11394 : else if (expr->value.function.isym->id == GFC_ISYM_REDUCE
3890 : 102 : && expr->rank == 0 && expr->ts.type != BT_CHARACTER)
3891 : 96 : sym->attr.allocatable = 1;
3892 : :
3893 : :
3894 : 12202 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
3895 : : append_args);
3896 : :
3897 : 12202 : if (specific_symbol)
3898 : 6999 : gfc_free_expr (expr);
3899 : : else
3900 : 5203 : gfc_free_symbol (sym);
3901 : 12202 : }
3902 : :
3903 : : /* ANY and ALL intrinsics. ANY->op == NE_EXPR, ALL->op == EQ_EXPR.
3904 : : Implemented as
3905 : : any(a)
3906 : : {
3907 : : forall (i=...)
3908 : : if (a[i] != 0)
3909 : : return 1
3910 : : end forall
3911 : : return 0
3912 : : }
3913 : : all(a)
3914 : : {
3915 : : forall (i=...)
3916 : : if (a[i] == 0)
3917 : : return 0
3918 : : end forall
3919 : : return 1
3920 : : }
3921 : : */
3922 : : static void
3923 : 34337 : gfc_conv_intrinsic_anyall (gfc_se * se, gfc_expr * expr, enum tree_code op)
3924 : : {
3925 : 34337 : tree resvar;
3926 : 34337 : stmtblock_t block;
3927 : 34337 : stmtblock_t body;
3928 : 34337 : tree type;
3929 : 34337 : tree tmp;
3930 : 34337 : tree found;
3931 : 34337 : gfc_loopinfo loop;
3932 : 34337 : gfc_actual_arglist *actual;
3933 : 34337 : gfc_ss *arrayss;
3934 : 34337 : gfc_se arrayse;
3935 : 34337 : tree exit_label;
3936 : :
3937 : 34337 : if (se->ss)
3938 : : {
3939 : 0 : gfc_conv_intrinsic_funcall (se, expr);
3940 : 0 : return;
3941 : : }
3942 : :
3943 : 34337 : actual = expr->value.function.actual;
3944 : 34337 : type = gfc_typenode_for_spec (&expr->ts);
3945 : : /* Initialize the result. */
3946 : 34337 : resvar = gfc_create_var (type, "test");
3947 : 34337 : if (op == EQ_EXPR)
3948 : 419 : tmp = convert (type, boolean_true_node);
3949 : : else
3950 : 33918 : tmp = convert (type, boolean_false_node);
3951 : 34337 : gfc_add_modify (&se->pre, resvar, tmp);
3952 : :
3953 : : /* Walk the arguments. */
3954 : 34337 : arrayss = gfc_walk_expr (actual->expr);
3955 : 34337 : gcc_assert (arrayss != gfc_ss_terminator);
3956 : :
3957 : : /* Initialize the scalarizer. */
3958 : 34337 : gfc_init_loopinfo (&loop);
3959 : 34337 : exit_label = gfc_build_label_decl (NULL_TREE);
3960 : 34337 : TREE_USED (exit_label) = 1;
3961 : 34337 : gfc_add_ss_to_loop (&loop, arrayss);
3962 : :
3963 : : /* Initialize the loop. */
3964 : 34337 : gfc_conv_ss_startstride (&loop);
3965 : 34337 : gfc_conv_loop_setup (&loop, &expr->where);
3966 : :
3967 : 34337 : gfc_mark_ss_chain_used (arrayss, 1);
3968 : : /* Generate the loop body. */
3969 : 34337 : gfc_start_scalarized_body (&loop, &body);
3970 : :
3971 : : /* If the condition matches then set the return value. */
3972 : 34337 : gfc_start_block (&block);
3973 : 34337 : if (op == EQ_EXPR)
3974 : 419 : tmp = convert (type, boolean_false_node);
3975 : : else
3976 : 33918 : tmp = convert (type, boolean_true_node);
3977 : 34337 : gfc_add_modify (&block, resvar, tmp);
3978 : :
3979 : : /* And break out of the loop. */
3980 : 34337 : tmp = build1_v (GOTO_EXPR, exit_label);
3981 : 34337 : gfc_add_expr_to_block (&block, tmp);
3982 : :
3983 : 34337 : found = gfc_finish_block (&block);
3984 : :
3985 : : /* Check this element. */
3986 : 34337 : gfc_init_se (&arrayse, NULL);
3987 : 34337 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
3988 : 34337 : arrayse.ss = arrayss;
3989 : 34337 : gfc_conv_expr_val (&arrayse, actual->expr);
3990 : :
3991 : 34337 : gfc_add_block_to_block (&body, &arrayse.pre);
3992 : 34337 : tmp = fold_build2_loc (input_location, op, logical_type_node, arrayse.expr,
3993 : 34337 : build_int_cst (TREE_TYPE (arrayse.expr), 0));
3994 : 34337 : tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
3995 : 34337 : gfc_add_expr_to_block (&body, tmp);
3996 : 34337 : gfc_add_block_to_block (&body, &arrayse.post);
3997 : :
3998 : 34337 : gfc_trans_scalarizing_loops (&loop, &body);
3999 : :
4000 : : /* Add the exit label. */
4001 : 34337 : tmp = build1_v (LABEL_EXPR, exit_label);
4002 : 34337 : gfc_add_expr_to_block (&loop.pre, tmp);
4003 : :
4004 : 34337 : gfc_add_block_to_block (&se->pre, &loop.pre);
4005 : 34337 : gfc_add_block_to_block (&se->pre, &loop.post);
4006 : 34337 : gfc_cleanup_loop (&loop);
4007 : :
4008 : 34337 : se->expr = resvar;
4009 : : }
4010 : :
4011 : :
4012 : : /* Generate the constant 180 / pi, which is used in the conversion
4013 : : of acosd(), asind(), atand(), atan2d(). */
4014 : :
4015 : : static tree
4016 : 288 : rad2deg (int kind)
4017 : : {
4018 : 288 : tree retval;
4019 : 288 : mpfr_t pi, t0;
4020 : :
4021 : 288 : gfc_set_model_kind (kind);
4022 : 288 : mpfr_init (pi);
4023 : 288 : mpfr_init (t0);
4024 : 288 : mpfr_set_si (t0, 180, GFC_RND_MODE);
4025 : 288 : mpfr_const_pi (pi, GFC_RND_MODE);
4026 : 288 : mpfr_div (t0, t0, pi, GFC_RND_MODE);
4027 : 288 : retval = gfc_conv_mpfr_to_tree (t0, kind, 0);
4028 : 288 : mpfr_clear (t0);
4029 : 288 : mpfr_clear (pi);
4030 : 288 : return retval;
4031 : : }
4032 : :
4033 : :
4034 : : static gfc_intrinsic_map_t *
4035 : 498 : gfc_lookup_intrinsic (gfc_isym_id id)
4036 : : {
4037 : 498 : gfc_intrinsic_map_t *m = gfc_intrinsic_map;
4038 : 8400 : for (; m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
4039 : 8400 : if (id == m->id)
4040 : : break;
4041 : 498 : gcc_assert (id == m->id);
4042 : 498 : return m;
4043 : : }
4044 : :
4045 : :
4046 : : /* ACOSD(x) is translated into ACOS(x) * 180 / pi.
4047 : : ASIND(x) is translated into ASIN(x) * 180 / pi.
4048 : : ATAND(x) is translated into ATAN(x) * 180 / pi. */
4049 : :
4050 : : static void
4051 : 216 : gfc_conv_intrinsic_atrigd (gfc_se * se, gfc_expr * expr, gfc_isym_id id)
4052 : : {
4053 : 216 : tree arg;
4054 : 216 : tree atrigd;
4055 : 216 : tree type;
4056 : 216 : gfc_intrinsic_map_t *m;
4057 : :
4058 : 216 : type = gfc_typenode_for_spec (&expr->ts);
4059 : :
4060 : 216 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4061 : :
4062 : 216 : switch (id)
4063 : : {
4064 : 72 : case GFC_ISYM_ACOSD:
4065 : 72 : m = gfc_lookup_intrinsic (GFC_ISYM_ACOS);
4066 : 72 : break;
4067 : 72 : case GFC_ISYM_ASIND:
4068 : 72 : m = gfc_lookup_intrinsic (GFC_ISYM_ASIN);
4069 : 72 : break;
4070 : 72 : case GFC_ISYM_ATAND:
4071 : 72 : m = gfc_lookup_intrinsic (GFC_ISYM_ATAN);
4072 : 72 : break;
4073 : 0 : default:
4074 : 0 : gcc_unreachable ();
4075 : : }
4076 : 216 : atrigd = gfc_get_intrinsic_lib_fndecl (m, expr);
4077 : 216 : atrigd = build_call_expr_loc (input_location, atrigd, 1, arg);
4078 : :
4079 : 216 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atrigd,
4080 : : fold_convert (type, rad2deg (expr->ts.kind)));
4081 : 216 : }
4082 : :
4083 : :
4084 : : /* COTAN(X) is translated into -TAN(X+PI/2) for REAL argument and
4085 : : COS(X) / SIN(X) for COMPLEX argument. */
4086 : :
4087 : : static void
4088 : 102 : gfc_conv_intrinsic_cotan (gfc_se *se, gfc_expr *expr)
4089 : : {
4090 : 102 : gfc_intrinsic_map_t *m;
4091 : 102 : tree arg;
4092 : 102 : tree type;
4093 : :
4094 : 102 : type = gfc_typenode_for_spec (&expr->ts);
4095 : 102 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4096 : :
4097 : 102 : if (expr->ts.type == BT_REAL)
4098 : : {
4099 : 102 : tree tan;
4100 : 102 : tree tmp;
4101 : 102 : mpfr_t pio2;
4102 : :
4103 : : /* Create pi/2. */
4104 : 102 : gfc_set_model_kind (expr->ts.kind);
4105 : 102 : mpfr_init (pio2);
4106 : 102 : mpfr_const_pi (pio2, GFC_RND_MODE);
4107 : 102 : mpfr_div_ui (pio2, pio2, 2, GFC_RND_MODE);
4108 : 102 : tmp = gfc_conv_mpfr_to_tree (pio2, expr->ts.kind, 0);
4109 : 102 : mpfr_clear (pio2);
4110 : :
4111 : : /* Find tan builtin function. */
4112 : 102 : m = gfc_lookup_intrinsic (GFC_ISYM_TAN);
4113 : 102 : tan = gfc_get_intrinsic_lib_fndecl (m, expr);
4114 : 102 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, arg, tmp);
4115 : 102 : tan = build_call_expr_loc (input_location, tan, 1, tmp);
4116 : 102 : se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tan);
4117 : : }
4118 : : else
4119 : : {
4120 : 0 : tree sin;
4121 : 0 : tree cos;
4122 : :
4123 : : /* Find cos builtin function. */
4124 : 0 : m = gfc_lookup_intrinsic (GFC_ISYM_COS);
4125 : 0 : cos = gfc_get_intrinsic_lib_fndecl (m, expr);
4126 : 0 : cos = build_call_expr_loc (input_location, cos, 1, arg);
4127 : :
4128 : : /* Find sin builtin function. */
4129 : 0 : m = gfc_lookup_intrinsic (GFC_ISYM_SIN);
4130 : 0 : sin = gfc_get_intrinsic_lib_fndecl (m, expr);
4131 : 0 : sin = build_call_expr_loc (input_location, sin, 1, arg);
4132 : :
4133 : : /* Divide cos by sin. */
4134 : 0 : se->expr = fold_build2_loc (input_location, RDIV_EXPR, type, cos, sin);
4135 : : }
4136 : 102 : }
4137 : :
4138 : :
4139 : : /* COTAND(X) is translated into -TAND(X+90) for REAL argument. */
4140 : :
4141 : : static void
4142 : 108 : gfc_conv_intrinsic_cotand (gfc_se *se, gfc_expr *expr)
4143 : : {
4144 : 108 : tree arg;
4145 : 108 : tree type;
4146 : 108 : tree ninety_tree;
4147 : 108 : mpfr_t ninety;
4148 : :
4149 : 108 : type = gfc_typenode_for_spec (&expr->ts);
4150 : 108 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4151 : :
4152 : 108 : gfc_set_model_kind (expr->ts.kind);
4153 : :
4154 : : /* Build the tree for x + 90. */
4155 : 108 : mpfr_init_set_ui (ninety, 90, GFC_RND_MODE);
4156 : 108 : ninety_tree = gfc_conv_mpfr_to_tree (ninety, expr->ts.kind, 0);
4157 : 108 : arg = fold_build2_loc (input_location, PLUS_EXPR, type, arg, ninety_tree);
4158 : 108 : mpfr_clear (ninety);
4159 : :
4160 : : /* Find tand. */
4161 : 108 : gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_TAND);
4162 : 108 : tree tand = gfc_get_intrinsic_lib_fndecl (m, expr);
4163 : 108 : tand = build_call_expr_loc (input_location, tand, 1, arg);
4164 : :
4165 : 108 : se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tand);
4166 : 108 : }
4167 : :
4168 : :
4169 : : /* ATAN2D(Y,X) is translated into ATAN2(Y,X) * 180 / PI. */
4170 : :
4171 : : static void
4172 : 72 : gfc_conv_intrinsic_atan2d (gfc_se *se, gfc_expr *expr)
4173 : : {
4174 : 72 : tree args[2];
4175 : 72 : tree atan2d;
4176 : 72 : tree type;
4177 : :
4178 : 72 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
4179 : 72 : type = TREE_TYPE (args[0]);
4180 : :
4181 : 72 : gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_ATAN2);
4182 : 72 : atan2d = gfc_get_intrinsic_lib_fndecl (m, expr);
4183 : 72 : atan2d = build_call_expr_loc (input_location, atan2d, 2, args[0], args[1]);
4184 : :
4185 : 72 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atan2d,
4186 : : rad2deg (expr->ts.kind));
4187 : 72 : }
4188 : :
4189 : :
4190 : : /* COUNT(A) = Number of true elements in A. */
4191 : : static void
4192 : 143 : gfc_conv_intrinsic_count (gfc_se * se, gfc_expr * expr)
4193 : : {
4194 : 143 : tree resvar;
4195 : 143 : tree type;
4196 : 143 : stmtblock_t body;
4197 : 143 : tree tmp;
4198 : 143 : gfc_loopinfo loop;
4199 : 143 : gfc_actual_arglist *actual;
4200 : 143 : gfc_ss *arrayss;
4201 : 143 : gfc_se arrayse;
4202 : :
4203 : 143 : if (se->ss)
4204 : : {
4205 : 0 : gfc_conv_intrinsic_funcall (se, expr);
4206 : 0 : return;
4207 : : }
4208 : :
4209 : 143 : actual = expr->value.function.actual;
4210 : :
4211 : 143 : type = gfc_typenode_for_spec (&expr->ts);
4212 : : /* Initialize the result. */
4213 : 143 : resvar = gfc_create_var (type, "count");
4214 : 143 : gfc_add_modify (&se->pre, resvar, build_int_cst (type, 0));
4215 : :
4216 : : /* Walk the arguments. */
4217 : 143 : arrayss = gfc_walk_expr (actual->expr);
4218 : 143 : gcc_assert (arrayss != gfc_ss_terminator);
4219 : :
4220 : : /* Initialize the scalarizer. */
4221 : 143 : gfc_init_loopinfo (&loop);
4222 : 143 : gfc_add_ss_to_loop (&loop, arrayss);
4223 : :
4224 : : /* Initialize the loop. */
4225 : 143 : gfc_conv_ss_startstride (&loop);
4226 : 143 : gfc_conv_loop_setup (&loop, &expr->where);
4227 : :
4228 : 143 : gfc_mark_ss_chain_used (arrayss, 1);
4229 : : /* Generate the loop body. */
4230 : 143 : gfc_start_scalarized_body (&loop, &body);
4231 : :
4232 : 143 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (resvar),
4233 : 143 : resvar, build_int_cst (TREE_TYPE (resvar), 1));
4234 : 143 : tmp = build2_v (MODIFY_EXPR, resvar, tmp);
4235 : :
4236 : 143 : gfc_init_se (&arrayse, NULL);
4237 : 143 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
4238 : 143 : arrayse.ss = arrayss;
4239 : 143 : gfc_conv_expr_val (&arrayse, actual->expr);
4240 : 143 : tmp = build3_v (COND_EXPR, arrayse.expr, tmp,
4241 : : build_empty_stmt (input_location));
4242 : :
4243 : 143 : gfc_add_block_to_block (&body, &arrayse.pre);
4244 : 143 : gfc_add_expr_to_block (&body, tmp);
4245 : 143 : gfc_add_block_to_block (&body, &arrayse.post);
4246 : :
4247 : 143 : gfc_trans_scalarizing_loops (&loop, &body);
4248 : :
4249 : 143 : gfc_add_block_to_block (&se->pre, &loop.pre);
4250 : 143 : gfc_add_block_to_block (&se->pre, &loop.post);
4251 : 143 : gfc_cleanup_loop (&loop);
4252 : :
4253 : 143 : se->expr = resvar;
4254 : : }
4255 : :
4256 : :
4257 : : /* Update given gfc_se to have ss component pointing to the nested gfc_ss
4258 : : struct and return the corresponding loopinfo. */
4259 : :
4260 : : static gfc_loopinfo *
4261 : 2450 : enter_nested_loop (gfc_se *se)
4262 : : {
4263 : 2450 : se->ss = se->ss->nested_ss;
4264 : 2450 : gcc_assert (se->ss == se->ss->loop->ss);
4265 : :
4266 : 2450 : return se->ss->loop;
4267 : : }
4268 : :
4269 : : /* Build the condition for a mask, which may be optional. */
4270 : :
4271 : : static tree
4272 : 10543 : conv_mask_condition (gfc_se *maskse, gfc_expr *maskexpr,
4273 : : bool optional_mask)
4274 : : {
4275 : 10543 : tree present;
4276 : 10543 : tree type;
4277 : :
4278 : 10543 : if (optional_mask)
4279 : : {
4280 : 206 : type = TREE_TYPE (maskse->expr);
4281 : 206 : present = gfc_conv_expr_present (maskexpr->symtree->n.sym);
4282 : 206 : present = convert (type, present);
4283 : 206 : present = fold_build1_loc (input_location, TRUTH_NOT_EXPR, type,
4284 : : present);
4285 : 206 : return fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
4286 : 206 : type, present, maskse->expr);
4287 : : }
4288 : : else
4289 : 10337 : return maskse->expr;
4290 : : }
4291 : :
4292 : : /* Inline implementation of the sum and product intrinsics. */
4293 : : static void
4294 : 2463 : gfc_conv_intrinsic_arith (gfc_se * se, gfc_expr * expr, enum tree_code op,
4295 : : bool norm2)
4296 : : {
4297 : 2463 : tree resvar;
4298 : 2463 : tree scale = NULL_TREE;
4299 : 2463 : tree type;
4300 : 2463 : stmtblock_t body;
4301 : 2463 : stmtblock_t block;
4302 : 2463 : tree tmp;
4303 : 2463 : gfc_loopinfo loop, *ploop;
4304 : 2463 : gfc_actual_arglist *arg_array, *arg_mask;
4305 : 2463 : gfc_ss *arrayss = NULL;
4306 : 2463 : gfc_ss *maskss = NULL;
4307 : 2463 : gfc_se arrayse;
4308 : 2463 : gfc_se maskse;
4309 : 2463 : gfc_se *parent_se;
4310 : 2463 : gfc_expr *arrayexpr;
4311 : 2463 : gfc_expr *maskexpr;
4312 : 2463 : bool optional_mask;
4313 : :
4314 : 2463 : if (expr->rank > 0)
4315 : : {
4316 : 578 : gcc_assert (gfc_inline_intrinsic_function_p (expr));
4317 : : parent_se = se;
4318 : : }
4319 : : else
4320 : : parent_se = NULL;
4321 : :
4322 : 2463 : type = gfc_typenode_for_spec (&expr->ts);
4323 : : /* Initialize the result. */
4324 : 2463 : resvar = gfc_create_var (type, "val");
4325 : 2463 : if (norm2)
4326 : : {
4327 : : /* result = 0.0;
4328 : : scale = 1.0. */
4329 : 68 : scale = gfc_create_var (type, "scale");
4330 : 68 : gfc_add_modify (&se->pre, scale,
4331 : : gfc_build_const (type, integer_one_node));
4332 : 68 : tmp = gfc_build_const (type, integer_zero_node);
4333 : : }
4334 : 2395 : else if (op == PLUS_EXPR || op == BIT_IOR_EXPR || op == BIT_XOR_EXPR)
4335 : 1991 : tmp = gfc_build_const (type, integer_zero_node);
4336 : 404 : else if (op == NE_EXPR)
4337 : : /* PARITY. */
4338 : 36 : tmp = convert (type, boolean_false_node);
4339 : 368 : else if (op == BIT_AND_EXPR)
4340 : 24 : tmp = gfc_build_const (type, fold_build1_loc (input_location, NEGATE_EXPR,
4341 : : type, integer_one_node));
4342 : : else
4343 : 344 : tmp = gfc_build_const (type, integer_one_node);
4344 : :
4345 : 2463 : gfc_add_modify (&se->pre, resvar, tmp);
4346 : :
4347 : 2463 : arg_array = expr->value.function.actual;
4348 : :
4349 : 2463 : arrayexpr = arg_array->expr;
4350 : :
4351 : 2463 : if (op == NE_EXPR || norm2)
4352 : : {
4353 : : /* PARITY and NORM2. */
4354 : : maskexpr = NULL;
4355 : : optional_mask = false;
4356 : : }
4357 : : else
4358 : : {
4359 : 2359 : arg_mask = arg_array->next->next;
4360 : 2359 : gcc_assert (arg_mask != NULL);
4361 : 2359 : maskexpr = arg_mask->expr;
4362 : 371 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
4363 : 266 : && maskexpr->symtree->n.sym->attr.dummy
4364 : 2625 : && maskexpr->symtree->n.sym->attr.optional;
4365 : : }
4366 : :
4367 : 2463 : if (expr->rank == 0)
4368 : : {
4369 : : /* Walk the arguments. */
4370 : 1885 : arrayss = gfc_walk_expr (arrayexpr);
4371 : 1885 : gcc_assert (arrayss != gfc_ss_terminator);
4372 : :
4373 : 1885 : if (maskexpr && maskexpr->rank > 0)
4374 : : {
4375 : 223 : maskss = gfc_walk_expr (maskexpr);
4376 : 223 : gcc_assert (maskss != gfc_ss_terminator);
4377 : : }
4378 : : else
4379 : : maskss = NULL;
4380 : :
4381 : : /* Initialize the scalarizer. */
4382 : 1885 : gfc_init_loopinfo (&loop);
4383 : :
4384 : : /* We add the mask first because the number of iterations is
4385 : : taken from the last ss, and this breaks if an absent
4386 : : optional argument is used for mask. */
4387 : :
4388 : 1885 : if (maskexpr && maskexpr->rank > 0)
4389 : 223 : gfc_add_ss_to_loop (&loop, maskss);
4390 : 1885 : gfc_add_ss_to_loop (&loop, arrayss);
4391 : :
4392 : : /* Initialize the loop. */
4393 : 1885 : gfc_conv_ss_startstride (&loop);
4394 : 1885 : gfc_conv_loop_setup (&loop, &expr->where);
4395 : :
4396 : 1885 : if (maskexpr && maskexpr->rank > 0)
4397 : 223 : gfc_mark_ss_chain_used (maskss, 1);
4398 : 1885 : gfc_mark_ss_chain_used (arrayss, 1);
4399 : :
4400 : 1885 : ploop = &loop;
4401 : : }
4402 : : else
4403 : : /* All the work has been done in the parent loops. */
4404 : 578 : ploop = enter_nested_loop (se);
4405 : :
4406 : 2463 : gcc_assert (ploop);
4407 : :
4408 : : /* Generate the loop body. */
4409 : 2463 : gfc_start_scalarized_body (ploop, &body);
4410 : :
4411 : : /* If we have a mask, only add this element if the mask is set. */
4412 : 2463 : if (maskexpr && maskexpr->rank > 0)
4413 : : {
4414 : 307 : gfc_init_se (&maskse, parent_se);
4415 : 307 : gfc_copy_loopinfo_to_se (&maskse, ploop);
4416 : 307 : if (expr->rank == 0)
4417 : 223 : maskse.ss = maskss;
4418 : 307 : gfc_conv_expr_val (&maskse, maskexpr);
4419 : 307 : gfc_add_block_to_block (&body, &maskse.pre);
4420 : :
4421 : 307 : gfc_start_block (&block);
4422 : : }
4423 : : else
4424 : 2156 : gfc_init_block (&block);
4425 : :
4426 : : /* Do the actual summation/product. */
4427 : 2463 : gfc_init_se (&arrayse, parent_se);
4428 : 2463 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
4429 : 2463 : if (expr->rank == 0)
4430 : 1885 : arrayse.ss = arrayss;
4431 : 2463 : gfc_conv_expr_val (&arrayse, arrayexpr);
4432 : 2463 : gfc_add_block_to_block (&block, &arrayse.pre);
4433 : :
4434 : 2463 : if (norm2)
4435 : : {
4436 : : /* if (x (i) != 0.0)
4437 : : {
4438 : : absX = abs(x(i))
4439 : : if (absX > scale)
4440 : : {
4441 : : val = scale/absX;
4442 : : result = 1.0 + result * val * val;
4443 : : scale = absX;
4444 : : }
4445 : : else
4446 : : {
4447 : : val = absX/scale;
4448 : : result += val * val;
4449 : : }
4450 : : } */
4451 : 68 : tree res1, res2, cond, absX, val;
4452 : 68 : stmtblock_t ifblock1, ifblock2, ifblock3;
4453 : :
4454 : 68 : gfc_init_block (&ifblock1);
4455 : :
4456 : 68 : absX = gfc_create_var (type, "absX");
4457 : 68 : gfc_add_modify (&ifblock1, absX,
4458 : : fold_build1_loc (input_location, ABS_EXPR, type,
4459 : : arrayse.expr));
4460 : 68 : val = gfc_create_var (type, "val");
4461 : 68 : gfc_add_expr_to_block (&ifblock1, val);
4462 : :
4463 : 68 : gfc_init_block (&ifblock2);
4464 : 68 : gfc_add_modify (&ifblock2, val,
4465 : : fold_build2_loc (input_location, RDIV_EXPR, type, scale,
4466 : : absX));
4467 : 68 : res1 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4468 : 68 : res1 = fold_build2_loc (input_location, MULT_EXPR, type, resvar, res1);
4469 : 68 : res1 = fold_build2_loc (input_location, PLUS_EXPR, type, res1,
4470 : : gfc_build_const (type, integer_one_node));
4471 : 68 : gfc_add_modify (&ifblock2, resvar, res1);
4472 : 68 : gfc_add_modify (&ifblock2, scale, absX);
4473 : 68 : res1 = gfc_finish_block (&ifblock2);
4474 : :
4475 : 68 : gfc_init_block (&ifblock3);
4476 : 68 : gfc_add_modify (&ifblock3, val,
4477 : : fold_build2_loc (input_location, RDIV_EXPR, type, absX,
4478 : : scale));
4479 : 68 : res2 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4480 : 68 : res2 = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, res2);
4481 : 68 : gfc_add_modify (&ifblock3, resvar, res2);
4482 : 68 : res2 = gfc_finish_block (&ifblock3);
4483 : :
4484 : 68 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
4485 : : absX, scale);
4486 : 68 : tmp = build3_v (COND_EXPR, cond, res1, res2);
4487 : 68 : gfc_add_expr_to_block (&ifblock1, tmp);
4488 : 68 : tmp = gfc_finish_block (&ifblock1);
4489 : :
4490 : 68 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
4491 : : arrayse.expr,
4492 : : gfc_build_const (type, integer_zero_node));
4493 : :
4494 : 68 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
4495 : 68 : gfc_add_expr_to_block (&block, tmp);
4496 : : }
4497 : : else
4498 : : {
4499 : 2395 : tmp = fold_build2_loc (input_location, op, type, resvar, arrayse.expr);
4500 : 2395 : gfc_add_modify (&block, resvar, tmp);
4501 : : }
4502 : :
4503 : 2463 : gfc_add_block_to_block (&block, &arrayse.post);
4504 : :
4505 : 2463 : if (maskexpr && maskexpr->rank > 0)
4506 : : {
4507 : : /* We enclose the above in if (mask) {...} . If the mask is an
4508 : : optional argument, generate
4509 : : IF (.NOT. PRESENT(MASK) .OR. MASK(I)). */
4510 : 307 : tree ifmask;
4511 : 307 : tmp = gfc_finish_block (&block);
4512 : 307 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4513 : 307 : tmp = build3_v (COND_EXPR, ifmask, tmp,
4514 : : build_empty_stmt (input_location));
4515 : 307 : }
4516 : : else
4517 : 2156 : tmp = gfc_finish_block (&block);
4518 : 2463 : gfc_add_expr_to_block (&body, tmp);
4519 : :
4520 : 2463 : gfc_trans_scalarizing_loops (ploop, &body);
4521 : :
4522 : : /* For a scalar mask, enclose the loop in an if statement. */
4523 : 2463 : if (maskexpr && maskexpr->rank == 0)
4524 : : {
4525 : 64 : gfc_init_block (&block);
4526 : 64 : gfc_add_block_to_block (&block, &ploop->pre);
4527 : 64 : gfc_add_block_to_block (&block, &ploop->post);
4528 : 64 : tmp = gfc_finish_block (&block);
4529 : :
4530 : 64 : if (expr->rank > 0)
4531 : : {
4532 : 34 : tmp = build3_v (COND_EXPR, se->ss->info->data.scalar.value, tmp,
4533 : : build_empty_stmt (input_location));
4534 : 34 : gfc_advance_se_ss_chain (se);
4535 : : }
4536 : : else
4537 : : {
4538 : 30 : tree ifmask;
4539 : :
4540 : 30 : gcc_assert (expr->rank == 0);
4541 : 30 : gfc_init_se (&maskse, NULL);
4542 : 30 : gfc_conv_expr_val (&maskse, maskexpr);
4543 : 30 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4544 : 30 : tmp = build3_v (COND_EXPR, ifmask, tmp,
4545 : : build_empty_stmt (input_location));
4546 : : }
4547 : :
4548 : 64 : gfc_add_expr_to_block (&block, tmp);
4549 : 64 : gfc_add_block_to_block (&se->pre, &block);
4550 : 64 : gcc_assert (se->post.head == NULL);
4551 : : }
4552 : : else
4553 : : {
4554 : 2399 : gfc_add_block_to_block (&se->pre, &ploop->pre);
4555 : 2399 : gfc_add_block_to_block (&se->pre, &ploop->post);
4556 : : }
4557 : :
4558 : 2463 : if (expr->rank == 0)
4559 : 1885 : gfc_cleanup_loop (ploop);
4560 : :
4561 : 2463 : if (norm2)
4562 : : {
4563 : : /* result = scale * sqrt(result). */
4564 : 68 : tree sqrt;
4565 : 68 : sqrt = gfc_builtin_decl_for_float_kind (BUILT_IN_SQRT, expr->ts.kind);
4566 : 68 : resvar = build_call_expr_loc (input_location,
4567 : : sqrt, 1, resvar);
4568 : 68 : resvar = fold_build2_loc (input_location, MULT_EXPR, type, scale, resvar);
4569 : : }
4570 : :
4571 : 2463 : se->expr = resvar;
4572 : 2463 : }
4573 : :
4574 : :
4575 : : /* Inline implementation of the dot_product intrinsic. This function
4576 : : is based on gfc_conv_intrinsic_arith (the previous function). */
4577 : : static void
4578 : 113 : gfc_conv_intrinsic_dot_product (gfc_se * se, gfc_expr * expr)
4579 : : {
4580 : 113 : tree resvar;
4581 : 113 : tree type;
4582 : 113 : stmtblock_t body;
4583 : 113 : stmtblock_t block;
4584 : 113 : tree tmp;
4585 : 113 : gfc_loopinfo loop;
4586 : 113 : gfc_actual_arglist *actual;
4587 : 113 : gfc_ss *arrayss1, *arrayss2;
4588 : 113 : gfc_se arrayse1, arrayse2;
4589 : 113 : gfc_expr *arrayexpr1, *arrayexpr2;
4590 : :
4591 : 113 : type = gfc_typenode_for_spec (&expr->ts);
4592 : :
4593 : : /* Initialize the result. */
4594 : 113 : resvar = gfc_create_var (type, "val");
4595 : 113 : if (expr->ts.type == BT_LOGICAL)
4596 : 30 : tmp = build_int_cst (type, 0);
4597 : : else
4598 : 83 : tmp = gfc_build_const (type, integer_zero_node);
4599 : :
4600 : 113 : gfc_add_modify (&se->pre, resvar, tmp);
4601 : :
4602 : : /* Walk argument #1. */
4603 : 113 : actual = expr->value.function.actual;
4604 : 113 : arrayexpr1 = actual->expr;
4605 : 113 : arrayss1 = gfc_walk_expr (arrayexpr1);
4606 : 113 : gcc_assert (arrayss1 != gfc_ss_terminator);
4607 : :
4608 : : /* Walk argument #2. */
4609 : 113 : actual = actual->next;
4610 : 113 : arrayexpr2 = actual->expr;
4611 : 113 : arrayss2 = gfc_walk_expr (arrayexpr2);
4612 : 113 : gcc_assert (arrayss2 != gfc_ss_terminator);
4613 : :
4614 : : /* Initialize the scalarizer. */
4615 : 113 : gfc_init_loopinfo (&loop);
4616 : 113 : gfc_add_ss_to_loop (&loop, arrayss1);
4617 : 113 : gfc_add_ss_to_loop (&loop, arrayss2);
4618 : :
4619 : : /* Initialize the loop. */
4620 : 113 : gfc_conv_ss_startstride (&loop);
4621 : 113 : gfc_conv_loop_setup (&loop, &expr->where);
4622 : :
4623 : 113 : gfc_mark_ss_chain_used (arrayss1, 1);
4624 : 113 : gfc_mark_ss_chain_used (arrayss2, 1);
4625 : :
4626 : : /* Generate the loop body. */
4627 : 113 : gfc_start_scalarized_body (&loop, &body);
4628 : 113 : gfc_init_block (&block);
4629 : :
4630 : : /* Make the tree expression for [conjg(]array1[)]. */
4631 : 113 : gfc_init_se (&arrayse1, NULL);
4632 : 113 : gfc_copy_loopinfo_to_se (&arrayse1, &loop);
4633 : 113 : arrayse1.ss = arrayss1;
4634 : 113 : gfc_conv_expr_val (&arrayse1, arrayexpr1);
4635 : 113 : if (expr->ts.type == BT_COMPLEX)
4636 : 9 : arrayse1.expr = fold_build1_loc (input_location, CONJ_EXPR, type,
4637 : : arrayse1.expr);
4638 : 113 : gfc_add_block_to_block (&block, &arrayse1.pre);
4639 : :
4640 : : /* Make the tree expression for array2. */
4641 : 113 : gfc_init_se (&arrayse2, NULL);
4642 : 113 : gfc_copy_loopinfo_to_se (&arrayse2, &loop);
4643 : 113 : arrayse2.ss = arrayss2;
4644 : 113 : gfc_conv_expr_val (&arrayse2, arrayexpr2);
4645 : 113 : gfc_add_block_to_block (&block, &arrayse2.pre);
4646 : :
4647 : : /* Do the actual product and sum. */
4648 : 113 : if (expr->ts.type == BT_LOGICAL)
4649 : : {
4650 : 30 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, type,
4651 : : arrayse1.expr, arrayse2.expr);
4652 : 30 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, type, resvar, tmp);
4653 : : }
4654 : : else
4655 : : {
4656 : 83 : tmp = fold_build2_loc (input_location, MULT_EXPR, type, arrayse1.expr,
4657 : : arrayse2.expr);
4658 : 83 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, tmp);
4659 : : }
4660 : 113 : gfc_add_modify (&block, resvar, tmp);
4661 : :
4662 : : /* Finish up the loop block and the loop. */
4663 : 113 : tmp = gfc_finish_block (&block);
4664 : 113 : gfc_add_expr_to_block (&body, tmp);
4665 : :
4666 : 113 : gfc_trans_scalarizing_loops (&loop, &body);
4667 : 113 : gfc_add_block_to_block (&se->pre, &loop.pre);
4668 : 113 : gfc_add_block_to_block (&se->pre, &loop.post);
4669 : 113 : gfc_cleanup_loop (&loop);
4670 : :
4671 : 113 : se->expr = resvar;
4672 : 113 : }
4673 : :
4674 : :
4675 : : /* Tells whether the expression E is a reference to an optional variable whose
4676 : : presence is not known at compile time. Those are variable references without
4677 : : subreference; if there is a subreference, we can assume the variable is
4678 : : present. We have to special case full arrays, which we represent with a fake
4679 : : "full" reference, and class descriptors for which a reference to data is not
4680 : : really a subreference. */
4681 : :
4682 : : bool
4683 : 10277 : maybe_absent_optional_variable (gfc_expr *e)
4684 : : {
4685 : 10277 : if (!(e && e->expr_type == EXPR_VARIABLE))
4686 : : return false;
4687 : :
4688 : 1100 : gfc_symbol *sym = e->symtree->n.sym;
4689 : 1100 : if (!sym->attr.optional)
4690 : : return false;
4691 : :
4692 : 224 : gfc_ref *ref = e->ref;
4693 : 224 : if (ref == nullptr)
4694 : : return true;
4695 : :
4696 : 20 : if (ref->type == REF_ARRAY
4697 : 20 : && ref->u.ar.type == AR_FULL
4698 : 20 : && ref->next == nullptr)
4699 : : return true;
4700 : :
4701 : 0 : if (!(sym->ts.type == BT_CLASS
4702 : 0 : && ref->type == REF_COMPONENT
4703 : 0 : && ref->u.c.component == CLASS_DATA (sym)))
4704 : : return false;
4705 : :
4706 : 0 : gfc_ref *next_ref = ref->next;
4707 : 0 : if (next_ref == nullptr)
4708 : : return true;
4709 : :
4710 : 0 : if (next_ref->type == REF_ARRAY
4711 : 0 : && next_ref->u.ar.type == AR_FULL
4712 : 0 : && next_ref->next == nullptr)
4713 : 0 : return true;
4714 : :
4715 : : return false;
4716 : : }
4717 : :
4718 : :
4719 : : /* Remove unneeded kind= argument from actual argument list when the
4720 : : result conversion is dealt with in a different place. */
4721 : :
4722 : : static void
4723 : 76 : strip_kind_from_actual (gfc_actual_arglist * actual)
4724 : : {
4725 : 456 : for (gfc_actual_arglist *a = actual; a; a = a->next)
4726 : : {
4727 : 380 : if (a && a->name && strcmp (a->name, "kind") == 0)
4728 : : {
4729 : 12 : gfc_free_expr (a->expr);
4730 : 12 : a->expr = NULL;
4731 : : }
4732 : : }
4733 : 76 : }
4734 : :
4735 : : /* Emit code for minloc or maxloc intrinsic. There are many different cases
4736 : : we need to handle. For performance reasons we sometimes create two
4737 : : loops instead of one, where the second one is much simpler.
4738 : : Examples for minloc intrinsic:
4739 : : A: Result is scalar.
4740 : : 1) Array mask is used and NaNs need to be supported:
4741 : : limit = Infinity;
4742 : : pos = 0;
4743 : : S = from;
4744 : : while (S <= to) {
4745 : : if (mask[S]) {
4746 : : if (pos == 0) pos = S + (1 - from);
4747 : : if (a[S] <= limit) {
4748 : : limit = a[S];
4749 : : pos = S + (1 - from);
4750 : : goto lab1;
4751 : : }
4752 : : }
4753 : : S++;
4754 : : }
4755 : : goto lab2;
4756 : : lab1:;
4757 : : while (S <= to) {
4758 : : if (mask[S])
4759 : : if (a[S] < limit) {
4760 : : limit = a[S];
4761 : : pos = S + (1 - from);
4762 : : }
4763 : : S++;
4764 : : }
4765 : : lab2:;
4766 : : 2) NaNs need to be supported, but it is known at compile time or cheaply
4767 : : at runtime whether array is nonempty or not:
4768 : : limit = Infinity;
4769 : : pos = 0;
4770 : : S = from;
4771 : : while (S <= to) {
4772 : : if (a[S] <= limit) {
4773 : : limit = a[S];
4774 : : pos = S + (1 - from);
4775 : : goto lab1;
4776 : : }
4777 : : S++;
4778 : : }
4779 : : if (from <= to) pos = 1;
4780 : : goto lab2;
4781 : : lab1:;
4782 : : while (S <= to) {
4783 : : if (a[S] < limit) {
4784 : : limit = a[S];
4785 : : pos = S + (1 - from);
4786 : : }
4787 : : S++;
4788 : : }
4789 : : lab2:;
4790 : : 3) NaNs aren't supported, array mask is used:
4791 : : limit = infinities_supported ? Infinity : huge (limit);
4792 : : pos = 0;
4793 : : S = from;
4794 : : while (S <= to) {
4795 : : if (mask[S]) {
4796 : : limit = a[S];
4797 : : pos = S + (1 - from);
4798 : : goto lab1;
4799 : : }
4800 : : S++;
4801 : : }
4802 : : goto lab2;
4803 : : lab1:;
4804 : : while (S <= to) {
4805 : : if (mask[S])
4806 : : if (a[S] < limit) {
4807 : : limit = a[S];
4808 : : pos = S + (1 - from);
4809 : : }
4810 : : S++;
4811 : : }
4812 : : lab2:;
4813 : : 4) Same without array mask:
4814 : : limit = infinities_supported ? Infinity : huge (limit);
4815 : : pos = (from <= to) ? 1 : 0;
4816 : : S = from;
4817 : : while (S <= to) {
4818 : : if (a[S] < limit) {
4819 : : limit = a[S];
4820 : : pos = S + (1 - from);
4821 : : }
4822 : : S++;
4823 : : }
4824 : : B: Array result, non-CHARACTER type, DIM absent
4825 : : Generate similar code as in the scalar case, using a collection of
4826 : : variables (one per dimension) instead of a single variable as result.
4827 : : Picking only cases 1) and 4) with ARRAY of rank 2, the generated code
4828 : : becomes:
4829 : : 1) Array mask is used and NaNs need to be supported:
4830 : : limit = Infinity;
4831 : : pos0 = 0;
4832 : : pos1 = 0;
4833 : : S1 = from1;
4834 : : second_loop_entry = false;
4835 : : while (S1 <= to1) {
4836 : : S0 = from0;
4837 : : while (s0 <= to0 {
4838 : : if (mask[S1][S0]) {
4839 : : if (pos0 == 0) {
4840 : : pos0 = S0 + (1 - from0);
4841 : : pos1 = S1 + (1 - from1);
4842 : : }
4843 : : if (a[S1][S0] <= limit) {
4844 : : limit = a[S1][S0];
4845 : : pos0 = S0 + (1 - from0);
4846 : : pos1 = S1 + (1 - from1);
4847 : : second_loop_entry = true;
4848 : : goto lab1;
4849 : : }
4850 : : }
4851 : : S0++;
4852 : : }
4853 : : S1++;
4854 : : }
4855 : : goto lab2;
4856 : : lab1:;
4857 : : S1 = second_loop_entry ? S1 : from1;
4858 : : while (S1 <= to1) {
4859 : : S0 = second_loop_entry ? S0 : from0;
4860 : : while (S0 <= to0) {
4861 : : if (mask[S1][S0])
4862 : : if (a[S1][S0] < limit) {
4863 : : limit = a[S1][S0];
4864 : : pos0 = S + (1 - from0);
4865 : : pos1 = S + (1 - from1);
4866 : : }
4867 : : second_loop_entry = false;
4868 : : S0++;
4869 : : }
4870 : : S1++;
4871 : : }
4872 : : lab2:;
4873 : : result = { pos0, pos1 };
4874 : : ...
4875 : : 4) NANs aren't supported, no array mask.
4876 : : limit = infinities_supported ? Infinity : huge (limit);
4877 : : pos0 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4878 : : pos1 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4879 : : S1 = from1;
4880 : : while (S1 <= to1) {
4881 : : S0 = from0;
4882 : : while (S0 <= to0) {
4883 : : if (a[S1][S0] < limit) {
4884 : : limit = a[S1][S0];
4885 : : pos0 = S + (1 - from0);
4886 : : pos1 = S + (1 - from1);
4887 : : }
4888 : : S0++;
4889 : : }
4890 : : S1++;
4891 : : }
4892 : : result = { pos0, pos1 };
4893 : : C: Otherwise, a call is generated.
4894 : : For 2) and 4), if mask is scalar, this all goes into a conditional,
4895 : : setting pos = 0; in the else branch.
4896 : :
4897 : : Since we now also support the BACK argument, instead of using
4898 : : if (a[S] < limit), we now use
4899 : :
4900 : : if (back)
4901 : : cond = a[S] <= limit;
4902 : : else
4903 : : cond = a[S] < limit;
4904 : : if (cond) {
4905 : : ....
4906 : :
4907 : : The optimizer is smart enough to move the condition out of the loop.
4908 : : They are now marked as unlikely too for further speedup. */
4909 : :
4910 : : static void
4911 : 13312 : gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op)
4912 : : {
4913 : 13312 : stmtblock_t body;
4914 : 13312 : stmtblock_t block;
4915 : 13312 : stmtblock_t ifblock;
4916 : 13312 : stmtblock_t elseblock;
4917 : 13312 : tree limit;
4918 : 13312 : tree type;
4919 : 13312 : tree tmp;
4920 : 13312 : tree cond;
4921 : 13312 : tree elsetmp;
4922 : 13312 : tree ifbody;
4923 : 13312 : tree offset[GFC_MAX_DIMENSIONS];
4924 : 13312 : tree nonempty;
4925 : 13312 : tree lab1, lab2;
4926 : 13312 : tree b_if, b_else;
4927 : 13312 : tree back;
4928 : 13312 : gfc_loopinfo loop, *ploop;
4929 : 13312 : gfc_actual_arglist *actual, *array_arg, *dim_arg, *mask_arg, *kind_arg;
4930 : 13312 : gfc_actual_arglist *back_arg;
4931 : 13312 : gfc_ss *arrayss = nullptr;
4932 : 13312 : gfc_ss *maskss = nullptr;
4933 : 13312 : gfc_ss *orig_ss = nullptr;
4934 : 13312 : gfc_se arrayse;
4935 : 13312 : gfc_se maskse;
4936 : 13312 : gfc_se nested_se;
4937 : 13312 : gfc_se *base_se;
4938 : 13312 : gfc_expr *arrayexpr;
4939 : 13312 : gfc_expr *maskexpr;
4940 : 13312 : gfc_expr *backexpr;
4941 : 13312 : gfc_se backse;
4942 : 13312 : tree pos[GFC_MAX_DIMENSIONS];
4943 : 13312 : tree idx[GFC_MAX_DIMENSIONS];
4944 : 13312 : tree result_var = NULL_TREE;
4945 : 13312 : int n;
4946 : 13312 : bool optional_mask;
4947 : :
4948 : 13312 : actual = expr->value.function.actual;
4949 : 13312 : array_arg = actual;
4950 : 13312 : dim_arg = array_arg->next;
4951 : 13312 : mask_arg = dim_arg->next;
4952 : 13312 : kind_arg = mask_arg->next;
4953 : 13312 : back_arg = kind_arg->next;
4954 : :
4955 : 13312 : bool dim_present = dim_arg->expr != nullptr;
4956 : 13312 : bool nested_loop = dim_present && expr->rank > 0;
4957 : :
4958 : : /* The last argument, BACK, is passed by value. Ensure that
4959 : : by setting its name to %VAL. */
4960 : 79872 : for (gfc_actual_arglist *a = actual; a; a = a->next)
4961 : : {
4962 : 66560 : if (a->next == NULL)
4963 : 13312 : a->name = "%VAL";
4964 : : }
4965 : :
4966 : 13312 : if (se->ss)
4967 : : {
4968 : 10658 : if (se->ss->info->useflags)
4969 : : {
4970 : 5193 : if (!dim_present || !gfc_inline_intrinsic_function_p (expr))
4971 : : {
4972 : : /* The code generating and initializing the result array has been
4973 : : generated already before the scalarization loop, either with a
4974 : : library function call or with inline code; now we can just use
4975 : : the result. */
4976 : 3321 : gfc_conv_tmp_array_ref (se);
4977 : 9478 : return;
4978 : : }
4979 : : }
4980 : 5465 : else if (!gfc_inline_intrinsic_function_p (expr))
4981 : : {
4982 : 2760 : gfc_conv_intrinsic_funcall (se, expr);
4983 : 2760 : return;
4984 : : }
4985 : : }
4986 : :
4987 : 7231 : arrayexpr = actual->expr;
4988 : :
4989 : : /* Special case for character maxloc. Remove unneeded actual
4990 : : arguments, then call a library function. */
4991 : :
4992 : 7231 : if (arrayexpr->ts.type == BT_CHARACTER)
4993 : : {
4994 : 76 : gcc_assert (expr->rank == 0);
4995 : :
4996 : 76 : gfc_actual_arglist *a = actual;
4997 : 76 : strip_kind_from_actual (a);
4998 : 532 : while (a)
4999 : : {
5000 : 380 : if (a->name && strcmp (a->name, "dim") == 0)
5001 : : {
5002 : 76 : gfc_free_expr (a->expr);
5003 : 76 : a->expr = NULL;
5004 : : }
5005 : 380 : a = a->next;
5006 : : }
5007 : 76 : gfc_conv_intrinsic_funcall (se, expr);
5008 : 76 : return;
5009 : : }
5010 : :
5011 : 7155 : type = gfc_typenode_for_spec (&expr->ts);
5012 : :
5013 : 7155 : if (expr->rank > 0 && !dim_present)
5014 : : {
5015 : 2705 : gfc_array_spec as;
5016 : 2705 : memset (&as, 0, sizeof (as));
5017 : :
5018 : 2705 : as.rank = 1;
5019 : 2705 : as.lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
5020 : : &arrayexpr->where,
5021 : : HOST_WIDE_INT_1);
5022 : 5410 : as.upper[0] = gfc_get_int_expr (gfc_index_integer_kind,
5023 : : &arrayexpr->where,
5024 : 2705 : arrayexpr->rank);
5025 : :
5026 : 2705 : tree array = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
5027 : :
5028 : 2705 : result_var = gfc_create_var (array, "loc_result");
5029 : : }
5030 : :
5031 : 5283 : const int reduction_dimensions = dim_present ? 1 : arrayexpr->rank;
5032 : :
5033 : : /* Initialize the result. */
5034 : 16009 : for (int i = 0; i < reduction_dimensions; i++)
5035 : : {
5036 : 8854 : pos[i] = gfc_create_var (gfc_array_index_type,
5037 : : gfc_get_string ("pos%d", i));
5038 : 8854 : offset[i] = gfc_create_var (gfc_array_index_type,
5039 : : gfc_get_string ("offset%d", i));
5040 : 8854 : idx[i] = gfc_create_var (gfc_array_index_type,
5041 : : gfc_get_string ("idx%d", i));
5042 : : }
5043 : :
5044 : 7155 : maskexpr = mask_arg->expr;
5045 : 5038 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5046 : 3849 : && maskexpr->symtree->n.sym->attr.dummy
5047 : 11004 : && maskexpr->symtree->n.sym->attr.optional;
5048 : 7155 : backexpr = back_arg->expr;
5049 : :
5050 : 12438 : gfc_init_se (&backse, nested_loop ? se : nullptr);
5051 : 7155 : if (backexpr == nullptr)
5052 : 0 : back = logical_false_node;
5053 : 7155 : else if (maybe_absent_optional_variable (backexpr))
5054 : : {
5055 : : /* This should have been checked already by
5056 : : maybe_absent_optional_variable. */
5057 : 184 : gcc_checking_assert (backexpr->expr_type == EXPR_VARIABLE);
5058 : :
5059 : 184 : gfc_conv_expr (&backse, backexpr);
5060 : 184 : tree present = gfc_conv_expr_present (backexpr->symtree->n.sym, false);
5061 : 184 : back = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5062 : : logical_type_node, present, backse.expr);
5063 : : }
5064 : : else
5065 : : {
5066 : 6971 : gfc_conv_expr (&backse, backexpr);
5067 : 6971 : back = backse.expr;
5068 : : }
5069 : 7155 : gfc_add_block_to_block (&se->pre, &backse.pre);
5070 : 7155 : back = gfc_evaluate_now_loc (input_location, back, &se->pre);
5071 : 7155 : gfc_add_block_to_block (&se->pre, &backse.post);
5072 : :
5073 : 7155 : if (nested_loop)
5074 : : {
5075 : 1872 : gfc_init_se (&nested_se, se);
5076 : 1872 : base_se = &nested_se;
5077 : : }
5078 : : else
5079 : : {
5080 : : /* Walk the arguments. */
5081 : 5283 : arrayss = gfc_walk_expr (arrayexpr);
5082 : 5283 : gcc_assert (arrayss != gfc_ss_terminator);
5083 : :
5084 : 5283 : if (maskexpr && maskexpr->rank != 0)
5085 : : {
5086 : 2268 : maskss = gfc_walk_expr (maskexpr);
5087 : 2268 : gcc_assert (maskss != gfc_ss_terminator);
5088 : : }
5089 : :
5090 : : base_se = nullptr;
5091 : : }
5092 : :
5093 : 13199 : nonempty = nullptr;
5094 : 5660 : if (!(maskexpr && maskexpr->rank > 0))
5095 : : {
5096 : 4021 : mpz_t asize;
5097 : 4021 : bool reduction_size_known;
5098 : :
5099 : 4021 : if (dim_present)
5100 : : {
5101 : 2552 : int reduction_dim;
5102 : 2552 : if (dim_arg->expr->expr_type == EXPR_CONSTANT)
5103 : 2550 : reduction_dim = mpz_get_si (dim_arg->expr->value.integer) - 1;
5104 : 2 : else if (arrayexpr->rank == 1)
5105 : : reduction_dim = 0;
5106 : : else
5107 : 0 : gcc_unreachable ();
5108 : 2552 : reduction_size_known = gfc_array_dimen_size (arrayexpr, reduction_dim,
5109 : : &asize);
5110 : : }
5111 : : else
5112 : 1469 : reduction_size_known = gfc_array_size (arrayexpr, &asize);
5113 : :
5114 : 4021 : if (reduction_size_known)
5115 : : {
5116 : 2426 : nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
5117 : 2426 : mpz_clear (asize);
5118 : 2426 : nonempty = fold_build2_loc (input_location, GT_EXPR,
5119 : : logical_type_node, nonempty,
5120 : : gfc_index_zero_node);
5121 : : }
5122 : 4021 : maskss = NULL;
5123 : : }
5124 : :
5125 : 7155 : limit = gfc_create_var (gfc_typenode_for_spec (&arrayexpr->ts), "limit");
5126 : 7155 : switch (arrayexpr->ts.type)
5127 : : {
5128 : 2986 : case BT_REAL:
5129 : 2986 : tmp = gfc_build_inf_or_huge (TREE_TYPE (limit), arrayexpr->ts.kind);
5130 : 2986 : break;
5131 : :
5132 : 4145 : case BT_INTEGER:
5133 : 4145 : n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5134 : 4145 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
5135 : : arrayexpr->ts.kind);
5136 : 4145 : break;
5137 : :
5138 : 24 : case BT_UNSIGNED:
5139 : : /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
5140 : 24 : if (op == GT_EXPR)
5141 : : {
5142 : 12 : tmp = gfc_get_unsigned_type (arrayexpr->ts.kind);
5143 : 12 : tmp = build_int_cst (tmp, 0);
5144 : : }
5145 : : else
5146 : : {
5147 : 12 : n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5148 : 12 : tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
5149 : : expr->ts.kind);
5150 : : }
5151 : : break;
5152 : :
5153 : 0 : default:
5154 : 0 : gcc_unreachable ();
5155 : : }
5156 : :
5157 : : /* We start with the most negative possible value for MAXLOC, and the most
5158 : : positive possible value for MINLOC. The most negative possible value is
5159 : : -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
5160 : : possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
5161 : : with above. */
5162 : 7155 : if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
5163 : 3296 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
5164 : 3296 : if (op == GT_EXPR && arrayexpr->ts.type == BT_INTEGER)
5165 : 1942 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp), tmp,
5166 : 1942 : build_int_cst (TREE_TYPE (tmp), 1));
5167 : :
5168 : 7155 : gfc_add_modify (&se->pre, limit, tmp);
5169 : :
5170 : : /* If we are in a case where we generate two sets of loops, the second one
5171 : : should continue where the first stopped instead of restarting from the
5172 : : beginning. So nested loops in the second set should have a partial range
5173 : : on the first iteration, but they should start from the beginning and span
5174 : : their full range on the following iterations. So we use conditionals in
5175 : : the loops lower bounds, and use the following variable in those
5176 : : conditionals to decide whether to use the original loop bound or to use
5177 : : the index at which the loop from the first set stopped. */
5178 : 7155 : tree second_loop_entry = gfc_create_var (logical_type_node,
5179 : : "second_loop_entry");
5180 : 7155 : gfc_add_modify (&se->pre, second_loop_entry, logical_false_node);
5181 : :
5182 : 7155 : if (nested_loop)
5183 : : {
5184 : 1872 : ploop = enter_nested_loop (&nested_se);
5185 : 1872 : orig_ss = nested_se.ss;
5186 : 1872 : ploop->temp_dim = 1;
5187 : : }
5188 : : else
5189 : : {
5190 : : /* Initialize the scalarizer. */
5191 : 5283 : gfc_init_loopinfo (&loop);
5192 : :
5193 : : /* We add the mask first because the number of iterations is taken
5194 : : from the last ss, and this breaks if an absent optional argument
5195 : : is used for mask. */
5196 : :
5197 : 5283 : if (maskss)
5198 : 2268 : gfc_add_ss_to_loop (&loop, maskss);
5199 : :
5200 : 5283 : gfc_add_ss_to_loop (&loop, arrayss);
5201 : :
5202 : : /* Initialize the loop. */
5203 : 5283 : gfc_conv_ss_startstride (&loop);
5204 : :
5205 : : /* The code generated can have more than one loop in sequence (see the
5206 : : comment at the function header). This doesn't work well with the
5207 : : scalarizer, which changes arrays' offset when the scalarization loops
5208 : : are generated (see gfc_trans_preloop_setup). Fortunately, we can use
5209 : : the scalarizer temporary code to handle multiple loops. Thus, we set
5210 : : temp_dim here, we call gfc_mark_ss_chain_used with flag=3 later, and
5211 : : we use gfc_trans_scalarized_loop_boundary even later to restore
5212 : : offset. */
5213 : 5283 : loop.temp_dim = loop.dimen;
5214 : 5283 : gfc_conv_loop_setup (&loop, &expr->where);
5215 : :
5216 : 5283 : ploop = &loop;
5217 : : }
5218 : :
5219 : 7155 : gcc_assert (reduction_dimensions == ploop->dimen);
5220 : :
5221 : 7155 : if (nonempty == NULL && !(maskexpr && maskexpr->rank > 0))
5222 : : {
5223 : 1595 : nonempty = logical_true_node;
5224 : :
5225 : 3697 : for (int i = 0; i < ploop->dimen; i++)
5226 : : {
5227 : 2102 : if (!(ploop->from[i] && ploop->to[i]))
5228 : : {
5229 : : nonempty = NULL;
5230 : : break;
5231 : : }
5232 : :
5233 : 2102 : tree tmp = fold_build2_loc (input_location, LE_EXPR,
5234 : : logical_type_node, ploop->from[i],
5235 : : ploop->to[i]);
5236 : :
5237 : 2102 : nonempty = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5238 : : logical_type_node, nonempty, tmp);
5239 : : }
5240 : : }
5241 : :
5242 : 8750 : lab1 = NULL;
5243 : 8750 : lab2 = NULL;
5244 : : /* Initialize the position to zero, following Fortran 2003. We are free
5245 : : to do this because Fortran 95 allows the result of an entirely false
5246 : : mask to be processor dependent. If we know at compile time the array
5247 : : is non-empty and no MASK is used, we can initialize to 1 to simplify
5248 : : the inner loop. */
5249 : 7155 : if (nonempty != NULL && !HONOR_NANS (DECL_MODE (limit)))
5250 : : {
5251 : 2364 : tree init = fold_build3_loc (input_location, COND_EXPR,
5252 : : gfc_array_index_type, nonempty,
5253 : : gfc_index_one_node,
5254 : : gfc_index_zero_node);
5255 : 5278 : for (int i = 0; i < ploop->dimen; i++)
5256 : 2914 : gfc_add_modify (&ploop->pre, pos[i], init);
5257 : : }
5258 : : else
5259 : : {
5260 : 10731 : for (int i = 0; i < ploop->dimen; i++)
5261 : 5940 : gfc_add_modify (&ploop->pre, pos[i], gfc_index_zero_node);
5262 : 4791 : lab1 = gfc_build_label_decl (NULL_TREE);
5263 : 4791 : TREE_USED (lab1) = 1;
5264 : 4791 : lab2 = gfc_build_label_decl (NULL_TREE);
5265 : 4791 : TREE_USED (lab2) = 1;
5266 : : }
5267 : :
5268 : : /* An offset must be added to the loop
5269 : : counter to obtain the required position. */
5270 : 16009 : for (int i = 0; i < ploop->dimen; i++)
5271 : : {
5272 : 8854 : gcc_assert (ploop->from[i]);
5273 : :
5274 : 8854 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5275 : : gfc_index_one_node, ploop->from[i]);
5276 : 8854 : gfc_add_modify (&ploop->pre, offset[i], tmp);
5277 : : }
5278 : :
5279 : 7155 : if (!nested_loop)
5280 : : {
5281 : 7133 : gfc_mark_ss_chain_used (arrayss, lab1 ? 3 : 1);
5282 : 5283 : if (maskss)
5283 : 2268 : gfc_mark_ss_chain_used (maskss, lab1 ? 3 : 1);
5284 : : }
5285 : :
5286 : : /* Generate the loop body. */
5287 : 7155 : gfc_start_scalarized_body (ploop, &body);
5288 : :
5289 : : /* If we have a mask, only check this element if the mask is set. */
5290 : 7155 : if (maskexpr && maskexpr->rank > 0)
5291 : : {
5292 : 3134 : gfc_init_se (&maskse, base_se);
5293 : 3134 : gfc_copy_loopinfo_to_se (&maskse, ploop);
5294 : 3134 : if (!nested_loop)
5295 : 2268 : maskse.ss = maskss;
5296 : 3134 : gfc_conv_expr_val (&maskse, maskexpr);
5297 : 3134 : gfc_add_block_to_block (&body, &maskse.pre);
5298 : :
5299 : 3134 : gfc_start_block (&block);
5300 : : }
5301 : : else
5302 : 4021 : gfc_init_block (&block);
5303 : :
5304 : : /* Compare with the current limit. */
5305 : 7155 : gfc_init_se (&arrayse, base_se);
5306 : 7155 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
5307 : 7155 : if (!nested_loop)
5308 : 5283 : arrayse.ss = arrayss;
5309 : 7155 : gfc_conv_expr_val (&arrayse, arrayexpr);
5310 : 7155 : gfc_add_block_to_block (&block, &arrayse.pre);
5311 : :
5312 : : /* We do the following if this is a more extreme value. */
5313 : 7155 : gfc_start_block (&ifblock);
5314 : :
5315 : : /* Assign the value to the limit... */
5316 : 7155 : gfc_add_modify (&ifblock, limit, arrayse.expr);
5317 : :
5318 : 7155 : if (nonempty == NULL && HONOR_NANS (DECL_MODE (limit)))
5319 : : {
5320 : 1329 : stmtblock_t ifblock2;
5321 : 1329 : tree ifbody2;
5322 : :
5323 : 1329 : gfc_start_block (&ifblock2);
5324 : 2959 : for (int i = 0; i < ploop->dimen; i++)
5325 : : {
5326 : 1630 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5327 : : ploop->loopvar[i], offset[i]);
5328 : 1630 : gfc_add_modify (&ifblock2, pos[i], tmp);
5329 : : }
5330 : 1329 : ifbody2 = gfc_finish_block (&ifblock2);
5331 : :
5332 : 1329 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5333 : : pos[0], gfc_index_zero_node);
5334 : 1329 : tmp = build3_v (COND_EXPR, cond, ifbody2,
5335 : : build_empty_stmt (input_location));
5336 : 1329 : gfc_add_expr_to_block (&block, tmp);
5337 : : }
5338 : :
5339 : 16009 : for (int i = 0; i < ploop->dimen; i++)
5340 : : {
5341 : 8854 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5342 : : ploop->loopvar[i], offset[i]);
5343 : 8854 : gfc_add_modify (&ifblock, pos[i], tmp);
5344 : 8854 : gfc_add_modify (&ifblock, idx[i], ploop->loopvar[i]);
5345 : : }
5346 : :
5347 : 7155 : gfc_add_modify (&ifblock, second_loop_entry, logical_true_node);
5348 : :
5349 : 7155 : if (lab1)
5350 : 4791 : gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab1));
5351 : :
5352 : 7155 : ifbody = gfc_finish_block (&ifblock);
5353 : :
5354 : 7155 : if (!lab1 || HONOR_NANS (DECL_MODE (limit)))
5355 : : {
5356 : 5350 : if (lab1)
5357 : 4630 : cond = fold_build2_loc (input_location,
5358 : : op == GT_EXPR ? GE_EXPR : LE_EXPR,
5359 : : logical_type_node, arrayse.expr, limit);
5360 : : else
5361 : : {
5362 : 2364 : tree ifbody2, elsebody2;
5363 : :
5364 : : /* We switch to > or >= depending on the value of the BACK argument. */
5365 : 2364 : cond = gfc_create_var (logical_type_node, "cond");
5366 : :
5367 : 2364 : gfc_start_block (&ifblock);
5368 : 3585 : b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5369 : : logical_type_node, arrayse.expr, limit);
5370 : :
5371 : 2364 : gfc_add_modify (&ifblock, cond, b_if);
5372 : 2364 : ifbody2 = gfc_finish_block (&ifblock);
5373 : :
5374 : 2364 : gfc_start_block (&elseblock);
5375 : 2364 : b_else = fold_build2_loc (input_location, op, logical_type_node,
5376 : : arrayse.expr, limit);
5377 : :
5378 : 2364 : gfc_add_modify (&elseblock, cond, b_else);
5379 : 2364 : elsebody2 = gfc_finish_block (&elseblock);
5380 : :
5381 : 2364 : tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5382 : : back, ifbody2, elsebody2);
5383 : :
5384 : 2364 : gfc_add_expr_to_block (&block, tmp);
5385 : : }
5386 : :
5387 : 5350 : cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5388 : 5350 : ifbody = build3_v (COND_EXPR, cond, ifbody,
5389 : : build_empty_stmt (input_location));
5390 : : }
5391 : 7155 : gfc_add_expr_to_block (&block, ifbody);
5392 : :
5393 : 7155 : if (maskexpr && maskexpr->rank > 0)
5394 : : {
5395 : : /* We enclose the above in if (mask) {...}. If the mask is an
5396 : : optional argument, generate IF (.NOT. PRESENT(MASK)
5397 : : .OR. MASK(I)). */
5398 : :
5399 : 3134 : tree ifmask;
5400 : 3134 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5401 : 3134 : tmp = gfc_finish_block (&block);
5402 : 3134 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5403 : : build_empty_stmt (input_location));
5404 : 3134 : }
5405 : : else
5406 : 4021 : tmp = gfc_finish_block (&block);
5407 : 7155 : gfc_add_expr_to_block (&body, tmp);
5408 : :
5409 : 7155 : if (lab1)
5410 : : {
5411 : 10731 : for (int i = 0; i < ploop->dimen; i++)
5412 : 5940 : ploop->from[i] = fold_build3_loc (input_location, COND_EXPR,
5413 : 5940 : TREE_TYPE (ploop->from[i]),
5414 : : second_loop_entry, idx[i],
5415 : : ploop->from[i]);
5416 : :
5417 : 4791 : gfc_trans_scalarized_loop_boundary (ploop, &body);
5418 : :
5419 : 4791 : if (nested_loop)
5420 : : {
5421 : : /* The first loop already advanced the parent se'ss chain, so clear
5422 : : the parent now to avoid doing it a second time, making the chain
5423 : : out of sync. */
5424 : 1358 : nested_se.parent = nullptr;
5425 : 1358 : nested_se.ss = orig_ss;
5426 : : }
5427 : :
5428 : 4791 : stmtblock_t * const outer_block = &ploop->code[ploop->dimen - 1];
5429 : :
5430 : 4791 : if (HONOR_NANS (DECL_MODE (limit)))
5431 : : {
5432 : 2986 : if (nonempty != NULL)
5433 : : {
5434 : 1657 : stmtblock_t init_block;
5435 : 1657 : gfc_init_block (&init_block);
5436 : :
5437 : 3693 : for (int i = 0; i < ploop->dimen; i++)
5438 : 2036 : gfc_add_modify (&init_block, pos[i], gfc_index_one_node);
5439 : :
5440 : 1657 : tree ifbody = gfc_finish_block (&init_block);
5441 : 1657 : tmp = build3_v (COND_EXPR, nonempty, ifbody,
5442 : : build_empty_stmt (input_location));
5443 : 1657 : gfc_add_expr_to_block (outer_block, tmp);
5444 : : }
5445 : : }
5446 : :
5447 : 4791 : gfc_add_expr_to_block (outer_block, build1_v (GOTO_EXPR, lab2));
5448 : 4791 : gfc_add_expr_to_block (outer_block, build1_v (LABEL_EXPR, lab1));
5449 : :
5450 : : /* If we have a mask, only check this element if the mask is set. */
5451 : 4791 : if (maskexpr && maskexpr->rank > 0)
5452 : : {
5453 : 3134 : gfc_init_se (&maskse, base_se);
5454 : 3134 : gfc_copy_loopinfo_to_se (&maskse, ploop);
5455 : 3134 : if (!nested_loop)
5456 : 2268 : maskse.ss = maskss;
5457 : 3134 : gfc_conv_expr_val (&maskse, maskexpr);
5458 : 3134 : gfc_add_block_to_block (&body, &maskse.pre);
5459 : :
5460 : 3134 : gfc_start_block (&block);
5461 : : }
5462 : : else
5463 : 1657 : gfc_init_block (&block);
5464 : :
5465 : : /* Compare with the current limit. */
5466 : 4791 : gfc_init_se (&arrayse, base_se);
5467 : 4791 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
5468 : 4791 : if (!nested_loop)
5469 : 3433 : arrayse.ss = arrayss;
5470 : 4791 : gfc_conv_expr_val (&arrayse, arrayexpr);
5471 : 4791 : gfc_add_block_to_block (&block, &arrayse.pre);
5472 : :
5473 : : /* We do the following if this is a more extreme value. */
5474 : 4791 : gfc_start_block (&ifblock);
5475 : :
5476 : : /* Assign the value to the limit... */
5477 : 4791 : gfc_add_modify (&ifblock, limit, arrayse.expr);
5478 : :
5479 : 10731 : for (int i = 0; i < ploop->dimen; i++)
5480 : : {
5481 : 5940 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5482 : : ploop->loopvar[i], offset[i]);
5483 : 5940 : gfc_add_modify (&ifblock, pos[i], tmp);
5484 : : }
5485 : :
5486 : 4791 : ifbody = gfc_finish_block (&ifblock);
5487 : :
5488 : : /* We switch to > or >= depending on the value of the BACK argument. */
5489 : 4791 : {
5490 : 4791 : tree ifbody2, elsebody2;
5491 : :
5492 : 4791 : cond = gfc_create_var (logical_type_node, "cond");
5493 : :
5494 : 4791 : gfc_start_block (&ifblock);
5495 : 7429 : b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5496 : : logical_type_node, arrayse.expr, limit);
5497 : :
5498 : 4791 : gfc_add_modify (&ifblock, cond, b_if);
5499 : 4791 : ifbody2 = gfc_finish_block (&ifblock);
5500 : :
5501 : 4791 : gfc_start_block (&elseblock);
5502 : 4791 : b_else = fold_build2_loc (input_location, op, logical_type_node,
5503 : : arrayse.expr, limit);
5504 : :
5505 : 4791 : gfc_add_modify (&elseblock, cond, b_else);
5506 : 4791 : elsebody2 = gfc_finish_block (&elseblock);
5507 : :
5508 : 4791 : tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5509 : : back, ifbody2, elsebody2);
5510 : : }
5511 : :
5512 : 4791 : gfc_add_expr_to_block (&block, tmp);
5513 : 4791 : cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5514 : 4791 : tmp = build3_v (COND_EXPR, cond, ifbody,
5515 : : build_empty_stmt (input_location));
5516 : :
5517 : 4791 : gfc_add_expr_to_block (&block, tmp);
5518 : :
5519 : 4791 : if (maskexpr && maskexpr->rank > 0)
5520 : : {
5521 : : /* We enclose the above in if (mask) {...}. If the mask is
5522 : : an optional argument, generate IF (.NOT. PRESENT(MASK)
5523 : : .OR. MASK(I)).*/
5524 : :
5525 : 3134 : tree ifmask;
5526 : 3134 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5527 : 3134 : tmp = gfc_finish_block (&block);
5528 : 3134 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5529 : : build_empty_stmt (input_location));
5530 : 3134 : }
5531 : : else
5532 : 1657 : tmp = gfc_finish_block (&block);
5533 : :
5534 : 4791 : gfc_add_expr_to_block (&body, tmp);
5535 : 4791 : gfc_add_modify (&body, second_loop_entry, logical_false_node);
5536 : : }
5537 : :
5538 : 7155 : gfc_trans_scalarizing_loops (ploop, &body);
5539 : :
5540 : 7155 : if (lab2)
5541 : 4791 : gfc_add_expr_to_block (&ploop->pre, build1_v (LABEL_EXPR, lab2));
5542 : :
5543 : : /* For a scalar mask, enclose the loop in an if statement. */
5544 : 7155 : if (maskexpr && maskexpr->rank == 0)
5545 : : {
5546 : 1904 : tree ifmask;
5547 : :
5548 : 1904 : gfc_init_se (&maskse, nested_loop ? se : nullptr);
5549 : 1904 : gfc_conv_expr_val (&maskse, maskexpr);
5550 : 1904 : gfc_add_block_to_block (&se->pre, &maskse.pre);
5551 : 1904 : gfc_init_block (&block);
5552 : 1904 : gfc_add_block_to_block (&block, &ploop->pre);
5553 : 1904 : gfc_add_block_to_block (&block, &ploop->post);
5554 : 1904 : tmp = gfc_finish_block (&block);
5555 : :
5556 : : /* For the else part of the scalar mask, just initialize
5557 : : the pos variable the same way as above. */
5558 : :
5559 : 1904 : gfc_init_block (&elseblock);
5560 : 4100 : for (int i = 0; i < ploop->dimen; i++)
5561 : 2196 : gfc_add_modify (&elseblock, pos[i], gfc_index_zero_node);
5562 : 1904 : elsetmp = gfc_finish_block (&elseblock);
5563 : 1904 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5564 : 1904 : tmp = build3_v (COND_EXPR, ifmask, tmp, elsetmp);
5565 : 1904 : gfc_add_expr_to_block (&block, tmp);
5566 : 1904 : gfc_add_block_to_block (&se->pre, &block);
5567 : 1904 : }
5568 : : else
5569 : : {
5570 : 5251 : gfc_add_block_to_block (&se->pre, &ploop->pre);
5571 : 5251 : gfc_add_block_to_block (&se->pre, &ploop->post);
5572 : : }
5573 : :
5574 : 7155 : if (!nested_loop)
5575 : 5283 : gfc_cleanup_loop (&loop);
5576 : :
5577 : 7155 : if (!dim_present)
5578 : : {
5579 : 7109 : for (int i = 0; i < arrayexpr->rank; i++)
5580 : : {
5581 : 4404 : tree res_idx = build_int_cst (gfc_array_index_type, i);
5582 : 4404 : tree res_arr_ref = gfc_build_array_ref (result_var, res_idx,
5583 : : NULL_TREE, true);
5584 : :
5585 : 4404 : tree value = convert (type, pos[i]);
5586 : 4404 : gfc_add_modify (&se->pre, res_arr_ref, value);
5587 : : }
5588 : :
5589 : 2705 : se->expr = result_var;
5590 : : }
5591 : : else
5592 : 4450 : se->expr = convert (type, pos[0]);
5593 : : }
5594 : :
5595 : : /* Emit code for findloc. */
5596 : :
5597 : : static void
5598 : 1296 : gfc_conv_intrinsic_findloc (gfc_se *se, gfc_expr *expr)
5599 : : {
5600 : 1296 : gfc_actual_arglist *array_arg, *value_arg, *dim_arg, *mask_arg,
5601 : : *kind_arg, *back_arg;
5602 : 1296 : gfc_expr *value_expr;
5603 : 1296 : int ikind;
5604 : 1296 : tree resvar;
5605 : 1296 : stmtblock_t block;
5606 : 1296 : stmtblock_t body;
5607 : 1296 : stmtblock_t loopblock;
5608 : 1296 : tree type;
5609 : 1296 : tree tmp;
5610 : 1296 : tree found;
5611 : 1296 : tree forward_branch = NULL_TREE;
5612 : 1296 : tree back_branch;
5613 : 1296 : gfc_loopinfo loop;
5614 : 1296 : gfc_ss *arrayss;
5615 : 1296 : gfc_ss *maskss;
5616 : 1296 : gfc_se arrayse;
5617 : 1296 : gfc_se valuese;
5618 : 1296 : gfc_se maskse;
5619 : 1296 : gfc_se backse;
5620 : 1296 : tree exit_label;
5621 : 1296 : gfc_expr *maskexpr;
5622 : 1296 : tree offset;
5623 : 1296 : int i;
5624 : 1296 : bool optional_mask;
5625 : :
5626 : 1296 : array_arg = expr->value.function.actual;
5627 : 1296 : value_arg = array_arg->next;
5628 : 1296 : dim_arg = value_arg->next;
5629 : 1296 : mask_arg = dim_arg->next;
5630 : 1296 : kind_arg = mask_arg->next;
5631 : 1296 : back_arg = kind_arg->next;
5632 : :
5633 : : /* Remove kind and set ikind. */
5634 : 1296 : if (kind_arg->expr)
5635 : : {
5636 : 0 : ikind = mpz_get_si (kind_arg->expr->value.integer);
5637 : 0 : gfc_free_expr (kind_arg->expr);
5638 : 0 : kind_arg->expr = NULL;
5639 : : }
5640 : : else
5641 : 1296 : ikind = gfc_default_integer_kind;
5642 : :
5643 : 1296 : value_expr = value_arg->expr;
5644 : :
5645 : : /* Unless it's a string, pass VALUE by value. */
5646 : 1296 : if (value_expr->ts.type != BT_CHARACTER)
5647 : 732 : value_arg->name = "%VAL";
5648 : :
5649 : : /* Pass BACK argument by value. */
5650 : 1296 : back_arg->name = "%VAL";
5651 : :
5652 : : /* Call the library if we have a character function or if
5653 : : rank > 0. */
5654 : 1296 : if (se->ss || array_arg->expr->ts.type == BT_CHARACTER)
5655 : : {
5656 : 1164 : se->ignore_optional = 1;
5657 : 1164 : if (expr->rank == 0)
5658 : : {
5659 : : /* Remove dim argument. */
5660 : 48 : gfc_free_expr (dim_arg->expr);
5661 : 48 : dim_arg->expr = NULL;
5662 : : }
5663 : 1164 : gfc_conv_intrinsic_funcall (se, expr);
5664 : 1164 : return;
5665 : : }
5666 : :
5667 : 132 : type = gfc_get_int_type (ikind);
5668 : :
5669 : : /* Initialize the result. */
5670 : 132 : resvar = gfc_create_var (gfc_array_index_type, "pos");
5671 : 132 : gfc_add_modify (&se->pre, resvar, build_int_cst (gfc_array_index_type, 0));
5672 : 132 : offset = gfc_create_var (gfc_array_index_type, "offset");
5673 : :
5674 : 132 : maskexpr = mask_arg->expr;
5675 : 72 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5676 : 60 : && maskexpr->symtree->n.sym->attr.dummy
5677 : 192 : && maskexpr->symtree->n.sym->attr.optional;
5678 : :
5679 : : /* Generate two loops, one for BACK=.true. and one for BACK=.false. */
5680 : :
5681 : 396 : for (i = 0 ; i < 2; i++)
5682 : : {
5683 : : /* Walk the arguments. */
5684 : 264 : arrayss = gfc_walk_expr (array_arg->expr);
5685 : 264 : gcc_assert (arrayss != gfc_ss_terminator);
5686 : :
5687 : 264 : if (maskexpr && maskexpr->rank != 0)
5688 : : {
5689 : 84 : maskss = gfc_walk_expr (maskexpr);
5690 : 84 : gcc_assert (maskss != gfc_ss_terminator);
5691 : : }
5692 : : else
5693 : : maskss = NULL;
5694 : :
5695 : : /* Initialize the scalarizer. */
5696 : 264 : gfc_init_loopinfo (&loop);
5697 : 264 : exit_label = gfc_build_label_decl (NULL_TREE);
5698 : 264 : TREE_USED (exit_label) = 1;
5699 : :
5700 : : /* We add the mask first because the number of iterations is
5701 : : taken from the last ss, and this breaks if an absent
5702 : : optional argument is used for mask. */
5703 : :
5704 : 264 : if (maskss)
5705 : 84 : gfc_add_ss_to_loop (&loop, maskss);
5706 : 264 : gfc_add_ss_to_loop (&loop, arrayss);
5707 : :
5708 : : /* Initialize the loop. */
5709 : 264 : gfc_conv_ss_startstride (&loop);
5710 : 264 : gfc_conv_loop_setup (&loop, &expr->where);
5711 : :
5712 : : /* Calculate the offset. */
5713 : 264 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5714 : : gfc_index_one_node, loop.from[0]);
5715 : 264 : gfc_add_modify (&loop.pre, offset, tmp);
5716 : :
5717 : 264 : gfc_mark_ss_chain_used (arrayss, 1);
5718 : 264 : if (maskss)
5719 : 84 : gfc_mark_ss_chain_used (maskss, 1);
5720 : :
5721 : : /* The first loop is for BACK=.true. */
5722 : 264 : if (i == 0)
5723 : 132 : loop.reverse[0] = GFC_REVERSE_SET;
5724 : :
5725 : : /* Generate the loop body. */
5726 : 264 : gfc_start_scalarized_body (&loop, &body);
5727 : :
5728 : : /* If we have an array mask, only add the element if it is
5729 : : set. */
5730 : 264 : if (maskss)
5731 : : {
5732 : 84 : gfc_init_se (&maskse, NULL);
5733 : 84 : gfc_copy_loopinfo_to_se (&maskse, &loop);
5734 : 84 : maskse.ss = maskss;
5735 : 84 : gfc_conv_expr_val (&maskse, maskexpr);
5736 : 84 : gfc_add_block_to_block (&body, &maskse.pre);
5737 : : }
5738 : :
5739 : : /* If the condition matches then set the return value. */
5740 : 264 : gfc_start_block (&block);
5741 : :
5742 : : /* Add the offset. */
5743 : 264 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5744 : 264 : TREE_TYPE (resvar),
5745 : : loop.loopvar[0], offset);
5746 : 264 : gfc_add_modify (&block, resvar, tmp);
5747 : : /* And break out of the loop. */
5748 : 264 : tmp = build1_v (GOTO_EXPR, exit_label);
5749 : 264 : gfc_add_expr_to_block (&block, tmp);
5750 : :
5751 : 264 : found = gfc_finish_block (&block);
5752 : :
5753 : : /* Check this element. */
5754 : 264 : gfc_init_se (&arrayse, NULL);
5755 : 264 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
5756 : 264 : arrayse.ss = arrayss;
5757 : 264 : gfc_conv_expr_val (&arrayse, array_arg->expr);
5758 : 264 : gfc_add_block_to_block (&body, &arrayse.pre);
5759 : :
5760 : 264 : gfc_init_se (&valuese, NULL);
5761 : 264 : gfc_conv_expr_val (&valuese, value_arg->expr);
5762 : 264 : gfc_add_block_to_block (&body, &valuese.pre);
5763 : :
5764 : 264 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5765 : : arrayse.expr, valuese.expr);
5766 : :
5767 : 264 : tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
5768 : 264 : if (maskss)
5769 : : {
5770 : : /* We enclose the above in if (mask) {...}. If the mask is
5771 : : an optional argument, generate IF (.NOT. PRESENT(MASK)
5772 : : .OR. MASK(I)). */
5773 : :
5774 : 84 : tree ifmask;
5775 : 84 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5776 : 84 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5777 : : build_empty_stmt (input_location));
5778 : : }
5779 : :
5780 : 264 : gfc_add_expr_to_block (&body, tmp);
5781 : 264 : gfc_add_block_to_block (&body, &arrayse.post);
5782 : :
5783 : 264 : gfc_trans_scalarizing_loops (&loop, &body);
5784 : :
5785 : : /* Add the exit label. */
5786 : 264 : tmp = build1_v (LABEL_EXPR, exit_label);
5787 : 264 : gfc_add_expr_to_block (&loop.pre, tmp);
5788 : 264 : gfc_start_block (&loopblock);
5789 : 264 : gfc_add_block_to_block (&loopblock, &loop.pre);
5790 : 264 : gfc_add_block_to_block (&loopblock, &loop.post);
5791 : 264 : if (i == 0)
5792 : 132 : forward_branch = gfc_finish_block (&loopblock);
5793 : : else
5794 : 132 : back_branch = gfc_finish_block (&loopblock);
5795 : :
5796 : 264 : gfc_cleanup_loop (&loop);
5797 : : }
5798 : :
5799 : : /* Enclose the two loops in an IF statement. */
5800 : :
5801 : 132 : gfc_init_se (&backse, NULL);
5802 : 132 : gfc_conv_expr_val (&backse, back_arg->expr);
5803 : 132 : gfc_add_block_to_block (&se->pre, &backse.pre);
5804 : 132 : tmp = build3_v (COND_EXPR, backse.expr, forward_branch, back_branch);
5805 : :
5806 : : /* For a scalar mask, enclose the loop in an if statement. */
5807 : 132 : if (maskexpr && maskss == NULL)
5808 : : {
5809 : 30 : tree ifmask;
5810 : 30 : tree if_stmt;
5811 : :
5812 : 30 : gfc_init_se (&maskse, NULL);
5813 : 30 : gfc_conv_expr_val (&maskse, maskexpr);
5814 : 30 : gfc_init_block (&block);
5815 : 30 : gfc_add_expr_to_block (&block, maskse.expr);
5816 : 30 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5817 : 30 : if_stmt = build3_v (COND_EXPR, ifmask, tmp,
5818 : : build_empty_stmt (input_location));
5819 : 30 : gfc_add_expr_to_block (&block, if_stmt);
5820 : 30 : tmp = gfc_finish_block (&block);
5821 : : }
5822 : :
5823 : 132 : gfc_add_expr_to_block (&se->pre, tmp);
5824 : 132 : se->expr = convert (type, resvar);
5825 : :
5826 : : }
5827 : :
5828 : : /* Emit code for minval or maxval intrinsic. There are many different cases
5829 : : we need to handle. For performance reasons we sometimes create two
5830 : : loops instead of one, where the second one is much simpler.
5831 : : Examples for minval intrinsic:
5832 : : 1) Result is an array, a call is generated
5833 : : 2) Array mask is used and NaNs need to be supported, rank 1:
5834 : : limit = Infinity;
5835 : : nonempty = false;
5836 : : S = from;
5837 : : while (S <= to) {
5838 : : if (mask[S]) {
5839 : : nonempty = true;
5840 : : if (a[S] <= limit) {
5841 : : limit = a[S];
5842 : : S++;
5843 : : goto lab;
5844 : : }
5845 : : else
5846 : : S++;
5847 : : }
5848 : : }
5849 : : limit = nonempty ? NaN : huge (limit);
5850 : : lab:
5851 : : while (S <= to) { if(mask[S]) limit = min (a[S], limit); S++; }
5852 : : 3) NaNs need to be supported, but it is known at compile time or cheaply
5853 : : at runtime whether array is nonempty or not, rank 1:
5854 : : limit = Infinity;
5855 : : S = from;
5856 : : while (S <= to) {
5857 : : if (a[S] <= limit) {
5858 : : limit = a[S];
5859 : : S++;
5860 : : goto lab;
5861 : : }
5862 : : else
5863 : : S++;
5864 : : }
5865 : : limit = (from <= to) ? NaN : huge (limit);
5866 : : lab:
5867 : : while (S <= to) { limit = min (a[S], limit); S++; }
5868 : : 4) Array mask is used and NaNs need to be supported, rank > 1:
5869 : : limit = Infinity;
5870 : : nonempty = false;
5871 : : fast = false;
5872 : : S1 = from1;
5873 : : while (S1 <= to1) {
5874 : : S2 = from2;
5875 : : while (S2 <= to2) {
5876 : : if (mask[S1][S2]) {
5877 : : if (fast) limit = min (a[S1][S2], limit);
5878 : : else {
5879 : : nonempty = true;
5880 : : if (a[S1][S2] <= limit) {
5881 : : limit = a[S1][S2];
5882 : : fast = true;
5883 : : }
5884 : : }
5885 : : }
5886 : : S2++;
5887 : : }
5888 : : S1++;
5889 : : }
5890 : : if (!fast)
5891 : : limit = nonempty ? NaN : huge (limit);
5892 : : 5) NaNs need to be supported, but it is known at compile time or cheaply
5893 : : at runtime whether array is nonempty or not, rank > 1:
5894 : : limit = Infinity;
5895 : : fast = false;
5896 : : S1 = from1;
5897 : : while (S1 <= to1) {
5898 : : S2 = from2;
5899 : : while (S2 <= to2) {
5900 : : if (fast) limit = min (a[S1][S2], limit);
5901 : : else {
5902 : : if (a[S1][S2] <= limit) {
5903 : : limit = a[S1][S2];
5904 : : fast = true;
5905 : : }
5906 : : }
5907 : : S2++;
5908 : : }
5909 : : S1++;
5910 : : }
5911 : : if (!fast)
5912 : : limit = (nonempty_array) ? NaN : huge (limit);
5913 : : 6) NaNs aren't supported, but infinities are. Array mask is used:
5914 : : limit = Infinity;
5915 : : nonempty = false;
5916 : : S = from;
5917 : : while (S <= to) {
5918 : : if (mask[S]) { nonempty = true; limit = min (a[S], limit); }
5919 : : S++;
5920 : : }
5921 : : limit = nonempty ? limit : huge (limit);
5922 : : 7) Same without array mask:
5923 : : limit = Infinity;
5924 : : S = from;
5925 : : while (S <= to) { limit = min (a[S], limit); S++; }
5926 : : limit = (from <= to) ? limit : huge (limit);
5927 : : 8) Neither NaNs nor infinities are supported (-ffast-math or BT_INTEGER):
5928 : : limit = huge (limit);
5929 : : S = from;
5930 : : while (S <= to) { limit = min (a[S], limit); S++); }
5931 : : (or
5932 : : while (S <= to) { if (mask[S]) limit = min (a[S], limit); S++; }
5933 : : with array mask instead).
5934 : : For 3), 5), 7) and 8), if mask is scalar, this all goes into a conditional,
5935 : : setting limit = huge (limit); in the else branch. */
5936 : :
5937 : : static void
5938 : 2409 : gfc_conv_intrinsic_minmaxval (gfc_se * se, gfc_expr * expr, enum tree_code op)
5939 : : {
5940 : 2409 : tree limit;
5941 : 2409 : tree type;
5942 : 2409 : tree tmp;
5943 : 2409 : tree ifbody;
5944 : 2409 : tree nonempty;
5945 : 2409 : tree nonempty_var;
5946 : 2409 : tree lab;
5947 : 2409 : tree fast;
5948 : 2409 : tree huge_cst = NULL, nan_cst = NULL;
5949 : 2409 : stmtblock_t body;
5950 : 2409 : stmtblock_t block, block2;
5951 : 2409 : gfc_loopinfo loop;
5952 : 2409 : gfc_actual_arglist *actual;
5953 : 2409 : gfc_ss *arrayss;
5954 : 2409 : gfc_ss *maskss;
5955 : 2409 : gfc_se arrayse;
5956 : 2409 : gfc_se maskse;
5957 : 2409 : gfc_expr *arrayexpr;
5958 : 2409 : gfc_expr *maskexpr;
5959 : 2409 : int n;
5960 : 2409 : bool optional_mask;
5961 : :
5962 : 2409 : if (se->ss)
5963 : : {
5964 : 0 : gfc_conv_intrinsic_funcall (se, expr);
5965 : 186 : return;
5966 : : }
5967 : :
5968 : 2409 : actual = expr->value.function.actual;
5969 : 2409 : arrayexpr = actual->expr;
5970 : :
5971 : 2409 : if (arrayexpr->ts.type == BT_CHARACTER)
5972 : : {
5973 : 186 : gfc_actual_arglist *dim = actual->next;
5974 : 186 : if (expr->rank == 0 && dim->expr != 0)
5975 : : {
5976 : 6 : gfc_free_expr (dim->expr);
5977 : 6 : dim->expr = NULL;
5978 : : }
5979 : 186 : gfc_conv_intrinsic_funcall (se, expr);
5980 : 186 : return;
5981 : : }
5982 : :
5983 : 2223 : type = gfc_typenode_for_spec (&expr->ts);
5984 : : /* Initialize the result. */
5985 : 2223 : limit = gfc_create_var (type, "limit");
5986 : 2223 : n = gfc_validate_kind (expr->ts.type, expr->ts.kind, false);
5987 : 2223 : switch (expr->ts.type)
5988 : : {
5989 : 1244 : case BT_REAL:
5990 : 1244 : huge_cst = gfc_conv_mpfr_to_tree (gfc_real_kinds[n].huge,
5991 : : expr->ts.kind, 0);
5992 : 1244 : if (HONOR_INFINITIES (DECL_MODE (limit)))
5993 : : {
5994 : 1240 : REAL_VALUE_TYPE real;
5995 : 1240 : real_inf (&real);
5996 : 1240 : tmp = build_real (type, real);
5997 : : }
5998 : : else
5999 : : tmp = huge_cst;
6000 : 1244 : if (HONOR_NANS (DECL_MODE (limit)))
6001 : 1240 : nan_cst = gfc_build_nan (type, "");
6002 : : break;
6003 : :
6004 : 955 : case BT_INTEGER:
6005 : 955 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge, expr->ts.kind);
6006 : 955 : break;
6007 : :
6008 : 24 : case BT_UNSIGNED:
6009 : : /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
6010 : 24 : if (op == GT_EXPR)
6011 : 12 : tmp = build_int_cst (type, 0);
6012 : : else
6013 : 12 : tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
6014 : : expr->ts.kind);
6015 : : break;
6016 : :
6017 : 0 : default:
6018 : 0 : gcc_unreachable ();
6019 : : }
6020 : :
6021 : : /* We start with the most negative possible value for MAXVAL, and the most
6022 : : positive possible value for MINVAL. The most negative possible value is
6023 : : -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
6024 : : possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
6025 : : with above. */
6026 : 2223 : if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
6027 : : {
6028 : 985 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
6029 : 985 : if (huge_cst)
6030 : 559 : huge_cst = fold_build1_loc (input_location, NEGATE_EXPR,
6031 : 559 : TREE_TYPE (huge_cst), huge_cst);
6032 : : }
6033 : :
6034 : 997 : if (op == GT_EXPR && expr->ts.type == BT_INTEGER)
6035 : 426 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
6036 : 426 : tmp, build_int_cst (type, 1));
6037 : :
6038 : 2223 : gfc_add_modify (&se->pre, limit, tmp);
6039 : :
6040 : : /* Walk the arguments. */
6041 : 2223 : arrayss = gfc_walk_expr (arrayexpr);
6042 : 2223 : gcc_assert (arrayss != gfc_ss_terminator);
6043 : :
6044 : 2223 : actual = actual->next->next;
6045 : 2223 : gcc_assert (actual);
6046 : 2223 : maskexpr = actual->expr;
6047 : 1572 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
6048 : 1560 : && maskexpr->symtree->n.sym->attr.dummy
6049 : 3783 : && maskexpr->symtree->n.sym->attr.optional;
6050 : 1560 : nonempty = NULL;
6051 : 1572 : if (maskexpr && maskexpr->rank != 0)
6052 : : {
6053 : 1026 : maskss = gfc_walk_expr (maskexpr);
6054 : 1026 : gcc_assert (maskss != gfc_ss_terminator);
6055 : : }
6056 : : else
6057 : : {
6058 : 1197 : mpz_t asize;
6059 : 1197 : if (gfc_array_size (arrayexpr, &asize))
6060 : : {
6061 : 672 : nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
6062 : 672 : mpz_clear (asize);
6063 : 672 : nonempty = fold_build2_loc (input_location, GT_EXPR,
6064 : : logical_type_node, nonempty,
6065 : : gfc_index_zero_node);
6066 : : }
6067 : 1197 : maskss = NULL;
6068 : : }
6069 : :
6070 : : /* Initialize the scalarizer. */
6071 : 2223 : gfc_init_loopinfo (&loop);
6072 : :
6073 : : /* We add the mask first because the number of iterations is taken
6074 : : from the last ss, and this breaks if an absent optional argument
6075 : : is used for mask. */
6076 : :
6077 : 2223 : if (maskss)
6078 : 1026 : gfc_add_ss_to_loop (&loop, maskss);
6079 : 2223 : gfc_add_ss_to_loop (&loop, arrayss);
6080 : :
6081 : : /* Initialize the loop. */
6082 : 2223 : gfc_conv_ss_startstride (&loop);
6083 : :
6084 : : /* The code generated can have more than one loop in sequence (see the
6085 : : comment at the function header). This doesn't work well with the
6086 : : scalarizer, which changes arrays' offset when the scalarization loops
6087 : : are generated (see gfc_trans_preloop_setup). Fortunately, {min,max}val
6088 : : are currently inlined in the scalar case only. As there is no dependency
6089 : : to care about in that case, there is no temporary, so that we can use the
6090 : : scalarizer temporary code to handle multiple loops. Thus, we set temp_dim
6091 : : here, we call gfc_mark_ss_chain_used with flag=3 later, and we use
6092 : : gfc_trans_scalarized_loop_boundary even later to restore offset.
6093 : : TODO: this prevents inlining of rank > 0 minmaxval calls, so this
6094 : : should eventually go away. We could either create two loops properly,
6095 : : or find another way to save/restore the array offsets between the two
6096 : : loops (without conflicting with temporary management), or use a single
6097 : : loop minmaxval implementation. See PR 31067. */
6098 : 2223 : loop.temp_dim = loop.dimen;
6099 : 2223 : gfc_conv_loop_setup (&loop, &expr->where);
6100 : :
6101 : 2223 : if (nonempty == NULL && maskss == NULL
6102 : 525 : && loop.dimen == 1 && loop.from[0] && loop.to[0])
6103 : 489 : nonempty = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
6104 : : loop.from[0], loop.to[0]);
6105 : 2223 : nonempty_var = NULL;
6106 : 2223 : if (nonempty == NULL
6107 : 2223 : && (HONOR_INFINITIES (DECL_MODE (limit))
6108 : 480 : || HONOR_NANS (DECL_MODE (limit))))
6109 : : {
6110 : 582 : nonempty_var = gfc_create_var (logical_type_node, "nonempty");
6111 : 582 : gfc_add_modify (&se->pre, nonempty_var, logical_false_node);
6112 : 582 : nonempty = nonempty_var;
6113 : : }
6114 : 2223 : lab = NULL;
6115 : 2223 : fast = NULL;
6116 : 2223 : if (HONOR_NANS (DECL_MODE (limit)))
6117 : : {
6118 : 1240 : if (loop.dimen == 1)
6119 : : {
6120 : 820 : lab = gfc_build_label_decl (NULL_TREE);
6121 : 820 : TREE_USED (lab) = 1;
6122 : : }
6123 : : else
6124 : : {
6125 : 420 : fast = gfc_create_var (logical_type_node, "fast");
6126 : 420 : gfc_add_modify (&se->pre, fast, logical_false_node);
6127 : : }
6128 : : }
6129 : :
6130 : 2223 : gfc_mark_ss_chain_used (arrayss, lab ? 3 : 1);
6131 : 2223 : if (maskss)
6132 : 1704 : gfc_mark_ss_chain_used (maskss, lab ? 3 : 1);
6133 : : /* Generate the loop body. */
6134 : 2223 : gfc_start_scalarized_body (&loop, &body);
6135 : :
6136 : : /* If we have a mask, only add this element if the mask is set. */
6137 : 2223 : if (maskss)
6138 : : {
6139 : 1026 : gfc_init_se (&maskse, NULL);
6140 : 1026 : gfc_copy_loopinfo_to_se (&maskse, &loop);
6141 : 1026 : maskse.ss = maskss;
6142 : 1026 : gfc_conv_expr_val (&maskse, maskexpr);
6143 : 1026 : gfc_add_block_to_block (&body, &maskse.pre);
6144 : :
6145 : 1026 : gfc_start_block (&block);
6146 : : }
6147 : : else
6148 : 1197 : gfc_init_block (&block);
6149 : :
6150 : : /* Compare with the current limit. */
6151 : 2223 : gfc_init_se (&arrayse, NULL);
6152 : 2223 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
6153 : 2223 : arrayse.ss = arrayss;
6154 : 2223 : gfc_conv_expr_val (&arrayse, arrayexpr);
6155 : 2223 : arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6156 : 2223 : gfc_add_block_to_block (&block, &arrayse.pre);
6157 : :
6158 : 2223 : gfc_init_block (&block2);
6159 : :
6160 : 2223 : if (nonempty_var)
6161 : 582 : gfc_add_modify (&block2, nonempty_var, logical_true_node);
6162 : :
6163 : 2223 : if (HONOR_NANS (DECL_MODE (limit)))
6164 : : {
6165 : 1921 : tmp = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
6166 : : logical_type_node, arrayse.expr, limit);
6167 : 1240 : if (lab)
6168 : : {
6169 : 820 : stmtblock_t ifblock;
6170 : 820 : tree inc_loop;
6171 : 820 : inc_loop = fold_build2_loc (input_location, PLUS_EXPR,
6172 : 820 : TREE_TYPE (loop.loopvar[0]),
6173 : : loop.loopvar[0], gfc_index_one_node);
6174 : 820 : gfc_init_block (&ifblock);
6175 : 820 : gfc_add_modify (&ifblock, limit, arrayse.expr);
6176 : 820 : gfc_add_modify (&ifblock, loop.loopvar[0], inc_loop);
6177 : 820 : gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab));
6178 : 820 : ifbody = gfc_finish_block (&ifblock);
6179 : : }
6180 : : else
6181 : : {
6182 : 420 : stmtblock_t ifblock;
6183 : :
6184 : 420 : gfc_init_block (&ifblock);
6185 : 420 : gfc_add_modify (&ifblock, limit, arrayse.expr);
6186 : 420 : gfc_add_modify (&ifblock, fast, logical_true_node);
6187 : 420 : ifbody = gfc_finish_block (&ifblock);
6188 : : }
6189 : 1240 : tmp = build3_v (COND_EXPR, tmp, ifbody,
6190 : : build_empty_stmt (input_location));
6191 : 1240 : gfc_add_expr_to_block (&block2, tmp);
6192 : : }
6193 : : else
6194 : : {
6195 : : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6196 : : signed zeros. */
6197 : 1528 : tmp = fold_build2_loc (input_location,
6198 : : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6199 : : type, arrayse.expr, limit);
6200 : 983 : gfc_add_modify (&block2, limit, tmp);
6201 : : }
6202 : :
6203 : 2223 : if (fast)
6204 : : {
6205 : 420 : tree elsebody = gfc_finish_block (&block2);
6206 : :
6207 : : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6208 : : signed zeros. */
6209 : 420 : if (HONOR_NANS (DECL_MODE (limit)))
6210 : : {
6211 : 420 : tmp = fold_build2_loc (input_location, op, logical_type_node,
6212 : : arrayse.expr, limit);
6213 : 420 : ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6214 : 420 : ifbody = build3_v (COND_EXPR, tmp, ifbody,
6215 : : build_empty_stmt (input_location));
6216 : : }
6217 : : else
6218 : : {
6219 : 0 : tmp = fold_build2_loc (input_location,
6220 : : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6221 : : type, arrayse.expr, limit);
6222 : 0 : ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6223 : : }
6224 : 420 : tmp = build3_v (COND_EXPR, fast, ifbody, elsebody);
6225 : 420 : gfc_add_expr_to_block (&block, tmp);
6226 : : }
6227 : : else
6228 : 1803 : gfc_add_block_to_block (&block, &block2);
6229 : :
6230 : 2223 : gfc_add_block_to_block (&block, &arrayse.post);
6231 : :
6232 : 2223 : tmp = gfc_finish_block (&block);
6233 : 2223 : if (maskss)
6234 : : {
6235 : : /* We enclose the above in if (mask) {...}. If the mask is an
6236 : : optional argument, generate IF (.NOT. PRESENT(MASK)
6237 : : .OR. MASK(I)). */
6238 : 1026 : tree ifmask;
6239 : 1026 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6240 : 1026 : tmp = build3_v (COND_EXPR, ifmask, tmp,
6241 : : build_empty_stmt (input_location));
6242 : : }
6243 : 2223 : gfc_add_expr_to_block (&body, tmp);
6244 : :
6245 : 2223 : if (lab)
6246 : : {
6247 : 820 : gfc_trans_scalarized_loop_boundary (&loop, &body);
6248 : :
6249 : 820 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6250 : : nan_cst, huge_cst);
6251 : 820 : gfc_add_modify (&loop.code[0], limit, tmp);
6252 : 820 : gfc_add_expr_to_block (&loop.code[0], build1_v (LABEL_EXPR, lab));
6253 : :
6254 : : /* If we have a mask, only add this element if the mask is set. */
6255 : 820 : if (maskss)
6256 : : {
6257 : 348 : gfc_init_se (&maskse, NULL);
6258 : 348 : gfc_copy_loopinfo_to_se (&maskse, &loop);
6259 : 348 : maskse.ss = maskss;
6260 : 348 : gfc_conv_expr_val (&maskse, maskexpr);
6261 : 348 : gfc_add_block_to_block (&body, &maskse.pre);
6262 : :
6263 : 348 : gfc_start_block (&block);
6264 : : }
6265 : : else
6266 : 472 : gfc_init_block (&block);
6267 : :
6268 : : /* Compare with the current limit. */
6269 : 820 : gfc_init_se (&arrayse, NULL);
6270 : 820 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
6271 : 820 : arrayse.ss = arrayss;
6272 : 820 : gfc_conv_expr_val (&arrayse, arrayexpr);
6273 : 820 : arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6274 : 820 : gfc_add_block_to_block (&block, &arrayse.pre);
6275 : :
6276 : : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6277 : : signed zeros. */
6278 : 820 : if (HONOR_NANS (DECL_MODE (limit)))
6279 : : {
6280 : 820 : tmp = fold_build2_loc (input_location, op, logical_type_node,
6281 : : arrayse.expr, limit);
6282 : 820 : ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6283 : 820 : tmp = build3_v (COND_EXPR, tmp, ifbody,
6284 : : build_empty_stmt (input_location));
6285 : 820 : gfc_add_expr_to_block (&block, tmp);
6286 : : }
6287 : : else
6288 : : {
6289 : 0 : tmp = fold_build2_loc (input_location,
6290 : : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6291 : : type, arrayse.expr, limit);
6292 : 0 : gfc_add_modify (&block, limit, tmp);
6293 : : }
6294 : :
6295 : 820 : gfc_add_block_to_block (&block, &arrayse.post);
6296 : :
6297 : 820 : tmp = gfc_finish_block (&block);
6298 : 820 : if (maskss)
6299 : : /* We enclose the above in if (mask) {...}. */
6300 : : {
6301 : 348 : tree ifmask;
6302 : 348 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6303 : 348 : tmp = build3_v (COND_EXPR, ifmask, tmp,
6304 : : build_empty_stmt (input_location));
6305 : : }
6306 : :
6307 : 820 : gfc_add_expr_to_block (&body, tmp);
6308 : : /* Avoid initializing loopvar[0] again, it should be left where
6309 : : it finished by the first loop. */
6310 : 820 : loop.from[0] = loop.loopvar[0];
6311 : : }
6312 : 2223 : gfc_trans_scalarizing_loops (&loop, &body);
6313 : :
6314 : 2223 : if (fast)
6315 : : {
6316 : 420 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6317 : : nan_cst, huge_cst);
6318 : 420 : ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6319 : 420 : tmp = build3_v (COND_EXPR, fast, build_empty_stmt (input_location),
6320 : : ifbody);
6321 : 420 : gfc_add_expr_to_block (&loop.pre, tmp);
6322 : : }
6323 : 1803 : else if (HONOR_INFINITIES (DECL_MODE (limit)) && !lab)
6324 : : {
6325 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty, limit,
6326 : : huge_cst);
6327 : 0 : gfc_add_modify (&loop.pre, limit, tmp);
6328 : : }
6329 : :
6330 : : /* For a scalar mask, enclose the loop in an if statement. */
6331 : 2223 : if (maskexpr && maskss == NULL)
6332 : : {
6333 : 546 : tree else_stmt;
6334 : 546 : tree ifmask;
6335 : :
6336 : 546 : gfc_init_se (&maskse, NULL);
6337 : 546 : gfc_conv_expr_val (&maskse, maskexpr);
6338 : 546 : gfc_init_block (&block);
6339 : 546 : gfc_add_block_to_block (&block, &loop.pre);
6340 : 546 : gfc_add_block_to_block (&block, &loop.post);
6341 : 546 : tmp = gfc_finish_block (&block);
6342 : :
6343 : 546 : if (HONOR_INFINITIES (DECL_MODE (limit)))
6344 : 354 : else_stmt = build2_v (MODIFY_EXPR, limit, huge_cst);
6345 : : else
6346 : 192 : else_stmt = build_empty_stmt (input_location);
6347 : :
6348 : 546 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6349 : 546 : tmp = build3_v (COND_EXPR, ifmask, tmp, else_stmt);
6350 : 546 : gfc_add_expr_to_block (&block, tmp);
6351 : 546 : gfc_add_block_to_block (&se->pre, &block);
6352 : : }
6353 : : else
6354 : : {
6355 : 1677 : gfc_add_block_to_block (&se->pre, &loop.pre);
6356 : 1677 : gfc_add_block_to_block (&se->pre, &loop.post);
6357 : : }
6358 : :
6359 : 2223 : gfc_cleanup_loop (&loop);
6360 : :
6361 : 2223 : se->expr = limit;
6362 : : }
6363 : :
6364 : : /* BTEST (i, pos) = (i & (1 << pos)) != 0. */
6365 : : static void
6366 : 145 : gfc_conv_intrinsic_btest (gfc_se * se, gfc_expr * expr)
6367 : : {
6368 : 145 : tree args[2];
6369 : 145 : tree type;
6370 : 145 : tree tmp;
6371 : :
6372 : 145 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6373 : 145 : type = TREE_TYPE (args[0]);
6374 : :
6375 : : /* Optionally generate code for runtime argument check. */
6376 : 145 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6377 : : {
6378 : 6 : tree below = fold_build2_loc (input_location, LT_EXPR,
6379 : : logical_type_node, args[1],
6380 : 6 : build_int_cst (TREE_TYPE (args[1]), 0));
6381 : 6 : tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6382 : 6 : tree above = fold_build2_loc (input_location, GE_EXPR,
6383 : : logical_type_node, args[1], nbits);
6384 : 6 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6385 : : logical_type_node, below, above);
6386 : 6 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6387 : : "POS argument (%ld) out of range 0:%ld "
6388 : : "in intrinsic BTEST",
6389 : : fold_convert (long_integer_type_node, args[1]),
6390 : : fold_convert (long_integer_type_node, nbits));
6391 : : }
6392 : :
6393 : 145 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6394 : 145 : build_int_cst (type, 1), args[1]);
6395 : 145 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], tmp);
6396 : 145 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
6397 : 145 : build_int_cst (type, 0));
6398 : 145 : type = gfc_typenode_for_spec (&expr->ts);
6399 : 145 : se->expr = convert (type, tmp);
6400 : 145 : }
6401 : :
6402 : :
6403 : : /* Generate code for BGE, BGT, BLE and BLT intrinsics. */
6404 : : static void
6405 : 216 : gfc_conv_intrinsic_bitcomp (gfc_se * se, gfc_expr * expr, enum tree_code op)
6406 : : {
6407 : 216 : tree args[2];
6408 : :
6409 : 216 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6410 : :
6411 : : /* Convert both arguments to the unsigned type of the same size. */
6412 : 216 : args[0] = fold_convert (unsigned_type_for (TREE_TYPE (args[0])), args[0]);
6413 : 216 : args[1] = fold_convert (unsigned_type_for (TREE_TYPE (args[1])), args[1]);
6414 : :
6415 : : /* If they have unequal type size, convert to the larger one. */
6416 : 216 : if (TYPE_PRECISION (TREE_TYPE (args[0]))
6417 : 216 : > TYPE_PRECISION (TREE_TYPE (args[1])))
6418 : 0 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
6419 : 216 : else if (TYPE_PRECISION (TREE_TYPE (args[1]))
6420 : 216 : > TYPE_PRECISION (TREE_TYPE (args[0])))
6421 : 0 : args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
6422 : :
6423 : : /* Now, we compare them. */
6424 : 216 : se->expr = fold_build2_loc (input_location, op, logical_type_node,
6425 : : args[0], args[1]);
6426 : 216 : }
6427 : :
6428 : :
6429 : : /* Generate code to perform the specified operation. */
6430 : : static void
6431 : 1891 : gfc_conv_intrinsic_bitop (gfc_se * se, gfc_expr * expr, enum tree_code op)
6432 : : {
6433 : 1891 : tree args[2];
6434 : :
6435 : 1891 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6436 : 1891 : se->expr = fold_build2_loc (input_location, op, TREE_TYPE (args[0]),
6437 : : args[0], args[1]);
6438 : 1891 : }
6439 : :
6440 : : /* Bitwise not. */
6441 : : static void
6442 : 228 : gfc_conv_intrinsic_not (gfc_se * se, gfc_expr * expr)
6443 : : {
6444 : 228 : tree arg;
6445 : :
6446 : 228 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
6447 : 228 : se->expr = fold_build1_loc (input_location, BIT_NOT_EXPR,
6448 : 228 : TREE_TYPE (arg), arg);
6449 : 228 : }
6450 : :
6451 : :
6452 : : /* Generate code for OUT_OF_RANGE. */
6453 : : static void
6454 : 468 : gfc_conv_intrinsic_out_of_range (gfc_se * se, gfc_expr * expr)
6455 : : {
6456 : 468 : tree *args;
6457 : 468 : tree type;
6458 : 468 : tree tmp = NULL_TREE, tmp1, tmp2;
6459 : 468 : unsigned int num_args;
6460 : 468 : int k;
6461 : 468 : gfc_se rnd_se;
6462 : 468 : gfc_actual_arglist *arg = expr->value.function.actual;
6463 : 468 : gfc_expr *x = arg->expr;
6464 : 468 : gfc_expr *mold = arg->next->expr;
6465 : :
6466 : 468 : num_args = gfc_intrinsic_argument_list_length (expr);
6467 : 468 : args = XALLOCAVEC (tree, num_args);
6468 : :
6469 : 468 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
6470 : :
6471 : 468 : gfc_init_se (&rnd_se, NULL);
6472 : :
6473 : 468 : if (num_args == 3)
6474 : : {
6475 : : /* The ROUND argument is optional and shall appear only if X is
6476 : : of type real and MOLD is of type integer (see edit F23/004). */
6477 : 270 : gfc_expr *round = arg->next->next->expr;
6478 : 270 : gfc_conv_expr (&rnd_se, round);
6479 : :
6480 : 270 : if (round->expr_type == EXPR_VARIABLE
6481 : 198 : && round->symtree->n.sym->attr.dummy
6482 : 198 : && round->symtree->n.sym->attr.optional)
6483 : : {
6484 : 30 : tree present = gfc_conv_expr_present (round->symtree->n.sym);
6485 : 30 : rnd_se.expr = build3_loc (input_location, COND_EXPR,
6486 : : logical_type_node, present,
6487 : : rnd_se.expr, logical_false_node);
6488 : 30 : gfc_add_block_to_block (&se->pre, &rnd_se.pre);
6489 : : }
6490 : : }
6491 : : else
6492 : : {
6493 : : /* If ROUND is absent, it is equivalent to having the value false. */
6494 : 198 : rnd_se.expr = logical_false_node;
6495 : : }
6496 : :
6497 : 468 : type = TREE_TYPE (args[0]);
6498 : 468 : k = gfc_validate_kind (mold->ts.type, mold->ts.kind, false);
6499 : :
6500 : 468 : switch (x->ts.type)
6501 : : {
6502 : 378 : case BT_REAL:
6503 : : /* X may be IEEE infinity or NaN, but the representation of MOLD may not
6504 : : support infinity or NaN. */
6505 : 378 : tree finite;
6506 : 378 : finite = build_call_expr_loc (input_location,
6507 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
6508 : : 1, args[0]);
6509 : 378 : finite = convert (logical_type_node, finite);
6510 : :
6511 : 378 : if (mold->ts.type == BT_REAL)
6512 : : {
6513 : 24 : tmp1 = build1 (ABS_EXPR, type, args[0]);
6514 : 24 : tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6515 : : mold->ts.kind, 0);
6516 : 24 : tmp = build2 (GT_EXPR, logical_type_node, tmp1,
6517 : : convert (type, tmp2));
6518 : :
6519 : : /* Check if MOLD representation supports infinity or NaN. */
6520 : 24 : bool infnan = (HONOR_INFINITIES (TREE_TYPE (args[1]))
6521 : 24 : || HONOR_NANS (TREE_TYPE (args[1])));
6522 : 24 : tmp = build3 (COND_EXPR, logical_type_node, finite, tmp,
6523 : : infnan ? logical_false_node : logical_true_node);
6524 : : }
6525 : : else
6526 : : {
6527 : 354 : tree rounded;
6528 : 354 : tree decl;
6529 : :
6530 : 354 : decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, x->ts.kind);
6531 : 354 : gcc_assert (decl != NULL_TREE);
6532 : :
6533 : : /* Round or truncate argument X, depending on the optional argument
6534 : : ROUND (default: .false.). */
6535 : 354 : tmp1 = build_round_expr (args[0], type);
6536 : 354 : tmp2 = build_call_expr_loc (input_location, decl, 1, args[0]);
6537 : 354 : rounded = build3 (COND_EXPR, type, rnd_se.expr, tmp1, tmp2);
6538 : :
6539 : 354 : if (mold->ts.type == BT_INTEGER)
6540 : : {
6541 : 180 : tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6542 : : x->ts.kind);
6543 : 180 : tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6544 : : x->ts.kind);
6545 : : }
6546 : 174 : else if (mold->ts.type == BT_UNSIGNED)
6547 : : {
6548 : 174 : tmp1 = build_real_from_int_cst (type, integer_zero_node);
6549 : 174 : tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6550 : : x->ts.kind);
6551 : : }
6552 : : else
6553 : 0 : gcc_unreachable ();
6554 : :
6555 : 354 : tmp1 = build2 (LT_EXPR, logical_type_node, rounded,
6556 : : convert (type, tmp1));
6557 : 354 : tmp2 = build2 (GT_EXPR, logical_type_node, rounded,
6558 : : convert (type, tmp2));
6559 : 354 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6560 : 354 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node,
6561 : : build1 (TRUTH_NOT_EXPR, logical_type_node, finite),
6562 : : tmp);
6563 : : }
6564 : : break;
6565 : :
6566 : 48 : case BT_INTEGER:
6567 : 48 : if (mold->ts.type == BT_INTEGER)
6568 : : {
6569 : 12 : tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6570 : : x->ts.kind);
6571 : 12 : tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6572 : : x->ts.kind);
6573 : 12 : tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6574 : : convert (type, tmp1));
6575 : 12 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6576 : : convert (type, tmp2));
6577 : 12 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6578 : : }
6579 : 36 : else if (mold->ts.type == BT_UNSIGNED)
6580 : : {
6581 : 36 : int i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
6582 : 36 : tmp = build_int_cst (type, 0);
6583 : 36 : tmp = build2 (LT_EXPR, logical_type_node, args[0], tmp);
6584 : 36 : if (mpz_cmp (gfc_integer_kinds[i].huge,
6585 : 36 : gfc_unsigned_kinds[k].huge) > 0)
6586 : : {
6587 : 0 : tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6588 : : x->ts.kind);
6589 : 0 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6590 : : convert (type, tmp2));
6591 : 0 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp, tmp2);
6592 : : }
6593 : : }
6594 : 0 : else if (mold->ts.type == BT_REAL)
6595 : : {
6596 : 0 : tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6597 : : mold->ts.kind, 0);
6598 : 0 : tmp1 = build1 (NEGATE_EXPR, TREE_TYPE (tmp2), tmp2);
6599 : 0 : tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6600 : : convert (type, tmp1));
6601 : 0 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6602 : : convert (type, tmp2));
6603 : 0 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6604 : : }
6605 : : else
6606 : 0 : gcc_unreachable ();
6607 : : break;
6608 : :
6609 : 42 : case BT_UNSIGNED:
6610 : 42 : if (mold->ts.type == BT_UNSIGNED)
6611 : : {
6612 : 12 : tmp = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6613 : : x->ts.kind);
6614 : 12 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6615 : : convert (type, tmp));
6616 : : }
6617 : 30 : else if (mold->ts.type == BT_INTEGER)
6618 : : {
6619 : 18 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6620 : : x->ts.kind);
6621 : 18 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6622 : : convert (type, tmp));
6623 : : }
6624 : 12 : else if (mold->ts.type == BT_REAL)
6625 : : {
6626 : 12 : tmp = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6627 : : mold->ts.kind, 0);
6628 : 12 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6629 : : convert (type, tmp));
6630 : : }
6631 : : else
6632 : 0 : gcc_unreachable ();
6633 : : break;
6634 : :
6635 : 0 : default:
6636 : 0 : gcc_unreachable ();
6637 : : }
6638 : :
6639 : 468 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
6640 : 468 : }
6641 : :
6642 : :
6643 : : /* Set or clear a single bit. */
6644 : : static void
6645 : 306 : gfc_conv_intrinsic_singlebitop (gfc_se * se, gfc_expr * expr, int set)
6646 : : {
6647 : 306 : tree args[2];
6648 : 306 : tree type;
6649 : 306 : tree tmp;
6650 : 306 : enum tree_code op;
6651 : :
6652 : 306 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6653 : 306 : type = TREE_TYPE (args[0]);
6654 : :
6655 : : /* Optionally generate code for runtime argument check. */
6656 : 306 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6657 : : {
6658 : 12 : tree below = fold_build2_loc (input_location, LT_EXPR,
6659 : : logical_type_node, args[1],
6660 : 12 : build_int_cst (TREE_TYPE (args[1]), 0));
6661 : 12 : tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6662 : 12 : tree above = fold_build2_loc (input_location, GE_EXPR,
6663 : : logical_type_node, args[1], nbits);
6664 : 12 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6665 : : logical_type_node, below, above);
6666 : 12 : size_t len_name = strlen (expr->value.function.isym->name);
6667 : 12 : char *name = XALLOCAVEC (char, len_name + 1);
6668 : 72 : for (size_t i = 0; i < len_name; i++)
6669 : 60 : name[i] = TOUPPER (expr->value.function.isym->name[i]);
6670 : 12 : name[len_name] = '\0';
6671 : 12 : tree iname = gfc_build_addr_expr (pchar_type_node,
6672 : : gfc_build_cstring_const (name));
6673 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6674 : : "POS argument (%ld) out of range 0:%ld "
6675 : : "in intrinsic %s",
6676 : : fold_convert (long_integer_type_node, args[1]),
6677 : : fold_convert (long_integer_type_node, nbits),
6678 : : iname);
6679 : : }
6680 : :
6681 : 306 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6682 : 306 : build_int_cst (type, 1), args[1]);
6683 : 306 : if (set)
6684 : : op = BIT_IOR_EXPR;
6685 : : else
6686 : : {
6687 : 168 : op = BIT_AND_EXPR;
6688 : 168 : tmp = fold_build1_loc (input_location, BIT_NOT_EXPR, type, tmp);
6689 : : }
6690 : 306 : se->expr = fold_build2_loc (input_location, op, type, args[0], tmp);
6691 : 306 : }
6692 : :
6693 : : /* Extract a sequence of bits.
6694 : : IBITS(I, POS, LEN) = (I >> POS) & ~((~0) << LEN). */
6695 : : static void
6696 : 27 : gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr)
6697 : : {
6698 : 27 : tree args[3];
6699 : 27 : tree type;
6700 : 27 : tree tmp;
6701 : 27 : tree mask;
6702 : 27 : tree num_bits, cond;
6703 : :
6704 : 27 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
6705 : 27 : type = TREE_TYPE (args[0]);
6706 : :
6707 : : /* Optionally generate code for runtime argument check. */
6708 : 27 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6709 : : {
6710 : 12 : tree tmp1 = fold_convert (long_integer_type_node, args[1]);
6711 : 12 : tree tmp2 = fold_convert (long_integer_type_node, args[2]);
6712 : 36 : tree nbits = build_int_cst (long_integer_type_node,
6713 : 12 : TYPE_PRECISION (type));
6714 : 12 : tree below = fold_build2_loc (input_location, LT_EXPR,
6715 : : logical_type_node, args[1],
6716 : 12 : build_int_cst (TREE_TYPE (args[1]), 0));
6717 : 12 : tree above = fold_build2_loc (input_location, GT_EXPR,
6718 : : logical_type_node, tmp1, nbits);
6719 : 12 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6720 : : logical_type_node, below, above);
6721 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6722 : : "POS argument (%ld) out of range 0:%ld "
6723 : : "in intrinsic IBITS", tmp1, nbits);
6724 : 12 : below = fold_build2_loc (input_location, LT_EXPR,
6725 : : logical_type_node, args[2],
6726 : 12 : build_int_cst (TREE_TYPE (args[2]), 0));
6727 : 12 : above = fold_build2_loc (input_location, GT_EXPR,
6728 : : logical_type_node, tmp2, nbits);
6729 : 12 : scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6730 : : logical_type_node, below, above);
6731 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6732 : : "LEN argument (%ld) out of range 0:%ld "
6733 : : "in intrinsic IBITS", tmp2, nbits);
6734 : 12 : above = fold_build2_loc (input_location, PLUS_EXPR,
6735 : : long_integer_type_node, tmp1, tmp2);
6736 : 12 : scond = fold_build2_loc (input_location, GT_EXPR,
6737 : : logical_type_node, above, nbits);
6738 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6739 : : "POS(%ld)+LEN(%ld)>BIT_SIZE(%ld) "
6740 : : "in intrinsic IBITS", tmp1, tmp2, nbits);
6741 : : }
6742 : :
6743 : : /* The Fortran standard allows (shift width) LEN <= BIT_SIZE(I), whereas
6744 : : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6745 : : special case. See also gfc_conv_intrinsic_ishft (). */
6746 : 27 : num_bits = build_int_cst (TREE_TYPE (args[2]), TYPE_PRECISION (type));
6747 : :
6748 : 27 : mask = build_int_cst (type, -1);
6749 : 27 : mask = fold_build2_loc (input_location, LSHIFT_EXPR, type, mask, args[2]);
6750 : 27 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[2],
6751 : : num_bits);
6752 : 27 : mask = fold_build3_loc (input_location, COND_EXPR, type, cond,
6753 : 27 : build_int_cst (type, 0), mask);
6754 : 27 : mask = fold_build1_loc (input_location, BIT_NOT_EXPR, type, mask);
6755 : :
6756 : 27 : tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, args[0], args[1]);
6757 : :
6758 : 27 : se->expr = fold_build2_loc (input_location, BIT_AND_EXPR, type, tmp, mask);
6759 : 27 : }
6760 : :
6761 : : static void
6762 : 441 : gfc_conv_intrinsic_shift (gfc_se * se, gfc_expr * expr, bool right_shift,
6763 : : bool arithmetic)
6764 : : {
6765 : 441 : tree args[2], type, num_bits, cond;
6766 : 441 : tree bigshift;
6767 : 441 : bool do_convert = false;
6768 : :
6769 : 441 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6770 : :
6771 : 441 : args[0] = gfc_evaluate_now (args[0], &se->pre);
6772 : 441 : args[1] = gfc_evaluate_now (args[1], &se->pre);
6773 : 441 : type = TREE_TYPE (args[0]);
6774 : :
6775 : 441 : if (!arithmetic)
6776 : : {
6777 : 339 : args[0] = fold_convert (unsigned_type_for (type), args[0]);
6778 : 339 : do_convert = true;
6779 : : }
6780 : : else
6781 : 102 : gcc_assert (right_shift);
6782 : :
6783 : 441 : if (flag_unsigned && arithmetic && expr->ts.type == BT_UNSIGNED)
6784 : : {
6785 : 30 : do_convert = true;
6786 : 30 : args[0] = fold_convert (signed_type_for (type), args[0]);
6787 : : }
6788 : :
6789 : 714 : se->expr = fold_build2_loc (input_location,
6790 : : right_shift ? RSHIFT_EXPR : LSHIFT_EXPR,
6791 : 441 : TREE_TYPE (args[0]), args[0], args[1]);
6792 : :
6793 : 441 : if (do_convert)
6794 : 369 : se->expr = fold_convert (type, se->expr);
6795 : :
6796 : 441 : if (!arithmetic)
6797 : 339 : bigshift = build_int_cst (type, 0);
6798 : : else
6799 : : {
6800 : 102 : tree nonneg = fold_build2_loc (input_location, GE_EXPR,
6801 : : logical_type_node, args[0],
6802 : 102 : build_int_cst (TREE_TYPE (args[0]), 0));
6803 : 102 : bigshift = fold_build3_loc (input_location, COND_EXPR, type, nonneg,
6804 : 102 : build_int_cst (type, 0),
6805 : 102 : build_int_cst (type, -1));
6806 : : }
6807 : :
6808 : : /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
6809 : : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6810 : : special case. */
6811 : 441 : num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6812 : :
6813 : : /* Optionally generate code for runtime argument check. */
6814 : 441 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6815 : : {
6816 : 30 : tree below = fold_build2_loc (input_location, LT_EXPR,
6817 : : logical_type_node, args[1],
6818 : 30 : build_int_cst (TREE_TYPE (args[1]), 0));
6819 : 30 : tree above = fold_build2_loc (input_location, GT_EXPR,
6820 : : logical_type_node, args[1], num_bits);
6821 : 30 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6822 : : logical_type_node, below, above);
6823 : 30 : size_t len_name = strlen (expr->value.function.isym->name);
6824 : 30 : char *name = XALLOCAVEC (char, len_name + 1);
6825 : 210 : for (size_t i = 0; i < len_name; i++)
6826 : 180 : name[i] = TOUPPER (expr->value.function.isym->name[i]);
6827 : 30 : name[len_name] = '\0';
6828 : 30 : tree iname = gfc_build_addr_expr (pchar_type_node,
6829 : : gfc_build_cstring_const (name));
6830 : 30 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6831 : : "SHIFT argument (%ld) out of range 0:%ld "
6832 : : "in intrinsic %s",
6833 : : fold_convert (long_integer_type_node, args[1]),
6834 : : fold_convert (long_integer_type_node, num_bits),
6835 : : iname);
6836 : : }
6837 : :
6838 : 441 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
6839 : : args[1], num_bits);
6840 : :
6841 : 441 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
6842 : : bigshift, se->expr);
6843 : 441 : }
6844 : :
6845 : : /* ISHFT (I, SHIFT) = (abs (shift) >= BIT_SIZE (i))
6846 : : ? 0
6847 : : : ((shift >= 0) ? i << shift : i >> -shift)
6848 : : where all shifts are logical shifts. */
6849 : : static void
6850 : 318 : gfc_conv_intrinsic_ishft (gfc_se * se, gfc_expr * expr)
6851 : : {
6852 : 318 : tree args[2];
6853 : 318 : tree type;
6854 : 318 : tree utype;
6855 : 318 : tree tmp;
6856 : 318 : tree width;
6857 : 318 : tree num_bits;
6858 : 318 : tree cond;
6859 : 318 : tree lshift;
6860 : 318 : tree rshift;
6861 : :
6862 : 318 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6863 : :
6864 : 318 : args[0] = gfc_evaluate_now (args[0], &se->pre);
6865 : 318 : args[1] = gfc_evaluate_now (args[1], &se->pre);
6866 : :
6867 : 318 : type = TREE_TYPE (args[0]);
6868 : 318 : utype = unsigned_type_for (type);
6869 : :
6870 : 318 : width = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (args[1]),
6871 : : args[1]);
6872 : :
6873 : : /* Left shift if positive. */
6874 : 318 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type, args[0], width);
6875 : :
6876 : : /* Right shift if negative.
6877 : : We convert to an unsigned type because we want a logical shift.
6878 : : The standard doesn't define the case of shifting negative
6879 : : numbers, and we try to be compatible with other compilers, most
6880 : : notably g77, here. */
6881 : 318 : rshift = fold_convert (type, fold_build2_loc (input_location, RSHIFT_EXPR,
6882 : : utype, convert (utype, args[0]), width));
6883 : :
6884 : 318 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[1],
6885 : 318 : build_int_cst (TREE_TYPE (args[1]), 0));
6886 : 318 : tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp, lshift, rshift);
6887 : :
6888 : : /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
6889 : : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6890 : : special case. */
6891 : 318 : num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6892 : :
6893 : : /* Optionally generate code for runtime argument check. */
6894 : 318 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6895 : : {
6896 : 24 : tree outside = fold_build2_loc (input_location, GT_EXPR,
6897 : : logical_type_node, width, num_bits);
6898 : 24 : gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
6899 : : "SHIFT argument (%ld) out of range -%ld:%ld "
6900 : : "in intrinsic ISHFT",
6901 : : fold_convert (long_integer_type_node, args[1]),
6902 : : fold_convert (long_integer_type_node, num_bits),
6903 : : fold_convert (long_integer_type_node, num_bits));
6904 : : }
6905 : :
6906 : 318 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, width,
6907 : : num_bits);
6908 : 318 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
6909 : 318 : build_int_cst (type, 0), tmp);
6910 : 318 : }
6911 : :
6912 : :
6913 : : /* Circular shift. AKA rotate or barrel shift. */
6914 : :
6915 : : static void
6916 : 658 : gfc_conv_intrinsic_ishftc (gfc_se * se, gfc_expr * expr)
6917 : : {
6918 : 658 : tree *args;
6919 : 658 : tree type;
6920 : 658 : tree tmp;
6921 : 658 : tree lrot;
6922 : 658 : tree rrot;
6923 : 658 : tree zero;
6924 : 658 : tree nbits;
6925 : 658 : unsigned int num_args;
6926 : :
6927 : 658 : num_args = gfc_intrinsic_argument_list_length (expr);
6928 : 658 : args = XALLOCAVEC (tree, num_args);
6929 : :
6930 : 658 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
6931 : :
6932 : 658 : type = TREE_TYPE (args[0]);
6933 : 658 : nbits = build_int_cst (long_integer_type_node, TYPE_PRECISION (type));
6934 : :
6935 : 658 : if (num_args == 3)
6936 : : {
6937 : 550 : gfc_expr *size = expr->value.function.actual->next->next->expr;
6938 : :
6939 : : /* Use a library function for the 3 parameter version. */
6940 : 550 : tree int4type = gfc_get_int_type (4);
6941 : :
6942 : : /* Treat optional SIZE argument when it is passed as an optional
6943 : : dummy. If SIZE is absent, the default value is BIT_SIZE(I). */
6944 : 550 : if (size->expr_type == EXPR_VARIABLE
6945 : 438 : && size->symtree->n.sym->attr.dummy
6946 : 438 : && size->symtree->n.sym->attr.optional)
6947 : : {
6948 : 36 : tree type_of_size = TREE_TYPE (args[2]);
6949 : 72 : args[2] = build3_loc (input_location, COND_EXPR, type_of_size,
6950 : 36 : gfc_conv_expr_present (size->symtree->n.sym),
6951 : : args[2], fold_convert (type_of_size, nbits));
6952 : : }
6953 : :
6954 : : /* We convert the first argument to at least 4 bytes, and
6955 : : convert back afterwards. This removes the need for library
6956 : : functions for all argument sizes, and function will be
6957 : : aligned to at least 32 bits, so there's no loss. */
6958 : 550 : if (expr->ts.kind < 4)
6959 : 242 : args[0] = convert (int4type, args[0]);
6960 : :
6961 : : /* Convert the SHIFT and SIZE args to INTEGER*4 otherwise we would
6962 : : need loads of library functions. They cannot have values >
6963 : : BIT_SIZE (I) so the conversion is safe. */
6964 : 550 : args[1] = convert (int4type, args[1]);
6965 : 550 : args[2] = convert (int4type, args[2]);
6966 : :
6967 : : /* Optionally generate code for runtime argument check. */
6968 : 550 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6969 : : {
6970 : 18 : tree size = fold_convert (long_integer_type_node, args[2]);
6971 : 18 : tree below = fold_build2_loc (input_location, LE_EXPR,
6972 : : logical_type_node, size,
6973 : 18 : build_int_cst (TREE_TYPE (args[1]), 0));
6974 : 18 : tree above = fold_build2_loc (input_location, GT_EXPR,
6975 : : logical_type_node, size, nbits);
6976 : 18 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6977 : : logical_type_node, below, above);
6978 : 18 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6979 : : "SIZE argument (%ld) out of range 1:%ld "
6980 : : "in intrinsic ISHFTC", size, nbits);
6981 : 18 : tree width = fold_convert (long_integer_type_node, args[1]);
6982 : 18 : width = fold_build1_loc (input_location, ABS_EXPR,
6983 : : long_integer_type_node, width);
6984 : 18 : scond = fold_build2_loc (input_location, GT_EXPR,
6985 : : logical_type_node, width, size);
6986 : 18 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6987 : : "SHIFT argument (%ld) out of range -%ld:%ld "
6988 : : "in intrinsic ISHFTC",
6989 : : fold_convert (long_integer_type_node, args[1]),
6990 : : size, size);
6991 : : }
6992 : :
6993 : 550 : switch (expr->ts.kind)
6994 : : {
6995 : 426 : case 1:
6996 : 426 : case 2:
6997 : 426 : case 4:
6998 : 426 : tmp = gfor_fndecl_math_ishftc4;
6999 : 426 : break;
7000 : 124 : case 8:
7001 : 124 : tmp = gfor_fndecl_math_ishftc8;
7002 : 124 : break;
7003 : 0 : case 16:
7004 : 0 : tmp = gfor_fndecl_math_ishftc16;
7005 : 0 : break;
7006 : 0 : default:
7007 : 0 : gcc_unreachable ();
7008 : : }
7009 : 550 : se->expr = build_call_expr_loc (input_location,
7010 : : tmp, 3, args[0], args[1], args[2]);
7011 : : /* Convert the result back to the original type, if we extended
7012 : : the first argument's width above. */
7013 : 550 : if (expr->ts.kind < 4)
7014 : 242 : se->expr = convert (type, se->expr);
7015 : :
7016 : 550 : return;
7017 : : }
7018 : :
7019 : : /* Evaluate arguments only once. */
7020 : 108 : args[0] = gfc_evaluate_now (args[0], &se->pre);
7021 : 108 : args[1] = gfc_evaluate_now (args[1], &se->pre);
7022 : :
7023 : : /* Optionally generate code for runtime argument check. */
7024 : 108 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7025 : : {
7026 : 12 : tree width = fold_convert (long_integer_type_node, args[1]);
7027 : 12 : width = fold_build1_loc (input_location, ABS_EXPR,
7028 : : long_integer_type_node, width);
7029 : 12 : tree outside = fold_build2_loc (input_location, GT_EXPR,
7030 : : logical_type_node, width, nbits);
7031 : 12 : gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
7032 : : "SHIFT argument (%ld) out of range -%ld:%ld "
7033 : : "in intrinsic ISHFTC",
7034 : : fold_convert (long_integer_type_node, args[1]),
7035 : : nbits, nbits);
7036 : : }
7037 : :
7038 : : /* Rotate left if positive. */
7039 : 108 : lrot = fold_build2_loc (input_location, LROTATE_EXPR, type, args[0], args[1]);
7040 : :
7041 : : /* Rotate right if negative. */
7042 : 108 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (args[1]),
7043 : : args[1]);
7044 : 108 : rrot = fold_build2_loc (input_location,RROTATE_EXPR, type, args[0], tmp);
7045 : :
7046 : 108 : zero = build_int_cst (TREE_TYPE (args[1]), 0);
7047 : 108 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, args[1],
7048 : : zero);
7049 : 108 : rrot = fold_build3_loc (input_location, COND_EXPR, type, tmp, lrot, rrot);
7050 : :
7051 : : /* Do nothing if shift == 0. */
7052 : 108 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, args[1],
7053 : : zero);
7054 : 108 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, args[0],
7055 : : rrot);
7056 : : }
7057 : :
7058 : :
7059 : : /* LEADZ (i) = (i == 0) ? BIT_SIZE (i)
7060 : : : __builtin_clz(i) - (BIT_SIZE('int') - BIT_SIZE(i))
7061 : :
7062 : : The conditional expression is necessary because the result of LEADZ(0)
7063 : : is defined, but the result of __builtin_clz(0) is undefined for most
7064 : : targets.
7065 : :
7066 : : For INTEGER kinds smaller than the C 'int' type, we have to subtract the
7067 : : difference in bit size between the argument of LEADZ and the C int. */
7068 : :
7069 : : static void
7070 : 270 : gfc_conv_intrinsic_leadz (gfc_se * se, gfc_expr * expr)
7071 : : {
7072 : 270 : tree arg;
7073 : 270 : tree arg_type;
7074 : 270 : tree cond;
7075 : 270 : tree result_type;
7076 : 270 : tree leadz;
7077 : 270 : tree bit_size;
7078 : 270 : tree tmp;
7079 : 270 : tree func;
7080 : 270 : int s, argsize;
7081 : :
7082 : 270 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7083 : 270 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7084 : :
7085 : : /* Which variant of __builtin_clz* should we call? */
7086 : 270 : if (argsize <= INT_TYPE_SIZE)
7087 : : {
7088 : 183 : arg_type = unsigned_type_node;
7089 : 183 : func = builtin_decl_explicit (BUILT_IN_CLZ);
7090 : : }
7091 : 87 : else if (argsize <= LONG_TYPE_SIZE)
7092 : : {
7093 : 57 : arg_type = long_unsigned_type_node;
7094 : 57 : func = builtin_decl_explicit (BUILT_IN_CLZL);
7095 : : }
7096 : 30 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7097 : : {
7098 : 0 : arg_type = long_long_unsigned_type_node;
7099 : 0 : func = builtin_decl_explicit (BUILT_IN_CLZLL);
7100 : : }
7101 : : else
7102 : : {
7103 : 30 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7104 : 30 : arg_type = gfc_build_uint_type (argsize);
7105 : 30 : func = NULL_TREE;
7106 : : }
7107 : :
7108 : : /* Convert the actual argument twice: first, to the unsigned type of the
7109 : : same size; then, to the proper argument type for the built-in
7110 : : function. But the return type is of the default INTEGER kind. */
7111 : 270 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7112 : 270 : arg = fold_convert (arg_type, arg);
7113 : 270 : arg = gfc_evaluate_now (arg, &se->pre);
7114 : 270 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7115 : :
7116 : : /* Compute LEADZ for the case i .ne. 0. */
7117 : 270 : if (func)
7118 : : {
7119 : 240 : s = TYPE_PRECISION (arg_type) - argsize;
7120 : 240 : tmp = fold_convert (result_type,
7121 : : build_call_expr_loc (input_location, func,
7122 : : 1, arg));
7123 : 240 : leadz = fold_build2_loc (input_location, MINUS_EXPR, result_type,
7124 : 240 : tmp, build_int_cst (result_type, s));
7125 : : }
7126 : : else
7127 : : {
7128 : : /* We end up here if the argument type is larger than 'long long'.
7129 : : We generate this code:
7130 : :
7131 : : if (x & (ULL_MAX << ULL_SIZE) != 0)
7132 : : return clzll ((unsigned long long) (x >> ULLSIZE));
7133 : : else
7134 : : return ULL_SIZE + clzll ((unsigned long long) x);
7135 : : where ULL_MAX is the largest value that a ULL_MAX can hold
7136 : : (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7137 : : is the bit-size of the long long type (64 in this example). */
7138 : 30 : tree ullsize, ullmax, tmp1, tmp2, btmp;
7139 : :
7140 : 30 : ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7141 : 30 : ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7142 : : long_long_unsigned_type_node,
7143 : 30 : build_int_cst (long_long_unsigned_type_node,
7144 : 30 : 0));
7145 : :
7146 : 30 : cond = fold_build2_loc (input_location, LSHIFT_EXPR, arg_type,
7147 : : fold_convert (arg_type, ullmax), ullsize);
7148 : 30 : cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type,
7149 : : arg, cond);
7150 : 30 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
7151 : 30 : cond, build_int_cst (arg_type, 0));
7152 : :
7153 : 30 : tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7154 : : arg, ullsize);
7155 : 30 : tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7156 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7157 : 30 : tmp1 = fold_convert (result_type,
7158 : : build_call_expr_loc (input_location, btmp, 1, tmp1));
7159 : :
7160 : 30 : tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7161 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7162 : 30 : tmp2 = fold_convert (result_type,
7163 : : build_call_expr_loc (input_location, btmp, 1, tmp2));
7164 : 30 : tmp2 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7165 : : tmp2, ullsize);
7166 : :
7167 : 30 : leadz = fold_build3_loc (input_location, COND_EXPR, result_type,
7168 : : cond, tmp1, tmp2);
7169 : : }
7170 : :
7171 : : /* Build BIT_SIZE. */
7172 : 270 : bit_size = build_int_cst (result_type, argsize);
7173 : :
7174 : 270 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7175 : 270 : arg, build_int_cst (arg_type, 0));
7176 : 270 : se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7177 : : bit_size, leadz);
7178 : 270 : }
7179 : :
7180 : :
7181 : : /* TRAILZ(i) = (i == 0) ? BIT_SIZE (i) : __builtin_ctz(i)
7182 : :
7183 : : The conditional expression is necessary because the result of TRAILZ(0)
7184 : : is defined, but the result of __builtin_ctz(0) is undefined for most
7185 : : targets. */
7186 : :
7187 : : static void
7188 : 282 : gfc_conv_intrinsic_trailz (gfc_se * se, gfc_expr *expr)
7189 : : {
7190 : 282 : tree arg;
7191 : 282 : tree arg_type;
7192 : 282 : tree cond;
7193 : 282 : tree result_type;
7194 : 282 : tree trailz;
7195 : 282 : tree bit_size;
7196 : 282 : tree func;
7197 : 282 : int argsize;
7198 : :
7199 : 282 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7200 : 282 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7201 : :
7202 : : /* Which variant of __builtin_ctz* should we call? */
7203 : 282 : if (argsize <= INT_TYPE_SIZE)
7204 : : {
7205 : 195 : arg_type = unsigned_type_node;
7206 : 195 : func = builtin_decl_explicit (BUILT_IN_CTZ);
7207 : : }
7208 : 87 : else if (argsize <= LONG_TYPE_SIZE)
7209 : : {
7210 : 57 : arg_type = long_unsigned_type_node;
7211 : 57 : func = builtin_decl_explicit (BUILT_IN_CTZL);
7212 : : }
7213 : 30 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7214 : : {
7215 : 0 : arg_type = long_long_unsigned_type_node;
7216 : 0 : func = builtin_decl_explicit (BUILT_IN_CTZLL);
7217 : : }
7218 : : else
7219 : : {
7220 : 30 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7221 : 30 : arg_type = gfc_build_uint_type (argsize);
7222 : 30 : func = NULL_TREE;
7223 : : }
7224 : :
7225 : : /* Convert the actual argument twice: first, to the unsigned type of the
7226 : : same size; then, to the proper argument type for the built-in
7227 : : function. But the return type is of the default INTEGER kind. */
7228 : 282 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7229 : 282 : arg = fold_convert (arg_type, arg);
7230 : 282 : arg = gfc_evaluate_now (arg, &se->pre);
7231 : 282 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7232 : :
7233 : : /* Compute TRAILZ for the case i .ne. 0. */
7234 : 282 : if (func)
7235 : 252 : trailz = fold_convert (result_type, build_call_expr_loc (input_location,
7236 : : func, 1, arg));
7237 : : else
7238 : : {
7239 : : /* We end up here if the argument type is larger than 'long long'.
7240 : : We generate this code:
7241 : :
7242 : : if ((x & ULL_MAX) == 0)
7243 : : return ULL_SIZE + ctzll ((unsigned long long) (x >> ULLSIZE));
7244 : : else
7245 : : return ctzll ((unsigned long long) x);
7246 : :
7247 : : where ULL_MAX is the largest value that a ULL_MAX can hold
7248 : : (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7249 : : is the bit-size of the long long type (64 in this example). */
7250 : 30 : tree ullsize, ullmax, tmp1, tmp2, btmp;
7251 : :
7252 : 30 : ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7253 : 30 : ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7254 : : long_long_unsigned_type_node,
7255 : 30 : build_int_cst (long_long_unsigned_type_node, 0));
7256 : :
7257 : 30 : cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type, arg,
7258 : : fold_convert (arg_type, ullmax));
7259 : 30 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, cond,
7260 : 30 : build_int_cst (arg_type, 0));
7261 : :
7262 : 30 : tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7263 : : arg, ullsize);
7264 : 30 : tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7265 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7266 : 30 : tmp1 = fold_convert (result_type,
7267 : : build_call_expr_loc (input_location, btmp, 1, tmp1));
7268 : 30 : tmp1 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7269 : : tmp1, ullsize);
7270 : :
7271 : 30 : tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7272 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7273 : 30 : tmp2 = fold_convert (result_type,
7274 : : build_call_expr_loc (input_location, btmp, 1, tmp2));
7275 : :
7276 : 30 : trailz = fold_build3_loc (input_location, COND_EXPR, result_type,
7277 : : cond, tmp1, tmp2);
7278 : : }
7279 : :
7280 : : /* Build BIT_SIZE. */
7281 : 282 : bit_size = build_int_cst (result_type, argsize);
7282 : :
7283 : 282 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7284 : 282 : arg, build_int_cst (arg_type, 0));
7285 : 282 : se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7286 : : bit_size, trailz);
7287 : 282 : }
7288 : :
7289 : : /* Using __builtin_popcount for POPCNT and __builtin_parity for POPPAR;
7290 : : for types larger than "long long", we call the long long built-in for
7291 : : the lower and higher bits and combine the result. */
7292 : :
7293 : : static void
7294 : 134 : gfc_conv_intrinsic_popcnt_poppar (gfc_se * se, gfc_expr *expr, int parity)
7295 : : {
7296 : 134 : tree arg;
7297 : 134 : tree arg_type;
7298 : 134 : tree result_type;
7299 : 134 : tree func;
7300 : 134 : int argsize;
7301 : :
7302 : 134 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7303 : 134 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7304 : 134 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7305 : :
7306 : : /* Which variant of the builtin should we call? */
7307 : 134 : if (argsize <= INT_TYPE_SIZE)
7308 : : {
7309 : 108 : arg_type = unsigned_type_node;
7310 : 198 : func = builtin_decl_explicit (parity
7311 : : ? BUILT_IN_PARITY
7312 : : : BUILT_IN_POPCOUNT);
7313 : : }
7314 : 26 : else if (argsize <= LONG_TYPE_SIZE)
7315 : : {
7316 : 12 : arg_type = long_unsigned_type_node;
7317 : 18 : func = builtin_decl_explicit (parity
7318 : : ? BUILT_IN_PARITYL
7319 : : : BUILT_IN_POPCOUNTL);
7320 : : }
7321 : 14 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7322 : : {
7323 : 0 : arg_type = long_long_unsigned_type_node;
7324 : 0 : func = builtin_decl_explicit (parity
7325 : : ? BUILT_IN_PARITYLL
7326 : : : BUILT_IN_POPCOUNTLL);
7327 : : }
7328 : : else
7329 : : {
7330 : : /* Our argument type is larger than 'long long', which mean none
7331 : : of the POPCOUNT builtins covers it. We thus call the 'long long'
7332 : : variant multiple times, and add the results. */
7333 : 14 : tree utype, arg2, call1, call2;
7334 : :
7335 : : /* For now, we only cover the case where argsize is twice as large
7336 : : as 'long long'. */
7337 : 14 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7338 : :
7339 : 21 : func = builtin_decl_explicit (parity
7340 : : ? BUILT_IN_PARITYLL
7341 : : : BUILT_IN_POPCOUNTLL);
7342 : :
7343 : : /* Convert it to an integer, and store into a variable. */
7344 : 14 : utype = gfc_build_uint_type (argsize);
7345 : 14 : arg = fold_convert (utype, arg);
7346 : 14 : arg = gfc_evaluate_now (arg, &se->pre);
7347 : :
7348 : : /* Call the builtin twice. */
7349 : 14 : call1 = build_call_expr_loc (input_location, func, 1,
7350 : : fold_convert (long_long_unsigned_type_node,
7351 : : arg));
7352 : :
7353 : 14 : arg2 = fold_build2_loc (input_location, RSHIFT_EXPR, utype, arg,
7354 : 14 : build_int_cst (utype, LONG_LONG_TYPE_SIZE));
7355 : 14 : call2 = build_call_expr_loc (input_location, func, 1,
7356 : : fold_convert (long_long_unsigned_type_node,
7357 : : arg2));
7358 : :
7359 : : /* Combine the results. */
7360 : 14 : if (parity)
7361 : 7 : se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR,
7362 : : integer_type_node, call1, call2);
7363 : : else
7364 : 7 : se->expr = fold_build2_loc (input_location, PLUS_EXPR,
7365 : : integer_type_node, call1, call2);
7366 : :
7367 : 14 : se->expr = convert (result_type, se->expr);
7368 : 14 : return;
7369 : : }
7370 : :
7371 : : /* Convert the actual argument twice: first, to the unsigned type of the
7372 : : same size; then, to the proper argument type for the built-in
7373 : : function. */
7374 : 120 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7375 : 120 : arg = fold_convert (arg_type, arg);
7376 : :
7377 : 120 : se->expr = fold_convert (result_type,
7378 : : build_call_expr_loc (input_location, func, 1, arg));
7379 : : }
7380 : :
7381 : :
7382 : : /* Process an intrinsic with unspecified argument-types that has an optional
7383 : : argument (which could be of type character), e.g. EOSHIFT. For those, we
7384 : : need to append the string length of the optional argument if it is not
7385 : : present and the type is really character.
7386 : : primary specifies the position (starting at 1) of the non-optional argument
7387 : : specifying the type and optional gives the position of the optional
7388 : : argument in the arglist. */
7389 : :
7390 : : static void
7391 : 6074 : conv_generic_with_optional_char_arg (gfc_se* se, gfc_expr* expr,
7392 : : unsigned primary, unsigned optional)
7393 : : {
7394 : 6074 : gfc_actual_arglist* prim_arg;
7395 : 6074 : gfc_actual_arglist* opt_arg;
7396 : 6074 : unsigned cur_pos;
7397 : 6074 : gfc_actual_arglist* arg;
7398 : 6074 : gfc_symbol* sym;
7399 : 6074 : vec<tree, va_gc> *append_args;
7400 : :
7401 : : /* Find the two arguments given as position. */
7402 : 6074 : cur_pos = 0;
7403 : 6074 : prim_arg = NULL;
7404 : 6074 : opt_arg = NULL;
7405 : 18222 : for (arg = expr->value.function.actual; arg; arg = arg->next)
7406 : : {
7407 : 18222 : ++cur_pos;
7408 : :
7409 : 18222 : if (cur_pos == primary)
7410 : 6074 : prim_arg = arg;
7411 : 18222 : if (cur_pos == optional)
7412 : 6074 : opt_arg = arg;
7413 : :
7414 : 18222 : if (cur_pos >= primary && cur_pos >= optional)
7415 : : break;
7416 : : }
7417 : 6074 : gcc_assert (prim_arg);
7418 : 6074 : gcc_assert (prim_arg->expr);
7419 : 6074 : gcc_assert (opt_arg);
7420 : :
7421 : : /* If we do have type CHARACTER and the optional argument is really absent,
7422 : : append a dummy 0 as string length. */
7423 : 6074 : append_args = NULL;
7424 : 6074 : if (prim_arg->expr->ts.type == BT_CHARACTER && !opt_arg->expr)
7425 : : {
7426 : 608 : tree dummy;
7427 : :
7428 : 608 : dummy = build_int_cst (gfc_charlen_type_node, 0);
7429 : 608 : vec_alloc (append_args, 1);
7430 : 608 : append_args->quick_push (dummy);
7431 : : }
7432 : :
7433 : : /* Build the call itself. */
7434 : 6074 : gcc_assert (!se->ignore_optional);
7435 : 6074 : sym = gfc_get_symbol_for_expr (expr, false);
7436 : 6074 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
7437 : : append_args);
7438 : 6074 : gfc_free_symbol (sym);
7439 : 6074 : }
7440 : :
7441 : : /* The length of a character string. */
7442 : : static void
7443 : 5589 : gfc_conv_intrinsic_len (gfc_se * se, gfc_expr * expr)
7444 : : {
7445 : 5589 : tree len;
7446 : 5589 : tree type;
7447 : 5589 : tree decl;
7448 : 5589 : gfc_symbol *sym;
7449 : 5589 : gfc_se argse;
7450 : 5589 : gfc_expr *arg;
7451 : :
7452 : 5589 : gcc_assert (!se->ss);
7453 : :
7454 : 5589 : arg = expr->value.function.actual->expr;
7455 : :
7456 : 5589 : type = gfc_typenode_for_spec (&expr->ts);
7457 : 5589 : switch (arg->expr_type)
7458 : : {
7459 : 0 : case EXPR_CONSTANT:
7460 : 0 : len = build_int_cst (gfc_charlen_type_node, arg->value.character.length);
7461 : 0 : break;
7462 : :
7463 : 2 : case EXPR_ARRAY:
7464 : : /* Obtain the string length from the function used by
7465 : : trans-array.cc(gfc_trans_array_constructor). */
7466 : 2 : len = NULL_TREE;
7467 : 2 : get_array_ctor_strlen (&se->pre, arg->value.constructor, &len);
7468 : 2 : break;
7469 : :
7470 : 5004 : case EXPR_VARIABLE:
7471 : 5004 : if (arg->ref == NULL
7472 : 2252 : || (arg->ref->next == NULL && arg->ref->type == REF_ARRAY))
7473 : : {
7474 : : /* This doesn't catch all cases.
7475 : : See http://gcc.gnu.org/ml/fortran/2004-06/msg00165.html
7476 : : and the surrounding thread. */
7477 : 4481 : sym = arg->symtree->n.sym;
7478 : 4481 : decl = gfc_get_symbol_decl (sym);
7479 : 4481 : if (decl == current_function_decl && sym->attr.function
7480 : 53 : && (sym->result == sym))
7481 : 53 : decl = gfc_get_fake_result_decl (sym, 0);
7482 : :
7483 : 4481 : len = sym->ts.u.cl->backend_decl;
7484 : 4481 : gcc_assert (len);
7485 : : break;
7486 : : }
7487 : :
7488 : : /* Fall through. */
7489 : :
7490 : 1106 : default:
7491 : 1106 : gfc_init_se (&argse, se);
7492 : 1106 : if (arg->rank == 0)
7493 : 989 : gfc_conv_expr (&argse, arg);
7494 : : else
7495 : 117 : gfc_conv_expr_descriptor (&argse, arg);
7496 : 1106 : gfc_add_block_to_block (&se->pre, &argse.pre);
7497 : 1106 : gfc_add_block_to_block (&se->post, &argse.post);
7498 : 1106 : len = argse.string_length;
7499 : 1106 : break;
7500 : : }
7501 : 5589 : se->expr = convert (type, len);
7502 : 5589 : }
7503 : :
7504 : : /* The length of a character string not including trailing blanks. */
7505 : : static void
7506 : 2313 : gfc_conv_intrinsic_len_trim (gfc_se * se, gfc_expr * expr)
7507 : : {
7508 : 2313 : int kind = expr->value.function.actual->expr->ts.kind;
7509 : 2313 : tree args[2], type, fndecl;
7510 : :
7511 : 2313 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7512 : 2313 : type = gfc_typenode_for_spec (&expr->ts);
7513 : :
7514 : 2313 : if (kind == 1)
7515 : 1911 : fndecl = gfor_fndecl_string_len_trim;
7516 : 402 : else if (kind == 4)
7517 : 402 : fndecl = gfor_fndecl_string_len_trim_char4;
7518 : : else
7519 : 0 : gcc_unreachable ();
7520 : :
7521 : 2313 : se->expr = build_call_expr_loc (input_location,
7522 : : fndecl, 2, args[0], args[1]);
7523 : 2313 : se->expr = convert (type, se->expr);
7524 : 2313 : }
7525 : :
7526 : :
7527 : : /* Returns the starting position of a substring within a string. */
7528 : :
7529 : : static void
7530 : 738 : gfc_conv_intrinsic_index_scan_verify (gfc_se * se, gfc_expr * expr,
7531 : : tree function)
7532 : : {
7533 : 738 : tree logical4_type_node = gfc_get_logical_type (4);
7534 : 738 : tree type;
7535 : 738 : tree fndecl;
7536 : 738 : tree *args;
7537 : 738 : unsigned int num_args;
7538 : :
7539 : 738 : args = XALLOCAVEC (tree, 5);
7540 : :
7541 : : /* Get number of arguments; characters count double due to the
7542 : : string length argument. Kind= is not passed to the library
7543 : : and thus ignored. */
7544 : 738 : if (expr->value.function.actual->next->next->expr == NULL)
7545 : : num_args = 4;
7546 : : else
7547 : 304 : num_args = 5;
7548 : :
7549 : 738 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7550 : 738 : type = gfc_typenode_for_spec (&expr->ts);
7551 : :
7552 : 738 : if (num_args == 4)
7553 : 434 : args[4] = build_int_cst (logical4_type_node, 0);
7554 : : else
7555 : 304 : args[4] = convert (logical4_type_node, args[4]);
7556 : :
7557 : 738 : fndecl = build_addr (function);
7558 : 738 : se->expr = build_call_array_loc (input_location,
7559 : 738 : TREE_TYPE (TREE_TYPE (function)), fndecl,
7560 : : 5, args);
7561 : 738 : se->expr = convert (type, se->expr);
7562 : :
7563 : 738 : }
7564 : :
7565 : : /* The ascii value for a single character. */
7566 : : static void
7567 : 2033 : gfc_conv_intrinsic_ichar (gfc_se * se, gfc_expr * expr)
7568 : : {
7569 : 2033 : tree args[3], type, pchartype;
7570 : 2033 : int nargs;
7571 : :
7572 : 2033 : nargs = gfc_intrinsic_argument_list_length (expr);
7573 : 2033 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
7574 : 2033 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (args[1])));
7575 : 2033 : pchartype = gfc_get_pchar_type (expr->value.function.actual->expr->ts.kind);
7576 : 2033 : args[1] = fold_build1_loc (input_location, NOP_EXPR, pchartype, args[1]);
7577 : 2033 : type = gfc_typenode_for_spec (&expr->ts);
7578 : :
7579 : 2033 : se->expr = build_fold_indirect_ref_loc (input_location,
7580 : : args[1]);
7581 : 2033 : se->expr = convert (type, se->expr);
7582 : 2033 : }
7583 : :
7584 : :
7585 : : /* Intrinsic ISNAN calls __builtin_isnan. */
7586 : :
7587 : : static void
7588 : 432 : gfc_conv_intrinsic_isnan (gfc_se * se, gfc_expr * expr)
7589 : : {
7590 : 432 : tree arg;
7591 : :
7592 : 432 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7593 : 432 : se->expr = build_call_expr_loc (input_location,
7594 : : builtin_decl_explicit (BUILT_IN_ISNAN),
7595 : : 1, arg);
7596 : 864 : STRIP_TYPE_NOPS (se->expr);
7597 : 432 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
7598 : 432 : }
7599 : :
7600 : :
7601 : : /* Intrinsics IS_IOSTAT_END and IS_IOSTAT_EOR just need to compare
7602 : : their argument against a constant integer value. */
7603 : :
7604 : : static void
7605 : 24 : gfc_conv_has_intvalue (gfc_se * se, gfc_expr * expr, const int value)
7606 : : {
7607 : 24 : tree arg;
7608 : :
7609 : 24 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7610 : 24 : se->expr = fold_build2_loc (input_location, EQ_EXPR,
7611 : : gfc_typenode_for_spec (&expr->ts),
7612 : 24 : arg, build_int_cst (TREE_TYPE (arg), value));
7613 : 24 : }
7614 : :
7615 : :
7616 : :
7617 : : /* MERGE (tsource, fsource, mask) = mask ? tsource : fsource. */
7618 : :
7619 : : static void
7620 : 949 : gfc_conv_intrinsic_merge (gfc_se * se, gfc_expr * expr)
7621 : : {
7622 : 949 : tree tsource;
7623 : 949 : tree fsource;
7624 : 949 : tree mask;
7625 : 949 : tree type;
7626 : 949 : tree len, len2;
7627 : 949 : tree *args;
7628 : 949 : unsigned int num_args;
7629 : :
7630 : 949 : num_args = gfc_intrinsic_argument_list_length (expr);
7631 : 949 : args = XALLOCAVEC (tree, num_args);
7632 : :
7633 : 949 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7634 : 949 : if (expr->ts.type != BT_CHARACTER)
7635 : : {
7636 : 422 : tsource = args[0];
7637 : 422 : fsource = args[1];
7638 : 422 : mask = args[2];
7639 : : }
7640 : : else
7641 : : {
7642 : : /* We do the same as in the non-character case, but the argument
7643 : : list is different because of the string length arguments. We
7644 : : also have to set the string length for the result. */
7645 : 527 : len = args[0];
7646 : 527 : tsource = args[1];
7647 : 527 : len2 = args[2];
7648 : 527 : fsource = args[3];
7649 : 527 : mask = args[4];
7650 : :
7651 : 527 : gfc_trans_same_strlen_check ("MERGE intrinsic", &expr->where, len, len2,
7652 : : &se->pre);
7653 : 527 : se->string_length = len;
7654 : : }
7655 : 949 : tsource = gfc_evaluate_now (tsource, &se->pre);
7656 : 949 : fsource = gfc_evaluate_now (fsource, &se->pre);
7657 : 949 : mask = gfc_evaluate_now (mask, &se->pre);
7658 : 949 : type = TREE_TYPE (tsource);
7659 : 949 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, mask, tsource,
7660 : : fold_convert (type, fsource));
7661 : 949 : }
7662 : :
7663 : :
7664 : : /* MERGE_BITS (I, J, MASK) = (I & MASK) | (I & (~MASK)). */
7665 : :
7666 : : static void
7667 : 42 : gfc_conv_intrinsic_merge_bits (gfc_se * se, gfc_expr * expr)
7668 : : {
7669 : 42 : tree args[3], mask, type;
7670 : :
7671 : 42 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
7672 : 42 : mask = gfc_evaluate_now (args[2], &se->pre);
7673 : :
7674 : 42 : type = TREE_TYPE (args[0]);
7675 : 42 : gcc_assert (TREE_TYPE (args[1]) == type);
7676 : 42 : gcc_assert (TREE_TYPE (mask) == type);
7677 : :
7678 : 42 : args[0] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], mask);
7679 : 42 : args[1] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[1],
7680 : : fold_build1_loc (input_location, BIT_NOT_EXPR,
7681 : : type, mask));
7682 : 42 : se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
7683 : : args[0], args[1]);
7684 : 42 : }
7685 : :
7686 : :
7687 : : /* MASKL(n) = n == 0 ? 0 : (~0) << (BIT_SIZE - n)
7688 : : MASKR(n) = n == BIT_SIZE ? ~0 : ~((~0) << n) */
7689 : :
7690 : : static void
7691 : 64 : gfc_conv_intrinsic_mask (gfc_se * se, gfc_expr * expr, int left)
7692 : : {
7693 : 64 : tree arg, allones, type, utype, res, cond, bitsize;
7694 : 64 : int i;
7695 : :
7696 : 64 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7697 : 64 : arg = gfc_evaluate_now (arg, &se->pre);
7698 : :
7699 : 64 : type = gfc_get_int_type (expr->ts.kind);
7700 : 64 : utype = unsigned_type_for (type);
7701 : :
7702 : 64 : i = gfc_validate_kind (BT_INTEGER, expr->ts.kind, false);
7703 : 64 : bitsize = build_int_cst (TREE_TYPE (arg), gfc_integer_kinds[i].bit_size);
7704 : :
7705 : 64 : allones = fold_build1_loc (input_location, BIT_NOT_EXPR, utype,
7706 : 64 : build_int_cst (utype, 0));
7707 : :
7708 : 64 : if (left)
7709 : : {
7710 : : /* Left-justified mask. */
7711 : 32 : res = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (arg),
7712 : : bitsize, arg);
7713 : 32 : res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7714 : : fold_convert (utype, res));
7715 : :
7716 : : /* Special case arg == 0, because SHIFT_EXPR wants a shift strictly
7717 : : smaller than type width. */
7718 : 32 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
7719 : 32 : build_int_cst (TREE_TYPE (arg), 0));
7720 : 32 : res = fold_build3_loc (input_location, COND_EXPR, utype, cond,
7721 : 32 : build_int_cst (utype, 0), res);
7722 : : }
7723 : : else
7724 : : {
7725 : : /* Right-justified mask. */
7726 : 32 : res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7727 : : fold_convert (utype, arg));
7728 : 32 : res = fold_build1_loc (input_location, BIT_NOT_EXPR, utype, res);
7729 : :
7730 : : /* Special case agr == bit_size, because SHIFT_EXPR wants a shift
7731 : : strictly smaller than type width. */
7732 : 32 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7733 : : arg, bitsize);
7734 : 32 : res = fold_build3_loc (input_location, COND_EXPR, utype,
7735 : : cond, allones, res);
7736 : : }
7737 : :
7738 : 64 : se->expr = fold_convert (type, res);
7739 : 64 : }
7740 : :
7741 : :
7742 : : /* FRACTION (s) is translated into:
7743 : : isfinite (s) ? frexp (s, &dummy_int) : NaN */
7744 : : static void
7745 : 60 : gfc_conv_intrinsic_fraction (gfc_se * se, gfc_expr * expr)
7746 : : {
7747 : 60 : tree arg, type, tmp, res, frexp, cond;
7748 : :
7749 : 60 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7750 : :
7751 : 60 : type = gfc_typenode_for_spec (&expr->ts);
7752 : 60 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7753 : 60 : arg = gfc_evaluate_now (arg, &se->pre);
7754 : :
7755 : 60 : cond = build_call_expr_loc (input_location,
7756 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
7757 : : 1, arg);
7758 : :
7759 : 60 : tmp = gfc_create_var (integer_type_node, NULL);
7760 : 60 : res = build_call_expr_loc (input_location, frexp, 2,
7761 : : fold_convert (type, arg),
7762 : : gfc_build_addr_expr (NULL_TREE, tmp));
7763 : 60 : res = fold_convert (type, res);
7764 : :
7765 : 60 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
7766 : : cond, res, gfc_build_nan (type, ""));
7767 : 60 : }
7768 : :
7769 : :
7770 : : /* NEAREST (s, dir) is translated into
7771 : : tmp = copysign (HUGE_VAL, dir);
7772 : : return nextafter (s, tmp);
7773 : : */
7774 : : static void
7775 : 1595 : gfc_conv_intrinsic_nearest (gfc_se * se, gfc_expr * expr)
7776 : : {
7777 : 1595 : tree args[2], type, tmp, nextafter, copysign, huge_val;
7778 : :
7779 : 1595 : nextafter = gfc_builtin_decl_for_float_kind (BUILT_IN_NEXTAFTER, expr->ts.kind);
7780 : 1595 : copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
7781 : :
7782 : 1595 : type = gfc_typenode_for_spec (&expr->ts);
7783 : 1595 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7784 : :
7785 : 1595 : huge_val = gfc_build_inf_or_huge (type, expr->ts.kind);
7786 : 1595 : tmp = build_call_expr_loc (input_location, copysign, 2, huge_val,
7787 : : fold_convert (type, args[1]));
7788 : 1595 : se->expr = build_call_expr_loc (input_location, nextafter, 2,
7789 : : fold_convert (type, args[0]), tmp);
7790 : 1595 : se->expr = fold_convert (type, se->expr);
7791 : 1595 : }
7792 : :
7793 : :
7794 : : /* SPACING (s) is translated into
7795 : : int e;
7796 : : if (!isfinite (s))
7797 : : res = NaN;
7798 : : else if (s == 0)
7799 : : res = tiny;
7800 : : else
7801 : : {
7802 : : frexp (s, &e);
7803 : : e = e - prec;
7804 : : e = MAX_EXPR (e, emin);
7805 : : res = scalbn (1., e);
7806 : : }
7807 : : return res;
7808 : :
7809 : : where prec is the precision of s, gfc_real_kinds[k].digits,
7810 : : emin is min_exponent - 1, gfc_real_kinds[k].min_exponent - 1,
7811 : : and tiny is tiny(s), gfc_real_kinds[k].tiny. */
7812 : :
7813 : : static void
7814 : 70 : gfc_conv_intrinsic_spacing (gfc_se * se, gfc_expr * expr)
7815 : : {
7816 : 70 : tree arg, type, prec, emin, tiny, res, e;
7817 : 70 : tree cond, nan, tmp, frexp, scalbn;
7818 : 70 : int k;
7819 : 70 : stmtblock_t block;
7820 : :
7821 : 70 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
7822 : 70 : prec = build_int_cst (integer_type_node, gfc_real_kinds[k].digits);
7823 : 70 : emin = build_int_cst (integer_type_node, gfc_real_kinds[k].min_exponent - 1);
7824 : 70 : tiny = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].tiny, expr->ts.kind, 0);
7825 : :
7826 : 70 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7827 : 70 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7828 : :
7829 : 70 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7830 : 70 : arg = gfc_evaluate_now (arg, &se->pre);
7831 : :
7832 : 70 : type = gfc_typenode_for_spec (&expr->ts);
7833 : 70 : e = gfc_create_var (integer_type_node, NULL);
7834 : 70 : res = gfc_create_var (type, NULL);
7835 : :
7836 : :
7837 : : /* Build the block for s /= 0. */
7838 : 70 : gfc_start_block (&block);
7839 : 70 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
7840 : : gfc_build_addr_expr (NULL_TREE, e));
7841 : 70 : gfc_add_expr_to_block (&block, tmp);
7842 : :
7843 : 70 : tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node, e,
7844 : : prec);
7845 : 70 : gfc_add_modify (&block, e, fold_build2_loc (input_location, MAX_EXPR,
7846 : : integer_type_node, tmp, emin));
7847 : :
7848 : 70 : tmp = build_call_expr_loc (input_location, scalbn, 2,
7849 : 70 : build_real_from_int_cst (type, integer_one_node), e);
7850 : 70 : gfc_add_modify (&block, res, tmp);
7851 : :
7852 : : /* Finish by building the IF statement for value zero. */
7853 : 70 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
7854 : 70 : build_real_from_int_cst (type, integer_zero_node));
7855 : 70 : tmp = build3_v (COND_EXPR, cond, build2_v (MODIFY_EXPR, res, tiny),
7856 : : gfc_finish_block (&block));
7857 : :
7858 : : /* And deal with infinities and NaNs. */
7859 : 70 : cond = build_call_expr_loc (input_location,
7860 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
7861 : : 1, arg);
7862 : 70 : nan = gfc_build_nan (type, "");
7863 : 70 : tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, res, nan));
7864 : :
7865 : 70 : gfc_add_expr_to_block (&se->pre, tmp);
7866 : 70 : se->expr = res;
7867 : 70 : }
7868 : :
7869 : :
7870 : : /* RRSPACING (s) is translated into
7871 : : int e;
7872 : : real x;
7873 : : x = fabs (s);
7874 : : if (isfinite (x))
7875 : : {
7876 : : if (x != 0)
7877 : : {
7878 : : frexp (s, &e);
7879 : : x = scalbn (x, precision - e);
7880 : : }
7881 : : }
7882 : : else
7883 : : x = NaN;
7884 : : return x;
7885 : :
7886 : : where precision is gfc_real_kinds[k].digits. */
7887 : :
7888 : : static void
7889 : 48 : gfc_conv_intrinsic_rrspacing (gfc_se * se, gfc_expr * expr)
7890 : : {
7891 : 48 : tree arg, type, e, x, cond, nan, stmt, tmp, frexp, scalbn, fabs;
7892 : 48 : int prec, k;
7893 : 48 : stmtblock_t block;
7894 : :
7895 : 48 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
7896 : 48 : prec = gfc_real_kinds[k].digits;
7897 : :
7898 : 48 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7899 : 48 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7900 : 48 : fabs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
7901 : :
7902 : 48 : type = gfc_typenode_for_spec (&expr->ts);
7903 : 48 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7904 : 48 : arg = gfc_evaluate_now (arg, &se->pre);
7905 : :
7906 : 48 : e = gfc_create_var (integer_type_node, NULL);
7907 : 48 : x = gfc_create_var (type, NULL);
7908 : 48 : gfc_add_modify (&se->pre, x,
7909 : : build_call_expr_loc (input_location, fabs, 1, arg));
7910 : :
7911 : :
7912 : 48 : gfc_start_block (&block);
7913 : 48 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
7914 : : gfc_build_addr_expr (NULL_TREE, e));
7915 : 48 : gfc_add_expr_to_block (&block, tmp);
7916 : :
7917 : 48 : tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
7918 : 48 : build_int_cst (integer_type_node, prec), e);
7919 : 48 : tmp = build_call_expr_loc (input_location, scalbn, 2, x, tmp);
7920 : 48 : gfc_add_modify (&block, x, tmp);
7921 : 48 : stmt = gfc_finish_block (&block);
7922 : :
7923 : : /* if (x != 0) */
7924 : 48 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, x,
7925 : 48 : build_real_from_int_cst (type, integer_zero_node));
7926 : 48 : tmp = build3_v (COND_EXPR, cond, stmt, build_empty_stmt (input_location));
7927 : :
7928 : : /* And deal with infinities and NaNs. */
7929 : 48 : cond = build_call_expr_loc (input_location,
7930 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
7931 : : 1, x);
7932 : 48 : nan = gfc_build_nan (type, "");
7933 : 48 : tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, x, nan));
7934 : :
7935 : 48 : gfc_add_expr_to_block (&se->pre, tmp);
7936 : 48 : se->expr = fold_convert (type, x);
7937 : 48 : }
7938 : :
7939 : :
7940 : : /* SCALE (s, i) is translated into scalbn (s, i). */
7941 : : static void
7942 : 72 : gfc_conv_intrinsic_scale (gfc_se * se, gfc_expr * expr)
7943 : : {
7944 : 72 : tree args[2], type, scalbn;
7945 : :
7946 : 72 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7947 : :
7948 : 72 : type = gfc_typenode_for_spec (&expr->ts);
7949 : 72 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7950 : 72 : se->expr = build_call_expr_loc (input_location, scalbn, 2,
7951 : : fold_convert (type, args[0]),
7952 : : fold_convert (integer_type_node, args[1]));
7953 : 72 : se->expr = fold_convert (type, se->expr);
7954 : 72 : }
7955 : :
7956 : :
7957 : : /* SET_EXPONENT (s, i) is translated into
7958 : : isfinite(s) ? scalbn (frexp (s, &dummy_int), i) : NaN */
7959 : : static void
7960 : 262 : gfc_conv_intrinsic_set_exponent (gfc_se * se, gfc_expr * expr)
7961 : : {
7962 : 262 : tree args[2], type, tmp, frexp, scalbn, cond, nan, res;
7963 : :
7964 : 262 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7965 : 262 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7966 : :
7967 : 262 : type = gfc_typenode_for_spec (&expr->ts);
7968 : 262 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7969 : 262 : args[0] = gfc_evaluate_now (args[0], &se->pre);
7970 : :
7971 : 262 : tmp = gfc_create_var (integer_type_node, NULL);
7972 : 262 : tmp = build_call_expr_loc (input_location, frexp, 2,
7973 : : fold_convert (type, args[0]),
7974 : : gfc_build_addr_expr (NULL_TREE, tmp));
7975 : 262 : res = build_call_expr_loc (input_location, scalbn, 2, tmp,
7976 : : fold_convert (integer_type_node, args[1]));
7977 : 262 : res = fold_convert (type, res);
7978 : :
7979 : : /* Call to isfinite */
7980 : 262 : cond = build_call_expr_loc (input_location,
7981 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
7982 : : 1, args[0]);
7983 : 262 : nan = gfc_build_nan (type, "");
7984 : :
7985 : 262 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
7986 : : res, nan);
7987 : 262 : }
7988 : :
7989 : :
7990 : : static void
7991 : 14518 : gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
7992 : : {
7993 : 14518 : gfc_actual_arglist *actual;
7994 : 14518 : tree arg1;
7995 : 14518 : tree type;
7996 : 14518 : tree size;
7997 : 14518 : gfc_se argse;
7998 : 14518 : gfc_expr *e;
7999 : 14518 : gfc_symbol *sym = NULL;
8000 : :
8001 : 14518 : gfc_init_se (&argse, NULL);
8002 : 14518 : actual = expr->value.function.actual;
8003 : :
8004 : 14518 : if (actual->expr->ts.type == BT_CLASS)
8005 : 590 : gfc_add_class_array_ref (actual->expr);
8006 : :
8007 : 14518 : e = actual->expr;
8008 : :
8009 : : /* These are emerging from the interface mapping, when a class valued
8010 : : function appears as the rhs in a realloc on assign statement, where
8011 : : the size of the result is that of one of the actual arguments. */
8012 : 14518 : if (e->expr_type == EXPR_VARIABLE
8013 : 14076 : && e->symtree->n.sym->ns == NULL /* This is distinctive! */
8014 : 555 : && e->symtree->n.sym->ts.type == BT_CLASS
8015 : 44 : && e->ref && e->ref->type == REF_COMPONENT
8016 : 26 : && strcmp (e->ref->u.c.component->name, "_data") == 0)
8017 : 14518 : sym = e->symtree->n.sym;
8018 : :
8019 : 14518 : if ((gfc_option.rtcheck & GFC_RTCHECK_POINTER)
8020 : : && e
8021 : 854 : && (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION))
8022 : : {
8023 : 854 : symbol_attribute attr;
8024 : 854 : char *msg;
8025 : 854 : tree temp;
8026 : 854 : tree cond;
8027 : :
8028 : 854 : if (e->symtree->n.sym && IS_CLASS_ARRAY (e->symtree->n.sym))
8029 : : {
8030 : 33 : attr = CLASS_DATA (e->symtree->n.sym)->attr;
8031 : 33 : attr.pointer = attr.class_pointer;
8032 : : }
8033 : : else
8034 : 821 : attr = gfc_expr_attr (e);
8035 : :
8036 : 854 : if (attr.allocatable)
8037 : 100 : msg = xasprintf ("Allocatable argument '%s' is not allocated",
8038 : 100 : e->symtree->n.sym->name);
8039 : 754 : else if (attr.pointer)
8040 : 46 : msg = xasprintf ("Pointer argument '%s' is not associated",
8041 : 46 : e->symtree->n.sym->name);
8042 : : else
8043 : 708 : goto end_arg_check;
8044 : :
8045 : 146 : if (sym)
8046 : : {
8047 : 0 : temp = gfc_class_data_get (sym->backend_decl);
8048 : 0 : temp = gfc_conv_descriptor_data_get (temp);
8049 : : }
8050 : : else
8051 : : {
8052 : 146 : argse.descriptor_only = 1;
8053 : 146 : gfc_conv_expr_descriptor (&argse, actual->expr);
8054 : 146 : temp = gfc_conv_descriptor_data_get (argse.expr);
8055 : : }
8056 : :
8057 : 146 : cond = fold_build2_loc (input_location, EQ_EXPR,
8058 : : logical_type_node, temp,
8059 : 146 : fold_convert (TREE_TYPE (temp),
8060 : : null_pointer_node));
8061 : 146 : gfc_trans_runtime_check (true, false, cond, &argse.pre, &e->where, msg);
8062 : :
8063 : 146 : free (msg);
8064 : : }
8065 : 13664 : end_arg_check:
8066 : :
8067 : 14518 : argse.data_not_needed = 1;
8068 : 14518 : if (gfc_is_class_array_function (e))
8069 : : {
8070 : : /* For functions that return a class array conv_expr_descriptor is not
8071 : : able to get the descriptor right. Therefore this special case. */
8072 : 6 : gfc_conv_expr_reference (&argse, e);
8073 : 6 : argse.expr = gfc_class_data_get (argse.expr);
8074 : : }
8075 : 14512 : else if (sym && sym->backend_decl)
8076 : : {
8077 : 14 : gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (sym->backend_decl)));
8078 : 14 : argse.expr = gfc_class_data_get (sym->backend_decl);
8079 : : }
8080 : : else
8081 : 14498 : gfc_conv_expr_descriptor (&argse, actual->expr);
8082 : 14518 : gfc_add_block_to_block (&se->pre, &argse.pre);
8083 : 14518 : gfc_add_block_to_block (&se->post, &argse.post);
8084 : 14518 : arg1 = argse.expr;
8085 : :
8086 : 14518 : actual = actual->next;
8087 : 14518 : if (actual->expr)
8088 : : {
8089 : 8608 : stmtblock_t block;
8090 : 8608 : gfc_init_block (&block);
8091 : 8608 : gfc_init_se (&argse, NULL);
8092 : 8608 : gfc_conv_expr_type (&argse, actual->expr,
8093 : : gfc_array_index_type);
8094 : 8608 : gfc_add_block_to_block (&block, &argse.pre);
8095 : 8608 : tree tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8096 : : argse.expr, gfc_index_one_node);
8097 : 8608 : size = gfc_tree_array_size (&block, arg1, e, tmp);
8098 : :
8099 : : /* Unusually, for an intrinsic, size does not exclude
8100 : : an optional arg2, so we must test for it. */
8101 : 8608 : if (actual->expr->expr_type == EXPR_VARIABLE
8102 : 2010 : && actual->expr->symtree->n.sym->attr.dummy
8103 : 2010 : && actual->expr->symtree->n.sym->attr.optional)
8104 : : {
8105 : 31 : tree cond;
8106 : 31 : stmtblock_t block2;
8107 : 31 : gfc_init_block (&block2);
8108 : 31 : gfc_init_se (&argse, NULL);
8109 : 31 : argse.want_pointer = 1;
8110 : 31 : argse.data_not_needed = 1;
8111 : 31 : gfc_conv_expr (&argse, actual->expr);
8112 : 31 : gfc_add_block_to_block (&se->pre, &argse.pre);
8113 : : /* 'block2' contains the arg2 absent case, 'block' the arg2 present
8114 : : case; size_var can be used in both blocks. */
8115 : 31 : tree size_var = gfc_create_var (TREE_TYPE (size), "size");
8116 : 31 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8117 : 31 : TREE_TYPE (size_var), size_var, size);
8118 : 31 : gfc_add_expr_to_block (&block, tmp);
8119 : 31 : size = gfc_tree_array_size (&block2, arg1, e, NULL_TREE);
8120 : 31 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8121 : 31 : TREE_TYPE (size_var), size_var, size);
8122 : 31 : gfc_add_expr_to_block (&block2, tmp);
8123 : 31 : cond = gfc_conv_expr_present (actual->expr->symtree->n.sym);
8124 : 31 : tmp = build3_v (COND_EXPR, cond, gfc_finish_block (&block),
8125 : : gfc_finish_block (&block2));
8126 : 31 : gfc_add_expr_to_block (&se->pre, tmp);
8127 : 31 : size = size_var;
8128 : 31 : }
8129 : : else
8130 : 8577 : gfc_add_block_to_block (&se->pre, &block);
8131 : : }
8132 : : else
8133 : 5910 : size = gfc_tree_array_size (&se->pre, arg1, e, NULL_TREE);
8134 : 14518 : type = gfc_typenode_for_spec (&expr->ts);
8135 : 14518 : se->expr = convert (type, size);
8136 : 14518 : }
8137 : :
8138 : :
8139 : : /* Helper function to compute the size of a character variable,
8140 : : excluding the terminating null characters. The result has
8141 : : gfc_array_index_type type. */
8142 : :
8143 : : tree
8144 : 1776 : size_of_string_in_bytes (int kind, tree string_length)
8145 : : {
8146 : 1776 : tree bytesize;
8147 : 1776 : int i = gfc_validate_kind (BT_CHARACTER, kind, false);
8148 : :
8149 : 3552 : bytesize = build_int_cst (gfc_array_index_type,
8150 : 1776 : gfc_character_kinds[i].bit_size / 8);
8151 : :
8152 : 1776 : return fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8153 : : bytesize,
8154 : 1776 : fold_convert (gfc_array_index_type, string_length));
8155 : : }
8156 : :
8157 : :
8158 : : static void
8159 : 1296 : gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr)
8160 : : {
8161 : 1296 : gfc_expr *arg;
8162 : 1296 : gfc_se argse;
8163 : 1296 : tree source_bytes;
8164 : 1296 : tree tmp;
8165 : 1296 : tree lower;
8166 : 1296 : tree upper;
8167 : 1296 : tree byte_size;
8168 : 1296 : tree field;
8169 : 1296 : int n;
8170 : :
8171 : 1296 : gfc_init_se (&argse, NULL);
8172 : 1296 : arg = expr->value.function.actual->expr;
8173 : :
8174 : 1296 : if (arg->rank || arg->ts.type == BT_ASSUMED)
8175 : 1004 : gfc_conv_expr_descriptor (&argse, arg);
8176 : : else
8177 : 292 : gfc_conv_expr_reference (&argse, arg);
8178 : :
8179 : 1296 : if (arg->ts.type == BT_ASSUMED)
8180 : : {
8181 : : /* This only works if an array descriptor has been passed; thus, extract
8182 : : the size from the descriptor. */
8183 : 164 : gcc_assert (TYPE_PRECISION (gfc_array_index_type)
8184 : : == TYPE_PRECISION (size_type_node));
8185 : 164 : tmp = arg->symtree->n.sym->backend_decl;
8186 : 164 : tmp = DECL_LANG_SPECIFIC (tmp)
8187 : 60 : && GFC_DECL_SAVED_DESCRIPTOR (tmp) != NULL_TREE
8188 : 218 : ? GFC_DECL_SAVED_DESCRIPTOR (tmp) : tmp;
8189 : 164 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
8190 : 164 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8191 : :
8192 : 164 : tmp = gfc_conv_descriptor_dtype (tmp);
8193 : 164 : field = gfc_advance_chain (TYPE_FIELDS (get_dtype_type_node ()),
8194 : : GFC_DTYPE_ELEM_LEN);
8195 : 164 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
8196 : : tmp, field, NULL_TREE);
8197 : :
8198 : 164 : byte_size = fold_convert (gfc_array_index_type, tmp);
8199 : : }
8200 : 1132 : else if (arg->ts.type == BT_CLASS)
8201 : : {
8202 : : /* Conv_expr_descriptor returns a component_ref to _data component of the
8203 : : class object. The class object may be a non-pointer object, e.g.
8204 : : located on the stack, or a memory location pointed to, e.g. a
8205 : : parameter, i.e., an indirect_ref. */
8206 : 954 : if (POINTER_TYPE_P (TREE_TYPE (argse.expr))
8207 : 584 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (argse.expr))))
8208 : 198 : byte_size
8209 : 198 : = gfc_class_vtab_size_get (build_fold_indirect_ref (argse.expr));
8210 : 386 : else if (GFC_CLASS_TYPE_P (TREE_TYPE (argse.expr)))
8211 : 0 : byte_size = gfc_class_vtab_size_get (argse.expr);
8212 : 386 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (argse.expr))
8213 : 386 : && TREE_CODE (argse.expr) == COMPONENT_REF)
8214 : 328 : byte_size = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8215 : 58 : else if (arg->rank > 0
8216 : 16 : || (arg->rank == 0
8217 : 16 : && arg->ref && arg->ref->type == REF_COMPONENT))
8218 : : {
8219 : : /* The scalarizer added an additional temp. To get the class' vptr
8220 : : one has to look at the original backend_decl. */
8221 : 58 : if (argse.class_container)
8222 : 16 : byte_size = gfc_class_vtab_size_get (argse.class_container);
8223 : 42 : else if (DECL_LANG_SPECIFIC (arg->symtree->n.sym->backend_decl))
8224 : 84 : byte_size = gfc_class_vtab_size_get (
8225 : 42 : GFC_DECL_SAVED_DESCRIPTOR (arg->symtree->n.sym->backend_decl));
8226 : : else
8227 : 0 : gcc_unreachable ();
8228 : : }
8229 : : else
8230 : 0 : gcc_unreachable ();
8231 : : }
8232 : : else
8233 : : {
8234 : 548 : if (arg->ts.type == BT_CHARACTER)
8235 : 84 : byte_size = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8236 : : else
8237 : : {
8238 : 464 : if (arg->rank == 0)
8239 : 0 : byte_size = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8240 : : argse.expr));
8241 : : else
8242 : 464 : byte_size = gfc_get_element_type (TREE_TYPE (argse.expr));
8243 : 464 : byte_size = fold_convert (gfc_array_index_type,
8244 : : size_in_bytes (byte_size));
8245 : : }
8246 : : }
8247 : :
8248 : 1296 : if (arg->rank == 0)
8249 : 292 : se->expr = byte_size;
8250 : : else
8251 : : {
8252 : 1004 : source_bytes = gfc_create_var (gfc_array_index_type, "bytes");
8253 : 1004 : gfc_add_modify (&argse.pre, source_bytes, byte_size);
8254 : :
8255 : 1004 : if (arg->rank == -1)
8256 : : {
8257 : 357 : tree cond, loop_var, exit_label;
8258 : 357 : stmtblock_t body;
8259 : :
8260 : 357 : tmp = fold_convert (gfc_array_index_type,
8261 : : gfc_conv_descriptor_rank (argse.expr));
8262 : 357 : loop_var = gfc_create_var (gfc_array_index_type, "i");
8263 : 357 : gfc_add_modify (&argse.pre, loop_var, gfc_index_zero_node);
8264 : 357 : exit_label = gfc_build_label_decl (NULL_TREE);
8265 : :
8266 : : /* Create loop:
8267 : : for (;;)
8268 : : {
8269 : : if (i >= rank)
8270 : : goto exit;
8271 : : source_bytes = source_bytes * array.dim[i].extent;
8272 : : i = i + 1;
8273 : : }
8274 : : exit: */
8275 : 357 : gfc_start_block (&body);
8276 : 357 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
8277 : : loop_var, tmp);
8278 : 357 : tmp = build1_v (GOTO_EXPR, exit_label);
8279 : 357 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
8280 : : cond, tmp, build_empty_stmt (input_location));
8281 : 357 : gfc_add_expr_to_block (&body, tmp);
8282 : :
8283 : 357 : lower = gfc_conv_descriptor_lbound_get (argse.expr, loop_var);
8284 : 357 : upper = gfc_conv_descriptor_ubound_get (argse.expr, loop_var);
8285 : 357 : tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8286 : 357 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8287 : : gfc_array_index_type, tmp, source_bytes);
8288 : 357 : gfc_add_modify (&body, source_bytes, tmp);
8289 : :
8290 : 357 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
8291 : : gfc_array_index_type, loop_var,
8292 : : gfc_index_one_node);
8293 : 357 : gfc_add_modify_loc (input_location, &body, loop_var, tmp);
8294 : :
8295 : 357 : tmp = gfc_finish_block (&body);
8296 : :
8297 : 357 : tmp = fold_build1_loc (input_location, LOOP_EXPR, void_type_node,
8298 : : tmp);
8299 : 357 : gfc_add_expr_to_block (&argse.pre, tmp);
8300 : :
8301 : 357 : tmp = build1_v (LABEL_EXPR, exit_label);
8302 : 357 : gfc_add_expr_to_block (&argse.pre, tmp);
8303 : : }
8304 : : else
8305 : : {
8306 : : /* Obtain the size of the array in bytes. */
8307 : 1834 : for (n = 0; n < arg->rank; n++)
8308 : : {
8309 : 1187 : tree idx;
8310 : 1187 : idx = gfc_rank_cst[n];
8311 : 1187 : lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8312 : 1187 : upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8313 : 1187 : tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8314 : 1187 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8315 : : gfc_array_index_type, tmp, source_bytes);
8316 : 1187 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8317 : : }
8318 : : }
8319 : 1004 : se->expr = source_bytes;
8320 : : }
8321 : :
8322 : 1296 : gfc_add_block_to_block (&se->pre, &argse.pre);
8323 : 1296 : }
8324 : :
8325 : :
8326 : : static void
8327 : 802 : gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
8328 : : {
8329 : 802 : gfc_expr *arg;
8330 : 802 : gfc_se argse;
8331 : 802 : tree type, result_type, tmp, class_decl = NULL;
8332 : 802 : gfc_symbol *sym;
8333 : 802 : bool unlimited = false;
8334 : :
8335 : 802 : arg = expr->value.function.actual->expr;
8336 : :
8337 : 802 : gfc_init_se (&argse, NULL);
8338 : 802 : result_type = gfc_get_int_type (expr->ts.kind);
8339 : :
8340 : 802 : if (arg->rank == 0)
8341 : : {
8342 : 211 : if (arg->ts.type == BT_CLASS)
8343 : : {
8344 : 86 : unlimited = UNLIMITED_POLY (arg);
8345 : 86 : gfc_add_vptr_component (arg);
8346 : 86 : gfc_add_size_component (arg);
8347 : 86 : gfc_conv_expr (&argse, arg);
8348 : 86 : tmp = fold_convert (result_type, argse.expr);
8349 : 86 : class_decl = gfc_get_class_from_expr (argse.expr);
8350 : 86 : goto done;
8351 : : }
8352 : :
8353 : 125 : gfc_conv_expr_reference (&argse, arg);
8354 : 125 : type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8355 : : argse.expr));
8356 : : }
8357 : : else
8358 : : {
8359 : 591 : argse.want_pointer = 0;
8360 : 591 : gfc_conv_expr_descriptor (&argse, arg);
8361 : 591 : sym = arg->expr_type == EXPR_VARIABLE ? arg->symtree->n.sym : NULL;
8362 : 591 : if (arg->ts.type == BT_CLASS)
8363 : : {
8364 : 60 : unlimited = UNLIMITED_POLY (arg);
8365 : 60 : if (TREE_CODE (argse.expr) == COMPONENT_REF)
8366 : 54 : tmp = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8367 : 6 : else if (arg->rank > 0 && sym
8368 : 12 : && DECL_LANG_SPECIFIC (sym->backend_decl))
8369 : 12 : tmp = gfc_class_vtab_size_get (
8370 : 6 : GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl));
8371 : : else
8372 : 0 : gcc_unreachable ();
8373 : 60 : tmp = fold_convert (result_type, tmp);
8374 : 60 : class_decl = gfc_get_class_from_expr (argse.expr);
8375 : 60 : goto done;
8376 : : }
8377 : 531 : type = gfc_get_element_type (TREE_TYPE (argse.expr));
8378 : : }
8379 : :
8380 : : /* Obtain the argument's word length. */
8381 : 656 : if (arg->ts.type == BT_CHARACTER)
8382 : 241 : tmp = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8383 : : else
8384 : 415 : tmp = size_in_bytes (type);
8385 : 656 : tmp = fold_convert (result_type, tmp);
8386 : :
8387 : 802 : done:
8388 : 802 : if (unlimited && class_decl)
8389 : 68 : tmp = gfc_resize_class_size_with_len (NULL, class_decl, tmp);
8390 : :
8391 : 802 : se->expr = fold_build2_loc (input_location, MULT_EXPR, result_type, tmp,
8392 : 802 : build_int_cst (result_type, BITS_PER_UNIT));
8393 : 802 : gfc_add_block_to_block (&se->pre, &argse.pre);
8394 : 802 : }
8395 : :
8396 : :
8397 : : /* Intrinsic string comparison functions. */
8398 : :
8399 : : static void
8400 : 99 : gfc_conv_intrinsic_strcmp (gfc_se * se, gfc_expr * expr, enum tree_code op)
8401 : : {
8402 : 99 : tree args[4];
8403 : :
8404 : 99 : gfc_conv_intrinsic_function_args (se, expr, args, 4);
8405 : :
8406 : 99 : se->expr
8407 : 198 : = gfc_build_compare_string (args[0], args[1], args[2], args[3],
8408 : 99 : expr->value.function.actual->expr->ts.kind,
8409 : : op);
8410 : 99 : se->expr = fold_build2_loc (input_location, op,
8411 : : gfc_typenode_for_spec (&expr->ts), se->expr,
8412 : 99 : build_int_cst (TREE_TYPE (se->expr), 0));
8413 : 99 : }
8414 : :
8415 : : /* Generate a call to the adjustl/adjustr library function. */
8416 : : static void
8417 : 462 : gfc_conv_intrinsic_adjust (gfc_se * se, gfc_expr * expr, tree fndecl)
8418 : : {
8419 : 462 : tree args[3];
8420 : 462 : tree len;
8421 : 462 : tree type;
8422 : 462 : tree var;
8423 : 462 : tree tmp;
8424 : :
8425 : 462 : gfc_conv_intrinsic_function_args (se, expr, &args[1], 2);
8426 : 462 : len = args[1];
8427 : :
8428 : 462 : type = TREE_TYPE (args[2]);
8429 : 462 : var = gfc_conv_string_tmp (se, type, len);
8430 : 462 : args[0] = var;
8431 : :
8432 : 462 : tmp = build_call_expr_loc (input_location,
8433 : : fndecl, 3, args[0], args[1], args[2]);
8434 : 462 : gfc_add_expr_to_block (&se->pre, tmp);
8435 : 462 : se->expr = var;
8436 : 462 : se->string_length = len;
8437 : 462 : }
8438 : :
8439 : :
8440 : : /* Generate code for the TRANSFER intrinsic:
8441 : : For scalar results:
8442 : : DEST = TRANSFER (SOURCE, MOLD)
8443 : : where:
8444 : : typeof<DEST> = typeof<MOLD>
8445 : : and:
8446 : : MOLD is scalar.
8447 : :
8448 : : For array results:
8449 : : DEST(1:N) = TRANSFER (SOURCE, MOLD[, SIZE])
8450 : : where:
8451 : : typeof<DEST> = typeof<MOLD>
8452 : : and:
8453 : : N = min (sizeof (SOURCE(:)), sizeof (DEST(:)),
8454 : : sizeof (DEST(0) * SIZE). */
8455 : : static void
8456 : 3253 : gfc_conv_intrinsic_transfer (gfc_se * se, gfc_expr * expr)
8457 : : {
8458 : 3253 : tree tmp;
8459 : 3253 : tree tmpdecl;
8460 : 3253 : tree ptr;
8461 : 3253 : tree extent;
8462 : 3253 : tree source;
8463 : 3253 : tree source_type;
8464 : 3253 : tree source_bytes;
8465 : 3253 : tree mold_type;
8466 : 3253 : tree dest_word_len;
8467 : 3253 : tree size_words;
8468 : 3253 : tree size_bytes;
8469 : 3253 : tree upper;
8470 : 3253 : tree lower;
8471 : 3253 : tree stmt;
8472 : 3253 : tree class_ref = NULL_TREE;
8473 : 3253 : gfc_actual_arglist *arg;
8474 : 3253 : gfc_se argse;
8475 : 3253 : gfc_array_info *info;
8476 : 3253 : stmtblock_t block;
8477 : 3253 : int n;
8478 : 3253 : bool scalar_mold;
8479 : 3253 : gfc_expr *source_expr, *mold_expr, *class_expr;
8480 : :
8481 : 3253 : info = NULL;
8482 : 3253 : if (se->loop)
8483 : 422 : info = &se->ss->info->data.array;
8484 : :
8485 : : /* Convert SOURCE. The output from this stage is:-
8486 : : source_bytes = length of the source in bytes
8487 : : source = pointer to the source data. */
8488 : 3253 : arg = expr->value.function.actual;
8489 : 3253 : source_expr = arg->expr;
8490 : :
8491 : : /* Ensure double transfer through LOGICAL preserves all
8492 : : the needed bits. */
8493 : 3253 : if (arg->expr->expr_type == EXPR_FUNCTION
8494 : 2325 : && arg->expr->value.function.esym == NULL
8495 : 2307 : && arg->expr->value.function.isym != NULL
8496 : 2307 : && arg->expr->value.function.isym->id == GFC_ISYM_TRANSFER
8497 : 12 : && arg->expr->ts.type == BT_LOGICAL
8498 : 12 : && expr->ts.type != arg->expr->ts.type)
8499 : 12 : arg->expr->value.function.name = "__transfer_in_transfer";
8500 : :
8501 : 3253 : gfc_init_se (&argse, NULL);
8502 : :
8503 : 3253 : source_bytes = gfc_create_var (gfc_array_index_type, NULL);
8504 : :
8505 : : /* Obtain the pointer to source and the length of source in bytes. */
8506 : 3253 : if (arg->expr->rank == 0)
8507 : : {
8508 : 2947 : gfc_conv_expr_reference (&argse, arg->expr);
8509 : 2947 : if (arg->expr->ts.type == BT_CLASS)
8510 : : {
8511 : 31 : tmp = build_fold_indirect_ref_loc (input_location, argse.expr);
8512 : 31 : if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
8513 : : {
8514 : 19 : source = gfc_class_data_get (tmp);
8515 : 19 : class_ref = tmp;
8516 : : }
8517 : : else
8518 : : {
8519 : : /* Array elements are evaluated as a reference to the data.
8520 : : To obtain the vptr for the element size, the argument
8521 : : expression must be stripped to the class reference and
8522 : : re-evaluated. The pre and post blocks are not needed. */
8523 : 12 : gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
8524 : 12 : source = argse.expr;
8525 : 12 : class_expr = gfc_find_and_cut_at_last_class_ref (arg->expr);
8526 : 12 : gfc_init_se (&argse, NULL);
8527 : 12 : gfc_conv_expr (&argse, class_expr);
8528 : 12 : class_ref = argse.expr;
8529 : : }
8530 : : }
8531 : : else
8532 : 2916 : source = argse.expr;
8533 : :
8534 : : /* Obtain the source word length. */
8535 : 2947 : switch (arg->expr->ts.type)
8536 : : {
8537 : 294 : case BT_CHARACTER:
8538 : 294 : tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8539 : : argse.string_length);
8540 : 294 : break;
8541 : 31 : case BT_CLASS:
8542 : 31 : if (class_ref != NULL_TREE)
8543 : : {
8544 : 31 : tmp = gfc_class_vtab_size_get (class_ref);
8545 : 31 : if (UNLIMITED_POLY (source_expr))
8546 : 24 : tmp = gfc_resize_class_size_with_len (NULL, class_ref, tmp);
8547 : : }
8548 : : else
8549 : : {
8550 : 0 : tmp = gfc_class_vtab_size_get (argse.expr);
8551 : 0 : if (UNLIMITED_POLY (source_expr))
8552 : 0 : tmp = gfc_resize_class_size_with_len (NULL, argse.expr, tmp);
8553 : : }
8554 : : break;
8555 : 2622 : default:
8556 : 2622 : source_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8557 : : source));
8558 : 2622 : tmp = fold_convert (gfc_array_index_type,
8559 : : size_in_bytes (source_type));
8560 : 2622 : break;
8561 : : }
8562 : : }
8563 : : else
8564 : : {
8565 : 306 : argse.want_pointer = 0;
8566 : 306 : gfc_conv_expr_descriptor (&argse, arg->expr);
8567 : 306 : source = gfc_conv_descriptor_data_get (argse.expr);
8568 : 306 : source_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8569 : :
8570 : : /* Repack the source if not simply contiguous. */
8571 : 306 : if (!gfc_is_simply_contiguous (arg->expr, false, true))
8572 : : {
8573 : 50 : tmp = gfc_build_addr_expr (NULL_TREE, argse.expr);
8574 : :
8575 : 50 : if (warn_array_temporaries)
8576 : 0 : gfc_warning (OPT_Warray_temporaries,
8577 : : "Creating array temporary at %L", &expr->where);
8578 : :
8579 : 50 : source = build_call_expr_loc (input_location,
8580 : : gfor_fndecl_in_pack, 1, tmp);
8581 : 50 : source = gfc_evaluate_now (source, &argse.pre);
8582 : :
8583 : : /* Free the temporary. */
8584 : 50 : gfc_start_block (&block);
8585 : 50 : tmp = gfc_call_free (source);
8586 : 50 : gfc_add_expr_to_block (&block, tmp);
8587 : 50 : stmt = gfc_finish_block (&block);
8588 : :
8589 : : /* Clean up if it was repacked. */
8590 : 50 : gfc_init_block (&block);
8591 : 50 : tmp = gfc_conv_array_data (argse.expr);
8592 : 50 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
8593 : : source, tmp);
8594 : 50 : tmp = build3_v (COND_EXPR, tmp, stmt,
8595 : : build_empty_stmt (input_location));
8596 : 50 : gfc_add_expr_to_block (&block, tmp);
8597 : 50 : gfc_add_block_to_block (&block, &se->post);
8598 : 50 : gfc_init_block (&se->post);
8599 : 50 : gfc_add_block_to_block (&se->post, &block);
8600 : : }
8601 : :
8602 : : /* Obtain the source word length. */
8603 : 306 : if (arg->expr->ts.type == BT_CHARACTER)
8604 : 144 : tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8605 : : argse.string_length);
8606 : 162 : else if (arg->expr->ts.type == BT_CLASS)
8607 : : {
8608 : 36 : class_ref = TREE_OPERAND (argse.expr, 0);
8609 : 36 : tmp = gfc_class_vtab_size_get (class_ref);
8610 : 36 : if (UNLIMITED_POLY (arg->expr))
8611 : 36 : tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8612 : : }
8613 : : else
8614 : 126 : tmp = fold_convert (gfc_array_index_type,
8615 : : size_in_bytes (source_type));
8616 : :
8617 : : /* Obtain the size of the array in bytes. */
8618 : 306 : extent = gfc_create_var (gfc_array_index_type, NULL);
8619 : 642 : for (n = 0; n < arg->expr->rank; n++)
8620 : : {
8621 : 336 : tree idx;
8622 : 336 : idx = gfc_rank_cst[n];
8623 : 336 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8624 : 336 : lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8625 : 336 : upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8626 : 336 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
8627 : : gfc_array_index_type, upper, lower);
8628 : 336 : gfc_add_modify (&argse.pre, extent, tmp);
8629 : 336 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
8630 : : gfc_array_index_type, extent,
8631 : : gfc_index_one_node);
8632 : 336 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8633 : : gfc_array_index_type, tmp, source_bytes);
8634 : : }
8635 : : }
8636 : :
8637 : 3253 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8638 : 3253 : gfc_add_block_to_block (&se->pre, &argse.pre);
8639 : 3253 : gfc_add_block_to_block (&se->post, &argse.post);
8640 : :
8641 : : /* Now convert MOLD. The outputs are:
8642 : : mold_type = the TREE type of MOLD
8643 : : dest_word_len = destination word length in bytes. */
8644 : 3253 : arg = arg->next;
8645 : 3253 : mold_expr = arg->expr;
8646 : :
8647 : 3253 : gfc_init_se (&argse, NULL);
8648 : :
8649 : 3253 : scalar_mold = arg->expr->rank == 0;
8650 : :
8651 : 3253 : if (arg->expr->rank == 0)
8652 : : {
8653 : 2980 : gfc_conv_expr_reference (&argse, mold_expr);
8654 : 2980 : mold_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8655 : : argse.expr));
8656 : : }
8657 : : else
8658 : : {
8659 : 273 : argse.want_pointer = 0;
8660 : 273 : gfc_conv_expr_descriptor (&argse, mold_expr);
8661 : 273 : mold_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8662 : : }
8663 : :
8664 : 3253 : gfc_add_block_to_block (&se->pre, &argse.pre);
8665 : 3253 : gfc_add_block_to_block (&se->post, &argse.post);
8666 : :
8667 : 3253 : if (strcmp (expr->value.function.name, "__transfer_in_transfer") == 0)
8668 : : {
8669 : : /* If this TRANSFER is nested in another TRANSFER, use a type
8670 : : that preserves all bits. */
8671 : 12 : if (mold_expr->ts.type == BT_LOGICAL)
8672 : 12 : mold_type = gfc_get_int_type (mold_expr->ts.kind);
8673 : : }
8674 : :
8675 : : /* Obtain the destination word length. */
8676 : 3253 : switch (mold_expr->ts.type)
8677 : : {
8678 : 467 : case BT_CHARACTER:
8679 : 467 : tmp = size_of_string_in_bytes (mold_expr->ts.kind, argse.string_length);
8680 : 467 : mold_type = gfc_get_character_type_len (mold_expr->ts.kind,
8681 : : argse.string_length);
8682 : 467 : break;
8683 : 6 : case BT_CLASS:
8684 : 6 : if (scalar_mold)
8685 : 6 : class_ref = argse.expr;
8686 : : else
8687 : 0 : class_ref = TREE_OPERAND (argse.expr, 0);
8688 : 6 : tmp = gfc_class_vtab_size_get (class_ref);
8689 : 6 : if (UNLIMITED_POLY (arg->expr))
8690 : 0 : tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8691 : : break;
8692 : 2780 : default:
8693 : 2780 : tmp = fold_convert (gfc_array_index_type, size_in_bytes (mold_type));
8694 : 2780 : break;
8695 : : }
8696 : :
8697 : : /* Do not fix dest_word_len if it is a variable, since the temporary can wind
8698 : : up being used before the assignment. */
8699 : 3253 : if (mold_expr->ts.type == BT_CHARACTER && mold_expr->ts.deferred)
8700 : : dest_word_len = tmp;
8701 : : else
8702 : : {
8703 : 3199 : dest_word_len = gfc_create_var (gfc_array_index_type, NULL);
8704 : 3199 : gfc_add_modify (&se->pre, dest_word_len, tmp);
8705 : : }
8706 : :
8707 : : /* Finally convert SIZE, if it is present. */
8708 : 3253 : arg = arg->next;
8709 : 3253 : size_words = gfc_create_var (gfc_array_index_type, NULL);
8710 : :
8711 : 3253 : if (arg->expr)
8712 : : {
8713 : 222 : gfc_init_se (&argse, NULL);
8714 : 222 : gfc_conv_expr_reference (&argse, arg->expr);
8715 : 222 : tmp = convert (gfc_array_index_type,
8716 : : build_fold_indirect_ref_loc (input_location,
8717 : : argse.expr));
8718 : 222 : gfc_add_block_to_block (&se->pre, &argse.pre);
8719 : 222 : gfc_add_block_to_block (&se->post, &argse.post);
8720 : : }
8721 : : else
8722 : : tmp = NULL_TREE;
8723 : :
8724 : : /* Separate array and scalar results. */
8725 : 3253 : if (scalar_mold && tmp == NULL_TREE)
8726 : 2831 : goto scalar_transfer;
8727 : :
8728 : 422 : size_bytes = gfc_create_var (gfc_array_index_type, NULL);
8729 : 422 : if (tmp != NULL_TREE)
8730 : 222 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8731 : : tmp, dest_word_len);
8732 : : else
8733 : : tmp = source_bytes;
8734 : :
8735 : 422 : gfc_add_modify (&se->pre, size_bytes, tmp);
8736 : 422 : gfc_add_modify (&se->pre, size_words,
8737 : : fold_build2_loc (input_location, CEIL_DIV_EXPR,
8738 : : gfc_array_index_type,
8739 : : size_bytes, dest_word_len));
8740 : :
8741 : : /* Evaluate the bounds of the result. If the loop range exists, we have
8742 : : to check if it is too large. If so, we modify loop->to be consistent
8743 : : with min(size, size(source)). Otherwise, size is made consistent with
8744 : : the loop range, so that the right number of bytes is transferred.*/
8745 : 422 : n = se->loop->order[0];
8746 : 422 : if (se->loop->to[n] != NULL_TREE)
8747 : : {
8748 : 197 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8749 : : se->loop->to[n], se->loop->from[n]);
8750 : 197 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8751 : : tmp, gfc_index_one_node);
8752 : 197 : tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
8753 : : tmp, size_words);
8754 : 197 : gfc_add_modify (&se->pre, size_words, tmp);
8755 : 197 : gfc_add_modify (&se->pre, size_bytes,
8756 : : fold_build2_loc (input_location, MULT_EXPR,
8757 : : gfc_array_index_type,
8758 : : size_words, dest_word_len));
8759 : 394 : upper = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8760 : 197 : size_words, se->loop->from[n]);
8761 : 197 : upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8762 : : upper, gfc_index_one_node);
8763 : : }
8764 : : else
8765 : : {
8766 : 225 : upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8767 : : size_words, gfc_index_one_node);
8768 : 225 : se->loop->from[n] = gfc_index_zero_node;
8769 : : }
8770 : :
8771 : 422 : se->loop->to[n] = upper;
8772 : :
8773 : : /* Build a destination descriptor, using the pointer, source, as the
8774 : : data field. */
8775 : 422 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss, mold_type,
8776 : : NULL_TREE, false, true, false, &expr->where);
8777 : :
8778 : : /* Cast the pointer to the result. */
8779 : 422 : tmp = gfc_conv_descriptor_data_get (info->descriptor);
8780 : 422 : tmp = fold_convert (pvoid_type_node, tmp);
8781 : :
8782 : : /* Use memcpy to do the transfer. */
8783 : 422 : tmp
8784 : 422 : = build_call_expr_loc (input_location,
8785 : : builtin_decl_explicit (BUILT_IN_MEMCPY), 3, tmp,
8786 : : fold_convert (pvoid_type_node, source),
8787 : : fold_convert (size_type_node,
8788 : : fold_build2_loc (input_location,
8789 : : MIN_EXPR,
8790 : : gfc_array_index_type,
8791 : : size_bytes,
8792 : : source_bytes)));
8793 : 422 : gfc_add_expr_to_block (&se->pre, tmp);
8794 : :
8795 : 422 : se->expr = info->descriptor;
8796 : 422 : if (expr->ts.type == BT_CHARACTER)
8797 : : {
8798 : 275 : tmp = fold_convert (gfc_charlen_type_node,
8799 : : TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
8800 : 275 : se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
8801 : : gfc_charlen_type_node,
8802 : : dest_word_len, tmp);
8803 : : }
8804 : :
8805 : 422 : return;
8806 : :
8807 : : /* Deal with scalar results. */
8808 : 2831 : scalar_transfer:
8809 : 2831 : extent = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
8810 : : dest_word_len, source_bytes);
8811 : 2831 : extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
8812 : : extent, gfc_index_zero_node);
8813 : :
8814 : 2831 : if (expr->ts.type == BT_CHARACTER)
8815 : : {
8816 : 192 : tree direct, indirect, free;
8817 : :
8818 : 192 : ptr = convert (gfc_get_pchar_type (expr->ts.kind), source);
8819 : 192 : tmpdecl = gfc_create_var (gfc_get_pchar_type (expr->ts.kind),
8820 : : "transfer");
8821 : :
8822 : : /* If source is longer than the destination, use a pointer to
8823 : : the source directly. */
8824 : 192 : gfc_init_block (&block);
8825 : 192 : gfc_add_modify (&block, tmpdecl, ptr);
8826 : 192 : direct = gfc_finish_block (&block);
8827 : :
8828 : : /* Otherwise, allocate a string with the length of the destination
8829 : : and copy the source into it. */
8830 : 192 : gfc_init_block (&block);
8831 : 192 : tmp = gfc_get_pchar_type (expr->ts.kind);
8832 : 192 : tmp = gfc_call_malloc (&block, tmp, dest_word_len);
8833 : 192 : gfc_add_modify (&block, tmpdecl,
8834 : 192 : fold_convert (TREE_TYPE (ptr), tmp));
8835 : 192 : tmp = build_call_expr_loc (input_location,
8836 : : builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
8837 : : fold_convert (pvoid_type_node, tmpdecl),
8838 : : fold_convert (pvoid_type_node, ptr),
8839 : : fold_convert (size_type_node, extent));
8840 : 192 : gfc_add_expr_to_block (&block, tmp);
8841 : 192 : indirect = gfc_finish_block (&block);
8842 : :
8843 : : /* Wrap it up with the condition. */
8844 : 192 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
8845 : : dest_word_len, source_bytes);
8846 : 192 : tmp = build3_v (COND_EXPR, tmp, direct, indirect);
8847 : 192 : gfc_add_expr_to_block (&se->pre, tmp);
8848 : :
8849 : : /* Free the temporary string, if necessary. */
8850 : 192 : free = gfc_call_free (tmpdecl);
8851 : 192 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
8852 : : dest_word_len, source_bytes);
8853 : 192 : tmp = build3_v (COND_EXPR, tmp, free, build_empty_stmt (input_location));
8854 : 192 : gfc_add_expr_to_block (&se->post, tmp);
8855 : :
8856 : 192 : se->expr = tmpdecl;
8857 : 192 : tmp = fold_convert (gfc_charlen_type_node,
8858 : : TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
8859 : 192 : se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
8860 : : gfc_charlen_type_node,
8861 : : dest_word_len, tmp);
8862 : : }
8863 : : else
8864 : : {
8865 : 2639 : tmpdecl = gfc_create_var (mold_type, "transfer");
8866 : :
8867 : 2639 : ptr = convert (build_pointer_type (mold_type), source);
8868 : :
8869 : : /* For CLASS results, allocate the needed memory first. */
8870 : 2639 : if (mold_expr->ts.type == BT_CLASS)
8871 : : {
8872 : 6 : tree cdata;
8873 : 6 : cdata = gfc_class_data_get (tmpdecl);
8874 : 6 : tmp = gfc_call_malloc (&se->pre, TREE_TYPE (cdata), dest_word_len);
8875 : 6 : gfc_add_modify (&se->pre, cdata, tmp);
8876 : : }
8877 : :
8878 : : /* Use memcpy to do the transfer. */
8879 : 2639 : if (mold_expr->ts.type == BT_CLASS)
8880 : 6 : tmp = gfc_class_data_get (tmpdecl);
8881 : : else
8882 : 2633 : tmp = gfc_build_addr_expr (NULL_TREE, tmpdecl);
8883 : :
8884 : 2639 : tmp = build_call_expr_loc (input_location,
8885 : : builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
8886 : : fold_convert (pvoid_type_node, tmp),
8887 : : fold_convert (pvoid_type_node, ptr),
8888 : : fold_convert (size_type_node, extent));
8889 : 2639 : gfc_add_expr_to_block (&se->pre, tmp);
8890 : :
8891 : : /* For CLASS results, set the _vptr. */
8892 : 2639 : if (mold_expr->ts.type == BT_CLASS)
8893 : 6 : gfc_reset_vptr (&se->pre, nullptr, tmpdecl, source_expr->ts.u.derived);
8894 : :
8895 : 2639 : se->expr = tmpdecl;
8896 : : }
8897 : : }
8898 : :
8899 : :
8900 : : /* Generate code for the ALLOCATED intrinsic.
8901 : : Generate inline code that directly check the address of the argument. */
8902 : :
8903 : : static void
8904 : 6930 : gfc_conv_allocated (gfc_se *se, gfc_expr *expr)
8905 : : {
8906 : 6930 : gfc_se arg1se;
8907 : 6930 : tree tmp;
8908 : 6930 : gfc_expr *e = expr->value.function.actual->expr;
8909 : :
8910 : 6930 : gfc_init_se (&arg1se, NULL);
8911 : 6930 : if (e->ts.type == BT_CLASS)
8912 : : {
8913 : : /* Make sure that class array expressions have both a _data
8914 : : component reference and an array reference.... */
8915 : 888 : if (CLASS_DATA (e)->attr.dimension)
8916 : 425 : gfc_add_class_array_ref (e);
8917 : : /* .... whilst scalars only need the _data component. */
8918 : : else
8919 : 463 : gfc_add_data_component (e);
8920 : : }
8921 : :
8922 : 6930 : gcc_assert (flag_coarray != GFC_FCOARRAY_LIB || !gfc_is_coindexed (e));
8923 : :
8924 : 6930 : if (e->rank == 0)
8925 : : {
8926 : : /* Allocatable scalar. */
8927 : 2754 : arg1se.want_pointer = 1;
8928 : 2754 : gfc_conv_expr (&arg1se, e);
8929 : 2754 : tmp = arg1se.expr;
8930 : : }
8931 : : else
8932 : : {
8933 : : /* Allocatable array. */
8934 : 4176 : arg1se.descriptor_only = 1;
8935 : 4176 : gfc_conv_expr_descriptor (&arg1se, e);
8936 : 4176 : tmp = gfc_conv_descriptor_data_get (arg1se.expr);
8937 : : }
8938 : :
8939 : 6930 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
8940 : 6930 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
8941 : :
8942 : : /* Components of pointer array references sometimes come back with a pre block. */
8943 : 6930 : if (arg1se.pre.head)
8944 : 6 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
8945 : :
8946 : 6930 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
8947 : 6930 : }
8948 : :
8949 : :
8950 : : /* Generate code for the ASSOCIATED intrinsic.
8951 : : If both POINTER and TARGET are arrays, generate a call to library function
8952 : : _gfor_associated, and pass descriptors of POINTER and TARGET to it.
8953 : : In other cases, generate inline code that directly compare the address of
8954 : : POINTER with the address of TARGET. */
8955 : :
8956 : : static void
8957 : 8810 : gfc_conv_associated (gfc_se *se, gfc_expr *expr)
8958 : : {
8959 : 8810 : gfc_actual_arglist *arg1;
8960 : 8810 : gfc_actual_arglist *arg2;
8961 : 8810 : gfc_se arg1se;
8962 : 8810 : gfc_se arg2se;
8963 : 8810 : tree tmp2;
8964 : 8810 : tree tmp;
8965 : 8810 : tree nonzero_arraylen = NULL_TREE;
8966 : 8810 : gfc_ss *ss;
8967 : 8810 : bool scalar;
8968 : :
8969 : 8810 : gfc_init_se (&arg1se, NULL);
8970 : 8810 : gfc_init_se (&arg2se, NULL);
8971 : 8810 : arg1 = expr->value.function.actual;
8972 : 8810 : arg2 = arg1->next;
8973 : :
8974 : : /* Check whether the expression is a scalar or not; we cannot use
8975 : : arg1->expr->rank as it can be nonzero for proc pointers. */
8976 : 8810 : ss = gfc_walk_expr (arg1->expr);
8977 : 8810 : scalar = ss == gfc_ss_terminator;
8978 : 8810 : if (!scalar)
8979 : 3834 : gfc_free_ss_chain (ss);
8980 : :
8981 : 8810 : if (!arg2->expr)
8982 : : {
8983 : : /* No optional target. */
8984 : 6479 : if (scalar)
8985 : : {
8986 : : /* A pointer to a scalar. */
8987 : 4097 : arg1se.want_pointer = 1;
8988 : 4097 : gfc_conv_expr (&arg1se, arg1->expr);
8989 : 4097 : if (arg1->expr->symtree->n.sym->attr.proc_pointer
8990 : 4097 : && arg1->expr->symtree->n.sym->attr.dummy)
8991 : 78 : arg1se.expr = build_fold_indirect_ref_loc (input_location,
8992 : : arg1se.expr);
8993 : 4097 : if (arg1->expr->ts.type == BT_CLASS)
8994 : : {
8995 : 384 : tmp2 = gfc_class_data_get (arg1se.expr);
8996 : 384 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
8997 : 0 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
8998 : : }
8999 : : else
9000 : 3713 : tmp2 = arg1se.expr;
9001 : : }
9002 : : else
9003 : : {
9004 : : /* A pointer to an array. */
9005 : 2382 : gfc_conv_expr_descriptor (&arg1se, arg1->expr);
9006 : 2382 : tmp2 = gfc_conv_descriptor_data_get (arg1se.expr);
9007 : : }
9008 : 6479 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9009 : 6479 : gfc_add_block_to_block (&se->post, &arg1se.post);
9010 : 6479 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp2,
9011 : 6479 : fold_convert (TREE_TYPE (tmp2), null_pointer_node));
9012 : 6479 : se->expr = tmp;
9013 : : }
9014 : : else
9015 : : {
9016 : : /* An optional target. */
9017 : 2331 : if (arg2->expr->ts.type == BT_CLASS
9018 : 24 : && arg2->expr->expr_type != EXPR_FUNCTION)
9019 : 18 : gfc_add_data_component (arg2->expr);
9020 : :
9021 : 2331 : if (scalar)
9022 : : {
9023 : : /* A pointer to a scalar. */
9024 : 879 : arg1se.want_pointer = 1;
9025 : 879 : gfc_conv_expr (&arg1se, arg1->expr);
9026 : 879 : if (arg1->expr->symtree->n.sym->attr.proc_pointer
9027 : 879 : && arg1->expr->symtree->n.sym->attr.dummy)
9028 : 42 : arg1se.expr = build_fold_indirect_ref_loc (input_location,
9029 : : arg1se.expr);
9030 : 879 : if (arg1->expr->ts.type == BT_CLASS)
9031 : 246 : arg1se.expr = gfc_class_data_get (arg1se.expr);
9032 : :
9033 : 879 : arg2se.want_pointer = 1;
9034 : 879 : gfc_conv_expr (&arg2se, arg2->expr);
9035 : 879 : if (arg2->expr->symtree->n.sym->attr.proc_pointer
9036 : 879 : && arg2->expr->symtree->n.sym->attr.dummy)
9037 : 0 : arg2se.expr = build_fold_indirect_ref_loc (input_location,
9038 : : arg2se.expr);
9039 : 879 : if (arg2->expr->ts.type == BT_CLASS)
9040 : : {
9041 : 6 : arg2se.expr = gfc_evaluate_now (arg2se.expr, &arg2se.pre);
9042 : 6 : arg2se.expr = gfc_class_data_get (arg2se.expr);
9043 : : }
9044 : 879 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9045 : 879 : gfc_add_block_to_block (&se->post, &arg1se.post);
9046 : 879 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9047 : 879 : gfc_add_block_to_block (&se->post, &arg2se.post);
9048 : 879 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9049 : : arg1se.expr, arg2se.expr);
9050 : 879 : tmp2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9051 : : arg1se.expr, null_pointer_node);
9052 : 879 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9053 : : logical_type_node, tmp, tmp2);
9054 : : }
9055 : : else
9056 : : {
9057 : : /* An array pointer of zero length is not associated if target is
9058 : : present. */
9059 : 1452 : arg1se.descriptor_only = 1;
9060 : 1452 : gfc_conv_expr_lhs (&arg1se, arg1->expr);
9061 : 1452 : if (arg1->expr->rank == -1)
9062 : : {
9063 : 84 : tmp = gfc_conv_descriptor_rank (arg1se.expr);
9064 : 168 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9065 : 84 : TREE_TYPE (tmp), tmp,
9066 : 84 : build_int_cst (TREE_TYPE (tmp), 1));
9067 : : }
9068 : : else
9069 : 1368 : tmp = gfc_rank_cst[arg1->expr->rank - 1];
9070 : 1452 : tmp = gfc_conv_descriptor_stride_get (arg1se.expr, tmp);
9071 : 1452 : if (arg2->expr->rank != 0)
9072 : 1422 : nonzero_arraylen = fold_build2_loc (input_location, NE_EXPR,
9073 : : logical_type_node, tmp,
9074 : 1422 : build_int_cst (TREE_TYPE (tmp), 0));
9075 : :
9076 : : /* A pointer to an array, call library function _gfor_associated. */
9077 : 1452 : arg1se.want_pointer = 1;
9078 : 1452 : gfc_conv_expr_descriptor (&arg1se, arg1->expr);
9079 : 1452 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9080 : 1452 : gfc_add_block_to_block (&se->post, &arg1se.post);
9081 : :
9082 : 1452 : arg2se.want_pointer = 1;
9083 : 1452 : arg2se.force_no_tmp = 1;
9084 : 1452 : if (arg2->expr->rank != 0)
9085 : 1422 : gfc_conv_expr_descriptor (&arg2se, arg2->expr);
9086 : : else
9087 : : {
9088 : 30 : gfc_conv_expr (&arg2se, arg2->expr);
9089 : 30 : arg2se.expr
9090 : 30 : = gfc_conv_scalar_to_descriptor (&arg2se, arg2se.expr,
9091 : : gfc_expr_attr (arg2->expr));
9092 : 30 : arg2se.expr = gfc_build_addr_expr (NULL_TREE, arg2se.expr);
9093 : : }
9094 : 1452 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9095 : 1452 : gfc_add_block_to_block (&se->post, &arg2se.post);
9096 : 1452 : se->expr = build_call_expr_loc (input_location,
9097 : : gfor_fndecl_associated, 2,
9098 : : arg1se.expr, arg2se.expr);
9099 : 1452 : se->expr = convert (logical_type_node, se->expr);
9100 : 1452 : if (arg2->expr->rank != 0)
9101 : 1422 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9102 : : logical_type_node, se->expr,
9103 : : nonzero_arraylen);
9104 : : }
9105 : :
9106 : : /* If target is present zero character length pointers cannot
9107 : : be associated. */
9108 : 2331 : if (arg1->expr->ts.type == BT_CHARACTER)
9109 : : {
9110 : 630 : tmp = arg1se.string_length;
9111 : 630 : tmp = fold_build2_loc (input_location, NE_EXPR,
9112 : : logical_type_node, tmp,
9113 : 630 : build_zero_cst (TREE_TYPE (tmp)));
9114 : 630 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9115 : : logical_type_node, se->expr, tmp);
9116 : : }
9117 : : }
9118 : :
9119 : 8810 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9120 : 8810 : }
9121 : :
9122 : :
9123 : : /* Generate code for the SAME_TYPE_AS intrinsic.
9124 : : Generate inline code that directly checks the vindices. */
9125 : :
9126 : : static void
9127 : 403 : gfc_conv_same_type_as (gfc_se *se, gfc_expr *expr)
9128 : : {
9129 : 403 : gfc_expr *a, *b;
9130 : 403 : gfc_se se1, se2;
9131 : 403 : tree tmp;
9132 : 403 : tree conda = NULL_TREE, condb = NULL_TREE;
9133 : :
9134 : 403 : gfc_init_se (&se1, NULL);
9135 : 403 : gfc_init_se (&se2, NULL);
9136 : :
9137 : 403 : a = expr->value.function.actual->expr;
9138 : 403 : b = expr->value.function.actual->next->expr;
9139 : :
9140 : 403 : bool unlimited_poly_a = UNLIMITED_POLY (a);
9141 : 403 : bool unlimited_poly_b = UNLIMITED_POLY (b);
9142 : 403 : if (unlimited_poly_a)
9143 : : {
9144 : 105 : se1.want_pointer = 1;
9145 : 105 : gfc_add_vptr_component (a);
9146 : : }
9147 : 298 : else if (a->ts.type == BT_CLASS)
9148 : : {
9149 : 256 : gfc_add_vptr_component (a);
9150 : 256 : gfc_add_hash_component (a);
9151 : : }
9152 : 42 : else if (a->ts.type == BT_DERIVED)
9153 : 42 : a = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9154 : 42 : a->ts.u.derived->hash_value);
9155 : :
9156 : 403 : if (unlimited_poly_b)
9157 : : {
9158 : 66 : se2.want_pointer = 1;
9159 : 66 : gfc_add_vptr_component (b);
9160 : : }
9161 : 337 : else if (b->ts.type == BT_CLASS)
9162 : : {
9163 : 169 : gfc_add_vptr_component (b);
9164 : 169 : gfc_add_hash_component (b);
9165 : : }
9166 : 168 : else if (b->ts.type == BT_DERIVED)
9167 : 168 : b = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9168 : 168 : b->ts.u.derived->hash_value);
9169 : :
9170 : 403 : gfc_conv_expr (&se1, a);
9171 : 403 : gfc_conv_expr (&se2, b);
9172 : :
9173 : 403 : if (unlimited_poly_a)
9174 : : {
9175 : 105 : conda = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9176 : : se1.expr,
9177 : 105 : build_int_cst (TREE_TYPE (se1.expr), 0));
9178 : 105 : se1.expr = gfc_vptr_hash_get (se1.expr);
9179 : : }
9180 : :
9181 : 403 : if (unlimited_poly_b)
9182 : : {
9183 : 66 : condb = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9184 : : se2.expr,
9185 : 66 : build_int_cst (TREE_TYPE (se2.expr), 0));
9186 : 66 : se2.expr = gfc_vptr_hash_get (se2.expr);
9187 : : }
9188 : :
9189 : 403 : tmp = fold_build2_loc (input_location, EQ_EXPR,
9190 : : logical_type_node, se1.expr,
9191 : 403 : fold_convert (TREE_TYPE (se1.expr), se2.expr));
9192 : :
9193 : 403 : if (conda)
9194 : 105 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9195 : : logical_type_node, conda, tmp);
9196 : :
9197 : 403 : if (condb)
9198 : 66 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9199 : : logical_type_node, condb, tmp);
9200 : :
9201 : 403 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
9202 : 403 : }
9203 : :
9204 : :
9205 : : /* Generate code for SELECTED_CHAR_KIND (NAME) intrinsic function. */
9206 : :
9207 : : static void
9208 : 42 : gfc_conv_intrinsic_sc_kind (gfc_se *se, gfc_expr *expr)
9209 : : {
9210 : 42 : tree args[2];
9211 : :
9212 : 42 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
9213 : 42 : se->expr = build_call_expr_loc (input_location,
9214 : : gfor_fndecl_sc_kind, 2, args[0], args[1]);
9215 : 42 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9216 : 42 : }
9217 : :
9218 : :
9219 : : /* Generate code for SELECTED_INT_KIND (R) intrinsic function. */
9220 : :
9221 : : static void
9222 : 45 : gfc_conv_intrinsic_si_kind (gfc_se *se, gfc_expr *expr)
9223 : : {
9224 : 45 : tree arg, type;
9225 : :
9226 : 45 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9227 : :
9228 : : /* The argument to SELECTED_INT_KIND is INTEGER(4). */
9229 : 45 : type = gfc_get_int_type (4);
9230 : 45 : arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9231 : :
9232 : : /* Convert it to the required type. */
9233 : 45 : type = gfc_typenode_for_spec (&expr->ts);
9234 : 45 : se->expr = build_call_expr_loc (input_location,
9235 : : gfor_fndecl_si_kind, 1, arg);
9236 : 45 : se->expr = fold_convert (type, se->expr);
9237 : 45 : }
9238 : :
9239 : :
9240 : : /* Generate code for SELECTED_LOGICAL_KIND (BITS) intrinsic function. */
9241 : :
9242 : : static void
9243 : 6 : gfc_conv_intrinsic_sl_kind (gfc_se *se, gfc_expr *expr)
9244 : : {
9245 : 6 : tree arg, type;
9246 : :
9247 : 6 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9248 : :
9249 : : /* The argument to SELECTED_LOGICAL_KIND is INTEGER(4). */
9250 : 6 : type = gfc_get_int_type (4);
9251 : 6 : arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9252 : :
9253 : : /* Convert it to the required type. */
9254 : 6 : type = gfc_typenode_for_spec (&expr->ts);
9255 : 6 : se->expr = build_call_expr_loc (input_location,
9256 : : gfor_fndecl_sl_kind, 1, arg);
9257 : 6 : se->expr = fold_convert (type, se->expr);
9258 : 6 : }
9259 : :
9260 : :
9261 : : /* Generate code for SELECTED_REAL_KIND (P, R, RADIX) intrinsic function. */
9262 : :
9263 : : static void
9264 : 82 : gfc_conv_intrinsic_sr_kind (gfc_se *se, gfc_expr *expr)
9265 : : {
9266 : 82 : gfc_actual_arglist *actual;
9267 : 82 : tree type;
9268 : 82 : gfc_se argse;
9269 : 82 : vec<tree, va_gc> *args = NULL;
9270 : :
9271 : 328 : for (actual = expr->value.function.actual; actual; actual = actual->next)
9272 : : {
9273 : 246 : gfc_init_se (&argse, se);
9274 : :
9275 : : /* Pass a NULL pointer for an absent arg. */
9276 : 246 : if (actual->expr == NULL)
9277 : 96 : argse.expr = null_pointer_node;
9278 : : else
9279 : : {
9280 : 150 : gfc_typespec ts;
9281 : 150 : gfc_clear_ts (&ts);
9282 : :
9283 : 150 : if (actual->expr->ts.kind != gfc_c_int_kind)
9284 : : {
9285 : : /* The arguments to SELECTED_REAL_KIND are INTEGER(4). */
9286 : 0 : ts.type = BT_INTEGER;
9287 : 0 : ts.kind = gfc_c_int_kind;
9288 : 0 : gfc_convert_type (actual->expr, &ts, 2);
9289 : : }
9290 : 150 : gfc_conv_expr_reference (&argse, actual->expr);
9291 : : }
9292 : :
9293 : 246 : gfc_add_block_to_block (&se->pre, &argse.pre);
9294 : 246 : gfc_add_block_to_block (&se->post, &argse.post);
9295 : 246 : vec_safe_push (args, argse.expr);
9296 : : }
9297 : :
9298 : : /* Convert it to the required type. */
9299 : 82 : type = gfc_typenode_for_spec (&expr->ts);
9300 : 82 : se->expr = build_call_expr_loc_vec (input_location,
9301 : : gfor_fndecl_sr_kind, args);
9302 : 82 : se->expr = fold_convert (type, se->expr);
9303 : 82 : }
9304 : :
9305 : :
9306 : : /* Generate code for TRIM (A) intrinsic function. */
9307 : :
9308 : : static void
9309 : 567 : gfc_conv_intrinsic_trim (gfc_se * se, gfc_expr * expr)
9310 : : {
9311 : 567 : tree var;
9312 : 567 : tree len;
9313 : 567 : tree addr;
9314 : 567 : tree tmp;
9315 : 567 : tree cond;
9316 : 567 : tree fndecl;
9317 : 567 : tree function;
9318 : 567 : tree *args;
9319 : 567 : unsigned int num_args;
9320 : :
9321 : 567 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
9322 : 567 : args = XALLOCAVEC (tree, num_args);
9323 : :
9324 : 567 : var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
9325 : 567 : addr = gfc_build_addr_expr (ppvoid_type_node, var);
9326 : 567 : len = gfc_create_var (gfc_charlen_type_node, "len");
9327 : :
9328 : 567 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
9329 : 567 : args[0] = gfc_build_addr_expr (NULL_TREE, len);
9330 : 567 : args[1] = addr;
9331 : :
9332 : 567 : if (expr->ts.kind == 1)
9333 : 535 : function = gfor_fndecl_string_trim;
9334 : 32 : else if (expr->ts.kind == 4)
9335 : 32 : function = gfor_fndecl_string_trim_char4;
9336 : : else
9337 : 0 : gcc_unreachable ();
9338 : :
9339 : 567 : fndecl = build_addr (function);
9340 : 567 : tmp = build_call_array_loc (input_location,
9341 : 567 : TREE_TYPE (TREE_TYPE (function)), fndecl,
9342 : : num_args, args);
9343 : 567 : gfc_add_expr_to_block (&se->pre, tmp);
9344 : :
9345 : : /* Free the temporary afterwards, if necessary. */
9346 : 567 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9347 : 567 : len, build_int_cst (TREE_TYPE (len), 0));
9348 : 567 : tmp = gfc_call_free (var);
9349 : 567 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
9350 : 567 : gfc_add_expr_to_block (&se->post, tmp);
9351 : :
9352 : 567 : se->expr = var;
9353 : 567 : se->string_length = len;
9354 : 567 : }
9355 : :
9356 : :
9357 : : /* Generate code for REPEAT (STRING, NCOPIES) intrinsic function. */
9358 : :
9359 : : static void
9360 : 529 : gfc_conv_intrinsic_repeat (gfc_se * se, gfc_expr * expr)
9361 : : {
9362 : 529 : tree args[3], ncopies, dest, dlen, src, slen, ncopies_type;
9363 : 529 : tree type, cond, tmp, count, exit_label, n, max, largest;
9364 : 529 : tree size;
9365 : 529 : stmtblock_t block, body;
9366 : 529 : int i;
9367 : :
9368 : : /* We store in charsize the size of a character. */
9369 : 529 : i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
9370 : 529 : size = build_int_cst (sizetype, gfc_character_kinds[i].bit_size / 8);
9371 : :
9372 : : /* Get the arguments. */
9373 : 529 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
9374 : 529 : slen = fold_convert (sizetype, gfc_evaluate_now (args[0], &se->pre));
9375 : 529 : src = args[1];
9376 : 529 : ncopies = gfc_evaluate_now (args[2], &se->pre);
9377 : 529 : ncopies_type = TREE_TYPE (ncopies);
9378 : :
9379 : : /* Check that NCOPIES is not negative. */
9380 : 529 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, ncopies,
9381 : 529 : build_int_cst (ncopies_type, 0));
9382 : 529 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9383 : : "Argument NCOPIES of REPEAT intrinsic is negative "
9384 : : "(its value is %ld)",
9385 : : fold_convert (long_integer_type_node, ncopies));
9386 : :
9387 : : /* If the source length is zero, any non negative value of NCOPIES
9388 : : is valid, and nothing happens. */
9389 : 529 : n = gfc_create_var (ncopies_type, "ncopies");
9390 : 529 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9391 : : size_zero_node);
9392 : 529 : tmp = fold_build3_loc (input_location, COND_EXPR, ncopies_type, cond,
9393 : 529 : build_int_cst (ncopies_type, 0), ncopies);
9394 : 529 : gfc_add_modify (&se->pre, n, tmp);
9395 : 529 : ncopies = n;
9396 : :
9397 : : /* Check that ncopies is not too large: ncopies should be less than
9398 : : (or equal to) MAX / slen, where MAX is the maximal integer of
9399 : : the gfc_charlen_type_node type. If slen == 0, we need a special
9400 : : case to avoid the division by zero. */
9401 : 529 : max = fold_build2_loc (input_location, TRUNC_DIV_EXPR, sizetype,
9402 : 529 : fold_convert (sizetype,
9403 : : TYPE_MAX_VALUE (gfc_charlen_type_node)),
9404 : : slen);
9405 : 529 : largest = TYPE_PRECISION (sizetype) > TYPE_PRECISION (ncopies_type)
9406 : 529 : ? sizetype : ncopies_type;
9407 : 529 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9408 : : fold_convert (largest, ncopies),
9409 : : fold_convert (largest, max));
9410 : 529 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9411 : : size_zero_node);
9412 : 529 : cond = fold_build3_loc (input_location, COND_EXPR, logical_type_node, tmp,
9413 : : logical_false_node, cond);
9414 : 529 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9415 : : "Argument NCOPIES of REPEAT intrinsic is too large");
9416 : :
9417 : : /* Compute the destination length. */
9418 : 529 : dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
9419 : : fold_convert (gfc_charlen_type_node, slen),
9420 : : fold_convert (gfc_charlen_type_node, ncopies));
9421 : 529 : type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
9422 : 529 : dest = gfc_conv_string_tmp (se, build_pointer_type (type), dlen);
9423 : :
9424 : : /* Generate the code to do the repeat operation:
9425 : : for (i = 0; i < ncopies; i++)
9426 : : memmove (dest + (i * slen * size), src, slen*size); */
9427 : 529 : gfc_start_block (&block);
9428 : 529 : count = gfc_create_var (sizetype, "count");
9429 : 529 : gfc_add_modify (&block, count, size_zero_node);
9430 : 529 : exit_label = gfc_build_label_decl (NULL_TREE);
9431 : :
9432 : : /* Start the loop body. */
9433 : 529 : gfc_start_block (&body);
9434 : :
9435 : : /* Exit the loop if count >= ncopies. */
9436 : 529 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, count,
9437 : : fold_convert (sizetype, ncopies));
9438 : 529 : tmp = build1_v (GOTO_EXPR, exit_label);
9439 : 529 : TREE_USED (exit_label) = 1;
9440 : 529 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9441 : : build_empty_stmt (input_location));
9442 : 529 : gfc_add_expr_to_block (&body, tmp);
9443 : :
9444 : : /* Call memmove (dest + (i*slen*size), src, slen*size). */
9445 : 529 : tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, slen,
9446 : : count);
9447 : 529 : tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, tmp,
9448 : : size);
9449 : 529 : tmp = fold_build_pointer_plus_loc (input_location,
9450 : : fold_convert (pvoid_type_node, dest), tmp);
9451 : 529 : tmp = build_call_expr_loc (input_location,
9452 : : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9453 : : 3, tmp, src,
9454 : : fold_build2_loc (input_location, MULT_EXPR,
9455 : : size_type_node, slen, size));
9456 : 529 : gfc_add_expr_to_block (&body, tmp);
9457 : :
9458 : : /* Increment count. */
9459 : 529 : tmp = fold_build2_loc (input_location, PLUS_EXPR, sizetype,
9460 : : count, size_one_node);
9461 : 529 : gfc_add_modify (&body, count, tmp);
9462 : :
9463 : : /* Build the loop. */
9464 : 529 : tmp = build1_v (LOOP_EXPR, gfc_finish_block (&body));
9465 : 529 : gfc_add_expr_to_block (&block, tmp);
9466 : :
9467 : : /* Add the exit label. */
9468 : 529 : tmp = build1_v (LABEL_EXPR, exit_label);
9469 : 529 : gfc_add_expr_to_block (&block, tmp);
9470 : :
9471 : : /* Finish the block. */
9472 : 529 : tmp = gfc_finish_block (&block);
9473 : 529 : gfc_add_expr_to_block (&se->pre, tmp);
9474 : :
9475 : : /* Set the result value. */
9476 : 529 : se->expr = dest;
9477 : 529 : se->string_length = dlen;
9478 : 529 : }
9479 : :
9480 : :
9481 : : /* Generate code for the IARGC intrinsic. */
9482 : :
9483 : : static void
9484 : 12 : gfc_conv_intrinsic_iargc (gfc_se * se, gfc_expr * expr)
9485 : : {
9486 : 12 : tree tmp;
9487 : 12 : tree fndecl;
9488 : 12 : tree type;
9489 : :
9490 : : /* Call the library function. This always returns an INTEGER(4). */
9491 : 12 : fndecl = gfor_fndecl_iargc;
9492 : 12 : tmp = build_call_expr_loc (input_location,
9493 : : fndecl, 0);
9494 : :
9495 : : /* Convert it to the required type. */
9496 : 12 : type = gfc_typenode_for_spec (&expr->ts);
9497 : 12 : tmp = fold_convert (type, tmp);
9498 : :
9499 : 12 : se->expr = tmp;
9500 : 12 : }
9501 : :
9502 : :
9503 : : /* Generate code for the KILL intrinsic. */
9504 : :
9505 : : static void
9506 : 8 : conv_intrinsic_kill (gfc_se *se, gfc_expr *expr)
9507 : : {
9508 : 8 : tree *args;
9509 : 8 : tree int4_type_node = gfc_get_int_type (4);
9510 : 8 : tree pid;
9511 : 8 : tree sig;
9512 : 8 : tree tmp;
9513 : 8 : unsigned int num_args;
9514 : :
9515 : 8 : num_args = gfc_intrinsic_argument_list_length (expr);
9516 : 8 : args = XALLOCAVEC (tree, num_args);
9517 : 8 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
9518 : :
9519 : : /* Convert PID to a INTEGER(4) entity. */
9520 : 8 : pid = convert (int4_type_node, args[0]);
9521 : :
9522 : : /* Convert SIG to a INTEGER(4) entity. */
9523 : 8 : sig = convert (int4_type_node, args[1]);
9524 : :
9525 : 8 : tmp = build_call_expr_loc (input_location, gfor_fndecl_kill, 2, pid, sig);
9526 : :
9527 : 8 : se->expr = fold_convert (TREE_TYPE (args[0]), tmp);
9528 : 8 : }
9529 : :
9530 : :
9531 : : static tree
9532 : 15 : conv_intrinsic_kill_sub (gfc_code *code)
9533 : : {
9534 : 15 : stmtblock_t block;
9535 : 15 : gfc_se se, se_stat;
9536 : 15 : tree int4_type_node = gfc_get_int_type (4);
9537 : 15 : tree pid;
9538 : 15 : tree sig;
9539 : 15 : tree statp;
9540 : 15 : tree tmp;
9541 : :
9542 : : /* Make the function call. */
9543 : 15 : gfc_init_block (&block);
9544 : 15 : gfc_init_se (&se, NULL);
9545 : :
9546 : : /* Convert PID to a INTEGER(4) entity. */
9547 : 15 : gfc_conv_expr (&se, code->ext.actual->expr);
9548 : 15 : gfc_add_block_to_block (&block, &se.pre);
9549 : 15 : pid = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9550 : 15 : gfc_add_block_to_block (&block, &se.post);
9551 : :
9552 : : /* Convert SIG to a INTEGER(4) entity. */
9553 : 15 : gfc_conv_expr (&se, code->ext.actual->next->expr);
9554 : 15 : gfc_add_block_to_block (&block, &se.pre);
9555 : 15 : sig = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9556 : 15 : gfc_add_block_to_block (&block, &se.post);
9557 : :
9558 : : /* Deal with an optional STATUS. */
9559 : 15 : if (code->ext.actual->next->next->expr)
9560 : : {
9561 : 10 : gfc_init_se (&se_stat, NULL);
9562 : 10 : gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
9563 : 10 : statp = gfc_create_var (gfc_get_int_type (4), "_statp");
9564 : : }
9565 : : else
9566 : : statp = NULL_TREE;
9567 : :
9568 : 25 : tmp = build_call_expr_loc (input_location, gfor_fndecl_kill_sub, 3, pid, sig,
9569 : 10 : statp ? gfc_build_addr_expr (NULL_TREE, statp) : null_pointer_node);
9570 : :
9571 : 15 : gfc_add_expr_to_block (&block, tmp);
9572 : :
9573 : 15 : if (statp && statp != se_stat.expr)
9574 : 10 : gfc_add_modify (&block, se_stat.expr,
9575 : 10 : fold_convert (TREE_TYPE (se_stat.expr), statp));
9576 : :
9577 : 15 : return gfc_finish_block (&block);
9578 : : }
9579 : :
9580 : :
9581 : :
9582 : : /* The loc intrinsic returns the address of its argument as
9583 : : gfc_index_integer_kind integer. */
9584 : :
9585 : : static void
9586 : 8565 : gfc_conv_intrinsic_loc (gfc_se * se, gfc_expr * expr)
9587 : : {
9588 : 8565 : tree temp_var;
9589 : 8565 : gfc_expr *arg_expr;
9590 : :
9591 : 8565 : gcc_assert (!se->ss);
9592 : :
9593 : 8565 : arg_expr = expr->value.function.actual->expr;
9594 : 8565 : if (arg_expr->rank == 0)
9595 : : {
9596 : 6169 : if (arg_expr->ts.type == BT_CLASS)
9597 : 18 : gfc_add_data_component (arg_expr);
9598 : 6169 : gfc_conv_expr_reference (se, arg_expr);
9599 : : }
9600 : : else
9601 : 2396 : gfc_conv_array_parameter (se, arg_expr, true, NULL, NULL, NULL);
9602 : 8565 : se->expr = convert (gfc_get_int_type (gfc_index_integer_kind), se->expr);
9603 : :
9604 : : /* Create a temporary variable for loc return value. Without this,
9605 : : we get an error an ICE in gcc/expr.cc(expand_expr_addr_expr_1). */
9606 : 8565 : temp_var = gfc_create_var (gfc_get_int_type (gfc_index_integer_kind), NULL);
9607 : 8565 : gfc_add_modify (&se->pre, temp_var, se->expr);
9608 : 8565 : se->expr = temp_var;
9609 : 8565 : }
9610 : :
9611 : :
9612 : : /* Specialized trim for f_c_string. */
9613 : :
9614 : : static void
9615 : 42 : conv_trim (gfc_se *tse, gfc_se *str)
9616 : : {
9617 : 42 : tree cond, plen, pvar, tlen, ttmp, tvar;
9618 : :
9619 : 42 : tlen = gfc_create_var (gfc_charlen_type_node, "tlen");
9620 : 42 : plen = gfc_build_addr_expr (NULL_TREE, tlen);
9621 : :
9622 : 42 : tvar = gfc_create_var (pchar_type_node, "tstr");
9623 : 42 : pvar = gfc_build_addr_expr (ppvoid_type_node, tvar);
9624 : :
9625 : 42 : ttmp = build_call_expr_loc (input_location, gfor_fndecl_string_trim, 4,
9626 : : plen, pvar, str->string_length, str->expr);
9627 : :
9628 : 42 : gfc_add_expr_to_block (&tse->pre, ttmp);
9629 : :
9630 : : /* Free the temporary afterwards, if necessary. */
9631 : 42 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9632 : 42 : tlen, build_int_cst (TREE_TYPE (tlen), 0));
9633 : 42 : ttmp = gfc_call_free (tvar);
9634 : 42 : ttmp = build3_v (COND_EXPR, cond, ttmp, build_empty_stmt (input_location));
9635 : 42 : gfc_add_expr_to_block (&tse->post, ttmp);
9636 : :
9637 : 42 : tse->expr = tvar;
9638 : 42 : tse->string_length = tlen;
9639 : 42 : }
9640 : :
9641 : :
9642 : : /* The following routine generates code for the intrinsic functions from
9643 : : the ISO_C_BINDING module: C_LOC, C_FUNLOC, C_ASSOCIATED, and
9644 : : F_C_STRING. */
9645 : :
9646 : : static void
9647 : 8906 : conv_isocbinding_function (gfc_se *se, gfc_expr *expr)
9648 : : {
9649 : 8906 : gfc_actual_arglist *arg = expr->value.function.actual;
9650 : :
9651 : 8906 : if (expr->value.function.isym->id == GFC_ISYM_C_LOC)
9652 : : {
9653 : 6615 : if (arg->expr->rank == 0)
9654 : 1901 : gfc_conv_expr_reference (se, arg->expr);
9655 : 4714 : else if (gfc_is_simply_contiguous (arg->expr, false, false))
9656 : 3678 : gfc_conv_array_parameter (se, arg->expr, true, NULL, NULL, NULL);
9657 : : else
9658 : : {
9659 : 1036 : gfc_conv_expr_descriptor (se, arg->expr);
9660 : 1036 : se->expr = gfc_conv_descriptor_data_get (se->expr);
9661 : : }
9662 : :
9663 : : /* TODO -- the following two lines shouldn't be necessary, but if
9664 : : they're removed, a bug is exposed later in the code path.
9665 : : This workaround was thus introduced, but will have to be
9666 : : removed; please see PR 35150 for details about the issue. */
9667 : 6615 : se->expr = convert (pvoid_type_node, se->expr);
9668 : 6615 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
9669 : : }
9670 : 2291 : else if (expr->value.function.isym->id == GFC_ISYM_C_FUNLOC)
9671 : 231 : gfc_conv_expr_reference (se, arg->expr);
9672 : 2060 : else if (expr->value.function.isym->id == GFC_ISYM_C_ASSOCIATED)
9673 : : {
9674 : 2018 : gfc_se arg1se;
9675 : 2018 : gfc_se arg2se;
9676 : :
9677 : : /* Build the addr_expr for the first argument. The argument is
9678 : : already an *address* so we don't need to set want_pointer in
9679 : : the gfc_se. */
9680 : 2018 : gfc_init_se (&arg1se, NULL);
9681 : 2018 : gfc_conv_expr (&arg1se, arg->expr);
9682 : 2018 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9683 : 2018 : gfc_add_block_to_block (&se->post, &arg1se.post);
9684 : :
9685 : : /* See if we were given two arguments. */
9686 : 2018 : if (arg->next->expr == NULL)
9687 : : /* Only given one arg so generate a null and do a
9688 : : not-equal comparison against the first arg. */
9689 : 1675 : se->expr = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9690 : : arg1se.expr,
9691 : 1675 : fold_convert (TREE_TYPE (arg1se.expr),
9692 : : null_pointer_node));
9693 : : else
9694 : : {
9695 : 343 : tree eq_expr;
9696 : 343 : tree not_null_expr;
9697 : :
9698 : : /* Given two arguments so build the arg2se from second arg. */
9699 : 343 : gfc_init_se (&arg2se, NULL);
9700 : 343 : gfc_conv_expr (&arg2se, arg->next->expr);
9701 : 343 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9702 : 343 : gfc_add_block_to_block (&se->post, &arg2se.post);
9703 : :
9704 : : /* Generate test to compare that the two args are equal. */
9705 : 343 : eq_expr = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9706 : : arg1se.expr, arg2se.expr);
9707 : : /* Generate test to ensure that the first arg is not null. */
9708 : 343 : not_null_expr = fold_build2_loc (input_location, NE_EXPR,
9709 : : logical_type_node,
9710 : : arg1se.expr, null_pointer_node);
9711 : :
9712 : : /* Finally, the generated test must check that both arg1 is not
9713 : : NULL and that it is equal to the second arg. */
9714 : 343 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9715 : : logical_type_node,
9716 : : not_null_expr, eq_expr);
9717 : : }
9718 : : }
9719 : 42 : else if (expr->value.function.isym->id == GFC_ISYM_F_C_STRING)
9720 : : {
9721 : : /* There are three cases:
9722 : : f_c_string(string) -> trim(string) // c_null_char
9723 : : f_c_string(string, .false.) -> trim(string) // c_null_char
9724 : : f_c_string(string, .true.) -> string // c_null_char */
9725 : :
9726 : 42 : gfc_se lse, rse, tse;
9727 : 42 : tree len, tmp, var;
9728 : 42 : gfc_expr *string = arg->expr;
9729 : 42 : gfc_expr *asis = arg->next->expr;
9730 : 42 : gfc_expr *cnc;
9731 : :
9732 : : /* Convert string. */
9733 : 42 : gfc_init_se (&lse, se);
9734 : 42 : gfc_conv_expr (&lse, string);
9735 : 42 : gfc_conv_string_parameter (&lse);
9736 : :
9737 : : /* Create a string for C_NULL_CHAR and convert it. */
9738 : 42 : cnc = gfc_get_character_expr (gfc_default_character_kind,
9739 : : &string->where, "\0", 1);
9740 : 42 : gfc_init_se (&rse, se);
9741 : 42 : gfc_conv_expr (&rse, cnc);
9742 : 42 : gfc_conv_string_parameter (&rse);
9743 : 42 : gfc_free_expr (cnc);
9744 : :
9745 : : #ifdef cnode
9746 : : #undef cnode
9747 : : #endif
9748 : : #define cnode gfc_charlen_type_node
9749 : 42 : if (asis)
9750 : : {
9751 : 30 : stmtblock_t block;
9752 : 30 : gfc_se asis_se, vse;
9753 : 30 : tree elen, evar, tlen, tvar;
9754 : 30 : tree else_branch, then_branch;
9755 : :
9756 : 30 : elen = evar = tlen = tvar = NULL_TREE;
9757 : :
9758 : : /* f_c_string(string, .true.) -> string // c_null_char */
9759 : :
9760 : 30 : gfc_init_block (&block);
9761 : :
9762 : 30 : gfc_add_block_to_block (&block, &lse.pre);
9763 : 30 : gfc_add_block_to_block (&block, &rse.pre);
9764 : :
9765 : 30 : tlen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9766 : : fold_convert (cnode, lse.string_length),
9767 : : fold_convert (cnode, rse.string_length));
9768 : :
9769 : 30 : gfc_init_se (&vse, se);
9770 : 30 : tvar = gfc_conv_string_tmp (&vse, pchar_type_node, tlen);
9771 : 30 : gfc_add_block_to_block (&block, &vse.pre);
9772 : :
9773 : 30 : tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9774 : : 6, tlen, tvar,
9775 : : lse.string_length, lse.expr,
9776 : : rse.string_length, rse.expr);
9777 : 30 : gfc_add_expr_to_block (&block, tmp);
9778 : :
9779 : 30 : then_branch = gfc_finish_block (&block);
9780 : :
9781 : : /* f_c_string(string, .false.) = trim(string) // c_null_char */
9782 : :
9783 : 30 : gfc_init_block (&block);
9784 : :
9785 : 30 : gfc_init_se (&tse, se);
9786 : 30 : conv_trim (&tse, &lse);
9787 : 30 : gfc_add_block_to_block (&block, &tse.pre);
9788 : 30 : gfc_add_block_to_block (&block, &rse.pre);
9789 : :
9790 : 30 : elen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9791 : : fold_convert (cnode, tse.string_length),
9792 : : fold_convert (cnode, rse.string_length));
9793 : :
9794 : 30 : gfc_init_se (&vse, se);
9795 : 30 : evar = gfc_conv_string_tmp (&vse, pchar_type_node, elen);
9796 : 30 : gfc_add_block_to_block (&block, &vse.pre);
9797 : :
9798 : 30 : tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9799 : : 6, elen, evar,
9800 : : tse.string_length, tse.expr,
9801 : : rse.string_length, rse.expr);
9802 : 30 : gfc_add_expr_to_block (&block, tmp);
9803 : :
9804 : 30 : else_branch = gfc_finish_block (&block);
9805 : :
9806 : 30 : gfc_init_se (&asis_se, se);
9807 : 30 : gfc_conv_expr (&asis_se, asis);
9808 : 30 : if (asis->expr_type == EXPR_VARIABLE
9809 : 18 : && asis->symtree->n.sym->attr.dummy
9810 : 18 : && asis->symtree->n.sym->attr.optional)
9811 : : {
9812 : 6 : tree present = gfc_conv_expr_present (asis->symtree->n.sym);
9813 : 6 : asis_se.expr = build3_loc (input_location, COND_EXPR,
9814 : : logical_type_node, present,
9815 : : asis_se.expr,
9816 : 6 : build_int_cst (logical_type_node, 0));
9817 : : }
9818 : 30 : gfc_add_block_to_block (&se->pre, &asis_se.pre);
9819 : 30 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
9820 : : asis_se.expr, then_branch, else_branch);
9821 : :
9822 : 30 : gfc_add_expr_to_block (&se->pre, tmp);
9823 : :
9824 : 30 : var = fold_build3_loc (input_location, COND_EXPR, pchar_type_node,
9825 : : asis_se.expr, tvar, evar);
9826 : 30 : gfc_add_expr_to_block (&se->pre, var);
9827 : :
9828 : 30 : len = fold_build3_loc (input_location, COND_EXPR, cnode,
9829 : : asis_se.expr, tlen, elen);
9830 : 30 : gfc_add_expr_to_block (&se->pre, len);
9831 : : }
9832 : : else
9833 : : {
9834 : : /* f_c_string(string) = trim(string) // c_null_char */
9835 : :
9836 : 12 : gfc_add_block_to_block (&se->pre, &lse.pre);
9837 : 12 : gfc_add_block_to_block (&se->pre, &rse.pre);
9838 : :
9839 : 12 : gfc_init_se (&tse, se);
9840 : 12 : conv_trim (&tse, &lse);
9841 : 12 : gfc_add_block_to_block (&se->pre, &tse.pre);
9842 : 12 : gfc_add_block_to_block (&se->post, &tse.post);
9843 : :
9844 : 12 : len = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9845 : : fold_convert (cnode, tse.string_length),
9846 : : fold_convert (cnode, rse.string_length));
9847 : :
9848 : 12 : var = gfc_conv_string_tmp (se, pchar_type_node, len);
9849 : :
9850 : 12 : tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9851 : : 6, len, var,
9852 : : tse.string_length, tse.expr,
9853 : : rse.string_length, rse.expr);
9854 : 12 : gfc_add_expr_to_block (&se->pre, tmp);
9855 : : }
9856 : :
9857 : 42 : se->expr = var;
9858 : 42 : se->string_length = len;
9859 : :
9860 : : #undef cnode
9861 : : }
9862 : : else
9863 : 0 : gcc_unreachable ();
9864 : 8906 : }
9865 : :
9866 : :
9867 : : /* The following routine generates code for the intrinsic
9868 : : subroutines from the ISO_C_BINDING module:
9869 : : * C_F_POINTER
9870 : : * C_F_PROCPOINTER. */
9871 : :
9872 : : static tree
9873 : 2740 : conv_isocbinding_subroutine (gfc_code *code)
9874 : : {
9875 : 2740 : gfc_se se;
9876 : 2740 : gfc_se cptrse;
9877 : 2740 : gfc_se fptrse;
9878 : 2740 : gfc_se shapese;
9879 : 2740 : gfc_ss *shape_ss;
9880 : 2740 : tree desc, dim, tmp, stride, offset;
9881 : 2740 : stmtblock_t body, block;
9882 : 2740 : gfc_loopinfo loop;
9883 : 2740 : gfc_actual_arglist *arg = code->ext.actual;
9884 : :
9885 : 2740 : gfc_init_se (&se, NULL);
9886 : 2740 : gfc_init_se (&cptrse, NULL);
9887 : 2740 : gfc_conv_expr (&cptrse, arg->expr);
9888 : 2740 : gfc_add_block_to_block (&se.pre, &cptrse.pre);
9889 : 2740 : gfc_add_block_to_block (&se.post, &cptrse.post);
9890 : :
9891 : 2740 : gfc_init_se (&fptrse, NULL);
9892 : 2740 : if (arg->next->expr->rank == 0)
9893 : : {
9894 : 2276 : fptrse.want_pointer = 1;
9895 : 2276 : gfc_conv_expr (&fptrse, arg->next->expr);
9896 : 2276 : gfc_add_block_to_block (&se.pre, &fptrse.pre);
9897 : 2276 : gfc_add_block_to_block (&se.post, &fptrse.post);
9898 : 2276 : if (arg->next->expr->symtree->n.sym->attr.proc_pointer
9899 : 2276 : && arg->next->expr->symtree->n.sym->attr.dummy)
9900 : 7 : fptrse.expr = build_fold_indirect_ref_loc (input_location,
9901 : : fptrse.expr);
9902 : 4552 : se.expr = fold_build2_loc (input_location, MODIFY_EXPR,
9903 : 2276 : TREE_TYPE (fptrse.expr),
9904 : : fptrse.expr,
9905 : 2276 : fold_convert (TREE_TYPE (fptrse.expr),
9906 : : cptrse.expr));
9907 : 2276 : gfc_add_expr_to_block (&se.pre, se.expr);
9908 : 2276 : gfc_add_block_to_block (&se.pre, &se.post);
9909 : 2276 : return gfc_finish_block (&se.pre);
9910 : : }
9911 : :
9912 : 464 : gfc_start_block (&block);
9913 : :
9914 : : /* Get the descriptor of the Fortran pointer. */
9915 : 464 : fptrse.descriptor_only = 1;
9916 : 464 : gfc_conv_expr_descriptor (&fptrse, arg->next->expr);
9917 : 464 : gfc_add_block_to_block (&block, &fptrse.pre);
9918 : 464 : desc = fptrse.expr;
9919 : :
9920 : : /* Set the span field. */
9921 : 464 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
9922 : 464 : tmp = fold_convert (gfc_array_index_type, tmp);
9923 : 464 : gfc_conv_descriptor_span_set (&block, desc, tmp);
9924 : :
9925 : : /* Set data value, dtype, and offset. */
9926 : 464 : tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc));
9927 : 464 : gfc_conv_descriptor_data_set (&block, desc, fold_convert (tmp, cptrse.expr));
9928 : 464 : gfc_add_modify (&block, gfc_conv_descriptor_dtype (desc),
9929 : 464 : gfc_get_dtype (TREE_TYPE (desc)));
9930 : :
9931 : : /* Start scalarization of the bounds, using the shape argument. */
9932 : :
9933 : 464 : shape_ss = gfc_walk_expr (arg->next->next->expr);
9934 : 464 : gcc_assert (shape_ss != gfc_ss_terminator);
9935 : 464 : gfc_init_se (&shapese, NULL);
9936 : :
9937 : 464 : gfc_init_loopinfo (&loop);
9938 : 464 : gfc_add_ss_to_loop (&loop, shape_ss);
9939 : 464 : gfc_conv_ss_startstride (&loop);
9940 : 464 : gfc_conv_loop_setup (&loop, &arg->next->expr->where);
9941 : 464 : gfc_mark_ss_chain_used (shape_ss, 1);
9942 : :
9943 : 464 : gfc_copy_loopinfo_to_se (&shapese, &loop);
9944 : 464 : shapese.ss = shape_ss;
9945 : :
9946 : 464 : stride = gfc_create_var (gfc_array_index_type, "stride");
9947 : 464 : offset = gfc_create_var (gfc_array_index_type, "offset");
9948 : 464 : gfc_add_modify (&block, stride, gfc_index_one_node);
9949 : 464 : gfc_add_modify (&block, offset, gfc_index_zero_node);
9950 : :
9951 : : /* Loop body. */
9952 : 464 : gfc_start_scalarized_body (&loop, &body);
9953 : :
9954 : 464 : dim = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9955 : : loop.loopvar[0], loop.from[0]);
9956 : :
9957 : : /* Set bounds and stride. */
9958 : 464 : gfc_conv_descriptor_lbound_set (&body, desc, dim, gfc_index_one_node);
9959 : 464 : gfc_conv_descriptor_stride_set (&body, desc, dim, stride);
9960 : :
9961 : 464 : gfc_conv_expr (&shapese, arg->next->next->expr);
9962 : 464 : gfc_add_block_to_block (&body, &shapese.pre);
9963 : 464 : gfc_conv_descriptor_ubound_set (&body, desc, dim, shapese.expr);
9964 : 464 : gfc_add_block_to_block (&body, &shapese.post);
9965 : :
9966 : : /* Calculate offset. */
9967 : 464 : gfc_add_modify (&body, offset,
9968 : : fold_build2_loc (input_location, PLUS_EXPR,
9969 : : gfc_array_index_type, offset, stride));
9970 : : /* Update stride. */
9971 : 464 : gfc_add_modify (&body, stride,
9972 : : fold_build2_loc (input_location, MULT_EXPR,
9973 : : gfc_array_index_type, stride,
9974 : : fold_convert (gfc_array_index_type,
9975 : : shapese.expr)));
9976 : : /* Finish scalarization loop. */
9977 : 464 : gfc_trans_scalarizing_loops (&loop, &body);
9978 : 464 : gfc_add_block_to_block (&block, &loop.pre);
9979 : 464 : gfc_add_block_to_block (&block, &loop.post);
9980 : 464 : gfc_add_block_to_block (&block, &fptrse.post);
9981 : 464 : gfc_cleanup_loop (&loop);
9982 : :
9983 : 464 : gfc_add_modify (&block, offset,
9984 : : fold_build1_loc (input_location, NEGATE_EXPR,
9985 : : gfc_array_index_type, offset));
9986 : 464 : gfc_conv_descriptor_offset_set (&block, desc, offset);
9987 : :
9988 : 464 : gfc_add_expr_to_block (&se.pre, gfc_finish_block (&block));
9989 : 464 : gfc_add_block_to_block (&se.pre, &se.post);
9990 : 464 : return gfc_finish_block (&se.pre);
9991 : : }
9992 : :
9993 : :
9994 : : /* Save and restore floating-point state. */
9995 : :
9996 : : tree
9997 : 946 : gfc_save_fp_state (stmtblock_t *block)
9998 : : {
9999 : 946 : tree type, fpstate, tmp;
10000 : :
10001 : 946 : type = build_array_type (char_type_node,
10002 : : build_range_type (size_type_node, size_zero_node,
10003 : 946 : size_int (GFC_FPE_STATE_BUFFER_SIZE)));
10004 : 946 : fpstate = gfc_create_var (type, "fpstate");
10005 : 946 : fpstate = gfc_build_addr_expr (pvoid_type_node, fpstate);
10006 : :
10007 : 946 : tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_entry,
10008 : : 1, fpstate);
10009 : 946 : gfc_add_expr_to_block (block, tmp);
10010 : :
10011 : 946 : return fpstate;
10012 : : }
10013 : :
10014 : :
10015 : : void
10016 : 946 : gfc_restore_fp_state (stmtblock_t *block, tree fpstate)
10017 : : {
10018 : 946 : tree tmp;
10019 : :
10020 : 946 : tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_exit,
10021 : : 1, fpstate);
10022 : 946 : gfc_add_expr_to_block (block, tmp);
10023 : 946 : }
10024 : :
10025 : :
10026 : : /* Generate code for arguments of IEEE functions. */
10027 : :
10028 : : static void
10029 : 12457 : conv_ieee_function_args (gfc_se *se, gfc_expr *expr, tree *argarray,
10030 : : int nargs)
10031 : : {
10032 : 12457 : gfc_actual_arglist *actual;
10033 : 12457 : gfc_expr *e;
10034 : 12457 : gfc_se argse;
10035 : 12457 : int arg;
10036 : :
10037 : 12457 : actual = expr->value.function.actual;
10038 : 34461 : for (arg = 0; arg < nargs; arg++, actual = actual->next)
10039 : : {
10040 : 22004 : gcc_assert (actual);
10041 : 22004 : e = actual->expr;
10042 : :
10043 : 22004 : gfc_init_se (&argse, se);
10044 : 22004 : gfc_conv_expr_val (&argse, e);
10045 : :
10046 : 22004 : gfc_add_block_to_block (&se->pre, &argse.pre);
10047 : 22004 : gfc_add_block_to_block (&se->post, &argse.post);
10048 : 22004 : argarray[arg] = argse.expr;
10049 : : }
10050 : 12457 : }
10051 : :
10052 : :
10053 : : /* Generate code for intrinsics IEEE_IS_NAN, IEEE_IS_FINITE
10054 : : and IEEE_UNORDERED, which translate directly to GCC type-generic
10055 : : built-ins. */
10056 : :
10057 : : static void
10058 : 1062 : conv_intrinsic_ieee_builtin (gfc_se * se, gfc_expr * expr,
10059 : : enum built_in_function code, int nargs)
10060 : : {
10061 : 1062 : tree args[2];
10062 : 1062 : gcc_assert ((unsigned) nargs <= ARRAY_SIZE (args));
10063 : :
10064 : 1062 : conv_ieee_function_args (se, expr, args, nargs);
10065 : 1062 : se->expr = build_call_expr_loc_array (input_location,
10066 : : builtin_decl_explicit (code),
10067 : : nargs, args);
10068 : 2388 : STRIP_TYPE_NOPS (se->expr);
10069 : 1062 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10070 : 1062 : }
10071 : :
10072 : :
10073 : : /* Generate code for intrinsics IEEE_SIGNBIT. */
10074 : :
10075 : : static void
10076 : 624 : conv_intrinsic_ieee_signbit (gfc_se * se, gfc_expr * expr)
10077 : : {
10078 : 624 : tree arg, signbit;
10079 : :
10080 : 624 : conv_ieee_function_args (se, expr, &arg, 1);
10081 : 624 : signbit = build_call_expr_loc (input_location,
10082 : : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10083 : : 1, arg);
10084 : 624 : signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10085 : : signbit, integer_zero_node);
10086 : 624 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), signbit);
10087 : 624 : }
10088 : :
10089 : :
10090 : : /* Generate code for IEEE_IS_NORMAL intrinsic:
10091 : : IEEE_IS_NORMAL(x) --> (__builtin_isnormal(x) || x == 0) */
10092 : :
10093 : : static void
10094 : 312 : conv_intrinsic_ieee_is_normal (gfc_se * se, gfc_expr * expr)
10095 : : {
10096 : 312 : tree arg, isnormal, iszero;
10097 : :
10098 : : /* Convert arg, evaluate it only once. */
10099 : 312 : conv_ieee_function_args (se, expr, &arg, 1);
10100 : 312 : arg = gfc_evaluate_now (arg, &se->pre);
10101 : :
10102 : 312 : isnormal = build_call_expr_loc (input_location,
10103 : : builtin_decl_explicit (BUILT_IN_ISNORMAL),
10104 : : 1, arg);
10105 : 312 : iszero = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
10106 : 312 : build_real_from_int_cst (TREE_TYPE (arg),
10107 : 312 : integer_zero_node));
10108 : 312 : se->expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
10109 : : logical_type_node, isnormal, iszero);
10110 : 312 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10111 : 312 : }
10112 : :
10113 : :
10114 : : /* Generate code for IEEE_IS_NEGATIVE intrinsic:
10115 : : IEEE_IS_NEGATIVE(x) --> (__builtin_signbit(x) && !__builtin_isnan(x)) */
10116 : :
10117 : : static void
10118 : 312 : conv_intrinsic_ieee_is_negative (gfc_se * se, gfc_expr * expr)
10119 : : {
10120 : 312 : tree arg, signbit, isnan;
10121 : :
10122 : : /* Convert arg, evaluate it only once. */
10123 : 312 : conv_ieee_function_args (se, expr, &arg, 1);
10124 : 312 : arg = gfc_evaluate_now (arg, &se->pre);
10125 : :
10126 : 312 : isnan = build_call_expr_loc (input_location,
10127 : : builtin_decl_explicit (BUILT_IN_ISNAN),
10128 : : 1, arg);
10129 : 936 : STRIP_TYPE_NOPS (isnan);
10130 : :
10131 : 312 : signbit = build_call_expr_loc (input_location,
10132 : : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10133 : : 1, arg);
10134 : 312 : signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10135 : : signbit, integer_zero_node);
10136 : :
10137 : 312 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10138 : : logical_type_node, signbit,
10139 : : fold_build1_loc (input_location, TRUTH_NOT_EXPR,
10140 : 312 : TREE_TYPE(isnan), isnan));
10141 : :
10142 : 312 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10143 : 312 : }
10144 : :
10145 : :
10146 : : /* Generate code for IEEE_LOGB and IEEE_RINT. */
10147 : :
10148 : : static void
10149 : 240 : conv_intrinsic_ieee_logb_rint (gfc_se * se, gfc_expr * expr,
10150 : : enum built_in_function code)
10151 : : {
10152 : 240 : tree arg, decl, call, fpstate;
10153 : 240 : int argprec;
10154 : :
10155 : 240 : conv_ieee_function_args (se, expr, &arg, 1);
10156 : 240 : argprec = TYPE_PRECISION (TREE_TYPE (arg));
10157 : 240 : decl = builtin_decl_for_precision (code, argprec);
10158 : :
10159 : : /* Save floating-point state. */
10160 : 240 : fpstate = gfc_save_fp_state (&se->pre);
10161 : :
10162 : : /* Make the function call. */
10163 : 240 : call = build_call_expr_loc (input_location, decl, 1, arg);
10164 : 240 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), call);
10165 : :
10166 : : /* Restore floating-point state. */
10167 : 240 : gfc_restore_fp_state (&se->post, fpstate);
10168 : 240 : }
10169 : :
10170 : :
10171 : : /* Generate code for IEEE_REM. */
10172 : :
10173 : : static void
10174 : 84 : conv_intrinsic_ieee_rem (gfc_se * se, gfc_expr * expr)
10175 : : {
10176 : 84 : tree args[2], decl, call, fpstate;
10177 : 84 : int argprec;
10178 : :
10179 : 84 : conv_ieee_function_args (se, expr, args, 2);
10180 : :
10181 : : /* If arguments have unequal size, convert them to the larger. */
10182 : 84 : if (TYPE_PRECISION (TREE_TYPE (args[0]))
10183 : 84 : > TYPE_PRECISION (TREE_TYPE (args[1])))
10184 : 6 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10185 : 78 : else if (TYPE_PRECISION (TREE_TYPE (args[1]))
10186 : 78 : > TYPE_PRECISION (TREE_TYPE (args[0])))
10187 : 24 : args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
10188 : :
10189 : 84 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10190 : 84 : decl = builtin_decl_for_precision (BUILT_IN_REMAINDER, argprec);
10191 : :
10192 : : /* Save floating-point state. */
10193 : 84 : fpstate = gfc_save_fp_state (&se->pre);
10194 : :
10195 : : /* Make the function call. */
10196 : 84 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10197 : 84 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10198 : :
10199 : : /* Restore floating-point state. */
10200 : 84 : gfc_restore_fp_state (&se->post, fpstate);
10201 : 84 : }
10202 : :
10203 : :
10204 : : /* Generate code for IEEE_NEXT_AFTER. */
10205 : :
10206 : : static void
10207 : 180 : conv_intrinsic_ieee_next_after (gfc_se * se, gfc_expr * expr)
10208 : : {
10209 : 180 : tree args[2], decl, call, fpstate;
10210 : 180 : int argprec;
10211 : :
10212 : 180 : conv_ieee_function_args (se, expr, args, 2);
10213 : :
10214 : : /* Result has the characteristics of first argument. */
10215 : 180 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10216 : 180 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10217 : 180 : decl = builtin_decl_for_precision (BUILT_IN_NEXTAFTER, argprec);
10218 : :
10219 : : /* Save floating-point state. */
10220 : 180 : fpstate = gfc_save_fp_state (&se->pre);
10221 : :
10222 : : /* Make the function call. */
10223 : 180 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10224 : 180 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10225 : :
10226 : : /* Restore floating-point state. */
10227 : 180 : gfc_restore_fp_state (&se->post, fpstate);
10228 : 180 : }
10229 : :
10230 : :
10231 : : /* Generate code for IEEE_SCALB. */
10232 : :
10233 : : static void
10234 : 228 : conv_intrinsic_ieee_scalb (gfc_se * se, gfc_expr * expr)
10235 : : {
10236 : 228 : tree args[2], decl, call, huge, type;
10237 : 228 : int argprec, n;
10238 : :
10239 : 228 : conv_ieee_function_args (se, expr, args, 2);
10240 : :
10241 : : /* Result has the characteristics of first argument. */
10242 : 228 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10243 : 228 : decl = builtin_decl_for_precision (BUILT_IN_SCALBN, argprec);
10244 : :
10245 : 228 : if (TYPE_PRECISION (TREE_TYPE (args[1])) > TYPE_PRECISION (integer_type_node))
10246 : : {
10247 : : /* We need to fold the integer into the range of a C int. */
10248 : 18 : args[1] = gfc_evaluate_now (args[1], &se->pre);
10249 : 18 : type = TREE_TYPE (args[1]);
10250 : :
10251 : 18 : n = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
10252 : 18 : huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
10253 : : gfc_c_int_kind);
10254 : 18 : huge = fold_convert (type, huge);
10255 : 18 : args[1] = fold_build2_loc (input_location, MIN_EXPR, type, args[1],
10256 : : huge);
10257 : 18 : args[1] = fold_build2_loc (input_location, MAX_EXPR, type, args[1],
10258 : : fold_build1_loc (input_location, NEGATE_EXPR,
10259 : : type, huge));
10260 : : }
10261 : :
10262 : 228 : args[1] = fold_convert (integer_type_node, args[1]);
10263 : :
10264 : : /* Make the function call. */
10265 : 228 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10266 : 228 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10267 : 228 : }
10268 : :
10269 : :
10270 : : /* Generate code for IEEE_COPY_SIGN. */
10271 : :
10272 : : static void
10273 : 576 : conv_intrinsic_ieee_copy_sign (gfc_se * se, gfc_expr * expr)
10274 : : {
10275 : 576 : tree args[2], decl, sign;
10276 : 576 : int argprec;
10277 : :
10278 : 576 : conv_ieee_function_args (se, expr, args, 2);
10279 : :
10280 : : /* Get the sign of the second argument. */
10281 : 576 : sign = build_call_expr_loc (input_location,
10282 : : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10283 : : 1, args[1]);
10284 : 576 : sign = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10285 : : sign, integer_zero_node);
10286 : :
10287 : : /* Create a value of one, with the right sign. */
10288 : 576 : sign = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
10289 : : sign,
10290 : : fold_build1_loc (input_location, NEGATE_EXPR,
10291 : : integer_type_node,
10292 : : integer_one_node),
10293 : : integer_one_node);
10294 : 576 : args[1] = fold_convert (TREE_TYPE (args[0]), sign);
10295 : :
10296 : 576 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10297 : 576 : decl = builtin_decl_for_precision (BUILT_IN_COPYSIGN, argprec);
10298 : :
10299 : 576 : se->expr = build_call_expr_loc_array (input_location, decl, 2, args);
10300 : 576 : }
10301 : :
10302 : :
10303 : : /* Generate code for IEEE_CLASS. */
10304 : :
10305 : : static void
10306 : 648 : conv_intrinsic_ieee_class (gfc_se *se, gfc_expr *expr)
10307 : : {
10308 : 648 : tree arg, c, t1, t2, t3, t4;
10309 : :
10310 : : /* Convert arg, evaluate it only once. */
10311 : 648 : conv_ieee_function_args (se, expr, &arg, 1);
10312 : 648 : arg = gfc_evaluate_now (arg, &se->pre);
10313 : :
10314 : 648 : c = build_call_expr_loc (input_location,
10315 : : builtin_decl_explicit (BUILT_IN_FPCLASSIFY), 6,
10316 : 648 : build_int_cst (integer_type_node, IEEE_QUIET_NAN),
10317 : 648 : build_int_cst (integer_type_node,
10318 : 648 : IEEE_POSITIVE_INF),
10319 : 648 : build_int_cst (integer_type_node,
10320 : 648 : IEEE_POSITIVE_NORMAL),
10321 : 648 : build_int_cst (integer_type_node,
10322 : 648 : IEEE_POSITIVE_DENORMAL),
10323 : 648 : build_int_cst (integer_type_node,
10324 : 648 : IEEE_POSITIVE_ZERO),
10325 : : arg);
10326 : 648 : c = gfc_evaluate_now (c, &se->pre);
10327 : 648 : t1 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10328 : 648 : c, build_int_cst (integer_type_node,
10329 : 648 : IEEE_QUIET_NAN));
10330 : 648 : t2 = build_call_expr_loc (input_location,
10331 : : builtin_decl_explicit (BUILT_IN_ISSIGNALING), 1,
10332 : : arg);
10333 : 648 : t2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10334 : 648 : t2, build_zero_cst (TREE_TYPE (t2)));
10335 : 648 : t1 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10336 : : logical_type_node, t1, t2);
10337 : 648 : t3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10338 : 648 : c, build_int_cst (integer_type_node,
10339 : 648 : IEEE_POSITIVE_ZERO));
10340 : 648 : t4 = build_call_expr_loc (input_location,
10341 : : builtin_decl_explicit (BUILT_IN_SIGNBIT), 1,
10342 : : arg);
10343 : 648 : t4 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10344 : 648 : t4, build_zero_cst (TREE_TYPE (t4)));
10345 : 648 : t3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10346 : : logical_type_node, t3, t4);
10347 : 648 : int s = IEEE_NEGATIVE_ZERO + IEEE_POSITIVE_ZERO;
10348 : 648 : gcc_assert (IEEE_NEGATIVE_INF == s - IEEE_POSITIVE_INF);
10349 : 648 : gcc_assert (IEEE_NEGATIVE_NORMAL == s - IEEE_POSITIVE_NORMAL);
10350 : 648 : gcc_assert (IEEE_NEGATIVE_DENORMAL == s - IEEE_POSITIVE_DENORMAL);
10351 : 648 : gcc_assert (IEEE_NEGATIVE_SUBNORMAL == s - IEEE_POSITIVE_SUBNORMAL);
10352 : 648 : gcc_assert (IEEE_NEGATIVE_ZERO == s - IEEE_POSITIVE_ZERO);
10353 : 648 : t4 = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (c),
10354 : 648 : build_int_cst (TREE_TYPE (c), s), c);
10355 : 648 : t3 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c),
10356 : : t3, t4, c);
10357 : 648 : t1 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c), t1,
10358 : 648 : build_int_cst (TREE_TYPE (c), IEEE_SIGNALING_NAN),
10359 : : t3);
10360 : 648 : tree type = gfc_typenode_for_spec (&expr->ts);
10361 : : /* Perform a quick sanity check that the return type is
10362 : : IEEE_CLASS_TYPE derived type defined in
10363 : : libgfortran/ieee/ieee_arithmetic.F90
10364 : : Primarily check that it is a derived type with a single
10365 : : member in it. */
10366 : 648 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10367 : 648 : tree field = NULL_TREE;
10368 : 1296 : for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10369 : 648 : if (TREE_CODE (f) == FIELD_DECL)
10370 : : {
10371 : 648 : gcc_assert (field == NULL_TREE);
10372 : : field = f;
10373 : : }
10374 : 648 : gcc_assert (field);
10375 : 648 : t1 = fold_convert (TREE_TYPE (field), t1);
10376 : 648 : se->expr = build_constructor_single (type, field, t1);
10377 : 648 : }
10378 : :
10379 : :
10380 : : /* Generate code for IEEE_VALUE. */
10381 : :
10382 : : static void
10383 : 1111 : conv_intrinsic_ieee_value (gfc_se *se, gfc_expr *expr)
10384 : : {
10385 : 1111 : tree args[2], arg, ret, tmp;
10386 : 1111 : stmtblock_t body;
10387 : :
10388 : : /* Convert args, evaluate the second one only once. */
10389 : 1111 : conv_ieee_function_args (se, expr, args, 2);
10390 : 1111 : arg = gfc_evaluate_now (args[1], &se->pre);
10391 : :
10392 : 1111 : tree type = TREE_TYPE (arg);
10393 : : /* Perform a quick sanity check that the second argument's type is
10394 : : IEEE_CLASS_TYPE derived type defined in
10395 : : libgfortran/ieee/ieee_arithmetic.F90
10396 : : Primarily check that it is a derived type with a single
10397 : : member in it. */
10398 : 1111 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10399 : 1111 : tree field = NULL_TREE;
10400 : 2222 : for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10401 : 1111 : if (TREE_CODE (f) == FIELD_DECL)
10402 : : {
10403 : 1111 : gcc_assert (field == NULL_TREE);
10404 : : field = f;
10405 : : }
10406 : 1111 : gcc_assert (field);
10407 : 1111 : arg = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
10408 : : arg, field, NULL_TREE);
10409 : 1111 : arg = gfc_evaluate_now (arg, &se->pre);
10410 : :
10411 : 1111 : type = gfc_typenode_for_spec (&expr->ts);
10412 : 1111 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
10413 : 1111 : ret = gfc_create_var (type, NULL);
10414 : :
10415 : 1111 : gfc_init_block (&body);
10416 : :
10417 : 1111 : tree end_label = gfc_build_label_decl (NULL_TREE);
10418 : 12221 : for (int c = IEEE_SIGNALING_NAN; c <= IEEE_POSITIVE_INF; ++c)
10419 : : {
10420 : 11110 : tree label = gfc_build_label_decl (NULL_TREE);
10421 : 11110 : tree low = build_int_cst (TREE_TYPE (arg), c);
10422 : 11110 : tmp = build_case_label (low, low, label);
10423 : 11110 : gfc_add_expr_to_block (&body, tmp);
10424 : :
10425 : 11110 : REAL_VALUE_TYPE real;
10426 : 11110 : int k;
10427 : 11110 : switch (c)
10428 : : {
10429 : 1111 : case IEEE_SIGNALING_NAN:
10430 : 1111 : real_nan (&real, "", 0, TYPE_MODE (type));
10431 : 1111 : break;
10432 : 1111 : case IEEE_QUIET_NAN:
10433 : 1111 : real_nan (&real, "", 1, TYPE_MODE (type));
10434 : 1111 : break;
10435 : 1111 : case IEEE_NEGATIVE_INF:
10436 : 1111 : real_inf (&real);
10437 : 1111 : real = real_value_negate (&real);
10438 : 1111 : break;
10439 : 1111 : case IEEE_NEGATIVE_NORMAL:
10440 : 1111 : real_from_integer (&real, TYPE_MODE (type), -42, SIGNED);
10441 : 1111 : break;
10442 : 1111 : case IEEE_NEGATIVE_DENORMAL:
10443 : 1111 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10444 : 1111 : real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10445 : : type, GFC_RND_MODE);
10446 : 1111 : real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10447 : 1111 : real = real_value_negate (&real);
10448 : 1111 : break;
10449 : 1111 : case IEEE_NEGATIVE_ZERO:
10450 : 1111 : real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10451 : 1111 : real = real_value_negate (&real);
10452 : 1111 : break;
10453 : 1111 : case IEEE_POSITIVE_ZERO:
10454 : : /* Make this also the default: label. The other possibility
10455 : : would be to add a separate default: label followed by
10456 : : __builtin_unreachable (). */
10457 : 1111 : label = gfc_build_label_decl (NULL_TREE);
10458 : 1111 : tmp = build_case_label (NULL_TREE, NULL_TREE, label);
10459 : 1111 : gfc_add_expr_to_block (&body, tmp);
10460 : 1111 : real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10461 : 1111 : break;
10462 : 1111 : case IEEE_POSITIVE_DENORMAL:
10463 : 1111 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10464 : 1111 : real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10465 : : type, GFC_RND_MODE);
10466 : 1111 : real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10467 : 1111 : break;
10468 : 1111 : case IEEE_POSITIVE_NORMAL:
10469 : 1111 : real_from_integer (&real, TYPE_MODE (type), 42, SIGNED);
10470 : 1111 : break;
10471 : 1111 : case IEEE_POSITIVE_INF:
10472 : 1111 : real_inf (&real);
10473 : 1111 : break;
10474 : : default:
10475 : : gcc_unreachable ();
10476 : : }
10477 : :
10478 : 11110 : tree val = build_real (type, real);
10479 : 11110 : gfc_add_modify (&body, ret, val);
10480 : :
10481 : 11110 : tmp = build1_v (GOTO_EXPR, end_label);
10482 : 11110 : gfc_add_expr_to_block (&body, tmp);
10483 : : }
10484 : :
10485 : 1111 : tmp = gfc_finish_block (&body);
10486 : 1111 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, arg, tmp);
10487 : 1111 : gfc_add_expr_to_block (&se->pre, tmp);
10488 : :
10489 : 1111 : tmp = build1_v (LABEL_EXPR, end_label);
10490 : 1111 : gfc_add_expr_to_block (&se->pre, tmp);
10491 : :
10492 : 1111 : se->expr = ret;
10493 : 1111 : }
10494 : :
10495 : :
10496 : : /* Generate code for IEEE_FMA. */
10497 : :
10498 : : static void
10499 : 120 : conv_intrinsic_ieee_fma (gfc_se * se, gfc_expr * expr)
10500 : : {
10501 : 120 : tree args[3], decl, call;
10502 : 120 : int argprec;
10503 : :
10504 : 120 : conv_ieee_function_args (se, expr, args, 3);
10505 : :
10506 : : /* All three arguments should have the same type. */
10507 : 120 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10508 : 120 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[2])));
10509 : :
10510 : : /* Call the type-generic FMA built-in. */
10511 : 120 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10512 : 120 : decl = builtin_decl_for_precision (BUILT_IN_FMA, argprec);
10513 : 120 : call = build_call_expr_loc_array (input_location, decl, 3, args);
10514 : :
10515 : : /* Convert to the final type. */
10516 : 120 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10517 : 120 : }
10518 : :
10519 : :
10520 : : /* Generate code for IEEE_{MIN,MAX}_NUM{,_MAG}. */
10521 : :
10522 : : static void
10523 : 3072 : conv_intrinsic_ieee_minmax (gfc_se * se, gfc_expr * expr, int max,
10524 : : const char *name)
10525 : : {
10526 : 3072 : tree args[2], func;
10527 : 3072 : built_in_function fn;
10528 : :
10529 : 3072 : conv_ieee_function_args (se, expr, args, 2);
10530 : 3072 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10531 : 3072 : args[0] = gfc_evaluate_now (args[0], &se->pre);
10532 : 3072 : args[1] = gfc_evaluate_now (args[1], &se->pre);
10533 : :
10534 : 3072 : if (startswith (name, "mag"))
10535 : : {
10536 : : /* IEEE_MIN_NUM_MAG and IEEE_MAX_NUM_MAG translate to C functions
10537 : : fminmag() and fmaxmag(), which do not exist as built-ins.
10538 : :
10539 : : Following glibc, we emit this:
10540 : :
10541 : : fminmag (x, y) {
10542 : : ax = ABS (x);
10543 : : ay = ABS (y);
10544 : : if (isless (ax, ay))
10545 : : return x;
10546 : : else if (isgreater (ax, ay))
10547 : : return y;
10548 : : else if (ax == ay)
10549 : : return x < y ? x : y;
10550 : : else if (issignaling (x) || issignaling (y))
10551 : : return x + y;
10552 : : else
10553 : : return isnan (y) ? x : y;
10554 : : }
10555 : :
10556 : : fmaxmag (x, y) {
10557 : : ax = ABS (x);
10558 : : ay = ABS (y);
10559 : : if (isgreater (ax, ay))
10560 : : return x;
10561 : : else if (isless (ax, ay))
10562 : : return y;
10563 : : else if (ax == ay)
10564 : : return x > y ? x : y;
10565 : : else if (issignaling (x) || issignaling (y))
10566 : : return x + y;
10567 : : else
10568 : : return isnan (y) ? x : y;
10569 : : }
10570 : :
10571 : : */
10572 : :
10573 : 1536 : tree abs0, abs1, sig0, sig1;
10574 : 1536 : tree cond1, cond2, cond3, cond4, cond5;
10575 : 1536 : tree res;
10576 : 1536 : tree type = TREE_TYPE (args[0]);
10577 : :
10578 : 1536 : func = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
10579 : 1536 : abs0 = build_call_expr_loc (input_location, func, 1, args[0]);
10580 : 1536 : abs1 = build_call_expr_loc (input_location, func, 1, args[1]);
10581 : 1536 : abs0 = gfc_evaluate_now (abs0, &se->pre);
10582 : 1536 : abs1 = gfc_evaluate_now (abs1, &se->pre);
10583 : :
10584 : 1536 : cond5 = build_call_expr_loc (input_location,
10585 : : builtin_decl_explicit (BUILT_IN_ISNAN),
10586 : : 1, args[1]);
10587 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond5,
10588 : : args[0], args[1]);
10589 : :
10590 : 1536 : sig0 = build_call_expr_loc (input_location,
10591 : : builtin_decl_explicit (BUILT_IN_ISSIGNALING),
10592 : : 1, args[0]);
10593 : 1536 : sig1 = build_call_expr_loc (input_location,
10594 : : builtin_decl_explicit (BUILT_IN_ISSIGNALING),
10595 : : 1, args[1]);
10596 : 1536 : cond4 = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
10597 : : logical_type_node, sig0, sig1);
10598 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond4,
10599 : : fold_build2_loc (input_location, PLUS_EXPR,
10600 : : type, args[0], args[1]),
10601 : : res);
10602 : :
10603 : 1536 : cond3 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10604 : : abs0, abs1);
10605 : 2304 : res = fold_build3_loc (input_location, COND_EXPR, type, cond3,
10606 : : fold_build2_loc (input_location,
10607 : : max ? MAX_EXPR : MIN_EXPR,
10608 : : type, args[0], args[1]),
10609 : : res);
10610 : :
10611 : 2304 : func = builtin_decl_explicit (max ? BUILT_IN_ISLESS : BUILT_IN_ISGREATER);
10612 : 1536 : cond2 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
10613 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond2,
10614 : : args[1], res);
10615 : :
10616 : 2304 : func = builtin_decl_explicit (max ? BUILT_IN_ISGREATER : BUILT_IN_ISLESS);
10617 : 1536 : cond1 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
10618 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond1,
10619 : : args[0], res);
10620 : :
10621 : 1536 : se->expr = res;
10622 : : }
10623 : : else
10624 : : {
10625 : : /* IEEE_MIN_NUM and IEEE_MAX_NUM translate to fmin() and fmax(). */
10626 : 1536 : fn = max ? BUILT_IN_FMAX : BUILT_IN_FMIN;
10627 : 1536 : func = gfc_builtin_decl_for_float_kind (fn, expr->ts.kind);
10628 : 1536 : se->expr = build_call_expr_loc_array (input_location, func, 2, args);
10629 : : }
10630 : 3072 : }
10631 : :
10632 : :
10633 : : /* Generate code for comparison functions IEEE_QUIET_* and
10634 : : IEEE_SIGNALING_*. */
10635 : :
10636 : : static void
10637 : 3888 : conv_intrinsic_ieee_comparison (gfc_se * se, gfc_expr * expr, int signaling,
10638 : : const char *name)
10639 : : {
10640 : 3888 : tree args[2];
10641 : 3888 : tree arg1, arg2, res;
10642 : :
10643 : : /* Evaluate arguments only once. */
10644 : 3888 : conv_ieee_function_args (se, expr, args, 2);
10645 : 3888 : arg1 = gfc_evaluate_now (args[0], &se->pre);
10646 : 3888 : arg2 = gfc_evaluate_now (args[1], &se->pre);
10647 : :
10648 : 3888 : if (startswith (name, "eq"))
10649 : : {
10650 : 648 : if (signaling)
10651 : 324 : res = build_call_expr_loc (input_location,
10652 : : builtin_decl_explicit (BUILT_IN_ISEQSIG),
10653 : : 2, arg1, arg2);
10654 : : else
10655 : 324 : res = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10656 : : arg1, arg2);
10657 : : }
10658 : 3240 : else if (startswith (name, "ne"))
10659 : : {
10660 : 648 : if (signaling)
10661 : : {
10662 : 324 : res = build_call_expr_loc (input_location,
10663 : : builtin_decl_explicit (BUILT_IN_ISEQSIG),
10664 : : 2, arg1, arg2);
10665 : 324 : res = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
10666 : : logical_type_node, res);
10667 : : }
10668 : : else
10669 : 324 : res = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10670 : : arg1, arg2);
10671 : : }
10672 : 2592 : else if (startswith (name, "ge"))
10673 : : {
10674 : 648 : if (signaling)
10675 : 324 : res = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10676 : : arg1, arg2);
10677 : : else
10678 : 324 : res = build_call_expr_loc (input_location,
10679 : : builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL),
10680 : : 2, arg1, arg2);
10681 : : }
10682 : 1944 : else if (startswith (name, "gt"))
10683 : : {
10684 : 648 : if (signaling)
10685 : 324 : res = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
10686 : : arg1, arg2);
10687 : : else
10688 : 324 : res = build_call_expr_loc (input_location,
10689 : : builtin_decl_explicit (BUILT_IN_ISGREATER),
10690 : : 2, arg1, arg2);
10691 : : }
10692 : 1296 : else if (startswith (name, "le"))
10693 : : {
10694 : 648 : if (signaling)
10695 : 324 : res = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
10696 : : arg1, arg2);
10697 : : else
10698 : 324 : res = build_call_expr_loc (input_location,
10699 : : builtin_decl_explicit (BUILT_IN_ISLESSEQUAL),
10700 : : 2, arg1, arg2);
10701 : : }
10702 : 648 : else if (startswith (name, "lt"))
10703 : : {
10704 : 648 : if (signaling)
10705 : 324 : res = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
10706 : : arg1, arg2);
10707 : : else
10708 : 324 : res = build_call_expr_loc (input_location,
10709 : : builtin_decl_explicit (BUILT_IN_ISLESS),
10710 : : 2, arg1, arg2);
10711 : : }
10712 : : else
10713 : 0 : gcc_unreachable ();
10714 : :
10715 : 3888 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), res);
10716 : 3888 : }
10717 : :
10718 : :
10719 : : /* Generate code for an intrinsic function from the IEEE_ARITHMETIC
10720 : : module. */
10721 : :
10722 : : bool
10723 : 13939 : gfc_conv_ieee_arithmetic_function (gfc_se * se, gfc_expr * expr)
10724 : : {
10725 : 13939 : const char *name = expr->value.function.name;
10726 : :
10727 : 13939 : if (startswith (name, "_gfortran_ieee_is_nan"))
10728 : 522 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISNAN, 1);
10729 : 13417 : else if (startswith (name, "_gfortran_ieee_is_finite"))
10730 : 372 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISFINITE, 1);
10731 : 13045 : else if (startswith (name, "_gfortran_ieee_unordered"))
10732 : 168 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISUNORDERED, 2);
10733 : 12877 : else if (startswith (name, "_gfortran_ieee_signbit"))
10734 : 624 : conv_intrinsic_ieee_signbit (se, expr);
10735 : 12253 : else if (startswith (name, "_gfortran_ieee_is_normal"))
10736 : 312 : conv_intrinsic_ieee_is_normal (se, expr);
10737 : 11941 : else if (startswith (name, "_gfortran_ieee_is_negative"))
10738 : 312 : conv_intrinsic_ieee_is_negative (se, expr);
10739 : 11629 : else if (startswith (name, "_gfortran_ieee_copy_sign"))
10740 : 576 : conv_intrinsic_ieee_copy_sign (se, expr);
10741 : 11053 : else if (startswith (name, "_gfortran_ieee_scalb"))
10742 : 228 : conv_intrinsic_ieee_scalb (se, expr);
10743 : 10825 : else if (startswith (name, "_gfortran_ieee_next_after"))
10744 : 180 : conv_intrinsic_ieee_next_after (se, expr);
10745 : 10645 : else if (startswith (name, "_gfortran_ieee_rem"))
10746 : 84 : conv_intrinsic_ieee_rem (se, expr);
10747 : 10561 : else if (startswith (name, "_gfortran_ieee_logb"))
10748 : 144 : conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_LOGB);
10749 : 10417 : else if (startswith (name, "_gfortran_ieee_rint"))
10750 : 96 : conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_RINT);
10751 : 10321 : else if (startswith (name, "ieee_class_") && ISDIGIT (name[11]))
10752 : 648 : conv_intrinsic_ieee_class (se, expr);
10753 : 9673 : else if (startswith (name, "ieee_value_") && ISDIGIT (name[11]))
10754 : 1111 : conv_intrinsic_ieee_value (se, expr);
10755 : 8562 : else if (startswith (name, "_gfortran_ieee_fma"))
10756 : 120 : conv_intrinsic_ieee_fma (se, expr);
10757 : 8442 : else if (startswith (name, "_gfortran_ieee_min_num_"))
10758 : 1536 : conv_intrinsic_ieee_minmax (se, expr, 0, name + 23);
10759 : 6906 : else if (startswith (name, "_gfortran_ieee_max_num_"))
10760 : 1536 : conv_intrinsic_ieee_minmax (se, expr, 1, name + 23);
10761 : 5370 : else if (startswith (name, "_gfortran_ieee_quiet_"))
10762 : 1944 : conv_intrinsic_ieee_comparison (se, expr, 0, name + 21);
10763 : 3426 : else if (startswith (name, "_gfortran_ieee_signaling_"))
10764 : 1944 : conv_intrinsic_ieee_comparison (se, expr, 1, name + 25);
10765 : : else
10766 : : /* It is not among the functions we translate directly. We return
10767 : : false, so a library function call is emitted. */
10768 : : return false;
10769 : :
10770 : : return true;
10771 : : }
10772 : :
10773 : :
10774 : : /* Generate a direct call to malloc() for the MALLOC intrinsic. */
10775 : :
10776 : : static void
10777 : 16 : gfc_conv_intrinsic_malloc (gfc_se * se, gfc_expr * expr)
10778 : : {
10779 : 16 : tree arg, res, restype;
10780 : :
10781 : 16 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
10782 : 16 : arg = fold_convert (size_type_node, arg);
10783 : 16 : res = build_call_expr_loc (input_location,
10784 : : builtin_decl_explicit (BUILT_IN_MALLOC), 1, arg);
10785 : 16 : restype = gfc_typenode_for_spec (&expr->ts);
10786 : 16 : se->expr = fold_convert (restype, res);
10787 : 16 : }
10788 : :
10789 : :
10790 : : /* Generate code for an intrinsic function. Some map directly to library
10791 : : calls, others get special handling. In some cases the name of the function
10792 : : used depends on the type specifiers. */
10793 : :
10794 : : void
10795 : 240185 : gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
10796 : : {
10797 : 240185 : const char *name;
10798 : 240185 : int lib, kind;
10799 : 240185 : tree fndecl;
10800 : :
10801 : 240185 : name = &expr->value.function.name[2];
10802 : :
10803 : 240185 : if (expr->rank > 0)
10804 : : {
10805 : 45296 : lib = gfc_is_intrinsic_libcall (expr);
10806 : 45296 : if (lib != 0)
10807 : : {
10808 : 17264 : if (lib == 1)
10809 : 9667 : se->ignore_optional = 1;
10810 : :
10811 : 17264 : switch (expr->value.function.isym->id)
10812 : : {
10813 : 6074 : case GFC_ISYM_EOSHIFT:
10814 : 6074 : case GFC_ISYM_PACK:
10815 : 6074 : case GFC_ISYM_RESHAPE:
10816 : 6074 : case GFC_ISYM_REDUCE:
10817 : : /* For all of those the first argument specifies the type and the
10818 : : third is optional. */
10819 : 6074 : conv_generic_with_optional_char_arg (se, expr, 1, 3);
10820 : 6074 : break;
10821 : :
10822 : 1116 : case GFC_ISYM_FINDLOC:
10823 : 1116 : gfc_conv_intrinsic_findloc (se, expr);
10824 : 1116 : break;
10825 : :
10826 : 1975 : case GFC_ISYM_MINLOC:
10827 : 1975 : gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
10828 : 1975 : break;
10829 : :
10830 : 1401 : case GFC_ISYM_MAXLOC:
10831 : 1401 : gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
10832 : 1401 : break;
10833 : :
10834 : 6698 : default:
10835 : 6698 : gfc_conv_intrinsic_funcall (se, expr);
10836 : 6698 : break;
10837 : : }
10838 : :
10839 : 17264 : return;
10840 : : }
10841 : : }
10842 : :
10843 : 222921 : switch (expr->value.function.isym->id)
10844 : : {
10845 : 0 : case GFC_ISYM_NONE:
10846 : 0 : gcc_unreachable ();
10847 : :
10848 : 529 : case GFC_ISYM_REPEAT:
10849 : 529 : gfc_conv_intrinsic_repeat (se, expr);
10850 : 529 : break;
10851 : :
10852 : 567 : case GFC_ISYM_TRIM:
10853 : 567 : gfc_conv_intrinsic_trim (se, expr);
10854 : 567 : break;
10855 : :
10856 : 42 : case GFC_ISYM_SC_KIND:
10857 : 42 : gfc_conv_intrinsic_sc_kind (se, expr);
10858 : 42 : break;
10859 : :
10860 : 45 : case GFC_ISYM_SI_KIND:
10861 : 45 : gfc_conv_intrinsic_si_kind (se, expr);
10862 : 45 : break;
10863 : :
10864 : 6 : case GFC_ISYM_SL_KIND:
10865 : 6 : gfc_conv_intrinsic_sl_kind (se, expr);
10866 : 6 : break;
10867 : :
10868 : 82 : case GFC_ISYM_SR_KIND:
10869 : 82 : gfc_conv_intrinsic_sr_kind (se, expr);
10870 : 82 : break;
10871 : :
10872 : 228 : case GFC_ISYM_EXPONENT:
10873 : 228 : gfc_conv_intrinsic_exponent (se, expr);
10874 : 228 : break;
10875 : :
10876 : 316 : case GFC_ISYM_SCAN:
10877 : 316 : kind = expr->value.function.actual->expr->ts.kind;
10878 : 316 : if (kind == 1)
10879 : 250 : fndecl = gfor_fndecl_string_scan;
10880 : 66 : else if (kind == 4)
10881 : 66 : fndecl = gfor_fndecl_string_scan_char4;
10882 : : else
10883 : 0 : gcc_unreachable ();
10884 : :
10885 : 316 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
10886 : 316 : break;
10887 : :
10888 : 94 : case GFC_ISYM_VERIFY:
10889 : 94 : kind = expr->value.function.actual->expr->ts.kind;
10890 : 94 : if (kind == 1)
10891 : 70 : fndecl = gfor_fndecl_string_verify;
10892 : 24 : else if (kind == 4)
10893 : 24 : fndecl = gfor_fndecl_string_verify_char4;
10894 : : else
10895 : 0 : gcc_unreachable ();
10896 : :
10897 : 94 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
10898 : 94 : break;
10899 : :
10900 : 6930 : case GFC_ISYM_ALLOCATED:
10901 : 6930 : gfc_conv_allocated (se, expr);
10902 : 6930 : break;
10903 : :
10904 : 8810 : case GFC_ISYM_ASSOCIATED:
10905 : 8810 : gfc_conv_associated(se, expr);
10906 : 8810 : break;
10907 : :
10908 : 403 : case GFC_ISYM_SAME_TYPE_AS:
10909 : 403 : gfc_conv_same_type_as (se, expr);
10910 : 403 : break;
10911 : :
10912 : 7554 : case GFC_ISYM_ABS:
10913 : 7554 : gfc_conv_intrinsic_abs (se, expr);
10914 : 7554 : break;
10915 : :
10916 : 351 : case GFC_ISYM_ADJUSTL:
10917 : 351 : if (expr->ts.kind == 1)
10918 : 297 : fndecl = gfor_fndecl_adjustl;
10919 : 54 : else if (expr->ts.kind == 4)
10920 : 54 : fndecl = gfor_fndecl_adjustl_char4;
10921 : : else
10922 : 0 : gcc_unreachable ();
10923 : :
10924 : 351 : gfc_conv_intrinsic_adjust (se, expr, fndecl);
10925 : 351 : break;
10926 : :
10927 : 111 : case GFC_ISYM_ADJUSTR:
10928 : 111 : if (expr->ts.kind == 1)
10929 : 56 : fndecl = gfor_fndecl_adjustr;
10930 : 55 : else if (expr->ts.kind == 4)
10931 : 55 : fndecl = gfor_fndecl_adjustr_char4;
10932 : : else
10933 : 0 : gcc_unreachable ();
10934 : :
10935 : 111 : gfc_conv_intrinsic_adjust (se, expr, fndecl);
10936 : 111 : break;
10937 : :
10938 : 428 : case GFC_ISYM_AIMAG:
10939 : 428 : gfc_conv_intrinsic_imagpart (se, expr);
10940 : 428 : break;
10941 : :
10942 : 146 : case GFC_ISYM_AINT:
10943 : 146 : gfc_conv_intrinsic_aint (se, expr, RND_TRUNC);
10944 : 146 : break;
10945 : :
10946 : 419 : case GFC_ISYM_ALL:
10947 : 419 : gfc_conv_intrinsic_anyall (se, expr, EQ_EXPR);
10948 : 419 : break;
10949 : :
10950 : 74 : case GFC_ISYM_ANINT:
10951 : 74 : gfc_conv_intrinsic_aint (se, expr, RND_ROUND);
10952 : 74 : break;
10953 : :
10954 : 90 : case GFC_ISYM_AND:
10955 : 90 : gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
10956 : 90 : break;
10957 : :
10958 : 33918 : case GFC_ISYM_ANY:
10959 : 33918 : gfc_conv_intrinsic_anyall (se, expr, NE_EXPR);
10960 : 33918 : break;
10961 : :
10962 : 216 : case GFC_ISYM_ACOSD:
10963 : 216 : case GFC_ISYM_ASIND:
10964 : 216 : case GFC_ISYM_ATAND:
10965 : 216 : gfc_conv_intrinsic_atrigd (se, expr, expr->value.function.isym->id);
10966 : 216 : break;
10967 : :
10968 : 102 : case GFC_ISYM_COTAN:
10969 : 102 : gfc_conv_intrinsic_cotan (se, expr);
10970 : 102 : break;
10971 : :
10972 : 108 : case GFC_ISYM_COTAND:
10973 : 108 : gfc_conv_intrinsic_cotand (se, expr);
10974 : 108 : break;
10975 : :
10976 : 72 : case GFC_ISYM_ATAN2D:
10977 : 72 : gfc_conv_intrinsic_atan2d (se, expr);
10978 : 72 : break;
10979 : :
10980 : 145 : case GFC_ISYM_BTEST:
10981 : 145 : gfc_conv_intrinsic_btest (se, expr);
10982 : 145 : break;
10983 : :
10984 : 54 : case GFC_ISYM_BGE:
10985 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, GE_EXPR);
10986 : 54 : break;
10987 : :
10988 : 54 : case GFC_ISYM_BGT:
10989 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, GT_EXPR);
10990 : 54 : break;
10991 : :
10992 : 54 : case GFC_ISYM_BLE:
10993 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, LE_EXPR);
10994 : 54 : break;
10995 : :
10996 : 54 : case GFC_ISYM_BLT:
10997 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, LT_EXPR);
10998 : 54 : break;
10999 : :
11000 : 8906 : case GFC_ISYM_C_ASSOCIATED:
11001 : 8906 : case GFC_ISYM_C_FUNLOC:
11002 : 8906 : case GFC_ISYM_C_LOC:
11003 : 8906 : case GFC_ISYM_F_C_STRING:
11004 : 8906 : conv_isocbinding_function (se, expr);
11005 : 8906 : break;
11006 : :
11007 : 2020 : case GFC_ISYM_ACHAR:
11008 : 2020 : case GFC_ISYM_CHAR:
11009 : 2020 : gfc_conv_intrinsic_char (se, expr);
11010 : 2020 : break;
11011 : :
11012 : 35559 : case GFC_ISYM_CONVERSION:
11013 : 35559 : case GFC_ISYM_DBLE:
11014 : 35559 : case GFC_ISYM_DFLOAT:
11015 : 35559 : case GFC_ISYM_FLOAT:
11016 : 35559 : case GFC_ISYM_LOGICAL:
11017 : 35559 : case GFC_ISYM_REAL:
11018 : 35559 : case GFC_ISYM_REALPART:
11019 : 35559 : case GFC_ISYM_SNGL:
11020 : 35559 : gfc_conv_intrinsic_conversion (se, expr);
11021 : 35559 : break;
11022 : :
11023 : : /* Integer conversions are handled separately to make sure we get the
11024 : : correct rounding mode. */
11025 : 2649 : case GFC_ISYM_INT:
11026 : 2649 : case GFC_ISYM_INT2:
11027 : 2649 : case GFC_ISYM_INT8:
11028 : 2649 : case GFC_ISYM_LONG:
11029 : 2649 : case GFC_ISYM_UINT:
11030 : 2649 : gfc_conv_intrinsic_int (se, expr, RND_TRUNC);
11031 : 2649 : break;
11032 : :
11033 : 162 : case GFC_ISYM_NINT:
11034 : 162 : gfc_conv_intrinsic_int (se, expr, RND_ROUND);
11035 : 162 : break;
11036 : :
11037 : 16 : case GFC_ISYM_CEILING:
11038 : 16 : gfc_conv_intrinsic_int (se, expr, RND_CEIL);
11039 : 16 : break;
11040 : :
11041 : 116 : case GFC_ISYM_FLOOR:
11042 : 116 : gfc_conv_intrinsic_int (se, expr, RND_FLOOR);
11043 : 116 : break;
11044 : :
11045 : 2682 : case GFC_ISYM_MOD:
11046 : 2682 : gfc_conv_intrinsic_mod (se, expr, 0);
11047 : 2682 : break;
11048 : :
11049 : 434 : case GFC_ISYM_MODULO:
11050 : 434 : gfc_conv_intrinsic_mod (se, expr, 1);
11051 : 434 : break;
11052 : :
11053 : 889 : case GFC_ISYM_CAF_GET:
11054 : 889 : gfc_conv_intrinsic_caf_get (se, expr, NULL_TREE, false, NULL);
11055 : 889 : break;
11056 : :
11057 : 66 : case GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE:
11058 : 66 : gfc_conv_intrinsic_caf_is_present_remote (se, expr);
11059 : 66 : break;
11060 : :
11061 : 479 : case GFC_ISYM_CMPLX:
11062 : 479 : gfc_conv_intrinsic_cmplx (se, expr, name[5] == '1');
11063 : 479 : break;
11064 : :
11065 : 10 : case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
11066 : 10 : gfc_conv_intrinsic_iargc (se, expr);
11067 : 10 : break;
11068 : :
11069 : 6 : case GFC_ISYM_COMPLEX:
11070 : 6 : gfc_conv_intrinsic_cmplx (se, expr, 1);
11071 : 6 : break;
11072 : :
11073 : 255 : case GFC_ISYM_CONJG:
11074 : 255 : gfc_conv_intrinsic_conjg (se, expr);
11075 : 255 : break;
11076 : :
11077 : 143 : case GFC_ISYM_COUNT:
11078 : 143 : gfc_conv_intrinsic_count (se, expr);
11079 : 143 : break;
11080 : :
11081 : 0 : case GFC_ISYM_CTIME:
11082 : 0 : gfc_conv_intrinsic_ctime (se, expr);
11083 : 0 : break;
11084 : :
11085 : 96 : case GFC_ISYM_DIM:
11086 : 96 : gfc_conv_intrinsic_dim (se, expr);
11087 : 96 : break;
11088 : :
11089 : 113 : case GFC_ISYM_DOT_PRODUCT:
11090 : 113 : gfc_conv_intrinsic_dot_product (se, expr);
11091 : 113 : break;
11092 : :
11093 : 13 : case GFC_ISYM_DPROD:
11094 : 13 : gfc_conv_intrinsic_dprod (se, expr);
11095 : 13 : break;
11096 : :
11097 : 66 : case GFC_ISYM_DSHIFTL:
11098 : 66 : gfc_conv_intrinsic_dshift (se, expr, true);
11099 : 66 : break;
11100 : :
11101 : 66 : case GFC_ISYM_DSHIFTR:
11102 : 66 : gfc_conv_intrinsic_dshift (se, expr, false);
11103 : 66 : break;
11104 : :
11105 : 0 : case GFC_ISYM_FDATE:
11106 : 0 : gfc_conv_intrinsic_fdate (se, expr);
11107 : 0 : break;
11108 : :
11109 : 60 : case GFC_ISYM_FRACTION:
11110 : 60 : gfc_conv_intrinsic_fraction (se, expr);
11111 : 60 : break;
11112 : :
11113 : 24 : case GFC_ISYM_IALL:
11114 : 24 : gfc_conv_intrinsic_arith (se, expr, BIT_AND_EXPR, false);
11115 : 24 : break;
11116 : :
11117 : 600 : case GFC_ISYM_IAND:
11118 : 600 : gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
11119 : 600 : break;
11120 : :
11121 : 12 : case GFC_ISYM_IANY:
11122 : 12 : gfc_conv_intrinsic_arith (se, expr, BIT_IOR_EXPR, false);
11123 : 12 : break;
11124 : :
11125 : 168 : case GFC_ISYM_IBCLR:
11126 : 168 : gfc_conv_intrinsic_singlebitop (se, expr, 0);
11127 : 168 : break;
11128 : :
11129 : 27 : case GFC_ISYM_IBITS:
11130 : 27 : gfc_conv_intrinsic_ibits (se, expr);
11131 : 27 : break;
11132 : :
11133 : 138 : case GFC_ISYM_IBSET:
11134 : 138 : gfc_conv_intrinsic_singlebitop (se, expr, 1);
11135 : 138 : break;
11136 : :
11137 : 2033 : case GFC_ISYM_IACHAR:
11138 : 2033 : case GFC_ISYM_ICHAR:
11139 : : /* We assume ASCII character sequence. */
11140 : 2033 : gfc_conv_intrinsic_ichar (se, expr);
11141 : 2033 : break;
11142 : :
11143 : 2 : case GFC_ISYM_IARGC:
11144 : 2 : gfc_conv_intrinsic_iargc (se, expr);
11145 : 2 : break;
11146 : :
11147 : 688 : case GFC_ISYM_IEOR:
11148 : 688 : gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11149 : 688 : break;
11150 : :
11151 : 328 : case GFC_ISYM_INDEX:
11152 : 328 : kind = expr->value.function.actual->expr->ts.kind;
11153 : 328 : if (kind == 1)
11154 : 262 : fndecl = gfor_fndecl_string_index;
11155 : 66 : else if (kind == 4)
11156 : 66 : fndecl = gfor_fndecl_string_index_char4;
11157 : : else
11158 : 0 : gcc_unreachable ();
11159 : :
11160 : 328 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
11161 : 328 : break;
11162 : :
11163 : 483 : case GFC_ISYM_IOR:
11164 : 483 : gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11165 : 483 : break;
11166 : :
11167 : 12 : case GFC_ISYM_IPARITY:
11168 : 12 : gfc_conv_intrinsic_arith (se, expr, BIT_XOR_EXPR, false);
11169 : 12 : break;
11170 : :
11171 : 6 : case GFC_ISYM_IS_IOSTAT_END:
11172 : 6 : gfc_conv_has_intvalue (se, expr, LIBERROR_END);
11173 : 6 : break;
11174 : :
11175 : 18 : case GFC_ISYM_IS_IOSTAT_EOR:
11176 : 18 : gfc_conv_has_intvalue (se, expr, LIBERROR_EOR);
11177 : 18 : break;
11178 : :
11179 : 621 : case GFC_ISYM_IS_CONTIGUOUS:
11180 : 621 : gfc_conv_intrinsic_is_contiguous (se, expr);
11181 : 621 : break;
11182 : :
11183 : 432 : case GFC_ISYM_ISNAN:
11184 : 432 : gfc_conv_intrinsic_isnan (se, expr);
11185 : 432 : break;
11186 : :
11187 : 8 : case GFC_ISYM_KILL:
11188 : 8 : conv_intrinsic_kill (se, expr);
11189 : 8 : break;
11190 : :
11191 : 90 : case GFC_ISYM_LSHIFT:
11192 : 90 : gfc_conv_intrinsic_shift (se, expr, false, false);
11193 : 90 : break;
11194 : :
11195 : 24 : case GFC_ISYM_RSHIFT:
11196 : 24 : gfc_conv_intrinsic_shift (se, expr, true, true);
11197 : 24 : break;
11198 : :
11199 : 78 : case GFC_ISYM_SHIFTA:
11200 : 78 : gfc_conv_intrinsic_shift (se, expr, true, true);
11201 : 78 : break;
11202 : :
11203 : 183 : case GFC_ISYM_SHIFTL:
11204 : 183 : gfc_conv_intrinsic_shift (se, expr, false, false);
11205 : 183 : break;
11206 : :
11207 : 66 : case GFC_ISYM_SHIFTR:
11208 : 66 : gfc_conv_intrinsic_shift (se, expr, true, false);
11209 : 66 : break;
11210 : :
11211 : 318 : case GFC_ISYM_ISHFT:
11212 : 318 : gfc_conv_intrinsic_ishft (se, expr);
11213 : 318 : break;
11214 : :
11215 : 658 : case GFC_ISYM_ISHFTC:
11216 : 658 : gfc_conv_intrinsic_ishftc (se, expr);
11217 : 658 : break;
11218 : :
11219 : 270 : case GFC_ISYM_LEADZ:
11220 : 270 : gfc_conv_intrinsic_leadz (se, expr);
11221 : 270 : break;
11222 : :
11223 : 282 : case GFC_ISYM_TRAILZ:
11224 : 282 : gfc_conv_intrinsic_trailz (se, expr);
11225 : 282 : break;
11226 : :
11227 : 103 : case GFC_ISYM_POPCNT:
11228 : 103 : gfc_conv_intrinsic_popcnt_poppar (se, expr, 0);
11229 : 103 : break;
11230 : :
11231 : 31 : case GFC_ISYM_POPPAR:
11232 : 31 : gfc_conv_intrinsic_popcnt_poppar (se, expr, 1);
11233 : 31 : break;
11234 : :
11235 : 5452 : case GFC_ISYM_LBOUND:
11236 : 5452 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_LBOUND);
11237 : 5452 : break;
11238 : :
11239 : 178 : case GFC_ISYM_LCOBOUND:
11240 : 178 : conv_intrinsic_cobound (se, expr);
11241 : 178 : break;
11242 : :
11243 : 744 : case GFC_ISYM_TRANSPOSE:
11244 : : /* The scalarizer has already been set up for reversed dimension access
11245 : : order ; now we just get the argument value normally. */
11246 : 744 : gfc_conv_expr (se, expr->value.function.actual->expr);
11247 : 744 : break;
11248 : :
11249 : 5589 : case GFC_ISYM_LEN:
11250 : 5589 : gfc_conv_intrinsic_len (se, expr);
11251 : 5589 : break;
11252 : :
11253 : 2313 : case GFC_ISYM_LEN_TRIM:
11254 : 2313 : gfc_conv_intrinsic_len_trim (se, expr);
11255 : 2313 : break;
11256 : :
11257 : 18 : case GFC_ISYM_LGE:
11258 : 18 : gfc_conv_intrinsic_strcmp (se, expr, GE_EXPR);
11259 : 18 : break;
11260 : :
11261 : 36 : case GFC_ISYM_LGT:
11262 : 36 : gfc_conv_intrinsic_strcmp (se, expr, GT_EXPR);
11263 : 36 : break;
11264 : :
11265 : 18 : case GFC_ISYM_LLE:
11266 : 18 : gfc_conv_intrinsic_strcmp (se, expr, LE_EXPR);
11267 : 18 : break;
11268 : :
11269 : 27 : case GFC_ISYM_LLT:
11270 : 27 : gfc_conv_intrinsic_strcmp (se, expr, LT_EXPR);
11271 : 27 : break;
11272 : :
11273 : 16 : case GFC_ISYM_MALLOC:
11274 : 16 : gfc_conv_intrinsic_malloc (se, expr);
11275 : 16 : break;
11276 : :
11277 : 32 : case GFC_ISYM_MASKL:
11278 : 32 : gfc_conv_intrinsic_mask (se, expr, 1);
11279 : 32 : break;
11280 : :
11281 : 32 : case GFC_ISYM_MASKR:
11282 : 32 : gfc_conv_intrinsic_mask (se, expr, 0);
11283 : 32 : break;
11284 : :
11285 : 1033 : case GFC_ISYM_MAX:
11286 : 1033 : if (expr->ts.type == BT_CHARACTER)
11287 : 138 : gfc_conv_intrinsic_minmax_char (se, expr, 1);
11288 : : else
11289 : 895 : gfc_conv_intrinsic_minmax (se, expr, GT_EXPR);
11290 : : break;
11291 : :
11292 : 4524 : case GFC_ISYM_MAXLOC:
11293 : 4524 : gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
11294 : 4524 : break;
11295 : :
11296 : 180 : case GFC_ISYM_FINDLOC:
11297 : 180 : gfc_conv_intrinsic_findloc (se, expr);
11298 : 180 : break;
11299 : :
11300 : 1093 : case GFC_ISYM_MAXVAL:
11301 : 1093 : gfc_conv_intrinsic_minmaxval (se, expr, GT_EXPR);
11302 : 1093 : break;
11303 : :
11304 : 949 : case GFC_ISYM_MERGE:
11305 : 949 : gfc_conv_intrinsic_merge (se, expr);
11306 : 949 : break;
11307 : :
11308 : 42 : case GFC_ISYM_MERGE_BITS:
11309 : 42 : gfc_conv_intrinsic_merge_bits (se, expr);
11310 : 42 : break;
11311 : :
11312 : 551 : case GFC_ISYM_MIN:
11313 : 551 : if (expr->ts.type == BT_CHARACTER)
11314 : 144 : gfc_conv_intrinsic_minmax_char (se, expr, -1);
11315 : : else
11316 : 407 : gfc_conv_intrinsic_minmax (se, expr, LT_EXPR);
11317 : : break;
11318 : :
11319 : 5412 : case GFC_ISYM_MINLOC:
11320 : 5412 : gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
11321 : 5412 : break;
11322 : :
11323 : 1316 : case GFC_ISYM_MINVAL:
11324 : 1316 : gfc_conv_intrinsic_minmaxval (se, expr, LT_EXPR);
11325 : 1316 : break;
11326 : :
11327 : 1595 : case GFC_ISYM_NEAREST:
11328 : 1595 : gfc_conv_intrinsic_nearest (se, expr);
11329 : 1595 : break;
11330 : :
11331 : 68 : case GFC_ISYM_NORM2:
11332 : 68 : gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, true);
11333 : 68 : break;
11334 : :
11335 : 228 : case GFC_ISYM_NOT:
11336 : 228 : gfc_conv_intrinsic_not (se, expr);
11337 : 228 : break;
11338 : :
11339 : 12 : case GFC_ISYM_OR:
11340 : 12 : gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11341 : 12 : break;
11342 : :
11343 : 468 : case GFC_ISYM_OUT_OF_RANGE:
11344 : 468 : gfc_conv_intrinsic_out_of_range (se, expr);
11345 : 468 : break;
11346 : :
11347 : 36 : case GFC_ISYM_PARITY:
11348 : 36 : gfc_conv_intrinsic_arith (se, expr, NE_EXPR, false);
11349 : 36 : break;
11350 : :
11351 : 5045 : case GFC_ISYM_PRESENT:
11352 : 5045 : gfc_conv_intrinsic_present (se, expr);
11353 : 5045 : break;
11354 : :
11355 : 344 : case GFC_ISYM_PRODUCT:
11356 : 344 : gfc_conv_intrinsic_arith (se, expr, MULT_EXPR, false);
11357 : 344 : break;
11358 : :
11359 : 10467 : case GFC_ISYM_RANK:
11360 : 10467 : gfc_conv_intrinsic_rank (se, expr);
11361 : 10467 : break;
11362 : :
11363 : 48 : case GFC_ISYM_RRSPACING:
11364 : 48 : gfc_conv_intrinsic_rrspacing (se, expr);
11365 : 48 : break;
11366 : :
11367 : 262 : case GFC_ISYM_SET_EXPONENT:
11368 : 262 : gfc_conv_intrinsic_set_exponent (se, expr);
11369 : 262 : break;
11370 : :
11371 : 72 : case GFC_ISYM_SCALE:
11372 : 72 : gfc_conv_intrinsic_scale (se, expr);
11373 : 72 : break;
11374 : :
11375 : 4775 : case GFC_ISYM_SHAPE:
11376 : 4775 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_SHAPE);
11377 : 4775 : break;
11378 : :
11379 : 424 : case GFC_ISYM_SIGN:
11380 : 424 : gfc_conv_intrinsic_sign (se, expr);
11381 : 424 : break;
11382 : :
11383 : 14518 : case GFC_ISYM_SIZE:
11384 : 14518 : gfc_conv_intrinsic_size (se, expr);
11385 : 14518 : break;
11386 : :
11387 : 1296 : case GFC_ISYM_SIZEOF:
11388 : 1296 : case GFC_ISYM_C_SIZEOF:
11389 : 1296 : gfc_conv_intrinsic_sizeof (se, expr);
11390 : 1296 : break;
11391 : :
11392 : 802 : case GFC_ISYM_STORAGE_SIZE:
11393 : 802 : gfc_conv_intrinsic_storage_size (se, expr);
11394 : 802 : break;
11395 : :
11396 : 70 : case GFC_ISYM_SPACING:
11397 : 70 : gfc_conv_intrinsic_spacing (se, expr);
11398 : 70 : break;
11399 : :
11400 : 1868 : case GFC_ISYM_STRIDE:
11401 : 1868 : conv_intrinsic_stride (se, expr);
11402 : 1868 : break;
11403 : :
11404 : 1967 : case GFC_ISYM_SUM:
11405 : 1967 : gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, false);
11406 : 1967 : break;
11407 : :
11408 : 31 : case GFC_ISYM_TEAM_NUMBER:
11409 : 31 : conv_intrinsic_team_number (se, expr);
11410 : 31 : break;
11411 : :
11412 : 3508 : case GFC_ISYM_TRANSFER:
11413 : 3508 : if (se->ss && se->ss->info->useflags)
11414 : : /* Access the previously obtained result. */
11415 : 255 : gfc_conv_tmp_array_ref (se);
11416 : : else
11417 : 3253 : gfc_conv_intrinsic_transfer (se, expr);
11418 : : break;
11419 : :
11420 : 0 : case GFC_ISYM_TTYNAM:
11421 : 0 : gfc_conv_intrinsic_ttynam (se, expr);
11422 : 0 : break;
11423 : :
11424 : 5631 : case GFC_ISYM_UBOUND:
11425 : 5631 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_UBOUND);
11426 : 5631 : break;
11427 : :
11428 : 191 : case GFC_ISYM_UCOBOUND:
11429 : 191 : conv_intrinsic_cobound (se, expr);
11430 : 191 : break;
11431 : :
11432 : 18 : case GFC_ISYM_XOR:
11433 : 18 : gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11434 : 18 : break;
11435 : :
11436 : 8565 : case GFC_ISYM_LOC:
11437 : 8565 : gfc_conv_intrinsic_loc (se, expr);
11438 : 8565 : break;
11439 : :
11440 : 858 : case GFC_ISYM_THIS_IMAGE:
11441 : : /* For num_images() == 1, handle as LCOBOUND. */
11442 : 858 : if (expr->value.function.actual->expr
11443 : 356 : && flag_coarray == GFC_FCOARRAY_SINGLE)
11444 : 208 : conv_intrinsic_cobound (se, expr);
11445 : : else
11446 : 650 : trans_this_image (se, expr);
11447 : : break;
11448 : :
11449 : 152 : case GFC_ISYM_IMAGE_INDEX:
11450 : 152 : trans_image_index (se, expr);
11451 : 152 : break;
11452 : :
11453 : 16 : case GFC_ISYM_IMAGE_STATUS:
11454 : 16 : conv_intrinsic_image_status (se, expr);
11455 : 16 : break;
11456 : :
11457 : 398 : case GFC_ISYM_NUM_IMAGES:
11458 : 398 : trans_num_images (se, expr);
11459 : 398 : break;
11460 : :
11461 : 1318 : case GFC_ISYM_ACCESS:
11462 : 1318 : case GFC_ISYM_CHDIR:
11463 : 1318 : case GFC_ISYM_CHMOD:
11464 : 1318 : case GFC_ISYM_DTIME:
11465 : 1318 : case GFC_ISYM_ETIME:
11466 : 1318 : case GFC_ISYM_EXTENDS_TYPE_OF:
11467 : 1318 : case GFC_ISYM_FGET:
11468 : 1318 : case GFC_ISYM_FGETC:
11469 : 1318 : case GFC_ISYM_FNUM:
11470 : 1318 : case GFC_ISYM_FPUT:
11471 : 1318 : case GFC_ISYM_FPUTC:
11472 : 1318 : case GFC_ISYM_FSTAT:
11473 : 1318 : case GFC_ISYM_FTELL:
11474 : 1318 : case GFC_ISYM_GETCWD:
11475 : 1318 : case GFC_ISYM_GETGID:
11476 : 1318 : case GFC_ISYM_GETPID:
11477 : 1318 : case GFC_ISYM_GETUID:
11478 : 1318 : case GFC_ISYM_HOSTNM:
11479 : 1318 : case GFC_ISYM_IERRNO:
11480 : 1318 : case GFC_ISYM_IRAND:
11481 : 1318 : case GFC_ISYM_ISATTY:
11482 : 1318 : case GFC_ISYM_JN2:
11483 : 1318 : case GFC_ISYM_LINK:
11484 : 1318 : case GFC_ISYM_LSTAT:
11485 : 1318 : case GFC_ISYM_MATMUL:
11486 : 1318 : case GFC_ISYM_MCLOCK:
11487 : 1318 : case GFC_ISYM_MCLOCK8:
11488 : 1318 : case GFC_ISYM_RAND:
11489 : 1318 : case GFC_ISYM_REDUCE:
11490 : 1318 : case GFC_ISYM_RENAME:
11491 : 1318 : case GFC_ISYM_SECOND:
11492 : 1318 : case GFC_ISYM_SECNDS:
11493 : 1318 : case GFC_ISYM_SIGNAL:
11494 : 1318 : case GFC_ISYM_STAT:
11495 : 1318 : case GFC_ISYM_SYMLNK:
11496 : 1318 : case GFC_ISYM_SYSTEM:
11497 : 1318 : case GFC_ISYM_TIME:
11498 : 1318 : case GFC_ISYM_TIME8:
11499 : 1318 : case GFC_ISYM_UMASK:
11500 : 1318 : case GFC_ISYM_UNLINK:
11501 : 1318 : case GFC_ISYM_YN2:
11502 : 1318 : gfc_conv_intrinsic_funcall (se, expr);
11503 : 1318 : break;
11504 : :
11505 : 0 : case GFC_ISYM_EOSHIFT:
11506 : 0 : case GFC_ISYM_PACK:
11507 : 0 : case GFC_ISYM_RESHAPE:
11508 : : /* For those, expr->rank should always be >0 and thus the if above the
11509 : : switch should have matched. */
11510 : 0 : gcc_unreachable ();
11511 : 3804 : break;
11512 : :
11513 : 3804 : default:
11514 : 3804 : gfc_conv_intrinsic_lib_function (se, expr);
11515 : 3804 : break;
11516 : : }
11517 : : }
11518 : :
11519 : :
11520 : : static gfc_ss *
11521 : 1558 : walk_inline_intrinsic_transpose (gfc_ss *ss, gfc_expr *expr)
11522 : : {
11523 : 1558 : gfc_ss *arg_ss, *tmp_ss;
11524 : 1558 : gfc_actual_arglist *arg;
11525 : :
11526 : 1558 : arg = expr->value.function.actual;
11527 : :
11528 : 1558 : gcc_assert (arg->expr);
11529 : :
11530 : 1558 : arg_ss = gfc_walk_subexpr (gfc_ss_terminator, arg->expr);
11531 : 1558 : gcc_assert (arg_ss != gfc_ss_terminator);
11532 : :
11533 : : for (tmp_ss = arg_ss; ; tmp_ss = tmp_ss->next)
11534 : : {
11535 : 1663 : if (tmp_ss->info->type != GFC_SS_SCALAR
11536 : : && tmp_ss->info->type != GFC_SS_REFERENCE)
11537 : : {
11538 : 1626 : gcc_assert (tmp_ss->dimen == 2);
11539 : :
11540 : : /* We just invert dimensions. */
11541 : 1626 : std::swap (tmp_ss->dim[0], tmp_ss->dim[1]);
11542 : : }
11543 : :
11544 : : /* Stop when tmp_ss points to the last valid element of the chain... */
11545 : 1663 : if (tmp_ss->next == gfc_ss_terminator)
11546 : : break;
11547 : : }
11548 : :
11549 : : /* ... so that we can attach the rest of the chain to it. */
11550 : 1558 : tmp_ss->next = ss;
11551 : :
11552 : 1558 : return arg_ss;
11553 : : }
11554 : :
11555 : :
11556 : : /* Move the given dimension of the given gfc_ss list to a nested gfc_ss list.
11557 : : This has the side effect of reversing the nested list, so there is no
11558 : : need to call gfc_reverse_ss on it (the given list is assumed not to be
11559 : : reversed yet). */
11560 : :
11561 : : static gfc_ss *
11562 : 2447 : nest_loop_dimension (gfc_ss *ss, int dim)
11563 : : {
11564 : 2447 : int ss_dim, i;
11565 : 2447 : gfc_ss *new_ss, *prev_ss = gfc_ss_terminator;
11566 : 2447 : gfc_loopinfo *new_loop;
11567 : :
11568 : 2447 : gcc_assert (ss != gfc_ss_terminator);
11569 : :
11570 : 5962 : for (; ss != gfc_ss_terminator; ss = ss->next)
11571 : : {
11572 : 3515 : new_ss = gfc_get_ss ();
11573 : 3515 : new_ss->next = prev_ss;
11574 : 3515 : new_ss->parent = ss;
11575 : 3515 : new_ss->info = ss->info;
11576 : 3515 : new_ss->info->refcount++;
11577 : 3515 : if (ss->dimen != 0)
11578 : : {
11579 : 3452 : gcc_assert (ss->info->type != GFC_SS_SCALAR
11580 : : && ss->info->type != GFC_SS_REFERENCE);
11581 : :
11582 : 3452 : new_ss->dimen = 1;
11583 : 3452 : new_ss->dim[0] = ss->dim[dim];
11584 : :
11585 : 3452 : gcc_assert (dim < ss->dimen);
11586 : :
11587 : 3452 : ss_dim = --ss->dimen;
11588 : 6814 : for (i = dim; i < ss_dim; i++)
11589 : 3362 : ss->dim[i] = ss->dim[i + 1];
11590 : :
11591 : 3452 : ss->dim[ss_dim] = 0;
11592 : : }
11593 : 3515 : prev_ss = new_ss;
11594 : :
11595 : 3515 : if (ss->nested_ss)
11596 : : {
11597 : 81 : ss->nested_ss->parent = new_ss;
11598 : 81 : new_ss->nested_ss = ss->nested_ss;
11599 : : }
11600 : 3515 : ss->nested_ss = new_ss;
11601 : : }
11602 : :
11603 : 2447 : new_loop = gfc_get_loopinfo ();
11604 : 2447 : gfc_init_loopinfo (new_loop);
11605 : :
11606 : 2447 : gcc_assert (prev_ss != NULL);
11607 : 2447 : gcc_assert (prev_ss != gfc_ss_terminator);
11608 : 2447 : gfc_add_ss_to_loop (new_loop, prev_ss);
11609 : 2447 : return new_ss->parent;
11610 : : }
11611 : :
11612 : :
11613 : : /* Create the gfc_ss list for the SUM/PRODUCT arguments when the function
11614 : : is to be inlined. */
11615 : :
11616 : : static gfc_ss *
11617 : 575 : walk_inline_intrinsic_arith (gfc_ss *ss, gfc_expr *expr)
11618 : : {
11619 : 575 : gfc_ss *tmp_ss, *tail, *array_ss;
11620 : 575 : gfc_actual_arglist *arg1, *arg2, *arg3;
11621 : 575 : int sum_dim;
11622 : 575 : bool scalar_mask = false;
11623 : :
11624 : : /* The rank of the result will be determined later. */
11625 : 575 : arg1 = expr->value.function.actual;
11626 : 575 : arg2 = arg1->next;
11627 : 575 : arg3 = arg2->next;
11628 : 575 : gcc_assert (arg3 != NULL);
11629 : :
11630 : 575 : if (expr->rank == 0)
11631 : : return ss;
11632 : :
11633 : 575 : tmp_ss = gfc_ss_terminator;
11634 : :
11635 : 575 : if (arg3->expr)
11636 : : {
11637 : 118 : gfc_ss *mask_ss;
11638 : :
11639 : 118 : mask_ss = gfc_walk_subexpr (tmp_ss, arg3->expr);
11640 : 118 : if (mask_ss == tmp_ss)
11641 : 34 : scalar_mask = 1;
11642 : :
11643 : : tmp_ss = mask_ss;
11644 : : }
11645 : :
11646 : 575 : array_ss = gfc_walk_subexpr (tmp_ss, arg1->expr);
11647 : 575 : gcc_assert (array_ss != tmp_ss);
11648 : :
11649 : : /* Odd thing: If the mask is scalar, it is used by the frontend after
11650 : : the array (to make an if around the nested loop). Thus it shall
11651 : : be after array_ss once the gfc_ss list is reversed. */
11652 : 575 : if (scalar_mask)
11653 : 34 : tmp_ss = gfc_get_scalar_ss (array_ss, arg3->expr);
11654 : : else
11655 : : tmp_ss = array_ss;
11656 : :
11657 : : /* "Hide" the dimension on which we will sum in the first arg's scalarization
11658 : : chain. */
11659 : 575 : sum_dim = mpz_get_si (arg2->expr->value.integer) - 1;
11660 : 575 : tail = nest_loop_dimension (tmp_ss, sum_dim);
11661 : 575 : tail->next = ss;
11662 : :
11663 : 575 : return tmp_ss;
11664 : : }
11665 : :
11666 : :
11667 : : /* Create the gfc_ss list for the arguments to MINLOC or MAXLOC when the
11668 : : function is to be inlined. */
11669 : :
11670 : : static gfc_ss *
11671 : 4585 : walk_inline_intrinsic_minmaxloc (gfc_ss *ss, gfc_expr *expr ATTRIBUTE_UNUSED)
11672 : : {
11673 : 4585 : if (expr->rank == 0)
11674 : : return ss;
11675 : :
11676 : 4585 : gfc_actual_arglist *array_arg = expr->value.function.actual;
11677 : 4585 : gfc_actual_arglist *dim_arg = array_arg->next;
11678 : 4585 : gfc_actual_arglist *mask_arg = dim_arg->next;
11679 : 4585 : gfc_actual_arglist *kind_arg = mask_arg->next;
11680 : 4585 : gfc_actual_arglist *back_arg = kind_arg->next;
11681 : :
11682 : 4585 : gfc_expr *array = array_arg->expr;
11683 : 4585 : gfc_expr *dim = dim_arg->expr;
11684 : 4585 : gfc_expr *mask = mask_arg->expr;
11685 : 4585 : gfc_expr *back = back_arg->expr;
11686 : :
11687 : 4585 : if (dim == nullptr)
11688 : 2713 : return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
11689 : :
11690 : 1872 : gfc_ss *tmp_ss = gfc_ss_terminator;
11691 : :
11692 : 1872 : bool scalar_mask = false;
11693 : 1872 : if (mask)
11694 : : {
11695 : 1250 : gfc_ss *mask_ss = gfc_walk_subexpr (tmp_ss, mask);
11696 : 1250 : if (mask_ss == tmp_ss)
11697 : : scalar_mask = true;
11698 : 866 : else if (maybe_absent_optional_variable (mask))
11699 : 20 : mask_ss->info->can_be_null_ref = true;
11700 : :
11701 : : tmp_ss = mask_ss;
11702 : : }
11703 : :
11704 : 1872 : gfc_ss *array_ss = gfc_walk_subexpr (tmp_ss, array);
11705 : 1872 : gcc_assert (array_ss != tmp_ss);
11706 : :
11707 : 1872 : tmp_ss = array_ss;
11708 : :
11709 : : /* Move the dimension on which we will sum to a separate nested scalarization
11710 : : chain, "hiding" that dimension from the outer scalarization. */
11711 : 1872 : int dim_val = mpz_get_si (dim->value.integer);
11712 : 1872 : gfc_ss *tail = nest_loop_dimension (tmp_ss, dim_val - 1);
11713 : :
11714 : 1872 : if (back && array->rank > 1)
11715 : : {
11716 : : /* If there are nested scalarization loops, include BACK in the
11717 : : scalarization chains to avoid evaluating it multiple times in a loop.
11718 : : Otherwise, prefer to handle it outside of scalarization. */
11719 : 1872 : gfc_ss *back_ss = gfc_get_scalar_ss (ss, back);
11720 : 1872 : back_ss->info->type = GFC_SS_REFERENCE;
11721 : 1872 : if (maybe_absent_optional_variable (back))
11722 : 16 : back_ss->info->can_be_null_ref = true;
11723 : :
11724 : 1872 : tail->next = back_ss;
11725 : 1872 : }
11726 : : else
11727 : 0 : tail->next = ss;
11728 : :
11729 : 1872 : if (scalar_mask)
11730 : : {
11731 : 384 : tmp_ss = gfc_get_scalar_ss (tmp_ss, mask);
11732 : : /* MASK can be a forwarded optional argument, so make the necessary setup
11733 : : to avoid the scalarizer generating any unguarded pointer dereference in
11734 : : that case. */
11735 : 384 : tmp_ss->info->type = GFC_SS_REFERENCE;
11736 : 384 : if (maybe_absent_optional_variable (mask))
11737 : 4 : tmp_ss->info->can_be_null_ref = true;
11738 : : }
11739 : :
11740 : : return tmp_ss;
11741 : : }
11742 : :
11743 : :
11744 : : static gfc_ss *
11745 : 6718 : walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr)
11746 : : {
11747 : :
11748 : 6718 : switch (expr->value.function.isym->id)
11749 : : {
11750 : 575 : case GFC_ISYM_PRODUCT:
11751 : 575 : case GFC_ISYM_SUM:
11752 : 575 : return walk_inline_intrinsic_arith (ss, expr);
11753 : :
11754 : 1558 : case GFC_ISYM_TRANSPOSE:
11755 : 1558 : return walk_inline_intrinsic_transpose (ss, expr);
11756 : :
11757 : 4585 : case GFC_ISYM_MAXLOC:
11758 : 4585 : case GFC_ISYM_MINLOC:
11759 : 4585 : return walk_inline_intrinsic_minmaxloc (ss, expr);
11760 : :
11761 : 0 : default:
11762 : 0 : gcc_unreachable ();
11763 : : }
11764 : : gcc_unreachable ();
11765 : : }
11766 : :
11767 : :
11768 : : /* This generates code to execute before entering the scalarization loop.
11769 : : Currently does nothing. */
11770 : :
11771 : : void
11772 : 10673 : gfc_add_intrinsic_ss_code (gfc_loopinfo * loop ATTRIBUTE_UNUSED, gfc_ss * ss)
11773 : : {
11774 : 10673 : switch (ss->info->expr->value.function.isym->id)
11775 : : {
11776 : 10673 : case GFC_ISYM_UBOUND:
11777 : 10673 : case GFC_ISYM_LBOUND:
11778 : 10673 : case GFC_ISYM_UCOBOUND:
11779 : 10673 : case GFC_ISYM_LCOBOUND:
11780 : 10673 : case GFC_ISYM_MAXLOC:
11781 : 10673 : case GFC_ISYM_MINLOC:
11782 : 10673 : case GFC_ISYM_THIS_IMAGE:
11783 : 10673 : case GFC_ISYM_SHAPE:
11784 : 10673 : break;
11785 : :
11786 : 0 : default:
11787 : 0 : gcc_unreachable ();
11788 : : }
11789 : 10673 : }
11790 : :
11791 : :
11792 : : /* The LBOUND, LCOBOUND, UBOUND, UCOBOUND, and SHAPE intrinsics with
11793 : : one parameter are expanded into code inside the scalarization loop. */
11794 : :
11795 : : static gfc_ss *
11796 : 9802 : gfc_walk_intrinsic_bound (gfc_ss * ss, gfc_expr * expr)
11797 : : {
11798 : 9802 : if (expr->value.function.actual->expr->ts.type == BT_CLASS)
11799 : 410 : gfc_add_class_array_ref (expr->value.function.actual->expr);
11800 : :
11801 : : /* The two argument version returns a scalar. */
11802 : 9802 : if (expr->value.function.isym->id != GFC_ISYM_SHAPE
11803 : 3403 : && expr->value.function.actual->next->expr)
11804 : : return ss;
11805 : :
11806 : 9802 : return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
11807 : : }
11808 : :
11809 : :
11810 : : /* Walk an intrinsic array libcall. */
11811 : :
11812 : : static gfc_ss *
11813 : 13477 : gfc_walk_intrinsic_libfunc (gfc_ss * ss, gfc_expr * expr)
11814 : : {
11815 : 13477 : gcc_assert (expr->rank > 0);
11816 : 13477 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
11817 : : }
11818 : :
11819 : :
11820 : : /* Return whether the function call expression EXPR will be expanded
11821 : : inline by gfc_conv_intrinsic_function. */
11822 : :
11823 : : bool
11824 : 275129 : gfc_inline_intrinsic_function_p (gfc_expr *expr)
11825 : : {
11826 : 275129 : gfc_actual_arglist *args, *dim_arg, *mask_arg;
11827 : 275129 : gfc_expr *maskexpr;
11828 : :
11829 : 275129 : gfc_intrinsic_sym *isym = expr->value.function.isym;
11830 : 275129 : if (!isym)
11831 : : return false;
11832 : :
11833 : 275093 : switch (isym->id)
11834 : : {
11835 : 5072 : case GFC_ISYM_PRODUCT:
11836 : 5072 : case GFC_ISYM_SUM:
11837 : : /* Disable inline expansion if code size matters. */
11838 : 5072 : if (optimize_size)
11839 : : return false;
11840 : :
11841 : 4223 : args = expr->value.function.actual;
11842 : 4223 : dim_arg = args->next;
11843 : :
11844 : : /* We need to be able to subset the SUM argument at compile-time. */
11845 : 4223 : if (dim_arg->expr && dim_arg->expr->expr_type != EXPR_CONSTANT)
11846 : : return false;
11847 : :
11848 : : /* FIXME: If MASK is optional for a more than two-dimensional
11849 : : argument, the scalarizer gets confused if the mask is
11850 : : absent. See PR 82995. For now, fall back to the library
11851 : : function. */
11852 : :
11853 : 3611 : mask_arg = dim_arg->next;
11854 : 3611 : maskexpr = mask_arg->expr;
11855 : :
11856 : 3611 : if (expr->rank > 0 && maskexpr && maskexpr->expr_type == EXPR_VARIABLE
11857 : 276 : && maskexpr->symtree->n.sym->attr.dummy
11858 : 276 : && maskexpr->symtree->n.sym->attr.optional)
11859 : : return false;
11860 : :
11861 : : return true;
11862 : :
11863 : : case GFC_ISYM_TRANSPOSE:
11864 : : return true;
11865 : :
11866 : 42926 : case GFC_ISYM_MINLOC:
11867 : 42926 : case GFC_ISYM_MAXLOC:
11868 : 42926 : {
11869 : 42926 : if ((isym->id == GFC_ISYM_MINLOC
11870 : 23719 : && (flag_inline_intrinsics
11871 : 23719 : & GFC_FLAG_INLINE_INTRINSIC_MINLOC) == 0)
11872 : 34927 : || (isym->id == GFC_ISYM_MAXLOC
11873 : 19207 : && (flag_inline_intrinsics
11874 : 19207 : & GFC_FLAG_INLINE_INTRINSIC_MAXLOC) == 0))
11875 : : return false;
11876 : :
11877 : 28758 : gfc_actual_arglist *array_arg = expr->value.function.actual;
11878 : 28758 : gfc_actual_arglist *dim_arg = array_arg->next;
11879 : :
11880 : 28758 : gfc_expr *array = array_arg->expr;
11881 : 28758 : gfc_expr *dim = dim_arg->expr;
11882 : :
11883 : 28758 : if (!(array->ts.type == BT_INTEGER
11884 : : || array->ts.type == BT_REAL))
11885 : : return false;
11886 : :
11887 : 27158 : if (array->rank == 1)
11888 : : return true;
11889 : :
11890 : 15515 : if (dim != nullptr
11891 : 9616 : && dim->expr_type != EXPR_CONSTANT)
11892 : : return false;
11893 : :
11894 : : return true;
11895 : : }
11896 : :
11897 : : default:
11898 : : return false;
11899 : : }
11900 : : }
11901 : :
11902 : :
11903 : : /* Returns nonzero if the specified intrinsic function call maps directly to
11904 : : an external library call. Should only be used for functions that return
11905 : : arrays. */
11906 : :
11907 : : int
11908 : 81436 : gfc_is_intrinsic_libcall (gfc_expr * expr)
11909 : : {
11910 : 81436 : gcc_assert (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym);
11911 : 81436 : gcc_assert (expr->rank > 0);
11912 : :
11913 : 81436 : if (gfc_inline_intrinsic_function_p (expr))
11914 : : return 0;
11915 : :
11916 : 68990 : switch (expr->value.function.isym->id)
11917 : : {
11918 : : case GFC_ISYM_ALL:
11919 : : case GFC_ISYM_ANY:
11920 : : case GFC_ISYM_COUNT:
11921 : : case GFC_ISYM_FINDLOC:
11922 : : case GFC_ISYM_JN2:
11923 : : case GFC_ISYM_IANY:
11924 : : case GFC_ISYM_IALL:
11925 : : case GFC_ISYM_IPARITY:
11926 : : case GFC_ISYM_MATMUL:
11927 : : case GFC_ISYM_MAXLOC:
11928 : : case GFC_ISYM_MAXVAL:
11929 : : case GFC_ISYM_MINLOC:
11930 : : case GFC_ISYM_MINVAL:
11931 : : case GFC_ISYM_NORM2:
11932 : : case GFC_ISYM_PARITY:
11933 : : case GFC_ISYM_PRODUCT:
11934 : : case GFC_ISYM_SUM:
11935 : : case GFC_ISYM_SPREAD:
11936 : : case GFC_ISYM_YN2:
11937 : : /* Ignore absent optional parameters. */
11938 : : return 1;
11939 : :
11940 : 16429 : case GFC_ISYM_CSHIFT:
11941 : 16429 : case GFC_ISYM_EOSHIFT:
11942 : 16429 : case GFC_ISYM_GET_TEAM:
11943 : 16429 : case GFC_ISYM_FAILED_IMAGES:
11944 : 16429 : case GFC_ISYM_STOPPED_IMAGES:
11945 : 16429 : case GFC_ISYM_PACK:
11946 : 16429 : case GFC_ISYM_REDUCE:
11947 : 16429 : case GFC_ISYM_RESHAPE:
11948 : 16429 : case GFC_ISYM_UNPACK:
11949 : : /* Pass absent optional parameters. */
11950 : 16429 : return 2;
11951 : :
11952 : : default:
11953 : : return 0;
11954 : : }
11955 : : }
11956 : :
11957 : : /* Walk an intrinsic function. */
11958 : : gfc_ss *
11959 : 51864 : gfc_walk_intrinsic_function (gfc_ss * ss, gfc_expr * expr,
11960 : : gfc_intrinsic_sym * isym)
11961 : : {
11962 : 51864 : gcc_assert (isym);
11963 : :
11964 : 51864 : if (isym->elemental)
11965 : 17505 : return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
11966 : : expr->value.function.isym,
11967 : 17505 : GFC_SS_SCALAR);
11968 : :
11969 : 34359 : if (expr->rank == 0 && expr->corank == 0)
11970 : : return ss;
11971 : :
11972 : 29997 : if (gfc_inline_intrinsic_function_p (expr))
11973 : 6718 : return walk_inline_intrinsic_function (ss, expr);
11974 : :
11975 : 23279 : if (expr->rank != 0 && gfc_is_intrinsic_libcall (expr))
11976 : 12573 : return gfc_walk_intrinsic_libfunc (ss, expr);
11977 : :
11978 : : /* Special cases. */
11979 : 10706 : switch (isym->id)
11980 : : {
11981 : 9802 : case GFC_ISYM_LBOUND:
11982 : 9802 : case GFC_ISYM_LCOBOUND:
11983 : 9802 : case GFC_ISYM_UBOUND:
11984 : 9802 : case GFC_ISYM_UCOBOUND:
11985 : 9802 : case GFC_ISYM_THIS_IMAGE:
11986 : 9802 : case GFC_ISYM_SHAPE:
11987 : 9802 : return gfc_walk_intrinsic_bound (ss, expr);
11988 : :
11989 : 904 : case GFC_ISYM_TRANSFER:
11990 : 904 : case GFC_ISYM_CAF_GET:
11991 : 904 : return gfc_walk_intrinsic_libfunc (ss, expr);
11992 : :
11993 : 0 : default:
11994 : : /* This probably meant someone forgot to add an intrinsic to the above
11995 : : list(s) when they implemented it, or something's gone horribly
11996 : : wrong. */
11997 : 0 : gcc_unreachable ();
11998 : : }
11999 : : }
12000 : :
12001 : : static tree
12002 : 63 : conv_co_collective (gfc_code *code)
12003 : : {
12004 : 63 : gfc_se argse;
12005 : 63 : stmtblock_t block, post_block;
12006 : 63 : tree fndecl, array = NULL_TREE, strlen, image_index, stat, errmsg, errmsg_len;
12007 : 63 : gfc_expr *image_idx_expr, *stat_expr, *errmsg_expr, *opr_expr;
12008 : :
12009 : 63 : gfc_start_block (&block);
12010 : 63 : gfc_init_block (&post_block);
12011 : :
12012 : 63 : if (code->resolved_isym->id == GFC_ISYM_CO_REDUCE)
12013 : : {
12014 : 7 : opr_expr = code->ext.actual->next->expr;
12015 : 7 : image_idx_expr = code->ext.actual->next->next->expr;
12016 : 7 : stat_expr = code->ext.actual->next->next->next->expr;
12017 : 7 : errmsg_expr = code->ext.actual->next->next->next->next->expr;
12018 : : }
12019 : : else
12020 : : {
12021 : 56 : opr_expr = NULL;
12022 : 56 : image_idx_expr = code->ext.actual->next->expr;
12023 : 56 : stat_expr = code->ext.actual->next->next->expr;
12024 : 56 : errmsg_expr = code->ext.actual->next->next->next->expr;
12025 : : }
12026 : :
12027 : : /* stat. */
12028 : 63 : if (stat_expr)
12029 : : {
12030 : 49 : gfc_init_se (&argse, NULL);
12031 : 49 : gfc_conv_expr (&argse, stat_expr);
12032 : 49 : gfc_add_block_to_block (&block, &argse.pre);
12033 : 49 : gfc_add_block_to_block (&post_block, &argse.post);
12034 : 49 : stat = argse.expr;
12035 : 49 : if (flag_coarray != GFC_FCOARRAY_SINGLE)
12036 : 22 : stat = gfc_build_addr_expr (NULL_TREE, stat);
12037 : : }
12038 : 14 : else if (flag_coarray == GFC_FCOARRAY_SINGLE)
12039 : : stat = NULL_TREE;
12040 : : else
12041 : 8 : stat = null_pointer_node;
12042 : :
12043 : : /* Early exit for GFC_FCOARRAY_SINGLE. */
12044 : 63 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
12045 : : {
12046 : 33 : if (stat != NULL_TREE)
12047 : : {
12048 : : /* For optional stats, check the pointer is valid before zero'ing. */
12049 : 27 : if (gfc_expr_attr (stat_expr).optional)
12050 : : {
12051 : 12 : tree tmp;
12052 : 12 : stmtblock_t ass_block;
12053 : 12 : gfc_start_block (&ass_block);
12054 : 12 : gfc_add_modify (&ass_block, stat,
12055 : 12 : fold_convert (TREE_TYPE (stat),
12056 : : integer_zero_node));
12057 : 12 : tmp = fold_build2 (NE_EXPR, logical_type_node,
12058 : : gfc_build_addr_expr (NULL_TREE, stat),
12059 : : null_pointer_node);
12060 : 12 : tmp = fold_build3 (COND_EXPR, void_type_node, tmp,
12061 : : gfc_finish_block (&ass_block),
12062 : : build_empty_stmt (input_location));
12063 : 12 : gfc_add_expr_to_block (&block, tmp);
12064 : : }
12065 : : else
12066 : 15 : gfc_add_modify (&block, stat,
12067 : 15 : fold_convert (TREE_TYPE (stat), integer_zero_node));
12068 : : }
12069 : 33 : return gfc_finish_block (&block);
12070 : : }
12071 : :
12072 : 60 : gfc_symbol *derived = code->ext.actual->expr->ts.type == BT_DERIVED
12073 : 30 : ? code->ext.actual->expr->ts.u.derived : NULL;
12074 : :
12075 : : /* Handle the array. */
12076 : 30 : gfc_init_se (&argse, NULL);
12077 : 30 : if (!derived || !derived->attr.alloc_comp
12078 : 1 : || code->resolved_isym->id != GFC_ISYM_CO_BROADCAST)
12079 : : {
12080 : 29 : if (code->ext.actual->expr->rank == 0)
12081 : : {
12082 : 16 : symbol_attribute attr;
12083 : 16 : gfc_clear_attr (&attr);
12084 : 16 : gfc_init_se (&argse, NULL);
12085 : 16 : gfc_conv_expr (&argse, code->ext.actual->expr);
12086 : 16 : gfc_add_block_to_block (&block, &argse.pre);
12087 : 16 : gfc_add_block_to_block (&post_block, &argse.post);
12088 : 16 : array = gfc_conv_scalar_to_descriptor (&argse, argse.expr, attr);
12089 : 16 : array = gfc_build_addr_expr (NULL_TREE, array);
12090 : : }
12091 : : else
12092 : : {
12093 : 13 : argse.want_pointer = 1;
12094 : 13 : gfc_conv_expr_descriptor (&argse, code->ext.actual->expr);
12095 : 13 : array = argse.expr;
12096 : : }
12097 : : }
12098 : :
12099 : 30 : gfc_add_block_to_block (&block, &argse.pre);
12100 : 30 : gfc_add_block_to_block (&post_block, &argse.post);
12101 : :
12102 : 30 : if (code->ext.actual->expr->ts.type == BT_CHARACTER)
12103 : 6 : strlen = argse.string_length;
12104 : : else
12105 : 24 : strlen = integer_zero_node;
12106 : :
12107 : : /* image_index. */
12108 : 30 : if (image_idx_expr)
12109 : : {
12110 : 22 : gfc_init_se (&argse, NULL);
12111 : 22 : gfc_conv_expr (&argse, image_idx_expr);
12112 : 22 : gfc_add_block_to_block (&block, &argse.pre);
12113 : 22 : gfc_add_block_to_block (&post_block, &argse.post);
12114 : 22 : image_index = fold_convert (integer_type_node, argse.expr);
12115 : : }
12116 : : else
12117 : 8 : image_index = integer_zero_node;
12118 : :
12119 : : /* errmsg. */
12120 : 30 : if (errmsg_expr)
12121 : : {
12122 : 17 : gfc_init_se (&argse, NULL);
12123 : 17 : gfc_conv_expr (&argse, errmsg_expr);
12124 : 17 : gfc_add_block_to_block (&block, &argse.pre);
12125 : 17 : gfc_add_block_to_block (&post_block, &argse.post);
12126 : 17 : errmsg = argse.expr;
12127 : 17 : errmsg_len = fold_convert (size_type_node, argse.string_length);
12128 : : }
12129 : : else
12130 : : {
12131 : 13 : errmsg = null_pointer_node;
12132 : 13 : errmsg_len = build_zero_cst (size_type_node);
12133 : : }
12134 : :
12135 : : /* Generate the function call. */
12136 : 30 : switch (code->resolved_isym->id)
12137 : : {
12138 : 12 : case GFC_ISYM_CO_BROADCAST:
12139 : 12 : fndecl = gfor_fndecl_co_broadcast;
12140 : 12 : break;
12141 : 5 : case GFC_ISYM_CO_MAX:
12142 : 5 : fndecl = gfor_fndecl_co_max;
12143 : 5 : break;
12144 : 4 : case GFC_ISYM_CO_MIN:
12145 : 4 : fndecl = gfor_fndecl_co_min;
12146 : 4 : break;
12147 : 5 : case GFC_ISYM_CO_REDUCE:
12148 : 5 : fndecl = gfor_fndecl_co_reduce;
12149 : 5 : break;
12150 : 4 : case GFC_ISYM_CO_SUM:
12151 : 4 : fndecl = gfor_fndecl_co_sum;
12152 : 4 : break;
12153 : 0 : default:
12154 : 0 : gcc_unreachable ();
12155 : : }
12156 : :
12157 : 30 : if (derived && derived->attr.alloc_comp
12158 : 1 : && code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12159 : : /* The derived type has the attribute 'alloc_comp'. */
12160 : : {
12161 : 2 : tree tmp = gfc_bcast_alloc_comp (derived, code->ext.actual->expr,
12162 : 1 : code->ext.actual->expr->rank,
12163 : : image_index, stat, errmsg, errmsg_len);
12164 : 1 : gfc_add_expr_to_block (&block, tmp);
12165 : 1 : }
12166 : : else
12167 : : {
12168 : 29 : if (code->resolved_isym->id == GFC_ISYM_CO_SUM
12169 : 25 : || code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12170 : 15 : fndecl = build_call_expr_loc (input_location, fndecl, 5, array,
12171 : : image_index, stat, errmsg, errmsg_len);
12172 : 14 : else if (code->resolved_isym->id != GFC_ISYM_CO_REDUCE)
12173 : 9 : fndecl = build_call_expr_loc (input_location, fndecl, 6, array,
12174 : : image_index, stat, errmsg,
12175 : : strlen, errmsg_len);
12176 : : else
12177 : : {
12178 : 5 : tree opr, opr_flags;
12179 : :
12180 : : // FIXME: Handle TS29113's bind(C) strings with descriptor.
12181 : 5 : int opr_flag_int;
12182 : 5 : if (gfc_is_proc_ptr_comp (opr_expr))
12183 : : {
12184 : 0 : gfc_symbol *sym = gfc_get_proc_ptr_comp (opr_expr)->ts.interface;
12185 : 0 : opr_flag_int = sym->attr.dimension
12186 : 0 : || (sym->ts.type == BT_CHARACTER
12187 : 0 : && !sym->attr.is_bind_c)
12188 : 0 : ? GFC_CAF_BYREF : 0;
12189 : 0 : opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12190 : 0 : && !sym->attr.is_bind_c
12191 : 0 : ? GFC_CAF_HIDDENLEN : 0;
12192 : 0 : opr_flag_int |= sym->formal->sym->attr.value
12193 : 0 : ? GFC_CAF_ARG_VALUE : 0;
12194 : : }
12195 : : else
12196 : : {
12197 : 5 : opr_flag_int = gfc_return_by_reference (opr_expr->symtree->n.sym)
12198 : 5 : ? GFC_CAF_BYREF : 0;
12199 : 10 : opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12200 : 0 : && !opr_expr->symtree->n.sym->attr.is_bind_c
12201 : 5 : ? GFC_CAF_HIDDENLEN : 0;
12202 : 5 : opr_flag_int |= opr_expr->symtree->n.sym->formal->sym->attr.value
12203 : 5 : ? GFC_CAF_ARG_VALUE : 0;
12204 : : }
12205 : 5 : opr_flags = build_int_cst (integer_type_node, opr_flag_int);
12206 : 5 : gfc_conv_expr (&argse, opr_expr);
12207 : 5 : opr = argse.expr;
12208 : 5 : fndecl = build_call_expr_loc (input_location, fndecl, 8, array, opr,
12209 : : opr_flags, image_index, stat, errmsg,
12210 : : strlen, errmsg_len);
12211 : : }
12212 : : }
12213 : :
12214 : 30 : gfc_add_expr_to_block (&block, fndecl);
12215 : 30 : gfc_add_block_to_block (&block, &post_block);
12216 : :
12217 : 30 : return gfc_finish_block (&block);
12218 : : }
12219 : :
12220 : :
12221 : : static tree
12222 : 68 : conv_intrinsic_atomic_op (gfc_code *code)
12223 : : {
12224 : 68 : gfc_se argse;
12225 : 68 : tree tmp, atom, value, old = NULL_TREE, stat = NULL_TREE;
12226 : 68 : stmtblock_t block, post_block;
12227 : 68 : gfc_expr *atom_expr = code->ext.actual->expr;
12228 : 68 : gfc_expr *stat_expr;
12229 : 68 : built_in_function fn;
12230 : :
12231 : 68 : if (atom_expr->expr_type == EXPR_FUNCTION
12232 : 0 : && atom_expr->value.function.isym
12233 : 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12234 : 0 : atom_expr = atom_expr->value.function.actual->expr;
12235 : :
12236 : 68 : gfc_start_block (&block);
12237 : 68 : gfc_init_block (&post_block);
12238 : :
12239 : 68 : gfc_init_se (&argse, NULL);
12240 : 68 : argse.want_pointer = 1;
12241 : 68 : gfc_conv_expr (&argse, atom_expr);
12242 : 68 : gfc_add_block_to_block (&block, &argse.pre);
12243 : 68 : gfc_add_block_to_block (&post_block, &argse.post);
12244 : 68 : atom = argse.expr;
12245 : :
12246 : 68 : gfc_init_se (&argse, NULL);
12247 : 68 : if (flag_coarray == GFC_FCOARRAY_LIB
12248 : 29 : && code->ext.actual->next->expr->ts.kind == atom_expr->ts.kind)
12249 : 28 : argse.want_pointer = 1;
12250 : 68 : gfc_conv_expr (&argse, code->ext.actual->next->expr);
12251 : 68 : gfc_add_block_to_block (&block, &argse.pre);
12252 : 68 : gfc_add_block_to_block (&post_block, &argse.post);
12253 : 68 : value = argse.expr;
12254 : :
12255 : 68 : switch (code->resolved_isym->id)
12256 : : {
12257 : 42 : case GFC_ISYM_ATOMIC_ADD:
12258 : 42 : case GFC_ISYM_ATOMIC_AND:
12259 : 42 : case GFC_ISYM_ATOMIC_DEF:
12260 : 42 : case GFC_ISYM_ATOMIC_OR:
12261 : 42 : case GFC_ISYM_ATOMIC_XOR:
12262 : 42 : stat_expr = code->ext.actual->next->next->expr;
12263 : 42 : if (flag_coarray == GFC_FCOARRAY_LIB)
12264 : 18 : old = null_pointer_node;
12265 : : break;
12266 : 26 : default:
12267 : 26 : gfc_init_se (&argse, NULL);
12268 : 26 : if (flag_coarray == GFC_FCOARRAY_LIB)
12269 : 11 : argse.want_pointer = 1;
12270 : 26 : gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
12271 : 26 : gfc_add_block_to_block (&block, &argse.pre);
12272 : 26 : gfc_add_block_to_block (&post_block, &argse.post);
12273 : 26 : old = argse.expr;
12274 : 26 : stat_expr = code->ext.actual->next->next->next->expr;
12275 : : }
12276 : :
12277 : : /* STAT= */
12278 : 68 : if (stat_expr != NULL)
12279 : : {
12280 : 58 : gcc_assert (stat_expr->expr_type == EXPR_VARIABLE);
12281 : 58 : gfc_init_se (&argse, NULL);
12282 : 58 : if (flag_coarray == GFC_FCOARRAY_LIB)
12283 : 24 : argse.want_pointer = 1;
12284 : 58 : gfc_conv_expr_val (&argse, stat_expr);
12285 : 58 : gfc_add_block_to_block (&block, &argse.pre);
12286 : 58 : gfc_add_block_to_block (&post_block, &argse.post);
12287 : 58 : stat = argse.expr;
12288 : : }
12289 : 10 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12290 : 5 : stat = null_pointer_node;
12291 : :
12292 : 68 : if (flag_coarray == GFC_FCOARRAY_LIB)
12293 : : {
12294 : 29 : tree image_index, caf_decl, offset, token;
12295 : 29 : int op;
12296 : :
12297 : 29 : switch (code->resolved_isym->id)
12298 : : {
12299 : : case GFC_ISYM_ATOMIC_ADD:
12300 : : case GFC_ISYM_ATOMIC_FETCH_ADD:
12301 : : op = (int) GFC_CAF_ATOMIC_ADD;
12302 : : break;
12303 : 6 : case GFC_ISYM_ATOMIC_AND:
12304 : 6 : case GFC_ISYM_ATOMIC_FETCH_AND:
12305 : 6 : op = (int) GFC_CAF_ATOMIC_AND;
12306 : 6 : break;
12307 : 6 : case GFC_ISYM_ATOMIC_OR:
12308 : 6 : case GFC_ISYM_ATOMIC_FETCH_OR:
12309 : 6 : op = (int) GFC_CAF_ATOMIC_OR;
12310 : 6 : break;
12311 : 6 : case GFC_ISYM_ATOMIC_XOR:
12312 : 6 : case GFC_ISYM_ATOMIC_FETCH_XOR:
12313 : 6 : op = (int) GFC_CAF_ATOMIC_XOR;
12314 : 6 : break;
12315 : 6 : case GFC_ISYM_ATOMIC_DEF:
12316 : 6 : op = 0; /* Unused. */
12317 : 6 : break;
12318 : 0 : default:
12319 : 0 : gcc_unreachable ();
12320 : : }
12321 : :
12322 : 29 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12323 : 29 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12324 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12325 : :
12326 : 29 : if (gfc_is_coindexed (atom_expr))
12327 : 25 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12328 : : else
12329 : 4 : image_index = integer_zero_node;
12330 : :
12331 : 29 : if (!POINTER_TYPE_P (TREE_TYPE (value)))
12332 : : {
12333 : 28 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
12334 : 28 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), value));
12335 : 28 : value = gfc_build_addr_expr (NULL_TREE, tmp);
12336 : : }
12337 : :
12338 : 29 : gfc_init_se (&argse, NULL);
12339 : 29 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12340 : : atom_expr);
12341 : :
12342 : 29 : gfc_add_block_to_block (&block, &argse.pre);
12343 : 29 : if (code->resolved_isym->id == GFC_ISYM_ATOMIC_DEF)
12344 : 6 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_def, 7,
12345 : : token, offset, image_index, value, stat,
12346 : 6 : build_int_cst (integer_type_node,
12347 : 6 : (int) atom_expr->ts.type),
12348 : 6 : build_int_cst (integer_type_node,
12349 : 6 : (int) atom_expr->ts.kind));
12350 : : else
12351 : 23 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_op, 9,
12352 : 23 : build_int_cst (integer_type_node, op),
12353 : : token, offset, image_index, value, old, stat,
12354 : 23 : build_int_cst (integer_type_node,
12355 : 23 : (int) atom_expr->ts.type),
12356 : 23 : build_int_cst (integer_type_node,
12357 : 23 : (int) atom_expr->ts.kind));
12358 : :
12359 : 29 : gfc_add_expr_to_block (&block, tmp);
12360 : 29 : gfc_add_block_to_block (&block, &argse.post);
12361 : 29 : gfc_add_block_to_block (&block, &post_block);
12362 : 29 : return gfc_finish_block (&block);
12363 : : }
12364 : :
12365 : :
12366 : 39 : switch (code->resolved_isym->id)
12367 : : {
12368 : : case GFC_ISYM_ATOMIC_ADD:
12369 : : case GFC_ISYM_ATOMIC_FETCH_ADD:
12370 : : fn = BUILT_IN_ATOMIC_FETCH_ADD_N;
12371 : : break;
12372 : 8 : case GFC_ISYM_ATOMIC_AND:
12373 : 8 : case GFC_ISYM_ATOMIC_FETCH_AND:
12374 : 8 : fn = BUILT_IN_ATOMIC_FETCH_AND_N;
12375 : 8 : break;
12376 : 9 : case GFC_ISYM_ATOMIC_DEF:
12377 : 9 : fn = BUILT_IN_ATOMIC_STORE_N;
12378 : 9 : break;
12379 : 8 : case GFC_ISYM_ATOMIC_OR:
12380 : 8 : case GFC_ISYM_ATOMIC_FETCH_OR:
12381 : 8 : fn = BUILT_IN_ATOMIC_FETCH_OR_N;
12382 : 8 : break;
12383 : 8 : case GFC_ISYM_ATOMIC_XOR:
12384 : 8 : case GFC_ISYM_ATOMIC_FETCH_XOR:
12385 : 8 : fn = BUILT_IN_ATOMIC_FETCH_XOR_N;
12386 : 8 : break;
12387 : 0 : default:
12388 : 0 : gcc_unreachable ();
12389 : : }
12390 : :
12391 : 39 : tmp = TREE_TYPE (TREE_TYPE (atom));
12392 : 78 : fn = (built_in_function) ((int) fn
12393 : 39 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12394 : 39 : + 1);
12395 : 39 : tree itype = TREE_TYPE (TREE_TYPE (atom));
12396 : 39 : tmp = builtin_decl_explicit (fn);
12397 : :
12398 : 39 : switch (code->resolved_isym->id)
12399 : : {
12400 : 24 : case GFC_ISYM_ATOMIC_ADD:
12401 : 24 : case GFC_ISYM_ATOMIC_AND:
12402 : 24 : case GFC_ISYM_ATOMIC_DEF:
12403 : 24 : case GFC_ISYM_ATOMIC_OR:
12404 : 24 : case GFC_ISYM_ATOMIC_XOR:
12405 : 24 : tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12406 : : fold_convert (itype, value),
12407 : 24 : build_int_cst (NULL, MEMMODEL_RELAXED));
12408 : 24 : gfc_add_expr_to_block (&block, tmp);
12409 : 24 : break;
12410 : 15 : default:
12411 : 15 : tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12412 : : fold_convert (itype, value),
12413 : 15 : build_int_cst (NULL, MEMMODEL_RELAXED));
12414 : 15 : gfc_add_modify (&block, old, fold_convert (TREE_TYPE (old), tmp));
12415 : 15 : break;
12416 : : }
12417 : :
12418 : 39 : if (stat != NULL_TREE)
12419 : 34 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12420 : 39 : gfc_add_block_to_block (&block, &post_block);
12421 : 39 : return gfc_finish_block (&block);
12422 : : }
12423 : :
12424 : :
12425 : : static tree
12426 : 119 : conv_intrinsic_atomic_ref (gfc_code *code)
12427 : : {
12428 : 119 : gfc_se argse;
12429 : 119 : tree tmp, atom, value, stat = NULL_TREE;
12430 : 119 : stmtblock_t block, post_block;
12431 : 119 : built_in_function fn;
12432 : 119 : gfc_expr *atom_expr = code->ext.actual->next->expr;
12433 : :
12434 : 119 : if (atom_expr->expr_type == EXPR_FUNCTION
12435 : 0 : && atom_expr->value.function.isym
12436 : 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12437 : 0 : atom_expr = atom_expr->value.function.actual->expr;
12438 : :
12439 : 119 : gfc_start_block (&block);
12440 : 119 : gfc_init_block (&post_block);
12441 : 119 : gfc_init_se (&argse, NULL);
12442 : 119 : argse.want_pointer = 1;
12443 : 119 : gfc_conv_expr (&argse, atom_expr);
12444 : 119 : gfc_add_block_to_block (&block, &argse.pre);
12445 : 119 : gfc_add_block_to_block (&post_block, &argse.post);
12446 : 119 : atom = argse.expr;
12447 : :
12448 : 119 : gfc_init_se (&argse, NULL);
12449 : 119 : if (flag_coarray == GFC_FCOARRAY_LIB
12450 : 58 : && code->ext.actual->expr->ts.kind == atom_expr->ts.kind)
12451 : 55 : argse.want_pointer = 1;
12452 : 119 : gfc_conv_expr (&argse, code->ext.actual->expr);
12453 : 119 : gfc_add_block_to_block (&block, &argse.pre);
12454 : 119 : gfc_add_block_to_block (&post_block, &argse.post);
12455 : 119 : value = argse.expr;
12456 : :
12457 : : /* STAT= */
12458 : 119 : if (code->ext.actual->next->next->expr != NULL)
12459 : : {
12460 : 110 : gcc_assert (code->ext.actual->next->next->expr->expr_type
12461 : : == EXPR_VARIABLE);
12462 : 110 : gfc_init_se (&argse, NULL);
12463 : 110 : if (flag_coarray == GFC_FCOARRAY_LIB)
12464 : 54 : argse.want_pointer = 1;
12465 : 110 : gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
12466 : 110 : gfc_add_block_to_block (&block, &argse.pre);
12467 : 110 : gfc_add_block_to_block (&post_block, &argse.post);
12468 : 110 : stat = argse.expr;
12469 : : }
12470 : 9 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12471 : 4 : stat = null_pointer_node;
12472 : :
12473 : 119 : if (flag_coarray == GFC_FCOARRAY_LIB)
12474 : : {
12475 : 58 : tree image_index, caf_decl, offset, token;
12476 : 58 : tree orig_value = NULL_TREE, vardecl = NULL_TREE;
12477 : :
12478 : 58 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12479 : 58 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12480 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12481 : :
12482 : 58 : if (gfc_is_coindexed (atom_expr))
12483 : 52 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12484 : : else
12485 : 6 : image_index = integer_zero_node;
12486 : :
12487 : 58 : gfc_init_se (&argse, NULL);
12488 : 58 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12489 : : atom_expr);
12490 : 58 : gfc_add_block_to_block (&block, &argse.pre);
12491 : :
12492 : : /* Different type, need type conversion. */
12493 : 58 : if (!POINTER_TYPE_P (TREE_TYPE (value)))
12494 : : {
12495 : 3 : vardecl = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
12496 : 3 : orig_value = value;
12497 : 3 : value = gfc_build_addr_expr (NULL_TREE, vardecl);
12498 : : }
12499 : :
12500 : 58 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_ref, 7,
12501 : : token, offset, image_index, value, stat,
12502 : 58 : build_int_cst (integer_type_node,
12503 : 58 : (int) atom_expr->ts.type),
12504 : 58 : build_int_cst (integer_type_node,
12505 : 58 : (int) atom_expr->ts.kind));
12506 : 58 : gfc_add_expr_to_block (&block, tmp);
12507 : 58 : if (vardecl != NULL_TREE)
12508 : 3 : gfc_add_modify (&block, orig_value,
12509 : 3 : fold_convert (TREE_TYPE (orig_value), vardecl));
12510 : 58 : gfc_add_block_to_block (&block, &argse.post);
12511 : 58 : gfc_add_block_to_block (&block, &post_block);
12512 : 58 : return gfc_finish_block (&block);
12513 : : }
12514 : :
12515 : 61 : tmp = TREE_TYPE (TREE_TYPE (atom));
12516 : 122 : fn = (built_in_function) ((int) BUILT_IN_ATOMIC_LOAD_N
12517 : 61 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12518 : 61 : + 1);
12519 : 61 : tmp = builtin_decl_explicit (fn);
12520 : 61 : tmp = build_call_expr_loc (input_location, tmp, 2, atom,
12521 : 61 : build_int_cst (integer_type_node,
12522 : 61 : MEMMODEL_RELAXED));
12523 : 61 : gfc_add_modify (&block, value, fold_convert (TREE_TYPE (value), tmp));
12524 : :
12525 : 61 : if (stat != NULL_TREE)
12526 : 56 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12527 : 61 : gfc_add_block_to_block (&block, &post_block);
12528 : 61 : return gfc_finish_block (&block);
12529 : : }
12530 : :
12531 : :
12532 : : static tree
12533 : 10 : conv_intrinsic_atomic_cas (gfc_code *code)
12534 : : {
12535 : 10 : gfc_se argse;
12536 : 10 : tree tmp, atom, old, new_val, comp, stat = NULL_TREE;
12537 : 10 : stmtblock_t block, post_block;
12538 : 10 : built_in_function fn;
12539 : 10 : gfc_expr *atom_expr = code->ext.actual->expr;
12540 : :
12541 : 10 : if (atom_expr->expr_type == EXPR_FUNCTION
12542 : 0 : && atom_expr->value.function.isym
12543 : 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12544 : 0 : atom_expr = atom_expr->value.function.actual->expr;
12545 : :
12546 : 10 : gfc_init_block (&block);
12547 : 10 : gfc_init_block (&post_block);
12548 : 10 : gfc_init_se (&argse, NULL);
12549 : 10 : argse.want_pointer = 1;
12550 : 10 : gfc_conv_expr (&argse, atom_expr);
12551 : 10 : atom = argse.expr;
12552 : :
12553 : 10 : gfc_init_se (&argse, NULL);
12554 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12555 : 4 : argse.want_pointer = 1;
12556 : 10 : gfc_conv_expr (&argse, code->ext.actual->next->expr);
12557 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12558 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12559 : 10 : old = argse.expr;
12560 : :
12561 : 10 : gfc_init_se (&argse, NULL);
12562 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12563 : 4 : argse.want_pointer = 1;
12564 : 10 : gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
12565 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12566 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12567 : 10 : comp = argse.expr;
12568 : :
12569 : 10 : gfc_init_se (&argse, NULL);
12570 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB
12571 : 4 : && code->ext.actual->next->next->next->expr->ts.kind
12572 : 4 : == atom_expr->ts.kind)
12573 : 4 : argse.want_pointer = 1;
12574 : 10 : gfc_conv_expr (&argse, code->ext.actual->next->next->next->expr);
12575 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12576 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12577 : 10 : new_val = argse.expr;
12578 : :
12579 : : /* STAT= */
12580 : 10 : if (code->ext.actual->next->next->next->next->expr != NULL)
12581 : : {
12582 : 10 : gcc_assert (code->ext.actual->next->next->next->next->expr->expr_type
12583 : : == EXPR_VARIABLE);
12584 : 10 : gfc_init_se (&argse, NULL);
12585 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12586 : 4 : argse.want_pointer = 1;
12587 : 10 : gfc_conv_expr_val (&argse,
12588 : 10 : code->ext.actual->next->next->next->next->expr);
12589 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12590 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12591 : 10 : stat = argse.expr;
12592 : : }
12593 : 0 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12594 : 0 : stat = null_pointer_node;
12595 : :
12596 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12597 : : {
12598 : 4 : tree image_index, caf_decl, offset, token;
12599 : :
12600 : 4 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12601 : 4 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12602 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12603 : :
12604 : 4 : if (gfc_is_coindexed (atom_expr))
12605 : 4 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12606 : : else
12607 : 0 : image_index = integer_zero_node;
12608 : :
12609 : 4 : if (TREE_TYPE (TREE_TYPE (new_val)) != TREE_TYPE (TREE_TYPE (old)))
12610 : : {
12611 : 4 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "new");
12612 : 4 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), new_val));
12613 : 4 : new_val = gfc_build_addr_expr (NULL_TREE, tmp);
12614 : : }
12615 : :
12616 : : /* Convert a constant to a pointer. */
12617 : 4 : if (!POINTER_TYPE_P (TREE_TYPE (comp)))
12618 : : {
12619 : 4 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "comp");
12620 : 4 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), comp));
12621 : 4 : comp = gfc_build_addr_expr (NULL_TREE, tmp);
12622 : : }
12623 : :
12624 : 4 : gfc_init_se (&argse, NULL);
12625 : 4 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12626 : : atom_expr);
12627 : 4 : gfc_add_block_to_block (&block, &argse.pre);
12628 : :
12629 : 4 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_cas, 9,
12630 : : token, offset, image_index, old, comp, new_val,
12631 : 4 : stat, build_int_cst (integer_type_node,
12632 : 4 : (int) atom_expr->ts.type),
12633 : 4 : build_int_cst (integer_type_node,
12634 : 4 : (int) atom_expr->ts.kind));
12635 : 4 : gfc_add_expr_to_block (&block, tmp);
12636 : 4 : gfc_add_block_to_block (&block, &argse.post);
12637 : 4 : gfc_add_block_to_block (&block, &post_block);
12638 : 4 : return gfc_finish_block (&block);
12639 : : }
12640 : :
12641 : 6 : tmp = TREE_TYPE (TREE_TYPE (atom));
12642 : 12 : fn = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
12643 : 6 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12644 : 6 : + 1);
12645 : 6 : tmp = builtin_decl_explicit (fn);
12646 : :
12647 : 6 : gfc_add_modify (&block, old, comp);
12648 : 12 : tmp = build_call_expr_loc (input_location, tmp, 6, atom,
12649 : : gfc_build_addr_expr (NULL, old),
12650 : 6 : fold_convert (TREE_TYPE (old), new_val),
12651 : : boolean_false_node,
12652 : 6 : build_int_cst (NULL, MEMMODEL_RELAXED),
12653 : 6 : build_int_cst (NULL, MEMMODEL_RELAXED));
12654 : 6 : gfc_add_expr_to_block (&block, tmp);
12655 : :
12656 : 6 : if (stat != NULL_TREE)
12657 : 6 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12658 : 6 : gfc_add_block_to_block (&block, &post_block);
12659 : 6 : return gfc_finish_block (&block);
12660 : : }
12661 : :
12662 : : static tree
12663 : 70 : conv_intrinsic_event_query (gfc_code *code)
12664 : : {
12665 : 70 : gfc_se se, argse;
12666 : 70 : tree stat = NULL_TREE, stat2 = NULL_TREE;
12667 : 70 : tree count = NULL_TREE, count2 = NULL_TREE;
12668 : :
12669 : 70 : gfc_expr *event_expr = code->ext.actual->expr;
12670 : :
12671 : 70 : if (code->ext.actual->next->next->expr)
12672 : : {
12673 : 12 : gcc_assert (code->ext.actual->next->next->expr->expr_type
12674 : : == EXPR_VARIABLE);
12675 : 12 : gfc_init_se (&argse, NULL);
12676 : 12 : gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
12677 : 12 : stat = argse.expr;
12678 : : }
12679 : 58 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12680 : 29 : stat = null_pointer_node;
12681 : :
12682 : 70 : if (code->ext.actual->next->expr)
12683 : : {
12684 : 70 : gcc_assert (code->ext.actual->next->expr->expr_type == EXPR_VARIABLE);
12685 : 70 : gfc_init_se (&argse, NULL);
12686 : 70 : gfc_conv_expr_val (&argse, code->ext.actual->next->expr);
12687 : 70 : count = argse.expr;
12688 : : }
12689 : :
12690 : 70 : gfc_start_block (&se.pre);
12691 : 70 : if (flag_coarray == GFC_FCOARRAY_LIB)
12692 : : {
12693 : 35 : tree tmp, token, image_index;
12694 : 35 : tree index = build_zero_cst (gfc_array_index_type);
12695 : :
12696 : 35 : if (event_expr->expr_type == EXPR_FUNCTION
12697 : 0 : && event_expr->value.function.isym
12698 : 0 : && event_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12699 : 0 : event_expr = event_expr->value.function.actual->expr;
12700 : :
12701 : 35 : tree caf_decl = gfc_get_tree_for_caf_expr (event_expr);
12702 : :
12703 : 35 : if (event_expr->symtree->n.sym->ts.type != BT_DERIVED
12704 : 35 : || event_expr->symtree->n.sym->ts.u.derived->from_intmod
12705 : : != INTMOD_ISO_FORTRAN_ENV
12706 : 35 : || event_expr->symtree->n.sym->ts.u.derived->intmod_sym_id
12707 : : != ISOFORTRAN_EVENT_TYPE)
12708 : : {
12709 : 0 : gfc_error ("Sorry, the event component of derived type at %L is not "
12710 : : "yet supported", &event_expr->where);
12711 : 0 : return NULL_TREE;
12712 : : }
12713 : :
12714 : 35 : if (gfc_is_coindexed (event_expr))
12715 : : {
12716 : 0 : gfc_error ("The event variable at %L shall not be coindexed",
12717 : : &event_expr->where);
12718 : 0 : return NULL_TREE;
12719 : : }
12720 : :
12721 : 35 : image_index = integer_zero_node;
12722 : :
12723 : 35 : gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
12724 : : event_expr);
12725 : :
12726 : : /* For arrays, obtain the array index. */
12727 : 35 : if (gfc_expr_attr (event_expr).dimension)
12728 : : {
12729 : 26 : tree desc, tmp, extent, lbound, ubound;
12730 : 26 : gfc_array_ref *ar, ar2;
12731 : 26 : int i;
12732 : :
12733 : : /* TODO: Extend this, once DT components are supported. */
12734 : 26 : ar = &event_expr->ref->u.ar;
12735 : 26 : ar2 = *ar;
12736 : 26 : memset (ar, '\0', sizeof (*ar));
12737 : 26 : ar->as = ar2.as;
12738 : 26 : ar->type = AR_FULL;
12739 : :
12740 : 26 : gfc_init_se (&argse, NULL);
12741 : 26 : argse.descriptor_only = 1;
12742 : 26 : gfc_conv_expr_descriptor (&argse, event_expr);
12743 : 26 : gfc_add_block_to_block (&se.pre, &argse.pre);
12744 : 26 : desc = argse.expr;
12745 : 26 : *ar = ar2;
12746 : :
12747 : 26 : extent = build_one_cst (gfc_array_index_type);
12748 : 78 : for (i = 0; i < ar->dimen; i++)
12749 : : {
12750 : 26 : gfc_init_se (&argse, NULL);
12751 : 26 : gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
12752 : 26 : gfc_add_block_to_block (&argse.pre, &argse.pre);
12753 : 26 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
12754 : 26 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
12755 : 26 : TREE_TYPE (lbound), argse.expr, lbound);
12756 : 26 : tmp = fold_build2_loc (input_location, MULT_EXPR,
12757 : 26 : TREE_TYPE (tmp), extent, tmp);
12758 : 26 : index = fold_build2_loc (input_location, PLUS_EXPR,
12759 : 26 : TREE_TYPE (tmp), index, tmp);
12760 : 26 : if (i < ar->dimen - 1)
12761 : : {
12762 : 0 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
12763 : 0 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
12764 : 0 : extent = fold_build2_loc (input_location, MULT_EXPR,
12765 : 0 : TREE_TYPE (tmp), extent, tmp);
12766 : : }
12767 : : }
12768 : : }
12769 : :
12770 : 35 : if (count != null_pointer_node && TREE_TYPE (count) != integer_type_node)
12771 : : {
12772 : 0 : count2 = count;
12773 : 0 : count = gfc_create_var (integer_type_node, "count");
12774 : : }
12775 : :
12776 : 35 : if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
12777 : : {
12778 : 0 : stat2 = stat;
12779 : 0 : stat = gfc_create_var (integer_type_node, "stat");
12780 : : }
12781 : :
12782 : 35 : index = fold_convert (size_type_node, index);
12783 : 70 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_query, 5,
12784 : : token, index, image_index, count
12785 : 35 : ? gfc_build_addr_expr (NULL, count) : count,
12786 : 35 : stat != null_pointer_node
12787 : 6 : ? gfc_build_addr_expr (NULL, stat) : stat);
12788 : 35 : gfc_add_expr_to_block (&se.pre, tmp);
12789 : :
12790 : 35 : if (count2 != NULL_TREE)
12791 : 0 : gfc_add_modify (&se.pre, count2,
12792 : 0 : fold_convert (TREE_TYPE (count2), count));
12793 : :
12794 : 35 : if (stat2 != NULL_TREE)
12795 : 0 : gfc_add_modify (&se.pre, stat2,
12796 : 0 : fold_convert (TREE_TYPE (stat2), stat));
12797 : :
12798 : 35 : return gfc_finish_block (&se.pre);
12799 : : }
12800 : :
12801 : 35 : gfc_init_se (&argse, NULL);
12802 : 35 : gfc_conv_expr_val (&argse, code->ext.actual->expr);
12803 : 35 : gfc_add_modify (&se.pre, count, fold_convert (TREE_TYPE (count), argse.expr));
12804 : :
12805 : 35 : if (stat != NULL_TREE)
12806 : 6 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
12807 : :
12808 : 35 : return gfc_finish_block (&se.pre);
12809 : : }
12810 : :
12811 : :
12812 : : /* This is a peculiar case because of the need to do dependency checking.
12813 : : It is called via trans-stmt.cc(gfc_trans_call), where it is picked out as
12814 : : a special case and this function called instead of
12815 : : gfc_conv_procedure_call. */
12816 : : void
12817 : 197 : gfc_conv_intrinsic_mvbits (gfc_se *se, gfc_actual_arglist *actual_args,
12818 : : gfc_loopinfo *loop)
12819 : : {
12820 : 197 : gfc_actual_arglist *actual;
12821 : 197 : gfc_se argse[5];
12822 : 197 : gfc_expr *arg[5];
12823 : 197 : gfc_ss *lss;
12824 : 197 : int n;
12825 : :
12826 : 197 : tree from, frompos, len, to, topos;
12827 : 197 : tree lenmask, oldbits, newbits, bitsize;
12828 : 197 : tree type, utype, above, mask1, mask2;
12829 : :
12830 : 197 : if (loop)
12831 : 67 : lss = loop->ss;
12832 : : else
12833 : 130 : lss = gfc_ss_terminator;
12834 : :
12835 : : actual = actual_args;
12836 : 1182 : for (n = 0; n < 5; n++, actual = actual->next)
12837 : : {
12838 : 985 : arg[n] = actual->expr;
12839 : 985 : gfc_init_se (&argse[n], NULL);
12840 : :
12841 : 985 : if (lss != gfc_ss_terminator)
12842 : : {
12843 : 335 : gfc_copy_loopinfo_to_se (&argse[n], loop);
12844 : : /* Find the ss for the expression if it is there. */
12845 : 335 : argse[n].ss = lss;
12846 : 335 : gfc_mark_ss_chain_used (lss, 1);
12847 : : }
12848 : :
12849 : 985 : gfc_conv_expr (&argse[n], arg[n]);
12850 : :
12851 : 985 : if (loop)
12852 : 335 : lss = argse[n].ss;
12853 : : }
12854 : :
12855 : 197 : from = argse[0].expr;
12856 : 197 : frompos = argse[1].expr;
12857 : 197 : len = argse[2].expr;
12858 : 197 : to = argse[3].expr;
12859 : 197 : topos = argse[4].expr;
12860 : :
12861 : : /* The type of the result (TO). */
12862 : 197 : type = TREE_TYPE (to);
12863 : 197 : bitsize = build_int_cst (integer_type_node, TYPE_PRECISION (type));
12864 : :
12865 : : /* Optionally generate code for runtime argument check. */
12866 : 197 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
12867 : : {
12868 : 18 : tree nbits, below, ccond;
12869 : 18 : tree fp = fold_convert (long_integer_type_node, frompos);
12870 : 18 : tree ln = fold_convert (long_integer_type_node, len);
12871 : 18 : tree tp = fold_convert (long_integer_type_node, topos);
12872 : 18 : below = fold_build2_loc (input_location, LT_EXPR,
12873 : : logical_type_node, frompos,
12874 : 18 : build_int_cst (TREE_TYPE (frompos), 0));
12875 : 18 : above = fold_build2_loc (input_location, GT_EXPR,
12876 : : logical_type_node, frompos,
12877 : 18 : fold_convert (TREE_TYPE (frompos), bitsize));
12878 : 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
12879 : : logical_type_node, below, above);
12880 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
12881 : 18 : &arg[1]->where,
12882 : : "FROMPOS argument (%ld) out of range 0:%d "
12883 : : "in intrinsic MVBITS", fp, bitsize);
12884 : 18 : below = fold_build2_loc (input_location, LT_EXPR,
12885 : : logical_type_node, len,
12886 : 18 : build_int_cst (TREE_TYPE (len), 0));
12887 : 18 : above = fold_build2_loc (input_location, GT_EXPR,
12888 : : logical_type_node, len,
12889 : 18 : fold_convert (TREE_TYPE (len), bitsize));
12890 : 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
12891 : : logical_type_node, below, above);
12892 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[2].pre,
12893 : 18 : &arg[2]->where,
12894 : : "LEN argument (%ld) out of range 0:%d "
12895 : : "in intrinsic MVBITS", ln, bitsize);
12896 : 18 : below = fold_build2_loc (input_location, LT_EXPR,
12897 : : logical_type_node, topos,
12898 : 18 : build_int_cst (TREE_TYPE (topos), 0));
12899 : 18 : above = fold_build2_loc (input_location, GT_EXPR,
12900 : : logical_type_node, topos,
12901 : 18 : fold_convert (TREE_TYPE (topos), bitsize));
12902 : 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
12903 : : logical_type_node, below, above);
12904 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
12905 : 18 : &arg[4]->where,
12906 : : "TOPOS argument (%ld) out of range 0:%d "
12907 : : "in intrinsic MVBITS", tp, bitsize);
12908 : :
12909 : : /* The tests above ensure that FROMPOS, LEN and TOPOS fit into short
12910 : : integers. Additions below cannot overflow. */
12911 : 18 : nbits = fold_convert (long_integer_type_node, bitsize);
12912 : 18 : above = fold_build2_loc (input_location, PLUS_EXPR,
12913 : : long_integer_type_node, fp, ln);
12914 : 18 : ccond = fold_build2_loc (input_location, GT_EXPR,
12915 : : logical_type_node, above, nbits);
12916 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
12917 : : &arg[1]->where,
12918 : : "FROMPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
12919 : : "in intrinsic MVBITS", fp, ln, bitsize);
12920 : 18 : above = fold_build2_loc (input_location, PLUS_EXPR,
12921 : : long_integer_type_node, tp, ln);
12922 : 18 : ccond = fold_build2_loc (input_location, GT_EXPR,
12923 : : logical_type_node, above, nbits);
12924 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
12925 : : &arg[4]->where,
12926 : : "TOPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
12927 : : "in intrinsic MVBITS", tp, ln, bitsize);
12928 : : }
12929 : :
12930 : 1182 : for (n = 0; n < 5; n++)
12931 : : {
12932 : 985 : gfc_add_block_to_block (&se->pre, &argse[n].pre);
12933 : 985 : gfc_add_block_to_block (&se->post, &argse[n].post);
12934 : : }
12935 : :
12936 : : /* lenmask = (LEN >= bit_size (TYPE)) ? ~(TYPE)0 : ((TYPE)1 << LEN) - 1 */
12937 : 197 : above = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
12938 : 197 : len, fold_convert (TREE_TYPE (len), bitsize));
12939 : 197 : mask1 = build_int_cst (type, -1);
12940 : 197 : mask2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
12941 : 197 : build_int_cst (type, 1), len);
12942 : 197 : mask2 = fold_build2_loc (input_location, MINUS_EXPR, type,
12943 : 197 : mask2, build_int_cst (type, 1));
12944 : 197 : lenmask = fold_build3_loc (input_location, COND_EXPR, type,
12945 : : above, mask1, mask2);
12946 : :
12947 : : /* newbits = (((UTYPE)(FROM) >> FROMPOS) & lenmask) << TOPOS.
12948 : : * For valid frompos+len <= bit_size(FROM) the conversion to unsigned is
12949 : : * not strictly necessary; artificial bits from rshift will be masked. */
12950 : 197 : utype = unsigned_type_for (type);
12951 : 197 : newbits = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
12952 : : fold_convert (utype, from), frompos);
12953 : 197 : newbits = fold_build2_loc (input_location, BIT_AND_EXPR, type,
12954 : : fold_convert (type, newbits), lenmask);
12955 : 197 : newbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
12956 : : newbits, topos);
12957 : :
12958 : : /* oldbits = TO & (~(lenmask << TOPOS)). */
12959 : 197 : oldbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
12960 : : lenmask, topos);
12961 : 197 : oldbits = fold_build1_loc (input_location, BIT_NOT_EXPR, type, oldbits);
12962 : 197 : oldbits = fold_build2_loc (input_location, BIT_AND_EXPR, type, oldbits, to);
12963 : :
12964 : : /* TO = newbits | oldbits. */
12965 : 197 : se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
12966 : : oldbits, newbits);
12967 : :
12968 : : /* Return the assignment. */
12969 : 197 : se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
12970 : : void_type_node, to, se->expr);
12971 : 197 : }
12972 : :
12973 : :
12974 : : static tree
12975 : 236 : conv_intrinsic_move_alloc (gfc_code *code)
12976 : : {
12977 : 236 : stmtblock_t block;
12978 : 236 : gfc_expr *from_expr, *to_expr;
12979 : 236 : gfc_se from_se, to_se;
12980 : 236 : tree tmp, to_tree, from_tree;
12981 : 236 : bool coarray, from_is_class, from_is_scalar;
12982 : :
12983 : 236 : gfc_start_block (&block);
12984 : :
12985 : 236 : from_expr = code->ext.actual->expr;
12986 : 236 : to_expr = code->ext.actual->next->expr;
12987 : :
12988 : 236 : gfc_init_se (&from_se, NULL);
12989 : 236 : gfc_init_se (&to_se, NULL);
12990 : :
12991 : 236 : gcc_assert (from_expr->ts.type != BT_CLASS || to_expr->ts.type == BT_CLASS);
12992 : 236 : coarray = from_expr->corank != 0;
12993 : :
12994 : 236 : from_is_class = from_expr->ts.type == BT_CLASS;
12995 : 236 : from_is_scalar = from_expr->rank == 0 && !coarray;
12996 : 236 : if (to_expr->ts.type == BT_CLASS || from_is_scalar)
12997 : : {
12998 : 163 : from_se.want_pointer = 1;
12999 : 163 : if (from_is_scalar)
13000 : 115 : gfc_conv_expr (&from_se, from_expr);
13001 : : else
13002 : 48 : gfc_conv_expr_descriptor (&from_se, from_expr);
13003 : 163 : if (from_is_class)
13004 : 64 : from_tree = gfc_class_data_get (from_se.expr);
13005 : : else
13006 : : {
13007 : 99 : gfc_symbol *vtab;
13008 : 99 : from_tree = from_se.expr;
13009 : :
13010 : 99 : if (to_expr->ts.type == BT_CLASS)
13011 : : {
13012 : 36 : vtab = gfc_find_vtab (&from_expr->ts);
13013 : 36 : gcc_assert (vtab);
13014 : 36 : from_se.expr = gfc_get_symbol_decl (vtab);
13015 : : }
13016 : : }
13017 : 163 : gfc_add_block_to_block (&block, &from_se.pre);
13018 : :
13019 : 163 : to_se.want_pointer = 1;
13020 : 163 : if (to_expr->rank == 0)
13021 : 115 : gfc_conv_expr (&to_se, to_expr);
13022 : : else
13023 : 48 : gfc_conv_expr_descriptor (&to_se, to_expr);
13024 : 163 : if (to_expr->ts.type == BT_CLASS)
13025 : 100 : to_tree = gfc_class_data_get (to_se.expr);
13026 : : else
13027 : 63 : to_tree = to_se.expr;
13028 : 163 : gfc_add_block_to_block (&block, &to_se.pre);
13029 : :
13030 : : /* Deallocate "to". */
13031 : 163 : if (to_expr->rank == 0)
13032 : : {
13033 : 115 : tmp
13034 : 115 : = gfc_deallocate_scalar_with_status (to_tree, NULL_TREE, NULL_TREE,
13035 : : true, to_expr, to_expr->ts);
13036 : 115 : gfc_add_expr_to_block (&block, tmp);
13037 : : }
13038 : :
13039 : 163 : if (from_is_scalar)
13040 : : {
13041 : : /* Assign (_data) pointers. */
13042 : 115 : gfc_add_modify_loc (input_location, &block, to_tree,
13043 : 115 : fold_convert (TREE_TYPE (to_tree), from_tree));
13044 : :
13045 : : /* Set "from" to NULL. */
13046 : 115 : gfc_add_modify_loc (input_location, &block, from_tree,
13047 : 115 : fold_convert (TREE_TYPE (from_tree),
13048 : : null_pointer_node));
13049 : :
13050 : 115 : gfc_add_block_to_block (&block, &from_se.post);
13051 : : }
13052 : 163 : gfc_add_block_to_block (&block, &to_se.post);
13053 : :
13054 : : /* Set _vptr. */
13055 : 163 : if (to_expr->ts.type == BT_CLASS)
13056 : : {
13057 : 100 : gfc_class_set_vptr (&block, to_se.expr, from_se.expr);
13058 : 100 : if (from_is_class)
13059 : 64 : gfc_reset_vptr (&block, from_expr);
13060 : 100 : if (UNLIMITED_POLY (to_expr))
13061 : : {
13062 : 20 : tree to_len = gfc_class_len_get (to_se.class_container);
13063 : 6 : tmp = from_expr->ts.type == BT_CHARACTER && from_se.string_length
13064 : 20 : ? from_se.string_length
13065 : : : size_zero_node;
13066 : 20 : gfc_add_modify_loc (input_location, &block, to_len,
13067 : 20 : fold_convert (TREE_TYPE (to_len), tmp));
13068 : : }
13069 : : }
13070 : :
13071 : 163 : if (from_is_scalar)
13072 : : {
13073 : 115 : if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13074 : : {
13075 : 6 : gfc_add_modify_loc (input_location, &block, to_se.string_length,
13076 : 6 : fold_convert (TREE_TYPE (to_se.string_length),
13077 : : from_se.string_length));
13078 : 6 : if (from_expr->ts.deferred)
13079 : 6 : gfc_add_modify_loc (
13080 : : input_location, &block, from_se.string_length,
13081 : 6 : build_int_cst (TREE_TYPE (from_se.string_length), 0));
13082 : : }
13083 : 115 : if (UNLIMITED_POLY (from_expr))
13084 : 2 : gfc_reset_len (&block, from_expr);
13085 : :
13086 : 115 : return gfc_finish_block (&block);
13087 : : }
13088 : :
13089 : 48 : gfc_init_se (&to_se, NULL);
13090 : 48 : gfc_init_se (&from_se, NULL);
13091 : : }
13092 : :
13093 : : /* Deallocate "to". */
13094 : 121 : if (from_expr->rank == 0)
13095 : : {
13096 : 3 : to_se.want_coarray = 1;
13097 : 3 : from_se.want_coarray = 1;
13098 : : }
13099 : 121 : gfc_conv_expr_descriptor (&to_se, to_expr);
13100 : 121 : gfc_conv_expr_descriptor (&from_se, from_expr);
13101 : :
13102 : : /* For coarrays, call SYNC ALL if TO is already deallocated as MOVE_ALLOC
13103 : : is an image control "statement", cf. IR F08/0040 in 12-006A. */
13104 : 121 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
13105 : : {
13106 : 4 : tree cond;
13107 : :
13108 : 4 : tmp = gfc_deallocate_with_status (to_se.expr, NULL_TREE, NULL_TREE,
13109 : : NULL_TREE, NULL_TREE, true, to_expr,
13110 : : GFC_CAF_COARRAY_DEALLOCATE_ONLY);
13111 : 4 : gfc_add_expr_to_block (&block, tmp);
13112 : :
13113 : 4 : tmp = gfc_conv_descriptor_data_get (to_se.expr);
13114 : 4 : cond = fold_build2_loc (input_location, EQ_EXPR,
13115 : : logical_type_node, tmp,
13116 : 4 : fold_convert (TREE_TYPE (tmp),
13117 : : null_pointer_node));
13118 : 4 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
13119 : : 3, null_pointer_node, null_pointer_node,
13120 : : integer_zero_node);
13121 : :
13122 : 4 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
13123 : : tmp, build_empty_stmt (input_location));
13124 : 4 : gfc_add_expr_to_block (&block, tmp);
13125 : 4 : }
13126 : : else
13127 : : {
13128 : 117 : if (to_expr->ts.type == BT_DERIVED
13129 : 25 : && to_expr->ts.u.derived->attr.alloc_comp)
13130 : : {
13131 : 19 : tmp = gfc_deallocate_alloc_comp (to_expr->ts.u.derived,
13132 : : to_se.expr, to_expr->rank);
13133 : 19 : gfc_add_expr_to_block (&block, tmp);
13134 : : }
13135 : :
13136 : 117 : tmp = gfc_deallocate_with_status (to_se.expr, NULL_TREE, NULL_TREE,
13137 : : NULL_TREE, NULL_TREE, true, to_expr,
13138 : : GFC_CAF_COARRAY_NOCOARRAY);
13139 : 117 : gfc_add_expr_to_block (&block, tmp);
13140 : : }
13141 : :
13142 : : /* Copy the array descriptor data. */
13143 : 121 : gfc_add_modify_loc (input_location, &block, to_se.expr, from_se.expr);
13144 : :
13145 : : /* Set "from" to NULL. */
13146 : 121 : tmp = gfc_conv_descriptor_data_get (from_se.expr);
13147 : 121 : gfc_add_modify_loc (input_location, &block, tmp,
13148 : 121 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
13149 : :
13150 : :
13151 : 121 : if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13152 : : {
13153 : 7 : gfc_add_modify_loc (input_location, &block, to_se.string_length,
13154 : 7 : fold_convert (TREE_TYPE (to_se.string_length),
13155 : : from_se.string_length));
13156 : 7 : if (from_expr->ts.deferred)
13157 : 6 : gfc_add_modify_loc (input_location, &block, from_se.string_length,
13158 : 6 : build_int_cst (TREE_TYPE (from_se.string_length), 0));
13159 : : }
13160 : :
13161 : 121 : return gfc_finish_block (&block);
13162 : : }
13163 : :
13164 : :
13165 : : tree
13166 : 5833 : gfc_conv_intrinsic_subroutine (gfc_code *code)
13167 : : {
13168 : 5833 : tree res;
13169 : :
13170 : 5833 : gcc_assert (code->resolved_isym);
13171 : :
13172 : 5833 : switch (code->resolved_isym->id)
13173 : : {
13174 : 236 : case GFC_ISYM_MOVE_ALLOC:
13175 : 236 : res = conv_intrinsic_move_alloc (code);
13176 : 236 : break;
13177 : :
13178 : 10 : case GFC_ISYM_ATOMIC_CAS:
13179 : 10 : res = conv_intrinsic_atomic_cas (code);
13180 : 10 : break;
13181 : :
13182 : 68 : case GFC_ISYM_ATOMIC_ADD:
13183 : 68 : case GFC_ISYM_ATOMIC_AND:
13184 : 68 : case GFC_ISYM_ATOMIC_DEF:
13185 : 68 : case GFC_ISYM_ATOMIC_OR:
13186 : 68 : case GFC_ISYM_ATOMIC_XOR:
13187 : 68 : case GFC_ISYM_ATOMIC_FETCH_ADD:
13188 : 68 : case GFC_ISYM_ATOMIC_FETCH_AND:
13189 : 68 : case GFC_ISYM_ATOMIC_FETCH_OR:
13190 : 68 : case GFC_ISYM_ATOMIC_FETCH_XOR:
13191 : 68 : res = conv_intrinsic_atomic_op (code);
13192 : 68 : break;
13193 : :
13194 : 119 : case GFC_ISYM_ATOMIC_REF:
13195 : 119 : res = conv_intrinsic_atomic_ref (code);
13196 : 119 : break;
13197 : :
13198 : 70 : case GFC_ISYM_EVENT_QUERY:
13199 : 70 : res = conv_intrinsic_event_query (code);
13200 : 70 : break;
13201 : :
13202 : 2740 : case GFC_ISYM_C_F_POINTER:
13203 : 2740 : case GFC_ISYM_C_F_PROCPOINTER:
13204 : 2740 : res = conv_isocbinding_subroutine (code);
13205 : 2740 : break;
13206 : :
13207 : 288 : case GFC_ISYM_CAF_SEND:
13208 : 288 : res = conv_caf_send_to_remote (code);
13209 : 288 : break;
13210 : :
13211 : 90 : case GFC_ISYM_CAF_SENDGET:
13212 : 90 : res = conv_caf_sendget (code);
13213 : 90 : break;
13214 : :
13215 : 63 : case GFC_ISYM_CO_BROADCAST:
13216 : 63 : case GFC_ISYM_CO_MIN:
13217 : 63 : case GFC_ISYM_CO_MAX:
13218 : 63 : case GFC_ISYM_CO_REDUCE:
13219 : 63 : case GFC_ISYM_CO_SUM:
13220 : 63 : res = conv_co_collective (code);
13221 : 63 : break;
13222 : :
13223 : 10 : case GFC_ISYM_FREE:
13224 : 10 : res = conv_intrinsic_free (code);
13225 : 10 : break;
13226 : :
13227 : 90 : case GFC_ISYM_RANDOM_INIT:
13228 : 90 : res = conv_intrinsic_random_init (code);
13229 : 90 : break;
13230 : :
13231 : 15 : case GFC_ISYM_KILL:
13232 : 15 : res = conv_intrinsic_kill_sub (code);
13233 : 15 : break;
13234 : :
13235 : : case GFC_ISYM_MVBITS:
13236 : : res = NULL_TREE;
13237 : : break;
13238 : :
13239 : 194 : case GFC_ISYM_SYSTEM_CLOCK:
13240 : 194 : res = conv_intrinsic_system_clock (code);
13241 : 194 : break;
13242 : :
13243 : : default:
13244 : : res = NULL_TREE;
13245 : : break;
13246 : : }
13247 : :
13248 : 5833 : return res;
13249 : : }
13250 : :
13251 : : #include "gt-fortran-trans-intrinsic.h"
|