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 : 11425 : builtin_decl_for_precision (enum built_in_function base_built_in,
147 : : int precision)
148 : : {
149 : 11425 : enum built_in_function i = END_BUILTINS;
150 : :
151 : 11425 : gfc_intrinsic_map_t *m;
152 : 489497 : for (m = gfc_intrinsic_map; m->double_built_in != base_built_in ; m++)
153 : : ;
154 : :
155 : 11425 : if (precision == TYPE_PRECISION (float_type_node))
156 : 5801 : i = m->float_built_in;
157 : 5624 : 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 : 11301 : return (i == END_BUILTINS ? NULL_TREE : builtin_decl_explicit (i));
171 : : }
172 : :
173 : :
174 : : tree
175 : 10386 : gfc_builtin_decl_for_float_kind (enum built_in_function double_built_in,
176 : : int kind)
177 : : {
178 : 10386 : int i = gfc_validate_kind (BT_REAL, kind, false);
179 : :
180 : 10386 : if (gfc_real_kinds[i].c_float128)
181 : : {
182 : : /* For _Float128, the story is a bit different, because we return
183 : : a decl to a library function rather than a built-in. */
184 : : gfc_intrinsic_map_t *m;
185 : 36328 : for (m = gfc_intrinsic_map; m->double_built_in != double_built_in ; m++)
186 : : ;
187 : :
188 : 905 : return m->real16_decl;
189 : : }
190 : :
191 : 9481 : return builtin_decl_for_precision (double_built_in,
192 : 9481 : 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 : 78222 : gfc_conv_intrinsic_function_args (gfc_se *se, gfc_expr *expr,
203 : : tree *argarray, int nargs)
204 : : {
205 : 78222 : gfc_actual_arglist *actual;
206 : 78222 : gfc_expr *e;
207 : 78222 : gfc_intrinsic_arg *formal;
208 : 78222 : gfc_se argse;
209 : 78222 : int curr_arg;
210 : :
211 : 78222 : formal = expr->value.function.isym->formal;
212 : 78222 : actual = expr->value.function.actual;
213 : :
214 : 176429 : for (curr_arg = 0; curr_arg < nargs; curr_arg++,
215 : 61486 : actual = actual->next,
216 : 98207 : formal = formal ? formal->next : NULL)
217 : : {
218 : 98207 : gcc_assert (actual);
219 : 98207 : e = actual->expr;
220 : : /* Skip omitted optional arguments. */
221 : 98207 : if (!e)
222 : : {
223 : 31 : --curr_arg;
224 : 31 : continue;
225 : : }
226 : :
227 : : /* Evaluate the parameter. This will substitute scalarized
228 : : references automatically. */
229 : 98176 : gfc_init_se (&argse, se);
230 : :
231 : 98176 : if (e->ts.type == BT_CHARACTER)
232 : : {
233 : 9529 : gfc_conv_expr (&argse, e);
234 : 9529 : gfc_conv_string_parameter (&argse);
235 : 9529 : argarray[curr_arg++] = argse.string_length;
236 : 9529 : gcc_assert (curr_arg < nargs);
237 : : }
238 : : else
239 : 88647 : 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 : 98176 : if (e->expr_type == EXPR_VARIABLE
244 : 51254 : && 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 : 98176 : gfc_add_block_to_block (&se->pre, &argse.pre);
250 : 98176 : gfc_add_block_to_block (&se->post, &argse.post);
251 : 98176 : argarray[curr_arg] = argse.expr;
252 : : }
253 : 78222 : }
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 : 53994 : gfc_intrinsic_argument_list_length (gfc_expr *expr)
260 : : {
261 : 53994 : int n = 0;
262 : 53994 : gfc_actual_arglist *actual;
263 : :
264 : 123090 : for (actual = expr->value.function.actual; actual; actual = actual->next)
265 : : {
266 : 69096 : if (!actual->expr)
267 : 6319 : continue;
268 : :
269 : 62777 : if (actual->expr->ts.type == BT_CHARACTER)
270 : 4505 : n += 2;
271 : : else
272 : 58272 : n++;
273 : : }
274 : :
275 : 53994 : 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 : 38093 : gfc_conv_intrinsic_conversion (gfc_se * se, gfc_expr * expr)
284 : : {
285 : 38093 : tree type;
286 : 38093 : tree *args;
287 : 38093 : int nargs;
288 : :
289 : 38093 : nargs = gfc_intrinsic_argument_list_length (expr);
290 : 38093 : 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 : 38093 : type = gfc_typenode_for_spec (&expr->ts);
296 : 38093 : gcc_assert (expr->value.function.actual->expr);
297 : 38093 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
298 : :
299 : : /* Conversion between character kinds involves a call to a library
300 : : function. */
301 : 38093 : 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 : 37885 : if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
338 : 37885 : && 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 : 37885 : se->expr = convert (type, args[0]);
348 : : }
349 : :
350 : : /* This is needed because the gcc backend only implements
351 : : FIX_TRUNC_EXPR, which is the same as INT() in Fortran.
352 : : FLOOR(x) = INT(x) <= x ? INT(x) : INT(x) - 1
353 : : Similarly for CEILING. */
354 : :
355 : : static tree
356 : 132 : build_fixbound_expr (stmtblock_t * pblock, tree arg, tree type, int up)
357 : : {
358 : 132 : tree tmp;
359 : 132 : tree cond;
360 : 132 : tree argtype;
361 : 132 : tree intval;
362 : :
363 : 132 : argtype = TREE_TYPE (arg);
364 : 132 : arg = gfc_evaluate_now (arg, pblock);
365 : :
366 : 132 : intval = convert (type, arg);
367 : 132 : intval = gfc_evaluate_now (intval, pblock);
368 : :
369 : 132 : tmp = convert (argtype, intval);
370 : 248 : cond = fold_build2_loc (input_location, up ? GE_EXPR : LE_EXPR,
371 : : logical_type_node, tmp, arg);
372 : :
373 : 248 : tmp = fold_build2_loc (input_location, up ? PLUS_EXPR : MINUS_EXPR, type,
374 : : intval, build_int_cst (type, 1));
375 : 132 : tmp = fold_build3_loc (input_location, COND_EXPR, type, cond, intval, tmp);
376 : 132 : return tmp;
377 : : }
378 : :
379 : :
380 : : /* Round to nearest integer, away from zero. */
381 : :
382 : : static tree
383 : 516 : build_round_expr (tree arg, tree restype)
384 : : {
385 : 516 : tree argtype;
386 : 516 : tree fn;
387 : 516 : int argprec, resprec;
388 : :
389 : 516 : argtype = TREE_TYPE (arg);
390 : 516 : argprec = TYPE_PRECISION (argtype);
391 : 516 : resprec = TYPE_PRECISION (restype);
392 : :
393 : : /* Depending on the type of the result, choose the int intrinsic (iround,
394 : : available only as a builtin, therefore cannot use it for _Float128), long
395 : : int intrinsic (lround family) or long long intrinsic (llround). If we
396 : : don't have an appropriate function that converts directly to the integer
397 : : type (such as kind == 16), just use ROUND, and then convert the result to
398 : : an integer. We might also need to convert the result afterwards. */
399 : 516 : if (resprec <= INT_TYPE_SIZE
400 : 516 : && argprec <= TYPE_PRECISION (long_double_type_node))
401 : 458 : fn = builtin_decl_for_precision (BUILT_IN_IROUND, argprec);
402 : 62 : else if (resprec <= LONG_TYPE_SIZE)
403 : 46 : fn = builtin_decl_for_precision (BUILT_IN_LROUND, argprec);
404 : 12 : else if (resprec <= LONG_LONG_TYPE_SIZE)
405 : 0 : fn = builtin_decl_for_precision (BUILT_IN_LLROUND, argprec);
406 : 12 : else if (resprec >= argprec)
407 : 12 : fn = builtin_decl_for_precision (BUILT_IN_ROUND, argprec);
408 : : else
409 : 0 : gcc_unreachable ();
410 : :
411 : 516 : return convert (restype, build_call_expr_loc (input_location,
412 : 516 : fn, 1, arg));
413 : : }
414 : :
415 : :
416 : : /* Convert a real to an integer using a specific rounding mode.
417 : : Ideally we would just build the corresponding GENERIC node,
418 : : however the RTL expander only actually supports FIX_TRUNC_EXPR. */
419 : :
420 : : static tree
421 : 1483 : build_fix_expr (stmtblock_t * pblock, tree arg, tree type,
422 : : enum rounding_mode op)
423 : : {
424 : 1483 : 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 : 1189 : case RND_TRUNC:
436 : 1189 : 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 : 3004 : gfc_conv_intrinsic_int (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
530 : : {
531 : 3004 : tree type;
532 : 3004 : tree *args;
533 : 3004 : int nargs;
534 : :
535 : 3004 : nargs = gfc_intrinsic_argument_list_length (expr);
536 : 3004 : 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 : 3004 : type = gfc_typenode_for_spec (&expr->ts);
541 : 3004 : gcc_assert (expr->value.function.actual->expr);
542 : 3004 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
543 : :
544 : 3004 : if (TREE_CODE (TREE_TYPE (args[0])) == INTEGER_TYPE)
545 : : {
546 : : /* Conversion to a different integer kind. */
547 : 1521 : 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 : 1483 : if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
554 : 1483 : && 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 : 1483 : se->expr = build_fix_expr (&se->pre, args[0], type, op);
564 : : }
565 : 3004 : }
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 : 257 : gfc_conv_intrinsic_conjg (gfc_se * se, gfc_expr * expr)
585 : : {
586 : 257 : tree arg;
587 : :
588 : 257 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
589 : 257 : se->expr = fold_build1_loc (input_location, CONJ_EXPR, TREE_TYPE (arg), arg);
590 : 257 : }
591 : :
592 : :
593 : :
594 : : static tree
595 : 646863 : define_quad_builtin (const char *name, tree type, bool is_const)
596 : : {
597 : 646863 : tree fndecl;
598 : 646863 : fndecl = build_decl (input_location, FUNCTION_DECL, get_identifier (name),
599 : : type);
600 : :
601 : : /* Mark the decl as external. */
602 : 646863 : DECL_EXTERNAL (fndecl) = 1;
603 : 646863 : TREE_PUBLIC (fndecl) = 1;
604 : :
605 : : /* Mark it __attribute__((const)). */
606 : 646863 : TREE_READONLY (fndecl) = is_const;
607 : :
608 : 646863 : rest_of_decl_compilation (fndecl, 1, 0);
609 : :
610 : 646863 : 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 : 44085390 : add_simd_flag_for_built_in (tree fndecl)
618 : : {
619 : 44085390 : if (gfc_vectorized_builtins == NULL
620 : 17755460 : || fndecl == NULL_TREE)
621 : 36411420 : return;
622 : :
623 : 7673970 : const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
624 : 7673970 : int *clauses = gfc_vectorized_builtins->get (name);
625 : 7673970 : if (clauses)
626 : : {
627 : 4814428 : for (unsigned i = 0; i < 3; i++)
628 : 3610821 : if (*clauses & (1 << i))
629 : : {
630 : 1203612 : gfc_simd_clause simd_type = (gfc_simd_clause)*clauses;
631 : 1203612 : tree omp_clause = NULL_TREE;
632 : 1203612 : if (simd_type == SIMD_NONE)
633 : : ; /* No SIMD clause. */
634 : : else
635 : : {
636 : 1203612 : omp_clause_code code
637 : : = (simd_type == SIMD_INBRANCH
638 : 1203612 : ? OMP_CLAUSE_INBRANCH : OMP_CLAUSE_NOTINBRANCH);
639 : 1203612 : omp_clause = build_omp_clause (UNKNOWN_LOCATION, code);
640 : 1203612 : omp_clause = build_tree_list (NULL_TREE, omp_clause);
641 : : }
642 : :
643 : 1203612 : DECL_ATTRIBUTES (fndecl)
644 : 2407224 : = tree_cons (get_identifier ("omp declare simd"), omp_clause,
645 : 1203612 : 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 : 74721 : gfc_adjust_builtins (void)
655 : : {
656 : 74721 : gfc_intrinsic_map_t *m;
657 : 4483260 : for (m = gfc_intrinsic_map;
658 : 4483260 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
659 : : {
660 : 4408539 : add_simd_flag_for_built_in (m->real4_decl);
661 : 4408539 : add_simd_flag_for_built_in (m->complex4_decl);
662 : 4408539 : add_simd_flag_for_built_in (m->real8_decl);
663 : 4408539 : add_simd_flag_for_built_in (m->complex8_decl);
664 : 4408539 : add_simd_flag_for_built_in (m->real10_decl);
665 : 4408539 : add_simd_flag_for_built_in (m->complex10_decl);
666 : 4408539 : add_simd_flag_for_built_in (m->real16_decl);
667 : 4408539 : add_simd_flag_for_built_in (m->complex16_decl);
668 : 4408539 : add_simd_flag_for_built_in (m->real16_decl);
669 : 4408539 : add_simd_flag_for_built_in (m->complex16_decl);
670 : : }
671 : :
672 : : /* Release all strings. */
673 : 74721 : if (gfc_vectorized_builtins != NULL)
674 : : {
675 : 1654961 : for (hash_map<nofree_string_hash, int>::iterator it
676 : 30094 : = gfc_vectorized_builtins->begin ();
677 : 1654961 : it != gfc_vectorized_builtins->end (); ++it)
678 : 1624867 : free (CONST_CAST (char *, (*it).first));
679 : :
680 : 60188 : delete gfc_vectorized_builtins;
681 : 30094 : gfc_vectorized_builtins = NULL;
682 : : }
683 : 74721 : }
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 : 30803 : gfc_build_intrinsic_lib_fndecls (void)
690 : : {
691 : 30803 : gfc_intrinsic_map_t *m;
692 : 30803 : tree quad_decls[END_BUILTINS + 1];
693 : :
694 : 30803 : 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 : 30803 : tree type, complex_type, func_1, func_2, func_3, func_cabs, func_frexp;
701 : 30803 : tree func_iround, func_lround, func_llround, func_scalbn, func_cpow;
702 : :
703 : 30803 : memset (quad_decls, 0, sizeof(tree) * (END_BUILTINS + 1));
704 : :
705 : 30803 : type = gfc_float128_type_node;
706 : 30803 : complex_type = gfc_complex_float128_type_node;
707 : : /* type (*) (type) */
708 : 30803 : func_1 = build_function_type_list (type, type, NULL_TREE);
709 : : /* int (*) (type) */
710 : 30803 : func_iround = build_function_type_list (integer_type_node,
711 : : type, NULL_TREE);
712 : : /* long (*) (type) */
713 : 30803 : func_lround = build_function_type_list (long_integer_type_node,
714 : : type, NULL_TREE);
715 : : /* long long (*) (type) */
716 : 30803 : func_llround = build_function_type_list (long_long_integer_type_node,
717 : : type, NULL_TREE);
718 : : /* type (*) (type, type) */
719 : 30803 : func_2 = build_function_type_list (type, type, type, NULL_TREE);
720 : : /* type (*) (type, type, type) */
721 : 30803 : func_3 = build_function_type_list (type, type, type, type, NULL_TREE);
722 : : /* type (*) (type, &int) */
723 : 30803 : func_frexp
724 : 30803 : = build_function_type_list (type,
725 : : type,
726 : : build_pointer_type (integer_type_node),
727 : : NULL_TREE);
728 : : /* type (*) (type, int) */
729 : 30803 : func_scalbn = build_function_type_list (type,
730 : : type, integer_type_node, NULL_TREE);
731 : : /* type (*) (complex type) */
732 : 30803 : func_cabs = build_function_type_list (type, complex_type, NULL_TREE);
733 : : /* complex type (*) (complex type, complex type) */
734 : 30803 : func_cpow
735 : 30803 : = 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 : 30803 : quad_decls[BUILT_IN_SQRT]
763 : 30803 : = define_quad_builtin (gfc_real16_use_iec_60559
764 : : ? "sqrtf128" : "sqrtq", func_1, true);
765 : : }
766 : :
767 : : /* Add GCC builtin functions. */
768 : 1817377 : for (m = gfc_intrinsic_map;
769 : 1848180 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
770 : : {
771 : 1817377 : if (m->float_built_in != END_BUILTINS)
772 : 1694165 : m->real4_decl = builtin_decl_explicit (m->float_built_in);
773 : 1817377 : if (m->complex_float_built_in != END_BUILTINS)
774 : 492848 : m->complex4_decl = builtin_decl_explicit (m->complex_float_built_in);
775 : 1817377 : if (m->double_built_in != END_BUILTINS)
776 : 1694165 : m->real8_decl = builtin_decl_explicit (m->double_built_in);
777 : 1817377 : if (m->complex_double_built_in != END_BUILTINS)
778 : 492848 : m->complex8_decl = builtin_decl_explicit (m->complex_double_built_in);
779 : :
780 : : /* If real(kind=10) exists, it is always long double. */
781 : 1817377 : if (m->long_double_built_in != END_BUILTINS)
782 : 1694165 : m->real10_decl = builtin_decl_explicit (m->long_double_built_in);
783 : 1817377 : if (m->complex_long_double_built_in != END_BUILTINS)
784 : 492848 : m->complex10_decl
785 : 492848 : = builtin_decl_explicit (m->complex_long_double_built_in);
786 : :
787 : 1817377 : 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 : 1817377 : 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 : 646863 : m->real16_decl = quad_decls[m->double_built_in];
801 : : }
802 : 1170514 : 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 : 30803 : }
809 : :
810 : :
811 : : /* Create a fndecl for a simple intrinsic library function. */
812 : :
813 : : static tree
814 : 4397 : gfc_get_intrinsic_lib_fndecl (gfc_intrinsic_map_t * m, gfc_expr * expr)
815 : : {
816 : 4397 : tree type;
817 : 4397 : vec<tree, va_gc> *argtypes;
818 : 4397 : tree fndecl;
819 : 4397 : gfc_actual_arglist *actual;
820 : 4397 : tree *pdecl;
821 : 4397 : gfc_typespec *ts;
822 : 4397 : char name[GFC_MAX_SYMBOL_LEN + 3];
823 : :
824 : 4397 : ts = &expr->ts;
825 : 4397 : if (ts->type == BT_REAL)
826 : : {
827 : 3553 : switch (ts->kind)
828 : : {
829 : 1265 : case 4:
830 : 1265 : pdecl = &m->real4_decl;
831 : 1265 : break;
832 : 1272 : case 8:
833 : 1272 : pdecl = &m->real8_decl;
834 : 1272 : break;
835 : 574 : case 10:
836 : 574 : pdecl = &m->real10_decl;
837 : 574 : break;
838 : 442 : case 16:
839 : 442 : pdecl = &m->real16_decl;
840 : 442 : 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 : 4397 : if (*pdecl)
871 : 4058 : 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 : 3851 : gfc_conv_intrinsic_lib_function (gfc_se * se, gfc_expr * expr)
927 : : {
928 : 3851 : gfc_intrinsic_map_t *m;
929 : 3851 : tree fndecl;
930 : 3851 : tree rettype;
931 : 3851 : tree *args;
932 : 3851 : unsigned int num_args;
933 : 3851 : gfc_isym_id id;
934 : :
935 : 3851 : id = expr->value.function.isym->id;
936 : : /* Find the entry for this function. */
937 : 79085 : for (m = gfc_intrinsic_map;
938 : 79085 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
939 : : {
940 : 79085 : if (id == m->id)
941 : : break;
942 : : }
943 : :
944 : 3851 : 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 : 3851 : num_args = gfc_intrinsic_argument_list_length (expr);
952 : 3851 : args = XALLOCAVEC (tree, num_args);
953 : :
954 : 3851 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
955 : 3851 : fndecl = gfc_get_intrinsic_lib_fndecl (m, expr);
956 : 3851 : rettype = TREE_TYPE (TREE_TYPE (fndecl));
957 : :
958 : 3851 : fndecl = build_addr (fndecl);
959 : 3851 : se->expr = build_call_array_loc (input_location, rettype, fndecl, num_args, args);
960 : 3851 : }
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 : 1162 : conv_caf_func_index (stmtblock_t *block, gfc_namespace *ns, const char *pat,
1032 : : gfc_expr *hash)
1033 : : {
1034 : 1162 : char *name;
1035 : 1162 : gfc_se argse;
1036 : 1162 : gfc_expr func_index;
1037 : 1162 : gfc_symtree *index_st;
1038 : 1162 : tree func_index_tree;
1039 : 1162 : stmtblock_t blk;
1040 : :
1041 : : /* Need to get namespace where static variables are possible. */
1042 : 1162 : while (ns && ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
1043 : 0 : ns = ns->parent;
1044 : 1162 : gcc_assert (ns);
1045 : :
1046 : 1162 : name = xasprintf (pat, caf_call_cnt);
1047 : 1162 : gcc_assert (!gfc_get_sym_tree (name, ns, &index_st, false));
1048 : 1162 : free (name);
1049 : :
1050 : 1162 : index_st->n.sym->attr.flavor = FL_VARIABLE;
1051 : 1162 : index_st->n.sym->attr.save = SAVE_EXPLICIT;
1052 : 1162 : index_st->n.sym->value
1053 : 1162 : = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
1054 : : &gfc_current_locus);
1055 : 1162 : mpz_set_si (index_st->n.sym->value->value.integer, -1);
1056 : 1162 : index_st->n.sym->ts.type = BT_INTEGER;
1057 : 1162 : index_st->n.sym->ts.kind = gfc_default_integer_kind;
1058 : 1162 : gfc_set_sym_referenced (index_st->n.sym);
1059 : 1162 : memset (&func_index, 0, sizeof (gfc_expr));
1060 : 1162 : gfc_clear_ts (&func_index.ts);
1061 : 1162 : func_index.expr_type = EXPR_VARIABLE;
1062 : 1162 : func_index.symtree = index_st;
1063 : 1162 : func_index.ts = index_st->n.sym->ts;
1064 : 1162 : gfc_commit_symbol (index_st->n.sym);
1065 : :
1066 : 1162 : gfc_init_se (&argse, NULL);
1067 : 1162 : gfc_conv_expr (&argse, &func_index);
1068 : 1162 : gfc_add_block_to_block (block, &argse.pre);
1069 : 1162 : func_index_tree = argse.expr;
1070 : :
1071 : 1162 : gfc_init_se (&argse, NULL);
1072 : 1162 : gfc_conv_expr (&argse, hash);
1073 : :
1074 : 1162 : gfc_init_block (&blk);
1075 : 1162 : gfc_add_modify (&blk, func_index_tree,
1076 : : build_call_expr (gfor_fndecl_caf_get_remote_function_index, 1,
1077 : : argse.expr));
1078 : 1162 : gfc_add_expr_to_block (
1079 : : block,
1080 : : build3 (COND_EXPR, void_type_node,
1081 : : gfc_likely (build2 (EQ_EXPR, logical_type_node, func_index_tree,
1082 : : build_int_cst (integer_type_node, -1)),
1083 : : PRED_FIRST_MATCH),
1084 : : gfc_finish_block (&blk), NULL_TREE));
1085 : :
1086 : 1162 : return func_index_tree;
1087 : : }
1088 : :
1089 : : static tree
1090 : 1162 : conv_caf_add_call_data (stmtblock_t *blk, gfc_namespace *ns, const char *pat,
1091 : : gfc_symbol *data_sym, tree *data_size)
1092 : : {
1093 : 1162 : char *name;
1094 : 1162 : gfc_symtree *data_st;
1095 : 1162 : gfc_constructor *con;
1096 : 1162 : gfc_expr data, data_init;
1097 : 1162 : gfc_se argse;
1098 : 1162 : tree data_tree;
1099 : :
1100 : 1162 : memset (&data, 0, sizeof (gfc_expr));
1101 : 1162 : gfc_clear_ts (&data.ts);
1102 : 1162 : data.expr_type = EXPR_VARIABLE;
1103 : 1162 : name = xasprintf (pat, caf_call_cnt);
1104 : 1162 : gcc_assert (!gfc_get_sym_tree (name, ns, &data_st, false));
1105 : 1162 : free (name);
1106 : 1162 : data_st->n.sym->attr.flavor = FL_VARIABLE;
1107 : 1162 : data_st->n.sym->ts = data_sym->ts;
1108 : 1162 : data.symtree = data_st;
1109 : 1162 : gfc_set_sym_referenced (data.symtree->n.sym);
1110 : 1162 : data.ts = data_st->n.sym->ts;
1111 : 1162 : gfc_commit_symbol (data_st->n.sym);
1112 : :
1113 : 1162 : memset (&data_init, 0, sizeof (gfc_expr));
1114 : 1162 : gfc_clear_ts (&data_init.ts);
1115 : 1162 : data_init.expr_type = EXPR_STRUCTURE;
1116 : 1162 : data_init.ts = data.ts;
1117 : 1366 : for (gfc_component *comp = data.ts.u.derived->components; comp;
1118 : 204 : comp = comp->next)
1119 : : {
1120 : 204 : con = gfc_constructor_get ();
1121 : 204 : con->expr = comp->initializer;
1122 : 204 : comp->initializer = NULL;
1123 : 204 : gfc_constructor_append (&data_init.value.constructor, con);
1124 : : }
1125 : :
1126 : 1162 : if (data.ts.u.derived->components)
1127 : : {
1128 : 68 : gfc_init_se (&argse, NULL);
1129 : 68 : gfc_conv_expr (&argse, &data);
1130 : 68 : data_tree = argse.expr;
1131 : 68 : gfc_add_expr_to_block (blk,
1132 : : gfc_trans_structure_assign (data_tree, &data_init,
1133 : : true, true));
1134 : 68 : gfc_constructor_free (data_init.value.constructor);
1135 : 68 : *data_size = TREE_TYPE (data_tree)->type_common.size_unit;
1136 : 68 : data_tree = gfc_build_addr_expr (pvoid_type_node, data_tree);
1137 : : }
1138 : : else
1139 : : {
1140 : 1094 : data_tree = build_zero_cst (pvoid_type_node);
1141 : 1094 : *data_size = build_zero_cst (size_type_node);
1142 : : }
1143 : :
1144 : 1162 : 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 : 1030 : conv_stat_and_team (stmtblock_t *block, gfc_expr *expr, tree *stat, tree *team,
1164 : : tree *team_no)
1165 : : {
1166 : 1030 : gfc_expr *stat_e, *team_e;
1167 : :
1168 : 1030 : stat_e = gfc_find_stat_co (expr);
1169 : 1030 : if (stat_e)
1170 : : {
1171 : 22 : gfc_se stat_se;
1172 : 22 : gfc_init_se (&stat_se, NULL);
1173 : 22 : gfc_conv_expr_reference (&stat_se, stat_e);
1174 : 22 : *stat = stat_se.expr;
1175 : 22 : gfc_add_block_to_block (block, &stat_se.pre);
1176 : 22 : gfc_add_block_to_block (block, &stat_se.post);
1177 : : }
1178 : : else
1179 : 1008 : *stat = null_pointer_node;
1180 : :
1181 : 1030 : team_e = gfc_find_team_co (expr, TEAM_TEAM);
1182 : 1030 : if (team_e)
1183 : : {
1184 : 9 : gfc_se team_se;
1185 : 9 : gfc_init_se (&team_se, NULL);
1186 : 9 : gfc_conv_expr (&team_se, team_e);
1187 : 9 : *team
1188 : 9 : = gfc_build_addr_expr (NULL_TREE, gfc_trans_force_lval (&team_se.pre,
1189 : : team_se.expr));
1190 : 9 : gfc_add_block_to_block (block, &team_se.pre);
1191 : 9 : gfc_add_block_to_block (block, &team_se.post);
1192 : : }
1193 : : else
1194 : 1021 : *team = null_pointer_node;
1195 : :
1196 : 1030 : team_e = gfc_find_team_co (expr, TEAM_NUMBER);
1197 : 1030 : if (team_e)
1198 : : {
1199 : 15 : gfc_se team_se;
1200 : 15 : gfc_init_se (&team_se, NULL);
1201 : 15 : gfc_conv_expr (&team_se, team_e);
1202 : 15 : *team_no = gfc_build_addr_expr (
1203 : : NULL_TREE,
1204 : : gfc_trans_force_lval (&team_se.pre,
1205 : : fold_convert (integer_type_node, team_se.expr)));
1206 : 15 : gfc_add_block_to_block (block, &team_se.pre);
1207 : 15 : gfc_add_block_to_block (block, &team_se.post);
1208 : : }
1209 : : else
1210 : 1015 : *team_no = null_pointer_node;
1211 : 1030 : }
1212 : :
1213 : : /* Get data from a remote coarray. */
1214 : :
1215 : : static void
1216 : 895 : gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs,
1217 : : bool may_realloc, symbol_attribute *caf_attr)
1218 : : {
1219 : 895 : gfc_expr *array_expr;
1220 : 895 : tree caf_decl, token, image_index, tmp, res_var, type, stat, dest_size,
1221 : : dest_data, opt_dest_desc, get_fn_index_tree, add_data_tree, add_data_size,
1222 : : opt_src_desc, opt_src_charlen, opt_dest_charlen, team, team_no;
1223 : 895 : symbol_attribute caf_attr_store;
1224 : 895 : gfc_namespace *ns;
1225 : 895 : gfc_expr *get_fn_hash = expr->value.function.actual->next->expr,
1226 : 895 : *get_fn_expr = expr->value.function.actual->next->next->expr;
1227 : 895 : gfc_symbol *add_data_sym = get_fn_expr->symtree->n.sym->formal->sym;
1228 : :
1229 : 895 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1230 : :
1231 : 895 : if (se->ss && se->ss->info->useflags)
1232 : : {
1233 : : /* Access the previously obtained result. */
1234 : 352 : gfc_conv_tmp_array_ref (se);
1235 : 352 : return;
1236 : : }
1237 : :
1238 : 543 : array_expr = expr->value.function.actual->expr;
1239 : 543 : ns = array_expr->expr_type == EXPR_VARIABLE
1240 : 543 : && !array_expr->symtree->n.sym->attr.associate_var
1241 : 543 : ? array_expr->symtree->n.sym->ns
1242 : : : gfc_current_ns;
1243 : 543 : type = gfc_typenode_for_spec (&array_expr->ts);
1244 : :
1245 : 543 : if (caf_attr == NULL)
1246 : : {
1247 : 543 : caf_attr_store = gfc_caf_attr (array_expr);
1248 : 543 : caf_attr = &caf_attr_store;
1249 : : }
1250 : :
1251 : 543 : res_var = lhs;
1252 : :
1253 : 543 : conv_stat_and_team (&se->pre, expr, &stat, &team, &team_no);
1254 : :
1255 : 543 : get_fn_index_tree
1256 : 543 : = conv_caf_func_index (&se->pre, ns, "__caf_get_from_remote_fn_index_%d",
1257 : : get_fn_hash);
1258 : 543 : add_data_tree
1259 : 543 : = conv_caf_add_call_data (&se->pre, ns, "__caf_get_from_remote_add_data_%d",
1260 : : add_data_sym, &add_data_size);
1261 : 543 : ++caf_call_cnt;
1262 : :
1263 : 543 : if (array_expr->rank == 0)
1264 : : {
1265 : 190 : res_var = gfc_create_var (type, "caf_res");
1266 : 190 : if (array_expr->ts.type == BT_CHARACTER)
1267 : : {
1268 : 17 : gfc_conv_string_length (array_expr->ts.u.cl, array_expr, &se->pre);
1269 : 17 : se->string_length = array_expr->ts.u.cl->backend_decl;
1270 : 17 : opt_src_charlen = gfc_build_addr_expr (
1271 : : NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1272 : 17 : dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1273 : : }
1274 : : else
1275 : : {
1276 : 173 : dest_size = res_var->typed.type->type_common.size_unit;
1277 : 173 : opt_src_charlen
1278 : 173 : = build_zero_cst (build_pointer_type (size_type_node));
1279 : : }
1280 : 190 : dest_data
1281 : 190 : = gfc_evaluate_now (gfc_build_addr_expr (NULL_TREE, res_var), &se->pre);
1282 : 190 : res_var = build_fold_indirect_ref (dest_data);
1283 : 190 : dest_data = gfc_build_addr_expr (pvoid_type_node, dest_data);
1284 : 190 : opt_dest_desc = build_zero_cst (pvoid_type_node);
1285 : : }
1286 : : else
1287 : : {
1288 : : /* Create temporary. */
1289 : 353 : may_realloc = gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
1290 : : type, NULL_TREE, false, false,
1291 : : false, &array_expr->where)
1292 : : == NULL_TREE;
1293 : 353 : res_var = se->ss->info->data.array.descriptor;
1294 : 353 : if (array_expr->ts.type == BT_CHARACTER)
1295 : : {
1296 : 8 : se->string_length = array_expr->ts.u.cl->backend_decl;
1297 : 8 : opt_src_charlen = gfc_build_addr_expr (
1298 : : NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1299 : 8 : dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1300 : : }
1301 : : else
1302 : : {
1303 : 345 : opt_src_charlen
1304 : 345 : = build_zero_cst (build_pointer_type (size_type_node));
1305 : 345 : dest_size = fold_build2 (
1306 : : MULT_EXPR, size_type_node,
1307 : : fold_convert (size_type_node,
1308 : : array_expr->shape
1309 : : ? conv_shape_to_cst (array_expr)
1310 : : : gfc_conv_descriptor_size (res_var,
1311 : : array_expr->rank)),
1312 : : fold_convert (size_type_node,
1313 : : gfc_conv_descriptor_span_get (res_var)));
1314 : : }
1315 : 353 : opt_dest_desc = res_var;
1316 : 353 : dest_data = gfc_conv_descriptor_data_get (res_var);
1317 : 353 : opt_dest_desc = gfc_build_addr_expr (NULL_TREE, opt_dest_desc);
1318 : 353 : if (may_realloc)
1319 : : {
1320 : 52 : tmp = gfc_conv_descriptor_data_get (res_var);
1321 : 52 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
1322 : : NULL_TREE, NULL_TREE, true, NULL,
1323 : : GFC_CAF_COARRAY_NOCOARRAY);
1324 : 52 : gfc_add_expr_to_block (&se->post, tmp);
1325 : : }
1326 : 353 : dest_data
1327 : 353 : = gfc_build_addr_expr (NULL_TREE,
1328 : : gfc_trans_force_lval (&se->pre, dest_data));
1329 : : }
1330 : :
1331 : 543 : opt_dest_charlen = opt_src_charlen;
1332 : 543 : caf_decl = gfc_get_tree_for_caf_expr (array_expr);
1333 : 543 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1334 : 1 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1335 : :
1336 : 543 : if (!TYPE_LANG_SPECIFIC (TREE_TYPE (caf_decl))->rank
1337 : 543 : || GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl)))
1338 : 473 : opt_src_desc = build_zero_cst (pvoid_type_node);
1339 : : else
1340 : 70 : opt_src_desc = gfc_build_addr_expr (pvoid_type_node, caf_decl);
1341 : :
1342 : 543 : image_index = gfc_caf_get_image_index (&se->pre, array_expr, caf_decl);
1343 : 543 : gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, array_expr);
1344 : :
1345 : : /* It guarantees memory consistency within the same segment. */
1346 : 543 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1347 : 543 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1348 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1349 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1350 : 543 : ASM_VOLATILE_P (tmp) = 1;
1351 : 543 : gfc_add_expr_to_block (&se->pre, tmp);
1352 : :
1353 : 543 : tmp = build_call_expr_loc (
1354 : : input_location, gfor_fndecl_caf_get_from_remote, 15, token, opt_src_desc,
1355 : : opt_src_charlen, image_index, dest_size, dest_data, opt_dest_charlen,
1356 : : opt_dest_desc, constant_boolean_node (may_realloc, boolean_type_node),
1357 : : get_fn_index_tree, add_data_tree, add_data_size, stat, team, team_no);
1358 : :
1359 : 543 : gfc_add_expr_to_block (&se->pre, tmp);
1360 : :
1361 : 543 : if (se->ss)
1362 : 353 : gfc_advance_se_ss_chain (se);
1363 : :
1364 : 543 : se->expr = res_var;
1365 : :
1366 : 543 : return;
1367 : : }
1368 : :
1369 : : /* Generate call to caf_is_present_on_remote for allocated (coarrary[...])
1370 : : calls. */
1371 : :
1372 : : static void
1373 : 132 : gfc_conv_intrinsic_caf_is_present_remote (gfc_se *se, gfc_expr *e)
1374 : : {
1375 : 132 : gfc_expr *caf_expr, *hash, *present_fn;
1376 : 132 : gfc_symbol *add_data_sym;
1377 : 132 : tree fn_index, add_data_tree, add_data_size, caf_decl, image_index, token;
1378 : :
1379 : 132 : gcc_assert (e->expr_type == EXPR_FUNCTION
1380 : : && e->value.function.isym->id
1381 : : == GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE);
1382 : 132 : caf_expr = e->value.function.actual->expr;
1383 : 132 : hash = e->value.function.actual->next->expr;
1384 : 132 : present_fn = e->value.function.actual->next->next->expr;
1385 : 132 : add_data_sym = present_fn->symtree->n.sym->formal->sym;
1386 : :
1387 : 132 : fn_index = conv_caf_func_index (&se->pre, e->symtree->n.sym->ns,
1388 : : "__caf_present_on_remote_fn_index_%d", hash);
1389 : 132 : add_data_tree = conv_caf_add_call_data (&se->pre, e->symtree->n.sym->ns,
1390 : : "__caf_present_on_remote_add_data_%d",
1391 : : add_data_sym, &add_data_size);
1392 : 132 : ++caf_call_cnt;
1393 : :
1394 : 132 : caf_decl = gfc_get_tree_for_caf_expr (caf_expr);
1395 : 132 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1396 : 2 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1397 : :
1398 : 132 : image_index = gfc_caf_get_image_index (&se->pre, caf_expr, caf_decl);
1399 : 132 : gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, caf_expr);
1400 : :
1401 : 132 : se->expr
1402 : 132 : = fold_convert (logical_type_node,
1403 : : build_call_expr_loc (input_location,
1404 : : gfor_fndecl_caf_is_present_on_remote,
1405 : : 5, token, image_index, fn_index,
1406 : : add_data_tree, add_data_size));
1407 : 132 : }
1408 : :
1409 : : static tree
1410 : 293 : conv_caf_send_to_remote (gfc_code *code)
1411 : : {
1412 : 293 : gfc_expr *lhs_expr, *rhs_expr, *lhs_hash, *receiver_fn_expr;
1413 : 293 : gfc_symbol *add_data_sym;
1414 : 293 : gfc_se lhs_se, rhs_se;
1415 : 293 : stmtblock_t block;
1416 : 293 : gfc_namespace *ns;
1417 : 293 : tree caf_decl, token, rhs_size, image_index, tmp, rhs_data;
1418 : 293 : tree lhs_stat, lhs_team, lhs_team_no, opt_lhs_charlen, opt_rhs_charlen;
1419 : 293 : tree opt_lhs_desc = NULL_TREE, opt_rhs_desc = NULL_TREE;
1420 : 293 : tree receiver_fn_index_tree, add_data_tree, add_data_size;
1421 : :
1422 : 293 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1423 : 293 : gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SEND);
1424 : :
1425 : 293 : lhs_expr = code->ext.actual->expr;
1426 : 293 : rhs_expr = code->ext.actual->next->expr;
1427 : 293 : lhs_hash = code->ext.actual->next->next->expr;
1428 : 293 : receiver_fn_expr = code->ext.actual->next->next->next->expr;
1429 : 293 : add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1430 : :
1431 : 293 : ns = lhs_expr->expr_type == EXPR_VARIABLE
1432 : 293 : && !lhs_expr->symtree->n.sym->attr.associate_var
1433 : 293 : ? lhs_expr->symtree->n.sym->ns
1434 : : : gfc_current_ns;
1435 : :
1436 : 293 : gfc_init_block (&block);
1437 : :
1438 : : /* LHS. */
1439 : 293 : gfc_init_se (&lhs_se, NULL);
1440 : 293 : caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1441 : 293 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1442 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1443 : 293 : if (lhs_expr->rank == 0)
1444 : : {
1445 : 241 : if (lhs_expr->ts.type == BT_CHARACTER)
1446 : : {
1447 : 12 : gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1448 : 12 : lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1449 : 12 : opt_lhs_charlen = gfc_build_addr_expr (
1450 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1451 : : }
1452 : : else
1453 : 229 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1454 : 241 : opt_lhs_desc = null_pointer_node;
1455 : : }
1456 : : else
1457 : : {
1458 : 52 : gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1459 : 52 : gfc_add_block_to_block (&block, &lhs_se.pre);
1460 : 52 : opt_lhs_desc = lhs_se.expr;
1461 : 52 : if (lhs_expr->ts.type == BT_CHARACTER)
1462 : 22 : opt_lhs_charlen = gfc_build_addr_expr (
1463 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1464 : : else
1465 : 30 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1466 : : /* Get the third formal argument of the receiver function. (This is the
1467 : : location where to put the data on the remote image.) Need to look at
1468 : : the argument in the function decl, because in the gfc_symbol's formal
1469 : : argument an array may have no descriptor while in the generated
1470 : : function decl it has. */
1471 : 52 : tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1472 : : TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1473 : 52 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1474 : 28 : opt_lhs_desc = null_pointer_node;
1475 : : else
1476 : 24 : opt_lhs_desc
1477 : 24 : = gfc_build_addr_expr (NULL_TREE,
1478 : : gfc_trans_force_lval (&block, opt_lhs_desc));
1479 : : }
1480 : :
1481 : : /* Obtain token, offset and image index for the LHS. */
1482 : 293 : image_index = gfc_caf_get_image_index (&block, lhs_expr, caf_decl);
1483 : 293 : gfc_get_caf_token_offset (&lhs_se, &token, NULL, caf_decl, NULL, lhs_expr);
1484 : :
1485 : : /* RHS. */
1486 : 293 : gfc_init_se (&rhs_se, NULL);
1487 : 293 : if (rhs_expr->rank == 0)
1488 : : {
1489 : 171 : rhs_se.want_pointer = rhs_expr->ts.type == BT_CHARACTER;
1490 : 171 : gfc_conv_expr (&rhs_se, rhs_expr);
1491 : 171 : gfc_add_block_to_block (&block, &rhs_se.pre);
1492 : 171 : opt_rhs_desc = null_pointer_node;
1493 : 171 : if (rhs_expr->ts.type == BT_CHARACTER)
1494 : : {
1495 : 20 : rhs_data
1496 : 20 : = rhs_expr->expr_type == EXPR_CONSTANT
1497 : 20 : ? gfc_build_addr_expr (NULL_TREE,
1498 : : gfc_trans_force_lval (&block,
1499 : : rhs_se.expr))
1500 : : : rhs_se.expr;
1501 : 20 : opt_rhs_charlen = gfc_build_addr_expr (
1502 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1503 : 20 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1504 : : }
1505 : : else
1506 : : {
1507 : 151 : rhs_data
1508 : 151 : = gfc_build_addr_expr (NULL_TREE,
1509 : : gfc_trans_force_lval (&block, rhs_se.expr));
1510 : 151 : opt_rhs_charlen
1511 : 151 : = build_zero_cst (build_pointer_type (size_type_node));
1512 : 151 : rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1513 : : }
1514 : : }
1515 : : else
1516 : : {
1517 : 244 : rhs_se.force_tmp = rhs_expr->shape == NULL
1518 : 122 : || !gfc_is_simply_contiguous (rhs_expr, false, false);
1519 : 122 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1520 : 122 : gfc_add_block_to_block (&block, &rhs_se.pre);
1521 : 122 : opt_rhs_desc = rhs_se.expr;
1522 : 122 : if (rhs_expr->ts.type == BT_CHARACTER)
1523 : : {
1524 : 14 : opt_rhs_charlen = gfc_build_addr_expr (
1525 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1526 : 14 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1527 : : }
1528 : : else
1529 : : {
1530 : 108 : opt_rhs_charlen
1531 : 108 : = build_zero_cst (build_pointer_type (size_type_node));
1532 : 108 : rhs_size = fold_build2 (
1533 : : MULT_EXPR, size_type_node,
1534 : : fold_convert (size_type_node,
1535 : : rhs_expr->shape
1536 : : ? conv_shape_to_cst (rhs_expr)
1537 : : : gfc_conv_descriptor_size (rhs_se.expr,
1538 : : rhs_expr->rank)),
1539 : : fold_convert (size_type_node,
1540 : : gfc_conv_descriptor_span_get (rhs_se.expr)));
1541 : : }
1542 : :
1543 : 122 : rhs_data = gfc_build_addr_expr (
1544 : : NULL_TREE, gfc_trans_force_lval (&block, gfc_conv_descriptor_data_get (
1545 : : opt_rhs_desc)));
1546 : 122 : opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1547 : : }
1548 : 293 : gfc_add_block_to_block (&block, &rhs_se.pre);
1549 : :
1550 : 293 : conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1551 : :
1552 : 293 : receiver_fn_index_tree
1553 : 293 : = conv_caf_func_index (&block, ns, "__caf_send_to_remote_fn_index_%d",
1554 : : lhs_hash);
1555 : 293 : add_data_tree
1556 : 293 : = conv_caf_add_call_data (&block, ns, "__caf_send_to_remote_add_data_%d",
1557 : : add_data_sym, &add_data_size);
1558 : 293 : ++caf_call_cnt;
1559 : :
1560 : 293 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_send_to_remote, 14,
1561 : : token, opt_lhs_desc, opt_lhs_charlen, image_index,
1562 : : rhs_size, rhs_data, opt_rhs_charlen, opt_rhs_desc,
1563 : : receiver_fn_index_tree, add_data_tree,
1564 : : add_data_size, lhs_stat, lhs_team, lhs_team_no);
1565 : :
1566 : 293 : gfc_add_expr_to_block (&block, tmp);
1567 : 293 : gfc_add_block_to_block (&block, &lhs_se.post);
1568 : 293 : gfc_add_block_to_block (&block, &rhs_se.post);
1569 : :
1570 : : /* It guarantees memory consistency within the same segment. */
1571 : 293 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1572 : 293 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1573 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1574 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1575 : 293 : ASM_VOLATILE_P (tmp) = 1;
1576 : 293 : gfc_add_expr_to_block (&block, tmp);
1577 : :
1578 : 293 : return gfc_finish_block (&block);
1579 : : }
1580 : :
1581 : : /* Send-get data to a remote coarray. */
1582 : :
1583 : : static tree
1584 : 97 : conv_caf_sendget (gfc_code *code)
1585 : : {
1586 : : /* lhs stuff */
1587 : 97 : gfc_expr *lhs_expr, *lhs_hash, *receiver_fn_expr;
1588 : 97 : gfc_symbol *lhs_add_data_sym;
1589 : 97 : gfc_se lhs_se;
1590 : 97 : tree lhs_caf_decl, lhs_token, opt_lhs_charlen,
1591 : 97 : opt_lhs_desc = NULL_TREE, receiver_fn_index_tree, lhs_image_index,
1592 : : lhs_add_data_tree, lhs_add_data_size, lhs_stat, lhs_team, lhs_team_no;
1593 : 97 : int transfer_rank;
1594 : :
1595 : : /* rhs stuff */
1596 : 97 : gfc_expr *rhs_expr, *rhs_hash, *sender_fn_expr;
1597 : 97 : gfc_symbol *rhs_add_data_sym;
1598 : 97 : gfc_se rhs_se;
1599 : 97 : tree rhs_caf_decl, rhs_token, opt_rhs_charlen,
1600 : 97 : opt_rhs_desc = NULL_TREE, sender_fn_index_tree, rhs_image_index,
1601 : : rhs_add_data_tree, rhs_add_data_size, rhs_stat, rhs_team, rhs_team_no;
1602 : :
1603 : : /* shared */
1604 : 97 : stmtblock_t block;
1605 : 97 : gfc_namespace *ns;
1606 : 97 : tree tmp, rhs_size;
1607 : :
1608 : 97 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1609 : 97 : gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SENDGET);
1610 : :
1611 : 97 : lhs_expr = code->ext.actual->expr;
1612 : 97 : rhs_expr = code->ext.actual->next->expr;
1613 : 97 : lhs_hash = code->ext.actual->next->next->expr;
1614 : 97 : receiver_fn_expr = code->ext.actual->next->next->next->expr;
1615 : 97 : rhs_hash = code->ext.actual->next->next->next->next->expr;
1616 : 97 : sender_fn_expr = code->ext.actual->next->next->next->next->next->expr;
1617 : :
1618 : 97 : lhs_add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1619 : 97 : rhs_add_data_sym = sender_fn_expr->symtree->n.sym->formal->sym;
1620 : :
1621 : 97 : ns = lhs_expr->expr_type == EXPR_VARIABLE
1622 : 97 : && !lhs_expr->symtree->n.sym->attr.associate_var
1623 : 97 : ? lhs_expr->symtree->n.sym->ns
1624 : : : gfc_current_ns;
1625 : :
1626 : 97 : gfc_init_block (&block);
1627 : :
1628 : 97 : lhs_stat = null_pointer_node;
1629 : 97 : lhs_team = null_pointer_node;
1630 : 97 : rhs_stat = null_pointer_node;
1631 : 97 : rhs_team = null_pointer_node;
1632 : :
1633 : : /* LHS. */
1634 : 97 : gfc_init_se (&lhs_se, NULL);
1635 : 97 : lhs_caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1636 : 97 : if (TREE_CODE (TREE_TYPE (lhs_caf_decl)) == REFERENCE_TYPE)
1637 : 0 : lhs_caf_decl = build_fold_indirect_ref_loc (input_location, lhs_caf_decl);
1638 : 97 : if (lhs_expr->rank == 0)
1639 : : {
1640 : 63 : if (lhs_expr->ts.type == BT_CHARACTER)
1641 : : {
1642 : 8 : gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1643 : 8 : lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1644 : 8 : opt_lhs_charlen = gfc_build_addr_expr (
1645 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1646 : : }
1647 : : else
1648 : 55 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1649 : 63 : opt_lhs_desc = null_pointer_node;
1650 : : }
1651 : : else
1652 : : {
1653 : 34 : gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1654 : 34 : gfc_add_block_to_block (&block, &lhs_se.pre);
1655 : 34 : opt_lhs_desc = lhs_se.expr;
1656 : 34 : if (lhs_expr->ts.type == BT_CHARACTER)
1657 : 16 : opt_lhs_charlen = gfc_build_addr_expr (
1658 : : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1659 : : else
1660 : 18 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1661 : : /* Get the third formal argument of the receiver function. (This is the
1662 : : location where to put the data on the remote image.) Need to look at
1663 : : the argument in the function decl, because in the gfc_symbol's formal
1664 : : argument an array may have no descriptor while in the generated
1665 : : function decl it has. */
1666 : 34 : tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1667 : : TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1668 : 34 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1669 : 30 : opt_lhs_desc = null_pointer_node;
1670 : : else
1671 : 4 : opt_lhs_desc
1672 : 4 : = gfc_build_addr_expr (NULL_TREE,
1673 : : gfc_trans_force_lval (&block, opt_lhs_desc));
1674 : : }
1675 : :
1676 : : /* Obtain token, offset and image index for the LHS. */
1677 : 97 : lhs_image_index = gfc_caf_get_image_index (&block, lhs_expr, lhs_caf_decl);
1678 : 97 : gfc_get_caf_token_offset (&lhs_se, &lhs_token, NULL, lhs_caf_decl, NULL,
1679 : : lhs_expr);
1680 : :
1681 : : /* RHS. */
1682 : 97 : rhs_caf_decl = gfc_get_tree_for_caf_expr (rhs_expr);
1683 : 97 : if (TREE_CODE (TREE_TYPE (rhs_caf_decl)) == REFERENCE_TYPE)
1684 : 0 : rhs_caf_decl = build_fold_indirect_ref_loc (input_location, rhs_caf_decl);
1685 : 97 : transfer_rank = rhs_expr->rank;
1686 : 97 : gfc_expression_rank (rhs_expr);
1687 : 97 : gfc_init_se (&rhs_se, NULL);
1688 : 97 : if (rhs_expr->rank == 0)
1689 : : {
1690 : 64 : opt_rhs_desc = null_pointer_node;
1691 : 64 : if (rhs_expr->ts.type == BT_CHARACTER)
1692 : : {
1693 : 16 : gfc_conv_expr (&rhs_se, rhs_expr);
1694 : 16 : gfc_add_block_to_block (&block, &rhs_se.pre);
1695 : 16 : opt_rhs_charlen = gfc_build_addr_expr (
1696 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1697 : 16 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1698 : : }
1699 : : else
1700 : : {
1701 : 48 : gfc_typespec *ts
1702 : 48 : = &sender_fn_expr->symtree->n.sym->formal->next->next->sym->ts;
1703 : :
1704 : 48 : opt_rhs_charlen
1705 : 48 : = build_zero_cst (build_pointer_type (size_type_node));
1706 : 48 : rhs_size = gfc_typenode_for_spec (ts)->type_common.size_unit;
1707 : : }
1708 : : }
1709 : : /* Get the fifth formal argument of the getter function. This is the argument
1710 : : pointing to the data to get on the remote image. Need to look at the
1711 : : argument in the function decl, because in the gfc_symbol's formal argument
1712 : : an array may have no descriptor while in the generated function decl it
1713 : : has. */
1714 : 33 : else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_VALUE (
1715 : : TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1716 : : TREE_TYPE (sender_fn_expr->symtree->n.sym->backend_decl))))))))))
1717 : : {
1718 : 29 : rhs_se.data_not_needed = 1;
1719 : 29 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1720 : 29 : gfc_add_block_to_block (&block, &rhs_se.pre);
1721 : 29 : if (rhs_expr->ts.type == BT_CHARACTER)
1722 : : {
1723 : 8 : opt_rhs_charlen = gfc_build_addr_expr (
1724 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1725 : 8 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1726 : : }
1727 : : else
1728 : : {
1729 : 21 : opt_rhs_charlen
1730 : 21 : = build_zero_cst (build_pointer_type (size_type_node));
1731 : 21 : rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1732 : : }
1733 : 29 : opt_rhs_desc = null_pointer_node;
1734 : : }
1735 : : else
1736 : : {
1737 : 4 : gfc_ref *arr_ref = rhs_expr->ref;
1738 : 4 : while (arr_ref && arr_ref->type != REF_ARRAY)
1739 : 0 : arr_ref = arr_ref->next;
1740 : 4 : rhs_se.force_tmp
1741 : 8 : = (rhs_expr->shape == NULL
1742 : 4 : && (!arr_ref || !gfc_full_array_ref_p (arr_ref, nullptr)))
1743 : 8 : || !gfc_is_simply_contiguous (rhs_expr, false, false);
1744 : 4 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1745 : 4 : gfc_add_block_to_block (&block, &rhs_se.pre);
1746 : 4 : opt_rhs_desc = rhs_se.expr;
1747 : 4 : if (rhs_expr->ts.type == BT_CHARACTER)
1748 : : {
1749 : 0 : opt_rhs_charlen = gfc_build_addr_expr (
1750 : : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1751 : 0 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1752 : : }
1753 : : else
1754 : : {
1755 : 4 : opt_rhs_charlen
1756 : 4 : = build_zero_cst (build_pointer_type (size_type_node));
1757 : 4 : rhs_size = fold_build2 (
1758 : : MULT_EXPR, size_type_node,
1759 : : fold_convert (size_type_node,
1760 : : rhs_expr->shape
1761 : : ? conv_shape_to_cst (rhs_expr)
1762 : : : gfc_conv_descriptor_size (rhs_se.expr,
1763 : : rhs_expr->rank)),
1764 : : fold_convert (size_type_node,
1765 : : gfc_conv_descriptor_span_get (rhs_se.expr)));
1766 : : }
1767 : :
1768 : 4 : opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1769 : : }
1770 : 97 : gfc_add_block_to_block (&block, &rhs_se.pre);
1771 : :
1772 : : /* Obtain token, offset and image index for the RHS. */
1773 : 97 : rhs_image_index = gfc_caf_get_image_index (&block, rhs_expr, rhs_caf_decl);
1774 : 97 : gfc_get_caf_token_offset (&rhs_se, &rhs_token, NULL, rhs_caf_decl, NULL,
1775 : : rhs_expr);
1776 : :
1777 : : /* stat and team. */
1778 : 97 : conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1779 : 97 : conv_stat_and_team (&block, rhs_expr, &rhs_stat, &rhs_team, &rhs_team_no);
1780 : :
1781 : 97 : sender_fn_index_tree
1782 : 97 : = conv_caf_func_index (&block, ns, "__caf_transfer_from_fn_index_%d",
1783 : : rhs_hash);
1784 : 97 : rhs_add_data_tree
1785 : 97 : = conv_caf_add_call_data (&block, ns,
1786 : : "__caf_transfer_from_remote_add_data_%d",
1787 : : rhs_add_data_sym, &rhs_add_data_size);
1788 : 97 : receiver_fn_index_tree
1789 : 97 : = conv_caf_func_index (&block, ns, "__caf_transfer_to_remote_fn_index_%d",
1790 : : lhs_hash);
1791 : 97 : lhs_add_data_tree
1792 : 97 : = conv_caf_add_call_data (&block, ns,
1793 : : "__caf_transfer_to_remote_add_data_%d",
1794 : : lhs_add_data_sym, &lhs_add_data_size);
1795 : 97 : ++caf_call_cnt;
1796 : :
1797 : 97 : tmp = build_call_expr_loc (
1798 : : input_location, gfor_fndecl_caf_transfer_between_remotes, 22, lhs_token,
1799 : : opt_lhs_desc, opt_lhs_charlen, lhs_image_index, receiver_fn_index_tree,
1800 : : lhs_add_data_tree, lhs_add_data_size, rhs_token, opt_rhs_desc,
1801 : : opt_rhs_charlen, rhs_image_index, sender_fn_index_tree, rhs_add_data_tree,
1802 : : rhs_add_data_size, rhs_size,
1803 : : transfer_rank == 0 ? boolean_true_node : boolean_false_node, lhs_stat,
1804 : : rhs_stat, lhs_team, lhs_team_no, rhs_team, rhs_team_no);
1805 : :
1806 : 97 : gfc_add_expr_to_block (&block, tmp);
1807 : 97 : gfc_add_block_to_block (&block, &lhs_se.post);
1808 : 97 : gfc_add_block_to_block (&block, &rhs_se.post);
1809 : :
1810 : : /* It guarantees memory consistency within the same segment. */
1811 : 97 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1812 : 97 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1813 : : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1814 : : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1815 : 97 : ASM_VOLATILE_P (tmp) = 1;
1816 : 97 : gfc_add_expr_to_block (&block, tmp);
1817 : :
1818 : 97 : return gfc_finish_block (&block);
1819 : : }
1820 : :
1821 : :
1822 : : static void
1823 : 727 : trans_this_image (gfc_se * se, gfc_expr *expr)
1824 : : {
1825 : 727 : stmtblock_t loop;
1826 : 727 : tree type, desc, dim_arg, cond, tmp, m, loop_var, exit_label, min_var, lbound,
1827 : : ubound, extent, ml, team;
1828 : 727 : gfc_se argse;
1829 : 727 : int rank, corank;
1830 : :
1831 : : /* The case -fcoarray=single is handled elsewhere. */
1832 : 727 : gcc_assert (flag_coarray != GFC_FCOARRAY_SINGLE);
1833 : :
1834 : : /* Translate team, if present. */
1835 : 727 : if (expr->value.function.actual->next->next->expr)
1836 : : {
1837 : 18 : gfc_init_se (&argse, NULL);
1838 : 18 : gfc_conv_expr_val (&argse, expr->value.function.actual->next->next->expr);
1839 : 18 : gfc_add_block_to_block (&se->pre, &argse.pre);
1840 : 18 : gfc_add_block_to_block (&se->post, &argse.post);
1841 : 18 : team = fold_convert (pvoid_type_node, argse.expr);
1842 : : }
1843 : : else
1844 : 709 : team = null_pointer_node;
1845 : :
1846 : : /* Argument-free version: THIS_IMAGE(). */
1847 : 727 : if (expr->value.function.actual->expr == NULL)
1848 : : {
1849 : 556 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
1850 : : team);
1851 : 556 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
1852 : : tmp);
1853 : 560 : return;
1854 : : }
1855 : :
1856 : : /* Coarray-argument version: THIS_IMAGE(coarray [, dim]). */
1857 : :
1858 : 171 : type = gfc_get_int_type (gfc_default_integer_kind);
1859 : 171 : corank = expr->value.function.actual->expr->corank;
1860 : 171 : rank = expr->value.function.actual->expr->rank;
1861 : :
1862 : : /* Obtain the descriptor of the COARRAY. */
1863 : 171 : gfc_init_se (&argse, NULL);
1864 : 171 : argse.want_coarray = 1;
1865 : 171 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
1866 : 171 : gfc_add_block_to_block (&se->pre, &argse.pre);
1867 : 171 : gfc_add_block_to_block (&se->post, &argse.post);
1868 : 171 : desc = argse.expr;
1869 : :
1870 : 171 : if (se->ss)
1871 : : {
1872 : : /* Create an implicit second parameter from the loop variable. */
1873 : 41 : gcc_assert (!expr->value.function.actual->next->expr);
1874 : 41 : gcc_assert (corank > 0);
1875 : 41 : gcc_assert (se->loop->dimen == 1);
1876 : 41 : gcc_assert (se->ss->info->expr == expr);
1877 : :
1878 : 41 : dim_arg = se->loop->loopvar[0];
1879 : 41 : dim_arg = fold_build2_loc (input_location, PLUS_EXPR,
1880 : : gfc_array_index_type, dim_arg,
1881 : 41 : build_int_cst (TREE_TYPE (dim_arg), 1));
1882 : 41 : gfc_advance_se_ss_chain (se);
1883 : : }
1884 : : else
1885 : : {
1886 : : /* Use the passed DIM= argument. */
1887 : 130 : gcc_assert (expr->value.function.actual->next->expr);
1888 : 130 : gfc_init_se (&argse, NULL);
1889 : 130 : gfc_conv_expr_type (&argse, expr->value.function.actual->next->expr,
1890 : : gfc_array_index_type);
1891 : 130 : gfc_add_block_to_block (&se->pre, &argse.pre);
1892 : 130 : dim_arg = argse.expr;
1893 : :
1894 : 130 : if (INTEGER_CST_P (dim_arg))
1895 : : {
1896 : 72 : if (wi::ltu_p (wi::to_wide (dim_arg), 1)
1897 : 144 : || wi::gtu_p (wi::to_wide (dim_arg),
1898 : 72 : GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
1899 : 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
1900 : 0 : "dimension index", expr->value.function.isym->name,
1901 : : &expr->where);
1902 : : }
1903 : 58 : else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1904 : : {
1905 : 0 : dim_arg = gfc_evaluate_now (dim_arg, &se->pre);
1906 : 0 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1907 : : dim_arg,
1908 : 0 : build_int_cst (TREE_TYPE (dim_arg), 1));
1909 : 0 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
1910 : 0 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
1911 : : dim_arg, tmp);
1912 : 0 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
1913 : : logical_type_node, cond, tmp);
1914 : 0 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
1915 : : gfc_msg_fault);
1916 : : }
1917 : : }
1918 : :
1919 : : /* Used algorithm; cf. Fortran 2008, C.10. Note, due to the scalarizer,
1920 : : one always has a dim_arg argument.
1921 : :
1922 : : m = this_image() - 1
1923 : : if (corank == 1)
1924 : : {
1925 : : sub(1) = m + lcobound(corank)
1926 : : return;
1927 : : }
1928 : : i = rank
1929 : : min_var = min (rank + corank - 2, rank + dim_arg - 1)
1930 : : for (;;)
1931 : : {
1932 : : extent = gfc_extent(i)
1933 : : ml = m
1934 : : m = m/extent
1935 : : if (i >= min_var)
1936 : : goto exit_label
1937 : : i++
1938 : : }
1939 : : exit_label:
1940 : : sub(dim_arg) = (dim_arg < corank) ? ml - m*extent + lcobound(dim_arg)
1941 : : : m + lcobound(corank)
1942 : : */
1943 : :
1944 : : /* this_image () - 1. */
1945 : 171 : tmp
1946 : 171 : = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1, team);
1947 : 171 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
1948 : : fold_convert (type, tmp), build_int_cst (type, 1));
1949 : 171 : if (corank == 1)
1950 : : {
1951 : : /* sub(1) = m + lcobound(corank). */
1952 : 4 : lbound = gfc_conv_descriptor_lbound_get (desc,
1953 : 4 : build_int_cst (TREE_TYPE (gfc_array_index_type),
1954 : 4 : corank+rank-1));
1955 : 4 : lbound = fold_convert (type, lbound);
1956 : 4 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
1957 : :
1958 : 4 : se->expr = tmp;
1959 : 4 : return;
1960 : : }
1961 : :
1962 : 167 : m = gfc_create_var (type, NULL);
1963 : 167 : ml = gfc_create_var (type, NULL);
1964 : 167 : loop_var = gfc_create_var (integer_type_node, NULL);
1965 : 167 : min_var = gfc_create_var (integer_type_node, NULL);
1966 : :
1967 : : /* m = this_image () - 1. */
1968 : 167 : gfc_add_modify (&se->pre, m, tmp);
1969 : :
1970 : : /* min_var = min (rank + corank-2, rank + dim_arg - 1). */
1971 : 167 : tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1972 : : fold_convert (integer_type_node, dim_arg),
1973 : 167 : build_int_cst (integer_type_node, rank - 1));
1974 : 167 : tmp = fold_build2_loc (input_location, MIN_EXPR, integer_type_node,
1975 : 167 : build_int_cst (integer_type_node, rank + corank - 2),
1976 : : tmp);
1977 : 167 : gfc_add_modify (&se->pre, min_var, tmp);
1978 : :
1979 : : /* i = rank. */
1980 : 167 : tmp = build_int_cst (integer_type_node, rank);
1981 : 167 : gfc_add_modify (&se->pre, loop_var, tmp);
1982 : :
1983 : 167 : exit_label = gfc_build_label_decl (NULL_TREE);
1984 : 167 : TREE_USED (exit_label) = 1;
1985 : :
1986 : : /* Loop body. */
1987 : 167 : gfc_init_block (&loop);
1988 : :
1989 : : /* ml = m. */
1990 : 167 : gfc_add_modify (&loop, ml, m);
1991 : :
1992 : : /* extent = ... */
1993 : 167 : lbound = gfc_conv_descriptor_lbound_get (desc, loop_var);
1994 : 167 : ubound = gfc_conv_descriptor_ubound_get (desc, loop_var);
1995 : 167 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1996 : 167 : extent = fold_convert (type, extent);
1997 : :
1998 : : /* m = m/extent. */
1999 : 167 : gfc_add_modify (&loop, m,
2000 : : fold_build2_loc (input_location, TRUNC_DIV_EXPR, type,
2001 : : m, extent));
2002 : :
2003 : : /* Exit condition: if (i >= min_var) goto exit_label. */
2004 : 167 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, loop_var,
2005 : : min_var);
2006 : 167 : tmp = build1_v (GOTO_EXPR, exit_label);
2007 : 167 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
2008 : : build_empty_stmt (input_location));
2009 : 167 : gfc_add_expr_to_block (&loop, tmp);
2010 : :
2011 : : /* Increment loop variable: i++. */
2012 : 167 : gfc_add_modify (&loop, loop_var,
2013 : : fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
2014 : : loop_var,
2015 : : integer_one_node));
2016 : :
2017 : : /* Making the loop... actually loop! */
2018 : 167 : tmp = gfc_finish_block (&loop);
2019 : 167 : tmp = build1_v (LOOP_EXPR, tmp);
2020 : 167 : gfc_add_expr_to_block (&se->pre, tmp);
2021 : :
2022 : : /* The exit label. */
2023 : 167 : tmp = build1_v (LABEL_EXPR, exit_label);
2024 : 167 : gfc_add_expr_to_block (&se->pre, tmp);
2025 : :
2026 : : /* sub(co_dim) = (co_dim < corank) ? ml - m*extent + lcobound(dim_arg)
2027 : : : m + lcobound(corank) */
2028 : :
2029 : 167 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, dim_arg,
2030 : 167 : build_int_cst (TREE_TYPE (dim_arg), corank));
2031 : :
2032 : 167 : lbound = gfc_conv_descriptor_lbound_get (desc,
2033 : : fold_build2_loc (input_location, PLUS_EXPR,
2034 : : gfc_array_index_type, dim_arg,
2035 : 167 : build_int_cst (TREE_TYPE (dim_arg), rank-1)));
2036 : 167 : lbound = fold_convert (type, lbound);
2037 : :
2038 : 167 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type, ml,
2039 : : fold_build2_loc (input_location, MULT_EXPR, type,
2040 : : m, extent));
2041 : 167 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
2042 : :
2043 : 167 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
2044 : : fold_build2_loc (input_location, PLUS_EXPR, type,
2045 : : m, lbound));
2046 : : }
2047 : :
2048 : :
2049 : : /* Convert a call to image_status. */
2050 : :
2051 : : static void
2052 : 16 : conv_intrinsic_image_status (gfc_se *se, gfc_expr *expr)
2053 : : {
2054 : 16 : unsigned int num_args;
2055 : 16 : tree *args, tmp;
2056 : :
2057 : 16 : num_args = gfc_intrinsic_argument_list_length (expr);
2058 : 16 : args = XALLOCAVEC (tree, num_args);
2059 : 16 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2060 : : /* In args[0] the number of the image the status is desired for has to be
2061 : : given. */
2062 : :
2063 : 16 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
2064 : : {
2065 : 0 : tree arg;
2066 : 0 : arg = gfc_evaluate_now (args[0], &se->pre);
2067 : 0 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2068 : : fold_convert (integer_type_node, arg),
2069 : : integer_one_node);
2070 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
2071 : : tmp, integer_zero_node,
2072 : : build_int_cst (integer_type_node,
2073 : : GFC_STAT_STOPPED_IMAGE));
2074 : : }
2075 : 16 : else if (flag_coarray == GFC_FCOARRAY_LIB)
2076 : 16 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_image_status, 2,
2077 : : args[0],
2078 : : num_args < 2 ? null_pointer_node : args[1]);
2079 : : else
2080 : 0 : gcc_unreachable ();
2081 : :
2082 : 16 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2083 : 16 : }
2084 : :
2085 : : static void
2086 : 19 : conv_intrinsic_team_number (gfc_se *se, gfc_expr *expr)
2087 : : {
2088 : 19 : unsigned int num_args;
2089 : :
2090 : 19 : tree *args, tmp;
2091 : :
2092 : 19 : num_args = gfc_intrinsic_argument_list_length (expr);
2093 : 19 : args = XALLOCAVEC (tree, num_args);
2094 : 19 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2095 : :
2096 : 19 : if (flag_coarray ==
2097 : 18 : GFC_FCOARRAY_SINGLE && expr->value.function.actual->expr)
2098 : 0 : tmp = gfc_evaluate_now (args[0], &se->pre);
2099 : 19 : else if (flag_coarray == GFC_FCOARRAY_SINGLE)
2100 : : {
2101 : : // the value -1 represents that no team has been created yet
2102 : 18 : tmp = build_int_cst (integer_type_node, -1);
2103 : : }
2104 : 1 : else if (flag_coarray == GFC_FCOARRAY_LIB && expr->value.function.actual->expr)
2105 : 0 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
2106 : : args[0]);
2107 : 1 : else if (flag_coarray == GFC_FCOARRAY_LIB)
2108 : 1 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
2109 : : null_pointer_node);
2110 : : else
2111 : 0 : gcc_unreachable ();
2112 : :
2113 : 19 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2114 : 19 : }
2115 : :
2116 : :
2117 : : static void
2118 : 152 : trans_image_index (gfc_se * se, gfc_expr *expr)
2119 : : {
2120 : 152 : tree num_images, cond, coindex, type, lbound, ubound, desc, subdesc, tmp,
2121 : 152 : invalid_bound, team = null_pointer_node, team_number = null_pointer_node;
2122 : 152 : gfc_se argse, subse;
2123 : 152 : int rank, corank, codim;
2124 : :
2125 : 152 : type = gfc_get_int_type (gfc_default_integer_kind);
2126 : 152 : corank = expr->value.function.actual->expr->corank;
2127 : 152 : rank = expr->value.function.actual->expr->rank;
2128 : :
2129 : : /* Obtain the descriptor of the COARRAY. */
2130 : 152 : gfc_init_se (&argse, NULL);
2131 : 152 : argse.want_coarray = 1;
2132 : 152 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2133 : 152 : gfc_add_block_to_block (&se->pre, &argse.pre);
2134 : 152 : gfc_add_block_to_block (&se->post, &argse.post);
2135 : 152 : desc = argse.expr;
2136 : :
2137 : : /* Obtain a handle to the SUB argument. */
2138 : 152 : gfc_init_se (&subse, NULL);
2139 : 152 : gfc_conv_expr_descriptor (&subse, expr->value.function.actual->next->expr);
2140 : 152 : gfc_add_block_to_block (&se->pre, &subse.pre);
2141 : 152 : gfc_add_block_to_block (&se->post, &subse.post);
2142 : 152 : subdesc = build_fold_indirect_ref_loc (input_location,
2143 : : gfc_conv_descriptor_data_get (subse.expr));
2144 : :
2145 : 152 : if (expr->value.function.actual->next->next->expr)
2146 : : {
2147 : 0 : gfc_init_se (&argse, NULL);
2148 : 0 : gfc_conv_expr_descriptor (&argse,
2149 : 0 : expr->value.function.actual->next->next->expr);
2150 : 0 : if (expr->value.function.actual->next->next->expr->ts.type == BT_DERIVED)
2151 : 0 : team = argse.expr;
2152 : : else
2153 : 0 : team_number = gfc_build_addr_expr (
2154 : : NULL_TREE,
2155 : : gfc_trans_force_lval (&argse.pre,
2156 : : fold_convert (integer_type_node, argse.expr)));
2157 : 0 : gfc_add_block_to_block (&se->pre, &argse.pre);
2158 : 0 : gfc_add_block_to_block (&se->post, &argse.post);
2159 : : }
2160 : :
2161 : : /* Fortran 2008 does not require that the values remain in the cobounds,
2162 : : thus we need explicitly check this - and return 0 if they are exceeded. */
2163 : :
2164 : 152 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2165 : 152 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1], NULL);
2166 : 152 : invalid_bound = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2167 : : fold_convert (gfc_array_index_type, tmp),
2168 : : lbound);
2169 : :
2170 : 352 : for (codim = corank + rank - 2; codim >= rank; codim--)
2171 : : {
2172 : 200 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2173 : 200 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2174 : 200 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2175 : 200 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2176 : : fold_convert (gfc_array_index_type, tmp),
2177 : : lbound);
2178 : 200 : invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2179 : : logical_type_node, invalid_bound, cond);
2180 : 200 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2181 : : fold_convert (gfc_array_index_type, tmp),
2182 : : ubound);
2183 : 200 : invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2184 : : logical_type_node, invalid_bound, cond);
2185 : : }
2186 : :
2187 : 152 : invalid_bound = gfc_unlikely (invalid_bound, PRED_FORTRAN_INVALID_BOUND);
2188 : :
2189 : : /* See Fortran 2008, C.10 for the following algorithm. */
2190 : :
2191 : : /* coindex = sub(corank) - lcobound(n). */
2192 : 152 : coindex = fold_convert (gfc_array_index_type,
2193 : : gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1],
2194 : : NULL));
2195 : 152 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2196 : 152 : coindex = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2197 : : fold_convert (gfc_array_index_type, coindex),
2198 : : lbound);
2199 : :
2200 : 352 : for (codim = corank + rank - 2; codim >= rank; codim--)
2201 : : {
2202 : 200 : tree extent, ubound;
2203 : :
2204 : : /* coindex = coindex*extent(codim) + sub(codim) - lcobound(codim). */
2205 : 200 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2206 : 200 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2207 : 200 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2208 : :
2209 : : /* coindex *= extent. */
2210 : 200 : coindex = fold_build2_loc (input_location, MULT_EXPR,
2211 : : gfc_array_index_type, coindex, extent);
2212 : :
2213 : : /* coindex += sub(codim). */
2214 : 200 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2215 : 200 : coindex = fold_build2_loc (input_location, PLUS_EXPR,
2216 : : gfc_array_index_type, coindex,
2217 : : fold_convert (gfc_array_index_type, tmp));
2218 : :
2219 : : /* coindex -= lbound(codim). */
2220 : 200 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2221 : 200 : coindex = fold_build2_loc (input_location, MINUS_EXPR,
2222 : : gfc_array_index_type, coindex, lbound);
2223 : : }
2224 : :
2225 : 152 : coindex = fold_build2_loc (input_location, PLUS_EXPR, type,
2226 : : fold_convert(type, coindex),
2227 : : build_int_cst (type, 1));
2228 : :
2229 : : /* Return 0 if "coindex" exceeds num_images(). */
2230 : :
2231 : 152 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
2232 : 108 : num_images = build_int_cst (type, 1);
2233 : : else
2234 : : {
2235 : 44 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2236 : : team, team_number);
2237 : 44 : num_images = fold_convert (type, tmp);
2238 : : }
2239 : :
2240 : 152 : tmp = gfc_create_var (type, NULL);
2241 : 152 : gfc_add_modify (&se->pre, tmp, coindex);
2242 : :
2243 : 152 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, tmp,
2244 : : num_images);
2245 : 152 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
2246 : : cond,
2247 : : fold_convert (logical_type_node, invalid_bound));
2248 : 152 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
2249 : : build_int_cst (type, 0), tmp);
2250 : 152 : }
2251 : :
2252 : : static void
2253 : 429 : trans_num_images (gfc_se * se, gfc_expr *expr)
2254 : : {
2255 : 429 : tree tmp, team = null_pointer_node, team_number = null_pointer_node;
2256 : 429 : gfc_se argse;
2257 : :
2258 : 429 : if (expr->value.function.actual->expr)
2259 : : {
2260 : 18 : gfc_init_se (&argse, NULL);
2261 : 18 : gfc_conv_expr_val (&argse, expr->value.function.actual->expr);
2262 : 18 : if (expr->value.function.actual->expr->ts.type == BT_DERIVED)
2263 : 6 : team = argse.expr;
2264 : : else
2265 : 12 : team_number = gfc_build_addr_expr (
2266 : : NULL_TREE,
2267 : : gfc_trans_force_lval (&se->pre,
2268 : : fold_convert (integer_type_node, argse.expr)));
2269 : 18 : gfc_add_block_to_block (&se->pre, &argse.pre);
2270 : 18 : gfc_add_block_to_block (&se->post, &argse.post);
2271 : : }
2272 : :
2273 : 429 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2274 : : team, team_number);
2275 : 429 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2276 : 429 : }
2277 : :
2278 : :
2279 : : static void
2280 : 10662 : gfc_conv_intrinsic_rank (gfc_se *se, gfc_expr *expr)
2281 : : {
2282 : 10662 : gfc_se argse;
2283 : :
2284 : 10662 : gfc_init_se (&argse, NULL);
2285 : 10662 : argse.data_not_needed = 1;
2286 : 10662 : argse.descriptor_only = 1;
2287 : :
2288 : 10662 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2289 : 10662 : gfc_add_block_to_block (&se->pre, &argse.pre);
2290 : 10662 : gfc_add_block_to_block (&se->post, &argse.post);
2291 : :
2292 : 10662 : se->expr = gfc_conv_descriptor_rank (argse.expr);
2293 : 10662 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
2294 : : se->expr);
2295 : 10662 : }
2296 : :
2297 : :
2298 : : static void
2299 : 621 : gfc_conv_intrinsic_is_contiguous (gfc_se * se, gfc_expr * expr)
2300 : : {
2301 : 621 : gfc_expr *arg;
2302 : 621 : arg = expr->value.function.actual->expr;
2303 : 621 : gfc_conv_is_contiguous_expr (se, arg);
2304 : 621 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
2305 : 621 : }
2306 : :
2307 : : /* This function does the work for gfc_conv_intrinsic_is_contiguous,
2308 : : plus it can be called directly. */
2309 : :
2310 : : void
2311 : 1964 : gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
2312 : : {
2313 : 1964 : gfc_ss *ss;
2314 : 1964 : gfc_se argse;
2315 : 1964 : tree desc, tmp, stride, extent, cond;
2316 : 1964 : int i;
2317 : 1964 : tree fncall0;
2318 : 1964 : gfc_array_spec *as;
2319 : :
2320 : 1964 : if (arg->ts.type == BT_CLASS)
2321 : 36 : gfc_add_class_array_ref (arg);
2322 : :
2323 : 1964 : ss = gfc_walk_expr (arg);
2324 : 1964 : gcc_assert (ss != gfc_ss_terminator);
2325 : 1964 : gfc_init_se (&argse, NULL);
2326 : 1964 : argse.data_not_needed = 1;
2327 : 1964 : gfc_conv_expr_descriptor (&argse, arg);
2328 : :
2329 : 1964 : as = gfc_get_full_arrayspec_from_expr (arg);
2330 : :
2331 : : /* Create: stride[0] == 1 && stride[1] == extend[0]*stride[0] && ...
2332 : : Note in addition that zero-sized arrays don't count as contiguous. */
2333 : :
2334 : 1964 : if (as && as->type == AS_ASSUMED_RANK)
2335 : : {
2336 : : /* Build the call to is_contiguous0. */
2337 : 243 : argse.want_pointer = 1;
2338 : 243 : gfc_conv_expr_descriptor (&argse, arg);
2339 : 243 : gfc_add_block_to_block (&se->pre, &argse.pre);
2340 : 243 : gfc_add_block_to_block (&se->post, &argse.post);
2341 : 243 : desc = gfc_evaluate_now (argse.expr, &se->pre);
2342 : 243 : fncall0 = build_call_expr_loc (input_location,
2343 : : gfor_fndecl_is_contiguous0, 1, desc);
2344 : 243 : se->expr = fncall0;
2345 : 243 : se->expr = convert (logical_type_node, se->expr);
2346 : : }
2347 : : else
2348 : : {
2349 : 1721 : gfc_add_block_to_block (&se->pre, &argse.pre);
2350 : 1721 : gfc_add_block_to_block (&se->post, &argse.post);
2351 : 1721 : desc = gfc_evaluate_now (argse.expr, &se->pre);
2352 : :
2353 : 1721 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[0]);
2354 : 1721 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2355 : 1721 : stride, build_int_cst (TREE_TYPE (stride), 1));
2356 : :
2357 : 2047 : for (i = 0; i < arg->rank - 1; i++)
2358 : : {
2359 : 326 : tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2360 : 326 : extent = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2361 : 326 : extent = fold_build2_loc (input_location, MINUS_EXPR,
2362 : : gfc_array_index_type, extent, tmp);
2363 : 326 : extent = fold_build2_loc (input_location, PLUS_EXPR,
2364 : : gfc_array_index_type, extent,
2365 : : gfc_index_one_node);
2366 : 326 : tmp = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i]);
2367 : 326 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2368 : : tmp, extent);
2369 : 326 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i+1]);
2370 : 326 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2371 : : stride, tmp);
2372 : 326 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2373 : : boolean_type_node, cond, tmp);
2374 : : }
2375 : 1721 : se->expr = cond;
2376 : : }
2377 : 1964 : }
2378 : :
2379 : :
2380 : : /* Evaluate a single upper or lower bound. */
2381 : : /* TODO: bound intrinsic generates way too much unnecessary code. */
2382 : :
2383 : : static void
2384 : 15966 : gfc_conv_intrinsic_bound (gfc_se * se, gfc_expr * expr, enum gfc_isym_id op)
2385 : : {
2386 : 15966 : gfc_actual_arglist *arg;
2387 : 15966 : gfc_actual_arglist *arg2;
2388 : 15966 : tree desc;
2389 : 15966 : tree type;
2390 : 15966 : tree bound;
2391 : 15966 : tree tmp;
2392 : 15966 : tree cond, cond1;
2393 : 15966 : tree ubound;
2394 : 15966 : tree lbound;
2395 : 15966 : tree size;
2396 : 15966 : gfc_se argse;
2397 : 15966 : gfc_array_spec * as;
2398 : 15966 : bool assumed_rank_lb_one;
2399 : :
2400 : 15966 : arg = expr->value.function.actual;
2401 : 15966 : arg2 = arg->next;
2402 : :
2403 : 15966 : if (se->ss)
2404 : : {
2405 : : /* Create an implicit second parameter from the loop variable. */
2406 : 7827 : gcc_assert (!arg2->expr || op == GFC_ISYM_SHAPE);
2407 : 7827 : gcc_assert (se->loop->dimen == 1);
2408 : 7827 : gcc_assert (se->ss->info->expr == expr);
2409 : 7827 : gfc_advance_se_ss_chain (se);
2410 : 7827 : bound = se->loop->loopvar[0];
2411 : 7827 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2412 : : gfc_array_index_type, bound,
2413 : : se->loop->from[0]);
2414 : : }
2415 : : else
2416 : : {
2417 : : /* use the passed argument. */
2418 : 8139 : gcc_assert (arg2->expr);
2419 : 8139 : gfc_init_se (&argse, NULL);
2420 : 8139 : gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
2421 : 8139 : gfc_add_block_to_block (&se->pre, &argse.pre);
2422 : 8139 : bound = argse.expr;
2423 : : /* Convert from one based to zero based. */
2424 : 8139 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2425 : : gfc_array_index_type, bound,
2426 : : gfc_index_one_node);
2427 : : }
2428 : :
2429 : : /* TODO: don't re-evaluate the descriptor on each iteration. */
2430 : : /* Get a descriptor for the first parameter. */
2431 : 15966 : gfc_init_se (&argse, NULL);
2432 : 15966 : gfc_conv_expr_descriptor (&argse, arg->expr);
2433 : 15966 : gfc_add_block_to_block (&se->pre, &argse.pre);
2434 : 15966 : gfc_add_block_to_block (&se->post, &argse.post);
2435 : :
2436 : 15966 : desc = argse.expr;
2437 : :
2438 : 15966 : as = gfc_get_full_arrayspec_from_expr (arg->expr);
2439 : :
2440 : 15966 : if (INTEGER_CST_P (bound))
2441 : : {
2442 : 8019 : gcc_assert (op != GFC_ISYM_SHAPE);
2443 : 7782 : if (((!as || as->type != AS_ASSUMED_RANK)
2444 : 7159 : && wi::geu_p (wi::to_wide (bound),
2445 : 7159 : GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))))
2446 : 16038 : || wi::gtu_p (wi::to_wide (bound), GFC_MAX_DIMENSIONS))
2447 : 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2448 : : "dimension index",
2449 : : (op == GFC_ISYM_UBOUND) ? "UBOUND" : "LBOUND",
2450 : : &expr->where);
2451 : : }
2452 : :
2453 : 15966 : if (!INTEGER_CST_P (bound) || (as && as->type == AS_ASSUMED_RANK))
2454 : : {
2455 : 8807 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2456 : : {
2457 : 651 : bound = gfc_evaluate_now (bound, &se->pre);
2458 : 651 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2459 : 651 : bound, build_int_cst (TREE_TYPE (bound), 0));
2460 : 651 : if (as && as->type == AS_ASSUMED_RANK)
2461 : 546 : tmp = gfc_conv_descriptor_rank (desc);
2462 : : else
2463 : 105 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))];
2464 : 651 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
2465 : 651 : bound, fold_convert(TREE_TYPE (bound), tmp));
2466 : 651 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2467 : : logical_type_node, cond, tmp);
2468 : 651 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2469 : : gfc_msg_fault);
2470 : : }
2471 : : }
2472 : :
2473 : : /* Take care of the lbound shift for assumed-rank arrays that are
2474 : : nonallocatable and nonpointers. Those have a lbound of 1. */
2475 : 15478 : assumed_rank_lb_one = as && as->type == AS_ASSUMED_RANK
2476 : 10926 : && ((arg->expr->ts.type != BT_CLASS
2477 : 1945 : && !arg->expr->symtree->n.sym->attr.allocatable
2478 : 1602 : && !arg->expr->symtree->n.sym->attr.pointer)
2479 : 896 : || (arg->expr->ts.type == BT_CLASS
2480 : 174 : && !CLASS_DATA (arg->expr)->attr.allocatable
2481 : 138 : && !CLASS_DATA (arg->expr)->attr.class_pointer));
2482 : :
2483 : 15966 : ubound = gfc_conv_descriptor_ubound_get (desc, bound);
2484 : 15966 : lbound = gfc_conv_descriptor_lbound_get (desc, bound);
2485 : 15966 : size = fold_build2_loc (input_location, MINUS_EXPR,
2486 : : gfc_array_index_type, ubound, lbound);
2487 : 15966 : size = fold_build2_loc (input_location, PLUS_EXPR,
2488 : : gfc_array_index_type, size, gfc_index_one_node);
2489 : :
2490 : : /* 13.14.53: Result value for LBOUND
2491 : :
2492 : : Case (i): For an array section or for an array expression other than a
2493 : : whole array or array structure component, LBOUND(ARRAY, DIM)
2494 : : has the value 1. For a whole array or array structure
2495 : : component, LBOUND(ARRAY, DIM) has the value:
2496 : : (a) equal to the lower bound for subscript DIM of ARRAY if
2497 : : dimension DIM of ARRAY does not have extent zero
2498 : : or if ARRAY is an assumed-size array of rank DIM,
2499 : : or (b) 1 otherwise.
2500 : :
2501 : : 13.14.113: Result value for UBOUND
2502 : :
2503 : : Case (i): For an array section or for an array expression other than a
2504 : : whole array or array structure component, UBOUND(ARRAY, DIM)
2505 : : has the value equal to the number of elements in the given
2506 : : dimension; otherwise, it has a value equal to the upper bound
2507 : : for subscript DIM of ARRAY if dimension DIM of ARRAY does
2508 : : not have size zero and has value zero if dimension DIM has
2509 : : size zero. */
2510 : :
2511 : 15966 : if (op == GFC_ISYM_LBOUND && assumed_rank_lb_one)
2512 : 532 : se->expr = gfc_index_one_node;
2513 : 15434 : else if (as)
2514 : : {
2515 : 14946 : if (op == GFC_ISYM_UBOUND)
2516 : : {
2517 : 5320 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2518 : : size, gfc_index_zero_node);
2519 : 10036 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2520 : : gfc_array_index_type, cond,
2521 : : (assumed_rank_lb_one ? size : ubound),
2522 : : gfc_index_zero_node);
2523 : : }
2524 : 9626 : else if (op == GFC_ISYM_LBOUND)
2525 : : {
2526 : 4848 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2527 : : size, gfc_index_zero_node);
2528 : 4848 : if (as->type == AS_ASSUMED_SIZE)
2529 : : {
2530 : 98 : cond1 = fold_build2_loc (input_location, EQ_EXPR,
2531 : : logical_type_node, bound,
2532 : 98 : build_int_cst (TREE_TYPE (bound),
2533 : 98 : arg->expr->rank - 1));
2534 : 98 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2535 : : logical_type_node, cond, cond1);
2536 : : }
2537 : 4848 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2538 : : gfc_array_index_type, cond,
2539 : : lbound, gfc_index_one_node);
2540 : : }
2541 : 4778 : else if (op == GFC_ISYM_SHAPE)
2542 : 4778 : se->expr = fold_build2_loc (input_location, MAX_EXPR,
2543 : : gfc_array_index_type, size,
2544 : : gfc_index_zero_node);
2545 : : else
2546 : 0 : gcc_unreachable ();
2547 : :
2548 : : /* According to F2018 16.9.172, para 5, an assumed rank object,
2549 : : argument associated with and assumed size array, has the ubound
2550 : : of the final dimension set to -1 and UBOUND must return this.
2551 : : Similarly for the SHAPE intrinsic. */
2552 : 14946 : if (op != GFC_ISYM_LBOUND && assumed_rank_lb_one)
2553 : : {
2554 : 793 : tree minus_one = build_int_cst (gfc_array_index_type, -1);
2555 : 793 : tree rank = fold_convert (gfc_array_index_type,
2556 : : gfc_conv_descriptor_rank (desc));
2557 : 793 : rank = fold_build2_loc (input_location, PLUS_EXPR,
2558 : : gfc_array_index_type, rank, minus_one);
2559 : :
2560 : : /* Fix the expression to stop it from becoming even more
2561 : : complicated. */
2562 : 793 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
2563 : :
2564 : : /* Descriptors for assumed-size arrays have ubound = -1
2565 : : in the last dimension. */
2566 : 793 : cond1 = fold_build2_loc (input_location, EQ_EXPR,
2567 : : logical_type_node, ubound, minus_one);
2568 : 793 : cond = fold_build2_loc (input_location, EQ_EXPR,
2569 : : logical_type_node, bound, rank);
2570 : 793 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2571 : : logical_type_node, cond, cond1);
2572 : 793 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2573 : : gfc_array_index_type, cond,
2574 : : minus_one, se->expr);
2575 : : }
2576 : : }
2577 : : else /* as is null; this is an old-fashioned 1-based array. */
2578 : : {
2579 : 488 : if (op != GFC_ISYM_LBOUND)
2580 : : {
2581 : 386 : se->expr = fold_build2_loc (input_location, MAX_EXPR,
2582 : : gfc_array_index_type, size,
2583 : : gfc_index_zero_node);
2584 : : }
2585 : : else
2586 : 102 : se->expr = gfc_index_one_node;
2587 : : }
2588 : :
2589 : :
2590 : 15966 : type = gfc_typenode_for_spec (&expr->ts);
2591 : 15966 : se->expr = convert (type, se->expr);
2592 : 15966 : }
2593 : :
2594 : :
2595 : : static void
2596 : 577 : conv_intrinsic_cobound (gfc_se * se, gfc_expr * expr)
2597 : : {
2598 : 577 : gfc_actual_arglist *arg;
2599 : 577 : gfc_actual_arglist *arg2;
2600 : 577 : gfc_se argse;
2601 : 577 : tree bound, resbound, resbound2, desc, cond, tmp;
2602 : 577 : tree type;
2603 : 577 : int corank;
2604 : :
2605 : 577 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_LCOBOUND
2606 : : || expr->value.function.isym->id == GFC_ISYM_UCOBOUND
2607 : : || expr->value.function.isym->id == GFC_ISYM_THIS_IMAGE);
2608 : :
2609 : 577 : arg = expr->value.function.actual;
2610 : 577 : arg2 = arg->next;
2611 : :
2612 : 577 : gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
2613 : 577 : corank = arg->expr->corank;
2614 : :
2615 : 577 : gfc_init_se (&argse, NULL);
2616 : 577 : argse.want_coarray = 1;
2617 : :
2618 : 577 : gfc_conv_expr_descriptor (&argse, arg->expr);
2619 : 577 : gfc_add_block_to_block (&se->pre, &argse.pre);
2620 : 577 : gfc_add_block_to_block (&se->post, &argse.post);
2621 : 577 : desc = argse.expr;
2622 : :
2623 : 577 : if (se->ss)
2624 : : {
2625 : : /* Create an implicit second parameter from the loop variable. */
2626 : 184 : gcc_assert (!arg2->expr);
2627 : 184 : gcc_assert (corank > 0);
2628 : 184 : gcc_assert (se->loop->dimen == 1);
2629 : 184 : gcc_assert (se->ss->info->expr == expr);
2630 : :
2631 : 184 : bound = se->loop->loopvar[0];
2632 : 368 : bound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2633 : 184 : bound, gfc_rank_cst[arg->expr->rank]);
2634 : 184 : gfc_advance_se_ss_chain (se);
2635 : : }
2636 : : else
2637 : : {
2638 : : /* use the passed argument. */
2639 : 393 : gcc_assert (arg2->expr);
2640 : 393 : gfc_init_se (&argse, NULL);
2641 : 393 : gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
2642 : 393 : gfc_add_block_to_block (&se->pre, &argse.pre);
2643 : 393 : bound = argse.expr;
2644 : :
2645 : 393 : if (INTEGER_CST_P (bound))
2646 : : {
2647 : 299 : if (wi::ltu_p (wi::to_wide (bound), 1)
2648 : 598 : || wi::gtu_p (wi::to_wide (bound),
2649 : 299 : GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
2650 : 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2651 : 0 : "dimension index", expr->value.function.isym->name,
2652 : : &expr->where);
2653 : : }
2654 : 94 : else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2655 : : {
2656 : 36 : bound = gfc_evaluate_now (bound, &se->pre);
2657 : 36 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2658 : 36 : bound, build_int_cst (TREE_TYPE (bound), 1));
2659 : 36 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
2660 : 36 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2661 : : bound, tmp);
2662 : 36 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2663 : : logical_type_node, cond, tmp);
2664 : 36 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2665 : : gfc_msg_fault);
2666 : : }
2667 : :
2668 : :
2669 : : /* Subtract 1 to get to zero based and add dimensions. */
2670 : 393 : switch (arg->expr->rank)
2671 : : {
2672 : 51 : case 0:
2673 : 51 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2674 : : gfc_array_index_type, bound,
2675 : : gfc_index_one_node);
2676 : : case 1:
2677 : : break;
2678 : 36 : default:
2679 : 36 : bound = fold_build2_loc (input_location, PLUS_EXPR,
2680 : : gfc_array_index_type, bound,
2681 : 36 : gfc_rank_cst[arg->expr->rank - 1]);
2682 : : }
2683 : : }
2684 : :
2685 : 577 : resbound = gfc_conv_descriptor_lbound_get (desc, bound);
2686 : :
2687 : : /* Handle UCOBOUND with special handling of the last codimension. */
2688 : 577 : if (expr->value.function.isym->id == GFC_ISYM_UCOBOUND)
2689 : : {
2690 : : /* Last codimension: For -fcoarray=single just return
2691 : : the lcobound - otherwise add
2692 : : ceiling (real (num_images ()) / real (size)) - 1
2693 : : = (num_images () + size - 1) / size - 1
2694 : : = (num_images - 1) / size(),
2695 : : where size is the product of the extent of all but the last
2696 : : codimension. */
2697 : :
2698 : 191 : if (flag_coarray != GFC_FCOARRAY_SINGLE && corank > 1)
2699 : : {
2700 : 30 : tree cosize;
2701 : :
2702 : 30 : cosize = gfc_conv_descriptor_cosize (desc, arg->expr->rank, corank);
2703 : 30 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
2704 : : 2, null_pointer_node, null_pointer_node);
2705 : 30 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2706 : : gfc_array_index_type,
2707 : : fold_convert (gfc_array_index_type, tmp),
2708 : : build_int_cst (gfc_array_index_type, 1));
2709 : 30 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
2710 : : gfc_array_index_type, tmp,
2711 : : fold_convert (gfc_array_index_type, cosize));
2712 : 30 : resbound = fold_build2_loc (input_location, PLUS_EXPR,
2713 : : gfc_array_index_type, resbound, tmp);
2714 : 30 : }
2715 : 161 : else if (flag_coarray != GFC_FCOARRAY_SINGLE)
2716 : : {
2717 : : /* ubound = lbound + num_images() - 1. */
2718 : 21 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
2719 : : 2, null_pointer_node, null_pointer_node);
2720 : 21 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2721 : : gfc_array_index_type,
2722 : : fold_convert (gfc_array_index_type, tmp),
2723 : : build_int_cst (gfc_array_index_type, 1));
2724 : 21 : resbound = fold_build2_loc (input_location, PLUS_EXPR,
2725 : : gfc_array_index_type, resbound, tmp);
2726 : : }
2727 : :
2728 : 191 : if (corank > 1)
2729 : : {
2730 : 137 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2731 : : bound,
2732 : 137 : build_int_cst (TREE_TYPE (bound),
2733 : 137 : arg->expr->rank + corank - 1));
2734 : :
2735 : 137 : resbound2 = gfc_conv_descriptor_ubound_get (desc, bound);
2736 : 137 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2737 : : gfc_array_index_type, cond,
2738 : : resbound, resbound2);
2739 : : }
2740 : : else
2741 : 54 : se->expr = resbound;
2742 : : }
2743 : : else
2744 : 386 : se->expr = resbound;
2745 : :
2746 : 577 : type = gfc_typenode_for_spec (&expr->ts);
2747 : 577 : se->expr = convert (type, se->expr);
2748 : 577 : }
2749 : :
2750 : :
2751 : : static void
2752 : 1907 : conv_intrinsic_stride (gfc_se * se, gfc_expr * expr)
2753 : : {
2754 : 1907 : gfc_actual_arglist *array_arg;
2755 : 1907 : gfc_actual_arglist *dim_arg;
2756 : 1907 : gfc_se argse;
2757 : 1907 : tree desc, tmp;
2758 : :
2759 : 1907 : array_arg = expr->value.function.actual;
2760 : 1907 : dim_arg = array_arg->next;
2761 : :
2762 : 1907 : gcc_assert (array_arg->expr->expr_type == EXPR_VARIABLE);
2763 : :
2764 : 1907 : gfc_init_se (&argse, NULL);
2765 : 1907 : gfc_conv_expr_descriptor (&argse, array_arg->expr);
2766 : 1907 : gfc_add_block_to_block (&se->pre, &argse.pre);
2767 : 1907 : gfc_add_block_to_block (&se->post, &argse.post);
2768 : 1907 : desc = argse.expr;
2769 : :
2770 : 1907 : gcc_assert (dim_arg->expr);
2771 : 1907 : gfc_init_se (&argse, NULL);
2772 : 1907 : gfc_conv_expr_type (&argse, dim_arg->expr, gfc_array_index_type);
2773 : 1907 : gfc_add_block_to_block (&se->pre, &argse.pre);
2774 : 1907 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2775 : : argse.expr, gfc_index_one_node);
2776 : 1907 : se->expr = gfc_conv_descriptor_stride_get (desc, tmp);
2777 : 1907 : }
2778 : :
2779 : : static void
2780 : 7723 : gfc_conv_intrinsic_abs (gfc_se * se, gfc_expr * expr)
2781 : : {
2782 : 7723 : tree arg, cabs;
2783 : :
2784 : 7723 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
2785 : :
2786 : 7723 : switch (expr->value.function.actual->expr->ts.type)
2787 : : {
2788 : 6747 : case BT_INTEGER:
2789 : 6747 : case BT_REAL:
2790 : 6747 : se->expr = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (arg),
2791 : : arg);
2792 : 6747 : break;
2793 : :
2794 : 976 : case BT_COMPLEX:
2795 : 976 : cabs = gfc_builtin_decl_for_float_kind (BUILT_IN_CABS, expr->ts.kind);
2796 : 976 : se->expr = build_call_expr_loc (input_location, cabs, 1, arg);
2797 : 976 : break;
2798 : :
2799 : 0 : default:
2800 : 0 : gcc_unreachable ();
2801 : : }
2802 : 7723 : }
2803 : :
2804 : :
2805 : : /* Create a complex value from one or two real components. */
2806 : :
2807 : : static void
2808 : 491 : gfc_conv_intrinsic_cmplx (gfc_se * se, gfc_expr * expr, int both)
2809 : : {
2810 : 491 : tree real;
2811 : 491 : tree imag;
2812 : 491 : tree type;
2813 : 491 : tree *args;
2814 : 491 : unsigned int num_args;
2815 : :
2816 : 491 : num_args = gfc_intrinsic_argument_list_length (expr);
2817 : 491 : args = XALLOCAVEC (tree, num_args);
2818 : :
2819 : 491 : type = gfc_typenode_for_spec (&expr->ts);
2820 : 491 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2821 : 491 : real = convert (TREE_TYPE (type), args[0]);
2822 : 491 : if (both)
2823 : 447 : imag = convert (TREE_TYPE (type), args[1]);
2824 : 44 : else if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE)
2825 : : {
2826 : 30 : imag = fold_build1_loc (input_location, IMAGPART_EXPR,
2827 : 30 : TREE_TYPE (TREE_TYPE (args[0])), args[0]);
2828 : 30 : imag = convert (TREE_TYPE (type), imag);
2829 : : }
2830 : : else
2831 : 14 : imag = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
2832 : :
2833 : 491 : se->expr = fold_build2_loc (input_location, COMPLEX_EXPR, type, real, imag);
2834 : 491 : }
2835 : :
2836 : :
2837 : : /* Remainder function MOD(A, P) = A - INT(A / P) * P
2838 : : MODULO(A, P) = A - FLOOR (A / P) * P
2839 : :
2840 : : The obvious algorithms above are numerically instable for large
2841 : : arguments, hence these intrinsics are instead implemented via calls
2842 : : to the fmod family of functions. It is the responsibility of the
2843 : : user to ensure that the second argument is non-zero. */
2844 : :
2845 : : static void
2846 : 3199 : gfc_conv_intrinsic_mod (gfc_se * se, gfc_expr * expr, int modulo)
2847 : : {
2848 : 3199 : tree type;
2849 : 3199 : tree tmp;
2850 : 3199 : tree test;
2851 : 3199 : tree test2;
2852 : 3199 : tree fmod;
2853 : 3199 : tree zero;
2854 : 3199 : tree args[2];
2855 : :
2856 : 3199 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
2857 : :
2858 : 3199 : switch (expr->ts.type)
2859 : : {
2860 : 3046 : case BT_INTEGER:
2861 : : /* Integer case is easy, we've got a builtin op. */
2862 : 3046 : type = TREE_TYPE (args[0]);
2863 : :
2864 : 3046 : if (modulo)
2865 : 403 : se->expr = fold_build2_loc (input_location, FLOOR_MOD_EXPR, type,
2866 : : args[0], args[1]);
2867 : : else
2868 : 2643 : se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2869 : : args[0], args[1]);
2870 : : break;
2871 : :
2872 : 30 : case BT_UNSIGNED:
2873 : : /* Even easier, we only need one. */
2874 : 30 : type = TREE_TYPE (args[0]);
2875 : 30 : se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2876 : : args[0], args[1]);
2877 : 30 : break;
2878 : :
2879 : 123 : case BT_REAL:
2880 : 123 : fmod = NULL_TREE;
2881 : : /* Check if we have a builtin fmod. */
2882 : 123 : fmod = gfc_builtin_decl_for_float_kind (BUILT_IN_FMOD, expr->ts.kind);
2883 : :
2884 : : /* The builtin should always be available. */
2885 : 123 : gcc_assert (fmod != NULL_TREE);
2886 : :
2887 : 123 : tmp = build_addr (fmod);
2888 : 123 : se->expr = build_call_array_loc (input_location,
2889 : 123 : TREE_TYPE (TREE_TYPE (fmod)),
2890 : : tmp, 2, args);
2891 : 123 : if (modulo == 0)
2892 : 123 : return;
2893 : :
2894 : 25 : type = TREE_TYPE (args[0]);
2895 : :
2896 : 25 : args[0] = gfc_evaluate_now (args[0], &se->pre);
2897 : 25 : args[1] = gfc_evaluate_now (args[1], &se->pre);
2898 : :
2899 : : /* Definition:
2900 : : modulo = arg - floor (arg/arg2) * arg2
2901 : :
2902 : : In order to calculate the result accurately, we use the fmod
2903 : : function as follows.
2904 : :
2905 : : res = fmod (arg, arg2);
2906 : : if (res)
2907 : : {
2908 : : if ((arg < 0) xor (arg2 < 0))
2909 : : res += arg2;
2910 : : }
2911 : : else
2912 : : res = copysign (0., arg2);
2913 : :
2914 : : => As two nested ternary exprs:
2915 : :
2916 : : res = res ? (((arg < 0) xor (arg2 < 0)) ? res + arg2 : res)
2917 : : : copysign (0., arg2);
2918 : :
2919 : : */
2920 : :
2921 : 25 : zero = gfc_build_const (type, integer_zero_node);
2922 : 25 : tmp = gfc_evaluate_now (se->expr, &se->pre);
2923 : 25 : if (!flag_signed_zeros)
2924 : : {
2925 : 1 : test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2926 : : args[0], zero);
2927 : 1 : test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2928 : : args[1], zero);
2929 : 1 : test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
2930 : : logical_type_node, test, test2);
2931 : 1 : test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2932 : : tmp, zero);
2933 : 1 : test = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2934 : : logical_type_node, test, test2);
2935 : 1 : test = gfc_evaluate_now (test, &se->pre);
2936 : 1 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
2937 : : fold_build2_loc (input_location,
2938 : : PLUS_EXPR,
2939 : : type, tmp, args[1]),
2940 : : tmp);
2941 : : }
2942 : : else
2943 : : {
2944 : 24 : tree expr1, copysign, cscall;
2945 : 24 : copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN,
2946 : : expr->ts.kind);
2947 : 24 : test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2948 : : args[0], zero);
2949 : 24 : test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2950 : : args[1], zero);
2951 : 24 : test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
2952 : : logical_type_node, test, test2);
2953 : 24 : expr1 = fold_build3_loc (input_location, COND_EXPR, type, test2,
2954 : : fold_build2_loc (input_location,
2955 : : PLUS_EXPR,
2956 : : type, tmp, args[1]),
2957 : : tmp);
2958 : 24 : test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2959 : : tmp, zero);
2960 : 24 : cscall = build_call_expr_loc (input_location, copysign, 2, zero,
2961 : : args[1]);
2962 : 24 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
2963 : : expr1, cscall);
2964 : : }
2965 : : return;
2966 : :
2967 : 0 : default:
2968 : 0 : gcc_unreachable ();
2969 : : }
2970 : : }
2971 : :
2972 : : /* DSHIFTL(I,J,S) = (I << S) | (J >> (BITSIZE(J) - S))
2973 : : DSHIFTR(I,J,S) = (I << (BITSIZE(I) - S)) | (J >> S)
2974 : : where the right shifts are logical (i.e. 0's are shifted in).
2975 : : Because SHIFT_EXPR's want shifts strictly smaller than the integral
2976 : : type width, we have to special-case both S == 0 and S == BITSIZE(J):
2977 : : DSHIFTL(I,J,0) = I
2978 : : DSHIFTL(I,J,BITSIZE) = J
2979 : : DSHIFTR(I,J,0) = J
2980 : : DSHIFTR(I,J,BITSIZE) = I. */
2981 : :
2982 : : static void
2983 : 132 : gfc_conv_intrinsic_dshift (gfc_se * se, gfc_expr * expr, bool dshiftl)
2984 : : {
2985 : 132 : tree type, utype, stype, arg1, arg2, shift, res, left, right;
2986 : 132 : tree args[3], cond, tmp;
2987 : 132 : int bitsize;
2988 : :
2989 : 132 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
2990 : :
2991 : 132 : gcc_assert (TREE_TYPE (args[0]) == TREE_TYPE (args[1]));
2992 : 132 : type = TREE_TYPE (args[0]);
2993 : 132 : bitsize = TYPE_PRECISION (type);
2994 : 132 : utype = unsigned_type_for (type);
2995 : 132 : stype = TREE_TYPE (args[2]);
2996 : :
2997 : 132 : arg1 = gfc_evaluate_now (args[0], &se->pre);
2998 : 132 : arg2 = gfc_evaluate_now (args[1], &se->pre);
2999 : 132 : shift = gfc_evaluate_now (args[2], &se->pre);
3000 : :
3001 : : /* The generic case. */
3002 : 132 : tmp = fold_build2_loc (input_location, MINUS_EXPR, stype,
3003 : 132 : build_int_cst (stype, bitsize), shift);
3004 : 198 : left = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3005 : : arg1, dshiftl ? shift : tmp);
3006 : :
3007 : 198 : right = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
3008 : : fold_convert (utype, arg2), dshiftl ? tmp : shift);
3009 : 132 : right = fold_convert (type, right);
3010 : :
3011 : 132 : res = fold_build2_loc (input_location, BIT_IOR_EXPR, type, left, right);
3012 : :
3013 : : /* Special cases. */
3014 : 132 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3015 : : build_int_cst (stype, 0));
3016 : 198 : res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3017 : : dshiftl ? arg1 : arg2, res);
3018 : :
3019 : 132 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3020 : 132 : build_int_cst (stype, bitsize));
3021 : 198 : res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3022 : : dshiftl ? arg2 : arg1, res);
3023 : :
3024 : 132 : se->expr = res;
3025 : 132 : }
3026 : :
3027 : :
3028 : : /* Positive difference DIM (x, y) = ((x - y) < 0) ? 0 : x - y. */
3029 : :
3030 : : static void
3031 : 96 : gfc_conv_intrinsic_dim (gfc_se * se, gfc_expr * expr)
3032 : : {
3033 : 96 : tree val;
3034 : 96 : tree tmp;
3035 : 96 : tree type;
3036 : 96 : tree zero;
3037 : 96 : tree args[2];
3038 : :
3039 : 96 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3040 : 96 : type = TREE_TYPE (args[0]);
3041 : :
3042 : 96 : val = fold_build2_loc (input_location, MINUS_EXPR, type, args[0], args[1]);
3043 : 96 : val = gfc_evaluate_now (val, &se->pre);
3044 : :
3045 : 96 : zero = gfc_build_const (type, integer_zero_node);
3046 : 96 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node, val, zero);
3047 : 96 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, zero, val);
3048 : 96 : }
3049 : :
3050 : :
3051 : : /* SIGN(A, B) is absolute value of A times sign of B.
3052 : : The real value versions use library functions to ensure the correct
3053 : : handling of negative zero. Integer case implemented as:
3054 : : SIGN(A, B) = { tmp = (A ^ B) >> C; (A + tmp) ^ tmp }
3055 : : */
3056 : :
3057 : : static void
3058 : 424 : gfc_conv_intrinsic_sign (gfc_se * se, gfc_expr * expr)
3059 : : {
3060 : 424 : tree tmp;
3061 : 424 : tree type;
3062 : 424 : tree args[2];
3063 : :
3064 : 424 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3065 : 424 : if (expr->ts.type == BT_REAL)
3066 : : {
3067 : 162 : tree abs;
3068 : :
3069 : 162 : tmp = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
3070 : 162 : abs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
3071 : :
3072 : : /* We explicitly have to ignore the minus sign. We do so by using
3073 : : result = (arg1 == 0) ? abs(arg0) : copysign(arg0, arg1). */
3074 : 162 : if (!flag_sign_zero
3075 : 198 : && MODE_HAS_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (args[1]))))
3076 : : {
3077 : 12 : tree cond, zero;
3078 : 12 : zero = build_real_from_int_cst (TREE_TYPE (args[1]), integer_zero_node);
3079 : 12 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3080 : : args[1], zero);
3081 : 24 : se->expr = fold_build3_loc (input_location, COND_EXPR,
3082 : 12 : TREE_TYPE (args[0]), cond,
3083 : : build_call_expr_loc (input_location, abs, 1,
3084 : : args[0]),
3085 : : build_call_expr_loc (input_location, tmp, 2,
3086 : : args[0], args[1]));
3087 : : }
3088 : : else
3089 : 150 : se->expr = build_call_expr_loc (input_location, tmp, 2,
3090 : : args[0], args[1]);
3091 : 162 : return;
3092 : : }
3093 : :
3094 : : /* Having excluded floating point types, we know we are now dealing
3095 : : with signed integer types. */
3096 : 262 : type = TREE_TYPE (args[0]);
3097 : :
3098 : : /* Args[0] is used multiple times below. */
3099 : 262 : args[0] = gfc_evaluate_now (args[0], &se->pre);
3100 : :
3101 : : /* Construct (A ^ B) >> 31, which generates a bit mask of all zeros if
3102 : : the signs of A and B are the same, and of all ones if they differ. */
3103 : 262 : tmp = fold_build2_loc (input_location, BIT_XOR_EXPR, type, args[0], args[1]);
3104 : 262 : tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, tmp,
3105 : 262 : build_int_cst (type, TYPE_PRECISION (type) - 1));
3106 : 262 : tmp = gfc_evaluate_now (tmp, &se->pre);
3107 : :
3108 : : /* Construct (A + tmp) ^ tmp, which is A if tmp is zero, and -A if tmp]
3109 : : is all ones (i.e. -1). */
3110 : 262 : se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR, type,
3111 : : fold_build2_loc (input_location, PLUS_EXPR,
3112 : : type, args[0], tmp), tmp);
3113 : : }
3114 : :
3115 : :
3116 : : /* Test for the presence of an optional argument. */
3117 : :
3118 : : static void
3119 : 5052 : gfc_conv_intrinsic_present (gfc_se * se, gfc_expr * expr)
3120 : : {
3121 : 5052 : gfc_expr *arg;
3122 : :
3123 : 5052 : arg = expr->value.function.actual->expr;
3124 : 5052 : gcc_assert (arg->expr_type == EXPR_VARIABLE);
3125 : 5052 : se->expr = gfc_conv_expr_present (arg->symtree->n.sym);
3126 : 5052 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
3127 : 5052 : }
3128 : :
3129 : :
3130 : : /* Calculate the double precision product of two single precision values. */
3131 : :
3132 : : static void
3133 : 13 : gfc_conv_intrinsic_dprod (gfc_se * se, gfc_expr * expr)
3134 : : {
3135 : 13 : tree type;
3136 : 13 : tree args[2];
3137 : :
3138 : 13 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3139 : :
3140 : : /* Convert the args to double precision before multiplying. */
3141 : 13 : type = gfc_typenode_for_spec (&expr->ts);
3142 : 13 : args[0] = convert (type, args[0]);
3143 : 13 : args[1] = convert (type, args[1]);
3144 : 13 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, args[0],
3145 : : args[1]);
3146 : 13 : }
3147 : :
3148 : :
3149 : : /* Return a length one character string containing an ascii character. */
3150 : :
3151 : : static void
3152 : 2020 : gfc_conv_intrinsic_char (gfc_se * se, gfc_expr * expr)
3153 : : {
3154 : 2020 : tree arg[2];
3155 : 2020 : tree var;
3156 : 2020 : tree type;
3157 : 2020 : unsigned int num_args;
3158 : :
3159 : 2020 : num_args = gfc_intrinsic_argument_list_length (expr);
3160 : 2020 : gfc_conv_intrinsic_function_args (se, expr, arg, num_args);
3161 : :
3162 : 2020 : type = gfc_get_char_type (expr->ts.kind);
3163 : 2020 : var = gfc_create_var (type, "char");
3164 : :
3165 : 2020 : arg[0] = fold_build1_loc (input_location, NOP_EXPR, type, arg[0]);
3166 : 2020 : gfc_add_modify (&se->pre, var, arg[0]);
3167 : 2020 : se->expr = gfc_build_addr_expr (build_pointer_type (type), var);
3168 : 2020 : se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3169 : 2020 : }
3170 : :
3171 : :
3172 : : static void
3173 : 0 : gfc_conv_intrinsic_ctime (gfc_se * se, gfc_expr * expr)
3174 : : {
3175 : 0 : tree var;
3176 : 0 : tree len;
3177 : 0 : tree tmp;
3178 : 0 : tree cond;
3179 : 0 : tree fndecl;
3180 : 0 : tree *args;
3181 : 0 : unsigned int num_args;
3182 : :
3183 : 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3184 : 0 : args = XALLOCAVEC (tree, num_args);
3185 : :
3186 : 0 : var = gfc_create_var (pchar_type_node, "pstr");
3187 : 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3188 : :
3189 : 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3190 : 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3191 : 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3192 : :
3193 : 0 : fndecl = build_addr (gfor_fndecl_ctime);
3194 : 0 : tmp = build_call_array_loc (input_location,
3195 : 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_ctime)),
3196 : : fndecl, num_args, args);
3197 : 0 : gfc_add_expr_to_block (&se->pre, tmp);
3198 : :
3199 : : /* Free the temporary afterwards, if necessary. */
3200 : 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3201 : 0 : len, build_int_cst (TREE_TYPE (len), 0));
3202 : 0 : tmp = gfc_call_free (var);
3203 : 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3204 : 0 : gfc_add_expr_to_block (&se->post, tmp);
3205 : :
3206 : 0 : se->expr = var;
3207 : 0 : se->string_length = len;
3208 : 0 : }
3209 : :
3210 : :
3211 : : static void
3212 : 0 : gfc_conv_intrinsic_fdate (gfc_se * se, gfc_expr * expr)
3213 : : {
3214 : 0 : tree var;
3215 : 0 : tree len;
3216 : 0 : tree tmp;
3217 : 0 : tree cond;
3218 : 0 : tree fndecl;
3219 : 0 : tree *args;
3220 : 0 : unsigned int num_args;
3221 : :
3222 : 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3223 : 0 : args = XALLOCAVEC (tree, num_args);
3224 : :
3225 : 0 : var = gfc_create_var (pchar_type_node, "pstr");
3226 : 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3227 : :
3228 : 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3229 : 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3230 : 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3231 : :
3232 : 0 : fndecl = build_addr (gfor_fndecl_fdate);
3233 : 0 : tmp = build_call_array_loc (input_location,
3234 : 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_fdate)),
3235 : : fndecl, num_args, args);
3236 : 0 : gfc_add_expr_to_block (&se->pre, tmp);
3237 : :
3238 : : /* Free the temporary afterwards, if necessary. */
3239 : 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3240 : 0 : len, build_int_cst (TREE_TYPE (len), 0));
3241 : 0 : tmp = gfc_call_free (var);
3242 : 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3243 : 0 : gfc_add_expr_to_block (&se->post, tmp);
3244 : :
3245 : 0 : se->expr = var;
3246 : 0 : se->string_length = len;
3247 : 0 : }
3248 : :
3249 : :
3250 : : /* Generate a direct call to free() for the FREE subroutine. */
3251 : :
3252 : : static tree
3253 : 10 : conv_intrinsic_free (gfc_code *code)
3254 : : {
3255 : 10 : stmtblock_t block;
3256 : 10 : gfc_se argse;
3257 : 10 : tree arg, call;
3258 : :
3259 : 10 : gfc_init_se (&argse, NULL);
3260 : 10 : gfc_conv_expr (&argse, code->ext.actual->expr);
3261 : 10 : arg = fold_convert (ptr_type_node, argse.expr);
3262 : :
3263 : 10 : gfc_init_block (&block);
3264 : 10 : call = build_call_expr_loc (input_location,
3265 : : builtin_decl_explicit (BUILT_IN_FREE), 1, arg);
3266 : 10 : gfc_add_expr_to_block (&block, call);
3267 : 10 : return gfc_finish_block (&block);
3268 : : }
3269 : :
3270 : :
3271 : : /* Call the RANDOM_INIT library subroutine with a hidden argument for
3272 : : handling seeding on coarray images. */
3273 : :
3274 : : static tree
3275 : 90 : conv_intrinsic_random_init (gfc_code *code)
3276 : : {
3277 : 90 : stmtblock_t block;
3278 : 90 : gfc_se se;
3279 : 90 : tree arg1, arg2, tmp;
3280 : : /* On none coarray == lib compiles use LOGICAL(4) else regular LOGICAL. */
3281 : 90 : tree used_bool_type_node = flag_coarray == GFC_FCOARRAY_LIB
3282 : 90 : ? logical_type_node
3283 : 90 : : gfc_get_logical_type (4);
3284 : :
3285 : : /* Make the function call. */
3286 : 90 : gfc_init_block (&block);
3287 : 90 : gfc_init_se (&se, NULL);
3288 : :
3289 : : /* Convert REPEATABLE to the desired LOGICAL entity. */
3290 : 90 : gfc_conv_expr (&se, code->ext.actual->expr);
3291 : 90 : gfc_add_block_to_block (&block, &se.pre);
3292 : 90 : arg1 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3293 : 90 : gfc_add_block_to_block (&block, &se.post);
3294 : :
3295 : : /* Convert IMAGE_DISTINCT to the desired LOGICAL entity. */
3296 : 90 : gfc_conv_expr (&se, code->ext.actual->next->expr);
3297 : 90 : gfc_add_block_to_block (&block, &se.pre);
3298 : 90 : arg2 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3299 : 90 : gfc_add_block_to_block (&block, &se.post);
3300 : :
3301 : 90 : if (flag_coarray == GFC_FCOARRAY_LIB)
3302 : : {
3303 : 0 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
3304 : : 2, arg1, arg2);
3305 : : }
3306 : : else
3307 : : {
3308 : : /* The ABI for libgfortran needs to be maintained, so a hidden
3309 : : argument must be include if code is compiled with -fcoarray=single
3310 : : or without the option. Set to 0. */
3311 : 90 : tree arg3 = build_int_cst (gfc_get_int_type (4), 0);
3312 : 90 : tmp = build_call_expr_loc (input_location, gfor_fndecl_random_init,
3313 : : 3, arg1, arg2, arg3);
3314 : : }
3315 : :
3316 : 90 : gfc_add_expr_to_block (&block, tmp);
3317 : :
3318 : 90 : return gfc_finish_block (&block);
3319 : : }
3320 : :
3321 : :
3322 : : /* Call the SYSTEM_CLOCK library functions, handling the type and kind
3323 : : conversions. */
3324 : :
3325 : : static tree
3326 : 194 : conv_intrinsic_system_clock (gfc_code *code)
3327 : : {
3328 : 194 : stmtblock_t block;
3329 : 194 : gfc_se count_se, count_rate_se, count_max_se;
3330 : 194 : tree arg1 = NULL_TREE, arg2 = NULL_TREE, arg3 = NULL_TREE;
3331 : 194 : tree tmp;
3332 : 194 : int least;
3333 : :
3334 : 194 : gfc_expr *count = code->ext.actual->expr;
3335 : 194 : gfc_expr *count_rate = code->ext.actual->next->expr;
3336 : 194 : gfc_expr *count_max = code->ext.actual->next->next->expr;
3337 : :
3338 : : /* Evaluate our arguments. */
3339 : 194 : if (count)
3340 : : {
3341 : 194 : gfc_init_se (&count_se, NULL);
3342 : 194 : gfc_conv_expr (&count_se, count);
3343 : : }
3344 : :
3345 : 194 : if (count_rate)
3346 : : {
3347 : 181 : gfc_init_se (&count_rate_se, NULL);
3348 : 181 : gfc_conv_expr (&count_rate_se, count_rate);
3349 : : }
3350 : :
3351 : 194 : if (count_max)
3352 : : {
3353 : 180 : gfc_init_se (&count_max_se, NULL);
3354 : 180 : gfc_conv_expr (&count_max_se, count_max);
3355 : : }
3356 : :
3357 : : /* Find the smallest kind found of the arguments. */
3358 : 194 : least = 16;
3359 : 194 : least = (count && count->ts.kind < least) ? count->ts.kind : least;
3360 : 194 : least = (count_rate && count_rate->ts.kind < least) ? count_rate->ts.kind
3361 : : : least;
3362 : 194 : least = (count_max && count_max->ts.kind < least) ? count_max->ts.kind
3363 : : : least;
3364 : :
3365 : : /* Prepare temporary variables. */
3366 : :
3367 : 194 : if (count)
3368 : : {
3369 : 194 : if (least >= 8)
3370 : 18 : arg1 = gfc_create_var (gfc_get_int_type (8), "count");
3371 : 176 : else if (least == 4)
3372 : 152 : arg1 = gfc_create_var (gfc_get_int_type (4), "count");
3373 : 24 : else if (count->ts.kind == 1)
3374 : 12 : arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[0].pedantic_min_int,
3375 : : count->ts.kind);
3376 : : else
3377 : 12 : arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[1].pedantic_min_int,
3378 : : count->ts.kind);
3379 : : }
3380 : :
3381 : 194 : if (count_rate)
3382 : : {
3383 : 181 : if (least >= 8)
3384 : 18 : arg2 = gfc_create_var (gfc_get_int_type (8), "count_rate");
3385 : 163 : else if (least == 4)
3386 : 139 : arg2 = gfc_create_var (gfc_get_int_type (4), "count_rate");
3387 : : else
3388 : 24 : arg2 = integer_zero_node;
3389 : : }
3390 : :
3391 : 194 : if (count_max)
3392 : : {
3393 : 180 : if (least >= 8)
3394 : 18 : arg3 = gfc_create_var (gfc_get_int_type (8), "count_max");
3395 : 162 : else if (least == 4)
3396 : 138 : arg3 = gfc_create_var (gfc_get_int_type (4), "count_max");
3397 : : else
3398 : 24 : arg3 = integer_zero_node;
3399 : : }
3400 : :
3401 : : /* Make the function call. */
3402 : 194 : gfc_init_block (&block);
3403 : :
3404 : 194 : if (least <= 2)
3405 : : {
3406 : 24 : if (least == 1)
3407 : : {
3408 : 12 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3409 : : : null_pointer_node;
3410 : 12 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3411 : : : null_pointer_node;
3412 : 12 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3413 : : : null_pointer_node;
3414 : : }
3415 : :
3416 : 24 : if (least == 2)
3417 : : {
3418 : 12 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3419 : : : null_pointer_node;
3420 : 12 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3421 : : : null_pointer_node;
3422 : 12 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3423 : : : null_pointer_node;
3424 : : }
3425 : : }
3426 : : else
3427 : : {
3428 : 170 : if (least == 4)
3429 : : {
3430 : 581 : tmp = build_call_expr_loc (input_location,
3431 : : gfor_fndecl_system_clock4, 3,
3432 : 152 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3433 : : : null_pointer_node,
3434 : 139 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3435 : : : null_pointer_node,
3436 : 138 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3437 : : : null_pointer_node);
3438 : 152 : gfc_add_expr_to_block (&block, tmp);
3439 : : }
3440 : : /* Handle kind>=8, 10, or 16 arguments */
3441 : 170 : if (least >= 8)
3442 : : {
3443 : 72 : tmp = build_call_expr_loc (input_location,
3444 : : gfor_fndecl_system_clock8, 3,
3445 : 18 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3446 : : : null_pointer_node,
3447 : 18 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3448 : : : null_pointer_node,
3449 : 18 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3450 : : : null_pointer_node);
3451 : 18 : gfc_add_expr_to_block (&block, tmp);
3452 : : }
3453 : : }
3454 : :
3455 : : /* And store values back if needed. */
3456 : 194 : if (arg1 && arg1 != count_se.expr)
3457 : 194 : gfc_add_modify (&block, count_se.expr,
3458 : 194 : fold_convert (TREE_TYPE (count_se.expr), arg1));
3459 : 194 : if (arg2 && arg2 != count_rate_se.expr)
3460 : 181 : gfc_add_modify (&block, count_rate_se.expr,
3461 : 181 : fold_convert (TREE_TYPE (count_rate_se.expr), arg2));
3462 : 194 : if (arg3 && arg3 != count_max_se.expr)
3463 : 180 : gfc_add_modify (&block, count_max_se.expr,
3464 : 180 : fold_convert (TREE_TYPE (count_max_se.expr), arg3));
3465 : :
3466 : 194 : return gfc_finish_block (&block);
3467 : : }
3468 : :
3469 : : static tree
3470 : 102 : conv_intrinsic_split (gfc_code *code)
3471 : : {
3472 : 102 : stmtblock_t block, post_block;
3473 : 102 : gfc_se se;
3474 : 102 : gfc_expr *string_expr, *set_expr, *pos_expr, *back_expr;
3475 : 102 : tree string, string_len;
3476 : 102 : tree set, set_len;
3477 : 102 : tree pos, pos_for_call;
3478 : 102 : tree back;
3479 : 102 : tree fndecl, call;
3480 : :
3481 : 102 : string_expr = code->ext.actual->expr;
3482 : 102 : set_expr = code->ext.actual->next->expr;
3483 : 102 : pos_expr = code->ext.actual->next->next->expr;
3484 : 102 : back_expr = code->ext.actual->next->next->next->expr;
3485 : :
3486 : 102 : gfc_start_block (&block);
3487 : 102 : gfc_init_block (&post_block);
3488 : :
3489 : 102 : gfc_init_se (&se, NULL);
3490 : 102 : gfc_conv_expr (&se, string_expr);
3491 : 102 : gfc_conv_string_parameter (&se);
3492 : 102 : gfc_add_block_to_block (&block, &se.pre);
3493 : 102 : gfc_add_block_to_block (&post_block, &se.post);
3494 : 102 : string = se.expr;
3495 : 102 : string_len = se.string_length;
3496 : :
3497 : 102 : gfc_init_se (&se, NULL);
3498 : 102 : gfc_conv_expr (&se, set_expr);
3499 : 102 : gfc_conv_string_parameter (&se);
3500 : 102 : gfc_add_block_to_block (&block, &se.pre);
3501 : 102 : gfc_add_block_to_block (&post_block, &se.post);
3502 : 102 : set = se.expr;
3503 : 102 : set_len = se.string_length;
3504 : :
3505 : 102 : gfc_init_se (&se, NULL);
3506 : 102 : gfc_conv_expr (&se, pos_expr);
3507 : 102 : gfc_add_block_to_block (&block, &se.pre);
3508 : 102 : gfc_add_block_to_block (&post_block, &se.post);
3509 : 102 : pos = se.expr;
3510 : 102 : pos_for_call = fold_convert (gfc_charlen_type_node, pos);
3511 : :
3512 : 102 : if (back_expr)
3513 : : {
3514 : 48 : gfc_init_se (&se, NULL);
3515 : 48 : gfc_conv_expr (&se, back_expr);
3516 : 48 : gfc_add_block_to_block (&block, &se.pre);
3517 : 48 : gfc_add_block_to_block (&post_block, &se.post);
3518 : 48 : back = se.expr;
3519 : : }
3520 : : else
3521 : 54 : back = logical_false_node;
3522 : :
3523 : 102 : if (string_expr->ts.kind == 1)
3524 : 66 : fndecl = gfor_fndecl_string_split;
3525 : 36 : else if (string_expr->ts.kind == 4)
3526 : 36 : fndecl = gfor_fndecl_string_split_char4;
3527 : : else
3528 : 0 : gcc_unreachable ();
3529 : :
3530 : 102 : call = build_call_expr_loc (input_location, fndecl, 6, string_len, string,
3531 : : set_len, set, pos_for_call, back);
3532 : 102 : gfc_add_modify (&block, pos, fold_convert (TREE_TYPE (pos), call));
3533 : :
3534 : 102 : gfc_add_block_to_block (&block, &post_block);
3535 : 102 : return gfc_finish_block (&block);
3536 : : }
3537 : :
3538 : : /* Return a character string containing the tty name. */
3539 : :
3540 : : static void
3541 : 0 : gfc_conv_intrinsic_ttynam (gfc_se * se, gfc_expr * expr)
3542 : : {
3543 : 0 : tree var;
3544 : 0 : tree len;
3545 : 0 : tree tmp;
3546 : 0 : tree cond;
3547 : 0 : tree fndecl;
3548 : 0 : tree *args;
3549 : 0 : unsigned int num_args;
3550 : :
3551 : 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3552 : 0 : args = XALLOCAVEC (tree, num_args);
3553 : :
3554 : 0 : var = gfc_create_var (pchar_type_node, "pstr");
3555 : 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3556 : :
3557 : 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3558 : 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3559 : 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3560 : :
3561 : 0 : fndecl = build_addr (gfor_fndecl_ttynam);
3562 : 0 : tmp = build_call_array_loc (input_location,
3563 : 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_ttynam)),
3564 : : fndecl, num_args, args);
3565 : 0 : gfc_add_expr_to_block (&se->pre, tmp);
3566 : :
3567 : : /* Free the temporary afterwards, if necessary. */
3568 : 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3569 : 0 : len, build_int_cst (TREE_TYPE (len), 0));
3570 : 0 : tmp = gfc_call_free (var);
3571 : 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3572 : 0 : gfc_add_expr_to_block (&se->post, tmp);
3573 : :
3574 : 0 : se->expr = var;
3575 : 0 : se->string_length = len;
3576 : 0 : }
3577 : :
3578 : :
3579 : : /* Get the minimum/maximum value of all the parameters.
3580 : : minmax (a1, a2, a3, ...)
3581 : : {
3582 : : mvar = a1;
3583 : : mvar = COMP (mvar, a2)
3584 : : mvar = COMP (mvar, a3)
3585 : : ...
3586 : : return mvar;
3587 : : }
3588 : : Where COMP is MIN/MAX_EXPR for integral types or when we don't
3589 : : care about NaNs, or IFN_FMIN/MAX when the target has support for
3590 : : fast NaN-honouring min/max. When neither holds expand a sequence
3591 : : of explicit comparisons. */
3592 : :
3593 : : /* TODO: Mismatching types can occur when specific names are used.
3594 : : These should be handled during resolution. */
3595 : : static void
3596 : 1308 : gfc_conv_intrinsic_minmax (gfc_se * se, gfc_expr * expr, enum tree_code op)
3597 : : {
3598 : 1308 : tree tmp;
3599 : 1308 : tree mvar;
3600 : 1308 : tree val;
3601 : 1308 : tree *args;
3602 : 1308 : tree type;
3603 : 1308 : tree argtype;
3604 : 1308 : gfc_actual_arglist *argexpr;
3605 : 1308 : unsigned int i, nargs;
3606 : :
3607 : 1308 : nargs = gfc_intrinsic_argument_list_length (expr);
3608 : 1308 : args = XALLOCAVEC (tree, nargs);
3609 : :
3610 : 1308 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
3611 : 1308 : type = gfc_typenode_for_spec (&expr->ts);
3612 : :
3613 : : /* Only evaluate the argument once. */
3614 : 1308 : if (!VAR_P (args[0]) && !TREE_CONSTANT (args[0]))
3615 : 309 : args[0] = gfc_evaluate_now (args[0], &se->pre);
3616 : :
3617 : : /* Determine suitable type of temporary, as a GNU extension allows
3618 : : different argument kinds. */
3619 : 1308 : argtype = TREE_TYPE (args[0]);
3620 : 1308 : argexpr = expr->value.function.actual;
3621 : 2835 : for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3622 : : {
3623 : 1527 : tree tmptype = TREE_TYPE (args[i]);
3624 : 1527 : if (TYPE_PRECISION (tmptype) > TYPE_PRECISION (argtype))
3625 : 1 : argtype = tmptype;
3626 : : }
3627 : 1308 : mvar = gfc_create_var (argtype, "M");
3628 : 1308 : gfc_add_modify (&se->pre, mvar, convert (argtype, args[0]));
3629 : :
3630 : 1308 : argexpr = expr->value.function.actual;
3631 : 2835 : for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3632 : : {
3633 : 1527 : tree cond = NULL_TREE;
3634 : 1527 : val = args[i];
3635 : :
3636 : : /* Handle absent optional arguments by ignoring the comparison. */
3637 : 1527 : if (argexpr->expr->expr_type == EXPR_VARIABLE
3638 : 908 : && argexpr->expr->symtree->n.sym->attr.optional
3639 : 45 : && INDIRECT_REF_P (val))
3640 : : {
3641 : 84 : cond = fold_build2_loc (input_location,
3642 : : NE_EXPR, logical_type_node,
3643 : 42 : TREE_OPERAND (val, 0),
3644 : 42 : build_int_cst (TREE_TYPE (TREE_OPERAND (val, 0)), 0));
3645 : : }
3646 : 1485 : else if (!VAR_P (val) && !TREE_CONSTANT (val))
3647 : : /* Only evaluate the argument once. */
3648 : 579 : val = gfc_evaluate_now (val, &se->pre);
3649 : :
3650 : 1527 : tree calc;
3651 : : /* For floating point types, the question is what MAX(a, NaN) or
3652 : : MIN(a, NaN) should return (where "a" is a normal number).
3653 : : There are valid use case for returning either one, but the
3654 : : Fortran standard doesn't specify which one should be chosen.
3655 : : Also, there is no consensus among other tested compilers. In
3656 : : short, it's a mess. So lets just do whatever is fastest. */
3657 : 1527 : tree_code code = op == GT_EXPR ? MAX_EXPR : MIN_EXPR;
3658 : 1527 : calc = fold_build2_loc (input_location, code, argtype,
3659 : : convert (argtype, val), mvar);
3660 : 1527 : tmp = build2_v (MODIFY_EXPR, mvar, calc);
3661 : :
3662 : 1527 : if (cond != NULL_TREE)
3663 : 42 : tmp = build3_v (COND_EXPR, cond, tmp,
3664 : : build_empty_stmt (input_location));
3665 : 1527 : gfc_add_expr_to_block (&se->pre, tmp);
3666 : : }
3667 : 1308 : se->expr = convert (type, mvar);
3668 : 1308 : }
3669 : :
3670 : :
3671 : : /* Generate library calls for MIN and MAX intrinsics for character
3672 : : variables. */
3673 : : static void
3674 : 282 : gfc_conv_intrinsic_minmax_char (gfc_se * se, gfc_expr * expr, int op)
3675 : : {
3676 : 282 : tree *args;
3677 : 282 : tree var, len, fndecl, tmp, cond, function;
3678 : 282 : unsigned int nargs;
3679 : :
3680 : 282 : nargs = gfc_intrinsic_argument_list_length (expr);
3681 : 282 : args = XALLOCAVEC (tree, nargs + 4);
3682 : 282 : gfc_conv_intrinsic_function_args (se, expr, &args[4], nargs);
3683 : :
3684 : : /* Create the result variables. */
3685 : 282 : len = gfc_create_var (gfc_charlen_type_node, "len");
3686 : 282 : args[0] = gfc_build_addr_expr (NULL_TREE, len);
3687 : 282 : var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
3688 : 282 : args[1] = gfc_build_addr_expr (ppvoid_type_node, var);
3689 : 282 : args[2] = build_int_cst (integer_type_node, op);
3690 : 282 : args[3] = build_int_cst (integer_type_node, nargs / 2);
3691 : :
3692 : 282 : if (expr->ts.kind == 1)
3693 : 210 : function = gfor_fndecl_string_minmax;
3694 : 72 : else if (expr->ts.kind == 4)
3695 : 72 : function = gfor_fndecl_string_minmax_char4;
3696 : : else
3697 : 0 : gcc_unreachable ();
3698 : :
3699 : : /* Make the function call. */
3700 : 282 : fndecl = build_addr (function);
3701 : 282 : tmp = build_call_array_loc (input_location,
3702 : 282 : TREE_TYPE (TREE_TYPE (function)), fndecl,
3703 : : nargs + 4, args);
3704 : 282 : gfc_add_expr_to_block (&se->pre, tmp);
3705 : :
3706 : : /* Free the temporary afterwards, if necessary. */
3707 : 282 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3708 : 282 : len, build_int_cst (TREE_TYPE (len), 0));
3709 : 282 : tmp = gfc_call_free (var);
3710 : 282 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3711 : 282 : gfc_add_expr_to_block (&se->post, tmp);
3712 : :
3713 : 282 : se->expr = var;
3714 : 282 : se->string_length = len;
3715 : 282 : }
3716 : :
3717 : :
3718 : : /* Create a symbol node for this intrinsic. The symbol from the frontend
3719 : : has the generic name. */
3720 : :
3721 : : static gfc_symbol *
3722 : 11345 : gfc_get_symbol_for_expr (gfc_expr * expr, bool ignore_optional)
3723 : : {
3724 : 11345 : gfc_symbol *sym;
3725 : :
3726 : : /* TODO: Add symbols for intrinsic function to the global namespace. */
3727 : 11345 : gcc_assert (strlen (expr->value.function.name) <= GFC_MAX_SYMBOL_LEN - 5);
3728 : 11345 : sym = gfc_new_symbol (expr->value.function.name, NULL);
3729 : :
3730 : 11345 : sym->ts = expr->ts;
3731 : 11345 : if (sym->ts.type == BT_CHARACTER)
3732 : 1781 : sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3733 : 11345 : sym->attr.external = 1;
3734 : 11345 : sym->attr.function = 1;
3735 : 11345 : sym->attr.always_explicit = 1;
3736 : 11345 : sym->attr.proc = PROC_INTRINSIC;
3737 : 11345 : sym->attr.flavor = FL_PROCEDURE;
3738 : 11345 : sym->result = sym;
3739 : 11345 : if (expr->rank > 0)
3740 : : {
3741 : 9973 : sym->attr.dimension = 1;
3742 : 9973 : sym->as = gfc_get_array_spec ();
3743 : 9973 : sym->as->type = AS_ASSUMED_SHAPE;
3744 : 9973 : sym->as->rank = expr->rank;
3745 : : }
3746 : :
3747 : 11345 : gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3748 : : ignore_optional ? expr->value.function.actual
3749 : : : NULL);
3750 : :
3751 : 11345 : return sym;
3752 : : }
3753 : :
3754 : : /* Remove empty actual arguments. */
3755 : :
3756 : : static void
3757 : 8277 : remove_empty_actual_arguments (gfc_actual_arglist **ap)
3758 : : {
3759 : 44456 : while (*ap)
3760 : : {
3761 : 36179 : if ((*ap)->expr == NULL)
3762 : : {
3763 : 11076 : gfc_actual_arglist *r = *ap;
3764 : 11076 : *ap = r->next;
3765 : 11076 : r->next = NULL;
3766 : 11076 : gfc_free_actual_arglist (r);
3767 : : }
3768 : : else
3769 : 25103 : ap = &((*ap)->next);
3770 : : }
3771 : 8277 : }
3772 : :
3773 : : #define MAX_SPEC_ARG 12
3774 : :
3775 : : /* Make up an fn spec that's right for intrinsic functions that we
3776 : : want to call. */
3777 : :
3778 : : static char *
3779 : 1939 : intrinsic_fnspec (gfc_expr *expr)
3780 : : {
3781 : 1939 : static char fnspec_buf[MAX_SPEC_ARG*2+1];
3782 : 1939 : char *fp;
3783 : 1939 : int i;
3784 : 1939 : int num_char_args;
3785 : :
3786 : : #define ADD_CHAR(c) do { *fp++ = c; *fp++ = ' '; } while(0)
3787 : :
3788 : : /* Set the fndecl. */
3789 : 1939 : fp = fnspec_buf;
3790 : : /* Function return value. FIXME: Check if the second letter could
3791 : : be something other than a space, for further optimization. */
3792 : 1939 : ADD_CHAR ('.');
3793 : 1939 : if (expr->rank == 0)
3794 : : {
3795 : 238 : if (expr->ts.type == BT_CHARACTER)
3796 : : {
3797 : 84 : ADD_CHAR ('w'); /* Address of character. */
3798 : 84 : ADD_CHAR ('.'); /* Length of character. */
3799 : : }
3800 : : }
3801 : : else
3802 : 1701 : ADD_CHAR ('w'); /* Return value is a descriptor. */
3803 : :
3804 : 1939 : num_char_args = 0;
3805 : 10224 : for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
3806 : : {
3807 : 8285 : if (a->expr == NULL)
3808 : 2565 : continue;
3809 : :
3810 : 5720 : if (a->name && strcmp (a->name,"%VAL") == 0)
3811 : 1300 : ADD_CHAR ('.');
3812 : : else
3813 : : {
3814 : 4420 : if (a->expr->rank > 0)
3815 : 2575 : ADD_CHAR ('r');
3816 : : else
3817 : 1845 : ADD_CHAR ('R');
3818 : : }
3819 : 5720 : num_char_args += a->expr->ts.type == BT_CHARACTER;
3820 : 5720 : gcc_assert (fp - fnspec_buf + num_char_args <= MAX_SPEC_ARG*2);
3821 : : }
3822 : :
3823 : 2743 : for (i = 0; i < num_char_args; i++)
3824 : 804 : ADD_CHAR ('.');
3825 : :
3826 : 1939 : *fp = '\0';
3827 : 1939 : return fnspec_buf;
3828 : : }
3829 : :
3830 : : #undef MAX_SPEC_ARG
3831 : : #undef ADD_CHAR
3832 : :
3833 : : /* Generate the right symbol for the specific intrinsic function and
3834 : : modify the expr accordingly. This assumes that absent optional
3835 : : arguments should be removed. */
3836 : :
3837 : : gfc_symbol *
3838 : 8277 : specific_intrinsic_symbol (gfc_expr *expr)
3839 : : {
3840 : 8277 : gfc_symbol *sym;
3841 : :
3842 : 8277 : sym = gfc_find_intrinsic_symbol (expr);
3843 : 8277 : if (sym == NULL)
3844 : : {
3845 : 1939 : sym = gfc_get_intrinsic_function_symbol (expr);
3846 : 1939 : sym->ts = expr->ts;
3847 : 1939 : if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl)
3848 : 240 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
3849 : :
3850 : 1939 : gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3851 : : expr->value.function.actual, true);
3852 : 1939 : sym->backend_decl
3853 : 1939 : = gfc_get_extern_function_decl (sym, expr->value.function.actual,
3854 : 1939 : intrinsic_fnspec (expr));
3855 : : }
3856 : :
3857 : 8277 : remove_empty_actual_arguments (&(expr->value.function.actual));
3858 : :
3859 : 8277 : return sym;
3860 : : }
3861 : :
3862 : : /* Generate a call to an external intrinsic function. FIXME: So far,
3863 : : this only works for functions which are called with well-defined
3864 : : types; CSHIFT and friends will come later. */
3865 : :
3866 : : static void
3867 : 13536 : gfc_conv_intrinsic_funcall (gfc_se * se, gfc_expr * expr)
3868 : : {
3869 : 13536 : gfc_symbol *sym;
3870 : 13536 : vec<tree, va_gc> *append_args;
3871 : 13536 : bool specific_symbol;
3872 : :
3873 : 13536 : gcc_assert (!se->ss || se->ss->info->expr == expr);
3874 : :
3875 : 13536 : if (se->ss)
3876 : 11602 : gcc_assert (expr->rank > 0);
3877 : : else
3878 : 1934 : gcc_assert (expr->rank == 0);
3879 : :
3880 : 13536 : switch (expr->value.function.isym->id)
3881 : : {
3882 : : case GFC_ISYM_ANY:
3883 : : case GFC_ISYM_ALL:
3884 : : case GFC_ISYM_FINDLOC:
3885 : : case GFC_ISYM_MAXLOC:
3886 : : case GFC_ISYM_MINLOC:
3887 : : case GFC_ISYM_MAXVAL:
3888 : : case GFC_ISYM_MINVAL:
3889 : : case GFC_ISYM_NORM2:
3890 : : case GFC_ISYM_PRODUCT:
3891 : : case GFC_ISYM_SUM:
3892 : : specific_symbol = true;
3893 : : break;
3894 : 5259 : default:
3895 : 5259 : specific_symbol = false;
3896 : : }
3897 : :
3898 : 13536 : if (specific_symbol)
3899 : : {
3900 : : /* Need to copy here because specific_intrinsic_symbol modifies
3901 : : expr to omit the absent optional arguments. */
3902 : 8277 : expr = gfc_copy_expr (expr);
3903 : 8277 : sym = specific_intrinsic_symbol (expr);
3904 : : }
3905 : : else
3906 : 5259 : sym = gfc_get_symbol_for_expr (expr, se->ignore_optional);
3907 : :
3908 : : /* Calls to libgfortran_matmul need to be appended special arguments,
3909 : : to be able to call the BLAS ?gemm functions if required and possible. */
3910 : 13536 : append_args = NULL;
3911 : 13536 : if (expr->value.function.isym->id == GFC_ISYM_MATMUL
3912 : 863 : && !expr->external_blas
3913 : 825 : && sym->ts.type != BT_LOGICAL)
3914 : : {
3915 : 809 : tree cint = gfc_get_int_type (gfc_c_int_kind);
3916 : :
3917 : 809 : if (flag_external_blas
3918 : 0 : && (sym->ts.type == BT_REAL || sym->ts.type == BT_COMPLEX)
3919 : 0 : && (sym->ts.kind == 4 || sym->ts.kind == 8))
3920 : : {
3921 : 0 : tree gemm_fndecl;
3922 : :
3923 : 0 : if (sym->ts.type == BT_REAL)
3924 : : {
3925 : 0 : if (sym->ts.kind == 4)
3926 : 0 : gemm_fndecl = gfor_fndecl_sgemm;
3927 : : else
3928 : 0 : gemm_fndecl = gfor_fndecl_dgemm;
3929 : : }
3930 : : else
3931 : : {
3932 : 0 : if (sym->ts.kind == 4)
3933 : 0 : gemm_fndecl = gfor_fndecl_cgemm;
3934 : : else
3935 : 0 : gemm_fndecl = gfor_fndecl_zgemm;
3936 : : }
3937 : :
3938 : 0 : vec_alloc (append_args, 3);
3939 : 0 : append_args->quick_push (build_int_cst (cint, 1));
3940 : 0 : append_args->quick_push (build_int_cst (cint,
3941 : 0 : flag_blas_matmul_limit));
3942 : 0 : append_args->quick_push (gfc_build_addr_expr (NULL_TREE,
3943 : : gemm_fndecl));
3944 : 0 : }
3945 : : else
3946 : : {
3947 : 809 : vec_alloc (append_args, 3);
3948 : 809 : append_args->quick_push (build_int_cst (cint, 0));
3949 : 809 : append_args->quick_push (build_int_cst (cint, 0));
3950 : 809 : append_args->quick_push (null_pointer_node);
3951 : : }
3952 : : }
3953 : : /* Non-character scalar reduce returns a pointer to a result of size set by
3954 : : the element size of 'array'. Setting 'sym' allocatable ensures that the
3955 : : result is deallocated at the appropriate time. */
3956 : 12727 : else if (expr->value.function.isym->id == GFC_ISYM_REDUCE
3957 : 102 : && expr->rank == 0 && expr->ts.type != BT_CHARACTER)
3958 : 96 : sym->attr.allocatable = 1;
3959 : :
3960 : :
3961 : 13536 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
3962 : : append_args);
3963 : :
3964 : 13536 : if (specific_symbol)
3965 : 8277 : gfc_free_expr (expr);
3966 : : else
3967 : 5259 : gfc_free_symbol (sym);
3968 : 13536 : }
3969 : :
3970 : : /* ANY and ALL intrinsics. ANY->op == NE_EXPR, ALL->op == EQ_EXPR.
3971 : : Implemented as
3972 : : any(a)
3973 : : {
3974 : : forall (i=...)
3975 : : if (a[i] != 0)
3976 : : return 1
3977 : : end forall
3978 : : return 0
3979 : : }
3980 : : all(a)
3981 : : {
3982 : : forall (i=...)
3983 : : if (a[i] == 0)
3984 : : return 0
3985 : : end forall
3986 : : return 1
3987 : : }
3988 : : */
3989 : : static void
3990 : 37168 : gfc_conv_intrinsic_anyall (gfc_se * se, gfc_expr * expr, enum tree_code op)
3991 : : {
3992 : 37168 : tree resvar;
3993 : 37168 : stmtblock_t block;
3994 : 37168 : stmtblock_t body;
3995 : 37168 : tree type;
3996 : 37168 : tree tmp;
3997 : 37168 : tree found;
3998 : 37168 : gfc_loopinfo loop;
3999 : 37168 : gfc_actual_arglist *actual;
4000 : 37168 : gfc_ss *arrayss;
4001 : 37168 : gfc_se arrayse;
4002 : 37168 : tree exit_label;
4003 : :
4004 : 37168 : if (se->ss)
4005 : : {
4006 : 0 : gfc_conv_intrinsic_funcall (se, expr);
4007 : 0 : return;
4008 : : }
4009 : :
4010 : 37168 : actual = expr->value.function.actual;
4011 : 37168 : type = gfc_typenode_for_spec (&expr->ts);
4012 : : /* Initialize the result. */
4013 : 37168 : resvar = gfc_create_var (type, "test");
4014 : 37168 : if (op == EQ_EXPR)
4015 : 419 : tmp = convert (type, boolean_true_node);
4016 : : else
4017 : 36749 : tmp = convert (type, boolean_false_node);
4018 : 37168 : gfc_add_modify (&se->pre, resvar, tmp);
4019 : :
4020 : : /* Walk the arguments. */
4021 : 37168 : arrayss = gfc_walk_expr (actual->expr);
4022 : 37168 : gcc_assert (arrayss != gfc_ss_terminator);
4023 : :
4024 : : /* Initialize the scalarizer. */
4025 : 37168 : gfc_init_loopinfo (&loop);
4026 : 37168 : exit_label = gfc_build_label_decl (NULL_TREE);
4027 : 37168 : TREE_USED (exit_label) = 1;
4028 : 37168 : gfc_add_ss_to_loop (&loop, arrayss);
4029 : :
4030 : : /* Initialize the loop. */
4031 : 37168 : gfc_conv_ss_startstride (&loop);
4032 : 37168 : gfc_conv_loop_setup (&loop, &expr->where);
4033 : :
4034 : 37168 : gfc_mark_ss_chain_used (arrayss, 1);
4035 : : /* Generate the loop body. */
4036 : 37168 : gfc_start_scalarized_body (&loop, &body);
4037 : :
4038 : : /* If the condition matches then set the return value. */
4039 : 37168 : gfc_start_block (&block);
4040 : 37168 : if (op == EQ_EXPR)
4041 : 419 : tmp = convert (type, boolean_false_node);
4042 : : else
4043 : 36749 : tmp = convert (type, boolean_true_node);
4044 : 37168 : gfc_add_modify (&block, resvar, tmp);
4045 : :
4046 : : /* And break out of the loop. */
4047 : 37168 : tmp = build1_v (GOTO_EXPR, exit_label);
4048 : 37168 : gfc_add_expr_to_block (&block, tmp);
4049 : :
4050 : 37168 : found = gfc_finish_block (&block);
4051 : :
4052 : : /* Check this element. */
4053 : 37168 : gfc_init_se (&arrayse, NULL);
4054 : 37168 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
4055 : 37168 : arrayse.ss = arrayss;
4056 : 37168 : gfc_conv_expr_val (&arrayse, actual->expr);
4057 : :
4058 : 37168 : gfc_add_block_to_block (&body, &arrayse.pre);
4059 : 37168 : tmp = fold_build2_loc (input_location, op, logical_type_node, arrayse.expr,
4060 : 37168 : build_int_cst (TREE_TYPE (arrayse.expr), 0));
4061 : 37168 : tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
4062 : 37168 : gfc_add_expr_to_block (&body, tmp);
4063 : 37168 : gfc_add_block_to_block (&body, &arrayse.post);
4064 : :
4065 : 37168 : gfc_trans_scalarizing_loops (&loop, &body);
4066 : :
4067 : : /* Add the exit label. */
4068 : 37168 : tmp = build1_v (LABEL_EXPR, exit_label);
4069 : 37168 : gfc_add_expr_to_block (&loop.pre, tmp);
4070 : :
4071 : 37168 : gfc_add_block_to_block (&se->pre, &loop.pre);
4072 : 37168 : gfc_add_block_to_block (&se->pre, &loop.post);
4073 : 37168 : gfc_cleanup_loop (&loop);
4074 : :
4075 : 37168 : se->expr = resvar;
4076 : : }
4077 : :
4078 : :
4079 : : /* Generate the constant 180 / pi, which is used in the conversion
4080 : : of acosd(), asind(), atand(), atan2d(). */
4081 : :
4082 : : static tree
4083 : 336 : rad2deg (int kind)
4084 : : {
4085 : 336 : tree retval;
4086 : 336 : mpfr_t pi, t0;
4087 : :
4088 : 336 : gfc_set_model_kind (kind);
4089 : 336 : mpfr_init (pi);
4090 : 336 : mpfr_init (t0);
4091 : 336 : mpfr_set_si (t0, 180, GFC_RND_MODE);
4092 : 336 : mpfr_const_pi (pi, GFC_RND_MODE);
4093 : 336 : mpfr_div (t0, t0, pi, GFC_RND_MODE);
4094 : 336 : retval = gfc_conv_mpfr_to_tree (t0, kind, 0);
4095 : 336 : mpfr_clear (t0);
4096 : 336 : mpfr_clear (pi);
4097 : 336 : return retval;
4098 : : }
4099 : :
4100 : :
4101 : : static gfc_intrinsic_map_t *
4102 : 546 : gfc_lookup_intrinsic (gfc_isym_id id)
4103 : : {
4104 : 546 : gfc_intrinsic_map_t *m = gfc_intrinsic_map;
4105 : 11154 : for (; m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
4106 : 11154 : if (id == m->id)
4107 : : break;
4108 : 546 : gcc_assert (id == m->id);
4109 : 546 : return m;
4110 : : }
4111 : :
4112 : :
4113 : : /* ACOSD(x) is translated into ACOS(x) * 180 / pi.
4114 : : ASIND(x) is translated into ASIN(x) * 180 / pi.
4115 : : ATAND(x) is translated into ATAN(x) * 180 / pi. */
4116 : :
4117 : : static void
4118 : 216 : gfc_conv_intrinsic_atrigd (gfc_se * se, gfc_expr * expr, gfc_isym_id id)
4119 : : {
4120 : 216 : tree arg;
4121 : 216 : tree atrigd;
4122 : 216 : tree type;
4123 : 216 : gfc_intrinsic_map_t *m;
4124 : :
4125 : 216 : type = gfc_typenode_for_spec (&expr->ts);
4126 : :
4127 : 216 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4128 : :
4129 : 216 : switch (id)
4130 : : {
4131 : 72 : case GFC_ISYM_ACOSD:
4132 : 72 : m = gfc_lookup_intrinsic (GFC_ISYM_ACOS);
4133 : 72 : break;
4134 : 72 : case GFC_ISYM_ASIND:
4135 : 72 : m = gfc_lookup_intrinsic (GFC_ISYM_ASIN);
4136 : 72 : break;
4137 : 72 : case GFC_ISYM_ATAND:
4138 : 72 : m = gfc_lookup_intrinsic (GFC_ISYM_ATAN);
4139 : 72 : break;
4140 : 0 : default:
4141 : 0 : gcc_unreachable ();
4142 : : }
4143 : 216 : atrigd = gfc_get_intrinsic_lib_fndecl (m, expr);
4144 : 216 : atrigd = build_call_expr_loc (input_location, atrigd, 1, arg);
4145 : :
4146 : 216 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atrigd,
4147 : : fold_convert (type, rad2deg (expr->ts.kind)));
4148 : 216 : }
4149 : :
4150 : :
4151 : : /* COTAN(X) is translated into -TAN(X+PI/2) for REAL argument and
4152 : : COS(X) / SIN(X) for COMPLEX argument. */
4153 : :
4154 : : static void
4155 : 102 : gfc_conv_intrinsic_cotan (gfc_se *se, gfc_expr *expr)
4156 : : {
4157 : 102 : gfc_intrinsic_map_t *m;
4158 : 102 : tree arg;
4159 : 102 : tree type;
4160 : :
4161 : 102 : type = gfc_typenode_for_spec (&expr->ts);
4162 : 102 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4163 : :
4164 : 102 : if (expr->ts.type == BT_REAL)
4165 : : {
4166 : 102 : tree tan;
4167 : 102 : tree tmp;
4168 : 102 : mpfr_t pio2;
4169 : :
4170 : : /* Create pi/2. */
4171 : 102 : gfc_set_model_kind (expr->ts.kind);
4172 : 102 : mpfr_init (pio2);
4173 : 102 : mpfr_const_pi (pio2, GFC_RND_MODE);
4174 : 102 : mpfr_div_ui (pio2, pio2, 2, GFC_RND_MODE);
4175 : 102 : tmp = gfc_conv_mpfr_to_tree (pio2, expr->ts.kind, 0);
4176 : 102 : mpfr_clear (pio2);
4177 : :
4178 : : /* Find tan builtin function. */
4179 : 102 : m = gfc_lookup_intrinsic (GFC_ISYM_TAN);
4180 : 102 : tan = gfc_get_intrinsic_lib_fndecl (m, expr);
4181 : 102 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, arg, tmp);
4182 : 102 : tan = build_call_expr_loc (input_location, tan, 1, tmp);
4183 : 102 : se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tan);
4184 : : }
4185 : : else
4186 : : {
4187 : 0 : tree sin;
4188 : 0 : tree cos;
4189 : :
4190 : : /* Find cos builtin function. */
4191 : 0 : m = gfc_lookup_intrinsic (GFC_ISYM_COS);
4192 : 0 : cos = gfc_get_intrinsic_lib_fndecl (m, expr);
4193 : 0 : cos = build_call_expr_loc (input_location, cos, 1, arg);
4194 : :
4195 : : /* Find sin builtin function. */
4196 : 0 : m = gfc_lookup_intrinsic (GFC_ISYM_SIN);
4197 : 0 : sin = gfc_get_intrinsic_lib_fndecl (m, expr);
4198 : 0 : sin = build_call_expr_loc (input_location, sin, 1, arg);
4199 : :
4200 : : /* Divide cos by sin. */
4201 : 0 : se->expr = fold_build2_loc (input_location, RDIV_EXPR, type, cos, sin);
4202 : : }
4203 : 102 : }
4204 : :
4205 : :
4206 : : /* COTAND(X) is translated into -TAND(X+90) for REAL argument. */
4207 : :
4208 : : static void
4209 : 108 : gfc_conv_intrinsic_cotand (gfc_se *se, gfc_expr *expr)
4210 : : {
4211 : 108 : tree arg;
4212 : 108 : tree type;
4213 : 108 : tree ninety_tree;
4214 : 108 : mpfr_t ninety;
4215 : :
4216 : 108 : type = gfc_typenode_for_spec (&expr->ts);
4217 : 108 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4218 : :
4219 : 108 : gfc_set_model_kind (expr->ts.kind);
4220 : :
4221 : : /* Build the tree for x + 90. */
4222 : 108 : mpfr_init_set_ui (ninety, 90, GFC_RND_MODE);
4223 : 108 : ninety_tree = gfc_conv_mpfr_to_tree (ninety, expr->ts.kind, 0);
4224 : 108 : arg = fold_build2_loc (input_location, PLUS_EXPR, type, arg, ninety_tree);
4225 : 108 : mpfr_clear (ninety);
4226 : :
4227 : : /* Find tand. */
4228 : 108 : gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_TAND);
4229 : 108 : tree tand = gfc_get_intrinsic_lib_fndecl (m, expr);
4230 : 108 : tand = build_call_expr_loc (input_location, tand, 1, arg);
4231 : :
4232 : 108 : se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tand);
4233 : 108 : }
4234 : :
4235 : :
4236 : : /* ATAN2D(Y,X) is translated into ATAN2(Y,X) * 180 / PI. */
4237 : :
4238 : : static void
4239 : 120 : gfc_conv_intrinsic_atan2d (gfc_se *se, gfc_expr *expr)
4240 : : {
4241 : 120 : tree args[2];
4242 : 120 : tree atan2d;
4243 : 120 : tree type;
4244 : :
4245 : 120 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
4246 : 120 : type = TREE_TYPE (args[0]);
4247 : :
4248 : 120 : gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_ATAN2);
4249 : 120 : atan2d = gfc_get_intrinsic_lib_fndecl (m, expr);
4250 : 120 : atan2d = build_call_expr_loc (input_location, atan2d, 2, args[0], args[1]);
4251 : :
4252 : 120 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atan2d,
4253 : : rad2deg (expr->ts.kind));
4254 : 120 : }
4255 : :
4256 : :
4257 : : /* COUNT(A) = Number of true elements in A. */
4258 : : static void
4259 : 143 : gfc_conv_intrinsic_count (gfc_se * se, gfc_expr * expr)
4260 : : {
4261 : 143 : tree resvar;
4262 : 143 : tree type;
4263 : 143 : stmtblock_t body;
4264 : 143 : tree tmp;
4265 : 143 : gfc_loopinfo loop;
4266 : 143 : gfc_actual_arglist *actual;
4267 : 143 : gfc_ss *arrayss;
4268 : 143 : gfc_se arrayse;
4269 : :
4270 : 143 : if (se->ss)
4271 : : {
4272 : 0 : gfc_conv_intrinsic_funcall (se, expr);
4273 : 0 : return;
4274 : : }
4275 : :
4276 : 143 : actual = expr->value.function.actual;
4277 : :
4278 : 143 : type = gfc_typenode_for_spec (&expr->ts);
4279 : : /* Initialize the result. */
4280 : 143 : resvar = gfc_create_var (type, "count");
4281 : 143 : gfc_add_modify (&se->pre, resvar, build_int_cst (type, 0));
4282 : :
4283 : : /* Walk the arguments. */
4284 : 143 : arrayss = gfc_walk_expr (actual->expr);
4285 : 143 : gcc_assert (arrayss != gfc_ss_terminator);
4286 : :
4287 : : /* Initialize the scalarizer. */
4288 : 143 : gfc_init_loopinfo (&loop);
4289 : 143 : gfc_add_ss_to_loop (&loop, arrayss);
4290 : :
4291 : : /* Initialize the loop. */
4292 : 143 : gfc_conv_ss_startstride (&loop);
4293 : 143 : gfc_conv_loop_setup (&loop, &expr->where);
4294 : :
4295 : 143 : gfc_mark_ss_chain_used (arrayss, 1);
4296 : : /* Generate the loop body. */
4297 : 143 : gfc_start_scalarized_body (&loop, &body);
4298 : :
4299 : 143 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (resvar),
4300 : 143 : resvar, build_int_cst (TREE_TYPE (resvar), 1));
4301 : 143 : tmp = build2_v (MODIFY_EXPR, resvar, tmp);
4302 : :
4303 : 143 : gfc_init_se (&arrayse, NULL);
4304 : 143 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
4305 : 143 : arrayse.ss = arrayss;
4306 : 143 : gfc_conv_expr_val (&arrayse, actual->expr);
4307 : 143 : tmp = build3_v (COND_EXPR, arrayse.expr, tmp,
4308 : : build_empty_stmt (input_location));
4309 : :
4310 : 143 : gfc_add_block_to_block (&body, &arrayse.pre);
4311 : 143 : gfc_add_expr_to_block (&body, tmp);
4312 : 143 : gfc_add_block_to_block (&body, &arrayse.post);
4313 : :
4314 : 143 : gfc_trans_scalarizing_loops (&loop, &body);
4315 : :
4316 : 143 : gfc_add_block_to_block (&se->pre, &loop.pre);
4317 : 143 : gfc_add_block_to_block (&se->pre, &loop.post);
4318 : 143 : gfc_cleanup_loop (&loop);
4319 : :
4320 : 143 : se->expr = resvar;
4321 : : }
4322 : :
4323 : :
4324 : : /* Update given gfc_se to have ss component pointing to the nested gfc_ss
4325 : : struct and return the corresponding loopinfo. */
4326 : :
4327 : : static gfc_loopinfo *
4328 : 3374 : enter_nested_loop (gfc_se *se)
4329 : : {
4330 : 3374 : se->ss = se->ss->nested_ss;
4331 : 3374 : gcc_assert (se->ss == se->ss->loop->ss);
4332 : :
4333 : 3374 : return se->ss->loop;
4334 : : }
4335 : :
4336 : : /* Build the condition for a mask, which may be optional. */
4337 : :
4338 : : static tree
4339 : 12763 : conv_mask_condition (gfc_se *maskse, gfc_expr *maskexpr,
4340 : : bool optional_mask)
4341 : : {
4342 : 12763 : tree present;
4343 : 12763 : tree type;
4344 : :
4345 : 12763 : if (optional_mask)
4346 : : {
4347 : 206 : type = TREE_TYPE (maskse->expr);
4348 : 206 : present = gfc_conv_expr_present (maskexpr->symtree->n.sym);
4349 : 206 : present = convert (type, present);
4350 : 206 : present = fold_build1_loc (input_location, TRUTH_NOT_EXPR, type,
4351 : : present);
4352 : 206 : return fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
4353 : 206 : type, present, maskse->expr);
4354 : : }
4355 : : else
4356 : 12557 : return maskse->expr;
4357 : : }
4358 : :
4359 : : /* Inline implementation of the sum and product intrinsics. */
4360 : : static void
4361 : 2487 : gfc_conv_intrinsic_arith (gfc_se * se, gfc_expr * expr, enum tree_code op,
4362 : : bool norm2)
4363 : : {
4364 : 2487 : tree resvar;
4365 : 2487 : tree scale = NULL_TREE;
4366 : 2487 : tree type;
4367 : 2487 : stmtblock_t body;
4368 : 2487 : stmtblock_t block;
4369 : 2487 : tree tmp;
4370 : 2487 : gfc_loopinfo loop, *ploop;
4371 : 2487 : gfc_actual_arglist *arg_array, *arg_mask;
4372 : 2487 : gfc_ss *arrayss = NULL;
4373 : 2487 : gfc_ss *maskss = NULL;
4374 : 2487 : gfc_se arrayse;
4375 : 2487 : gfc_se maskse;
4376 : 2487 : gfc_se *parent_se;
4377 : 2487 : gfc_expr *arrayexpr;
4378 : 2487 : gfc_expr *maskexpr;
4379 : 2487 : bool optional_mask;
4380 : :
4381 : 2487 : if (expr->rank > 0)
4382 : : {
4383 : 578 : gcc_assert (gfc_inline_intrinsic_function_p (expr));
4384 : : parent_se = se;
4385 : : }
4386 : : else
4387 : : parent_se = NULL;
4388 : :
4389 : 2487 : type = gfc_typenode_for_spec (&expr->ts);
4390 : : /* Initialize the result. */
4391 : 2487 : resvar = gfc_create_var (type, "val");
4392 : 2487 : if (norm2)
4393 : : {
4394 : : /* result = 0.0;
4395 : : scale = 1.0. */
4396 : 68 : scale = gfc_create_var (type, "scale");
4397 : 68 : gfc_add_modify (&se->pre, scale,
4398 : : gfc_build_const (type, integer_one_node));
4399 : 68 : tmp = gfc_build_const (type, integer_zero_node);
4400 : : }
4401 : 2419 : else if (op == PLUS_EXPR || op == BIT_IOR_EXPR || op == BIT_XOR_EXPR)
4402 : 2015 : tmp = gfc_build_const (type, integer_zero_node);
4403 : 404 : else if (op == NE_EXPR)
4404 : : /* PARITY. */
4405 : 36 : tmp = convert (type, boolean_false_node);
4406 : 368 : else if (op == BIT_AND_EXPR)
4407 : 24 : tmp = gfc_build_const (type, fold_build1_loc (input_location, NEGATE_EXPR,
4408 : : type, integer_one_node));
4409 : : else
4410 : 344 : tmp = gfc_build_const (type, integer_one_node);
4411 : :
4412 : 2487 : gfc_add_modify (&se->pre, resvar, tmp);
4413 : :
4414 : 2487 : arg_array = expr->value.function.actual;
4415 : :
4416 : 2487 : arrayexpr = arg_array->expr;
4417 : :
4418 : 2487 : if (op == NE_EXPR || norm2)
4419 : : {
4420 : : /* PARITY and NORM2. */
4421 : : maskexpr = NULL;
4422 : : optional_mask = false;
4423 : : }
4424 : : else
4425 : : {
4426 : 2383 : arg_mask = arg_array->next->next;
4427 : 2383 : gcc_assert (arg_mask != NULL);
4428 : 2383 : maskexpr = arg_mask->expr;
4429 : 371 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
4430 : 266 : && maskexpr->symtree->n.sym->attr.dummy
4431 : 2401 : && maskexpr->symtree->n.sym->attr.optional;
4432 : : }
4433 : :
4434 : 2487 : if (expr->rank == 0)
4435 : : {
4436 : : /* Walk the arguments. */
4437 : 1909 : arrayss = gfc_walk_expr (arrayexpr);
4438 : 1909 : gcc_assert (arrayss != gfc_ss_terminator);
4439 : :
4440 : 1909 : if (maskexpr && maskexpr->rank > 0)
4441 : : {
4442 : 223 : maskss = gfc_walk_expr (maskexpr);
4443 : 223 : gcc_assert (maskss != gfc_ss_terminator);
4444 : : }
4445 : : else
4446 : : maskss = NULL;
4447 : :
4448 : : /* Initialize the scalarizer. */
4449 : 1909 : gfc_init_loopinfo (&loop);
4450 : :
4451 : : /* We add the mask first because the number of iterations is
4452 : : taken from the last ss, and this breaks if an absent
4453 : : optional argument is used for mask. */
4454 : :
4455 : 1909 : if (maskexpr && maskexpr->rank > 0)
4456 : 223 : gfc_add_ss_to_loop (&loop, maskss);
4457 : 1909 : gfc_add_ss_to_loop (&loop, arrayss);
4458 : :
4459 : : /* Initialize the loop. */
4460 : 1909 : gfc_conv_ss_startstride (&loop);
4461 : 1909 : gfc_conv_loop_setup (&loop, &expr->where);
4462 : :
4463 : 1909 : if (maskexpr && maskexpr->rank > 0)
4464 : 223 : gfc_mark_ss_chain_used (maskss, 1);
4465 : 1909 : gfc_mark_ss_chain_used (arrayss, 1);
4466 : :
4467 : 1909 : ploop = &loop;
4468 : : }
4469 : : else
4470 : : /* All the work has been done in the parent loops. */
4471 : 578 : ploop = enter_nested_loop (se);
4472 : :
4473 : 2487 : gcc_assert (ploop);
4474 : :
4475 : : /* Generate the loop body. */
4476 : 2487 : gfc_start_scalarized_body (ploop, &body);
4477 : :
4478 : : /* If we have a mask, only add this element if the mask is set. */
4479 : 2487 : if (maskexpr && maskexpr->rank > 0)
4480 : : {
4481 : 307 : gfc_init_se (&maskse, parent_se);
4482 : 307 : gfc_copy_loopinfo_to_se (&maskse, ploop);
4483 : 307 : if (expr->rank == 0)
4484 : 223 : maskse.ss = maskss;
4485 : 307 : gfc_conv_expr_val (&maskse, maskexpr);
4486 : 307 : gfc_add_block_to_block (&body, &maskse.pre);
4487 : :
4488 : 307 : gfc_start_block (&block);
4489 : : }
4490 : : else
4491 : 2180 : gfc_init_block (&block);
4492 : :
4493 : : /* Do the actual summation/product. */
4494 : 2487 : gfc_init_se (&arrayse, parent_se);
4495 : 2487 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
4496 : 2487 : if (expr->rank == 0)
4497 : 1909 : arrayse.ss = arrayss;
4498 : 2487 : gfc_conv_expr_val (&arrayse, arrayexpr);
4499 : 2487 : gfc_add_block_to_block (&block, &arrayse.pre);
4500 : :
4501 : 2487 : if (norm2)
4502 : : {
4503 : : /* if (x (i) != 0.0)
4504 : : {
4505 : : absX = abs(x(i))
4506 : : if (absX > scale)
4507 : : {
4508 : : val = scale/absX;
4509 : : result = 1.0 + result * val * val;
4510 : : scale = absX;
4511 : : }
4512 : : else
4513 : : {
4514 : : val = absX/scale;
4515 : : result += val * val;
4516 : : }
4517 : : } */
4518 : 68 : tree res1, res2, cond, absX, val;
4519 : 68 : stmtblock_t ifblock1, ifblock2, ifblock3;
4520 : :
4521 : 68 : gfc_init_block (&ifblock1);
4522 : :
4523 : 68 : absX = gfc_create_var (type, "absX");
4524 : 68 : gfc_add_modify (&ifblock1, absX,
4525 : : fold_build1_loc (input_location, ABS_EXPR, type,
4526 : : arrayse.expr));
4527 : 68 : val = gfc_create_var (type, "val");
4528 : 68 : gfc_add_expr_to_block (&ifblock1, val);
4529 : :
4530 : 68 : gfc_init_block (&ifblock2);
4531 : 68 : gfc_add_modify (&ifblock2, val,
4532 : : fold_build2_loc (input_location, RDIV_EXPR, type, scale,
4533 : : absX));
4534 : 68 : res1 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4535 : 68 : res1 = fold_build2_loc (input_location, MULT_EXPR, type, resvar, res1);
4536 : 68 : res1 = fold_build2_loc (input_location, PLUS_EXPR, type, res1,
4537 : : gfc_build_const (type, integer_one_node));
4538 : 68 : gfc_add_modify (&ifblock2, resvar, res1);
4539 : 68 : gfc_add_modify (&ifblock2, scale, absX);
4540 : 68 : res1 = gfc_finish_block (&ifblock2);
4541 : :
4542 : 68 : gfc_init_block (&ifblock3);
4543 : 68 : gfc_add_modify (&ifblock3, val,
4544 : : fold_build2_loc (input_location, RDIV_EXPR, type, absX,
4545 : : scale));
4546 : 68 : res2 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4547 : 68 : res2 = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, res2);
4548 : 68 : gfc_add_modify (&ifblock3, resvar, res2);
4549 : 68 : res2 = gfc_finish_block (&ifblock3);
4550 : :
4551 : 68 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
4552 : : absX, scale);
4553 : 68 : tmp = build3_v (COND_EXPR, cond, res1, res2);
4554 : 68 : gfc_add_expr_to_block (&ifblock1, tmp);
4555 : 68 : tmp = gfc_finish_block (&ifblock1);
4556 : :
4557 : 68 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
4558 : : arrayse.expr,
4559 : : gfc_build_const (type, integer_zero_node));
4560 : :
4561 : 68 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
4562 : 68 : gfc_add_expr_to_block (&block, tmp);
4563 : : }
4564 : : else
4565 : : {
4566 : 2419 : tmp = fold_build2_loc (input_location, op, type, resvar, arrayse.expr);
4567 : 2419 : gfc_add_modify (&block, resvar, tmp);
4568 : : }
4569 : :
4570 : 2487 : gfc_add_block_to_block (&block, &arrayse.post);
4571 : :
4572 : 2487 : if (maskexpr && maskexpr->rank > 0)
4573 : : {
4574 : : /* We enclose the above in if (mask) {...} . If the mask is an
4575 : : optional argument, generate
4576 : : IF (.NOT. PRESENT(MASK) .OR. MASK(I)). */
4577 : 307 : tree ifmask;
4578 : 307 : tmp = gfc_finish_block (&block);
4579 : 307 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4580 : 307 : tmp = build3_v (COND_EXPR, ifmask, tmp,
4581 : : build_empty_stmt (input_location));
4582 : 307 : }
4583 : : else
4584 : 2180 : tmp = gfc_finish_block (&block);
4585 : 2487 : gfc_add_expr_to_block (&body, tmp);
4586 : :
4587 : 2487 : gfc_trans_scalarizing_loops (ploop, &body);
4588 : :
4589 : : /* For a scalar mask, enclose the loop in an if statement. */
4590 : 2487 : if (maskexpr && maskexpr->rank == 0)
4591 : : {
4592 : 64 : gfc_init_block (&block);
4593 : 64 : gfc_add_block_to_block (&block, &ploop->pre);
4594 : 64 : gfc_add_block_to_block (&block, &ploop->post);
4595 : 64 : tmp = gfc_finish_block (&block);
4596 : :
4597 : 64 : if (expr->rank > 0)
4598 : : {
4599 : 34 : tmp = build3_v (COND_EXPR, se->ss->info->data.scalar.value, tmp,
4600 : : build_empty_stmt (input_location));
4601 : 34 : gfc_advance_se_ss_chain (se);
4602 : : }
4603 : : else
4604 : : {
4605 : 30 : tree ifmask;
4606 : :
4607 : 30 : gcc_assert (expr->rank == 0);
4608 : 30 : gfc_init_se (&maskse, NULL);
4609 : 30 : gfc_conv_expr_val (&maskse, maskexpr);
4610 : 30 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4611 : 30 : tmp = build3_v (COND_EXPR, ifmask, tmp,
4612 : : build_empty_stmt (input_location));
4613 : : }
4614 : :
4615 : 64 : gfc_add_expr_to_block (&block, tmp);
4616 : 64 : gfc_add_block_to_block (&se->pre, &block);
4617 : 64 : gcc_assert (se->post.head == NULL);
4618 : : }
4619 : : else
4620 : : {
4621 : 2423 : gfc_add_block_to_block (&se->pre, &ploop->pre);
4622 : 2423 : gfc_add_block_to_block (&se->pre, &ploop->post);
4623 : : }
4624 : :
4625 : 2487 : if (expr->rank == 0)
4626 : 1909 : gfc_cleanup_loop (ploop);
4627 : :
4628 : 2487 : if (norm2)
4629 : : {
4630 : : /* result = scale * sqrt(result). */
4631 : 68 : tree sqrt;
4632 : 68 : sqrt = gfc_builtin_decl_for_float_kind (BUILT_IN_SQRT, expr->ts.kind);
4633 : 68 : resvar = build_call_expr_loc (input_location,
4634 : : sqrt, 1, resvar);
4635 : 68 : resvar = fold_build2_loc (input_location, MULT_EXPR, type, scale, resvar);
4636 : : }
4637 : :
4638 : 2487 : se->expr = resvar;
4639 : 2487 : }
4640 : :
4641 : :
4642 : : /* Inline implementation of the dot_product intrinsic. This function
4643 : : is based on gfc_conv_intrinsic_arith (the previous function). */
4644 : : static void
4645 : 113 : gfc_conv_intrinsic_dot_product (gfc_se * se, gfc_expr * expr)
4646 : : {
4647 : 113 : tree resvar;
4648 : 113 : tree type;
4649 : 113 : stmtblock_t body;
4650 : 113 : stmtblock_t block;
4651 : 113 : tree tmp;
4652 : 113 : gfc_loopinfo loop;
4653 : 113 : gfc_actual_arglist *actual;
4654 : 113 : gfc_ss *arrayss1, *arrayss2;
4655 : 113 : gfc_se arrayse1, arrayse2;
4656 : 113 : gfc_expr *arrayexpr1, *arrayexpr2;
4657 : :
4658 : 113 : type = gfc_typenode_for_spec (&expr->ts);
4659 : :
4660 : : /* Initialize the result. */
4661 : 113 : resvar = gfc_create_var (type, "val");
4662 : 113 : if (expr->ts.type == BT_LOGICAL)
4663 : 30 : tmp = build_int_cst (type, 0);
4664 : : else
4665 : 83 : tmp = gfc_build_const (type, integer_zero_node);
4666 : :
4667 : 113 : gfc_add_modify (&se->pre, resvar, tmp);
4668 : :
4669 : : /* Walk argument #1. */
4670 : 113 : actual = expr->value.function.actual;
4671 : 113 : arrayexpr1 = actual->expr;
4672 : 113 : arrayss1 = gfc_walk_expr (arrayexpr1);
4673 : 113 : gcc_assert (arrayss1 != gfc_ss_terminator);
4674 : :
4675 : : /* Walk argument #2. */
4676 : 113 : actual = actual->next;
4677 : 113 : arrayexpr2 = actual->expr;
4678 : 113 : arrayss2 = gfc_walk_expr (arrayexpr2);
4679 : 113 : gcc_assert (arrayss2 != gfc_ss_terminator);
4680 : :
4681 : : /* Initialize the scalarizer. */
4682 : 113 : gfc_init_loopinfo (&loop);
4683 : 113 : gfc_add_ss_to_loop (&loop, arrayss1);
4684 : 113 : gfc_add_ss_to_loop (&loop, arrayss2);
4685 : :
4686 : : /* Initialize the loop. */
4687 : 113 : gfc_conv_ss_startstride (&loop);
4688 : 113 : gfc_conv_loop_setup (&loop, &expr->where);
4689 : :
4690 : 113 : gfc_mark_ss_chain_used (arrayss1, 1);
4691 : 113 : gfc_mark_ss_chain_used (arrayss2, 1);
4692 : :
4693 : : /* Generate the loop body. */
4694 : 113 : gfc_start_scalarized_body (&loop, &body);
4695 : 113 : gfc_init_block (&block);
4696 : :
4697 : : /* Make the tree expression for [conjg(]array1[)]. */
4698 : 113 : gfc_init_se (&arrayse1, NULL);
4699 : 113 : gfc_copy_loopinfo_to_se (&arrayse1, &loop);
4700 : 113 : arrayse1.ss = arrayss1;
4701 : 113 : gfc_conv_expr_val (&arrayse1, arrayexpr1);
4702 : 113 : if (expr->ts.type == BT_COMPLEX)
4703 : 9 : arrayse1.expr = fold_build1_loc (input_location, CONJ_EXPR, type,
4704 : : arrayse1.expr);
4705 : 113 : gfc_add_block_to_block (&block, &arrayse1.pre);
4706 : :
4707 : : /* Make the tree expression for array2. */
4708 : 113 : gfc_init_se (&arrayse2, NULL);
4709 : 113 : gfc_copy_loopinfo_to_se (&arrayse2, &loop);
4710 : 113 : arrayse2.ss = arrayss2;
4711 : 113 : gfc_conv_expr_val (&arrayse2, arrayexpr2);
4712 : 113 : gfc_add_block_to_block (&block, &arrayse2.pre);
4713 : :
4714 : : /* Do the actual product and sum. */
4715 : 113 : if (expr->ts.type == BT_LOGICAL)
4716 : : {
4717 : 30 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, type,
4718 : : arrayse1.expr, arrayse2.expr);
4719 : 30 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, type, resvar, tmp);
4720 : : }
4721 : : else
4722 : : {
4723 : 83 : tmp = fold_build2_loc (input_location, MULT_EXPR, type, arrayse1.expr,
4724 : : arrayse2.expr);
4725 : 83 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, tmp);
4726 : : }
4727 : 113 : gfc_add_modify (&block, resvar, tmp);
4728 : :
4729 : : /* Finish up the loop block and the loop. */
4730 : 113 : tmp = gfc_finish_block (&block);
4731 : 113 : gfc_add_expr_to_block (&body, tmp);
4732 : :
4733 : 113 : gfc_trans_scalarizing_loops (&loop, &body);
4734 : 113 : gfc_add_block_to_block (&se->pre, &loop.pre);
4735 : 113 : gfc_add_block_to_block (&se->pre, &loop.post);
4736 : 113 : gfc_cleanup_loop (&loop);
4737 : :
4738 : 113 : se->expr = resvar;
4739 : 113 : }
4740 : :
4741 : :
4742 : : /* Tells whether the expression E is a reference to an optional variable whose
4743 : : presence is not known at compile time. Those are variable references without
4744 : : subreference; if there is a subreference, we can assume the variable is
4745 : : present. We have to special case full arrays, which we represent with a fake
4746 : : "full" reference, and class descriptors for which a reference to data is not
4747 : : really a subreference. */
4748 : :
4749 : : bool
4750 : 14613 : maybe_absent_optional_variable (gfc_expr *e)
4751 : : {
4752 : 14613 : if (!(e && e->expr_type == EXPR_VARIABLE))
4753 : : return false;
4754 : :
4755 : 1716 : gfc_symbol *sym = e->symtree->n.sym;
4756 : 1716 : if (!sym->attr.optional)
4757 : : return false;
4758 : :
4759 : 224 : gfc_ref *ref = e->ref;
4760 : 224 : if (ref == nullptr)
4761 : : return true;
4762 : :
4763 : 20 : if (ref->type == REF_ARRAY
4764 : 20 : && ref->u.ar.type == AR_FULL
4765 : 20 : && ref->next == nullptr)
4766 : : return true;
4767 : :
4768 : 0 : if (!(sym->ts.type == BT_CLASS
4769 : 0 : && ref->type == REF_COMPONENT
4770 : 0 : && ref->u.c.component == CLASS_DATA (sym)))
4771 : : return false;
4772 : :
4773 : 0 : gfc_ref *next_ref = ref->next;
4774 : 0 : if (next_ref == nullptr)
4775 : : return true;
4776 : :
4777 : 0 : if (next_ref->type == REF_ARRAY
4778 : 0 : && next_ref->u.ar.type == AR_FULL
4779 : 0 : && next_ref->next == nullptr)
4780 : 0 : return true;
4781 : :
4782 : : return false;
4783 : : }
4784 : :
4785 : :
4786 : : /* Emit code for minloc or maxloc intrinsic. There are many different cases
4787 : : we need to handle. For performance reasons we sometimes create two
4788 : : loops instead of one, where the second one is much simpler.
4789 : : Examples for minloc intrinsic:
4790 : : A: Result is scalar.
4791 : : 1) Array mask is used and NaNs need to be supported:
4792 : : limit = Infinity;
4793 : : pos = 0;
4794 : : S = from;
4795 : : while (S <= to) {
4796 : : if (mask[S]) {
4797 : : if (pos == 0) pos = S + (1 - from);
4798 : : if (a[S] <= limit) {
4799 : : limit = a[S];
4800 : : pos = S + (1 - from);
4801 : : goto lab1;
4802 : : }
4803 : : }
4804 : : S++;
4805 : : }
4806 : : goto lab2;
4807 : : lab1:;
4808 : : while (S <= to) {
4809 : : if (mask[S])
4810 : : if (a[S] < limit) {
4811 : : limit = a[S];
4812 : : pos = S + (1 - from);
4813 : : }
4814 : : S++;
4815 : : }
4816 : : lab2:;
4817 : : 2) NaNs need to be supported, but it is known at compile time or cheaply
4818 : : at runtime whether array is nonempty or not:
4819 : : limit = Infinity;
4820 : : pos = 0;
4821 : : S = from;
4822 : : while (S <= to) {
4823 : : if (a[S] <= limit) {
4824 : : limit = a[S];
4825 : : pos = S + (1 - from);
4826 : : goto lab1;
4827 : : }
4828 : : S++;
4829 : : }
4830 : : if (from <= to) pos = 1;
4831 : : goto lab2;
4832 : : lab1:;
4833 : : while (S <= to) {
4834 : : if (a[S] < limit) {
4835 : : limit = a[S];
4836 : : pos = S + (1 - from);
4837 : : }
4838 : : S++;
4839 : : }
4840 : : lab2:;
4841 : : 3) NaNs aren't supported, array mask is used:
4842 : : limit = infinities_supported ? Infinity : huge (limit);
4843 : : pos = 0;
4844 : : S = from;
4845 : : while (S <= to) {
4846 : : if (mask[S]) {
4847 : : limit = a[S];
4848 : : pos = S + (1 - from);
4849 : : goto lab1;
4850 : : }
4851 : : S++;
4852 : : }
4853 : : goto lab2;
4854 : : lab1:;
4855 : : while (S <= to) {
4856 : : if (mask[S])
4857 : : if (a[S] < limit) {
4858 : : limit = a[S];
4859 : : pos = S + (1 - from);
4860 : : }
4861 : : S++;
4862 : : }
4863 : : lab2:;
4864 : : 4) Same without array mask:
4865 : : limit = infinities_supported ? Infinity : huge (limit);
4866 : : pos = (from <= to) ? 1 : 0;
4867 : : S = from;
4868 : : while (S <= to) {
4869 : : if (a[S] < limit) {
4870 : : limit = a[S];
4871 : : pos = S + (1 - from);
4872 : : }
4873 : : S++;
4874 : : }
4875 : : B: Array result, non-CHARACTER type, DIM absent
4876 : : Generate similar code as in the scalar case, using a collection of
4877 : : variables (one per dimension) instead of a single variable as result.
4878 : : Picking only cases 1) and 4) with ARRAY of rank 2, the generated code
4879 : : becomes:
4880 : : 1) Array mask is used and NaNs need to be supported:
4881 : : limit = Infinity;
4882 : : pos0 = 0;
4883 : : pos1 = 0;
4884 : : S1 = from1;
4885 : : second_loop_entry = false;
4886 : : while (S1 <= to1) {
4887 : : S0 = from0;
4888 : : while (s0 <= to0 {
4889 : : if (mask[S1][S0]) {
4890 : : if (pos0 == 0) {
4891 : : pos0 = S0 + (1 - from0);
4892 : : pos1 = S1 + (1 - from1);
4893 : : }
4894 : : if (a[S1][S0] <= limit) {
4895 : : limit = a[S1][S0];
4896 : : pos0 = S0 + (1 - from0);
4897 : : pos1 = S1 + (1 - from1);
4898 : : second_loop_entry = true;
4899 : : goto lab1;
4900 : : }
4901 : : }
4902 : : S0++;
4903 : : }
4904 : : S1++;
4905 : : }
4906 : : goto lab2;
4907 : : lab1:;
4908 : : S1 = second_loop_entry ? S1 : from1;
4909 : : while (S1 <= to1) {
4910 : : S0 = second_loop_entry ? S0 : from0;
4911 : : while (S0 <= to0) {
4912 : : if (mask[S1][S0])
4913 : : if (a[S1][S0] < limit) {
4914 : : limit = a[S1][S0];
4915 : : pos0 = S + (1 - from0);
4916 : : pos1 = S + (1 - from1);
4917 : : }
4918 : : second_loop_entry = false;
4919 : : S0++;
4920 : : }
4921 : : S1++;
4922 : : }
4923 : : lab2:;
4924 : : result = { pos0, pos1 };
4925 : : ...
4926 : : 4) NANs aren't supported, no array mask.
4927 : : limit = infinities_supported ? Infinity : huge (limit);
4928 : : pos0 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4929 : : pos1 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4930 : : S1 = from1;
4931 : : while (S1 <= to1) {
4932 : : S0 = from0;
4933 : : while (S0 <= to0) {
4934 : : if (a[S1][S0] < limit) {
4935 : : limit = a[S1][S0];
4936 : : pos0 = S + (1 - from0);
4937 : : pos1 = S + (1 - from1);
4938 : : }
4939 : : S0++;
4940 : : }
4941 : : S1++;
4942 : : }
4943 : : result = { pos0, pos1 };
4944 : : C: Otherwise, a call is generated.
4945 : : For 2) and 4), if mask is scalar, this all goes into a conditional,
4946 : : setting pos = 0; in the else branch.
4947 : :
4948 : : Since we now also support the BACK argument, instead of using
4949 : : if (a[S] < limit), we now use
4950 : :
4951 : : if (back)
4952 : : cond = a[S] <= limit;
4953 : : else
4954 : : cond = a[S] < limit;
4955 : : if (cond) {
4956 : : ....
4957 : :
4958 : : The optimizer is smart enough to move the condition out of the loop.
4959 : : They are now marked as unlikely too for further speedup. */
4960 : :
4961 : : static void
4962 : 18898 : gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op)
4963 : : {
4964 : 18898 : stmtblock_t body;
4965 : 18898 : stmtblock_t block;
4966 : 18898 : stmtblock_t ifblock;
4967 : 18898 : stmtblock_t elseblock;
4968 : 18898 : tree limit;
4969 : 18898 : tree type;
4970 : 18898 : tree tmp;
4971 : 18898 : tree cond;
4972 : 18898 : tree elsetmp;
4973 : 18898 : tree ifbody;
4974 : 18898 : tree offset[GFC_MAX_DIMENSIONS];
4975 : 18898 : tree nonempty;
4976 : 18898 : tree lab1, lab2;
4977 : 18898 : tree b_if, b_else;
4978 : 18898 : tree back;
4979 : 18898 : gfc_loopinfo loop, *ploop;
4980 : 18898 : gfc_actual_arglist *array_arg, *dim_arg, *mask_arg, *kind_arg;
4981 : 18898 : gfc_actual_arglist *back_arg;
4982 : 18898 : gfc_ss *arrayss = nullptr;
4983 : 18898 : gfc_ss *maskss = nullptr;
4984 : 18898 : gfc_ss *orig_ss = nullptr;
4985 : 18898 : gfc_se arrayse;
4986 : 18898 : gfc_se maskse;
4987 : 18898 : gfc_se nested_se;
4988 : 18898 : gfc_se *base_se;
4989 : 18898 : gfc_expr *arrayexpr;
4990 : 18898 : gfc_expr *maskexpr;
4991 : 18898 : gfc_expr *backexpr;
4992 : 18898 : gfc_se backse;
4993 : 18898 : tree pos[GFC_MAX_DIMENSIONS];
4994 : 18898 : tree idx[GFC_MAX_DIMENSIONS];
4995 : 18898 : tree result_var = NULL_TREE;
4996 : 18898 : int n;
4997 : 18898 : bool optional_mask;
4998 : :
4999 : 18898 : array_arg = expr->value.function.actual;
5000 : 18898 : dim_arg = array_arg->next;
5001 : 18898 : mask_arg = dim_arg->next;
5002 : 18898 : kind_arg = mask_arg->next;
5003 : 18898 : back_arg = kind_arg->next;
5004 : :
5005 : 18898 : bool dim_present = dim_arg->expr != nullptr;
5006 : 18898 : bool nested_loop = dim_present && expr->rank > 0;
5007 : :
5008 : : /* Remove kind. */
5009 : 18898 : if (kind_arg->expr)
5010 : : {
5011 : 2240 : gfc_free_expr (kind_arg->expr);
5012 : 2240 : kind_arg->expr = NULL;
5013 : : }
5014 : :
5015 : : /* Pass BACK argument by value. */
5016 : 18898 : back_arg->name = "%VAL";
5017 : :
5018 : 18898 : if (se->ss)
5019 : : {
5020 : 14732 : if (se->ss->info->useflags)
5021 : : {
5022 : 7671 : if (!dim_present || !gfc_inline_intrinsic_function_p (expr))
5023 : : {
5024 : : /* The code generating and initializing the result array has been
5025 : : generated already before the scalarization loop, either with a
5026 : : library function call or with inline code; now we can just use
5027 : : the result. */
5028 : 4875 : gfc_conv_tmp_array_ref (se);
5029 : 13822 : return;
5030 : : }
5031 : : }
5032 : 7061 : else if (!gfc_inline_intrinsic_function_p (expr))
5033 : : {
5034 : 3780 : gfc_conv_intrinsic_funcall (se, expr);
5035 : 3780 : return;
5036 : : }
5037 : : }
5038 : :
5039 : 10243 : arrayexpr = array_arg->expr;
5040 : :
5041 : : /* Special case for character maxloc. Remove unneeded "dim" actual
5042 : : argument, then call a library function. */
5043 : :
5044 : 10243 : if (arrayexpr->ts.type == BT_CHARACTER)
5045 : : {
5046 : 292 : gcc_assert (expr->rank == 0);
5047 : :
5048 : 292 : if (dim_arg->expr)
5049 : : {
5050 : 292 : gfc_free_expr (dim_arg->expr);
5051 : 292 : dim_arg->expr = NULL;
5052 : : }
5053 : 292 : gfc_conv_intrinsic_funcall (se, expr);
5054 : 292 : return;
5055 : : }
5056 : :
5057 : 9951 : type = gfc_typenode_for_spec (&expr->ts);
5058 : :
5059 : 9951 : if (expr->rank > 0 && !dim_present)
5060 : : {
5061 : 3281 : gfc_array_spec as;
5062 : 3281 : memset (&as, 0, sizeof (as));
5063 : :
5064 : 3281 : as.rank = 1;
5065 : 3281 : as.lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
5066 : : &arrayexpr->where,
5067 : : HOST_WIDE_INT_1);
5068 : 6562 : as.upper[0] = gfc_get_int_expr (gfc_index_integer_kind,
5069 : : &arrayexpr->where,
5070 : 3281 : arrayexpr->rank);
5071 : :
5072 : 3281 : tree array = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
5073 : :
5074 : 3281 : result_var = gfc_create_var (array, "loc_result");
5075 : : }
5076 : :
5077 : 7155 : const int reduction_dimensions = dim_present ? 1 : arrayexpr->rank;
5078 : :
5079 : : /* Initialize the result. */
5080 : 22177 : for (int i = 0; i < reduction_dimensions; i++)
5081 : : {
5082 : 12226 : pos[i] = gfc_create_var (gfc_array_index_type,
5083 : : gfc_get_string ("pos%d", i));
5084 : 12226 : offset[i] = gfc_create_var (gfc_array_index_type,
5085 : : gfc_get_string ("offset%d", i));
5086 : 12226 : idx[i] = gfc_create_var (gfc_array_index_type,
5087 : : gfc_get_string ("idx%d", i));
5088 : : }
5089 : :
5090 : 9951 : maskexpr = mask_arg->expr;
5091 : 6518 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5092 : 5329 : && maskexpr->symtree->n.sym->attr.dummy
5093 : 10116 : && maskexpr->symtree->n.sym->attr.optional;
5094 : 9951 : backexpr = back_arg->expr;
5095 : :
5096 : 17106 : gfc_init_se (&backse, nested_loop ? se : nullptr);
5097 : 9951 : if (backexpr == nullptr)
5098 : 0 : back = logical_false_node;
5099 : 9951 : else if (maybe_absent_optional_variable (backexpr))
5100 : : {
5101 : : /* This should have been checked already by
5102 : : maybe_absent_optional_variable. */
5103 : 184 : gcc_checking_assert (backexpr->expr_type == EXPR_VARIABLE);
5104 : :
5105 : 184 : gfc_conv_expr (&backse, backexpr);
5106 : 184 : tree present = gfc_conv_expr_present (backexpr->symtree->n.sym, false);
5107 : 184 : back = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5108 : : logical_type_node, present, backse.expr);
5109 : : }
5110 : : else
5111 : : {
5112 : 9767 : gfc_conv_expr (&backse, backexpr);
5113 : 9767 : back = backse.expr;
5114 : : }
5115 : 9951 : gfc_add_block_to_block (&se->pre, &backse.pre);
5116 : 9951 : back = gfc_evaluate_now_loc (input_location, back, &se->pre);
5117 : 9951 : gfc_add_block_to_block (&se->pre, &backse.post);
5118 : :
5119 : 9951 : if (nested_loop)
5120 : : {
5121 : 2796 : gfc_init_se (&nested_se, se);
5122 : 2796 : base_se = &nested_se;
5123 : : }
5124 : : else
5125 : : {
5126 : : /* Walk the arguments. */
5127 : 7155 : arrayss = gfc_walk_expr (arrayexpr);
5128 : 7155 : gcc_assert (arrayss != gfc_ss_terminator);
5129 : :
5130 : 7155 : if (maskexpr && maskexpr->rank != 0)
5131 : : {
5132 : 2700 : maskss = gfc_walk_expr (maskexpr);
5133 : 2700 : gcc_assert (maskss != gfc_ss_terminator);
5134 : : }
5135 : :
5136 : : base_se = nullptr;
5137 : : }
5138 : :
5139 : 18091 : nonempty = nullptr;
5140 : 7448 : if (!(maskexpr && maskexpr->rank > 0))
5141 : : {
5142 : 6077 : mpz_t asize;
5143 : 6077 : bool reduction_size_known;
5144 : :
5145 : 6077 : if (dim_present)
5146 : : {
5147 : 4032 : int reduction_dim;
5148 : 4032 : if (dim_arg->expr->expr_type == EXPR_CONSTANT)
5149 : 4030 : reduction_dim = mpz_get_si (dim_arg->expr->value.integer) - 1;
5150 : 2 : else if (arrayexpr->rank == 1)
5151 : : reduction_dim = 0;
5152 : : else
5153 : 0 : gcc_unreachable ();
5154 : 4032 : reduction_size_known = gfc_array_dimen_size (arrayexpr, reduction_dim,
5155 : : &asize);
5156 : : }
5157 : : else
5158 : 2045 : reduction_size_known = gfc_array_size (arrayexpr, &asize);
5159 : :
5160 : 6077 : if (reduction_size_known)
5161 : : {
5162 : 4482 : nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
5163 : 4482 : mpz_clear (asize);
5164 : 4482 : nonempty = fold_build2_loc (input_location, GT_EXPR,
5165 : : logical_type_node, nonempty,
5166 : : gfc_index_zero_node);
5167 : : }
5168 : 6077 : maskss = NULL;
5169 : : }
5170 : :
5171 : 9951 : limit = gfc_create_var (gfc_typenode_for_spec (&arrayexpr->ts), "limit");
5172 : 9951 : switch (arrayexpr->ts.type)
5173 : : {
5174 : 3898 : case BT_REAL:
5175 : 3898 : tmp = gfc_build_inf_or_huge (TREE_TYPE (limit), arrayexpr->ts.kind);
5176 : 3898 : break;
5177 : :
5178 : 6029 : case BT_INTEGER:
5179 : 6029 : n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5180 : 6029 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
5181 : : arrayexpr->ts.kind);
5182 : 6029 : break;
5183 : :
5184 : 24 : case BT_UNSIGNED:
5185 : : /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
5186 : 24 : if (op == GT_EXPR)
5187 : : {
5188 : 12 : tmp = gfc_get_unsigned_type (arrayexpr->ts.kind);
5189 : 12 : tmp = build_int_cst (tmp, 0);
5190 : : }
5191 : : else
5192 : : {
5193 : 12 : n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5194 : 12 : tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
5195 : : expr->ts.kind);
5196 : : }
5197 : : break;
5198 : :
5199 : 0 : default:
5200 : 0 : gcc_unreachable ();
5201 : : }
5202 : :
5203 : : /* We start with the most negative possible value for MAXLOC, and the most
5204 : : positive possible value for MINLOC. The most negative possible value is
5205 : : -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
5206 : : possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
5207 : : with above. */
5208 : 9951 : if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
5209 : 4724 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
5210 : 4724 : if (op == GT_EXPR && arrayexpr->ts.type == BT_INTEGER)
5211 : 2914 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp), tmp,
5212 : 2914 : build_int_cst (TREE_TYPE (tmp), 1));
5213 : :
5214 : 9951 : gfc_add_modify (&se->pre, limit, tmp);
5215 : :
5216 : : /* If we are in a case where we generate two sets of loops, the second one
5217 : : should continue where the first stopped instead of restarting from the
5218 : : beginning. So nested loops in the second set should have a partial range
5219 : : on the first iteration, but they should start from the beginning and span
5220 : : their full range on the following iterations. So we use conditionals in
5221 : : the loops lower bounds, and use the following variable in those
5222 : : conditionals to decide whether to use the original loop bound or to use
5223 : : the index at which the loop from the first set stopped. */
5224 : 9951 : tree second_loop_entry = gfc_create_var (logical_type_node,
5225 : : "second_loop_entry");
5226 : 9951 : gfc_add_modify (&se->pre, second_loop_entry, logical_false_node);
5227 : :
5228 : 9951 : if (nested_loop)
5229 : : {
5230 : 2796 : ploop = enter_nested_loop (&nested_se);
5231 : 2796 : orig_ss = nested_se.ss;
5232 : 2796 : ploop->temp_dim = 1;
5233 : : }
5234 : : else
5235 : : {
5236 : : /* Initialize the scalarizer. */
5237 : 7155 : gfc_init_loopinfo (&loop);
5238 : :
5239 : : /* We add the mask first because the number of iterations is taken
5240 : : from the last ss, and this breaks if an absent optional argument
5241 : : is used for mask. */
5242 : :
5243 : 7155 : if (maskss)
5244 : 2700 : gfc_add_ss_to_loop (&loop, maskss);
5245 : :
5246 : 7155 : gfc_add_ss_to_loop (&loop, arrayss);
5247 : :
5248 : : /* Initialize the loop. */
5249 : 7155 : gfc_conv_ss_startstride (&loop);
5250 : :
5251 : : /* The code generated can have more than one loop in sequence (see the
5252 : : comment at the function header). This doesn't work well with the
5253 : : scalarizer, which changes arrays' offset when the scalarization loops
5254 : : are generated (see gfc_trans_preloop_setup). Fortunately, we can use
5255 : : the scalarizer temporary code to handle multiple loops. Thus, we set
5256 : : temp_dim here, we call gfc_mark_ss_chain_used with flag=3 later, and
5257 : : we use gfc_trans_scalarized_loop_boundary even later to restore
5258 : : offset. */
5259 : 7155 : loop.temp_dim = loop.dimen;
5260 : 7155 : gfc_conv_loop_setup (&loop, &expr->where);
5261 : :
5262 : 7155 : ploop = &loop;
5263 : : }
5264 : :
5265 : 9951 : gcc_assert (reduction_dimensions == ploop->dimen);
5266 : :
5267 : 9951 : if (nonempty == NULL && !(maskexpr && maskexpr->rank > 0))
5268 : : {
5269 : 1595 : nonempty = logical_true_node;
5270 : :
5271 : 3697 : for (int i = 0; i < ploop->dimen; i++)
5272 : : {
5273 : 2102 : if (!(ploop->from[i] && ploop->to[i]))
5274 : : {
5275 : : nonempty = NULL;
5276 : : break;
5277 : : }
5278 : :
5279 : 2102 : tree tmp = fold_build2_loc (input_location, LE_EXPR,
5280 : : logical_type_node, ploop->from[i],
5281 : : ploop->to[i]);
5282 : :
5283 : 2102 : nonempty = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5284 : : logical_type_node, nonempty, tmp);
5285 : : }
5286 : : }
5287 : :
5288 : 11546 : lab1 = NULL;
5289 : 11546 : lab2 = NULL;
5290 : : /* Initialize the position to zero, following Fortran 2003. We are free
5291 : : to do this because Fortran 95 allows the result of an entirely false
5292 : : mask to be processor dependent. If we know at compile time the array
5293 : : is non-empty and no MASK is used, we can initialize to 1 to simplify
5294 : : the inner loop. */
5295 : 9951 : if (nonempty != NULL && !HONOR_NANS (DECL_MODE (limit)))
5296 : : {
5297 : 3748 : tree init = fold_build3_loc (input_location, COND_EXPR,
5298 : : gfc_array_index_type, nonempty,
5299 : : gfc_index_one_node,
5300 : : gfc_index_zero_node);
5301 : 8430 : for (int i = 0; i < ploop->dimen; i++)
5302 : 4682 : gfc_add_modify (&ploop->pre, pos[i], init);
5303 : : }
5304 : : else
5305 : : {
5306 : 13747 : for (int i = 0; i < ploop->dimen; i++)
5307 : 7544 : gfc_add_modify (&ploop->pre, pos[i], gfc_index_zero_node);
5308 : 6203 : lab1 = gfc_build_label_decl (NULL_TREE);
5309 : 6203 : TREE_USED (lab1) = 1;
5310 : 6203 : lab2 = gfc_build_label_decl (NULL_TREE);
5311 : 6203 : TREE_USED (lab2) = 1;
5312 : : }
5313 : :
5314 : : /* An offset must be added to the loop
5315 : : counter to obtain the required position. */
5316 : 22177 : for (int i = 0; i < ploop->dimen; i++)
5317 : : {
5318 : 12226 : gcc_assert (ploop->from[i]);
5319 : :
5320 : 12226 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5321 : : gfc_index_one_node, ploop->from[i]);
5322 : 12226 : gfc_add_modify (&ploop->pre, offset[i], tmp);
5323 : : }
5324 : :
5325 : 9951 : if (!nested_loop)
5326 : : {
5327 : 9965 : gfc_mark_ss_chain_used (arrayss, lab1 ? 3 : 1);
5328 : 7155 : if (maskss)
5329 : 2700 : gfc_mark_ss_chain_used (maskss, lab1 ? 3 : 1);
5330 : : }
5331 : :
5332 : : /* Generate the loop body. */
5333 : 9951 : gfc_start_scalarized_body (ploop, &body);
5334 : :
5335 : : /* If we have a mask, only check this element if the mask is set. */
5336 : 9951 : if (maskexpr && maskexpr->rank > 0)
5337 : : {
5338 : 3874 : gfc_init_se (&maskse, base_se);
5339 : 3874 : gfc_copy_loopinfo_to_se (&maskse, ploop);
5340 : 3874 : if (!nested_loop)
5341 : 2700 : maskse.ss = maskss;
5342 : 3874 : gfc_conv_expr_val (&maskse, maskexpr);
5343 : 3874 : gfc_add_block_to_block (&body, &maskse.pre);
5344 : :
5345 : 3874 : gfc_start_block (&block);
5346 : : }
5347 : : else
5348 : 6077 : gfc_init_block (&block);
5349 : :
5350 : : /* Compare with the current limit. */
5351 : 9951 : gfc_init_se (&arrayse, base_se);
5352 : 9951 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
5353 : 9951 : if (!nested_loop)
5354 : 7155 : arrayse.ss = arrayss;
5355 : 9951 : gfc_conv_expr_val (&arrayse, arrayexpr);
5356 : 9951 : gfc_add_block_to_block (&block, &arrayse.pre);
5357 : :
5358 : : /* We do the following if this is a more extreme value. */
5359 : 9951 : gfc_start_block (&ifblock);
5360 : :
5361 : : /* Assign the value to the limit... */
5362 : 9951 : gfc_add_modify (&ifblock, limit, arrayse.expr);
5363 : :
5364 : 9951 : if (nonempty == NULL && HONOR_NANS (DECL_MODE (limit)))
5365 : : {
5366 : 1569 : stmtblock_t ifblock2;
5367 : 1569 : tree ifbody2;
5368 : :
5369 : 1569 : gfc_start_block (&ifblock2);
5370 : 3439 : for (int i = 0; i < ploop->dimen; i++)
5371 : : {
5372 : 1870 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5373 : : ploop->loopvar[i], offset[i]);
5374 : 1870 : gfc_add_modify (&ifblock2, pos[i], tmp);
5375 : : }
5376 : 1569 : ifbody2 = gfc_finish_block (&ifblock2);
5377 : :
5378 : 1569 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5379 : : pos[0], gfc_index_zero_node);
5380 : 1569 : tmp = build3_v (COND_EXPR, cond, ifbody2,
5381 : : build_empty_stmt (input_location));
5382 : 1569 : gfc_add_expr_to_block (&block, tmp);
5383 : : }
5384 : :
5385 : 22177 : for (int i = 0; i < ploop->dimen; i++)
5386 : : {
5387 : 12226 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5388 : : ploop->loopvar[i], offset[i]);
5389 : 12226 : gfc_add_modify (&ifblock, pos[i], tmp);
5390 : 12226 : gfc_add_modify (&ifblock, idx[i], ploop->loopvar[i]);
5391 : : }
5392 : :
5393 : 9951 : gfc_add_modify (&ifblock, second_loop_entry, logical_true_node);
5394 : :
5395 : 9951 : if (lab1)
5396 : 6203 : gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab1));
5397 : :
5398 : 9951 : ifbody = gfc_finish_block (&ifblock);
5399 : :
5400 : 9951 : if (!lab1 || HONOR_NANS (DECL_MODE (limit)))
5401 : : {
5402 : 7646 : if (lab1)
5403 : 5998 : cond = fold_build2_loc (input_location,
5404 : : op == GT_EXPR ? GE_EXPR : LE_EXPR,
5405 : : logical_type_node, arrayse.expr, limit);
5406 : : else
5407 : : {
5408 : 3748 : tree ifbody2, elsebody2;
5409 : :
5410 : : /* We switch to > or >= depending on the value of the BACK argument. */
5411 : 3748 : cond = gfc_create_var (logical_type_node, "cond");
5412 : :
5413 : 3748 : gfc_start_block (&ifblock);
5414 : 5641 : b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5415 : : logical_type_node, arrayse.expr, limit);
5416 : :
5417 : 3748 : gfc_add_modify (&ifblock, cond, b_if);
5418 : 3748 : ifbody2 = gfc_finish_block (&ifblock);
5419 : :
5420 : 3748 : gfc_start_block (&elseblock);
5421 : 3748 : b_else = fold_build2_loc (input_location, op, logical_type_node,
5422 : : arrayse.expr, limit);
5423 : :
5424 : 3748 : gfc_add_modify (&elseblock, cond, b_else);
5425 : 3748 : elsebody2 = gfc_finish_block (&elseblock);
5426 : :
5427 : 3748 : tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5428 : : back, ifbody2, elsebody2);
5429 : :
5430 : 3748 : gfc_add_expr_to_block (&block, tmp);
5431 : : }
5432 : :
5433 : 7646 : cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5434 : 7646 : ifbody = build3_v (COND_EXPR, cond, ifbody,
5435 : : build_empty_stmt (input_location));
5436 : : }
5437 : 9951 : gfc_add_expr_to_block (&block, ifbody);
5438 : :
5439 : 9951 : if (maskexpr && maskexpr->rank > 0)
5440 : : {
5441 : : /* We enclose the above in if (mask) {...}. If the mask is an
5442 : : optional argument, generate IF (.NOT. PRESENT(MASK)
5443 : : .OR. MASK(I)). */
5444 : :
5445 : 3874 : tree ifmask;
5446 : 3874 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5447 : 3874 : tmp = gfc_finish_block (&block);
5448 : 3874 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5449 : : build_empty_stmt (input_location));
5450 : 3874 : }
5451 : : else
5452 : 6077 : tmp = gfc_finish_block (&block);
5453 : 9951 : gfc_add_expr_to_block (&body, tmp);
5454 : :
5455 : 9951 : if (lab1)
5456 : : {
5457 : 13747 : for (int i = 0; i < ploop->dimen; i++)
5458 : 7544 : ploop->from[i] = fold_build3_loc (input_location, COND_EXPR,
5459 : 7544 : TREE_TYPE (ploop->from[i]),
5460 : : second_loop_entry, idx[i],
5461 : : ploop->from[i]);
5462 : :
5463 : 6203 : gfc_trans_scalarized_loop_boundary (ploop, &body);
5464 : :
5465 : 6203 : if (nested_loop)
5466 : : {
5467 : : /* The first loop already advanced the parent se'ss chain, so clear
5468 : : the parent now to avoid doing it a second time, making the chain
5469 : : out of sync. */
5470 : 1858 : nested_se.parent = nullptr;
5471 : 1858 : nested_se.ss = orig_ss;
5472 : : }
5473 : :
5474 : 6203 : stmtblock_t * const outer_block = &ploop->code[ploop->dimen - 1];
5475 : :
5476 : 6203 : if (HONOR_NANS (DECL_MODE (limit)))
5477 : : {
5478 : 3898 : if (nonempty != NULL)
5479 : : {
5480 : 2329 : stmtblock_t init_block;
5481 : 2329 : gfc_init_block (&init_block);
5482 : :
5483 : 5229 : for (int i = 0; i < ploop->dimen; i++)
5484 : 2900 : gfc_add_modify (&init_block, pos[i], gfc_index_one_node);
5485 : :
5486 : 2329 : tree ifbody = gfc_finish_block (&init_block);
5487 : 2329 : tmp = build3_v (COND_EXPR, nonempty, ifbody,
5488 : : build_empty_stmt (input_location));
5489 : 2329 : gfc_add_expr_to_block (outer_block, tmp);
5490 : : }
5491 : : }
5492 : :
5493 : 6203 : gfc_add_expr_to_block (outer_block, build1_v (GOTO_EXPR, lab2));
5494 : 6203 : gfc_add_expr_to_block (outer_block, build1_v (LABEL_EXPR, lab1));
5495 : :
5496 : : /* If we have a mask, only check this element if the mask is set. */
5497 : 6203 : if (maskexpr && maskexpr->rank > 0)
5498 : : {
5499 : 3874 : gfc_init_se (&maskse, base_se);
5500 : 3874 : gfc_copy_loopinfo_to_se (&maskse, ploop);
5501 : 3874 : if (!nested_loop)
5502 : 2700 : maskse.ss = maskss;
5503 : 3874 : gfc_conv_expr_val (&maskse, maskexpr);
5504 : 3874 : gfc_add_block_to_block (&body, &maskse.pre);
5505 : :
5506 : 3874 : gfc_start_block (&block);
5507 : : }
5508 : : else
5509 : 2329 : gfc_init_block (&block);
5510 : :
5511 : : /* Compare with the current limit. */
5512 : 6203 : gfc_init_se (&arrayse, base_se);
5513 : 6203 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
5514 : 6203 : if (!nested_loop)
5515 : 4345 : arrayse.ss = arrayss;
5516 : 6203 : gfc_conv_expr_val (&arrayse, arrayexpr);
5517 : 6203 : gfc_add_block_to_block (&block, &arrayse.pre);
5518 : :
5519 : : /* We do the following if this is a more extreme value. */
5520 : 6203 : gfc_start_block (&ifblock);
5521 : :
5522 : : /* Assign the value to the limit... */
5523 : 6203 : gfc_add_modify (&ifblock, limit, arrayse.expr);
5524 : :
5525 : 13747 : for (int i = 0; i < ploop->dimen; i++)
5526 : : {
5527 : 7544 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5528 : : ploop->loopvar[i], offset[i]);
5529 : 7544 : gfc_add_modify (&ifblock, pos[i], tmp);
5530 : : }
5531 : :
5532 : 6203 : ifbody = gfc_finish_block (&ifblock);
5533 : :
5534 : : /* We switch to > or >= depending on the value of the BACK argument. */
5535 : 6203 : {
5536 : 6203 : tree ifbody2, elsebody2;
5537 : :
5538 : 6203 : cond = gfc_create_var (logical_type_node, "cond");
5539 : :
5540 : 6203 : gfc_start_block (&ifblock);
5541 : 9537 : b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5542 : : logical_type_node, arrayse.expr, limit);
5543 : :
5544 : 6203 : gfc_add_modify (&ifblock, cond, b_if);
5545 : 6203 : ifbody2 = gfc_finish_block (&ifblock);
5546 : :
5547 : 6203 : gfc_start_block (&elseblock);
5548 : 6203 : b_else = fold_build2_loc (input_location, op, logical_type_node,
5549 : : arrayse.expr, limit);
5550 : :
5551 : 6203 : gfc_add_modify (&elseblock, cond, b_else);
5552 : 6203 : elsebody2 = gfc_finish_block (&elseblock);
5553 : :
5554 : 6203 : tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5555 : : back, ifbody2, elsebody2);
5556 : : }
5557 : :
5558 : 6203 : gfc_add_expr_to_block (&block, tmp);
5559 : 6203 : cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5560 : 6203 : tmp = build3_v (COND_EXPR, cond, ifbody,
5561 : : build_empty_stmt (input_location));
5562 : :
5563 : 6203 : gfc_add_expr_to_block (&block, tmp);
5564 : :
5565 : 6203 : if (maskexpr && maskexpr->rank > 0)
5566 : : {
5567 : : /* We enclose the above in if (mask) {...}. If the mask is
5568 : : an optional argument, generate IF (.NOT. PRESENT(MASK)
5569 : : .OR. MASK(I)).*/
5570 : :
5571 : 3874 : tree ifmask;
5572 : 3874 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5573 : 3874 : tmp = gfc_finish_block (&block);
5574 : 3874 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5575 : : build_empty_stmt (input_location));
5576 : 3874 : }
5577 : : else
5578 : 2329 : tmp = gfc_finish_block (&block);
5579 : :
5580 : 6203 : gfc_add_expr_to_block (&body, tmp);
5581 : 6203 : gfc_add_modify (&body, second_loop_entry, logical_false_node);
5582 : : }
5583 : :
5584 : 9951 : gfc_trans_scalarizing_loops (ploop, &body);
5585 : :
5586 : 9951 : if (lab2)
5587 : 6203 : gfc_add_expr_to_block (&ploop->pre, build1_v (LABEL_EXPR, lab2));
5588 : :
5589 : : /* For a scalar mask, enclose the loop in an if statement. */
5590 : 9951 : if (maskexpr && maskexpr->rank == 0)
5591 : : {
5592 : 2644 : tree ifmask;
5593 : :
5594 : 2644 : gfc_init_se (&maskse, nested_loop ? se : nullptr);
5595 : 2644 : gfc_conv_expr_val (&maskse, maskexpr);
5596 : 2644 : gfc_add_block_to_block (&se->pre, &maskse.pre);
5597 : 2644 : gfc_init_block (&block);
5598 : 2644 : gfc_add_block_to_block (&block, &ploop->pre);
5599 : 2644 : gfc_add_block_to_block (&block, &ploop->post);
5600 : 2644 : tmp = gfc_finish_block (&block);
5601 : :
5602 : : /* For the else part of the scalar mask, just initialize
5603 : : the pos variable the same way as above. */
5604 : :
5605 : 2644 : gfc_init_block (&elseblock);
5606 : 5580 : for (int i = 0; i < ploop->dimen; i++)
5607 : 2936 : gfc_add_modify (&elseblock, pos[i], gfc_index_zero_node);
5608 : 2644 : elsetmp = gfc_finish_block (&elseblock);
5609 : 2644 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5610 : 2644 : tmp = build3_v (COND_EXPR, ifmask, tmp, elsetmp);
5611 : 2644 : gfc_add_expr_to_block (&block, tmp);
5612 : 2644 : gfc_add_block_to_block (&se->pre, &block);
5613 : 2644 : }
5614 : : else
5615 : : {
5616 : 7307 : gfc_add_block_to_block (&se->pre, &ploop->pre);
5617 : 7307 : gfc_add_block_to_block (&se->pre, &ploop->post);
5618 : : }
5619 : :
5620 : 9951 : if (!nested_loop)
5621 : 7155 : gfc_cleanup_loop (&loop);
5622 : :
5623 : 9951 : if (!dim_present)
5624 : : {
5625 : 8837 : for (int i = 0; i < arrayexpr->rank; i++)
5626 : : {
5627 : 5556 : tree res_idx = build_int_cst (gfc_array_index_type, i);
5628 : 5556 : tree res_arr_ref = gfc_build_array_ref (result_var, res_idx,
5629 : : NULL_TREE, true);
5630 : :
5631 : 5556 : tree value = convert (type, pos[i]);
5632 : 5556 : gfc_add_modify (&se->pre, res_arr_ref, value);
5633 : : }
5634 : :
5635 : 3281 : se->expr = result_var;
5636 : : }
5637 : : else
5638 : 6670 : se->expr = convert (type, pos[0]);
5639 : : }
5640 : :
5641 : : /* Emit code for findloc. */
5642 : :
5643 : : static void
5644 : 1332 : gfc_conv_intrinsic_findloc (gfc_se *se, gfc_expr *expr)
5645 : : {
5646 : 1332 : gfc_actual_arglist *array_arg, *value_arg, *dim_arg, *mask_arg,
5647 : : *kind_arg, *back_arg;
5648 : 1332 : gfc_expr *value_expr;
5649 : 1332 : int ikind;
5650 : 1332 : tree resvar;
5651 : 1332 : stmtblock_t block;
5652 : 1332 : stmtblock_t body;
5653 : 1332 : stmtblock_t loopblock;
5654 : 1332 : tree type;
5655 : 1332 : tree tmp;
5656 : 1332 : tree found;
5657 : 1332 : tree forward_branch = NULL_TREE;
5658 : 1332 : tree back_branch;
5659 : 1332 : gfc_loopinfo loop;
5660 : 1332 : gfc_ss *arrayss;
5661 : 1332 : gfc_ss *maskss;
5662 : 1332 : gfc_se arrayse;
5663 : 1332 : gfc_se valuese;
5664 : 1332 : gfc_se maskse;
5665 : 1332 : gfc_se backse;
5666 : 1332 : tree exit_label;
5667 : 1332 : gfc_expr *maskexpr;
5668 : 1332 : tree offset;
5669 : 1332 : int i;
5670 : 1332 : bool optional_mask;
5671 : :
5672 : 1332 : array_arg = expr->value.function.actual;
5673 : 1332 : value_arg = array_arg->next;
5674 : 1332 : dim_arg = value_arg->next;
5675 : 1332 : mask_arg = dim_arg->next;
5676 : 1332 : kind_arg = mask_arg->next;
5677 : 1332 : back_arg = kind_arg->next;
5678 : :
5679 : : /* Remove kind and set ikind. */
5680 : 1332 : if (kind_arg->expr)
5681 : : {
5682 : 0 : ikind = mpz_get_si (kind_arg->expr->value.integer);
5683 : 0 : gfc_free_expr (kind_arg->expr);
5684 : 0 : kind_arg->expr = NULL;
5685 : : }
5686 : : else
5687 : 1332 : ikind = gfc_default_integer_kind;
5688 : :
5689 : 1332 : value_expr = value_arg->expr;
5690 : :
5691 : : /* Unless it's a string, pass VALUE by value. */
5692 : 1332 : if (value_expr->ts.type != BT_CHARACTER)
5693 : 732 : value_arg->name = "%VAL";
5694 : :
5695 : : /* Pass BACK argument by value. */
5696 : 1332 : back_arg->name = "%VAL";
5697 : :
5698 : : /* Call the library if we have a character function or if
5699 : : rank > 0. */
5700 : 1332 : if (se->ss || array_arg->expr->ts.type == BT_CHARACTER)
5701 : : {
5702 : 1200 : se->ignore_optional = 1;
5703 : 1200 : if (expr->rank == 0)
5704 : : {
5705 : : /* Remove dim argument. */
5706 : 84 : gfc_free_expr (dim_arg->expr);
5707 : 84 : dim_arg->expr = NULL;
5708 : : }
5709 : 1200 : gfc_conv_intrinsic_funcall (se, expr);
5710 : 1200 : return;
5711 : : }
5712 : :
5713 : 132 : type = gfc_get_int_type (ikind);
5714 : :
5715 : : /* Initialize the result. */
5716 : 132 : resvar = gfc_create_var (gfc_array_index_type, "pos");
5717 : 132 : gfc_add_modify (&se->pre, resvar, build_int_cst (gfc_array_index_type, 0));
5718 : 132 : offset = gfc_create_var (gfc_array_index_type, "offset");
5719 : :
5720 : 132 : maskexpr = mask_arg->expr;
5721 : 72 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5722 : 60 : && maskexpr->symtree->n.sym->attr.dummy
5723 : 144 : && maskexpr->symtree->n.sym->attr.optional;
5724 : :
5725 : : /* Generate two loops, one for BACK=.true. and one for BACK=.false. */
5726 : :
5727 : 396 : for (i = 0 ; i < 2; i++)
5728 : : {
5729 : : /* Walk the arguments. */
5730 : 264 : arrayss = gfc_walk_expr (array_arg->expr);
5731 : 264 : gcc_assert (arrayss != gfc_ss_terminator);
5732 : :
5733 : 264 : if (maskexpr && maskexpr->rank != 0)
5734 : : {
5735 : 84 : maskss = gfc_walk_expr (maskexpr);
5736 : 84 : gcc_assert (maskss != gfc_ss_terminator);
5737 : : }
5738 : : else
5739 : : maskss = NULL;
5740 : :
5741 : : /* Initialize the scalarizer. */
5742 : 264 : gfc_init_loopinfo (&loop);
5743 : 264 : exit_label = gfc_build_label_decl (NULL_TREE);
5744 : 264 : TREE_USED (exit_label) = 1;
5745 : :
5746 : : /* We add the mask first because the number of iterations is
5747 : : taken from the last ss, and this breaks if an absent
5748 : : optional argument is used for mask. */
5749 : :
5750 : 264 : if (maskss)
5751 : 84 : gfc_add_ss_to_loop (&loop, maskss);
5752 : 264 : gfc_add_ss_to_loop (&loop, arrayss);
5753 : :
5754 : : /* Initialize the loop. */
5755 : 264 : gfc_conv_ss_startstride (&loop);
5756 : 264 : gfc_conv_loop_setup (&loop, &expr->where);
5757 : :
5758 : : /* Calculate the offset. */
5759 : 264 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5760 : : gfc_index_one_node, loop.from[0]);
5761 : 264 : gfc_add_modify (&loop.pre, offset, tmp);
5762 : :
5763 : 264 : gfc_mark_ss_chain_used (arrayss, 1);
5764 : 264 : if (maskss)
5765 : 84 : gfc_mark_ss_chain_used (maskss, 1);
5766 : :
5767 : : /* The first loop is for BACK=.true. */
5768 : 264 : if (i == 0)
5769 : 132 : loop.reverse[0] = GFC_REVERSE_SET;
5770 : :
5771 : : /* Generate the loop body. */
5772 : 264 : gfc_start_scalarized_body (&loop, &body);
5773 : :
5774 : : /* If we have an array mask, only add the element if it is
5775 : : set. */
5776 : 264 : if (maskss)
5777 : : {
5778 : 84 : gfc_init_se (&maskse, NULL);
5779 : 84 : gfc_copy_loopinfo_to_se (&maskse, &loop);
5780 : 84 : maskse.ss = maskss;
5781 : 84 : gfc_conv_expr_val (&maskse, maskexpr);
5782 : 84 : gfc_add_block_to_block (&body, &maskse.pre);
5783 : : }
5784 : :
5785 : : /* If the condition matches then set the return value. */
5786 : 264 : gfc_start_block (&block);
5787 : :
5788 : : /* Add the offset. */
5789 : 264 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5790 : 264 : TREE_TYPE (resvar),
5791 : : loop.loopvar[0], offset);
5792 : 264 : gfc_add_modify (&block, resvar, tmp);
5793 : : /* And break out of the loop. */
5794 : 264 : tmp = build1_v (GOTO_EXPR, exit_label);
5795 : 264 : gfc_add_expr_to_block (&block, tmp);
5796 : :
5797 : 264 : found = gfc_finish_block (&block);
5798 : :
5799 : : /* Check this element. */
5800 : 264 : gfc_init_se (&arrayse, NULL);
5801 : 264 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
5802 : 264 : arrayse.ss = arrayss;
5803 : 264 : gfc_conv_expr_val (&arrayse, array_arg->expr);
5804 : 264 : gfc_add_block_to_block (&body, &arrayse.pre);
5805 : :
5806 : 264 : gfc_init_se (&valuese, NULL);
5807 : 264 : gfc_conv_expr_val (&valuese, value_arg->expr);
5808 : 264 : gfc_add_block_to_block (&body, &valuese.pre);
5809 : :
5810 : 264 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5811 : : arrayse.expr, valuese.expr);
5812 : :
5813 : 264 : tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
5814 : 264 : if (maskss)
5815 : : {
5816 : : /* We enclose the above in if (mask) {...}. If the mask is
5817 : : an optional argument, generate IF (.NOT. PRESENT(MASK)
5818 : : .OR. MASK(I)). */
5819 : :
5820 : 84 : tree ifmask;
5821 : 84 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5822 : 84 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5823 : : build_empty_stmt (input_location));
5824 : : }
5825 : :
5826 : 264 : gfc_add_expr_to_block (&body, tmp);
5827 : 264 : gfc_add_block_to_block (&body, &arrayse.post);
5828 : :
5829 : 264 : gfc_trans_scalarizing_loops (&loop, &body);
5830 : :
5831 : : /* Add the exit label. */
5832 : 264 : tmp = build1_v (LABEL_EXPR, exit_label);
5833 : 264 : gfc_add_expr_to_block (&loop.pre, tmp);
5834 : 264 : gfc_start_block (&loopblock);
5835 : 264 : gfc_add_block_to_block (&loopblock, &loop.pre);
5836 : 264 : gfc_add_block_to_block (&loopblock, &loop.post);
5837 : 264 : if (i == 0)
5838 : 132 : forward_branch = gfc_finish_block (&loopblock);
5839 : : else
5840 : 132 : back_branch = gfc_finish_block (&loopblock);
5841 : :
5842 : 264 : gfc_cleanup_loop (&loop);
5843 : : }
5844 : :
5845 : : /* Enclose the two loops in an IF statement. */
5846 : :
5847 : 132 : gfc_init_se (&backse, NULL);
5848 : 132 : gfc_conv_expr_val (&backse, back_arg->expr);
5849 : 132 : gfc_add_block_to_block (&se->pre, &backse.pre);
5850 : 132 : tmp = build3_v (COND_EXPR, backse.expr, forward_branch, back_branch);
5851 : :
5852 : : /* For a scalar mask, enclose the loop in an if statement. */
5853 : 132 : if (maskexpr && maskss == NULL)
5854 : : {
5855 : 30 : tree ifmask;
5856 : 30 : tree if_stmt;
5857 : :
5858 : 30 : gfc_init_se (&maskse, NULL);
5859 : 30 : gfc_conv_expr_val (&maskse, maskexpr);
5860 : 30 : gfc_init_block (&block);
5861 : 30 : gfc_add_expr_to_block (&block, maskse.expr);
5862 : 30 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5863 : 30 : if_stmt = build3_v (COND_EXPR, ifmask, tmp,
5864 : : build_empty_stmt (input_location));
5865 : 30 : gfc_add_expr_to_block (&block, if_stmt);
5866 : 30 : tmp = gfc_finish_block (&block);
5867 : : }
5868 : :
5869 : 132 : gfc_add_expr_to_block (&se->pre, tmp);
5870 : 132 : se->expr = convert (type, resvar);
5871 : :
5872 : : }
5873 : :
5874 : : /* Emit code for fstat, lstat and stat intrinsic subroutines. */
5875 : :
5876 : : static tree
5877 : 55 : conv_intrinsic_fstat_lstat_stat_sub (gfc_code *code)
5878 : : {
5879 : 55 : stmtblock_t block;
5880 : 55 : gfc_se se, se_stat;
5881 : 55 : tree unit = NULL_TREE;
5882 : 55 : tree name = NULL_TREE;
5883 : 55 : tree slen = NULL_TREE;
5884 : 55 : tree vals;
5885 : 55 : tree arg3 = NULL_TREE;
5886 : 55 : tree stat = NULL_TREE ;
5887 : 55 : tree present = NULL_TREE;
5888 : 55 : tree tmp;
5889 : 55 : int kind;
5890 : :
5891 : 55 : gfc_init_block (&block);
5892 : 55 : gfc_init_se (&se, NULL);
5893 : :
5894 : 55 : switch (code->resolved_isym->id)
5895 : : {
5896 : 21 : case GFC_ISYM_FSTAT:
5897 : : /* Deal with the UNIT argument. */
5898 : 21 : gfc_conv_expr (&se, code->ext.actual->expr);
5899 : 21 : gfc_add_block_to_block (&block, &se.pre);
5900 : 21 : unit = gfc_evaluate_now (se.expr, &block);
5901 : 21 : unit = gfc_build_addr_expr (NULL_TREE, unit);
5902 : 21 : gfc_add_block_to_block (&block, &se.post);
5903 : 21 : break;
5904 : :
5905 : 34 : case GFC_ISYM_LSTAT:
5906 : 34 : case GFC_ISYM_STAT:
5907 : : /* Deal with the NAME argument. */
5908 : 34 : gfc_conv_expr (&se, code->ext.actual->expr);
5909 : 34 : gfc_conv_string_parameter (&se);
5910 : 34 : gfc_add_block_to_block (&block, &se.pre);
5911 : 34 : name = se.expr;
5912 : 34 : slen = se.string_length;
5913 : 34 : gfc_add_block_to_block (&block, &se.post);
5914 : 34 : break;
5915 : :
5916 : 0 : default:
5917 : 0 : gcc_unreachable ();
5918 : : }
5919 : :
5920 : : /* Deal with the VALUES argument. */
5921 : 55 : gfc_init_se (&se, NULL);
5922 : 55 : gfc_conv_expr_descriptor (&se, code->ext.actual->next->expr);
5923 : 55 : vals = gfc_build_addr_expr (NULL_TREE, se.expr);
5924 : 55 : gfc_add_block_to_block (&block, &se.pre);
5925 : 55 : gfc_add_block_to_block (&block, &se.post);
5926 : 55 : kind = code->ext.actual->next->expr->ts.kind;
5927 : :
5928 : : /* Deal with an optional STATUS. */
5929 : 55 : if (code->ext.actual->next->next->expr)
5930 : : {
5931 : 45 : gfc_init_se (&se_stat, NULL);
5932 : 45 : gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
5933 : 45 : stat = gfc_create_var (gfc_get_int_type (kind), "_stat");
5934 : 45 : arg3 = gfc_build_addr_expr (NULL_TREE, stat);
5935 : :
5936 : : /* Handle case of status being an optional dummy. */
5937 : 45 : gfc_symbol *sym = code->ext.actual->next->next->expr->symtree->n.sym;
5938 : 45 : if (sym->attr.dummy && sym->attr.optional)
5939 : : {
5940 : 6 : present = gfc_conv_expr_present (sym);
5941 : 12 : arg3 = fold_build3_loc (input_location, COND_EXPR,
5942 : 6 : TREE_TYPE (arg3), present, arg3,
5943 : 6 : fold_convert (TREE_TYPE (arg3),
5944 : : null_pointer_node));
5945 : : }
5946 : : }
5947 : :
5948 : : /* Call library function depending on KIND of VALUES argument. */
5949 : 55 : switch (code->resolved_isym->id)
5950 : : {
5951 : 21 : case GFC_ISYM_FSTAT:
5952 : 21 : tmp = (kind == 4 ? gfor_fndecl_fstat_i4_sub : gfor_fndecl_fstat_i8_sub);
5953 : : break;
5954 : 14 : case GFC_ISYM_LSTAT:
5955 : 14 : tmp = (kind == 4 ? gfor_fndecl_lstat_i4_sub : gfor_fndecl_lstat_i8_sub);
5956 : : break;
5957 : 20 : case GFC_ISYM_STAT:
5958 : 20 : tmp = (kind == 4 ? gfor_fndecl_stat_i4_sub : gfor_fndecl_stat_i8_sub);
5959 : : break;
5960 : 0 : default:
5961 : 0 : gcc_unreachable ();
5962 : : }
5963 : :
5964 : 55 : if (code->resolved_isym->id == GFC_ISYM_FSTAT)
5965 : 21 : tmp = build_call_expr_loc (input_location, tmp, 3, unit, vals,
5966 : : stat ? arg3 : null_pointer_node);
5967 : : else
5968 : 34 : tmp = build_call_expr_loc (input_location, tmp, 4, name, vals,
5969 : : stat ? arg3 : null_pointer_node, slen);
5970 : 55 : gfc_add_expr_to_block (&block, tmp);
5971 : :
5972 : : /* Handle kind conversion of status. */
5973 : 55 : if (stat && stat != se_stat.expr)
5974 : : {
5975 : 45 : stmtblock_t block2;
5976 : :
5977 : 45 : gfc_init_block (&block2);
5978 : 45 : gfc_add_modify (&block2, se_stat.expr,
5979 : 45 : fold_convert (TREE_TYPE (se_stat.expr), stat));
5980 : :
5981 : 45 : if (present)
5982 : : {
5983 : 6 : tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block2),
5984 : : build_empty_stmt (input_location));
5985 : 6 : gfc_add_expr_to_block (&block, tmp);
5986 : : }
5987 : : else
5988 : 39 : gfc_add_block_to_block (&block, &block2);
5989 : : }
5990 : :
5991 : 55 : return gfc_finish_block (&block);
5992 : : }
5993 : :
5994 : : /* Emit code for minval or maxval intrinsic. There are many different cases
5995 : : we need to handle. For performance reasons we sometimes create two
5996 : : loops instead of one, where the second one is much simpler.
5997 : : Examples for minval intrinsic:
5998 : : 1) Result is an array, a call is generated
5999 : : 2) Array mask is used and NaNs need to be supported, rank 1:
6000 : : limit = Infinity;
6001 : : nonempty = false;
6002 : : S = from;
6003 : : while (S <= to) {
6004 : : if (mask[S]) {
6005 : : nonempty = true;
6006 : : if (a[S] <= limit) {
6007 : : limit = a[S];
6008 : : S++;
6009 : : goto lab;
6010 : : }
6011 : : else
6012 : : S++;
6013 : : }
6014 : : }
6015 : : limit = nonempty ? NaN : huge (limit);
6016 : : lab:
6017 : : while (S <= to) { if(mask[S]) limit = min (a[S], limit); S++; }
6018 : : 3) NaNs need to be supported, but it is known at compile time or cheaply
6019 : : at runtime whether array is nonempty or not, rank 1:
6020 : : limit = Infinity;
6021 : : S = from;
6022 : : while (S <= to) {
6023 : : if (a[S] <= limit) {
6024 : : limit = a[S];
6025 : : S++;
6026 : : goto lab;
6027 : : }
6028 : : else
6029 : : S++;
6030 : : }
6031 : : limit = (from <= to) ? NaN : huge (limit);
6032 : : lab:
6033 : : while (S <= to) { limit = min (a[S], limit); S++; }
6034 : : 4) Array mask is used and NaNs need to be supported, rank > 1:
6035 : : limit = Infinity;
6036 : : nonempty = false;
6037 : : fast = false;
6038 : : S1 = from1;
6039 : : while (S1 <= to1) {
6040 : : S2 = from2;
6041 : : while (S2 <= to2) {
6042 : : if (mask[S1][S2]) {
6043 : : if (fast) limit = min (a[S1][S2], limit);
6044 : : else {
6045 : : nonempty = true;
6046 : : if (a[S1][S2] <= limit) {
6047 : : limit = a[S1][S2];
6048 : : fast = true;
6049 : : }
6050 : : }
6051 : : }
6052 : : S2++;
6053 : : }
6054 : : S1++;
6055 : : }
6056 : : if (!fast)
6057 : : limit = nonempty ? NaN : huge (limit);
6058 : : 5) NaNs need to be supported, but it is known at compile time or cheaply
6059 : : at runtime whether array is nonempty or not, rank > 1:
6060 : : limit = Infinity;
6061 : : fast = false;
6062 : : S1 = from1;
6063 : : while (S1 <= to1) {
6064 : : S2 = from2;
6065 : : while (S2 <= to2) {
6066 : : if (fast) limit = min (a[S1][S2], limit);
6067 : : else {
6068 : : if (a[S1][S2] <= limit) {
6069 : : limit = a[S1][S2];
6070 : : fast = true;
6071 : : }
6072 : : }
6073 : : S2++;
6074 : : }
6075 : : S1++;
6076 : : }
6077 : : if (!fast)
6078 : : limit = (nonempty_array) ? NaN : huge (limit);
6079 : : 6) NaNs aren't supported, but infinities are. Array mask is used:
6080 : : limit = Infinity;
6081 : : nonempty = false;
6082 : : S = from;
6083 : : while (S <= to) {
6084 : : if (mask[S]) { nonempty = true; limit = min (a[S], limit); }
6085 : : S++;
6086 : : }
6087 : : limit = nonempty ? limit : huge (limit);
6088 : : 7) Same without array mask:
6089 : : limit = Infinity;
6090 : : S = from;
6091 : : while (S <= to) { limit = min (a[S], limit); S++; }
6092 : : limit = (from <= to) ? limit : huge (limit);
6093 : : 8) Neither NaNs nor infinities are supported (-ffast-math or BT_INTEGER):
6094 : : limit = huge (limit);
6095 : : S = from;
6096 : : while (S <= to) { limit = min (a[S], limit); S++); }
6097 : : (or
6098 : : while (S <= to) { if (mask[S]) limit = min (a[S], limit); S++; }
6099 : : with array mask instead).
6100 : : For 3), 5), 7) and 8), if mask is scalar, this all goes into a conditional,
6101 : : setting limit = huge (limit); in the else branch. */
6102 : :
6103 : : static void
6104 : 2415 : gfc_conv_intrinsic_minmaxval (gfc_se * se, gfc_expr * expr, enum tree_code op)
6105 : : {
6106 : 2415 : tree limit;
6107 : 2415 : tree type;
6108 : 2415 : tree tmp;
6109 : 2415 : tree ifbody;
6110 : 2415 : tree nonempty;
6111 : 2415 : tree nonempty_var;
6112 : 2415 : tree lab;
6113 : 2415 : tree fast;
6114 : 2415 : tree huge_cst = NULL, nan_cst = NULL;
6115 : 2415 : stmtblock_t body;
6116 : 2415 : stmtblock_t block, block2;
6117 : 2415 : gfc_loopinfo loop;
6118 : 2415 : gfc_actual_arglist *actual;
6119 : 2415 : gfc_ss *arrayss;
6120 : 2415 : gfc_ss *maskss;
6121 : 2415 : gfc_se arrayse;
6122 : 2415 : gfc_se maskse;
6123 : 2415 : gfc_expr *arrayexpr;
6124 : 2415 : gfc_expr *maskexpr;
6125 : 2415 : int n;
6126 : 2415 : bool optional_mask;
6127 : :
6128 : 2415 : if (se->ss)
6129 : : {
6130 : 0 : gfc_conv_intrinsic_funcall (se, expr);
6131 : 186 : return;
6132 : : }
6133 : :
6134 : 2415 : actual = expr->value.function.actual;
6135 : 2415 : arrayexpr = actual->expr;
6136 : :
6137 : 2415 : if (arrayexpr->ts.type == BT_CHARACTER)
6138 : : {
6139 : 186 : gfc_actual_arglist *dim = actual->next;
6140 : 186 : if (expr->rank == 0 && dim->expr != 0)
6141 : : {
6142 : 6 : gfc_free_expr (dim->expr);
6143 : 6 : dim->expr = NULL;
6144 : : }
6145 : 186 : gfc_conv_intrinsic_funcall (se, expr);
6146 : 186 : return;
6147 : : }
6148 : :
6149 : 2229 : type = gfc_typenode_for_spec (&expr->ts);
6150 : : /* Initialize the result. */
6151 : 2229 : limit = gfc_create_var (type, "limit");
6152 : 2229 : n = gfc_validate_kind (expr->ts.type, expr->ts.kind, false);
6153 : 2229 : switch (expr->ts.type)
6154 : : {
6155 : 1244 : case BT_REAL:
6156 : 1244 : huge_cst = gfc_conv_mpfr_to_tree (gfc_real_kinds[n].huge,
6157 : : expr->ts.kind, 0);
6158 : 1244 : if (HONOR_INFINITIES (DECL_MODE (limit)))
6159 : : {
6160 : 1240 : REAL_VALUE_TYPE real;
6161 : 1240 : real_inf (&real);
6162 : 1240 : tmp = build_real (type, real);
6163 : : }
6164 : : else
6165 : : tmp = huge_cst;
6166 : 1244 : if (HONOR_NANS (DECL_MODE (limit)))
6167 : 1240 : nan_cst = gfc_build_nan (type, "");
6168 : : break;
6169 : :
6170 : 955 : case BT_INTEGER:
6171 : 955 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge, expr->ts.kind);
6172 : 955 : break;
6173 : :
6174 : 30 : case BT_UNSIGNED:
6175 : : /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
6176 : 30 : if (op == GT_EXPR)
6177 : 18 : tmp = build_int_cst (type, 0);
6178 : : else
6179 : 12 : tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
6180 : : expr->ts.kind);
6181 : : break;
6182 : :
6183 : 0 : default:
6184 : 0 : gcc_unreachable ();
6185 : : }
6186 : :
6187 : : /* We start with the most negative possible value for MAXVAL, and the most
6188 : : positive possible value for MINVAL. The most negative possible value is
6189 : : -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
6190 : : possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
6191 : : with above. */
6192 : 2229 : if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
6193 : : {
6194 : 985 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
6195 : 985 : if (huge_cst)
6196 : 559 : huge_cst = fold_build1_loc (input_location, NEGATE_EXPR,
6197 : 559 : TREE_TYPE (huge_cst), huge_cst);
6198 : : }
6199 : :
6200 : 1003 : if (op == GT_EXPR && expr->ts.type == BT_INTEGER)
6201 : 426 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
6202 : : tmp, build_int_cst (type, 1));
6203 : :
6204 : 2229 : gfc_add_modify (&se->pre, limit, tmp);
6205 : :
6206 : : /* Walk the arguments. */
6207 : 2229 : arrayss = gfc_walk_expr (arrayexpr);
6208 : 2229 : gcc_assert (arrayss != gfc_ss_terminator);
6209 : :
6210 : 2229 : actual = actual->next->next;
6211 : 2229 : gcc_assert (actual);
6212 : 2229 : maskexpr = actual->expr;
6213 : 1572 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
6214 : 1560 : && maskexpr->symtree->n.sym->attr.dummy
6215 : 2241 : && maskexpr->symtree->n.sym->attr.optional;
6216 : 1560 : nonempty = NULL;
6217 : 1572 : if (maskexpr && maskexpr->rank != 0)
6218 : : {
6219 : 1026 : maskss = gfc_walk_expr (maskexpr);
6220 : 1026 : gcc_assert (maskss != gfc_ss_terminator);
6221 : : }
6222 : : else
6223 : : {
6224 : 1203 : mpz_t asize;
6225 : 1203 : if (gfc_array_size (arrayexpr, &asize))
6226 : : {
6227 : 678 : nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
6228 : 678 : mpz_clear (asize);
6229 : 678 : nonempty = fold_build2_loc (input_location, GT_EXPR,
6230 : : logical_type_node, nonempty,
6231 : : gfc_index_zero_node);
6232 : : }
6233 : 1203 : maskss = NULL;
6234 : : }
6235 : :
6236 : : /* Initialize the scalarizer. */
6237 : 2229 : gfc_init_loopinfo (&loop);
6238 : :
6239 : : /* We add the mask first because the number of iterations is taken
6240 : : from the last ss, and this breaks if an absent optional argument
6241 : : is used for mask. */
6242 : :
6243 : 2229 : if (maskss)
6244 : 1026 : gfc_add_ss_to_loop (&loop, maskss);
6245 : 2229 : gfc_add_ss_to_loop (&loop, arrayss);
6246 : :
6247 : : /* Initialize the loop. */
6248 : 2229 : gfc_conv_ss_startstride (&loop);
6249 : :
6250 : : /* The code generated can have more than one loop in sequence (see the
6251 : : comment at the function header). This doesn't work well with the
6252 : : scalarizer, which changes arrays' offset when the scalarization loops
6253 : : are generated (see gfc_trans_preloop_setup). Fortunately, {min,max}val
6254 : : are currently inlined in the scalar case only. As there is no dependency
6255 : : to care about in that case, there is no temporary, so that we can use the
6256 : : scalarizer temporary code to handle multiple loops. Thus, we set temp_dim
6257 : : here, we call gfc_mark_ss_chain_used with flag=3 later, and we use
6258 : : gfc_trans_scalarized_loop_boundary even later to restore offset.
6259 : : TODO: this prevents inlining of rank > 0 minmaxval calls, so this
6260 : : should eventually go away. We could either create two loops properly,
6261 : : or find another way to save/restore the array offsets between the two
6262 : : loops (without conflicting with temporary management), or use a single
6263 : : loop minmaxval implementation. See PR 31067. */
6264 : 2229 : loop.temp_dim = loop.dimen;
6265 : 2229 : gfc_conv_loop_setup (&loop, &expr->where);
6266 : :
6267 : 2229 : if (nonempty == NULL && maskss == NULL
6268 : 525 : && loop.dimen == 1 && loop.from[0] && loop.to[0])
6269 : 489 : nonempty = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
6270 : : loop.from[0], loop.to[0]);
6271 : 2229 : nonempty_var = NULL;
6272 : 2229 : if (nonempty == NULL
6273 : 2229 : && (HONOR_INFINITIES (DECL_MODE (limit))
6274 : 480 : || HONOR_NANS (DECL_MODE (limit))))
6275 : : {
6276 : 582 : nonempty_var = gfc_create_var (logical_type_node, "nonempty");
6277 : 582 : gfc_add_modify (&se->pre, nonempty_var, logical_false_node);
6278 : 582 : nonempty = nonempty_var;
6279 : : }
6280 : 2229 : lab = NULL;
6281 : 2229 : fast = NULL;
6282 : 2229 : if (HONOR_NANS (DECL_MODE (limit)))
6283 : : {
6284 : 1240 : if (loop.dimen == 1)
6285 : : {
6286 : 820 : lab = gfc_build_label_decl (NULL_TREE);
6287 : 820 : TREE_USED (lab) = 1;
6288 : : }
6289 : : else
6290 : : {
6291 : 420 : fast = gfc_create_var (logical_type_node, "fast");
6292 : 420 : gfc_add_modify (&se->pre, fast, logical_false_node);
6293 : : }
6294 : : }
6295 : :
6296 : 2229 : gfc_mark_ss_chain_used (arrayss, lab ? 3 : 1);
6297 : 2229 : if (maskss)
6298 : 1704 : gfc_mark_ss_chain_used (maskss, lab ? 3 : 1);
6299 : : /* Generate the loop body. */
6300 : 2229 : gfc_start_scalarized_body (&loop, &body);
6301 : :
6302 : : /* If we have a mask, only add this element if the mask is set. */
6303 : 2229 : if (maskss)
6304 : : {
6305 : 1026 : gfc_init_se (&maskse, NULL);
6306 : 1026 : gfc_copy_loopinfo_to_se (&maskse, &loop);
6307 : 1026 : maskse.ss = maskss;
6308 : 1026 : gfc_conv_expr_val (&maskse, maskexpr);
6309 : 1026 : gfc_add_block_to_block (&body, &maskse.pre);
6310 : :
6311 : 1026 : gfc_start_block (&block);
6312 : : }
6313 : : else
6314 : 1203 : gfc_init_block (&block);
6315 : :
6316 : : /* Compare with the current limit. */
6317 : 2229 : gfc_init_se (&arrayse, NULL);
6318 : 2229 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
6319 : 2229 : arrayse.ss = arrayss;
6320 : 2229 : gfc_conv_expr_val (&arrayse, arrayexpr);
6321 : 2229 : arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6322 : 2229 : gfc_add_block_to_block (&block, &arrayse.pre);
6323 : :
6324 : 2229 : gfc_init_block (&block2);
6325 : :
6326 : 2229 : if (nonempty_var)
6327 : 582 : gfc_add_modify (&block2, nonempty_var, logical_true_node);
6328 : :
6329 : 2229 : if (HONOR_NANS (DECL_MODE (limit)))
6330 : : {
6331 : 1921 : tmp = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
6332 : : logical_type_node, arrayse.expr, limit);
6333 : 1240 : if (lab)
6334 : : {
6335 : 820 : stmtblock_t ifblock;
6336 : 820 : tree inc_loop;
6337 : 820 : inc_loop = fold_build2_loc (input_location, PLUS_EXPR,
6338 : 820 : TREE_TYPE (loop.loopvar[0]),
6339 : : loop.loopvar[0], gfc_index_one_node);
6340 : 820 : gfc_init_block (&ifblock);
6341 : 820 : gfc_add_modify (&ifblock, limit, arrayse.expr);
6342 : 820 : gfc_add_modify (&ifblock, loop.loopvar[0], inc_loop);
6343 : 820 : gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab));
6344 : 820 : ifbody = gfc_finish_block (&ifblock);
6345 : : }
6346 : : else
6347 : : {
6348 : 420 : stmtblock_t ifblock;
6349 : :
6350 : 420 : gfc_init_block (&ifblock);
6351 : 420 : gfc_add_modify (&ifblock, limit, arrayse.expr);
6352 : 420 : gfc_add_modify (&ifblock, fast, logical_true_node);
6353 : 420 : ifbody = gfc_finish_block (&ifblock);
6354 : : }
6355 : 1240 : tmp = build3_v (COND_EXPR, tmp, ifbody,
6356 : : build_empty_stmt (input_location));
6357 : 1240 : gfc_add_expr_to_block (&block2, tmp);
6358 : : }
6359 : : else
6360 : : {
6361 : : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6362 : : signed zeros. */
6363 : 1534 : tmp = fold_build2_loc (input_location,
6364 : : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6365 : : type, arrayse.expr, limit);
6366 : 989 : gfc_add_modify (&block2, limit, tmp);
6367 : : }
6368 : :
6369 : 2229 : if (fast)
6370 : : {
6371 : 420 : tree elsebody = gfc_finish_block (&block2);
6372 : :
6373 : : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6374 : : signed zeros. */
6375 : 420 : if (HONOR_NANS (DECL_MODE (limit)))
6376 : : {
6377 : 420 : tmp = fold_build2_loc (input_location, op, logical_type_node,
6378 : : arrayse.expr, limit);
6379 : 420 : ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6380 : 420 : ifbody = build3_v (COND_EXPR, tmp, ifbody,
6381 : : build_empty_stmt (input_location));
6382 : : }
6383 : : else
6384 : : {
6385 : 0 : tmp = fold_build2_loc (input_location,
6386 : : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6387 : : type, arrayse.expr, limit);
6388 : 0 : ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6389 : : }
6390 : 420 : tmp = build3_v (COND_EXPR, fast, ifbody, elsebody);
6391 : 420 : gfc_add_expr_to_block (&block, tmp);
6392 : : }
6393 : : else
6394 : 1809 : gfc_add_block_to_block (&block, &block2);
6395 : :
6396 : 2229 : gfc_add_block_to_block (&block, &arrayse.post);
6397 : :
6398 : 2229 : tmp = gfc_finish_block (&block);
6399 : 2229 : if (maskss)
6400 : : {
6401 : : /* We enclose the above in if (mask) {...}. If the mask is an
6402 : : optional argument, generate IF (.NOT. PRESENT(MASK)
6403 : : .OR. MASK(I)). */
6404 : 1026 : tree ifmask;
6405 : 1026 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6406 : 1026 : tmp = build3_v (COND_EXPR, ifmask, tmp,
6407 : : build_empty_stmt (input_location));
6408 : : }
6409 : 2229 : gfc_add_expr_to_block (&body, tmp);
6410 : :
6411 : 2229 : if (lab)
6412 : : {
6413 : 820 : gfc_trans_scalarized_loop_boundary (&loop, &body);
6414 : :
6415 : 820 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6416 : : nan_cst, huge_cst);
6417 : 820 : gfc_add_modify (&loop.code[0], limit, tmp);
6418 : 820 : gfc_add_expr_to_block (&loop.code[0], build1_v (LABEL_EXPR, lab));
6419 : :
6420 : : /* If we have a mask, only add this element if the mask is set. */
6421 : 820 : if (maskss)
6422 : : {
6423 : 348 : gfc_init_se (&maskse, NULL);
6424 : 348 : gfc_copy_loopinfo_to_se (&maskse, &loop);
6425 : 348 : maskse.ss = maskss;
6426 : 348 : gfc_conv_expr_val (&maskse, maskexpr);
6427 : 348 : gfc_add_block_to_block (&body, &maskse.pre);
6428 : :
6429 : 348 : gfc_start_block (&block);
6430 : : }
6431 : : else
6432 : 472 : gfc_init_block (&block);
6433 : :
6434 : : /* Compare with the current limit. */
6435 : 820 : gfc_init_se (&arrayse, NULL);
6436 : 820 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
6437 : 820 : arrayse.ss = arrayss;
6438 : 820 : gfc_conv_expr_val (&arrayse, arrayexpr);
6439 : 820 : arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6440 : 820 : gfc_add_block_to_block (&block, &arrayse.pre);
6441 : :
6442 : : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6443 : : signed zeros. */
6444 : 820 : if (HONOR_NANS (DECL_MODE (limit)))
6445 : : {
6446 : 820 : tmp = fold_build2_loc (input_location, op, logical_type_node,
6447 : : arrayse.expr, limit);
6448 : 820 : ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6449 : 820 : tmp = build3_v (COND_EXPR, tmp, ifbody,
6450 : : build_empty_stmt (input_location));
6451 : 820 : gfc_add_expr_to_block (&block, tmp);
6452 : : }
6453 : : else
6454 : : {
6455 : 0 : tmp = fold_build2_loc (input_location,
6456 : : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6457 : : type, arrayse.expr, limit);
6458 : 0 : gfc_add_modify (&block, limit, tmp);
6459 : : }
6460 : :
6461 : 820 : gfc_add_block_to_block (&block, &arrayse.post);
6462 : :
6463 : 820 : tmp = gfc_finish_block (&block);
6464 : 820 : if (maskss)
6465 : : /* We enclose the above in if (mask) {...}. */
6466 : : {
6467 : 348 : tree ifmask;
6468 : 348 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6469 : 348 : tmp = build3_v (COND_EXPR, ifmask, tmp,
6470 : : build_empty_stmt (input_location));
6471 : : }
6472 : :
6473 : 820 : gfc_add_expr_to_block (&body, tmp);
6474 : : /* Avoid initializing loopvar[0] again, it should be left where
6475 : : it finished by the first loop. */
6476 : 820 : loop.from[0] = loop.loopvar[0];
6477 : : }
6478 : 2229 : gfc_trans_scalarizing_loops (&loop, &body);
6479 : :
6480 : 2229 : if (fast)
6481 : : {
6482 : 420 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6483 : : nan_cst, huge_cst);
6484 : 420 : ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6485 : 420 : tmp = build3_v (COND_EXPR, fast, build_empty_stmt (input_location),
6486 : : ifbody);
6487 : 420 : gfc_add_expr_to_block (&loop.pre, tmp);
6488 : : }
6489 : 1809 : else if (HONOR_INFINITIES (DECL_MODE (limit)) && !lab)
6490 : : {
6491 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty, limit,
6492 : : huge_cst);
6493 : 0 : gfc_add_modify (&loop.pre, limit, tmp);
6494 : : }
6495 : :
6496 : : /* For a scalar mask, enclose the loop in an if statement. */
6497 : 2229 : if (maskexpr && maskss == NULL)
6498 : : {
6499 : 546 : tree else_stmt;
6500 : 546 : tree ifmask;
6501 : :
6502 : 546 : gfc_init_se (&maskse, NULL);
6503 : 546 : gfc_conv_expr_val (&maskse, maskexpr);
6504 : 546 : gfc_init_block (&block);
6505 : 546 : gfc_add_block_to_block (&block, &loop.pre);
6506 : 546 : gfc_add_block_to_block (&block, &loop.post);
6507 : 546 : tmp = gfc_finish_block (&block);
6508 : :
6509 : 546 : if (HONOR_INFINITIES (DECL_MODE (limit)))
6510 : 354 : else_stmt = build2_v (MODIFY_EXPR, limit, huge_cst);
6511 : : else
6512 : 192 : else_stmt = build_empty_stmt (input_location);
6513 : :
6514 : 546 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6515 : 546 : tmp = build3_v (COND_EXPR, ifmask, tmp, else_stmt);
6516 : 546 : gfc_add_expr_to_block (&block, tmp);
6517 : 546 : gfc_add_block_to_block (&se->pre, &block);
6518 : : }
6519 : : else
6520 : : {
6521 : 1683 : gfc_add_block_to_block (&se->pre, &loop.pre);
6522 : 1683 : gfc_add_block_to_block (&se->pre, &loop.post);
6523 : : }
6524 : :
6525 : 2229 : gfc_cleanup_loop (&loop);
6526 : :
6527 : 2229 : se->expr = limit;
6528 : : }
6529 : :
6530 : : /* BTEST (i, pos) = (i & (1 << pos)) != 0. */
6531 : : static void
6532 : 145 : gfc_conv_intrinsic_btest (gfc_se * se, gfc_expr * expr)
6533 : : {
6534 : 145 : tree args[2];
6535 : 145 : tree type;
6536 : 145 : tree tmp;
6537 : :
6538 : 145 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6539 : 145 : type = TREE_TYPE (args[0]);
6540 : :
6541 : : /* Optionally generate code for runtime argument check. */
6542 : 145 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6543 : : {
6544 : 6 : tree below = fold_build2_loc (input_location, LT_EXPR,
6545 : : logical_type_node, args[1],
6546 : 6 : build_int_cst (TREE_TYPE (args[1]), 0));
6547 : 6 : tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6548 : 6 : tree above = fold_build2_loc (input_location, GE_EXPR,
6549 : : logical_type_node, args[1], nbits);
6550 : 6 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6551 : : logical_type_node, below, above);
6552 : 6 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6553 : : "POS argument (%ld) out of range 0:%ld "
6554 : : "in intrinsic BTEST",
6555 : : fold_convert (long_integer_type_node, args[1]),
6556 : : fold_convert (long_integer_type_node, nbits));
6557 : : }
6558 : :
6559 : 145 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6560 : : build_int_cst (type, 1), args[1]);
6561 : 145 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], tmp);
6562 : 145 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
6563 : : build_int_cst (type, 0));
6564 : 145 : type = gfc_typenode_for_spec (&expr->ts);
6565 : 145 : se->expr = convert (type, tmp);
6566 : 145 : }
6567 : :
6568 : :
6569 : : /* Generate code for BGE, BGT, BLE and BLT intrinsics. */
6570 : : static void
6571 : 216 : gfc_conv_intrinsic_bitcomp (gfc_se * se, gfc_expr * expr, enum tree_code op)
6572 : : {
6573 : 216 : tree args[2];
6574 : :
6575 : 216 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6576 : :
6577 : : /* Convert both arguments to the unsigned type of the same size. */
6578 : 216 : args[0] = fold_convert (unsigned_type_for (TREE_TYPE (args[0])), args[0]);
6579 : 216 : args[1] = fold_convert (unsigned_type_for (TREE_TYPE (args[1])), args[1]);
6580 : :
6581 : : /* If they have unequal type size, convert to the larger one. */
6582 : 216 : if (TYPE_PRECISION (TREE_TYPE (args[0]))
6583 : 216 : > TYPE_PRECISION (TREE_TYPE (args[1])))
6584 : 0 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
6585 : 216 : else if (TYPE_PRECISION (TREE_TYPE (args[1]))
6586 : 216 : > TYPE_PRECISION (TREE_TYPE (args[0])))
6587 : 0 : args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
6588 : :
6589 : : /* Now, we compare them. */
6590 : 216 : se->expr = fold_build2_loc (input_location, op, logical_type_node,
6591 : : args[0], args[1]);
6592 : 216 : }
6593 : :
6594 : :
6595 : : /* Generate code to perform the specified operation. */
6596 : : static void
6597 : 1891 : gfc_conv_intrinsic_bitop (gfc_se * se, gfc_expr * expr, enum tree_code op)
6598 : : {
6599 : 1891 : tree args[2];
6600 : :
6601 : 1891 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6602 : 1891 : se->expr = fold_build2_loc (input_location, op, TREE_TYPE (args[0]),
6603 : : args[0], args[1]);
6604 : 1891 : }
6605 : :
6606 : : /* Bitwise not. */
6607 : : static void
6608 : 230 : gfc_conv_intrinsic_not (gfc_se * se, gfc_expr * expr)
6609 : : {
6610 : 230 : tree arg;
6611 : :
6612 : 230 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
6613 : 230 : se->expr = fold_build1_loc (input_location, BIT_NOT_EXPR,
6614 : 230 : TREE_TYPE (arg), arg);
6615 : 230 : }
6616 : :
6617 : :
6618 : : /* Generate code for OUT_OF_RANGE. */
6619 : : static void
6620 : 468 : gfc_conv_intrinsic_out_of_range (gfc_se * se, gfc_expr * expr)
6621 : : {
6622 : 468 : tree *args;
6623 : 468 : tree type;
6624 : 468 : tree tmp = NULL_TREE, tmp1, tmp2;
6625 : 468 : unsigned int num_args;
6626 : 468 : int k;
6627 : 468 : gfc_se rnd_se;
6628 : 468 : gfc_actual_arglist *arg = expr->value.function.actual;
6629 : 468 : gfc_expr *x = arg->expr;
6630 : 468 : gfc_expr *mold = arg->next->expr;
6631 : :
6632 : 468 : num_args = gfc_intrinsic_argument_list_length (expr);
6633 : 468 : args = XALLOCAVEC (tree, num_args);
6634 : :
6635 : 468 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
6636 : :
6637 : 468 : gfc_init_se (&rnd_se, NULL);
6638 : :
6639 : 468 : if (num_args == 3)
6640 : : {
6641 : : /* The ROUND argument is optional and shall appear only if X is
6642 : : of type real and MOLD is of type integer (see edit F23/004). */
6643 : 270 : gfc_expr *round = arg->next->next->expr;
6644 : 270 : gfc_conv_expr (&rnd_se, round);
6645 : :
6646 : 270 : if (round->expr_type == EXPR_VARIABLE
6647 : 198 : && round->symtree->n.sym->attr.dummy
6648 : 30 : && round->symtree->n.sym->attr.optional)
6649 : : {
6650 : 30 : tree present = gfc_conv_expr_present (round->symtree->n.sym);
6651 : 30 : rnd_se.expr = build3_loc (input_location, COND_EXPR,
6652 : : logical_type_node, present,
6653 : : rnd_se.expr, logical_false_node);
6654 : 30 : gfc_add_block_to_block (&se->pre, &rnd_se.pre);
6655 : : }
6656 : : }
6657 : : else
6658 : : {
6659 : : /* If ROUND is absent, it is equivalent to having the value false. */
6660 : 198 : rnd_se.expr = logical_false_node;
6661 : : }
6662 : :
6663 : 468 : type = TREE_TYPE (args[0]);
6664 : 468 : k = gfc_validate_kind (mold->ts.type, mold->ts.kind, false);
6665 : :
6666 : 468 : switch (x->ts.type)
6667 : : {
6668 : 378 : case BT_REAL:
6669 : : /* X may be IEEE infinity or NaN, but the representation of MOLD may not
6670 : : support infinity or NaN. */
6671 : 378 : tree finite;
6672 : 378 : finite = build_call_expr_loc (input_location,
6673 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
6674 : : 1, args[0]);
6675 : 378 : finite = convert (logical_type_node, finite);
6676 : :
6677 : 378 : if (mold->ts.type == BT_REAL)
6678 : : {
6679 : 24 : tmp1 = build1 (ABS_EXPR, type, args[0]);
6680 : 24 : tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6681 : : mold->ts.kind, 0);
6682 : 24 : tmp = build2 (GT_EXPR, logical_type_node, tmp1,
6683 : : convert (type, tmp2));
6684 : :
6685 : : /* Check if MOLD representation supports infinity or NaN. */
6686 : 24 : bool infnan = (HONOR_INFINITIES (TREE_TYPE (args[1]))
6687 : 24 : || HONOR_NANS (TREE_TYPE (args[1])));
6688 : 24 : tmp = build3 (COND_EXPR, logical_type_node, finite, tmp,
6689 : : infnan ? logical_false_node : logical_true_node);
6690 : : }
6691 : : else
6692 : : {
6693 : 354 : tree rounded;
6694 : 354 : tree decl;
6695 : :
6696 : 354 : decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, x->ts.kind);
6697 : 354 : gcc_assert (decl != NULL_TREE);
6698 : :
6699 : : /* Round or truncate argument X, depending on the optional argument
6700 : : ROUND (default: .false.). */
6701 : 354 : tmp1 = build_round_expr (args[0], type);
6702 : 354 : tmp2 = build_call_expr_loc (input_location, decl, 1, args[0]);
6703 : 354 : rounded = build3 (COND_EXPR, type, rnd_se.expr, tmp1, tmp2);
6704 : :
6705 : 354 : if (mold->ts.type == BT_INTEGER)
6706 : : {
6707 : 180 : tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6708 : : x->ts.kind);
6709 : 180 : tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6710 : : x->ts.kind);
6711 : : }
6712 : 174 : else if (mold->ts.type == BT_UNSIGNED)
6713 : : {
6714 : 174 : tmp1 = build_real_from_int_cst (type, integer_zero_node);
6715 : 174 : tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6716 : : x->ts.kind);
6717 : : }
6718 : : else
6719 : 0 : gcc_unreachable ();
6720 : :
6721 : 354 : tmp1 = build2 (LT_EXPR, logical_type_node, rounded,
6722 : : convert (type, tmp1));
6723 : 354 : tmp2 = build2 (GT_EXPR, logical_type_node, rounded,
6724 : : convert (type, tmp2));
6725 : 354 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6726 : 354 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node,
6727 : : build1 (TRUTH_NOT_EXPR, logical_type_node, finite),
6728 : : tmp);
6729 : : }
6730 : : break;
6731 : :
6732 : 48 : case BT_INTEGER:
6733 : 48 : if (mold->ts.type == BT_INTEGER)
6734 : : {
6735 : 12 : tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6736 : : x->ts.kind);
6737 : 12 : tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6738 : : x->ts.kind);
6739 : 12 : tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6740 : : convert (type, tmp1));
6741 : 12 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6742 : : convert (type, tmp2));
6743 : 12 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6744 : : }
6745 : 36 : else if (mold->ts.type == BT_UNSIGNED)
6746 : : {
6747 : 36 : int i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
6748 : 36 : tmp = build_int_cst (type, 0);
6749 : 36 : tmp = build2 (LT_EXPR, logical_type_node, args[0], tmp);
6750 : 36 : if (mpz_cmp (gfc_integer_kinds[i].huge,
6751 : 36 : gfc_unsigned_kinds[k].huge) > 0)
6752 : : {
6753 : 0 : tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6754 : : x->ts.kind);
6755 : 0 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6756 : : convert (type, tmp2));
6757 : 0 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp, tmp2);
6758 : : }
6759 : : }
6760 : 0 : else if (mold->ts.type == BT_REAL)
6761 : : {
6762 : 0 : tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6763 : : mold->ts.kind, 0);
6764 : 0 : tmp1 = build1 (NEGATE_EXPR, TREE_TYPE (tmp2), tmp2);
6765 : 0 : tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6766 : : convert (type, tmp1));
6767 : 0 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6768 : : convert (type, tmp2));
6769 : 0 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6770 : : }
6771 : : else
6772 : 0 : gcc_unreachable ();
6773 : : break;
6774 : :
6775 : 42 : case BT_UNSIGNED:
6776 : 42 : if (mold->ts.type == BT_UNSIGNED)
6777 : : {
6778 : 12 : tmp = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6779 : : x->ts.kind);
6780 : 12 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6781 : : convert (type, tmp));
6782 : : }
6783 : 30 : else if (mold->ts.type == BT_INTEGER)
6784 : : {
6785 : 18 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6786 : : x->ts.kind);
6787 : 18 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6788 : : convert (type, tmp));
6789 : : }
6790 : 12 : else if (mold->ts.type == BT_REAL)
6791 : : {
6792 : 12 : tmp = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6793 : : mold->ts.kind, 0);
6794 : 12 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6795 : : convert (type, tmp));
6796 : : }
6797 : : else
6798 : 0 : gcc_unreachable ();
6799 : : break;
6800 : :
6801 : 0 : default:
6802 : 0 : gcc_unreachable ();
6803 : : }
6804 : :
6805 : 468 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
6806 : 468 : }
6807 : :
6808 : :
6809 : : /* Set or clear a single bit. */
6810 : : static void
6811 : 306 : gfc_conv_intrinsic_singlebitop (gfc_se * se, gfc_expr * expr, int set)
6812 : : {
6813 : 306 : tree args[2];
6814 : 306 : tree type;
6815 : 306 : tree tmp;
6816 : 306 : enum tree_code op;
6817 : :
6818 : 306 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6819 : 306 : type = TREE_TYPE (args[0]);
6820 : :
6821 : : /* Optionally generate code for runtime argument check. */
6822 : 306 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6823 : : {
6824 : 12 : tree below = fold_build2_loc (input_location, LT_EXPR,
6825 : : logical_type_node, args[1],
6826 : 12 : build_int_cst (TREE_TYPE (args[1]), 0));
6827 : 12 : tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6828 : 12 : tree above = fold_build2_loc (input_location, GE_EXPR,
6829 : : logical_type_node, args[1], nbits);
6830 : 12 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6831 : : logical_type_node, below, above);
6832 : 12 : size_t len_name = strlen (expr->value.function.isym->name);
6833 : 12 : char *name = XALLOCAVEC (char, len_name + 1);
6834 : 72 : for (size_t i = 0; i < len_name; i++)
6835 : 60 : name[i] = TOUPPER (expr->value.function.isym->name[i]);
6836 : 12 : name[len_name] = '\0';
6837 : 12 : tree iname = gfc_build_addr_expr (pchar_type_node,
6838 : : gfc_build_cstring_const (name));
6839 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6840 : : "POS argument (%ld) out of range 0:%ld "
6841 : : "in intrinsic %s",
6842 : : fold_convert (long_integer_type_node, args[1]),
6843 : : fold_convert (long_integer_type_node, nbits),
6844 : : iname);
6845 : : }
6846 : :
6847 : 306 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6848 : : build_int_cst (type, 1), args[1]);
6849 : 306 : if (set)
6850 : : op = BIT_IOR_EXPR;
6851 : : else
6852 : : {
6853 : 168 : op = BIT_AND_EXPR;
6854 : 168 : tmp = fold_build1_loc (input_location, BIT_NOT_EXPR, type, tmp);
6855 : : }
6856 : 306 : se->expr = fold_build2_loc (input_location, op, type, args[0], tmp);
6857 : 306 : }
6858 : :
6859 : : /* Extract a sequence of bits.
6860 : : IBITS(I, POS, LEN) = (I >> POS) & ~((~0) << LEN). */
6861 : : static void
6862 : 27 : gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr)
6863 : : {
6864 : 27 : tree args[3];
6865 : 27 : tree type;
6866 : 27 : tree tmp;
6867 : 27 : tree mask;
6868 : 27 : tree num_bits, cond;
6869 : :
6870 : 27 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
6871 : 27 : type = TREE_TYPE (args[0]);
6872 : :
6873 : : /* Optionally generate code for runtime argument check. */
6874 : 27 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6875 : : {
6876 : 12 : tree tmp1 = fold_convert (long_integer_type_node, args[1]);
6877 : 12 : tree tmp2 = fold_convert (long_integer_type_node, args[2]);
6878 : 12 : tree nbits = build_int_cst (long_integer_type_node,
6879 : 12 : TYPE_PRECISION (type));
6880 : 12 : tree below = fold_build2_loc (input_location, LT_EXPR,
6881 : : logical_type_node, args[1],
6882 : 12 : build_int_cst (TREE_TYPE (args[1]), 0));
6883 : 12 : tree above = fold_build2_loc (input_location, GT_EXPR,
6884 : : logical_type_node, tmp1, nbits);
6885 : 12 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6886 : : logical_type_node, below, above);
6887 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6888 : : "POS argument (%ld) out of range 0:%ld "
6889 : : "in intrinsic IBITS", tmp1, nbits);
6890 : 12 : below = fold_build2_loc (input_location, LT_EXPR,
6891 : : logical_type_node, args[2],
6892 : 12 : build_int_cst (TREE_TYPE (args[2]), 0));
6893 : 12 : above = fold_build2_loc (input_location, GT_EXPR,
6894 : : logical_type_node, tmp2, nbits);
6895 : 12 : scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6896 : : logical_type_node, below, above);
6897 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6898 : : "LEN argument (%ld) out of range 0:%ld "
6899 : : "in intrinsic IBITS", tmp2, nbits);
6900 : 12 : above = fold_build2_loc (input_location, PLUS_EXPR,
6901 : : long_integer_type_node, tmp1, tmp2);
6902 : 12 : scond = fold_build2_loc (input_location, GT_EXPR,
6903 : : logical_type_node, above, nbits);
6904 : 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6905 : : "POS(%ld)+LEN(%ld)>BIT_SIZE(%ld) "
6906 : : "in intrinsic IBITS", tmp1, tmp2, nbits);
6907 : : }
6908 : :
6909 : : /* The Fortran standard allows (shift width) LEN <= BIT_SIZE(I), whereas
6910 : : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6911 : : special case. See also gfc_conv_intrinsic_ishft (). */
6912 : 27 : num_bits = build_int_cst (TREE_TYPE (args[2]), TYPE_PRECISION (type));
6913 : :
6914 : 27 : mask = build_int_cst (type, -1);
6915 : 27 : mask = fold_build2_loc (input_location, LSHIFT_EXPR, type, mask, args[2]);
6916 : 27 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[2],
6917 : : num_bits);
6918 : 27 : mask = fold_build3_loc (input_location, COND_EXPR, type, cond,
6919 : : build_int_cst (type, 0), mask);
6920 : 27 : mask = fold_build1_loc (input_location, BIT_NOT_EXPR, type, mask);
6921 : :
6922 : 27 : tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, args[0], args[1]);
6923 : :
6924 : 27 : se->expr = fold_build2_loc (input_location, BIT_AND_EXPR, type, tmp, mask);
6925 : 27 : }
6926 : :
6927 : : static void
6928 : 441 : gfc_conv_intrinsic_shift (gfc_se * se, gfc_expr * expr, bool right_shift,
6929 : : bool arithmetic)
6930 : : {
6931 : 441 : tree args[2], type, num_bits, cond;
6932 : 441 : tree bigshift;
6933 : 441 : bool do_convert = false;
6934 : :
6935 : 441 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6936 : :
6937 : 441 : args[0] = gfc_evaluate_now (args[0], &se->pre);
6938 : 441 : args[1] = gfc_evaluate_now (args[1], &se->pre);
6939 : 441 : type = TREE_TYPE (args[0]);
6940 : :
6941 : 441 : if (!arithmetic)
6942 : : {
6943 : 339 : args[0] = fold_convert (unsigned_type_for (type), args[0]);
6944 : 339 : do_convert = true;
6945 : : }
6946 : : else
6947 : 102 : gcc_assert (right_shift);
6948 : :
6949 : 441 : if (flag_unsigned && arithmetic && expr->ts.type == BT_UNSIGNED)
6950 : : {
6951 : 30 : do_convert = true;
6952 : 30 : args[0] = fold_convert (signed_type_for (type), args[0]);
6953 : : }
6954 : :
6955 : 714 : se->expr = fold_build2_loc (input_location,
6956 : : right_shift ? RSHIFT_EXPR : LSHIFT_EXPR,
6957 : 441 : TREE_TYPE (args[0]), args[0], args[1]);
6958 : :
6959 : 441 : if (do_convert)
6960 : 369 : se->expr = fold_convert (type, se->expr);
6961 : :
6962 : 441 : if (!arithmetic)
6963 : 339 : bigshift = build_int_cst (type, 0);
6964 : : else
6965 : : {
6966 : 102 : tree nonneg = fold_build2_loc (input_location, GE_EXPR,
6967 : : logical_type_node, args[0],
6968 : 102 : build_int_cst (TREE_TYPE (args[0]), 0));
6969 : 102 : bigshift = fold_build3_loc (input_location, COND_EXPR, type, nonneg,
6970 : : build_int_cst (type, 0),
6971 : : build_int_cst (type, -1));
6972 : : }
6973 : :
6974 : : /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
6975 : : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6976 : : special case. */
6977 : 441 : num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6978 : :
6979 : : /* Optionally generate code for runtime argument check. */
6980 : 441 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6981 : : {
6982 : 30 : tree below = fold_build2_loc (input_location, LT_EXPR,
6983 : : logical_type_node, args[1],
6984 : 30 : build_int_cst (TREE_TYPE (args[1]), 0));
6985 : 30 : tree above = fold_build2_loc (input_location, GT_EXPR,
6986 : : logical_type_node, args[1], num_bits);
6987 : 30 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6988 : : logical_type_node, below, above);
6989 : 30 : size_t len_name = strlen (expr->value.function.isym->name);
6990 : 30 : char *name = XALLOCAVEC (char, len_name + 1);
6991 : 210 : for (size_t i = 0; i < len_name; i++)
6992 : 180 : name[i] = TOUPPER (expr->value.function.isym->name[i]);
6993 : 30 : name[len_name] = '\0';
6994 : 30 : tree iname = gfc_build_addr_expr (pchar_type_node,
6995 : : gfc_build_cstring_const (name));
6996 : 30 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6997 : : "SHIFT argument (%ld) out of range 0:%ld "
6998 : : "in intrinsic %s",
6999 : : fold_convert (long_integer_type_node, args[1]),
7000 : : fold_convert (long_integer_type_node, num_bits),
7001 : : iname);
7002 : : }
7003 : :
7004 : 441 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
7005 : : args[1], num_bits);
7006 : :
7007 : 441 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
7008 : : bigshift, se->expr);
7009 : 441 : }
7010 : :
7011 : : /* ISHFT (I, SHIFT) = (abs (shift) >= BIT_SIZE (i))
7012 : : ? 0
7013 : : : ((shift >= 0) ? i << shift : i >> -shift)
7014 : : where all shifts are logical shifts. */
7015 : : static void
7016 : 318 : gfc_conv_intrinsic_ishft (gfc_se * se, gfc_expr * expr)
7017 : : {
7018 : 318 : tree args[2];
7019 : 318 : tree type;
7020 : 318 : tree utype;
7021 : 318 : tree tmp;
7022 : 318 : tree width;
7023 : 318 : tree num_bits;
7024 : 318 : tree cond;
7025 : 318 : tree lshift;
7026 : 318 : tree rshift;
7027 : :
7028 : 318 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7029 : :
7030 : 318 : args[0] = gfc_evaluate_now (args[0], &se->pre);
7031 : 318 : args[1] = gfc_evaluate_now (args[1], &se->pre);
7032 : :
7033 : 318 : type = TREE_TYPE (args[0]);
7034 : 318 : utype = unsigned_type_for (type);
7035 : :
7036 : 318 : width = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (args[1]),
7037 : : args[1]);
7038 : :
7039 : : /* Left shift if positive. */
7040 : 318 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type, args[0], width);
7041 : :
7042 : : /* Right shift if negative.
7043 : : We convert to an unsigned type because we want a logical shift.
7044 : : The standard doesn't define the case of shifting negative
7045 : : numbers, and we try to be compatible with other compilers, most
7046 : : notably g77, here. */
7047 : 318 : rshift = fold_convert (type, fold_build2_loc (input_location, RSHIFT_EXPR,
7048 : : utype, convert (utype, args[0]), width));
7049 : :
7050 : 318 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[1],
7051 : 318 : build_int_cst (TREE_TYPE (args[1]), 0));
7052 : 318 : tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp, lshift, rshift);
7053 : :
7054 : : /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
7055 : : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
7056 : : special case. */
7057 : 318 : num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
7058 : :
7059 : : /* Optionally generate code for runtime argument check. */
7060 : 318 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7061 : : {
7062 : 24 : tree outside = fold_build2_loc (input_location, GT_EXPR,
7063 : : logical_type_node, width, num_bits);
7064 : 24 : gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
7065 : : "SHIFT argument (%ld) out of range -%ld:%ld "
7066 : : "in intrinsic ISHFT",
7067 : : fold_convert (long_integer_type_node, args[1]),
7068 : : fold_convert (long_integer_type_node, num_bits),
7069 : : fold_convert (long_integer_type_node, num_bits));
7070 : : }
7071 : :
7072 : 318 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, width,
7073 : : num_bits);
7074 : 318 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
7075 : : build_int_cst (type, 0), tmp);
7076 : 318 : }
7077 : :
7078 : :
7079 : : /* Circular shift. AKA rotate or barrel shift. */
7080 : :
7081 : : static void
7082 : 658 : gfc_conv_intrinsic_ishftc (gfc_se * se, gfc_expr * expr)
7083 : : {
7084 : 658 : tree *args;
7085 : 658 : tree type;
7086 : 658 : tree tmp;
7087 : 658 : tree lrot;
7088 : 658 : tree rrot;
7089 : 658 : tree zero;
7090 : 658 : tree nbits;
7091 : 658 : unsigned int num_args;
7092 : :
7093 : 658 : num_args = gfc_intrinsic_argument_list_length (expr);
7094 : 658 : args = XALLOCAVEC (tree, num_args);
7095 : :
7096 : 658 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7097 : :
7098 : 658 : type = TREE_TYPE (args[0]);
7099 : 658 : nbits = build_int_cst (long_integer_type_node, TYPE_PRECISION (type));
7100 : :
7101 : 658 : if (num_args == 3)
7102 : : {
7103 : 550 : gfc_expr *size = expr->value.function.actual->next->next->expr;
7104 : :
7105 : : /* Use a library function for the 3 parameter version. */
7106 : 550 : tree int4type = gfc_get_int_type (4);
7107 : :
7108 : : /* Treat optional SIZE argument when it is passed as an optional
7109 : : dummy. If SIZE is absent, the default value is BIT_SIZE(I). */
7110 : 550 : if (size->expr_type == EXPR_VARIABLE
7111 : 438 : && size->symtree->n.sym->attr.dummy
7112 : 36 : && size->symtree->n.sym->attr.optional)
7113 : : {
7114 : 36 : tree type_of_size = TREE_TYPE (args[2]);
7115 : 72 : args[2] = build3_loc (input_location, COND_EXPR, type_of_size,
7116 : 36 : gfc_conv_expr_present (size->symtree->n.sym),
7117 : : args[2], fold_convert (type_of_size, nbits));
7118 : : }
7119 : :
7120 : : /* We convert the first argument to at least 4 bytes, and
7121 : : convert back afterwards. This removes the need for library
7122 : : functions for all argument sizes, and function will be
7123 : : aligned to at least 32 bits, so there's no loss. */
7124 : 550 : if (expr->ts.kind < 4)
7125 : 242 : args[0] = convert (int4type, args[0]);
7126 : :
7127 : : /* Convert the SHIFT and SIZE args to INTEGER*4 otherwise we would
7128 : : need loads of library functions. They cannot have values >
7129 : : BIT_SIZE (I) so the conversion is safe. */
7130 : 550 : args[1] = convert (int4type, args[1]);
7131 : 550 : args[2] = convert (int4type, args[2]);
7132 : :
7133 : : /* Optionally generate code for runtime argument check. */
7134 : 550 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7135 : : {
7136 : 18 : tree size = fold_convert (long_integer_type_node, args[2]);
7137 : 18 : tree below = fold_build2_loc (input_location, LE_EXPR,
7138 : : logical_type_node, size,
7139 : 18 : build_int_cst (TREE_TYPE (args[1]), 0));
7140 : 18 : tree above = fold_build2_loc (input_location, GT_EXPR,
7141 : : logical_type_node, size, nbits);
7142 : 18 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
7143 : : logical_type_node, below, above);
7144 : 18 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
7145 : : "SIZE argument (%ld) out of range 1:%ld "
7146 : : "in intrinsic ISHFTC", size, nbits);
7147 : 18 : tree width = fold_convert (long_integer_type_node, args[1]);
7148 : 18 : width = fold_build1_loc (input_location, ABS_EXPR,
7149 : : long_integer_type_node, width);
7150 : 18 : scond = fold_build2_loc (input_location, GT_EXPR,
7151 : : logical_type_node, width, size);
7152 : 18 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
7153 : : "SHIFT argument (%ld) out of range -%ld:%ld "
7154 : : "in intrinsic ISHFTC",
7155 : : fold_convert (long_integer_type_node, args[1]),
7156 : : size, size);
7157 : : }
7158 : :
7159 : 550 : switch (expr->ts.kind)
7160 : : {
7161 : 426 : case 1:
7162 : 426 : case 2:
7163 : 426 : case 4:
7164 : 426 : tmp = gfor_fndecl_math_ishftc4;
7165 : 426 : break;
7166 : 124 : case 8:
7167 : 124 : tmp = gfor_fndecl_math_ishftc8;
7168 : 124 : break;
7169 : 0 : case 16:
7170 : 0 : tmp = gfor_fndecl_math_ishftc16;
7171 : 0 : break;
7172 : 0 : default:
7173 : 0 : gcc_unreachable ();
7174 : : }
7175 : 550 : se->expr = build_call_expr_loc (input_location,
7176 : : tmp, 3, args[0], args[1], args[2]);
7177 : : /* Convert the result back to the original type, if we extended
7178 : : the first argument's width above. */
7179 : 550 : if (expr->ts.kind < 4)
7180 : 242 : se->expr = convert (type, se->expr);
7181 : :
7182 : 550 : return;
7183 : : }
7184 : :
7185 : : /* Evaluate arguments only once. */
7186 : 108 : args[0] = gfc_evaluate_now (args[0], &se->pre);
7187 : 108 : args[1] = gfc_evaluate_now (args[1], &se->pre);
7188 : :
7189 : : /* Optionally generate code for runtime argument check. */
7190 : 108 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7191 : : {
7192 : 12 : tree width = fold_convert (long_integer_type_node, args[1]);
7193 : 12 : width = fold_build1_loc (input_location, ABS_EXPR,
7194 : : long_integer_type_node, width);
7195 : 12 : tree outside = fold_build2_loc (input_location, GT_EXPR,
7196 : : logical_type_node, width, nbits);
7197 : 12 : gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
7198 : : "SHIFT argument (%ld) out of range -%ld:%ld "
7199 : : "in intrinsic ISHFTC",
7200 : : fold_convert (long_integer_type_node, args[1]),
7201 : : nbits, nbits);
7202 : : }
7203 : :
7204 : : /* Rotate left if positive. */
7205 : 108 : lrot = fold_build2_loc (input_location, LROTATE_EXPR, type, args[0], args[1]);
7206 : :
7207 : : /* Rotate right if negative. */
7208 : 108 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (args[1]),
7209 : : args[1]);
7210 : 108 : rrot = fold_build2_loc (input_location,RROTATE_EXPR, type, args[0], tmp);
7211 : :
7212 : 108 : zero = build_int_cst (TREE_TYPE (args[1]), 0);
7213 : 108 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, args[1],
7214 : : zero);
7215 : 108 : rrot = fold_build3_loc (input_location, COND_EXPR, type, tmp, lrot, rrot);
7216 : :
7217 : : /* Do nothing if shift == 0. */
7218 : 108 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, args[1],
7219 : : zero);
7220 : 108 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, args[0],
7221 : : rrot);
7222 : : }
7223 : :
7224 : :
7225 : : /* LEADZ (i) = (i == 0) ? BIT_SIZE (i)
7226 : : : __builtin_clz(i) - (BIT_SIZE('int') - BIT_SIZE(i))
7227 : :
7228 : : The conditional expression is necessary because the result of LEADZ(0)
7229 : : is defined, but the result of __builtin_clz(0) is undefined for most
7230 : : targets.
7231 : :
7232 : : For INTEGER kinds smaller than the C 'int' type, we have to subtract the
7233 : : difference in bit size between the argument of LEADZ and the C int. */
7234 : :
7235 : : static void
7236 : 270 : gfc_conv_intrinsic_leadz (gfc_se * se, gfc_expr * expr)
7237 : : {
7238 : 270 : tree arg;
7239 : 270 : tree arg_type;
7240 : 270 : tree cond;
7241 : 270 : tree result_type;
7242 : 270 : tree leadz;
7243 : 270 : tree bit_size;
7244 : 270 : tree tmp;
7245 : 270 : tree func;
7246 : 270 : int s, argsize;
7247 : :
7248 : 270 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7249 : 270 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7250 : :
7251 : : /* Which variant of __builtin_clz* should we call? */
7252 : 270 : if (argsize <= INT_TYPE_SIZE)
7253 : : {
7254 : 183 : arg_type = unsigned_type_node;
7255 : 183 : func = builtin_decl_explicit (BUILT_IN_CLZ);
7256 : : }
7257 : 87 : else if (argsize <= LONG_TYPE_SIZE)
7258 : : {
7259 : 57 : arg_type = long_unsigned_type_node;
7260 : 57 : func = builtin_decl_explicit (BUILT_IN_CLZL);
7261 : : }
7262 : 30 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7263 : : {
7264 : 0 : arg_type = long_long_unsigned_type_node;
7265 : 0 : func = builtin_decl_explicit (BUILT_IN_CLZLL);
7266 : : }
7267 : : else
7268 : : {
7269 : 30 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7270 : 30 : arg_type = gfc_build_uint_type (argsize);
7271 : 30 : func = NULL_TREE;
7272 : : }
7273 : :
7274 : : /* Convert the actual argument twice: first, to the unsigned type of the
7275 : : same size; then, to the proper argument type for the built-in
7276 : : function. But the return type is of the default INTEGER kind. */
7277 : 270 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7278 : 270 : arg = fold_convert (arg_type, arg);
7279 : 270 : arg = gfc_evaluate_now (arg, &se->pre);
7280 : 270 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7281 : :
7282 : : /* Compute LEADZ for the case i .ne. 0. */
7283 : 270 : if (func)
7284 : : {
7285 : 240 : s = TYPE_PRECISION (arg_type) - argsize;
7286 : 240 : tmp = fold_convert (result_type,
7287 : : build_call_expr_loc (input_location, func,
7288 : : 1, arg));
7289 : 240 : leadz = fold_build2_loc (input_location, MINUS_EXPR, result_type,
7290 : 240 : tmp, build_int_cst (result_type, s));
7291 : : }
7292 : : else
7293 : : {
7294 : : /* We end up here if the argument type is larger than 'long long'.
7295 : : We generate this code:
7296 : :
7297 : : if (x & (ULL_MAX << ULL_SIZE) != 0)
7298 : : return clzll ((unsigned long long) (x >> ULLSIZE));
7299 : : else
7300 : : return ULL_SIZE + clzll ((unsigned long long) x);
7301 : : where ULL_MAX is the largest value that a ULL_MAX can hold
7302 : : (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7303 : : is the bit-size of the long long type (64 in this example). */
7304 : 30 : tree ullsize, ullmax, tmp1, tmp2, btmp;
7305 : :
7306 : 30 : ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7307 : 30 : ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7308 : : long_long_unsigned_type_node,
7309 : : build_int_cst (long_long_unsigned_type_node,
7310 : : 0));
7311 : :
7312 : 30 : cond = fold_build2_loc (input_location, LSHIFT_EXPR, arg_type,
7313 : : fold_convert (arg_type, ullmax), ullsize);
7314 : 30 : cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type,
7315 : : arg, cond);
7316 : 30 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
7317 : : cond, build_int_cst (arg_type, 0));
7318 : :
7319 : 30 : tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7320 : : arg, ullsize);
7321 : 30 : tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7322 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7323 : 30 : tmp1 = fold_convert (result_type,
7324 : : build_call_expr_loc (input_location, btmp, 1, tmp1));
7325 : :
7326 : 30 : tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7327 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7328 : 30 : tmp2 = fold_convert (result_type,
7329 : : build_call_expr_loc (input_location, btmp, 1, tmp2));
7330 : 30 : tmp2 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7331 : : tmp2, ullsize);
7332 : :
7333 : 30 : leadz = fold_build3_loc (input_location, COND_EXPR, result_type,
7334 : : cond, tmp1, tmp2);
7335 : : }
7336 : :
7337 : : /* Build BIT_SIZE. */
7338 : 270 : bit_size = build_int_cst (result_type, argsize);
7339 : :
7340 : 270 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7341 : : arg, build_int_cst (arg_type, 0));
7342 : 270 : se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7343 : : bit_size, leadz);
7344 : 270 : }
7345 : :
7346 : :
7347 : : /* TRAILZ(i) = (i == 0) ? BIT_SIZE (i) : __builtin_ctz(i)
7348 : :
7349 : : The conditional expression is necessary because the result of TRAILZ(0)
7350 : : is defined, but the result of __builtin_ctz(0) is undefined for most
7351 : : targets. */
7352 : :
7353 : : static void
7354 : 282 : gfc_conv_intrinsic_trailz (gfc_se * se, gfc_expr *expr)
7355 : : {
7356 : 282 : tree arg;
7357 : 282 : tree arg_type;
7358 : 282 : tree cond;
7359 : 282 : tree result_type;
7360 : 282 : tree trailz;
7361 : 282 : tree bit_size;
7362 : 282 : tree func;
7363 : 282 : int argsize;
7364 : :
7365 : 282 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7366 : 282 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7367 : :
7368 : : /* Which variant of __builtin_ctz* should we call? */
7369 : 282 : if (argsize <= INT_TYPE_SIZE)
7370 : : {
7371 : 195 : arg_type = unsigned_type_node;
7372 : 195 : func = builtin_decl_explicit (BUILT_IN_CTZ);
7373 : : }
7374 : 87 : else if (argsize <= LONG_TYPE_SIZE)
7375 : : {
7376 : 57 : arg_type = long_unsigned_type_node;
7377 : 57 : func = builtin_decl_explicit (BUILT_IN_CTZL);
7378 : : }
7379 : 30 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7380 : : {
7381 : 0 : arg_type = long_long_unsigned_type_node;
7382 : 0 : func = builtin_decl_explicit (BUILT_IN_CTZLL);
7383 : : }
7384 : : else
7385 : : {
7386 : 30 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7387 : 30 : arg_type = gfc_build_uint_type (argsize);
7388 : 30 : func = NULL_TREE;
7389 : : }
7390 : :
7391 : : /* Convert the actual argument twice: first, to the unsigned type of the
7392 : : same size; then, to the proper argument type for the built-in
7393 : : function. But the return type is of the default INTEGER kind. */
7394 : 282 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7395 : 282 : arg = fold_convert (arg_type, arg);
7396 : 282 : arg = gfc_evaluate_now (arg, &se->pre);
7397 : 282 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7398 : :
7399 : : /* Compute TRAILZ for the case i .ne. 0. */
7400 : 282 : if (func)
7401 : 252 : trailz = fold_convert (result_type, build_call_expr_loc (input_location,
7402 : : func, 1, arg));
7403 : : else
7404 : : {
7405 : : /* We end up here if the argument type is larger than 'long long'.
7406 : : We generate this code:
7407 : :
7408 : : if ((x & ULL_MAX) == 0)
7409 : : return ULL_SIZE + ctzll ((unsigned long long) (x >> ULLSIZE));
7410 : : else
7411 : : return ctzll ((unsigned long long) x);
7412 : :
7413 : : where ULL_MAX is the largest value that a ULL_MAX can hold
7414 : : (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7415 : : is the bit-size of the long long type (64 in this example). */
7416 : 30 : tree ullsize, ullmax, tmp1, tmp2, btmp;
7417 : :
7418 : 30 : ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7419 : 30 : ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7420 : : long_long_unsigned_type_node,
7421 : : build_int_cst (long_long_unsigned_type_node, 0));
7422 : :
7423 : 30 : cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type, arg,
7424 : : fold_convert (arg_type, ullmax));
7425 : 30 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, cond,
7426 : : build_int_cst (arg_type, 0));
7427 : :
7428 : 30 : tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7429 : : arg, ullsize);
7430 : 30 : tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7431 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7432 : 30 : tmp1 = fold_convert (result_type,
7433 : : build_call_expr_loc (input_location, btmp, 1, tmp1));
7434 : 30 : tmp1 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7435 : : tmp1, ullsize);
7436 : :
7437 : 30 : tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7438 : 30 : btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7439 : 30 : tmp2 = fold_convert (result_type,
7440 : : build_call_expr_loc (input_location, btmp, 1, tmp2));
7441 : :
7442 : 30 : trailz = fold_build3_loc (input_location, COND_EXPR, result_type,
7443 : : cond, tmp1, tmp2);
7444 : : }
7445 : :
7446 : : /* Build BIT_SIZE. */
7447 : 282 : bit_size = build_int_cst (result_type, argsize);
7448 : :
7449 : 282 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7450 : : arg, build_int_cst (arg_type, 0));
7451 : 282 : se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7452 : : bit_size, trailz);
7453 : 282 : }
7454 : :
7455 : : /* Using __builtin_popcount for POPCNT and __builtin_parity for POPPAR;
7456 : : for types larger than "long long", we call the long long built-in for
7457 : : the lower and higher bits and combine the result. */
7458 : :
7459 : : static void
7460 : 134 : gfc_conv_intrinsic_popcnt_poppar (gfc_se * se, gfc_expr *expr, int parity)
7461 : : {
7462 : 134 : tree arg;
7463 : 134 : tree arg_type;
7464 : 134 : tree result_type;
7465 : 134 : tree func;
7466 : 134 : int argsize;
7467 : :
7468 : 134 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7469 : 134 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7470 : 134 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7471 : :
7472 : : /* Which variant of the builtin should we call? */
7473 : 134 : if (argsize <= INT_TYPE_SIZE)
7474 : : {
7475 : 108 : arg_type = unsigned_type_node;
7476 : 198 : func = builtin_decl_explicit (parity
7477 : : ? BUILT_IN_PARITY
7478 : : : BUILT_IN_POPCOUNT);
7479 : : }
7480 : 26 : else if (argsize <= LONG_TYPE_SIZE)
7481 : : {
7482 : 12 : arg_type = long_unsigned_type_node;
7483 : 18 : func = builtin_decl_explicit (parity
7484 : : ? BUILT_IN_PARITYL
7485 : : : BUILT_IN_POPCOUNTL);
7486 : : }
7487 : 14 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7488 : : {
7489 : 0 : arg_type = long_long_unsigned_type_node;
7490 : 0 : func = builtin_decl_explicit (parity
7491 : : ? BUILT_IN_PARITYLL
7492 : : : BUILT_IN_POPCOUNTLL);
7493 : : }
7494 : : else
7495 : : {
7496 : : /* Our argument type is larger than 'long long', which mean none
7497 : : of the POPCOUNT builtins covers it. We thus call the 'long long'
7498 : : variant multiple times, and add the results. */
7499 : 14 : tree utype, arg2, call1, call2;
7500 : :
7501 : : /* For now, we only cover the case where argsize is twice as large
7502 : : as 'long long'. */
7503 : 14 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7504 : :
7505 : 21 : func = builtin_decl_explicit (parity
7506 : : ? BUILT_IN_PARITYLL
7507 : : : BUILT_IN_POPCOUNTLL);
7508 : :
7509 : : /* Convert it to an integer, and store into a variable. */
7510 : 14 : utype = gfc_build_uint_type (argsize);
7511 : 14 : arg = fold_convert (utype, arg);
7512 : 14 : arg = gfc_evaluate_now (arg, &se->pre);
7513 : :
7514 : : /* Call the builtin twice. */
7515 : 14 : call1 = build_call_expr_loc (input_location, func, 1,
7516 : : fold_convert (long_long_unsigned_type_node,
7517 : : arg));
7518 : :
7519 : 14 : arg2 = fold_build2_loc (input_location, RSHIFT_EXPR, utype, arg,
7520 : : build_int_cst (utype, LONG_LONG_TYPE_SIZE));
7521 : 14 : call2 = build_call_expr_loc (input_location, func, 1,
7522 : : fold_convert (long_long_unsigned_type_node,
7523 : : arg2));
7524 : :
7525 : : /* Combine the results. */
7526 : 14 : if (parity)
7527 : 7 : se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR,
7528 : : integer_type_node, call1, call2);
7529 : : else
7530 : 7 : se->expr = fold_build2_loc (input_location, PLUS_EXPR,
7531 : : integer_type_node, call1, call2);
7532 : :
7533 : 14 : se->expr = convert (result_type, se->expr);
7534 : 14 : return;
7535 : : }
7536 : :
7537 : : /* Convert the actual argument twice: first, to the unsigned type of the
7538 : : same size; then, to the proper argument type for the built-in
7539 : : function. */
7540 : 120 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7541 : 120 : arg = fold_convert (arg_type, arg);
7542 : :
7543 : 120 : se->expr = fold_convert (result_type,
7544 : : build_call_expr_loc (input_location, func, 1, arg));
7545 : : }
7546 : :
7547 : :
7548 : : /* Process an intrinsic with unspecified argument-types that has an optional
7549 : : argument (which could be of type character), e.g. EOSHIFT. For those, we
7550 : : need to append the string length of the optional argument if it is not
7551 : : present and the type is really character.
7552 : : primary specifies the position (starting at 1) of the non-optional argument
7553 : : specifying the type and optional gives the position of the optional
7554 : : argument in the arglist. */
7555 : :
7556 : : static void
7557 : 6086 : conv_generic_with_optional_char_arg (gfc_se* se, gfc_expr* expr,
7558 : : unsigned primary, unsigned optional)
7559 : : {
7560 : 6086 : gfc_actual_arglist* prim_arg;
7561 : 6086 : gfc_actual_arglist* opt_arg;
7562 : 6086 : unsigned cur_pos;
7563 : 6086 : gfc_actual_arglist* arg;
7564 : 6086 : gfc_symbol* sym;
7565 : 6086 : vec<tree, va_gc> *append_args;
7566 : :
7567 : : /* Find the two arguments given as position. */
7568 : 6086 : cur_pos = 0;
7569 : 6086 : prim_arg = NULL;
7570 : 6086 : opt_arg = NULL;
7571 : 18258 : for (arg = expr->value.function.actual; arg; arg = arg->next)
7572 : : {
7573 : 18258 : ++cur_pos;
7574 : :
7575 : 18258 : if (cur_pos == primary)
7576 : 6086 : prim_arg = arg;
7577 : 18258 : if (cur_pos == optional)
7578 : 6086 : opt_arg = arg;
7579 : :
7580 : 18258 : if (cur_pos >= primary && cur_pos >= optional)
7581 : : break;
7582 : : }
7583 : 6086 : gcc_assert (prim_arg);
7584 : 6086 : gcc_assert (prim_arg->expr);
7585 : 6086 : gcc_assert (opt_arg);
7586 : :
7587 : : /* If we do have type CHARACTER and the optional argument is really absent,
7588 : : append a dummy 0 as string length. */
7589 : 6086 : append_args = NULL;
7590 : 6086 : if (prim_arg->expr->ts.type == BT_CHARACTER && !opt_arg->expr)
7591 : : {
7592 : 608 : tree dummy;
7593 : :
7594 : 608 : dummy = build_int_cst (gfc_charlen_type_node, 0);
7595 : 608 : vec_alloc (append_args, 1);
7596 : 608 : append_args->quick_push (dummy);
7597 : : }
7598 : :
7599 : : /* Build the call itself. */
7600 : 6086 : gcc_assert (!se->ignore_optional);
7601 : 6086 : sym = gfc_get_symbol_for_expr (expr, false);
7602 : 6086 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
7603 : : append_args);
7604 : 6086 : gfc_free_symbol (sym);
7605 : 6086 : }
7606 : :
7607 : : /* The length of a character string. */
7608 : : static void
7609 : 5625 : gfc_conv_intrinsic_len (gfc_se * se, gfc_expr * expr)
7610 : : {
7611 : 5625 : tree len;
7612 : 5625 : tree type;
7613 : 5625 : tree decl;
7614 : 5625 : gfc_symbol *sym;
7615 : 5625 : gfc_se argse;
7616 : 5625 : gfc_expr *arg;
7617 : :
7618 : 5625 : gcc_assert (!se->ss);
7619 : :
7620 : 5625 : arg = expr->value.function.actual->expr;
7621 : :
7622 : 5625 : type = gfc_typenode_for_spec (&expr->ts);
7623 : 5625 : switch (arg->expr_type)
7624 : : {
7625 : 0 : case EXPR_CONSTANT:
7626 : 0 : len = build_int_cst (gfc_charlen_type_node, arg->value.character.length);
7627 : 0 : break;
7628 : :
7629 : 2 : case EXPR_ARRAY:
7630 : : /* Obtain the string length from the function used by
7631 : : trans-array.cc(gfc_trans_array_constructor). */
7632 : 2 : len = NULL_TREE;
7633 : 2 : get_array_ctor_strlen (&se->pre, arg->value.constructor, &len);
7634 : 2 : break;
7635 : :
7636 : 5038 : case EXPR_VARIABLE:
7637 : 5038 : if (arg->ref == NULL
7638 : 2276 : || (arg->ref->next == NULL && arg->ref->type == REF_ARRAY))
7639 : : {
7640 : : /* This doesn't catch all cases.
7641 : : See http://gcc.gnu.org/ml/fortran/2004-06/msg00165.html
7642 : : and the surrounding thread. */
7643 : 4506 : sym = arg->symtree->n.sym;
7644 : 4506 : decl = gfc_get_symbol_decl (sym);
7645 : 4506 : if (decl == current_function_decl && sym->attr.function
7646 : 53 : && (sym->result == sym))
7647 : 53 : decl = gfc_get_fake_result_decl (sym, 0);
7648 : :
7649 : 4506 : len = sym->ts.u.cl->backend_decl;
7650 : 4506 : gcc_assert (len);
7651 : : break;
7652 : : }
7653 : :
7654 : : /* Fall through. */
7655 : :
7656 : 1117 : default:
7657 : 1117 : gfc_init_se (&argse, se);
7658 : 1117 : if (arg->rank == 0)
7659 : 995 : gfc_conv_expr (&argse, arg);
7660 : : else
7661 : 122 : gfc_conv_expr_descriptor (&argse, arg);
7662 : 1117 : gfc_add_block_to_block (&se->pre, &argse.pre);
7663 : 1117 : gfc_add_block_to_block (&se->post, &argse.post);
7664 : 1117 : len = argse.string_length;
7665 : 1117 : break;
7666 : : }
7667 : 5625 : se->expr = convert (type, len);
7668 : 5625 : }
7669 : :
7670 : : /* The length of a character string not including trailing blanks. */
7671 : : static void
7672 : 2315 : gfc_conv_intrinsic_len_trim (gfc_se * se, gfc_expr * expr)
7673 : : {
7674 : 2315 : int kind = expr->value.function.actual->expr->ts.kind;
7675 : 2315 : tree args[2], type, fndecl;
7676 : :
7677 : 2315 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7678 : 2315 : type = gfc_typenode_for_spec (&expr->ts);
7679 : :
7680 : 2315 : if (kind == 1)
7681 : 1913 : fndecl = gfor_fndecl_string_len_trim;
7682 : 402 : else if (kind == 4)
7683 : 402 : fndecl = gfor_fndecl_string_len_trim_char4;
7684 : : else
7685 : 0 : gcc_unreachable ();
7686 : :
7687 : 2315 : se->expr = build_call_expr_loc (input_location,
7688 : : fndecl, 2, args[0], args[1]);
7689 : 2315 : se->expr = convert (type, se->expr);
7690 : 2315 : }
7691 : :
7692 : :
7693 : : /* Returns the starting position of a substring within a string. */
7694 : :
7695 : : static void
7696 : 739 : gfc_conv_intrinsic_index_scan_verify (gfc_se * se, gfc_expr * expr,
7697 : : tree function)
7698 : : {
7699 : 739 : tree logical4_type_node = gfc_get_logical_type (4);
7700 : 739 : tree type;
7701 : 739 : tree fndecl;
7702 : 739 : tree *args;
7703 : 739 : unsigned int num_args;
7704 : :
7705 : 739 : args = XALLOCAVEC (tree, 5);
7706 : :
7707 : : /* Get number of arguments; characters count double due to the
7708 : : string length argument. Kind= is not passed to the library
7709 : : and thus ignored. */
7710 : 739 : if (expr->value.function.actual->next->next->expr == NULL)
7711 : : num_args = 4;
7712 : : else
7713 : 304 : num_args = 5;
7714 : :
7715 : 739 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7716 : 739 : type = gfc_typenode_for_spec (&expr->ts);
7717 : :
7718 : 739 : if (num_args == 4)
7719 : 435 : args[4] = build_int_cst (logical4_type_node, 0);
7720 : : else
7721 : 304 : args[4] = convert (logical4_type_node, args[4]);
7722 : :
7723 : 739 : fndecl = build_addr (function);
7724 : 739 : se->expr = build_call_array_loc (input_location,
7725 : 739 : TREE_TYPE (TREE_TYPE (function)), fndecl,
7726 : : 5, args);
7727 : 739 : se->expr = convert (type, se->expr);
7728 : :
7729 : 739 : }
7730 : :
7731 : : /* The ascii value for a single character. */
7732 : : static void
7733 : 2033 : gfc_conv_intrinsic_ichar (gfc_se * se, gfc_expr * expr)
7734 : : {
7735 : 2033 : tree args[3], type, pchartype;
7736 : 2033 : int nargs;
7737 : :
7738 : 2033 : nargs = gfc_intrinsic_argument_list_length (expr);
7739 : 2033 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
7740 : 2033 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (args[1])));
7741 : 2033 : pchartype = gfc_get_pchar_type (expr->value.function.actual->expr->ts.kind);
7742 : 2033 : args[1] = fold_build1_loc (input_location, NOP_EXPR, pchartype, args[1]);
7743 : 2033 : type = gfc_typenode_for_spec (&expr->ts);
7744 : :
7745 : 2033 : se->expr = build_fold_indirect_ref_loc (input_location,
7746 : : args[1]);
7747 : 2033 : se->expr = convert (type, se->expr);
7748 : 2033 : }
7749 : :
7750 : :
7751 : : /* Intrinsic ISNAN calls __builtin_isnan. */
7752 : :
7753 : : static void
7754 : 432 : gfc_conv_intrinsic_isnan (gfc_se * se, gfc_expr * expr)
7755 : : {
7756 : 432 : tree arg;
7757 : :
7758 : 432 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7759 : 432 : se->expr = build_call_expr_loc (input_location,
7760 : : builtin_decl_explicit (BUILT_IN_ISNAN),
7761 : : 1, arg);
7762 : 864 : STRIP_TYPE_NOPS (se->expr);
7763 : 432 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
7764 : 432 : }
7765 : :
7766 : :
7767 : : /* Intrinsics IS_IOSTAT_END and IS_IOSTAT_EOR just need to compare
7768 : : their argument against a constant integer value. */
7769 : :
7770 : : static void
7771 : 24 : gfc_conv_has_intvalue (gfc_se * se, gfc_expr * expr, const int value)
7772 : : {
7773 : 24 : tree arg;
7774 : :
7775 : 24 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7776 : 24 : se->expr = fold_build2_loc (input_location, EQ_EXPR,
7777 : : gfc_typenode_for_spec (&expr->ts),
7778 : 24 : arg, build_int_cst (TREE_TYPE (arg), value));
7779 : 24 : }
7780 : :
7781 : :
7782 : :
7783 : : /* MERGE (tsource, fsource, mask) = mask ? tsource : fsource. */
7784 : :
7785 : : static void
7786 : 949 : gfc_conv_intrinsic_merge (gfc_se * se, gfc_expr * expr)
7787 : : {
7788 : 949 : tree tsource;
7789 : 949 : tree fsource;
7790 : 949 : tree mask;
7791 : 949 : tree type;
7792 : 949 : tree len, len2;
7793 : 949 : tree *args;
7794 : 949 : unsigned int num_args;
7795 : :
7796 : 949 : num_args = gfc_intrinsic_argument_list_length (expr);
7797 : 949 : args = XALLOCAVEC (tree, num_args);
7798 : :
7799 : 949 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7800 : 949 : if (expr->ts.type != BT_CHARACTER)
7801 : : {
7802 : 422 : tsource = args[0];
7803 : 422 : fsource = args[1];
7804 : 422 : mask = args[2];
7805 : : }
7806 : : else
7807 : : {
7808 : : /* We do the same as in the non-character case, but the argument
7809 : : list is different because of the string length arguments. We
7810 : : also have to set the string length for the result. */
7811 : 527 : len = args[0];
7812 : 527 : tsource = args[1];
7813 : 527 : len2 = args[2];
7814 : 527 : fsource = args[3];
7815 : 527 : mask = args[4];
7816 : :
7817 : 527 : gfc_trans_same_strlen_check ("MERGE intrinsic", &expr->where, len, len2,
7818 : : &se->pre);
7819 : 527 : se->string_length = len;
7820 : : }
7821 : 949 : tsource = gfc_evaluate_now (tsource, &se->pre);
7822 : 949 : fsource = gfc_evaluate_now (fsource, &se->pre);
7823 : 949 : mask = gfc_evaluate_now (mask, &se->pre);
7824 : 949 : type = TREE_TYPE (tsource);
7825 : 949 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, mask, tsource,
7826 : : fold_convert (type, fsource));
7827 : 949 : }
7828 : :
7829 : :
7830 : : /* MERGE_BITS (I, J, MASK) = (I & MASK) | (I & (~MASK)). */
7831 : :
7832 : : static void
7833 : 42 : gfc_conv_intrinsic_merge_bits (gfc_se * se, gfc_expr * expr)
7834 : : {
7835 : 42 : tree args[3], mask, type;
7836 : :
7837 : 42 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
7838 : 42 : mask = gfc_evaluate_now (args[2], &se->pre);
7839 : :
7840 : 42 : type = TREE_TYPE (args[0]);
7841 : 42 : gcc_assert (TREE_TYPE (args[1]) == type);
7842 : 42 : gcc_assert (TREE_TYPE (mask) == type);
7843 : :
7844 : 42 : args[0] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], mask);
7845 : 42 : args[1] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[1],
7846 : : fold_build1_loc (input_location, BIT_NOT_EXPR,
7847 : : type, mask));
7848 : 42 : se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
7849 : : args[0], args[1]);
7850 : 42 : }
7851 : :
7852 : :
7853 : : /* MASKL(n) = n == 0 ? 0 : (~0) << (BIT_SIZE - n)
7854 : : MASKR(n) = n == BIT_SIZE ? ~0 : ~((~0) << n) */
7855 : :
7856 : : static void
7857 : 64 : gfc_conv_intrinsic_mask (gfc_se * se, gfc_expr * expr, int left)
7858 : : {
7859 : 64 : tree arg, allones, type, utype, res, cond, bitsize;
7860 : 64 : int i;
7861 : :
7862 : 64 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7863 : 64 : arg = gfc_evaluate_now (arg, &se->pre);
7864 : :
7865 : 64 : type = gfc_get_int_type (expr->ts.kind);
7866 : 64 : utype = unsigned_type_for (type);
7867 : :
7868 : 64 : i = gfc_validate_kind (BT_INTEGER, expr->ts.kind, false);
7869 : 64 : bitsize = build_int_cst (TREE_TYPE (arg), gfc_integer_kinds[i].bit_size);
7870 : :
7871 : 64 : allones = fold_build1_loc (input_location, BIT_NOT_EXPR, utype,
7872 : : build_int_cst (utype, 0));
7873 : :
7874 : 64 : if (left)
7875 : : {
7876 : : /* Left-justified mask. */
7877 : 32 : res = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (arg),
7878 : : bitsize, arg);
7879 : 32 : res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7880 : : fold_convert (utype, res));
7881 : :
7882 : : /* Special case arg == 0, because SHIFT_EXPR wants a shift strictly
7883 : : smaller than type width. */
7884 : 32 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
7885 : 32 : build_int_cst (TREE_TYPE (arg), 0));
7886 : 32 : res = fold_build3_loc (input_location, COND_EXPR, utype, cond,
7887 : : build_int_cst (utype, 0), res);
7888 : : }
7889 : : else
7890 : : {
7891 : : /* Right-justified mask. */
7892 : 32 : res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7893 : : fold_convert (utype, arg));
7894 : 32 : res = fold_build1_loc (input_location, BIT_NOT_EXPR, utype, res);
7895 : :
7896 : : /* Special case agr == bit_size, because SHIFT_EXPR wants a shift
7897 : : strictly smaller than type width. */
7898 : 32 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7899 : : arg, bitsize);
7900 : 32 : res = fold_build3_loc (input_location, COND_EXPR, utype,
7901 : : cond, allones, res);
7902 : : }
7903 : :
7904 : 64 : se->expr = fold_convert (type, res);
7905 : 64 : }
7906 : :
7907 : :
7908 : : /* FRACTION (s) is translated into:
7909 : : isfinite (s) ? frexp (s, &dummy_int) : NaN */
7910 : : static void
7911 : 60 : gfc_conv_intrinsic_fraction (gfc_se * se, gfc_expr * expr)
7912 : : {
7913 : 60 : tree arg, type, tmp, res, frexp, cond;
7914 : :
7915 : 60 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7916 : :
7917 : 60 : type = gfc_typenode_for_spec (&expr->ts);
7918 : 60 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7919 : 60 : arg = gfc_evaluate_now (arg, &se->pre);
7920 : :
7921 : 60 : cond = build_call_expr_loc (input_location,
7922 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
7923 : : 1, arg);
7924 : :
7925 : 60 : tmp = gfc_create_var (integer_type_node, NULL);
7926 : 60 : res = build_call_expr_loc (input_location, frexp, 2,
7927 : : fold_convert (type, arg),
7928 : : gfc_build_addr_expr (NULL_TREE, tmp));
7929 : 60 : res = fold_convert (type, res);
7930 : :
7931 : 60 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
7932 : : cond, res, gfc_build_nan (type, ""));
7933 : 60 : }
7934 : :
7935 : :
7936 : : /* NEAREST (s, dir) is translated into
7937 : : tmp = copysign (HUGE_VAL, dir);
7938 : : return nextafter (s, tmp);
7939 : : */
7940 : : static void
7941 : 1595 : gfc_conv_intrinsic_nearest (gfc_se * se, gfc_expr * expr)
7942 : : {
7943 : 1595 : tree args[2], type, tmp, nextafter, copysign, huge_val;
7944 : :
7945 : 1595 : nextafter = gfc_builtin_decl_for_float_kind (BUILT_IN_NEXTAFTER, expr->ts.kind);
7946 : 1595 : copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
7947 : :
7948 : 1595 : type = gfc_typenode_for_spec (&expr->ts);
7949 : 1595 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7950 : :
7951 : 1595 : huge_val = gfc_build_inf_or_huge (type, expr->ts.kind);
7952 : 1595 : tmp = build_call_expr_loc (input_location, copysign, 2, huge_val,
7953 : : fold_convert (type, args[1]));
7954 : 1595 : se->expr = build_call_expr_loc (input_location, nextafter, 2,
7955 : : fold_convert (type, args[0]), tmp);
7956 : 1595 : se->expr = fold_convert (type, se->expr);
7957 : 1595 : }
7958 : :
7959 : :
7960 : : /* SPACING (s) is translated into
7961 : : int e;
7962 : : if (!isfinite (s))
7963 : : res = NaN;
7964 : : else if (s == 0)
7965 : : res = tiny;
7966 : : else
7967 : : {
7968 : : frexp (s, &e);
7969 : : e = e - prec;
7970 : : e = MAX_EXPR (e, emin);
7971 : : res = scalbn (1., e);
7972 : : }
7973 : : return res;
7974 : :
7975 : : where prec is the precision of s, gfc_real_kinds[k].digits,
7976 : : emin is min_exponent - 1, gfc_real_kinds[k].min_exponent - 1,
7977 : : and tiny is tiny(s), gfc_real_kinds[k].tiny. */
7978 : :
7979 : : static void
7980 : 70 : gfc_conv_intrinsic_spacing (gfc_se * se, gfc_expr * expr)
7981 : : {
7982 : 70 : tree arg, type, prec, emin, tiny, res, e;
7983 : 70 : tree cond, nan, tmp, frexp, scalbn;
7984 : 70 : int k;
7985 : 70 : stmtblock_t block;
7986 : :
7987 : 70 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
7988 : 70 : prec = build_int_cst (integer_type_node, gfc_real_kinds[k].digits);
7989 : 70 : emin = build_int_cst (integer_type_node, gfc_real_kinds[k].min_exponent - 1);
7990 : 70 : tiny = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].tiny, expr->ts.kind, 0);
7991 : :
7992 : 70 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7993 : 70 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7994 : :
7995 : 70 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7996 : 70 : arg = gfc_evaluate_now (arg, &se->pre);
7997 : :
7998 : 70 : type = gfc_typenode_for_spec (&expr->ts);
7999 : 70 : e = gfc_create_var (integer_type_node, NULL);
8000 : 70 : res = gfc_create_var (type, NULL);
8001 : :
8002 : :
8003 : : /* Build the block for s /= 0. */
8004 : 70 : gfc_start_block (&block);
8005 : 70 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
8006 : : gfc_build_addr_expr (NULL_TREE, e));
8007 : 70 : gfc_add_expr_to_block (&block, tmp);
8008 : :
8009 : 70 : tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node, e,
8010 : : prec);
8011 : 70 : gfc_add_modify (&block, e, fold_build2_loc (input_location, MAX_EXPR,
8012 : : integer_type_node, tmp, emin));
8013 : :
8014 : 70 : tmp = build_call_expr_loc (input_location, scalbn, 2,
8015 : 70 : build_real_from_int_cst (type, integer_one_node), e);
8016 : 70 : gfc_add_modify (&block, res, tmp);
8017 : :
8018 : : /* Finish by building the IF statement for value zero. */
8019 : 70 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
8020 : 70 : build_real_from_int_cst (type, integer_zero_node));
8021 : 70 : tmp = build3_v (COND_EXPR, cond, build2_v (MODIFY_EXPR, res, tiny),
8022 : : gfc_finish_block (&block));
8023 : :
8024 : : /* And deal with infinities and NaNs. */
8025 : 70 : cond = build_call_expr_loc (input_location,
8026 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
8027 : : 1, arg);
8028 : 70 : nan = gfc_build_nan (type, "");
8029 : 70 : tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, res, nan));
8030 : :
8031 : 70 : gfc_add_expr_to_block (&se->pre, tmp);
8032 : 70 : se->expr = res;
8033 : 70 : }
8034 : :
8035 : :
8036 : : /* RRSPACING (s) is translated into
8037 : : int e;
8038 : : real x;
8039 : : x = fabs (s);
8040 : : if (isfinite (x))
8041 : : {
8042 : : if (x != 0)
8043 : : {
8044 : : frexp (s, &e);
8045 : : x = scalbn (x, precision - e);
8046 : : }
8047 : : }
8048 : : else
8049 : : x = NaN;
8050 : : return x;
8051 : :
8052 : : where precision is gfc_real_kinds[k].digits. */
8053 : :
8054 : : static void
8055 : 48 : gfc_conv_intrinsic_rrspacing (gfc_se * se, gfc_expr * expr)
8056 : : {
8057 : 48 : tree arg, type, e, x, cond, nan, stmt, tmp, frexp, scalbn, fabs;
8058 : 48 : int prec, k;
8059 : 48 : stmtblock_t block;
8060 : :
8061 : 48 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
8062 : 48 : prec = gfc_real_kinds[k].digits;
8063 : :
8064 : 48 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
8065 : 48 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
8066 : 48 : fabs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
8067 : :
8068 : 48 : type = gfc_typenode_for_spec (&expr->ts);
8069 : 48 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
8070 : 48 : arg = gfc_evaluate_now (arg, &se->pre);
8071 : :
8072 : 48 : e = gfc_create_var (integer_type_node, NULL);
8073 : 48 : x = gfc_create_var (type, NULL);
8074 : 48 : gfc_add_modify (&se->pre, x,
8075 : : build_call_expr_loc (input_location, fabs, 1, arg));
8076 : :
8077 : :
8078 : 48 : gfc_start_block (&block);
8079 : 48 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
8080 : : gfc_build_addr_expr (NULL_TREE, e));
8081 : 48 : gfc_add_expr_to_block (&block, tmp);
8082 : :
8083 : 48 : tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
8084 : 48 : build_int_cst (integer_type_node, prec), e);
8085 : 48 : tmp = build_call_expr_loc (input_location, scalbn, 2, x, tmp);
8086 : 48 : gfc_add_modify (&block, x, tmp);
8087 : 48 : stmt = gfc_finish_block (&block);
8088 : :
8089 : : /* if (x != 0) */
8090 : 48 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, x,
8091 : 48 : build_real_from_int_cst (type, integer_zero_node));
8092 : 48 : tmp = build3_v (COND_EXPR, cond, stmt, build_empty_stmt (input_location));
8093 : :
8094 : : /* And deal with infinities and NaNs. */
8095 : 48 : cond = build_call_expr_loc (input_location,
8096 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
8097 : : 1, x);
8098 : 48 : nan = gfc_build_nan (type, "");
8099 : 48 : tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, x, nan));
8100 : :
8101 : 48 : gfc_add_expr_to_block (&se->pre, tmp);
8102 : 48 : se->expr = fold_convert (type, x);
8103 : 48 : }
8104 : :
8105 : :
8106 : : /* SCALE (s, i) is translated into scalbn (s, i). */
8107 : : static void
8108 : 72 : gfc_conv_intrinsic_scale (gfc_se * se, gfc_expr * expr)
8109 : : {
8110 : 72 : tree args[2], type, scalbn;
8111 : :
8112 : 72 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
8113 : :
8114 : 72 : type = gfc_typenode_for_spec (&expr->ts);
8115 : 72 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
8116 : 72 : se->expr = build_call_expr_loc (input_location, scalbn, 2,
8117 : : fold_convert (type, args[0]),
8118 : : fold_convert (integer_type_node, args[1]));
8119 : 72 : se->expr = fold_convert (type, se->expr);
8120 : 72 : }
8121 : :
8122 : :
8123 : : /* SET_EXPONENT (s, i) is translated into
8124 : : isfinite(s) ? scalbn (frexp (s, &dummy_int), i) : NaN */
8125 : : static void
8126 : 262 : gfc_conv_intrinsic_set_exponent (gfc_se * se, gfc_expr * expr)
8127 : : {
8128 : 262 : tree args[2], type, tmp, frexp, scalbn, cond, nan, res;
8129 : :
8130 : 262 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
8131 : 262 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
8132 : :
8133 : 262 : type = gfc_typenode_for_spec (&expr->ts);
8134 : 262 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
8135 : 262 : args[0] = gfc_evaluate_now (args[0], &se->pre);
8136 : :
8137 : 262 : tmp = gfc_create_var (integer_type_node, NULL);
8138 : 262 : tmp = build_call_expr_loc (input_location, frexp, 2,
8139 : : fold_convert (type, args[0]),
8140 : : gfc_build_addr_expr (NULL_TREE, tmp));
8141 : 262 : res = build_call_expr_loc (input_location, scalbn, 2, tmp,
8142 : : fold_convert (integer_type_node, args[1]));
8143 : 262 : res = fold_convert (type, res);
8144 : :
8145 : : /* Call to isfinite */
8146 : 262 : cond = build_call_expr_loc (input_location,
8147 : : builtin_decl_explicit (BUILT_IN_ISFINITE),
8148 : : 1, args[0]);
8149 : 262 : nan = gfc_build_nan (type, "");
8150 : :
8151 : 262 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
8152 : : res, nan);
8153 : 262 : }
8154 : :
8155 : :
8156 : : static void
8157 : 14661 : gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
8158 : : {
8159 : 14661 : gfc_actual_arglist *actual;
8160 : 14661 : tree arg1;
8161 : 14661 : tree type;
8162 : 14661 : tree size;
8163 : 14661 : gfc_se argse;
8164 : 14661 : gfc_expr *e;
8165 : 14661 : gfc_symbol *sym = NULL;
8166 : :
8167 : 14661 : gfc_init_se (&argse, NULL);
8168 : 14661 : actual = expr->value.function.actual;
8169 : :
8170 : 14661 : if (actual->expr->ts.type == BT_CLASS)
8171 : 578 : gfc_add_class_array_ref (actual->expr);
8172 : :
8173 : 14661 : e = actual->expr;
8174 : :
8175 : : /* These are emerging from the interface mapping, when a class valued
8176 : : function appears as the rhs in a realloc on assign statement, where
8177 : : the size of the result is that of one of the actual arguments. */
8178 : 14661 : if (e->expr_type == EXPR_VARIABLE
8179 : 14207 : && e->symtree->n.sym->ns == NULL /* This is distinctive! */
8180 : 555 : && e->symtree->n.sym->ts.type == BT_CLASS
8181 : 44 : && e->ref && e->ref->type == REF_COMPONENT
8182 : 26 : && strcmp (e->ref->u.c.component->name, "_data") == 0)
8183 : 14661 : sym = e->symtree->n.sym;
8184 : :
8185 : 14661 : if ((gfc_option.rtcheck & GFC_RTCHECK_POINTER)
8186 : : && e
8187 : 854 : && (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION))
8188 : : {
8189 : 854 : symbol_attribute attr;
8190 : 854 : char *msg;
8191 : 854 : tree temp;
8192 : 854 : tree cond;
8193 : :
8194 : 854 : if (e->symtree->n.sym && IS_CLASS_ARRAY (e->symtree->n.sym))
8195 : : {
8196 : 33 : attr = CLASS_DATA (e->symtree->n.sym)->attr;
8197 : 33 : attr.pointer = attr.class_pointer;
8198 : : }
8199 : : else
8200 : 821 : attr = gfc_expr_attr (e);
8201 : :
8202 : 854 : if (attr.allocatable)
8203 : 100 : msg = xasprintf ("Allocatable argument '%s' is not allocated",
8204 : 100 : e->symtree->n.sym->name);
8205 : 754 : else if (attr.pointer)
8206 : 46 : msg = xasprintf ("Pointer argument '%s' is not associated",
8207 : 46 : e->symtree->n.sym->name);
8208 : : else
8209 : 708 : goto end_arg_check;
8210 : :
8211 : 146 : if (sym)
8212 : : {
8213 : 0 : temp = gfc_class_data_get (sym->backend_decl);
8214 : 0 : temp = gfc_conv_descriptor_data_get (temp);
8215 : : }
8216 : : else
8217 : : {
8218 : 146 : argse.descriptor_only = 1;
8219 : 146 : gfc_conv_expr_descriptor (&argse, actual->expr);
8220 : 146 : temp = gfc_conv_descriptor_data_get (argse.expr);
8221 : : }
8222 : :
8223 : 146 : cond = fold_build2_loc (input_location, EQ_EXPR,
8224 : : logical_type_node, temp,
8225 : 146 : fold_convert (TREE_TYPE (temp),
8226 : : null_pointer_node));
8227 : 146 : gfc_trans_runtime_check (true, false, cond, &argse.pre, &e->where, msg);
8228 : :
8229 : 146 : free (msg);
8230 : : }
8231 : 13807 : end_arg_check:
8232 : :
8233 : 14661 : argse.data_not_needed = 1;
8234 : 14661 : if (gfc_is_class_array_function (e))
8235 : : {
8236 : : /* For functions that return a class array conv_expr_descriptor is not
8237 : : able to get the descriptor right. Therefore this special case. */
8238 : 6 : gfc_conv_expr_reference (&argse, e);
8239 : 6 : argse.expr = gfc_class_data_get (argse.expr);
8240 : : }
8241 : 14655 : else if (sym && sym->backend_decl)
8242 : : {
8243 : 14 : gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (sym->backend_decl)));
8244 : 14 : argse.expr = gfc_class_data_get (sym->backend_decl);
8245 : : }
8246 : : else
8247 : 14641 : gfc_conv_expr_descriptor (&argse, actual->expr);
8248 : 14661 : gfc_add_block_to_block (&se->pre, &argse.pre);
8249 : 14661 : gfc_add_block_to_block (&se->post, &argse.post);
8250 : 14661 : arg1 = argse.expr;
8251 : :
8252 : 14661 : actual = actual->next;
8253 : 14661 : if (actual->expr)
8254 : : {
8255 : 8677 : stmtblock_t block;
8256 : 8677 : gfc_init_block (&block);
8257 : 8677 : gfc_init_se (&argse, NULL);
8258 : 8677 : gfc_conv_expr_type (&argse, actual->expr,
8259 : : gfc_array_index_type);
8260 : 8677 : gfc_add_block_to_block (&block, &argse.pre);
8261 : 8677 : tree tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8262 : : argse.expr, gfc_index_one_node);
8263 : 8677 : size = gfc_tree_array_size (&block, arg1, e, tmp);
8264 : :
8265 : : /* Unusually, for an intrinsic, size does not exclude
8266 : : an optional arg2, so we must test for it. */
8267 : 8677 : if (actual->expr->expr_type == EXPR_VARIABLE
8268 : 2049 : && actual->expr->symtree->n.sym->attr.dummy
8269 : 31 : && actual->expr->symtree->n.sym->attr.optional)
8270 : : {
8271 : 31 : tree cond;
8272 : 31 : stmtblock_t block2;
8273 : 31 : gfc_init_block (&block2);
8274 : 31 : gfc_init_se (&argse, NULL);
8275 : 31 : argse.want_pointer = 1;
8276 : 31 : argse.data_not_needed = 1;
8277 : 31 : gfc_conv_expr (&argse, actual->expr);
8278 : 31 : gfc_add_block_to_block (&se->pre, &argse.pre);
8279 : : /* 'block2' contains the arg2 absent case, 'block' the arg2 present
8280 : : case; size_var can be used in both blocks. */
8281 : 31 : tree size_var = gfc_create_var (TREE_TYPE (size), "size");
8282 : 31 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8283 : 31 : TREE_TYPE (size_var), size_var, size);
8284 : 31 : gfc_add_expr_to_block (&block, tmp);
8285 : 31 : size = gfc_tree_array_size (&block2, arg1, e, NULL_TREE);
8286 : 31 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8287 : 31 : TREE_TYPE (size_var), size_var, size);
8288 : 31 : gfc_add_expr_to_block (&block2, tmp);
8289 : 31 : cond = gfc_conv_expr_present (actual->expr->symtree->n.sym);
8290 : 31 : tmp = build3_v (COND_EXPR, cond, gfc_finish_block (&block),
8291 : : gfc_finish_block (&block2));
8292 : 31 : gfc_add_expr_to_block (&se->pre, tmp);
8293 : 31 : size = size_var;
8294 : 31 : }
8295 : : else
8296 : 8646 : gfc_add_block_to_block (&se->pre, &block);
8297 : : }
8298 : : else
8299 : 5984 : size = gfc_tree_array_size (&se->pre, arg1, e, NULL_TREE);
8300 : 14661 : type = gfc_typenode_for_spec (&expr->ts);
8301 : 14661 : se->expr = convert (type, size);
8302 : 14661 : }
8303 : :
8304 : :
8305 : : /* Helper function to compute the size of a character variable,
8306 : : excluding the terminating null characters. The result has
8307 : : gfc_array_index_type type. */
8308 : :
8309 : : tree
8310 : 1779 : size_of_string_in_bytes (int kind, tree string_length)
8311 : : {
8312 : 1779 : tree bytesize;
8313 : 1779 : int i = gfc_validate_kind (BT_CHARACTER, kind, false);
8314 : :
8315 : 3558 : bytesize = build_int_cst (gfc_array_index_type,
8316 : 1779 : gfc_character_kinds[i].bit_size / 8);
8317 : :
8318 : 1779 : return fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8319 : : bytesize,
8320 : 1779 : fold_convert (gfc_array_index_type, string_length));
8321 : : }
8322 : :
8323 : :
8324 : : static void
8325 : 1304 : gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr)
8326 : : {
8327 : 1304 : gfc_expr *arg;
8328 : 1304 : gfc_se argse;
8329 : 1304 : tree source_bytes;
8330 : 1304 : tree tmp;
8331 : 1304 : tree lower;
8332 : 1304 : tree upper;
8333 : 1304 : tree byte_size;
8334 : 1304 : tree field;
8335 : 1304 : int n;
8336 : :
8337 : 1304 : gfc_init_se (&argse, NULL);
8338 : 1304 : arg = expr->value.function.actual->expr;
8339 : :
8340 : 1304 : if (arg->rank || arg->ts.type == BT_ASSUMED)
8341 : 1012 : gfc_conv_expr_descriptor (&argse, arg);
8342 : : else
8343 : 292 : gfc_conv_expr_reference (&argse, arg);
8344 : :
8345 : 1304 : if (arg->ts.type == BT_ASSUMED)
8346 : : {
8347 : : /* This only works if an array descriptor has been passed; thus, extract
8348 : : the size from the descriptor. */
8349 : 172 : gcc_assert (TYPE_PRECISION (gfc_array_index_type)
8350 : : == TYPE_PRECISION (size_type_node));
8351 : 172 : tmp = arg->symtree->n.sym->backend_decl;
8352 : 172 : tmp = DECL_LANG_SPECIFIC (tmp)
8353 : 60 : && GFC_DECL_SAVED_DESCRIPTOR (tmp) != NULL_TREE
8354 : 226 : ? GFC_DECL_SAVED_DESCRIPTOR (tmp) : tmp;
8355 : 172 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
8356 : 172 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8357 : :
8358 : 172 : tmp = gfc_conv_descriptor_dtype (tmp);
8359 : 172 : field = gfc_advance_chain (TYPE_FIELDS (get_dtype_type_node ()),
8360 : : GFC_DTYPE_ELEM_LEN);
8361 : 172 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
8362 : : tmp, field, NULL_TREE);
8363 : :
8364 : 172 : byte_size = fold_convert (gfc_array_index_type, tmp);
8365 : : }
8366 : 1132 : else if (arg->ts.type == BT_CLASS)
8367 : : {
8368 : : /* Conv_expr_descriptor returns a component_ref to _data component of the
8369 : : class object. The class object may be a non-pointer object, e.g.
8370 : : located on the stack, or a memory location pointed to, e.g. a
8371 : : parameter, i.e., an indirect_ref. */
8372 : 954 : if (POINTER_TYPE_P (TREE_TYPE (argse.expr))
8373 : 584 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (argse.expr))))
8374 : 198 : byte_size
8375 : 198 : = gfc_class_vtab_size_get (build_fold_indirect_ref (argse.expr));
8376 : 386 : else if (GFC_CLASS_TYPE_P (TREE_TYPE (argse.expr)))
8377 : 0 : byte_size = gfc_class_vtab_size_get (argse.expr);
8378 : 386 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (argse.expr))
8379 : 386 : && TREE_CODE (argse.expr) == COMPONENT_REF)
8380 : 328 : byte_size = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8381 : 58 : else if (arg->rank > 0
8382 : 16 : || (arg->rank == 0
8383 : 16 : && arg->ref && arg->ref->type == REF_COMPONENT))
8384 : : {
8385 : : /* The scalarizer added an additional temp. To get the class' vptr
8386 : : one has to look at the original backend_decl. */
8387 : 58 : if (argse.class_container)
8388 : 16 : byte_size = gfc_class_vtab_size_get (argse.class_container);
8389 : 42 : else if (DECL_LANG_SPECIFIC (arg->symtree->n.sym->backend_decl))
8390 : 84 : byte_size = gfc_class_vtab_size_get (
8391 : 42 : GFC_DECL_SAVED_DESCRIPTOR (arg->symtree->n.sym->backend_decl));
8392 : : else
8393 : 0 : gcc_unreachable ();
8394 : : }
8395 : : else
8396 : 0 : gcc_unreachable ();
8397 : : }
8398 : : else
8399 : : {
8400 : 548 : if (arg->ts.type == BT_CHARACTER)
8401 : 84 : byte_size = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8402 : : else
8403 : : {
8404 : 464 : if (arg->rank == 0)
8405 : 0 : byte_size = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8406 : : argse.expr));
8407 : : else
8408 : 464 : byte_size = gfc_get_element_type (TREE_TYPE (argse.expr));
8409 : 464 : byte_size = fold_convert (gfc_array_index_type,
8410 : : size_in_bytes (byte_size));
8411 : : }
8412 : : }
8413 : :
8414 : 1304 : if (arg->rank == 0)
8415 : 292 : se->expr = byte_size;
8416 : : else
8417 : : {
8418 : 1012 : source_bytes = gfc_create_var (gfc_array_index_type, "bytes");
8419 : 1012 : gfc_add_modify (&argse.pre, source_bytes, byte_size);
8420 : :
8421 : 1012 : if (arg->rank == -1)
8422 : : {
8423 : 365 : tree cond, loop_var, exit_label;
8424 : 365 : stmtblock_t body;
8425 : :
8426 : 365 : tmp = fold_convert (gfc_array_index_type,
8427 : : gfc_conv_descriptor_rank (argse.expr));
8428 : 365 : loop_var = gfc_create_var (gfc_array_index_type, "i");
8429 : 365 : gfc_add_modify (&argse.pre, loop_var, gfc_index_zero_node);
8430 : 365 : exit_label = gfc_build_label_decl (NULL_TREE);
8431 : :
8432 : : /* Create loop:
8433 : : for (;;)
8434 : : {
8435 : : if (i >= rank)
8436 : : goto exit;
8437 : : source_bytes = source_bytes * array.dim[i].extent;
8438 : : i = i + 1;
8439 : : }
8440 : : exit: */
8441 : 365 : gfc_start_block (&body);
8442 : 365 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
8443 : : loop_var, tmp);
8444 : 365 : tmp = build1_v (GOTO_EXPR, exit_label);
8445 : 365 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
8446 : : cond, tmp, build_empty_stmt (input_location));
8447 : 365 : gfc_add_expr_to_block (&body, tmp);
8448 : :
8449 : 365 : lower = gfc_conv_descriptor_lbound_get (argse.expr, loop_var);
8450 : 365 : upper = gfc_conv_descriptor_ubound_get (argse.expr, loop_var);
8451 : 365 : tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8452 : 365 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8453 : : gfc_array_index_type, tmp, source_bytes);
8454 : 365 : gfc_add_modify (&body, source_bytes, tmp);
8455 : :
8456 : 365 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
8457 : : gfc_array_index_type, loop_var,
8458 : : gfc_index_one_node);
8459 : 365 : gfc_add_modify_loc (input_location, &body, loop_var, tmp);
8460 : :
8461 : 365 : tmp = gfc_finish_block (&body);
8462 : :
8463 : 365 : tmp = fold_build1_loc (input_location, LOOP_EXPR, void_type_node,
8464 : : tmp);
8465 : 365 : gfc_add_expr_to_block (&argse.pre, tmp);
8466 : :
8467 : 365 : tmp = build1_v (LABEL_EXPR, exit_label);
8468 : 365 : gfc_add_expr_to_block (&argse.pre, tmp);
8469 : : }
8470 : : else
8471 : : {
8472 : : /* Obtain the size of the array in bytes. */
8473 : 1834 : for (n = 0; n < arg->rank; n++)
8474 : : {
8475 : 1187 : tree idx;
8476 : 1187 : idx = gfc_rank_cst[n];
8477 : 1187 : lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8478 : 1187 : upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8479 : 1187 : tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8480 : 1187 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8481 : : gfc_array_index_type, tmp, source_bytes);
8482 : 1187 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8483 : : }
8484 : : }
8485 : 1012 : se->expr = source_bytes;
8486 : : }
8487 : :
8488 : 1304 : gfc_add_block_to_block (&se->pre, &argse.pre);
8489 : 1304 : }
8490 : :
8491 : :
8492 : : static void
8493 : 802 : gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
8494 : : {
8495 : 802 : gfc_expr *arg;
8496 : 802 : gfc_se argse;
8497 : 802 : tree type, result_type, tmp, class_decl = NULL;
8498 : 802 : gfc_symbol *sym;
8499 : 802 : bool unlimited = false;
8500 : :
8501 : 802 : arg = expr->value.function.actual->expr;
8502 : :
8503 : 802 : gfc_init_se (&argse, NULL);
8504 : 802 : result_type = gfc_get_int_type (expr->ts.kind);
8505 : :
8506 : 802 : if (arg->rank == 0)
8507 : : {
8508 : 211 : if (arg->ts.type == BT_CLASS)
8509 : : {
8510 : 86 : unlimited = UNLIMITED_POLY (arg);
8511 : 86 : gfc_add_vptr_component (arg);
8512 : 86 : gfc_add_size_component (arg);
8513 : 86 : gfc_conv_expr (&argse, arg);
8514 : 86 : tmp = fold_convert (result_type, argse.expr);
8515 : 86 : class_decl = gfc_get_class_from_expr (argse.expr);
8516 : 86 : goto done;
8517 : : }
8518 : :
8519 : 125 : gfc_conv_expr_reference (&argse, arg);
8520 : 125 : type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8521 : : argse.expr));
8522 : : }
8523 : : else
8524 : : {
8525 : 591 : argse.want_pointer = 0;
8526 : 591 : gfc_conv_expr_descriptor (&argse, arg);
8527 : 591 : sym = arg->expr_type == EXPR_VARIABLE ? arg->symtree->n.sym : NULL;
8528 : 591 : if (arg->ts.type == BT_CLASS)
8529 : : {
8530 : 60 : unlimited = UNLIMITED_POLY (arg);
8531 : 60 : if (TREE_CODE (argse.expr) == COMPONENT_REF)
8532 : 54 : tmp = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8533 : 6 : else if (arg->rank > 0 && sym
8534 : 12 : && DECL_LANG_SPECIFIC (sym->backend_decl))
8535 : 12 : tmp = gfc_class_vtab_size_get (
8536 : 6 : GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl));
8537 : : else
8538 : 0 : gcc_unreachable ();
8539 : 60 : tmp = fold_convert (result_type, tmp);
8540 : 60 : class_decl = gfc_get_class_from_expr (argse.expr);
8541 : 60 : goto done;
8542 : : }
8543 : 531 : type = gfc_get_element_type (TREE_TYPE (argse.expr));
8544 : : }
8545 : :
8546 : : /* Obtain the argument's word length. */
8547 : 656 : if (arg->ts.type == BT_CHARACTER)
8548 : 241 : tmp = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8549 : : else
8550 : 415 : tmp = size_in_bytes (type);
8551 : 656 : tmp = fold_convert (result_type, tmp);
8552 : :
8553 : 802 : done:
8554 : 802 : if (unlimited && class_decl)
8555 : 68 : tmp = gfc_resize_class_size_with_len (NULL, class_decl, tmp);
8556 : :
8557 : 802 : se->expr = fold_build2_loc (input_location, MULT_EXPR, result_type, tmp,
8558 : : build_int_cst (result_type, BITS_PER_UNIT));
8559 : 802 : gfc_add_block_to_block (&se->pre, &argse.pre);
8560 : 802 : }
8561 : :
8562 : :
8563 : : /* Intrinsic string comparison functions. */
8564 : :
8565 : : static void
8566 : 99 : gfc_conv_intrinsic_strcmp (gfc_se * se, gfc_expr * expr, enum tree_code op)
8567 : : {
8568 : 99 : tree args[4];
8569 : :
8570 : 99 : gfc_conv_intrinsic_function_args (se, expr, args, 4);
8571 : :
8572 : 99 : se->expr
8573 : 198 : = gfc_build_compare_string (args[0], args[1], args[2], args[3],
8574 : 99 : expr->value.function.actual->expr->ts.kind,
8575 : : op);
8576 : 99 : se->expr = fold_build2_loc (input_location, op,
8577 : : gfc_typenode_for_spec (&expr->ts), se->expr,
8578 : 99 : build_int_cst (TREE_TYPE (se->expr), 0));
8579 : 99 : }
8580 : :
8581 : : /* Generate a call to the adjustl/adjustr library function. */
8582 : : static void
8583 : 462 : gfc_conv_intrinsic_adjust (gfc_se * se, gfc_expr * expr, tree fndecl)
8584 : : {
8585 : 462 : tree args[3];
8586 : 462 : tree len;
8587 : 462 : tree type;
8588 : 462 : tree var;
8589 : 462 : tree tmp;
8590 : :
8591 : 462 : gfc_conv_intrinsic_function_args (se, expr, &args[1], 2);
8592 : 462 : len = args[1];
8593 : :
8594 : 462 : type = TREE_TYPE (args[2]);
8595 : 462 : var = gfc_conv_string_tmp (se, type, len);
8596 : 462 : args[0] = var;
8597 : :
8598 : 462 : tmp = build_call_expr_loc (input_location,
8599 : : fndecl, 3, args[0], args[1], args[2]);
8600 : 462 : gfc_add_expr_to_block (&se->pre, tmp);
8601 : 462 : se->expr = var;
8602 : 462 : se->string_length = len;
8603 : 462 : }
8604 : :
8605 : :
8606 : : /* Generate code for the TRANSFER intrinsic:
8607 : : For scalar results:
8608 : : DEST = TRANSFER (SOURCE, MOLD)
8609 : : where:
8610 : : typeof<DEST> = typeof<MOLD>
8611 : : and:
8612 : : MOLD is scalar.
8613 : :
8614 : : For array results:
8615 : : DEST(1:N) = TRANSFER (SOURCE, MOLD[, SIZE])
8616 : : where:
8617 : : typeof<DEST> = typeof<MOLD>
8618 : : and:
8619 : : N = min (sizeof (SOURCE(:)), sizeof (DEST(:)),
8620 : : sizeof (DEST(0) * SIZE). */
8621 : : static void
8622 : 3334 : gfc_conv_intrinsic_transfer (gfc_se * se, gfc_expr * expr)
8623 : : {
8624 : 3334 : tree tmp;
8625 : 3334 : tree tmpdecl;
8626 : 3334 : tree ptr;
8627 : 3334 : tree extent;
8628 : 3334 : tree source;
8629 : 3334 : tree source_type;
8630 : 3334 : tree source_bytes;
8631 : 3334 : tree mold_type;
8632 : 3334 : tree dest_word_len;
8633 : 3334 : tree size_words;
8634 : 3334 : tree size_bytes;
8635 : 3334 : tree upper;
8636 : 3334 : tree lower;
8637 : 3334 : tree stmt;
8638 : 3334 : tree class_ref = NULL_TREE;
8639 : 3334 : gfc_actual_arglist *arg;
8640 : 3334 : gfc_se argse;
8641 : 3334 : gfc_array_info *info;
8642 : 3334 : stmtblock_t block;
8643 : 3334 : int n;
8644 : 3334 : bool scalar_mold;
8645 : 3334 : gfc_expr *source_expr, *mold_expr, *class_expr;
8646 : :
8647 : 3334 : info = NULL;
8648 : 3334 : if (se->loop)
8649 : 456 : info = &se->ss->info->data.array;
8650 : :
8651 : : /* Convert SOURCE. The output from this stage is:-
8652 : : source_bytes = length of the source in bytes
8653 : : source = pointer to the source data. */
8654 : 3334 : arg = expr->value.function.actual;
8655 : 3334 : source_expr = arg->expr;
8656 : :
8657 : : /* Ensure double transfer through LOGICAL preserves all
8658 : : the needed bits. */
8659 : 3334 : if (arg->expr->expr_type == EXPR_FUNCTION
8660 : 2361 : && arg->expr->value.function.esym == NULL
8661 : 2343 : && arg->expr->value.function.isym != NULL
8662 : 2343 : && arg->expr->value.function.isym->id == GFC_ISYM_TRANSFER
8663 : 12 : && arg->expr->ts.type == BT_LOGICAL
8664 : 12 : && expr->ts.type != arg->expr->ts.type)
8665 : 12 : arg->expr->value.function.name = "__transfer_in_transfer";
8666 : :
8667 : 3334 : gfc_init_se (&argse, NULL);
8668 : :
8669 : 3334 : source_bytes = gfc_create_var (gfc_array_index_type, NULL);
8670 : :
8671 : : /* Obtain the pointer to source and the length of source in bytes. */
8672 : 3334 : if (arg->expr->rank == 0)
8673 : : {
8674 : 2994 : gfc_conv_expr_reference (&argse, arg->expr);
8675 : 2994 : if (arg->expr->ts.type == BT_CLASS)
8676 : : {
8677 : 37 : tmp = build_fold_indirect_ref_loc (input_location, argse.expr);
8678 : 37 : if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
8679 : : {
8680 : 19 : source = gfc_class_data_get (tmp);
8681 : 19 : class_ref = tmp;
8682 : : }
8683 : : else
8684 : : {
8685 : : /* Array elements are evaluated as a reference to the data.
8686 : : To obtain the vptr for the element size, the argument
8687 : : expression must be stripped to the class reference and
8688 : : re-evaluated. The pre and post blocks are not needed. */
8689 : 18 : gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
8690 : 18 : source = argse.expr;
8691 : 18 : class_expr = gfc_find_and_cut_at_last_class_ref (arg->expr);
8692 : 18 : gfc_init_se (&argse, NULL);
8693 : 18 : gfc_conv_expr (&argse, class_expr);
8694 : 18 : class_ref = argse.expr;
8695 : : }
8696 : : }
8697 : : else
8698 : 2957 : source = argse.expr;
8699 : :
8700 : : /* Obtain the source word length. */
8701 : 2994 : switch (arg->expr->ts.type)
8702 : : {
8703 : 294 : case BT_CHARACTER:
8704 : 294 : tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8705 : : argse.string_length);
8706 : 294 : break;
8707 : 37 : case BT_CLASS:
8708 : 37 : if (class_ref != NULL_TREE)
8709 : : {
8710 : 37 : tmp = gfc_class_vtab_size_get (class_ref);
8711 : 37 : if (UNLIMITED_POLY (source_expr))
8712 : 30 : tmp = gfc_resize_class_size_with_len (NULL, class_ref, tmp);
8713 : : }
8714 : : else
8715 : : {
8716 : 0 : tmp = gfc_class_vtab_size_get (argse.expr);
8717 : 0 : if (UNLIMITED_POLY (source_expr))
8718 : 0 : tmp = gfc_resize_class_size_with_len (NULL, argse.expr, tmp);
8719 : : }
8720 : : break;
8721 : 2663 : default:
8722 : 2663 : source_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8723 : : source));
8724 : 2663 : tmp = fold_convert (gfc_array_index_type,
8725 : : size_in_bytes (source_type));
8726 : 2663 : break;
8727 : : }
8728 : : }
8729 : : else
8730 : : {
8731 : 340 : argse.want_pointer = 0;
8732 : 340 : gfc_conv_expr_descriptor (&argse, arg->expr);
8733 : 340 : source = gfc_conv_descriptor_data_get (argse.expr);
8734 : 340 : source_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8735 : :
8736 : : /* Repack the source if not simply contiguous. */
8737 : 340 : if (!gfc_is_simply_contiguous (arg->expr, false, true))
8738 : : {
8739 : 74 : tmp = gfc_build_addr_expr (NULL_TREE, argse.expr);
8740 : :
8741 : 74 : if (warn_array_temporaries)
8742 : 0 : gfc_warning (OPT_Warray_temporaries,
8743 : : "Creating array temporary at %L", &expr->where);
8744 : :
8745 : 74 : source = build_call_expr_loc (input_location,
8746 : : gfor_fndecl_in_pack, 1, tmp);
8747 : 74 : source = gfc_evaluate_now (source, &argse.pre);
8748 : :
8749 : : /* Free the temporary. */
8750 : 74 : gfc_start_block (&block);
8751 : 74 : tmp = gfc_call_free (source);
8752 : 74 : gfc_add_expr_to_block (&block, tmp);
8753 : 74 : stmt = gfc_finish_block (&block);
8754 : :
8755 : : /* Clean up if it was repacked. */
8756 : 74 : gfc_init_block (&block);
8757 : 74 : tmp = gfc_conv_array_data (argse.expr);
8758 : 74 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
8759 : : source, tmp);
8760 : 74 : tmp = build3_v (COND_EXPR, tmp, stmt,
8761 : : build_empty_stmt (input_location));
8762 : 74 : gfc_add_expr_to_block (&block, tmp);
8763 : 74 : gfc_add_block_to_block (&block, &se->post);
8764 : 74 : gfc_init_block (&se->post);
8765 : 74 : gfc_add_block_to_block (&se->post, &block);
8766 : : }
8767 : :
8768 : : /* Obtain the source word length. */
8769 : 340 : if (arg->expr->ts.type == BT_CHARACTER)
8770 : 144 : tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8771 : : argse.string_length);
8772 : 196 : else if (arg->expr->ts.type == BT_CLASS)
8773 : : {
8774 : 54 : if (UNLIMITED_POLY (source_expr)
8775 : 54 : && DECL_LANG_SPECIFIC (source_expr->symtree->n.sym->backend_decl))
8776 : 12 : class_ref = GFC_DECL_SAVED_DESCRIPTOR
8777 : : (source_expr->symtree->n.sym->backend_decl);
8778 : : else
8779 : 42 : class_ref = TREE_OPERAND (argse.expr, 0);
8780 : 54 : tmp = gfc_class_vtab_size_get (class_ref);
8781 : 54 : if (UNLIMITED_POLY (arg->expr))
8782 : 54 : tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8783 : : }
8784 : : else
8785 : 142 : tmp = fold_convert (gfc_array_index_type,
8786 : : size_in_bytes (source_type));
8787 : :
8788 : : /* Obtain the size of the array in bytes. */
8789 : 340 : extent = gfc_create_var (gfc_array_index_type, NULL);
8790 : 710 : for (n = 0; n < arg->expr->rank; n++)
8791 : : {
8792 : 370 : tree idx;
8793 : 370 : idx = gfc_rank_cst[n];
8794 : 370 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8795 : 370 : lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8796 : 370 : upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8797 : 370 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
8798 : : gfc_array_index_type, upper, lower);
8799 : 370 : gfc_add_modify (&argse.pre, extent, tmp);
8800 : 370 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
8801 : : gfc_array_index_type, extent,
8802 : : gfc_index_one_node);
8803 : 370 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8804 : : gfc_array_index_type, tmp, source_bytes);
8805 : : }
8806 : : }
8807 : :
8808 : 3334 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8809 : 3334 : gfc_add_block_to_block (&se->pre, &argse.pre);
8810 : 3334 : gfc_add_block_to_block (&se->post, &argse.post);
8811 : :
8812 : : /* Now convert MOLD. The outputs are:
8813 : : mold_type = the TREE type of MOLD
8814 : : dest_word_len = destination word length in bytes. */
8815 : 3334 : arg = arg->next;
8816 : 3334 : mold_expr = arg->expr;
8817 : :
8818 : 3334 : gfc_init_se (&argse, NULL);
8819 : :
8820 : 3334 : scalar_mold = arg->expr->rank == 0;
8821 : :
8822 : 3334 : if (arg->expr->rank == 0)
8823 : : {
8824 : 3027 : gfc_conv_expr_reference (&argse, mold_expr);
8825 : 3027 : mold_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8826 : : argse.expr));
8827 : : }
8828 : : else
8829 : : {
8830 : 307 : argse.want_pointer = 0;
8831 : 307 : gfc_conv_expr_descriptor (&argse, mold_expr);
8832 : 307 : mold_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8833 : : }
8834 : :
8835 : 3334 : gfc_add_block_to_block (&se->pre, &argse.pre);
8836 : 3334 : gfc_add_block_to_block (&se->post, &argse.post);
8837 : :
8838 : 3334 : if (strcmp (expr->value.function.name, "__transfer_in_transfer") == 0)
8839 : : {
8840 : : /* If this TRANSFER is nested in another TRANSFER, use a type
8841 : : that preserves all bits. */
8842 : 12 : if (mold_expr->ts.type == BT_LOGICAL)
8843 : 12 : mold_type = gfc_get_int_type (mold_expr->ts.kind);
8844 : : }
8845 : :
8846 : : /* Obtain the destination word length. */
8847 : 3334 : switch (mold_expr->ts.type)
8848 : : {
8849 : 467 : case BT_CHARACTER:
8850 : 467 : tmp = size_of_string_in_bytes (mold_expr->ts.kind, argse.string_length);
8851 : 467 : mold_type = gfc_get_character_type_len (mold_expr->ts.kind,
8852 : : argse.string_length);
8853 : 467 : break;
8854 : 6 : case BT_CLASS:
8855 : 6 : if (scalar_mold)
8856 : 6 : class_ref = argse.expr;
8857 : : else
8858 : 0 : class_ref = TREE_OPERAND (argse.expr, 0);
8859 : 6 : tmp = gfc_class_vtab_size_get (class_ref);
8860 : 6 : if (UNLIMITED_POLY (arg->expr))
8861 : 0 : tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8862 : : break;
8863 : 2861 : default:
8864 : 2861 : tmp = fold_convert (gfc_array_index_type, size_in_bytes (mold_type));
8865 : 2861 : break;
8866 : : }
8867 : :
8868 : : /* Do not fix dest_word_len if it is a variable, since the temporary can wind
8869 : : up being used before the assignment. */
8870 : 3334 : if (mold_expr->ts.type == BT_CHARACTER && mold_expr->ts.deferred)
8871 : : dest_word_len = tmp;
8872 : : else
8873 : : {
8874 : 3280 : dest_word_len = gfc_create_var (gfc_array_index_type, NULL);
8875 : 3280 : gfc_add_modify (&se->pre, dest_word_len, tmp);
8876 : : }
8877 : :
8878 : : /* Finally convert SIZE, if it is present. */
8879 : 3334 : arg = arg->next;
8880 : 3334 : size_words = gfc_create_var (gfc_array_index_type, NULL);
8881 : :
8882 : 3334 : if (arg->expr)
8883 : : {
8884 : 222 : gfc_init_se (&argse, NULL);
8885 : 222 : gfc_conv_expr_reference (&argse, arg->expr);
8886 : 222 : tmp = convert (gfc_array_index_type,
8887 : : build_fold_indirect_ref_loc (input_location,
8888 : : argse.expr));
8889 : 222 : gfc_add_block_to_block (&se->pre, &argse.pre);
8890 : 222 : gfc_add_block_to_block (&se->post, &argse.post);
8891 : : }
8892 : : else
8893 : : tmp = NULL_TREE;
8894 : :
8895 : : /* Separate array and scalar results. */
8896 : 3334 : if (scalar_mold && tmp == NULL_TREE)
8897 : 2878 : goto scalar_transfer;
8898 : :
8899 : 456 : size_bytes = gfc_create_var (gfc_array_index_type, NULL);
8900 : 456 : if (tmp != NULL_TREE)
8901 : 222 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8902 : : tmp, dest_word_len);
8903 : : else
8904 : : tmp = source_bytes;
8905 : :
8906 : 456 : gfc_add_modify (&se->pre, size_bytes, tmp);
8907 : 456 : gfc_add_modify (&se->pre, size_words,
8908 : : fold_build2_loc (input_location, CEIL_DIV_EXPR,
8909 : : gfc_array_index_type,
8910 : : size_bytes, dest_word_len));
8911 : :
8912 : : /* Evaluate the bounds of the result. If the loop range exists, we have
8913 : : to check if it is too large. If so, we modify loop->to be consistent
8914 : : with min(size, size(source)). Otherwise, size is made consistent with
8915 : : the loop range, so that the right number of bytes is transferred.*/
8916 : 456 : n = se->loop->order[0];
8917 : 456 : if (se->loop->to[n] != NULL_TREE)
8918 : : {
8919 : 201 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8920 : : se->loop->to[n], se->loop->from[n]);
8921 : 201 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8922 : : tmp, gfc_index_one_node);
8923 : 201 : tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
8924 : : tmp, size_words);
8925 : 201 : gfc_add_modify (&se->pre, size_words, tmp);
8926 : 201 : gfc_add_modify (&se->pre, size_bytes,
8927 : : fold_build2_loc (input_location, MULT_EXPR,
8928 : : gfc_array_index_type,
8929 : : size_words, dest_word_len));
8930 : 402 : upper = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8931 : 201 : size_words, se->loop->from[n]);
8932 : 201 : upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8933 : : upper, gfc_index_one_node);
8934 : : }
8935 : : else
8936 : : {
8937 : 255 : upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8938 : : size_words, gfc_index_one_node);
8939 : 255 : se->loop->from[n] = gfc_index_zero_node;
8940 : : }
8941 : :
8942 : 456 : se->loop->to[n] = upper;
8943 : :
8944 : : /* Build a destination descriptor, using the pointer, source, as the
8945 : : data field. */
8946 : 456 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss, mold_type,
8947 : : NULL_TREE, false, true, false, &expr->where);
8948 : :
8949 : : /* Cast the pointer to the result. */
8950 : 456 : tmp = gfc_conv_descriptor_data_get (info->descriptor);
8951 : 456 : tmp = fold_convert (pvoid_type_node, tmp);
8952 : :
8953 : : /* Use memcpy to do the transfer. */
8954 : 456 : tmp
8955 : 456 : = build_call_expr_loc (input_location,
8956 : : builtin_decl_explicit (BUILT_IN_MEMCPY), 3, tmp,
8957 : : fold_convert (pvoid_type_node, source),
8958 : : fold_convert (size_type_node,
8959 : : fold_build2_loc (input_location,
8960 : : MIN_EXPR,
8961 : : gfc_array_index_type,
8962 : : size_bytes,
8963 : : source_bytes)));
8964 : 456 : gfc_add_expr_to_block (&se->pre, tmp);
8965 : :
8966 : 456 : se->expr = info->descriptor;
8967 : 456 : if (expr->ts.type == BT_CHARACTER)
8968 : : {
8969 : 275 : tmp = fold_convert (gfc_charlen_type_node,
8970 : : TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
8971 : 275 : se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
8972 : : gfc_charlen_type_node,
8973 : : dest_word_len, tmp);
8974 : : }
8975 : :
8976 : 456 : return;
8977 : :
8978 : : /* Deal with scalar results. */
8979 : 2878 : scalar_transfer:
8980 : 2878 : extent = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
8981 : : dest_word_len, source_bytes);
8982 : 2878 : extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
8983 : : extent, gfc_index_zero_node);
8984 : :
8985 : 2878 : if (expr->ts.type == BT_CHARACTER)
8986 : : {
8987 : 192 : tree direct, indirect, free;
8988 : :
8989 : 192 : ptr = convert (gfc_get_pchar_type (expr->ts.kind), source);
8990 : 192 : tmpdecl = gfc_create_var (gfc_get_pchar_type (expr->ts.kind),
8991 : : "transfer");
8992 : :
8993 : : /* If source is longer than the destination, use a pointer to
8994 : : the source directly. */
8995 : 192 : gfc_init_block (&block);
8996 : 192 : gfc_add_modify (&block, tmpdecl, ptr);
8997 : 192 : direct = gfc_finish_block (&block);
8998 : :
8999 : : /* Otherwise, allocate a string with the length of the destination
9000 : : and copy the source into it. */
9001 : 192 : gfc_init_block (&block);
9002 : 192 : tmp = gfc_get_pchar_type (expr->ts.kind);
9003 : 192 : tmp = gfc_call_malloc (&block, tmp, dest_word_len);
9004 : 192 : gfc_add_modify (&block, tmpdecl,
9005 : 192 : fold_convert (TREE_TYPE (ptr), tmp));
9006 : 192 : tmp = build_call_expr_loc (input_location,
9007 : : builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
9008 : : fold_convert (pvoid_type_node, tmpdecl),
9009 : : fold_convert (pvoid_type_node, ptr),
9010 : : fold_convert (size_type_node, extent));
9011 : 192 : gfc_add_expr_to_block (&block, tmp);
9012 : 192 : indirect = gfc_finish_block (&block);
9013 : :
9014 : : /* Wrap it up with the condition. */
9015 : 192 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
9016 : : dest_word_len, source_bytes);
9017 : 192 : tmp = build3_v (COND_EXPR, tmp, direct, indirect);
9018 : 192 : gfc_add_expr_to_block (&se->pre, tmp);
9019 : :
9020 : : /* Free the temporary string, if necessary. */
9021 : 192 : free = gfc_call_free (tmpdecl);
9022 : 192 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9023 : : dest_word_len, source_bytes);
9024 : 192 : tmp = build3_v (COND_EXPR, tmp, free, build_empty_stmt (input_location));
9025 : 192 : gfc_add_expr_to_block (&se->post, tmp);
9026 : :
9027 : 192 : se->expr = tmpdecl;
9028 : 192 : tmp = fold_convert (gfc_charlen_type_node,
9029 : : TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
9030 : 192 : se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
9031 : : gfc_charlen_type_node,
9032 : : dest_word_len, tmp);
9033 : : }
9034 : : else
9035 : : {
9036 : 2686 : tmpdecl = gfc_create_var (mold_type, "transfer");
9037 : :
9038 : 2686 : ptr = convert (build_pointer_type (mold_type), source);
9039 : :
9040 : : /* For CLASS results, allocate the needed memory first. */
9041 : 2686 : if (mold_expr->ts.type == BT_CLASS)
9042 : : {
9043 : 6 : tree cdata;
9044 : 6 : cdata = gfc_class_data_get (tmpdecl);
9045 : 6 : tmp = gfc_call_malloc (&se->pre, TREE_TYPE (cdata), dest_word_len);
9046 : 6 : gfc_add_modify (&se->pre, cdata, tmp);
9047 : : }
9048 : :
9049 : : /* Use memcpy to do the transfer. */
9050 : 2686 : if (mold_expr->ts.type == BT_CLASS)
9051 : 6 : tmp = gfc_class_data_get (tmpdecl);
9052 : : else
9053 : 2680 : tmp = gfc_build_addr_expr (NULL_TREE, tmpdecl);
9054 : :
9055 : 2686 : tmp = build_call_expr_loc (input_location,
9056 : : builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
9057 : : fold_convert (pvoid_type_node, tmp),
9058 : : fold_convert (pvoid_type_node, ptr),
9059 : : fold_convert (size_type_node, extent));
9060 : 2686 : gfc_add_expr_to_block (&se->pre, tmp);
9061 : :
9062 : : /* For CLASS results, set the _vptr. */
9063 : 2686 : if (mold_expr->ts.type == BT_CLASS)
9064 : 6 : gfc_reset_vptr (&se->pre, nullptr, tmpdecl, source_expr->ts.u.derived);
9065 : :
9066 : 2686 : se->expr = tmpdecl;
9067 : : }
9068 : : }
9069 : :
9070 : :
9071 : : /* Generate code for the ALLOCATED intrinsic.
9072 : : Generate inline code that directly check the address of the argument. */
9073 : :
9074 : : static void
9075 : 7045 : gfc_conv_allocated (gfc_se *se, gfc_expr *expr)
9076 : : {
9077 : 7045 : gfc_se arg1se;
9078 : 7045 : tree tmp;
9079 : 7045 : gfc_expr *e = expr->value.function.actual->expr;
9080 : :
9081 : 7045 : gfc_init_se (&arg1se, NULL);
9082 : 7045 : if (e->ts.type == BT_CLASS)
9083 : : {
9084 : : /* Make sure that class array expressions have both a _data
9085 : : component reference and an array reference.... */
9086 : 893 : if (CLASS_DATA (e)->attr.dimension)
9087 : 418 : gfc_add_class_array_ref (e);
9088 : : /* .... whilst scalars only need the _data component. */
9089 : : else
9090 : 475 : gfc_add_data_component (e);
9091 : : }
9092 : :
9093 : 7045 : gcc_assert (flag_coarray != GFC_FCOARRAY_LIB || !gfc_is_coindexed (e));
9094 : :
9095 : 7045 : if (e->rank == 0)
9096 : : {
9097 : : /* Allocatable scalar. */
9098 : 2755 : arg1se.want_pointer = 1;
9099 : 2755 : gfc_conv_expr (&arg1se, e);
9100 : 2755 : tmp = arg1se.expr;
9101 : : }
9102 : : else
9103 : : {
9104 : : /* Allocatable array. */
9105 : 4290 : arg1se.descriptor_only = 1;
9106 : 4290 : gfc_conv_expr_descriptor (&arg1se, e);
9107 : 4290 : tmp = gfc_conv_descriptor_data_get (arg1se.expr);
9108 : : }
9109 : :
9110 : 7045 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
9111 : 7045 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
9112 : :
9113 : : /* Components of pointer array references sometimes come back with a pre block. */
9114 : 7045 : if (arg1se.pre.head)
9115 : 320 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9116 : :
9117 : 7045 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
9118 : 7045 : }
9119 : :
9120 : :
9121 : : /* Generate code for the ASSOCIATED intrinsic.
9122 : : If both POINTER and TARGET are arrays, generate a call to library function
9123 : : _gfor_associated, and pass descriptors of POINTER and TARGET to it.
9124 : : In other cases, generate inline code that directly compare the address of
9125 : : POINTER with the address of TARGET. */
9126 : :
9127 : : static void
9128 : 8857 : gfc_conv_associated (gfc_se *se, gfc_expr *expr)
9129 : : {
9130 : 8857 : gfc_actual_arglist *arg1;
9131 : 8857 : gfc_actual_arglist *arg2;
9132 : 8857 : gfc_se arg1se;
9133 : 8857 : gfc_se arg2se;
9134 : 8857 : tree tmp2;
9135 : 8857 : tree tmp;
9136 : 8857 : tree nonzero_arraylen = NULL_TREE;
9137 : 8857 : gfc_ss *ss;
9138 : 8857 : bool scalar;
9139 : :
9140 : 8857 : gfc_init_se (&arg1se, NULL);
9141 : 8857 : gfc_init_se (&arg2se, NULL);
9142 : 8857 : arg1 = expr->value.function.actual;
9143 : 8857 : arg2 = arg1->next;
9144 : :
9145 : : /* Check whether the expression is a scalar or not; we cannot use
9146 : : arg1->expr->rank as it can be nonzero for proc pointers. */
9147 : 8857 : ss = gfc_walk_expr (arg1->expr);
9148 : 8857 : scalar = ss == gfc_ss_terminator;
9149 : 8857 : if (!scalar)
9150 : 3846 : gfc_free_ss_chain (ss);
9151 : :
9152 : 8857 : if (!arg2->expr)
9153 : : {
9154 : : /* No optional target. */
9155 : 6532 : if (scalar)
9156 : : {
9157 : : /* A pointer to a scalar. */
9158 : 4138 : arg1se.want_pointer = 1;
9159 : 4138 : gfc_conv_expr (&arg1se, arg1->expr);
9160 : 4138 : if (arg1->expr->symtree->n.sym->attr.proc_pointer
9161 : 185 : && arg1->expr->symtree->n.sym->attr.dummy)
9162 : 78 : arg1se.expr = build_fold_indirect_ref_loc (input_location,
9163 : : arg1se.expr);
9164 : 4138 : if (arg1->expr->ts.type == BT_CLASS)
9165 : : {
9166 : 384 : tmp2 = gfc_class_data_get (arg1se.expr);
9167 : 384 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
9168 : 0 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
9169 : : }
9170 : : else
9171 : 3754 : tmp2 = arg1se.expr;
9172 : : }
9173 : : else
9174 : : {
9175 : : /* A pointer to an array. */
9176 : 2394 : gfc_conv_expr_descriptor (&arg1se, arg1->expr);
9177 : 2394 : tmp2 = gfc_conv_descriptor_data_get (arg1se.expr);
9178 : : }
9179 : 6532 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9180 : 6532 : gfc_add_block_to_block (&se->post, &arg1se.post);
9181 : 6532 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp2,
9182 : 6532 : fold_convert (TREE_TYPE (tmp2), null_pointer_node));
9183 : 6532 : se->expr = tmp;
9184 : : }
9185 : : else
9186 : : {
9187 : : /* An optional target. */
9188 : 2325 : if (arg2->expr->ts.type == BT_CLASS
9189 : 24 : && arg2->expr->expr_type != EXPR_FUNCTION)
9190 : 18 : gfc_add_data_component (arg2->expr);
9191 : :
9192 : 2325 : if (scalar)
9193 : : {
9194 : : /* A pointer to a scalar. */
9195 : 873 : arg1se.want_pointer = 1;
9196 : 873 : gfc_conv_expr (&arg1se, arg1->expr);
9197 : 873 : if (arg1->expr->symtree->n.sym->attr.proc_pointer
9198 : 92 : && arg1->expr->symtree->n.sym->attr.dummy)
9199 : 42 : arg1se.expr = build_fold_indirect_ref_loc (input_location,
9200 : : arg1se.expr);
9201 : 873 : if (arg1->expr->ts.type == BT_CLASS)
9202 : 246 : arg1se.expr = gfc_class_data_get (arg1se.expr);
9203 : :
9204 : 873 : arg2se.want_pointer = 1;
9205 : 873 : gfc_conv_expr (&arg2se, arg2->expr);
9206 : 873 : if (arg2->expr->symtree->n.sym->attr.proc_pointer
9207 : 0 : && arg2->expr->symtree->n.sym->attr.dummy)
9208 : 0 : arg2se.expr = build_fold_indirect_ref_loc (input_location,
9209 : : arg2se.expr);
9210 : 873 : if (arg2->expr->ts.type == BT_CLASS)
9211 : : {
9212 : 6 : arg2se.expr = gfc_evaluate_now (arg2se.expr, &arg2se.pre);
9213 : 6 : arg2se.expr = gfc_class_data_get (arg2se.expr);
9214 : : }
9215 : 873 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9216 : 873 : gfc_add_block_to_block (&se->post, &arg1se.post);
9217 : 873 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9218 : 873 : gfc_add_block_to_block (&se->post, &arg2se.post);
9219 : 873 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9220 : : arg1se.expr, arg2se.expr);
9221 : 873 : tmp2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9222 : : arg1se.expr, null_pointer_node);
9223 : 873 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9224 : : logical_type_node, tmp, tmp2);
9225 : : }
9226 : : else
9227 : : {
9228 : : /* An array pointer of zero length is not associated if target is
9229 : : present. */
9230 : 1452 : arg1se.descriptor_only = 1;
9231 : 1452 : gfc_conv_expr_lhs (&arg1se, arg1->expr);
9232 : 1452 : if (arg1->expr->rank == -1)
9233 : : {
9234 : 84 : tmp = gfc_conv_descriptor_rank (arg1se.expr);
9235 : 168 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9236 : 84 : TREE_TYPE (tmp), tmp,
9237 : 84 : build_int_cst (TREE_TYPE (tmp), 1));
9238 : : }
9239 : : else
9240 : 1368 : tmp = gfc_rank_cst[arg1->expr->rank - 1];
9241 : 1452 : tmp = gfc_conv_descriptor_stride_get (arg1se.expr, tmp);
9242 : 1452 : if (arg2->expr->rank != 0)
9243 : 1422 : nonzero_arraylen = fold_build2_loc (input_location, NE_EXPR,
9244 : : logical_type_node, tmp,
9245 : 1422 : build_int_cst (TREE_TYPE (tmp), 0));
9246 : :
9247 : : /* A pointer to an array, call library function _gfor_associated. */
9248 : 1452 : arg1se.want_pointer = 1;
9249 : 1452 : gfc_conv_expr_descriptor (&arg1se, arg1->expr);
9250 : 1452 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9251 : 1452 : gfc_add_block_to_block (&se->post, &arg1se.post);
9252 : :
9253 : 1452 : arg2se.want_pointer = 1;
9254 : 1452 : arg2se.force_no_tmp = 1;
9255 : 1452 : if (arg2->expr->rank != 0)
9256 : 1422 : gfc_conv_expr_descriptor (&arg2se, arg2->expr);
9257 : : else
9258 : : {
9259 : 30 : gfc_conv_expr (&arg2se, arg2->expr);
9260 : 30 : arg2se.expr
9261 : 30 : = gfc_conv_scalar_to_descriptor (&arg2se, arg2se.expr,
9262 : 30 : gfc_expr_attr (arg2->expr));
9263 : 30 : arg2se.expr = gfc_build_addr_expr (NULL_TREE, arg2se.expr);
9264 : : }
9265 : 1452 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9266 : 1452 : gfc_add_block_to_block (&se->post, &arg2se.post);
9267 : 1452 : se->expr = build_call_expr_loc (input_location,
9268 : : gfor_fndecl_associated, 2,
9269 : : arg1se.expr, arg2se.expr);
9270 : 1452 : se->expr = convert (logical_type_node, se->expr);
9271 : 1452 : if (arg2->expr->rank != 0)
9272 : 1422 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9273 : : logical_type_node, se->expr,
9274 : : nonzero_arraylen);
9275 : : }
9276 : :
9277 : : /* If target is present zero character length pointers cannot
9278 : : be associated. */
9279 : 2325 : if (arg1->expr->ts.type == BT_CHARACTER)
9280 : : {
9281 : 630 : tmp = arg1se.string_length;
9282 : 630 : tmp = fold_build2_loc (input_location, NE_EXPR,
9283 : : logical_type_node, tmp,
9284 : 630 : build_zero_cst (TREE_TYPE (tmp)));
9285 : 630 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9286 : : logical_type_node, se->expr, tmp);
9287 : : }
9288 : : }
9289 : :
9290 : 8857 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9291 : 8857 : }
9292 : :
9293 : :
9294 : : /* Generate code for the SAME_TYPE_AS intrinsic.
9295 : : Generate inline code that directly checks the vindices. */
9296 : :
9297 : : static void
9298 : 409 : gfc_conv_same_type_as (gfc_se *se, gfc_expr *expr)
9299 : : {
9300 : 409 : gfc_expr *a, *b;
9301 : 409 : gfc_se se1, se2;
9302 : 409 : tree tmp;
9303 : 409 : tree conda = NULL_TREE, condb = NULL_TREE;
9304 : :
9305 : 409 : gfc_init_se (&se1, NULL);
9306 : 409 : gfc_init_se (&se2, NULL);
9307 : :
9308 : 409 : a = expr->value.function.actual->expr;
9309 : 409 : b = expr->value.function.actual->next->expr;
9310 : :
9311 : 409 : bool unlimited_poly_a = UNLIMITED_POLY (a);
9312 : 409 : bool unlimited_poly_b = UNLIMITED_POLY (b);
9313 : 409 : if (unlimited_poly_a)
9314 : : {
9315 : 111 : se1.want_pointer = 1;
9316 : 111 : gfc_add_vptr_component (a);
9317 : : }
9318 : 298 : else if (a->ts.type == BT_CLASS)
9319 : : {
9320 : 256 : gfc_add_vptr_component (a);
9321 : 256 : gfc_add_hash_component (a);
9322 : : }
9323 : 42 : else if (a->ts.type == BT_DERIVED)
9324 : 42 : a = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9325 : 42 : a->ts.u.derived->hash_value);
9326 : :
9327 : 409 : if (unlimited_poly_b)
9328 : : {
9329 : 72 : se2.want_pointer = 1;
9330 : 72 : gfc_add_vptr_component (b);
9331 : : }
9332 : 337 : else if (b->ts.type == BT_CLASS)
9333 : : {
9334 : 169 : gfc_add_vptr_component (b);
9335 : 169 : gfc_add_hash_component (b);
9336 : : }
9337 : 168 : else if (b->ts.type == BT_DERIVED)
9338 : 168 : b = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9339 : 168 : b->ts.u.derived->hash_value);
9340 : :
9341 : 409 : gfc_conv_expr (&se1, a);
9342 : 409 : gfc_conv_expr (&se2, b);
9343 : :
9344 : 409 : if (unlimited_poly_a)
9345 : : {
9346 : 111 : conda = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9347 : : se1.expr,
9348 : 111 : build_int_cst (TREE_TYPE (se1.expr), 0));
9349 : 111 : se1.expr = gfc_vptr_hash_get (se1.expr);
9350 : : }
9351 : :
9352 : 409 : if (unlimited_poly_b)
9353 : : {
9354 : 72 : condb = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9355 : : se2.expr,
9356 : 72 : build_int_cst (TREE_TYPE (se2.expr), 0));
9357 : 72 : se2.expr = gfc_vptr_hash_get (se2.expr);
9358 : : }
9359 : :
9360 : 409 : tmp = fold_build2_loc (input_location, EQ_EXPR,
9361 : : logical_type_node, se1.expr,
9362 : 409 : fold_convert (TREE_TYPE (se1.expr), se2.expr));
9363 : :
9364 : 409 : if (conda)
9365 : 111 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9366 : : logical_type_node, conda, tmp);
9367 : :
9368 : 409 : if (condb)
9369 : 72 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9370 : : logical_type_node, condb, tmp);
9371 : :
9372 : 409 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
9373 : 409 : }
9374 : :
9375 : :
9376 : : /* Generate code for SELECTED_CHAR_KIND (NAME) intrinsic function. */
9377 : :
9378 : : static void
9379 : 42 : gfc_conv_intrinsic_sc_kind (gfc_se *se, gfc_expr *expr)
9380 : : {
9381 : 42 : tree args[2];
9382 : :
9383 : 42 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
9384 : 42 : se->expr = build_call_expr_loc (input_location,
9385 : : gfor_fndecl_sc_kind, 2, args[0], args[1]);
9386 : 42 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9387 : 42 : }
9388 : :
9389 : :
9390 : : /* Generate code for SELECTED_INT_KIND (R) intrinsic function. */
9391 : :
9392 : : static void
9393 : 45 : gfc_conv_intrinsic_si_kind (gfc_se *se, gfc_expr *expr)
9394 : : {
9395 : 45 : tree arg, type;
9396 : :
9397 : 45 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9398 : :
9399 : : /* The argument to SELECTED_INT_KIND is INTEGER(4). */
9400 : 45 : type = gfc_get_int_type (4);
9401 : 45 : arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9402 : :
9403 : : /* Convert it to the required type. */
9404 : 45 : type = gfc_typenode_for_spec (&expr->ts);
9405 : 45 : se->expr = build_call_expr_loc (input_location,
9406 : : gfor_fndecl_si_kind, 1, arg);
9407 : 45 : se->expr = fold_convert (type, se->expr);
9408 : 45 : }
9409 : :
9410 : :
9411 : : /* Generate code for SELECTED_LOGICAL_KIND (BITS) intrinsic function. */
9412 : :
9413 : : static void
9414 : 6 : gfc_conv_intrinsic_sl_kind (gfc_se *se, gfc_expr *expr)
9415 : : {
9416 : 6 : tree arg, type;
9417 : :
9418 : 6 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9419 : :
9420 : : /* The argument to SELECTED_LOGICAL_KIND is INTEGER(4). */
9421 : 6 : type = gfc_get_int_type (4);
9422 : 6 : arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9423 : :
9424 : : /* Convert it to the required type. */
9425 : 6 : type = gfc_typenode_for_spec (&expr->ts);
9426 : 6 : se->expr = build_call_expr_loc (input_location,
9427 : : gfor_fndecl_sl_kind, 1, arg);
9428 : 6 : se->expr = fold_convert (type, se->expr);
9429 : 6 : }
9430 : :
9431 : :
9432 : : /* Generate code for SELECTED_REAL_KIND (P, R, RADIX) intrinsic function. */
9433 : :
9434 : : static void
9435 : 82 : gfc_conv_intrinsic_sr_kind (gfc_se *se, gfc_expr *expr)
9436 : : {
9437 : 82 : gfc_actual_arglist *actual;
9438 : 82 : tree type;
9439 : 82 : gfc_se argse;
9440 : 82 : vec<tree, va_gc> *args = NULL;
9441 : :
9442 : 328 : for (actual = expr->value.function.actual; actual; actual = actual->next)
9443 : : {
9444 : 246 : gfc_init_se (&argse, se);
9445 : :
9446 : : /* Pass a NULL pointer for an absent arg. */
9447 : 246 : if (actual->expr == NULL)
9448 : 96 : argse.expr = null_pointer_node;
9449 : : else
9450 : : {
9451 : 150 : gfc_typespec ts;
9452 : 150 : gfc_clear_ts (&ts);
9453 : :
9454 : 150 : if (actual->expr->ts.kind != gfc_c_int_kind)
9455 : : {
9456 : : /* The arguments to SELECTED_REAL_KIND are INTEGER(4). */
9457 : 0 : ts.type = BT_INTEGER;
9458 : 0 : ts.kind = gfc_c_int_kind;
9459 : 0 : gfc_convert_type (actual->expr, &ts, 2);
9460 : : }
9461 : 150 : gfc_conv_expr_reference (&argse, actual->expr);
9462 : : }
9463 : :
9464 : 246 : gfc_add_block_to_block (&se->pre, &argse.pre);
9465 : 246 : gfc_add_block_to_block (&se->post, &argse.post);
9466 : 246 : vec_safe_push (args, argse.expr);
9467 : : }
9468 : :
9469 : : /* Convert it to the required type. */
9470 : 82 : type = gfc_typenode_for_spec (&expr->ts);
9471 : 82 : se->expr = build_call_expr_loc_vec (input_location,
9472 : : gfor_fndecl_sr_kind, args);
9473 : 82 : se->expr = fold_convert (type, se->expr);
9474 : 82 : }
9475 : :
9476 : :
9477 : : /* Generate code for TRIM (A) intrinsic function. */
9478 : :
9479 : : static void
9480 : 574 : gfc_conv_intrinsic_trim (gfc_se * se, gfc_expr * expr)
9481 : : {
9482 : 574 : tree var;
9483 : 574 : tree len;
9484 : 574 : tree addr;
9485 : 574 : tree tmp;
9486 : 574 : tree cond;
9487 : 574 : tree fndecl;
9488 : 574 : tree function;
9489 : 574 : tree *args;
9490 : 574 : unsigned int num_args;
9491 : :
9492 : 574 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
9493 : 574 : args = XALLOCAVEC (tree, num_args);
9494 : :
9495 : 574 : var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
9496 : 574 : addr = gfc_build_addr_expr (ppvoid_type_node, var);
9497 : 574 : len = gfc_create_var (gfc_charlen_type_node, "len");
9498 : :
9499 : 574 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
9500 : 574 : args[0] = gfc_build_addr_expr (NULL_TREE, len);
9501 : 574 : args[1] = addr;
9502 : :
9503 : 574 : if (expr->ts.kind == 1)
9504 : 542 : function = gfor_fndecl_string_trim;
9505 : 32 : else if (expr->ts.kind == 4)
9506 : 32 : function = gfor_fndecl_string_trim_char4;
9507 : : else
9508 : 0 : gcc_unreachable ();
9509 : :
9510 : 574 : fndecl = build_addr (function);
9511 : 574 : tmp = build_call_array_loc (input_location,
9512 : 574 : TREE_TYPE (TREE_TYPE (function)), fndecl,
9513 : : num_args, args);
9514 : 574 : gfc_add_expr_to_block (&se->pre, tmp);
9515 : :
9516 : : /* Free the temporary afterwards, if necessary. */
9517 : 574 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9518 : 574 : len, build_int_cst (TREE_TYPE (len), 0));
9519 : 574 : tmp = gfc_call_free (var);
9520 : 574 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
9521 : 574 : gfc_add_expr_to_block (&se->post, tmp);
9522 : :
9523 : 574 : se->expr = var;
9524 : 574 : se->string_length = len;
9525 : 574 : }
9526 : :
9527 : :
9528 : : /* Generate code for REPEAT (STRING, NCOPIES) intrinsic function. */
9529 : :
9530 : : static void
9531 : 529 : gfc_conv_intrinsic_repeat (gfc_se * se, gfc_expr * expr)
9532 : : {
9533 : 529 : tree args[3], ncopies, dest, dlen, src, slen, ncopies_type;
9534 : 529 : tree type, cond, tmp, count, exit_label, n, max, largest;
9535 : 529 : tree size;
9536 : 529 : stmtblock_t block, body;
9537 : 529 : int i;
9538 : :
9539 : : /* We store in charsize the size of a character. */
9540 : 529 : i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
9541 : 529 : size = build_int_cst (sizetype, gfc_character_kinds[i].bit_size / 8);
9542 : :
9543 : : /* Get the arguments. */
9544 : 529 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
9545 : 529 : slen = fold_convert (sizetype, gfc_evaluate_now (args[0], &se->pre));
9546 : 529 : src = args[1];
9547 : 529 : ncopies = gfc_evaluate_now (args[2], &se->pre);
9548 : 529 : ncopies_type = TREE_TYPE (ncopies);
9549 : :
9550 : : /* Check that NCOPIES is not negative. */
9551 : 529 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, ncopies,
9552 : : build_int_cst (ncopies_type, 0));
9553 : 529 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9554 : : "Argument NCOPIES of REPEAT intrinsic is negative "
9555 : : "(its value is %ld)",
9556 : : fold_convert (long_integer_type_node, ncopies));
9557 : :
9558 : : /* If the source length is zero, any non negative value of NCOPIES
9559 : : is valid, and nothing happens. */
9560 : 529 : n = gfc_create_var (ncopies_type, "ncopies");
9561 : 529 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9562 : : size_zero_node);
9563 : 529 : tmp = fold_build3_loc (input_location, COND_EXPR, ncopies_type, cond,
9564 : : build_int_cst (ncopies_type, 0), ncopies);
9565 : 529 : gfc_add_modify (&se->pre, n, tmp);
9566 : 529 : ncopies = n;
9567 : :
9568 : : /* Check that ncopies is not too large: ncopies should be less than
9569 : : (or equal to) MAX / slen, where MAX is the maximal integer of
9570 : : the gfc_charlen_type_node type. If slen == 0, we need a special
9571 : : case to avoid the division by zero. */
9572 : 529 : max = fold_build2_loc (input_location, TRUNC_DIV_EXPR, sizetype,
9573 : 529 : fold_convert (sizetype,
9574 : : TYPE_MAX_VALUE (gfc_charlen_type_node)),
9575 : : slen);
9576 : 1054 : largest = TYPE_PRECISION (sizetype) > TYPE_PRECISION (ncopies_type)
9577 : 529 : ? sizetype : ncopies_type;
9578 : 529 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9579 : : fold_convert (largest, ncopies),
9580 : : fold_convert (largest, max));
9581 : 529 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9582 : : size_zero_node);
9583 : 529 : cond = fold_build3_loc (input_location, COND_EXPR, logical_type_node, tmp,
9584 : : logical_false_node, cond);
9585 : 529 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9586 : : "Argument NCOPIES of REPEAT intrinsic is too large");
9587 : :
9588 : : /* Compute the destination length. */
9589 : 529 : dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
9590 : : fold_convert (gfc_charlen_type_node, slen),
9591 : : fold_convert (gfc_charlen_type_node, ncopies));
9592 : 529 : type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
9593 : 529 : dest = gfc_conv_string_tmp (se, build_pointer_type (type), dlen);
9594 : :
9595 : : /* Generate the code to do the repeat operation:
9596 : : for (i = 0; i < ncopies; i++)
9597 : : memmove (dest + (i * slen * size), src, slen*size); */
9598 : 529 : gfc_start_block (&block);
9599 : 529 : count = gfc_create_var (sizetype, "count");
9600 : 529 : gfc_add_modify (&block, count, size_zero_node);
9601 : 529 : exit_label = gfc_build_label_decl (NULL_TREE);
9602 : :
9603 : : /* Start the loop body. */
9604 : 529 : gfc_start_block (&body);
9605 : :
9606 : : /* Exit the loop if count >= ncopies. */
9607 : 529 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, count,
9608 : : fold_convert (sizetype, ncopies));
9609 : 529 : tmp = build1_v (GOTO_EXPR, exit_label);
9610 : 529 : TREE_USED (exit_label) = 1;
9611 : 529 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9612 : : build_empty_stmt (input_location));
9613 : 529 : gfc_add_expr_to_block (&body, tmp);
9614 : :
9615 : : /* Call memmove (dest + (i*slen*size), src, slen*size). */
9616 : 529 : tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, slen,
9617 : : count);
9618 : 529 : tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, tmp,
9619 : : size);
9620 : 529 : tmp = fold_build_pointer_plus_loc (input_location,
9621 : : fold_convert (pvoid_type_node, dest), tmp);
9622 : 529 : tmp = build_call_expr_loc (input_location,
9623 : : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9624 : : 3, tmp, src,
9625 : : fold_build2_loc (input_location, MULT_EXPR,
9626 : : size_type_node, slen, size));
9627 : 529 : gfc_add_expr_to_block (&body, tmp);
9628 : :
9629 : : /* Increment count. */
9630 : 529 : tmp = fold_build2_loc (input_location, PLUS_EXPR, sizetype,
9631 : : count, size_one_node);
9632 : 529 : gfc_add_modify (&body, count, tmp);
9633 : :
9634 : : /* Build the loop. */
9635 : 529 : tmp = build1_v (LOOP_EXPR, gfc_finish_block (&body));
9636 : 529 : gfc_add_expr_to_block (&block, tmp);
9637 : :
9638 : : /* Add the exit label. */
9639 : 529 : tmp = build1_v (LABEL_EXPR, exit_label);
9640 : 529 : gfc_add_expr_to_block (&block, tmp);
9641 : :
9642 : : /* Finish the block. */
9643 : 529 : tmp = gfc_finish_block (&block);
9644 : 529 : gfc_add_expr_to_block (&se->pre, tmp);
9645 : :
9646 : : /* Set the result value. */
9647 : 529 : se->expr = dest;
9648 : 529 : se->string_length = dlen;
9649 : 529 : }
9650 : :
9651 : :
9652 : : /* Generate code for the IARGC intrinsic. */
9653 : :
9654 : : static void
9655 : 12 : gfc_conv_intrinsic_iargc (gfc_se * se, gfc_expr * expr)
9656 : : {
9657 : 12 : tree tmp;
9658 : 12 : tree fndecl;
9659 : 12 : tree type;
9660 : :
9661 : : /* Call the library function. This always returns an INTEGER(4). */
9662 : 12 : fndecl = gfor_fndecl_iargc;
9663 : 12 : tmp = build_call_expr_loc (input_location,
9664 : : fndecl, 0);
9665 : :
9666 : : /* Convert it to the required type. */
9667 : 12 : type = gfc_typenode_for_spec (&expr->ts);
9668 : 12 : tmp = fold_convert (type, tmp);
9669 : :
9670 : 12 : se->expr = tmp;
9671 : 12 : }
9672 : :
9673 : :
9674 : : /* Generate code for the KILL intrinsic. */
9675 : :
9676 : : static void
9677 : 8 : conv_intrinsic_kill (gfc_se *se, gfc_expr *expr)
9678 : : {
9679 : 8 : tree *args;
9680 : 8 : tree int4_type_node = gfc_get_int_type (4);
9681 : 8 : tree pid;
9682 : 8 : tree sig;
9683 : 8 : tree tmp;
9684 : 8 : unsigned int num_args;
9685 : :
9686 : 8 : num_args = gfc_intrinsic_argument_list_length (expr);
9687 : 8 : args = XALLOCAVEC (tree, num_args);
9688 : 8 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
9689 : :
9690 : : /* Convert PID to a INTEGER(4) entity. */
9691 : 8 : pid = convert (int4_type_node, args[0]);
9692 : :
9693 : : /* Convert SIG to a INTEGER(4) entity. */
9694 : 8 : sig = convert (int4_type_node, args[1]);
9695 : :
9696 : 8 : tmp = build_call_expr_loc (input_location, gfor_fndecl_kill, 2, pid, sig);
9697 : :
9698 : 8 : se->expr = fold_convert (TREE_TYPE (args[0]), tmp);
9699 : 8 : }
9700 : :
9701 : :
9702 : : static tree
9703 : 15 : conv_intrinsic_kill_sub (gfc_code *code)
9704 : : {
9705 : 15 : stmtblock_t block;
9706 : 15 : gfc_se se, se_stat;
9707 : 15 : tree int4_type_node = gfc_get_int_type (4);
9708 : 15 : tree pid;
9709 : 15 : tree sig;
9710 : 15 : tree statp;
9711 : 15 : tree tmp;
9712 : :
9713 : : /* Make the function call. */
9714 : 15 : gfc_init_block (&block);
9715 : 15 : gfc_init_se (&se, NULL);
9716 : :
9717 : : /* Convert PID to a INTEGER(4) entity. */
9718 : 15 : gfc_conv_expr (&se, code->ext.actual->expr);
9719 : 15 : gfc_add_block_to_block (&block, &se.pre);
9720 : 15 : pid = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9721 : 15 : gfc_add_block_to_block (&block, &se.post);
9722 : :
9723 : : /* Convert SIG to a INTEGER(4) entity. */
9724 : 15 : gfc_conv_expr (&se, code->ext.actual->next->expr);
9725 : 15 : gfc_add_block_to_block (&block, &se.pre);
9726 : 15 : sig = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9727 : 15 : gfc_add_block_to_block (&block, &se.post);
9728 : :
9729 : : /* Deal with an optional STATUS. */
9730 : 15 : if (code->ext.actual->next->next->expr)
9731 : : {
9732 : 10 : gfc_init_se (&se_stat, NULL);
9733 : 10 : gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
9734 : 10 : statp = gfc_create_var (gfc_get_int_type (4), "_statp");
9735 : : }
9736 : : else
9737 : : statp = NULL_TREE;
9738 : :
9739 : 25 : tmp = build_call_expr_loc (input_location, gfor_fndecl_kill_sub, 3, pid, sig,
9740 : 10 : statp ? gfc_build_addr_expr (NULL_TREE, statp) : null_pointer_node);
9741 : :
9742 : 15 : gfc_add_expr_to_block (&block, tmp);
9743 : :
9744 : 15 : if (statp && statp != se_stat.expr)
9745 : 10 : gfc_add_modify (&block, se_stat.expr,
9746 : 10 : fold_convert (TREE_TYPE (se_stat.expr), statp));
9747 : :
9748 : 15 : return gfc_finish_block (&block);
9749 : : }
9750 : :
9751 : :
9752 : :
9753 : : /* The loc intrinsic returns the address of its argument as
9754 : : gfc_index_integer_kind integer. */
9755 : :
9756 : : static void
9757 : 8634 : gfc_conv_intrinsic_loc (gfc_se * se, gfc_expr * expr)
9758 : : {
9759 : 8634 : tree temp_var;
9760 : 8634 : gfc_expr *arg_expr;
9761 : :
9762 : 8634 : gcc_assert (!se->ss);
9763 : :
9764 : 8634 : arg_expr = expr->value.function.actual->expr;
9765 : 8634 : if (arg_expr->rank == 0)
9766 : : {
9767 : 6234 : if (arg_expr->ts.type == BT_CLASS)
9768 : 18 : gfc_add_data_component (arg_expr);
9769 : 6234 : gfc_conv_expr_reference (se, arg_expr);
9770 : : }
9771 : : else
9772 : 2400 : gfc_conv_array_parameter (se, arg_expr, true, NULL, NULL, NULL);
9773 : 8634 : se->expr = convert (gfc_get_int_type (gfc_index_integer_kind), se->expr);
9774 : :
9775 : : /* Create a temporary variable for loc return value. Without this,
9776 : : we get an error an ICE in gcc/expr.cc(expand_expr_addr_expr_1). */
9777 : 8634 : temp_var = gfc_create_var (gfc_get_int_type (gfc_index_integer_kind), NULL);
9778 : 8634 : gfc_add_modify (&se->pre, temp_var, se->expr);
9779 : 8634 : se->expr = temp_var;
9780 : 8634 : }
9781 : :
9782 : :
9783 : : /* Specialized trim for f_c_string. */
9784 : :
9785 : : static void
9786 : 42 : conv_trim (gfc_se *tse, gfc_se *str)
9787 : : {
9788 : 42 : tree cond, plen, pvar, tlen, ttmp, tvar;
9789 : :
9790 : 42 : tlen = gfc_create_var (gfc_charlen_type_node, "tlen");
9791 : 42 : plen = gfc_build_addr_expr (NULL_TREE, tlen);
9792 : :
9793 : 42 : tvar = gfc_create_var (pchar_type_node, "tstr");
9794 : 42 : pvar = gfc_build_addr_expr (ppvoid_type_node, tvar);
9795 : :
9796 : 42 : ttmp = build_call_expr_loc (input_location, gfor_fndecl_string_trim, 4,
9797 : : plen, pvar, str->string_length, str->expr);
9798 : :
9799 : 42 : gfc_add_expr_to_block (&tse->pre, ttmp);
9800 : :
9801 : : /* Free the temporary afterwards, if necessary. */
9802 : 42 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9803 : 42 : tlen, build_int_cst (TREE_TYPE (tlen), 0));
9804 : 42 : ttmp = gfc_call_free (tvar);
9805 : 42 : ttmp = build3_v (COND_EXPR, cond, ttmp, build_empty_stmt (input_location));
9806 : 42 : gfc_add_expr_to_block (&tse->post, ttmp);
9807 : :
9808 : 42 : tse->expr = tvar;
9809 : 42 : tse->string_length = tlen;
9810 : 42 : }
9811 : :
9812 : :
9813 : : /* The following routine generates code for the intrinsic functions from
9814 : : the ISO_C_BINDING module: C_LOC, C_FUNLOC, C_ASSOCIATED, and
9815 : : F_C_STRING. */
9816 : :
9817 : : static void
9818 : 8990 : conv_isocbinding_function (gfc_se *se, gfc_expr *expr)
9819 : : {
9820 : 8990 : gfc_actual_arglist *arg = expr->value.function.actual;
9821 : :
9822 : 8990 : if (expr->value.function.isym->id == GFC_ISYM_C_LOC)
9823 : : {
9824 : 6687 : if (arg->expr->rank == 0)
9825 : 1919 : gfc_conv_expr_reference (se, arg->expr);
9826 : 4768 : else if (gfc_is_simply_contiguous (arg->expr, false, false))
9827 : 3732 : gfc_conv_array_parameter (se, arg->expr, true, NULL, NULL, NULL);
9828 : : else
9829 : : {
9830 : 1036 : gfc_conv_expr_descriptor (se, arg->expr);
9831 : 1036 : se->expr = gfc_conv_descriptor_data_get (se->expr);
9832 : : }
9833 : :
9834 : : /* TODO -- the following two lines shouldn't be necessary, but if
9835 : : they're removed, a bug is exposed later in the code path.
9836 : : This workaround was thus introduced, but will have to be
9837 : : removed; please see PR 35150 for details about the issue. */
9838 : 6687 : se->expr = convert (pvoid_type_node, se->expr);
9839 : 6687 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
9840 : : }
9841 : 2303 : else if (expr->value.function.isym->id == GFC_ISYM_C_FUNLOC)
9842 : 231 : gfc_conv_expr_reference (se, arg->expr);
9843 : 2072 : else if (expr->value.function.isym->id == GFC_ISYM_C_ASSOCIATED)
9844 : : {
9845 : 2030 : gfc_se arg1se;
9846 : 2030 : gfc_se arg2se;
9847 : :
9848 : : /* Build the addr_expr for the first argument. The argument is
9849 : : already an *address* so we don't need to set want_pointer in
9850 : : the gfc_se. */
9851 : 2030 : gfc_init_se (&arg1se, NULL);
9852 : 2030 : gfc_conv_expr (&arg1se, arg->expr);
9853 : 2030 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9854 : 2030 : gfc_add_block_to_block (&se->post, &arg1se.post);
9855 : :
9856 : : /* See if we were given two arguments. */
9857 : 2030 : if (arg->next->expr == NULL)
9858 : : /* Only given one arg so generate a null and do a
9859 : : not-equal comparison against the first arg. */
9860 : 1675 : se->expr = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9861 : : arg1se.expr,
9862 : 1675 : fold_convert (TREE_TYPE (arg1se.expr),
9863 : : null_pointer_node));
9864 : : else
9865 : : {
9866 : 355 : tree eq_expr;
9867 : 355 : tree not_null_expr;
9868 : :
9869 : : /* Given two arguments so build the arg2se from second arg. */
9870 : 355 : gfc_init_se (&arg2se, NULL);
9871 : 355 : gfc_conv_expr (&arg2se, arg->next->expr);
9872 : 355 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9873 : 355 : gfc_add_block_to_block (&se->post, &arg2se.post);
9874 : :
9875 : : /* Generate test to compare that the two args are equal. */
9876 : 355 : eq_expr = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9877 : : arg1se.expr, arg2se.expr);
9878 : : /* Generate test to ensure that the first arg is not null. */
9879 : 355 : not_null_expr = fold_build2_loc (input_location, NE_EXPR,
9880 : : logical_type_node,
9881 : : arg1se.expr, null_pointer_node);
9882 : :
9883 : : /* Finally, the generated test must check that both arg1 is not
9884 : : NULL and that it is equal to the second arg. */
9885 : 355 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9886 : : logical_type_node,
9887 : : not_null_expr, eq_expr);
9888 : : }
9889 : : }
9890 : 42 : else if (expr->value.function.isym->id == GFC_ISYM_F_C_STRING)
9891 : : {
9892 : : /* There are three cases:
9893 : : f_c_string(string) -> trim(string) // c_null_char
9894 : : f_c_string(string, .false.) -> trim(string) // c_null_char
9895 : : f_c_string(string, .true.) -> string // c_null_char */
9896 : :
9897 : 42 : gfc_se lse, rse, tse;
9898 : 42 : tree len, tmp, var;
9899 : 42 : gfc_expr *string = arg->expr;
9900 : 42 : gfc_expr *asis = arg->next->expr;
9901 : 42 : gfc_expr *cnc;
9902 : :
9903 : : /* Convert string. */
9904 : 42 : gfc_init_se (&lse, se);
9905 : 42 : gfc_conv_expr (&lse, string);
9906 : 42 : gfc_conv_string_parameter (&lse);
9907 : :
9908 : : /* Create a string for C_NULL_CHAR and convert it. */
9909 : 42 : cnc = gfc_get_character_expr (gfc_default_character_kind,
9910 : : &string->where, "\0", 1);
9911 : 42 : gfc_init_se (&rse, se);
9912 : 42 : gfc_conv_expr (&rse, cnc);
9913 : 42 : gfc_conv_string_parameter (&rse);
9914 : 42 : gfc_free_expr (cnc);
9915 : :
9916 : : #ifdef cnode
9917 : : #undef cnode
9918 : : #endif
9919 : : #define cnode gfc_charlen_type_node
9920 : 42 : if (asis)
9921 : : {
9922 : 30 : stmtblock_t block;
9923 : 30 : gfc_se asis_se, vse;
9924 : 30 : tree elen, evar, tlen, tvar;
9925 : 30 : tree else_branch, then_branch;
9926 : :
9927 : 30 : elen = evar = tlen = tvar = NULL_TREE;
9928 : :
9929 : : /* f_c_string(string, .true.) -> string // c_null_char */
9930 : :
9931 : 30 : gfc_init_block (&block);
9932 : :
9933 : 30 : gfc_add_block_to_block (&block, &lse.pre);
9934 : 30 : gfc_add_block_to_block (&block, &rse.pre);
9935 : :
9936 : 30 : tlen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9937 : : fold_convert (cnode, lse.string_length),
9938 : : fold_convert (cnode, rse.string_length));
9939 : :
9940 : 30 : gfc_init_se (&vse, se);
9941 : 30 : tvar = gfc_conv_string_tmp (&vse, pchar_type_node, tlen);
9942 : 30 : gfc_add_block_to_block (&block, &vse.pre);
9943 : :
9944 : 30 : tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9945 : : 6, tlen, tvar,
9946 : : lse.string_length, lse.expr,
9947 : : rse.string_length, rse.expr);
9948 : 30 : gfc_add_expr_to_block (&block, tmp);
9949 : :
9950 : 30 : then_branch = gfc_finish_block (&block);
9951 : :
9952 : : /* f_c_string(string, .false.) = trim(string) // c_null_char */
9953 : :
9954 : 30 : gfc_init_block (&block);
9955 : :
9956 : 30 : gfc_init_se (&tse, se);
9957 : 30 : conv_trim (&tse, &lse);
9958 : 30 : gfc_add_block_to_block (&block, &tse.pre);
9959 : 30 : gfc_add_block_to_block (&block, &rse.pre);
9960 : :
9961 : 30 : elen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9962 : : fold_convert (cnode, tse.string_length),
9963 : : fold_convert (cnode, rse.string_length));
9964 : :
9965 : 30 : gfc_init_se (&vse, se);
9966 : 30 : evar = gfc_conv_string_tmp (&vse, pchar_type_node, elen);
9967 : 30 : gfc_add_block_to_block (&block, &vse.pre);
9968 : :
9969 : 30 : tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9970 : : 6, elen, evar,
9971 : : tse.string_length, tse.expr,
9972 : : rse.string_length, rse.expr);
9973 : 30 : gfc_add_expr_to_block (&block, tmp);
9974 : :
9975 : 30 : else_branch = gfc_finish_block (&block);
9976 : :
9977 : 30 : gfc_init_se (&asis_se, se);
9978 : 30 : gfc_conv_expr (&asis_se, asis);
9979 : 30 : if (asis->expr_type == EXPR_VARIABLE
9980 : 18 : && asis->symtree->n.sym->attr.dummy
9981 : 6 : && asis->symtree->n.sym->attr.optional)
9982 : : {
9983 : 6 : tree present = gfc_conv_expr_present (asis->symtree->n.sym);
9984 : 6 : asis_se.expr = build3_loc (input_location, COND_EXPR,
9985 : : logical_type_node, present,
9986 : : asis_se.expr,
9987 : : build_int_cst (logical_type_node, 0));
9988 : : }
9989 : 30 : gfc_add_block_to_block (&se->pre, &asis_se.pre);
9990 : 30 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
9991 : : asis_se.expr, then_branch, else_branch);
9992 : :
9993 : 30 : gfc_add_expr_to_block (&se->pre, tmp);
9994 : :
9995 : 30 : var = fold_build3_loc (input_location, COND_EXPR, pchar_type_node,
9996 : : asis_se.expr, tvar, evar);
9997 : 30 : gfc_add_expr_to_block (&se->pre, var);
9998 : :
9999 : 30 : len = fold_build3_loc (input_location, COND_EXPR, cnode,
10000 : : asis_se.expr, tlen, elen);
10001 : 30 : gfc_add_expr_to_block (&se->pre, len);
10002 : : }
10003 : : else
10004 : : {
10005 : : /* f_c_string(string) = trim(string) // c_null_char */
10006 : :
10007 : 12 : gfc_add_block_to_block (&se->pre, &lse.pre);
10008 : 12 : gfc_add_block_to_block (&se->pre, &rse.pre);
10009 : :
10010 : 12 : gfc_init_se (&tse, se);
10011 : 12 : conv_trim (&tse, &lse);
10012 : 12 : gfc_add_block_to_block (&se->pre, &tse.pre);
10013 : 12 : gfc_add_block_to_block (&se->post, &tse.post);
10014 : :
10015 : 12 : len = fold_build2_loc (input_location, PLUS_EXPR, cnode,
10016 : : fold_convert (cnode, tse.string_length),
10017 : : fold_convert (cnode, rse.string_length));
10018 : :
10019 : 12 : var = gfc_conv_string_tmp (se, pchar_type_node, len);
10020 : :
10021 : 12 : tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
10022 : : 6, len, var,
10023 : : tse.string_length, tse.expr,
10024 : : rse.string_length, rse.expr);
10025 : 12 : gfc_add_expr_to_block (&se->pre, tmp);
10026 : : }
10027 : :
10028 : 42 : se->expr = var;
10029 : 42 : se->string_length = len;
10030 : :
10031 : : #undef cnode
10032 : : }
10033 : : else
10034 : 0 : gcc_unreachable ();
10035 : 8990 : }
10036 : :
10037 : :
10038 : : /* The following routine generates code for the intrinsic
10039 : : subroutines from the ISO_C_BINDING module:
10040 : : * C_F_POINTER
10041 : : * C_F_PROCPOINTER. */
10042 : :
10043 : : static tree
10044 : 2794 : conv_isocbinding_subroutine (gfc_code *code)
10045 : : {
10046 : 2794 : gfc_expr *cptr, *fptr, *shape, *lower;
10047 : 2794 : gfc_se se, cptrse, fptrse, shapese, lowerse;
10048 : 2794 : gfc_ss *shape_ss, *lower_ss;
10049 : 2794 : tree desc, dim, tmp, stride, offset, lbound, ubound;
10050 : 2794 : stmtblock_t body, block;
10051 : 2794 : gfc_loopinfo loop;
10052 : 2794 : gfc_actual_arglist *arg;
10053 : :
10054 : 2794 : arg = code->ext.actual;
10055 : 2794 : cptr = arg->expr;
10056 : 2794 : fptr = arg->next->expr;
10057 : 2794 : shape = arg->next->next ? arg->next->next->expr : NULL;
10058 : 2736 : lower = shape && arg->next->next->next ? arg->next->next->next->expr : NULL;
10059 : :
10060 : 2794 : gfc_init_se (&se, NULL);
10061 : 2794 : gfc_init_se (&cptrse, NULL);
10062 : 2794 : gfc_conv_expr (&cptrse, cptr);
10063 : 2794 : gfc_add_block_to_block (&se.pre, &cptrse.pre);
10064 : 2794 : gfc_add_block_to_block (&se.post, &cptrse.post);
10065 : :
10066 : 2794 : gfc_init_se (&fptrse, NULL);
10067 : 2794 : if (fptr->rank == 0)
10068 : : {
10069 : 2309 : fptrse.want_pointer = 1;
10070 : 2309 : gfc_conv_expr (&fptrse, fptr);
10071 : 2309 : gfc_add_block_to_block (&se.pre, &fptrse.pre);
10072 : 2309 : gfc_add_block_to_block (&se.post, &fptrse.post);
10073 : 2309 : if (fptr->symtree->n.sym->attr.proc_pointer
10074 : 57 : && fptr->symtree->n.sym->attr.dummy)
10075 : 7 : fptrse.expr = build_fold_indirect_ref_loc (input_location, fptrse.expr);
10076 : 2309 : se.expr
10077 : 2309 : = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (fptrse.expr),
10078 : : fptrse.expr,
10079 : 2309 : fold_convert (TREE_TYPE (fptrse.expr), cptrse.expr));
10080 : 2309 : gfc_add_expr_to_block (&se.pre, se.expr);
10081 : 2309 : gfc_add_block_to_block (&se.pre, &se.post);
10082 : 2309 : return gfc_finish_block (&se.pre);
10083 : : }
10084 : :
10085 : 485 : gfc_start_block (&block);
10086 : :
10087 : : /* Get the descriptor of the Fortran pointer. */
10088 : 485 : fptrse.descriptor_only = 1;
10089 : 485 : gfc_conv_expr_descriptor (&fptrse, fptr);
10090 : 485 : gfc_add_block_to_block (&block, &fptrse.pre);
10091 : 485 : desc = fptrse.expr;
10092 : :
10093 : : /* Set the span field. */
10094 : 485 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
10095 : 485 : tmp = fold_convert (gfc_array_index_type, tmp);
10096 : 485 : gfc_conv_descriptor_span_set (&block, desc, tmp);
10097 : :
10098 : : /* Set data value, dtype, and offset. */
10099 : 485 : tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc));
10100 : 485 : gfc_conv_descriptor_data_set (&block, desc, fold_convert (tmp, cptrse.expr));
10101 : 485 : gfc_add_modify (&block, gfc_conv_descriptor_dtype (desc),
10102 : 485 : gfc_get_dtype (TREE_TYPE (desc)));
10103 : :
10104 : : /* Start scalarization of the bounds, using the shape argument. */
10105 : :
10106 : 485 : shape_ss = gfc_walk_expr (shape);
10107 : 485 : gcc_assert (shape_ss != gfc_ss_terminator);
10108 : 485 : gfc_init_se (&shapese, NULL);
10109 : 485 : if (lower)
10110 : : {
10111 : 12 : lower_ss = gfc_walk_expr (lower);
10112 : 12 : gcc_assert (lower_ss != gfc_ss_terminator);
10113 : 12 : gfc_init_se (&lowerse, NULL);
10114 : : }
10115 : :
10116 : 485 : gfc_init_loopinfo (&loop);
10117 : 485 : gfc_add_ss_to_loop (&loop, shape_ss);
10118 : 485 : if (lower)
10119 : 12 : gfc_add_ss_to_loop (&loop, lower_ss);
10120 : 485 : gfc_conv_ss_startstride (&loop);
10121 : 485 : gfc_conv_loop_setup (&loop, &fptr->where);
10122 : 485 : gfc_mark_ss_chain_used (shape_ss, 1);
10123 : 485 : if (lower)
10124 : 12 : gfc_mark_ss_chain_used (lower_ss, 1);
10125 : :
10126 : 485 : gfc_copy_loopinfo_to_se (&shapese, &loop);
10127 : 485 : shapese.ss = shape_ss;
10128 : 485 : if (lower)
10129 : : {
10130 : 12 : gfc_copy_loopinfo_to_se (&lowerse, &loop);
10131 : 12 : lowerse.ss = lower_ss;
10132 : : }
10133 : :
10134 : 485 : stride = gfc_create_var (gfc_array_index_type, "stride");
10135 : 485 : offset = gfc_create_var (gfc_array_index_type, "offset");
10136 : 485 : gfc_add_modify (&block, stride, gfc_index_one_node);
10137 : 485 : gfc_add_modify (&block, offset, gfc_index_zero_node);
10138 : :
10139 : : /* Loop body. */
10140 : 485 : gfc_start_scalarized_body (&loop, &body);
10141 : :
10142 : 485 : dim = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
10143 : : loop.loopvar[0], loop.from[0]);
10144 : :
10145 : 485 : if (lower)
10146 : : {
10147 : 12 : gfc_conv_expr (&lowerse, lower);
10148 : 12 : gfc_add_block_to_block (&body, &lowerse.pre);
10149 : 12 : lbound = fold_convert (gfc_array_index_type, lowerse.expr);
10150 : 12 : gfc_add_block_to_block (&body, &lowerse.post);
10151 : : }
10152 : : else
10153 : 473 : lbound = gfc_index_one_node;
10154 : :
10155 : : /* Set bounds and stride. */
10156 : 485 : gfc_conv_descriptor_lbound_set (&body, desc, dim, lbound);
10157 : 485 : gfc_conv_descriptor_stride_set (&body, desc, dim, stride);
10158 : :
10159 : 485 : gfc_conv_expr (&shapese, shape);
10160 : 485 : gfc_add_block_to_block (&body, &shapese.pre);
10161 : 485 : ubound = fold_build2_loc (
10162 : : input_location, MINUS_EXPR, gfc_array_index_type,
10163 : : fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, lbound,
10164 : : fold_convert (gfc_array_index_type, shapese.expr)),
10165 : : gfc_index_one_node);
10166 : 485 : gfc_conv_descriptor_ubound_set (&body, desc, dim, ubound);
10167 : 485 : gfc_add_block_to_block (&body, &shapese.post);
10168 : :
10169 : : /* Calculate offset. */
10170 : 485 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
10171 : : stride, lbound);
10172 : 485 : gfc_add_modify (&body, offset,
10173 : : fold_build2_loc (input_location, PLUS_EXPR,
10174 : : gfc_array_index_type, offset, tmp));
10175 : :
10176 : : /* Update stride. */
10177 : 485 : gfc_add_modify (
10178 : : &body, stride,
10179 : : fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, stride,
10180 : : fold_convert (gfc_array_index_type, shapese.expr)));
10181 : : /* Finish scalarization loop. */
10182 : 485 : gfc_trans_scalarizing_loops (&loop, &body);
10183 : 485 : gfc_add_block_to_block (&block, &loop.pre);
10184 : 485 : gfc_add_block_to_block (&block, &loop.post);
10185 : 485 : gfc_add_block_to_block (&block, &fptrse.post);
10186 : 485 : gfc_cleanup_loop (&loop);
10187 : :
10188 : 485 : gfc_add_modify (&block, offset,
10189 : : fold_build1_loc (input_location, NEGATE_EXPR,
10190 : : gfc_array_index_type, offset));
10191 : 485 : gfc_conv_descriptor_offset_set (&block, desc, offset);
10192 : :
10193 : 485 : gfc_add_expr_to_block (&se.pre, gfc_finish_block (&block));
10194 : 485 : gfc_add_block_to_block (&se.pre, &se.post);
10195 : 485 : return gfc_finish_block (&se.pre);
10196 : : }
10197 : :
10198 : :
10199 : : /* Save and restore floating-point state. */
10200 : :
10201 : : tree
10202 : 945 : gfc_save_fp_state (stmtblock_t *block)
10203 : : {
10204 : 945 : tree type, fpstate, tmp;
10205 : :
10206 : 945 : type = build_array_type (char_type_node,
10207 : : build_range_type (size_type_node, size_zero_node,
10208 : : size_int (GFC_FPE_STATE_BUFFER_SIZE)));
10209 : 945 : fpstate = gfc_create_var (type, "fpstate");
10210 : 945 : fpstate = gfc_build_addr_expr (pvoid_type_node, fpstate);
10211 : :
10212 : 945 : tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_entry,
10213 : : 1, fpstate);
10214 : 945 : gfc_add_expr_to_block (block, tmp);
10215 : :
10216 : 945 : return fpstate;
10217 : : }
10218 : :
10219 : :
10220 : : void
10221 : 945 : gfc_restore_fp_state (stmtblock_t *block, tree fpstate)
10222 : : {
10223 : 945 : tree tmp;
10224 : :
10225 : 945 : tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_exit,
10226 : : 1, fpstate);
10227 : 945 : gfc_add_expr_to_block (block, tmp);
10228 : 945 : }
10229 : :
10230 : :
10231 : : /* Generate code for arguments of IEEE functions. */
10232 : :
10233 : : static void
10234 : 12457 : conv_ieee_function_args (gfc_se *se, gfc_expr *expr, tree *argarray,
10235 : : int nargs)
10236 : : {
10237 : 12457 : gfc_actual_arglist *actual;
10238 : 12457 : gfc_expr *e;
10239 : 12457 : gfc_se argse;
10240 : 12457 : int arg;
10241 : :
10242 : 12457 : actual = expr->value.function.actual;
10243 : 34461 : for (arg = 0; arg < nargs; arg++, actual = actual->next)
10244 : : {
10245 : 22004 : gcc_assert (actual);
10246 : 22004 : e = actual->expr;
10247 : :
10248 : 22004 : gfc_init_se (&argse, se);
10249 : 22004 : gfc_conv_expr_val (&argse, e);
10250 : :
10251 : 22004 : gfc_add_block_to_block (&se->pre, &argse.pre);
10252 : 22004 : gfc_add_block_to_block (&se->post, &argse.post);
10253 : 22004 : argarray[arg] = argse.expr;
10254 : : }
10255 : 12457 : }
10256 : :
10257 : :
10258 : : /* Generate code for intrinsics IEEE_IS_NAN, IEEE_IS_FINITE
10259 : : and IEEE_UNORDERED, which translate directly to GCC type-generic
10260 : : built-ins. */
10261 : :
10262 : : static void
10263 : 1062 : conv_intrinsic_ieee_builtin (gfc_se * se, gfc_expr * expr,
10264 : : enum built_in_function code, int nargs)
10265 : : {
10266 : 1062 : tree args[2];
10267 : 1062 : gcc_assert ((unsigned) nargs <= ARRAY_SIZE (args));
10268 : :
10269 : 1062 : conv_ieee_function_args (se, expr, args, nargs);
10270 : 1062 : se->expr = build_call_expr_loc_array (input_location,
10271 : : builtin_decl_explicit (code),
10272 : : nargs, args);
10273 : 2388 : STRIP_TYPE_NOPS (se->expr);
10274 : 1062 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10275 : 1062 : }
10276 : :
10277 : :
10278 : : /* Generate code for intrinsics IEEE_SIGNBIT. */
10279 : :
10280 : : static void
10281 : 624 : conv_intrinsic_ieee_signbit (gfc_se * se, gfc_expr * expr)
10282 : : {
10283 : 624 : tree arg, signbit;
10284 : :
10285 : 624 : conv_ieee_function_args (se, expr, &arg, 1);
10286 : 624 : signbit = build_call_expr_loc (input_location,
10287 : : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10288 : : 1, arg);
10289 : 624 : signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10290 : : signbit, integer_zero_node);
10291 : 624 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), signbit);
10292 : 624 : }
10293 : :
10294 : :
10295 : : /* Generate code for IEEE_IS_NORMAL intrinsic:
10296 : : IEEE_IS_NORMAL(x) --> (__builtin_isnormal(x) || x == 0) */
10297 : :
10298 : : static void
10299 : 312 : conv_intrinsic_ieee_is_normal (gfc_se * se, gfc_expr * expr)
10300 : : {
10301 : 312 : tree arg, isnormal, iszero;
10302 : :
10303 : : /* Convert arg, evaluate it only once. */
10304 : 312 : conv_ieee_function_args (se, expr, &arg, 1);
10305 : 312 : arg = gfc_evaluate_now (arg, &se->pre);
10306 : :
10307 : 312 : isnormal = build_call_expr_loc (input_location,
10308 : : builtin_decl_explicit (BUILT_IN_ISNORMAL),
10309 : : 1, arg);
10310 : 312 : iszero = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
10311 : 312 : build_real_from_int_cst (TREE_TYPE (arg),
10312 : 312 : integer_zero_node));
10313 : 312 : se->expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
10314 : : logical_type_node, isnormal, iszero);
10315 : 312 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10316 : 312 : }
10317 : :
10318 : :
10319 : : /* Generate code for IEEE_IS_NEGATIVE intrinsic:
10320 : : IEEE_IS_NEGATIVE(x) --> (__builtin_signbit(x) && !__builtin_isnan(x)) */
10321 : :
10322 : : static void
10323 : 312 : conv_intrinsic_ieee_is_negative (gfc_se * se, gfc_expr * expr)
10324 : : {
10325 : 312 : tree arg, signbit, isnan;
10326 : :
10327 : : /* Convert arg, evaluate it only once. */
10328 : 312 : conv_ieee_function_args (se, expr, &arg, 1);
10329 : 312 : arg = gfc_evaluate_now (arg, &se->pre);
10330 : :
10331 : 312 : isnan = build_call_expr_loc (input_location,
10332 : : builtin_decl_explicit (BUILT_IN_ISNAN),
10333 : : 1, arg);
10334 : 936 : STRIP_TYPE_NOPS (isnan);
10335 : :
10336 : 312 : signbit = build_call_expr_loc (input_location,
10337 : : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10338 : : 1, arg);
10339 : 312 : signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10340 : : signbit, integer_zero_node);
10341 : :
10342 : 312 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10343 : : logical_type_node, signbit,
10344 : : fold_build1_loc (input_location, TRUTH_NOT_EXPR,
10345 : 312 : TREE_TYPE(isnan), isnan));
10346 : :
10347 : 312 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10348 : 312 : }
10349 : :
10350 : :
10351 : : /* Generate code for IEEE_LOGB and IEEE_RINT. */
10352 : :
10353 : : static void
10354 : 240 : conv_intrinsic_ieee_logb_rint (gfc_se * se, gfc_expr * expr,
10355 : : enum built_in_function code)
10356 : : {
10357 : 240 : tree arg, decl, call, fpstate;
10358 : 240 : int argprec;
10359 : :
10360 : 240 : conv_ieee_function_args (se, expr, &arg, 1);
10361 : 240 : argprec = TYPE_PRECISION (TREE_TYPE (arg));
10362 : 240 : decl = builtin_decl_for_precision (code, argprec);
10363 : :
10364 : : /* Save floating-point state. */
10365 : 240 : fpstate = gfc_save_fp_state (&se->pre);
10366 : :
10367 : : /* Make the function call. */
10368 : 240 : call = build_call_expr_loc (input_location, decl, 1, arg);
10369 : 240 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), call);
10370 : :
10371 : : /* Restore floating-point state. */
10372 : 240 : gfc_restore_fp_state (&se->post, fpstate);
10373 : 240 : }
10374 : :
10375 : :
10376 : : /* Generate code for IEEE_REM. */
10377 : :
10378 : : static void
10379 : 84 : conv_intrinsic_ieee_rem (gfc_se * se, gfc_expr * expr)
10380 : : {
10381 : 84 : tree args[2], decl, call, fpstate;
10382 : 84 : int argprec;
10383 : :
10384 : 84 : conv_ieee_function_args (se, expr, args, 2);
10385 : :
10386 : : /* If arguments have unequal size, convert them to the larger. */
10387 : 84 : if (TYPE_PRECISION (TREE_TYPE (args[0]))
10388 : 84 : > TYPE_PRECISION (TREE_TYPE (args[1])))
10389 : 6 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10390 : 78 : else if (TYPE_PRECISION (TREE_TYPE (args[1]))
10391 : 78 : > TYPE_PRECISION (TREE_TYPE (args[0])))
10392 : 24 : args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
10393 : :
10394 : 84 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10395 : 84 : decl = builtin_decl_for_precision (BUILT_IN_REMAINDER, argprec);
10396 : :
10397 : : /* Save floating-point state. */
10398 : 84 : fpstate = gfc_save_fp_state (&se->pre);
10399 : :
10400 : : /* Make the function call. */
10401 : 84 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10402 : 84 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10403 : :
10404 : : /* Restore floating-point state. */
10405 : 84 : gfc_restore_fp_state (&se->post, fpstate);
10406 : 84 : }
10407 : :
10408 : :
10409 : : /* Generate code for IEEE_NEXT_AFTER. */
10410 : :
10411 : : static void
10412 : 180 : conv_intrinsic_ieee_next_after (gfc_se * se, gfc_expr * expr)
10413 : : {
10414 : 180 : tree args[2], decl, call, fpstate;
10415 : 180 : int argprec;
10416 : :
10417 : 180 : conv_ieee_function_args (se, expr, args, 2);
10418 : :
10419 : : /* Result has the characteristics of first argument. */
10420 : 180 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10421 : 180 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10422 : 180 : decl = builtin_decl_for_precision (BUILT_IN_NEXTAFTER, argprec);
10423 : :
10424 : : /* Save floating-point state. */
10425 : 180 : fpstate = gfc_save_fp_state (&se->pre);
10426 : :
10427 : : /* Make the function call. */
10428 : 180 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10429 : 180 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10430 : :
10431 : : /* Restore floating-point state. */
10432 : 180 : gfc_restore_fp_state (&se->post, fpstate);
10433 : 180 : }
10434 : :
10435 : :
10436 : : /* Generate code for IEEE_SCALB. */
10437 : :
10438 : : static void
10439 : 228 : conv_intrinsic_ieee_scalb (gfc_se * se, gfc_expr * expr)
10440 : : {
10441 : 228 : tree args[2], decl, call, huge, type;
10442 : 228 : int argprec, n;
10443 : :
10444 : 228 : conv_ieee_function_args (se, expr, args, 2);
10445 : :
10446 : : /* Result has the characteristics of first argument. */
10447 : 228 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10448 : 228 : decl = builtin_decl_for_precision (BUILT_IN_SCALBN, argprec);
10449 : :
10450 : 228 : if (TYPE_PRECISION (TREE_TYPE (args[1])) > TYPE_PRECISION (integer_type_node))
10451 : : {
10452 : : /* We need to fold the integer into the range of a C int. */
10453 : 18 : args[1] = gfc_evaluate_now (args[1], &se->pre);
10454 : 18 : type = TREE_TYPE (args[1]);
10455 : :
10456 : 18 : n = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
10457 : 18 : huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
10458 : : gfc_c_int_kind);
10459 : 18 : huge = fold_convert (type, huge);
10460 : 18 : args[1] = fold_build2_loc (input_location, MIN_EXPR, type, args[1],
10461 : : huge);
10462 : 18 : args[1] = fold_build2_loc (input_location, MAX_EXPR, type, args[1],
10463 : : fold_build1_loc (input_location, NEGATE_EXPR,
10464 : : type, huge));
10465 : : }
10466 : :
10467 : 228 : args[1] = fold_convert (integer_type_node, args[1]);
10468 : :
10469 : : /* Make the function call. */
10470 : 228 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10471 : 228 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10472 : 228 : }
10473 : :
10474 : :
10475 : : /* Generate code for IEEE_COPY_SIGN. */
10476 : :
10477 : : static void
10478 : 576 : conv_intrinsic_ieee_copy_sign (gfc_se * se, gfc_expr * expr)
10479 : : {
10480 : 576 : tree args[2], decl, sign;
10481 : 576 : int argprec;
10482 : :
10483 : 576 : conv_ieee_function_args (se, expr, args, 2);
10484 : :
10485 : : /* Get the sign of the second argument. */
10486 : 576 : sign = build_call_expr_loc (input_location,
10487 : : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10488 : : 1, args[1]);
10489 : 576 : sign = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10490 : : sign, integer_zero_node);
10491 : :
10492 : : /* Create a value of one, with the right sign. */
10493 : 576 : sign = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
10494 : : sign,
10495 : : fold_build1_loc (input_location, NEGATE_EXPR,
10496 : : integer_type_node,
10497 : : integer_one_node),
10498 : : integer_one_node);
10499 : 576 : args[1] = fold_convert (TREE_TYPE (args[0]), sign);
10500 : :
10501 : 576 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10502 : 576 : decl = builtin_decl_for_precision (BUILT_IN_COPYSIGN, argprec);
10503 : :
10504 : 576 : se->expr = build_call_expr_loc_array (input_location, decl, 2, args);
10505 : 576 : }
10506 : :
10507 : :
10508 : : /* Generate code for IEEE_CLASS. */
10509 : :
10510 : : static void
10511 : 648 : conv_intrinsic_ieee_class (gfc_se *se, gfc_expr *expr)
10512 : : {
10513 : 648 : tree arg, c, t1, t2, t3, t4;
10514 : :
10515 : : /* Convert arg, evaluate it only once. */
10516 : 648 : conv_ieee_function_args (se, expr, &arg, 1);
10517 : 648 : arg = gfc_evaluate_now (arg, &se->pre);
10518 : :
10519 : 648 : c = build_call_expr_loc (input_location,
10520 : : builtin_decl_explicit (BUILT_IN_FPCLASSIFY), 6,
10521 : : build_int_cst (integer_type_node, IEEE_QUIET_NAN),
10522 : : build_int_cst (integer_type_node,
10523 : : IEEE_POSITIVE_INF),
10524 : : build_int_cst (integer_type_node,
10525 : : IEEE_POSITIVE_NORMAL),
10526 : : build_int_cst (integer_type_node,
10527 : : IEEE_POSITIVE_DENORMAL),
10528 : : build_int_cst (integer_type_node,
10529 : : IEEE_POSITIVE_ZERO),
10530 : : arg);
10531 : 648 : c = gfc_evaluate_now (c, &se->pre);
10532 : 648 : t1 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10533 : : c, build_int_cst (integer_type_node,
10534 : : IEEE_QUIET_NAN));
10535 : 648 : t2 = build_call_expr_loc (input_location,
10536 : : builtin_decl_explicit (BUILT_IN_ISSIGNALING), 1,
10537 : : arg);
10538 : 648 : t2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10539 : 648 : t2, build_zero_cst (TREE_TYPE (t2)));
10540 : 648 : t1 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10541 : : logical_type_node, t1, t2);
10542 : 648 : t3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10543 : : c, build_int_cst (integer_type_node,
10544 : : IEEE_POSITIVE_ZERO));
10545 : 648 : t4 = build_call_expr_loc (input_location,
10546 : : builtin_decl_explicit (BUILT_IN_SIGNBIT), 1,
10547 : : arg);
10548 : 648 : t4 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10549 : 648 : t4, build_zero_cst (TREE_TYPE (t4)));
10550 : 648 : t3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10551 : : logical_type_node, t3, t4);
10552 : 648 : int s = IEEE_NEGATIVE_ZERO + IEEE_POSITIVE_ZERO;
10553 : 648 : gcc_assert (IEEE_NEGATIVE_INF == s - IEEE_POSITIVE_INF);
10554 : 648 : gcc_assert (IEEE_NEGATIVE_NORMAL == s - IEEE_POSITIVE_NORMAL);
10555 : 648 : gcc_assert (IEEE_NEGATIVE_DENORMAL == s - IEEE_POSITIVE_DENORMAL);
10556 : 648 : gcc_assert (IEEE_NEGATIVE_SUBNORMAL == s - IEEE_POSITIVE_SUBNORMAL);
10557 : 648 : gcc_assert (IEEE_NEGATIVE_ZERO == s - IEEE_POSITIVE_ZERO);
10558 : 648 : t4 = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (c),
10559 : 648 : build_int_cst (TREE_TYPE (c), s), c);
10560 : 648 : t3 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c),
10561 : : t3, t4, c);
10562 : 648 : t1 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c), t1,
10563 : 648 : build_int_cst (TREE_TYPE (c), IEEE_SIGNALING_NAN),
10564 : : t3);
10565 : 648 : tree type = gfc_typenode_for_spec (&expr->ts);
10566 : : /* Perform a quick sanity check that the return type is
10567 : : IEEE_CLASS_TYPE derived type defined in
10568 : : libgfortran/ieee/ieee_arithmetic.F90
10569 : : Primarily check that it is a derived type with a single
10570 : : member in it. */
10571 : 648 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10572 : 648 : tree field = NULL_TREE;
10573 : 1296 : for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10574 : 648 : if (TREE_CODE (f) == FIELD_DECL)
10575 : : {
10576 : 648 : gcc_assert (field == NULL_TREE);
10577 : : field = f;
10578 : : }
10579 : 648 : gcc_assert (field);
10580 : 648 : t1 = fold_convert (TREE_TYPE (field), t1);
10581 : 648 : se->expr = build_constructor_single (type, field, t1);
10582 : 648 : }
10583 : :
10584 : :
10585 : : /* Generate code for IEEE_VALUE. */
10586 : :
10587 : : static void
10588 : 1111 : conv_intrinsic_ieee_value (gfc_se *se, gfc_expr *expr)
10589 : : {
10590 : 1111 : tree args[2], arg, ret, tmp;
10591 : 1111 : stmtblock_t body;
10592 : :
10593 : : /* Convert args, evaluate the second one only once. */
10594 : 1111 : conv_ieee_function_args (se, expr, args, 2);
10595 : 1111 : arg = gfc_evaluate_now (args[1], &se->pre);
10596 : :
10597 : 1111 : tree type = TREE_TYPE (arg);
10598 : : /* Perform a quick sanity check that the second argument's type is
10599 : : IEEE_CLASS_TYPE derived type defined in
10600 : : libgfortran/ieee/ieee_arithmetic.F90
10601 : : Primarily check that it is a derived type with a single
10602 : : member in it. */
10603 : 1111 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10604 : 1111 : tree field = NULL_TREE;
10605 : 2222 : for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10606 : 1111 : if (TREE_CODE (f) == FIELD_DECL)
10607 : : {
10608 : 1111 : gcc_assert (field == NULL_TREE);
10609 : : field = f;
10610 : : }
10611 : 1111 : gcc_assert (field);
10612 : 1111 : arg = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
10613 : : arg, field, NULL_TREE);
10614 : 1111 : arg = gfc_evaluate_now (arg, &se->pre);
10615 : :
10616 : 1111 : type = gfc_typenode_for_spec (&expr->ts);
10617 : 1111 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
10618 : 1111 : ret = gfc_create_var (type, NULL);
10619 : :
10620 : 1111 : gfc_init_block (&body);
10621 : :
10622 : 1111 : tree end_label = gfc_build_label_decl (NULL_TREE);
10623 : 12221 : for (int c = IEEE_SIGNALING_NAN; c <= IEEE_POSITIVE_INF; ++c)
10624 : : {
10625 : 11110 : tree label = gfc_build_label_decl (NULL_TREE);
10626 : 11110 : tree low = build_int_cst (TREE_TYPE (arg), c);
10627 : 11110 : tmp = build_case_label (low, low, label);
10628 : 11110 : gfc_add_expr_to_block (&body, tmp);
10629 : :
10630 : 11110 : REAL_VALUE_TYPE real;
10631 : 11110 : int k;
10632 : 11110 : switch (c)
10633 : : {
10634 : 1111 : case IEEE_SIGNALING_NAN:
10635 : 1111 : real_nan (&real, "", 0, TYPE_MODE (type));
10636 : 1111 : break;
10637 : 1111 : case IEEE_QUIET_NAN:
10638 : 1111 : real_nan (&real, "", 1, TYPE_MODE (type));
10639 : 1111 : break;
10640 : 1111 : case IEEE_NEGATIVE_INF:
10641 : 1111 : real_inf (&real);
10642 : 1111 : real = real_value_negate (&real);
10643 : 1111 : break;
10644 : 1111 : case IEEE_NEGATIVE_NORMAL:
10645 : 1111 : real_from_integer (&real, TYPE_MODE (type), -42, SIGNED);
10646 : 1111 : break;
10647 : 1111 : case IEEE_NEGATIVE_DENORMAL:
10648 : 1111 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10649 : 1111 : real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10650 : : type, GFC_RND_MODE);
10651 : 1111 : real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10652 : 1111 : real = real_value_negate (&real);
10653 : 1111 : break;
10654 : 1111 : case IEEE_NEGATIVE_ZERO:
10655 : 1111 : real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10656 : 1111 : real = real_value_negate (&real);
10657 : 1111 : break;
10658 : 1111 : case IEEE_POSITIVE_ZERO:
10659 : : /* Make this also the default: label. The other possibility
10660 : : would be to add a separate default: label followed by
10661 : : __builtin_unreachable (). */
10662 : 1111 : label = gfc_build_label_decl (NULL_TREE);
10663 : 1111 : tmp = build_case_label (NULL_TREE, NULL_TREE, label);
10664 : 1111 : gfc_add_expr_to_block (&body, tmp);
10665 : 1111 : real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10666 : 1111 : break;
10667 : 1111 : case IEEE_POSITIVE_DENORMAL:
10668 : 1111 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10669 : 1111 : real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10670 : : type, GFC_RND_MODE);
10671 : 1111 : real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10672 : 1111 : break;
10673 : 1111 : case IEEE_POSITIVE_NORMAL:
10674 : 1111 : real_from_integer (&real, TYPE_MODE (type), 42, SIGNED);
10675 : 1111 : break;
10676 : 1111 : case IEEE_POSITIVE_INF:
10677 : 1111 : real_inf (&real);
10678 : 1111 : break;
10679 : : default:
10680 : : gcc_unreachable ();
10681 : : }
10682 : :
10683 : 11110 : tree val = build_real (type, real);
10684 : 11110 : gfc_add_modify (&body, ret, val);
10685 : :
10686 : 11110 : tmp = build1_v (GOTO_EXPR, end_label);
10687 : 11110 : gfc_add_expr_to_block (&body, tmp);
10688 : : }
10689 : :
10690 : 1111 : tmp = gfc_finish_block (&body);
10691 : 1111 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, arg, tmp);
10692 : 1111 : gfc_add_expr_to_block (&se->pre, tmp);
10693 : :
10694 : 1111 : tmp = build1_v (LABEL_EXPR, end_label);
10695 : 1111 : gfc_add_expr_to_block (&se->pre, tmp);
10696 : :
10697 : 1111 : se->expr = ret;
10698 : 1111 : }
10699 : :
10700 : :
10701 : : /* Generate code for IEEE_FMA. */
10702 : :
10703 : : static void
10704 : 120 : conv_intrinsic_ieee_fma (gfc_se * se, gfc_expr * expr)
10705 : : {
10706 : 120 : tree args[3], decl, call;
10707 : 120 : int argprec;
10708 : :
10709 : 120 : conv_ieee_function_args (se, expr, args, 3);
10710 : :
10711 : : /* All three arguments should have the same type. */
10712 : 120 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10713 : 120 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[2])));
10714 : :
10715 : : /* Call the type-generic FMA built-in. */
10716 : 120 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10717 : 120 : decl = builtin_decl_for_precision (BUILT_IN_FMA, argprec);
10718 : 120 : call = build_call_expr_loc_array (input_location, decl, 3, args);
10719 : :
10720 : : /* Convert to the final type. */
10721 : 120 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10722 : 120 : }
10723 : :
10724 : :
10725 : : /* Generate code for IEEE_{MIN,MAX}_NUM{,_MAG}. */
10726 : :
10727 : : static void
10728 : 3072 : conv_intrinsic_ieee_minmax (gfc_se * se, gfc_expr * expr, int max,
10729 : : const char *name)
10730 : : {
10731 : 3072 : tree args[2], func;
10732 : 3072 : built_in_function fn;
10733 : :
10734 : 3072 : conv_ieee_function_args (se, expr, args, 2);
10735 : 3072 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10736 : 3072 : args[0] = gfc_evaluate_now (args[0], &se->pre);
10737 : 3072 : args[1] = gfc_evaluate_now (args[1], &se->pre);
10738 : :
10739 : 3072 : if (startswith (name, "mag"))
10740 : : {
10741 : : /* IEEE_MIN_NUM_MAG and IEEE_MAX_NUM_MAG translate to C functions
10742 : : fminmag() and fmaxmag(), which do not exist as built-ins.
10743 : :
10744 : : Following glibc, we emit this:
10745 : :
10746 : : fminmag (x, y) {
10747 : : ax = ABS (x);
10748 : : ay = ABS (y);
10749 : : if (isless (ax, ay))
10750 : : return x;
10751 : : else if (isgreater (ax, ay))
10752 : : return y;
10753 : : else if (ax == ay)
10754 : : return x < y ? x : y;
10755 : : else if (issignaling (x) || issignaling (y))
10756 : : return x + y;
10757 : : else
10758 : : return isnan (y) ? x : y;
10759 : : }
10760 : :
10761 : : fmaxmag (x, y) {
10762 : : ax = ABS (x);
10763 : : ay = ABS (y);
10764 : : if (isgreater (ax, ay))
10765 : : return x;
10766 : : else if (isless (ax, ay))
10767 : : return y;
10768 : : else if (ax == ay)
10769 : : return x > y ? x : y;
10770 : : else if (issignaling (x) || issignaling (y))
10771 : : return x + y;
10772 : : else
10773 : : return isnan (y) ? x : y;
10774 : : }
10775 : :
10776 : : */
10777 : :
10778 : 1536 : tree abs0, abs1, sig0, sig1;
10779 : 1536 : tree cond1, cond2, cond3, cond4, cond5;
10780 : 1536 : tree res;
10781 : 1536 : tree type = TREE_TYPE (args[0]);
10782 : :
10783 : 1536 : func = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
10784 : 1536 : abs0 = build_call_expr_loc (input_location, func, 1, args[0]);
10785 : 1536 : abs1 = build_call_expr_loc (input_location, func, 1, args[1]);
10786 : 1536 : abs0 = gfc_evaluate_now (abs0, &se->pre);
10787 : 1536 : abs1 = gfc_evaluate_now (abs1, &se->pre);
10788 : :
10789 : 1536 : cond5 = build_call_expr_loc (input_location,
10790 : : builtin_decl_explicit (BUILT_IN_ISNAN),
10791 : : 1, args[1]);
10792 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond5,
10793 : : args[0], args[1]);
10794 : :
10795 : 1536 : sig0 = build_call_expr_loc (input_location,
10796 : : builtin_decl_explicit (BUILT_IN_ISSIGNALING),
10797 : : 1, args[0]);
10798 : 1536 : sig1 = build_call_expr_loc (input_location,
10799 : : builtin_decl_explicit (BUILT_IN_ISSIGNALING),
10800 : : 1, args[1]);
10801 : 1536 : cond4 = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
10802 : : logical_type_node, sig0, sig1);
10803 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond4,
10804 : : fold_build2_loc (input_location, PLUS_EXPR,
10805 : : type, args[0], args[1]),
10806 : : res);
10807 : :
10808 : 1536 : cond3 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10809 : : abs0, abs1);
10810 : 2304 : res = fold_build3_loc (input_location, COND_EXPR, type, cond3,
10811 : : fold_build2_loc (input_location,
10812 : : max ? MAX_EXPR : MIN_EXPR,
10813 : : type, args[0], args[1]),
10814 : : res);
10815 : :
10816 : 2304 : func = builtin_decl_explicit (max ? BUILT_IN_ISLESS : BUILT_IN_ISGREATER);
10817 : 1536 : cond2 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
10818 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond2,
10819 : : args[1], res);
10820 : :
10821 : 2304 : func = builtin_decl_explicit (max ? BUILT_IN_ISGREATER : BUILT_IN_ISLESS);
10822 : 1536 : cond1 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
10823 : 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond1,
10824 : : args[0], res);
10825 : :
10826 : 1536 : se->expr = res;
10827 : : }
10828 : : else
10829 : : {
10830 : : /* IEEE_MIN_NUM and IEEE_MAX_NUM translate to fmin() and fmax(). */
10831 : 1536 : fn = max ? BUILT_IN_FMAX : BUILT_IN_FMIN;
10832 : 1536 : func = gfc_builtin_decl_for_float_kind (fn, expr->ts.kind);
10833 : 1536 : se->expr = build_call_expr_loc_array (input_location, func, 2, args);
10834 : : }
10835 : 3072 : }
10836 : :
10837 : :
10838 : : /* Generate code for comparison functions IEEE_QUIET_* and
10839 : : IEEE_SIGNALING_*. */
10840 : :
10841 : : static void
10842 : 3888 : conv_intrinsic_ieee_comparison (gfc_se * se, gfc_expr * expr, int signaling,
10843 : : const char *name)
10844 : : {
10845 : 3888 : tree args[2];
10846 : 3888 : tree arg1, arg2, res;
10847 : :
10848 : : /* Evaluate arguments only once. */
10849 : 3888 : conv_ieee_function_args (se, expr, args, 2);
10850 : 3888 : arg1 = gfc_evaluate_now (args[0], &se->pre);
10851 : 3888 : arg2 = gfc_evaluate_now (args[1], &se->pre);
10852 : :
10853 : 3888 : if (startswith (name, "eq"))
10854 : : {
10855 : 648 : if (signaling)
10856 : 324 : res = build_call_expr_loc (input_location,
10857 : : builtin_decl_explicit (BUILT_IN_ISEQSIG),
10858 : : 2, arg1, arg2);
10859 : : else
10860 : 324 : res = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10861 : : arg1, arg2);
10862 : : }
10863 : 3240 : else if (startswith (name, "ne"))
10864 : : {
10865 : 648 : if (signaling)
10866 : : {
10867 : 324 : res = build_call_expr_loc (input_location,
10868 : : builtin_decl_explicit (BUILT_IN_ISEQSIG),
10869 : : 2, arg1, arg2);
10870 : 324 : res = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
10871 : : logical_type_node, res);
10872 : : }
10873 : : else
10874 : 324 : res = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10875 : : arg1, arg2);
10876 : : }
10877 : 2592 : else if (startswith (name, "ge"))
10878 : : {
10879 : 648 : if (signaling)
10880 : 324 : res = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10881 : : arg1, arg2);
10882 : : else
10883 : 324 : res = build_call_expr_loc (input_location,
10884 : : builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL),
10885 : : 2, arg1, arg2);
10886 : : }
10887 : 1944 : else if (startswith (name, "gt"))
10888 : : {
10889 : 648 : if (signaling)
10890 : 324 : res = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
10891 : : arg1, arg2);
10892 : : else
10893 : 324 : res = build_call_expr_loc (input_location,
10894 : : builtin_decl_explicit (BUILT_IN_ISGREATER),
10895 : : 2, arg1, arg2);
10896 : : }
10897 : 1296 : else if (startswith (name, "le"))
10898 : : {
10899 : 648 : if (signaling)
10900 : 324 : res = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
10901 : : arg1, arg2);
10902 : : else
10903 : 324 : res = build_call_expr_loc (input_location,
10904 : : builtin_decl_explicit (BUILT_IN_ISLESSEQUAL),
10905 : : 2, arg1, arg2);
10906 : : }
10907 : 648 : else if (startswith (name, "lt"))
10908 : : {
10909 : 648 : if (signaling)
10910 : 324 : res = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
10911 : : arg1, arg2);
10912 : : else
10913 : 324 : res = build_call_expr_loc (input_location,
10914 : : builtin_decl_explicit (BUILT_IN_ISLESS),
10915 : : 2, arg1, arg2);
10916 : : }
10917 : : else
10918 : 0 : gcc_unreachable ();
10919 : :
10920 : 3888 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), res);
10921 : 3888 : }
10922 : :
10923 : :
10924 : : /* Generate code for an intrinsic function from the IEEE_ARITHMETIC
10925 : : module. */
10926 : :
10927 : : bool
10928 : 13939 : gfc_conv_ieee_arithmetic_function (gfc_se * se, gfc_expr * expr)
10929 : : {
10930 : 13939 : const char *name = expr->value.function.name;
10931 : :
10932 : 13939 : if (startswith (name, "_gfortran_ieee_is_nan"))
10933 : 522 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISNAN, 1);
10934 : 13417 : else if (startswith (name, "_gfortran_ieee_is_finite"))
10935 : 372 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISFINITE, 1);
10936 : 13045 : else if (startswith (name, "_gfortran_ieee_unordered"))
10937 : 168 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISUNORDERED, 2);
10938 : 12877 : else if (startswith (name, "_gfortran_ieee_signbit"))
10939 : 624 : conv_intrinsic_ieee_signbit (se, expr);
10940 : 12253 : else if (startswith (name, "_gfortran_ieee_is_normal"))
10941 : 312 : conv_intrinsic_ieee_is_normal (se, expr);
10942 : 11941 : else if (startswith (name, "_gfortran_ieee_is_negative"))
10943 : 312 : conv_intrinsic_ieee_is_negative (se, expr);
10944 : 11629 : else if (startswith (name, "_gfortran_ieee_copy_sign"))
10945 : 576 : conv_intrinsic_ieee_copy_sign (se, expr);
10946 : 11053 : else if (startswith (name, "_gfortran_ieee_scalb"))
10947 : 228 : conv_intrinsic_ieee_scalb (se, expr);
10948 : 10825 : else if (startswith (name, "_gfortran_ieee_next_after"))
10949 : 180 : conv_intrinsic_ieee_next_after (se, expr);
10950 : 10645 : else if (startswith (name, "_gfortran_ieee_rem"))
10951 : 84 : conv_intrinsic_ieee_rem (se, expr);
10952 : 10561 : else if (startswith (name, "_gfortran_ieee_logb"))
10953 : 144 : conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_LOGB);
10954 : 10417 : else if (startswith (name, "_gfortran_ieee_rint"))
10955 : 96 : conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_RINT);
10956 : 10321 : else if (startswith (name, "ieee_class_") && ISDIGIT (name[11]))
10957 : 648 : conv_intrinsic_ieee_class (se, expr);
10958 : 9673 : else if (startswith (name, "ieee_value_") && ISDIGIT (name[11]))
10959 : 1111 : conv_intrinsic_ieee_value (se, expr);
10960 : 8562 : else if (startswith (name, "_gfortran_ieee_fma"))
10961 : 120 : conv_intrinsic_ieee_fma (se, expr);
10962 : 8442 : else if (startswith (name, "_gfortran_ieee_min_num_"))
10963 : 1536 : conv_intrinsic_ieee_minmax (se, expr, 0, name + 23);
10964 : 6906 : else if (startswith (name, "_gfortran_ieee_max_num_"))
10965 : 1536 : conv_intrinsic_ieee_minmax (se, expr, 1, name + 23);
10966 : 5370 : else if (startswith (name, "_gfortran_ieee_quiet_"))
10967 : 1944 : conv_intrinsic_ieee_comparison (se, expr, 0, name + 21);
10968 : 3426 : else if (startswith (name, "_gfortran_ieee_signaling_"))
10969 : 1944 : conv_intrinsic_ieee_comparison (se, expr, 1, name + 25);
10970 : : else
10971 : : /* It is not among the functions we translate directly. We return
10972 : : false, so a library function call is emitted. */
10973 : : return false;
10974 : :
10975 : : return true;
10976 : : }
10977 : :
10978 : :
10979 : : /* Generate a direct call to malloc() for the MALLOC intrinsic. */
10980 : :
10981 : : static void
10982 : 16 : gfc_conv_intrinsic_malloc (gfc_se * se, gfc_expr * expr)
10983 : : {
10984 : 16 : tree arg, res, restype;
10985 : :
10986 : 16 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
10987 : 16 : arg = fold_convert (size_type_node, arg);
10988 : 16 : res = build_call_expr_loc (input_location,
10989 : : builtin_decl_explicit (BUILT_IN_MALLOC), 1, arg);
10990 : 16 : restype = gfc_typenode_for_spec (&expr->ts);
10991 : 16 : se->expr = fold_convert (restype, res);
10992 : 16 : }
10993 : :
10994 : :
10995 : : /* Generate code for an intrinsic function. Some map directly to library
10996 : : calls, others get special handling. In some cases the name of the function
10997 : : used depends on the type specifiers. */
10998 : :
10999 : : void
11000 : 252838 : gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
11001 : : {
11002 : 252838 : const char *name;
11003 : 252838 : int lib, kind;
11004 : 252838 : tree fndecl;
11005 : :
11006 : 252838 : name = &expr->value.function.name[2];
11007 : :
11008 : 252838 : if (expr->rank > 0)
11009 : : {
11010 : 50887 : lib = gfc_is_intrinsic_libcall (expr);
11011 : 50887 : if (lib != 0)
11012 : : {
11013 : 19282 : if (lib == 1)
11014 : 11673 : se->ignore_optional = 1;
11015 : :
11016 : 19282 : switch (expr->value.function.isym->id)
11017 : : {
11018 : 6086 : case GFC_ISYM_EOSHIFT:
11019 : 6086 : case GFC_ISYM_PACK:
11020 : 6086 : case GFC_ISYM_RESHAPE:
11021 : 6086 : case GFC_ISYM_REDUCE:
11022 : : /* For all of those the first argument specifies the type and the
11023 : : third is optional. */
11024 : 6086 : conv_generic_with_optional_char_arg (se, expr, 1, 3);
11025 : 6086 : break;
11026 : :
11027 : 1116 : case GFC_ISYM_FINDLOC:
11028 : 1116 : gfc_conv_intrinsic_findloc (se, expr);
11029 : 1116 : break;
11030 : :
11031 : 2935 : case GFC_ISYM_MINLOC:
11032 : 2935 : gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
11033 : 2935 : break;
11034 : :
11035 : 2439 : case GFC_ISYM_MAXLOC:
11036 : 2439 : gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
11037 : 2439 : break;
11038 : :
11039 : 6706 : default:
11040 : 6706 : gfc_conv_intrinsic_funcall (se, expr);
11041 : 6706 : break;
11042 : : }
11043 : :
11044 : 19282 : return;
11045 : : }
11046 : : }
11047 : :
11048 : 233556 : switch (expr->value.function.isym->id)
11049 : : {
11050 : 0 : case GFC_ISYM_NONE:
11051 : 0 : gcc_unreachable ();
11052 : :
11053 : 529 : case GFC_ISYM_REPEAT:
11054 : 529 : gfc_conv_intrinsic_repeat (se, expr);
11055 : 529 : break;
11056 : :
11057 : 574 : case GFC_ISYM_TRIM:
11058 : 574 : gfc_conv_intrinsic_trim (se, expr);
11059 : 574 : break;
11060 : :
11061 : 42 : case GFC_ISYM_SC_KIND:
11062 : 42 : gfc_conv_intrinsic_sc_kind (se, expr);
11063 : 42 : break;
11064 : :
11065 : 45 : case GFC_ISYM_SI_KIND:
11066 : 45 : gfc_conv_intrinsic_si_kind (se, expr);
11067 : 45 : break;
11068 : :
11069 : 6 : case GFC_ISYM_SL_KIND:
11070 : 6 : gfc_conv_intrinsic_sl_kind (se, expr);
11071 : 6 : break;
11072 : :
11073 : 82 : case GFC_ISYM_SR_KIND:
11074 : 82 : gfc_conv_intrinsic_sr_kind (se, expr);
11075 : 82 : break;
11076 : :
11077 : 228 : case GFC_ISYM_EXPONENT:
11078 : 228 : gfc_conv_intrinsic_exponent (se, expr);
11079 : 228 : break;
11080 : :
11081 : 316 : case GFC_ISYM_SCAN:
11082 : 316 : kind = expr->value.function.actual->expr->ts.kind;
11083 : 316 : if (kind == 1)
11084 : 250 : fndecl = gfor_fndecl_string_scan;
11085 : 66 : else if (kind == 4)
11086 : 66 : fndecl = gfor_fndecl_string_scan_char4;
11087 : : else
11088 : 0 : gcc_unreachable ();
11089 : :
11090 : 316 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
11091 : 316 : break;
11092 : :
11093 : 94 : case GFC_ISYM_VERIFY:
11094 : 94 : kind = expr->value.function.actual->expr->ts.kind;
11095 : 94 : if (kind == 1)
11096 : 70 : fndecl = gfor_fndecl_string_verify;
11097 : 24 : else if (kind == 4)
11098 : 24 : fndecl = gfor_fndecl_string_verify_char4;
11099 : : else
11100 : 0 : gcc_unreachable ();
11101 : :
11102 : 94 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
11103 : 94 : break;
11104 : :
11105 : 7045 : case GFC_ISYM_ALLOCATED:
11106 : 7045 : gfc_conv_allocated (se, expr);
11107 : 7045 : break;
11108 : :
11109 : 8857 : case GFC_ISYM_ASSOCIATED:
11110 : 8857 : gfc_conv_associated(se, expr);
11111 : 8857 : break;
11112 : :
11113 : 409 : case GFC_ISYM_SAME_TYPE_AS:
11114 : 409 : gfc_conv_same_type_as (se, expr);
11115 : 409 : break;
11116 : :
11117 : 7723 : case GFC_ISYM_ABS:
11118 : 7723 : gfc_conv_intrinsic_abs (se, expr);
11119 : 7723 : break;
11120 : :
11121 : 351 : case GFC_ISYM_ADJUSTL:
11122 : 351 : if (expr->ts.kind == 1)
11123 : 297 : fndecl = gfor_fndecl_adjustl;
11124 : 54 : else if (expr->ts.kind == 4)
11125 : 54 : fndecl = gfor_fndecl_adjustl_char4;
11126 : : else
11127 : 0 : gcc_unreachable ();
11128 : :
11129 : 351 : gfc_conv_intrinsic_adjust (se, expr, fndecl);
11130 : 351 : break;
11131 : :
11132 : 111 : case GFC_ISYM_ADJUSTR:
11133 : 111 : if (expr->ts.kind == 1)
11134 : 56 : fndecl = gfor_fndecl_adjustr;
11135 : 55 : else if (expr->ts.kind == 4)
11136 : 55 : fndecl = gfor_fndecl_adjustr_char4;
11137 : : else
11138 : 0 : gcc_unreachable ();
11139 : :
11140 : 111 : gfc_conv_intrinsic_adjust (se, expr, fndecl);
11141 : 111 : break;
11142 : :
11143 : 428 : case GFC_ISYM_AIMAG:
11144 : 428 : gfc_conv_intrinsic_imagpart (se, expr);
11145 : 428 : break;
11146 : :
11147 : 146 : case GFC_ISYM_AINT:
11148 : 146 : gfc_conv_intrinsic_aint (se, expr, RND_TRUNC);
11149 : 146 : break;
11150 : :
11151 : 419 : case GFC_ISYM_ALL:
11152 : 419 : gfc_conv_intrinsic_anyall (se, expr, EQ_EXPR);
11153 : 419 : break;
11154 : :
11155 : 74 : case GFC_ISYM_ANINT:
11156 : 74 : gfc_conv_intrinsic_aint (se, expr, RND_ROUND);
11157 : 74 : break;
11158 : :
11159 : 90 : case GFC_ISYM_AND:
11160 : 90 : gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
11161 : 90 : break;
11162 : :
11163 : 36749 : case GFC_ISYM_ANY:
11164 : 36749 : gfc_conv_intrinsic_anyall (se, expr, NE_EXPR);
11165 : 36749 : break;
11166 : :
11167 : 216 : case GFC_ISYM_ACOSD:
11168 : 216 : case GFC_ISYM_ASIND:
11169 : 216 : case GFC_ISYM_ATAND:
11170 : 216 : gfc_conv_intrinsic_atrigd (se, expr, expr->value.function.isym->id);
11171 : 216 : break;
11172 : :
11173 : 102 : case GFC_ISYM_COTAN:
11174 : 102 : gfc_conv_intrinsic_cotan (se, expr);
11175 : 102 : break;
11176 : :
11177 : 108 : case GFC_ISYM_COTAND:
11178 : 108 : gfc_conv_intrinsic_cotand (se, expr);
11179 : 108 : break;
11180 : :
11181 : 120 : case GFC_ISYM_ATAN2D:
11182 : 120 : gfc_conv_intrinsic_atan2d (se, expr);
11183 : 120 : break;
11184 : :
11185 : 145 : case GFC_ISYM_BTEST:
11186 : 145 : gfc_conv_intrinsic_btest (se, expr);
11187 : 145 : break;
11188 : :
11189 : 54 : case GFC_ISYM_BGE:
11190 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, GE_EXPR);
11191 : 54 : break;
11192 : :
11193 : 54 : case GFC_ISYM_BGT:
11194 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, GT_EXPR);
11195 : 54 : break;
11196 : :
11197 : 54 : case GFC_ISYM_BLE:
11198 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, LE_EXPR);
11199 : 54 : break;
11200 : :
11201 : 54 : case GFC_ISYM_BLT:
11202 : 54 : gfc_conv_intrinsic_bitcomp (se, expr, LT_EXPR);
11203 : 54 : break;
11204 : :
11205 : 8990 : case GFC_ISYM_C_ASSOCIATED:
11206 : 8990 : case GFC_ISYM_C_FUNLOC:
11207 : 8990 : case GFC_ISYM_C_LOC:
11208 : 8990 : case GFC_ISYM_F_C_STRING:
11209 : 8990 : conv_isocbinding_function (se, expr);
11210 : 8990 : break;
11211 : :
11212 : 2020 : case GFC_ISYM_ACHAR:
11213 : 2020 : case GFC_ISYM_CHAR:
11214 : 2020 : gfc_conv_intrinsic_char (se, expr);
11215 : 2020 : break;
11216 : :
11217 : 38093 : case GFC_ISYM_CONVERSION:
11218 : 38093 : case GFC_ISYM_DBLE:
11219 : 38093 : case GFC_ISYM_DFLOAT:
11220 : 38093 : case GFC_ISYM_FLOAT:
11221 : 38093 : case GFC_ISYM_LOGICAL:
11222 : 38093 : case GFC_ISYM_REAL:
11223 : 38093 : case GFC_ISYM_REALPART:
11224 : 38093 : case GFC_ISYM_SNGL:
11225 : 38093 : gfc_conv_intrinsic_conversion (se, expr);
11226 : 38093 : break;
11227 : :
11228 : : /* Integer conversions are handled separately to make sure we get the
11229 : : correct rounding mode. */
11230 : 2710 : case GFC_ISYM_INT:
11231 : 2710 : case GFC_ISYM_INT2:
11232 : 2710 : case GFC_ISYM_INT8:
11233 : 2710 : case GFC_ISYM_LONG:
11234 : 2710 : case GFC_ISYM_UINT:
11235 : 2710 : gfc_conv_intrinsic_int (se, expr, RND_TRUNC);
11236 : 2710 : break;
11237 : :
11238 : 162 : case GFC_ISYM_NINT:
11239 : 162 : gfc_conv_intrinsic_int (se, expr, RND_ROUND);
11240 : 162 : break;
11241 : :
11242 : 16 : case GFC_ISYM_CEILING:
11243 : 16 : gfc_conv_intrinsic_int (se, expr, RND_CEIL);
11244 : 16 : break;
11245 : :
11246 : 116 : case GFC_ISYM_FLOOR:
11247 : 116 : gfc_conv_intrinsic_int (se, expr, RND_FLOOR);
11248 : 116 : break;
11249 : :
11250 : 2765 : case GFC_ISYM_MOD:
11251 : 2765 : gfc_conv_intrinsic_mod (se, expr, 0);
11252 : 2765 : break;
11253 : :
11254 : 434 : case GFC_ISYM_MODULO:
11255 : 434 : gfc_conv_intrinsic_mod (se, expr, 1);
11256 : 434 : break;
11257 : :
11258 : 895 : case GFC_ISYM_CAF_GET:
11259 : 895 : gfc_conv_intrinsic_caf_get (se, expr, NULL_TREE, false, NULL);
11260 : 895 : break;
11261 : :
11262 : 132 : case GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE:
11263 : 132 : gfc_conv_intrinsic_caf_is_present_remote (se, expr);
11264 : 132 : break;
11265 : :
11266 : 485 : case GFC_ISYM_CMPLX:
11267 : 485 : gfc_conv_intrinsic_cmplx (se, expr, name[5] == '1');
11268 : 485 : break;
11269 : :
11270 : 10 : case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
11271 : 10 : gfc_conv_intrinsic_iargc (se, expr);
11272 : 10 : break;
11273 : :
11274 : 6 : case GFC_ISYM_COMPLEX:
11275 : 6 : gfc_conv_intrinsic_cmplx (se, expr, 1);
11276 : 6 : break;
11277 : :
11278 : 257 : case GFC_ISYM_CONJG:
11279 : 257 : gfc_conv_intrinsic_conjg (se, expr);
11280 : 257 : break;
11281 : :
11282 : 143 : case GFC_ISYM_COUNT:
11283 : 143 : gfc_conv_intrinsic_count (se, expr);
11284 : 143 : break;
11285 : :
11286 : 0 : case GFC_ISYM_CTIME:
11287 : 0 : gfc_conv_intrinsic_ctime (se, expr);
11288 : 0 : break;
11289 : :
11290 : 96 : case GFC_ISYM_DIM:
11291 : 96 : gfc_conv_intrinsic_dim (se, expr);
11292 : 96 : break;
11293 : :
11294 : 113 : case GFC_ISYM_DOT_PRODUCT:
11295 : 113 : gfc_conv_intrinsic_dot_product (se, expr);
11296 : 113 : break;
11297 : :
11298 : 13 : case GFC_ISYM_DPROD:
11299 : 13 : gfc_conv_intrinsic_dprod (se, expr);
11300 : 13 : break;
11301 : :
11302 : 66 : case GFC_ISYM_DSHIFTL:
11303 : 66 : gfc_conv_intrinsic_dshift (se, expr, true);
11304 : 66 : break;
11305 : :
11306 : 66 : case GFC_ISYM_DSHIFTR:
11307 : 66 : gfc_conv_intrinsic_dshift (se, expr, false);
11308 : 66 : break;
11309 : :
11310 : 0 : case GFC_ISYM_FDATE:
11311 : 0 : gfc_conv_intrinsic_fdate (se, expr);
11312 : 0 : break;
11313 : :
11314 : 60 : case GFC_ISYM_FRACTION:
11315 : 60 : gfc_conv_intrinsic_fraction (se, expr);
11316 : 60 : break;
11317 : :
11318 : 24 : case GFC_ISYM_IALL:
11319 : 24 : gfc_conv_intrinsic_arith (se, expr, BIT_AND_EXPR, false);
11320 : 24 : break;
11321 : :
11322 : 600 : case GFC_ISYM_IAND:
11323 : 600 : gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
11324 : 600 : break;
11325 : :
11326 : 12 : case GFC_ISYM_IANY:
11327 : 12 : gfc_conv_intrinsic_arith (se, expr, BIT_IOR_EXPR, false);
11328 : 12 : break;
11329 : :
11330 : 168 : case GFC_ISYM_IBCLR:
11331 : 168 : gfc_conv_intrinsic_singlebitop (se, expr, 0);
11332 : 168 : break;
11333 : :
11334 : 27 : case GFC_ISYM_IBITS:
11335 : 27 : gfc_conv_intrinsic_ibits (se, expr);
11336 : 27 : break;
11337 : :
11338 : 138 : case GFC_ISYM_IBSET:
11339 : 138 : gfc_conv_intrinsic_singlebitop (se, expr, 1);
11340 : 138 : break;
11341 : :
11342 : 2033 : case GFC_ISYM_IACHAR:
11343 : 2033 : case GFC_ISYM_ICHAR:
11344 : : /* We assume ASCII character sequence. */
11345 : 2033 : gfc_conv_intrinsic_ichar (se, expr);
11346 : 2033 : break;
11347 : :
11348 : 2 : case GFC_ISYM_IARGC:
11349 : 2 : gfc_conv_intrinsic_iargc (se, expr);
11350 : 2 : break;
11351 : :
11352 : 688 : case GFC_ISYM_IEOR:
11353 : 688 : gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11354 : 688 : break;
11355 : :
11356 : 329 : case GFC_ISYM_INDEX:
11357 : 329 : kind = expr->value.function.actual->expr->ts.kind;
11358 : 329 : if (kind == 1)
11359 : 263 : fndecl = gfor_fndecl_string_index;
11360 : 66 : else if (kind == 4)
11361 : 66 : fndecl = gfor_fndecl_string_index_char4;
11362 : : else
11363 : 0 : gcc_unreachable ();
11364 : :
11365 : 329 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
11366 : 329 : break;
11367 : :
11368 : 483 : case GFC_ISYM_IOR:
11369 : 483 : gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11370 : 483 : break;
11371 : :
11372 : 12 : case GFC_ISYM_IPARITY:
11373 : 12 : gfc_conv_intrinsic_arith (se, expr, BIT_XOR_EXPR, false);
11374 : 12 : break;
11375 : :
11376 : 6 : case GFC_ISYM_IS_IOSTAT_END:
11377 : 6 : gfc_conv_has_intvalue (se, expr, LIBERROR_END);
11378 : 6 : break;
11379 : :
11380 : 18 : case GFC_ISYM_IS_IOSTAT_EOR:
11381 : 18 : gfc_conv_has_intvalue (se, expr, LIBERROR_EOR);
11382 : 18 : break;
11383 : :
11384 : 621 : case GFC_ISYM_IS_CONTIGUOUS:
11385 : 621 : gfc_conv_intrinsic_is_contiguous (se, expr);
11386 : 621 : break;
11387 : :
11388 : 432 : case GFC_ISYM_ISNAN:
11389 : 432 : gfc_conv_intrinsic_isnan (se, expr);
11390 : 432 : break;
11391 : :
11392 : 8 : case GFC_ISYM_KILL:
11393 : 8 : conv_intrinsic_kill (se, expr);
11394 : 8 : break;
11395 : :
11396 : 90 : case GFC_ISYM_LSHIFT:
11397 : 90 : gfc_conv_intrinsic_shift (se, expr, false, false);
11398 : 90 : break;
11399 : :
11400 : 24 : case GFC_ISYM_RSHIFT:
11401 : 24 : gfc_conv_intrinsic_shift (se, expr, true, true);
11402 : 24 : break;
11403 : :
11404 : 78 : case GFC_ISYM_SHIFTA:
11405 : 78 : gfc_conv_intrinsic_shift (se, expr, true, true);
11406 : 78 : break;
11407 : :
11408 : 183 : case GFC_ISYM_SHIFTL:
11409 : 183 : gfc_conv_intrinsic_shift (se, expr, false, false);
11410 : 183 : break;
11411 : :
11412 : 66 : case GFC_ISYM_SHIFTR:
11413 : 66 : gfc_conv_intrinsic_shift (se, expr, true, false);
11414 : 66 : break;
11415 : :
11416 : 318 : case GFC_ISYM_ISHFT:
11417 : 318 : gfc_conv_intrinsic_ishft (se, expr);
11418 : 318 : break;
11419 : :
11420 : 658 : case GFC_ISYM_ISHFTC:
11421 : 658 : gfc_conv_intrinsic_ishftc (se, expr);
11422 : 658 : break;
11423 : :
11424 : 270 : case GFC_ISYM_LEADZ:
11425 : 270 : gfc_conv_intrinsic_leadz (se, expr);
11426 : 270 : break;
11427 : :
11428 : 282 : case GFC_ISYM_TRAILZ:
11429 : 282 : gfc_conv_intrinsic_trailz (se, expr);
11430 : 282 : break;
11431 : :
11432 : 103 : case GFC_ISYM_POPCNT:
11433 : 103 : gfc_conv_intrinsic_popcnt_poppar (se, expr, 0);
11434 : 103 : break;
11435 : :
11436 : 31 : case GFC_ISYM_POPPAR:
11437 : 31 : gfc_conv_intrinsic_popcnt_poppar (se, expr, 1);
11438 : 31 : break;
11439 : :
11440 : 5482 : case GFC_ISYM_LBOUND:
11441 : 5482 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_LBOUND);
11442 : 5482 : break;
11443 : :
11444 : 178 : case GFC_ISYM_LCOBOUND:
11445 : 178 : conv_intrinsic_cobound (se, expr);
11446 : 178 : break;
11447 : :
11448 : 744 : case GFC_ISYM_TRANSPOSE:
11449 : : /* The scalarizer has already been set up for reversed dimension access
11450 : : order ; now we just get the argument value normally. */
11451 : 744 : gfc_conv_expr (se, expr->value.function.actual->expr);
11452 : 744 : break;
11453 : :
11454 : 5625 : case GFC_ISYM_LEN:
11455 : 5625 : gfc_conv_intrinsic_len (se, expr);
11456 : 5625 : break;
11457 : :
11458 : 2315 : case GFC_ISYM_LEN_TRIM:
11459 : 2315 : gfc_conv_intrinsic_len_trim (se, expr);
11460 : 2315 : break;
11461 : :
11462 : 18 : case GFC_ISYM_LGE:
11463 : 18 : gfc_conv_intrinsic_strcmp (se, expr, GE_EXPR);
11464 : 18 : break;
11465 : :
11466 : 36 : case GFC_ISYM_LGT:
11467 : 36 : gfc_conv_intrinsic_strcmp (se, expr, GT_EXPR);
11468 : 36 : break;
11469 : :
11470 : 18 : case GFC_ISYM_LLE:
11471 : 18 : gfc_conv_intrinsic_strcmp (se, expr, LE_EXPR);
11472 : 18 : break;
11473 : :
11474 : 27 : case GFC_ISYM_LLT:
11475 : 27 : gfc_conv_intrinsic_strcmp (se, expr, LT_EXPR);
11476 : 27 : break;
11477 : :
11478 : 16 : case GFC_ISYM_MALLOC:
11479 : 16 : gfc_conv_intrinsic_malloc (se, expr);
11480 : 16 : break;
11481 : :
11482 : 32 : case GFC_ISYM_MASKL:
11483 : 32 : gfc_conv_intrinsic_mask (se, expr, 1);
11484 : 32 : break;
11485 : :
11486 : 32 : case GFC_ISYM_MASKR:
11487 : 32 : gfc_conv_intrinsic_mask (se, expr, 0);
11488 : 32 : break;
11489 : :
11490 : 1039 : case GFC_ISYM_MAX:
11491 : 1039 : if (expr->ts.type == BT_CHARACTER)
11492 : 138 : gfc_conv_intrinsic_minmax_char (se, expr, 1);
11493 : : else
11494 : 901 : gfc_conv_intrinsic_minmax (se, expr, GT_EXPR);
11495 : : break;
11496 : :
11497 : 6348 : case GFC_ISYM_MAXLOC:
11498 : 6348 : gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
11499 : 6348 : break;
11500 : :
11501 : 216 : case GFC_ISYM_FINDLOC:
11502 : 216 : gfc_conv_intrinsic_findloc (se, expr);
11503 : 216 : break;
11504 : :
11505 : 1099 : case GFC_ISYM_MAXVAL:
11506 : 1099 : gfc_conv_intrinsic_minmaxval (se, expr, GT_EXPR);
11507 : 1099 : break;
11508 : :
11509 : 949 : case GFC_ISYM_MERGE:
11510 : 949 : gfc_conv_intrinsic_merge (se, expr);
11511 : 949 : break;
11512 : :
11513 : 42 : case GFC_ISYM_MERGE_BITS:
11514 : 42 : gfc_conv_intrinsic_merge_bits (se, expr);
11515 : 42 : break;
11516 : :
11517 : 551 : case GFC_ISYM_MIN:
11518 : 551 : if (expr->ts.type == BT_CHARACTER)
11519 : 144 : gfc_conv_intrinsic_minmax_char (se, expr, -1);
11520 : : else
11521 : 407 : gfc_conv_intrinsic_minmax (se, expr, LT_EXPR);
11522 : : break;
11523 : :
11524 : 7176 : case GFC_ISYM_MINLOC:
11525 : 7176 : gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
11526 : 7176 : break;
11527 : :
11528 : 1316 : case GFC_ISYM_MINVAL:
11529 : 1316 : gfc_conv_intrinsic_minmaxval (se, expr, LT_EXPR);
11530 : 1316 : break;
11531 : :
11532 : 1595 : case GFC_ISYM_NEAREST:
11533 : 1595 : gfc_conv_intrinsic_nearest (se, expr);
11534 : 1595 : break;
11535 : :
11536 : 68 : case GFC_ISYM_NORM2:
11537 : 68 : gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, true);
11538 : 68 : break;
11539 : :
11540 : 230 : case GFC_ISYM_NOT:
11541 : 230 : gfc_conv_intrinsic_not (se, expr);
11542 : 230 : break;
11543 : :
11544 : 12 : case GFC_ISYM_OR:
11545 : 12 : gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11546 : 12 : break;
11547 : :
11548 : 468 : case GFC_ISYM_OUT_OF_RANGE:
11549 : 468 : gfc_conv_intrinsic_out_of_range (se, expr);
11550 : 468 : break;
11551 : :
11552 : 36 : case GFC_ISYM_PARITY:
11553 : 36 : gfc_conv_intrinsic_arith (se, expr, NE_EXPR, false);
11554 : 36 : break;
11555 : :
11556 : 5052 : case GFC_ISYM_PRESENT:
11557 : 5052 : gfc_conv_intrinsic_present (se, expr);
11558 : 5052 : break;
11559 : :
11560 : 344 : case GFC_ISYM_PRODUCT:
11561 : 344 : gfc_conv_intrinsic_arith (se, expr, MULT_EXPR, false);
11562 : 344 : break;
11563 : :
11564 : 10662 : case GFC_ISYM_RANK:
11565 : 10662 : gfc_conv_intrinsic_rank (se, expr);
11566 : 10662 : break;
11567 : :
11568 : 48 : case GFC_ISYM_RRSPACING:
11569 : 48 : gfc_conv_intrinsic_rrspacing (se, expr);
11570 : 48 : break;
11571 : :
11572 : 262 : case GFC_ISYM_SET_EXPONENT:
11573 : 262 : gfc_conv_intrinsic_set_exponent (se, expr);
11574 : 262 : break;
11575 : :
11576 : 72 : case GFC_ISYM_SCALE:
11577 : 72 : gfc_conv_intrinsic_scale (se, expr);
11578 : 72 : break;
11579 : :
11580 : 4823 : case GFC_ISYM_SHAPE:
11581 : 4823 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_SHAPE);
11582 : 4823 : break;
11583 : :
11584 : 424 : case GFC_ISYM_SIGN:
11585 : 424 : gfc_conv_intrinsic_sign (se, expr);
11586 : 424 : break;
11587 : :
11588 : 14661 : case GFC_ISYM_SIZE:
11589 : 14661 : gfc_conv_intrinsic_size (se, expr);
11590 : 14661 : break;
11591 : :
11592 : 1304 : case GFC_ISYM_SIZEOF:
11593 : 1304 : case GFC_ISYM_C_SIZEOF:
11594 : 1304 : gfc_conv_intrinsic_sizeof (se, expr);
11595 : 1304 : break;
11596 : :
11597 : 802 : case GFC_ISYM_STORAGE_SIZE:
11598 : 802 : gfc_conv_intrinsic_storage_size (se, expr);
11599 : 802 : break;
11600 : :
11601 : 70 : case GFC_ISYM_SPACING:
11602 : 70 : gfc_conv_intrinsic_spacing (se, expr);
11603 : 70 : break;
11604 : :
11605 : 1907 : case GFC_ISYM_STRIDE:
11606 : 1907 : conv_intrinsic_stride (se, expr);
11607 : 1907 : break;
11608 : :
11609 : 1991 : case GFC_ISYM_SUM:
11610 : 1991 : gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, false);
11611 : 1991 : break;
11612 : :
11613 : 19 : case GFC_ISYM_TEAM_NUMBER:
11614 : 19 : conv_intrinsic_team_number (se, expr);
11615 : 19 : break;
11616 : :
11617 : 3611 : case GFC_ISYM_TRANSFER:
11618 : 3611 : if (se->ss && se->ss->info->useflags)
11619 : : /* Access the previously obtained result. */
11620 : 277 : gfc_conv_tmp_array_ref (se);
11621 : : else
11622 : 3334 : gfc_conv_intrinsic_transfer (se, expr);
11623 : : break;
11624 : :
11625 : 0 : case GFC_ISYM_TTYNAM:
11626 : 0 : gfc_conv_intrinsic_ttynam (se, expr);
11627 : 0 : break;
11628 : :
11629 : 5661 : case GFC_ISYM_UBOUND:
11630 : 5661 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_UBOUND);
11631 : 5661 : break;
11632 : :
11633 : 191 : case GFC_ISYM_UCOBOUND:
11634 : 191 : conv_intrinsic_cobound (se, expr);
11635 : 191 : break;
11636 : :
11637 : 18 : case GFC_ISYM_XOR:
11638 : 18 : gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11639 : 18 : break;
11640 : :
11641 : 8634 : case GFC_ISYM_LOC:
11642 : 8634 : gfc_conv_intrinsic_loc (se, expr);
11643 : 8634 : break;
11644 : :
11645 : 935 : case GFC_ISYM_THIS_IMAGE:
11646 : : /* For num_images() == 1, handle as LCOBOUND. */
11647 : 935 : if (expr->value.function.actual->expr
11648 : 379 : && flag_coarray == GFC_FCOARRAY_SINGLE)
11649 : 208 : conv_intrinsic_cobound (se, expr);
11650 : : else
11651 : 727 : trans_this_image (se, expr);
11652 : : break;
11653 : :
11654 : 152 : case GFC_ISYM_IMAGE_INDEX:
11655 : 152 : trans_image_index (se, expr);
11656 : 152 : break;
11657 : :
11658 : 16 : case GFC_ISYM_IMAGE_STATUS:
11659 : 16 : conv_intrinsic_image_status (se, expr);
11660 : 16 : break;
11661 : :
11662 : 429 : case GFC_ISYM_NUM_IMAGES:
11663 : 429 : trans_num_images (se, expr);
11664 : 429 : break;
11665 : :
11666 : 1372 : case GFC_ISYM_ACCESS:
11667 : 1372 : case GFC_ISYM_CHDIR:
11668 : 1372 : case GFC_ISYM_CHMOD:
11669 : 1372 : case GFC_ISYM_DTIME:
11670 : 1372 : case GFC_ISYM_ETIME:
11671 : 1372 : case GFC_ISYM_EXTENDS_TYPE_OF:
11672 : 1372 : case GFC_ISYM_FGET:
11673 : 1372 : case GFC_ISYM_FGETC:
11674 : 1372 : case GFC_ISYM_FNUM:
11675 : 1372 : case GFC_ISYM_FPUT:
11676 : 1372 : case GFC_ISYM_FPUTC:
11677 : 1372 : case GFC_ISYM_FSTAT:
11678 : 1372 : case GFC_ISYM_FTELL:
11679 : 1372 : case GFC_ISYM_GETCWD:
11680 : 1372 : case GFC_ISYM_GETGID:
11681 : 1372 : case GFC_ISYM_GETPID:
11682 : 1372 : case GFC_ISYM_GETUID:
11683 : 1372 : case GFC_ISYM_GET_TEAM:
11684 : 1372 : case GFC_ISYM_HOSTNM:
11685 : 1372 : case GFC_ISYM_IERRNO:
11686 : 1372 : case GFC_ISYM_IRAND:
11687 : 1372 : case GFC_ISYM_ISATTY:
11688 : 1372 : case GFC_ISYM_JN2:
11689 : 1372 : case GFC_ISYM_LINK:
11690 : 1372 : case GFC_ISYM_LSTAT:
11691 : 1372 : case GFC_ISYM_MATMUL:
11692 : 1372 : case GFC_ISYM_MCLOCK:
11693 : 1372 : case GFC_ISYM_MCLOCK8:
11694 : 1372 : case GFC_ISYM_RAND:
11695 : 1372 : case GFC_ISYM_REDUCE:
11696 : 1372 : case GFC_ISYM_RENAME:
11697 : 1372 : case GFC_ISYM_SECOND:
11698 : 1372 : case GFC_ISYM_SECNDS:
11699 : 1372 : case GFC_ISYM_SIGNAL:
11700 : 1372 : case GFC_ISYM_STAT:
11701 : 1372 : case GFC_ISYM_SYMLNK:
11702 : 1372 : case GFC_ISYM_SYSTEM:
11703 : 1372 : case GFC_ISYM_TIME:
11704 : 1372 : case GFC_ISYM_TIME8:
11705 : 1372 : case GFC_ISYM_UMASK:
11706 : 1372 : case GFC_ISYM_UNLINK:
11707 : 1372 : case GFC_ISYM_YN2:
11708 : 1372 : gfc_conv_intrinsic_funcall (se, expr);
11709 : 1372 : break;
11710 : :
11711 : 0 : case GFC_ISYM_EOSHIFT:
11712 : 0 : case GFC_ISYM_PACK:
11713 : 0 : case GFC_ISYM_RESHAPE:
11714 : : /* For those, expr->rank should always be >0 and thus the if above the
11715 : : switch should have matched. */
11716 : 0 : gcc_unreachable ();
11717 : 3851 : break;
11718 : :
11719 : 3851 : default:
11720 : 3851 : gfc_conv_intrinsic_lib_function (se, expr);
11721 : 3851 : break;
11722 : : }
11723 : : }
11724 : :
11725 : :
11726 : : static gfc_ss *
11727 : 1560 : walk_inline_intrinsic_transpose (gfc_ss *ss, gfc_expr *expr)
11728 : : {
11729 : 1560 : gfc_ss *arg_ss, *tmp_ss;
11730 : 1560 : gfc_actual_arglist *arg;
11731 : :
11732 : 1560 : arg = expr->value.function.actual;
11733 : :
11734 : 1560 : gcc_assert (arg->expr);
11735 : :
11736 : 1560 : arg_ss = gfc_walk_subexpr (gfc_ss_terminator, arg->expr);
11737 : 1560 : gcc_assert (arg_ss != gfc_ss_terminator);
11738 : :
11739 : : for (tmp_ss = arg_ss; ; tmp_ss = tmp_ss->next)
11740 : : {
11741 : 1665 : if (tmp_ss->info->type != GFC_SS_SCALAR
11742 : : && tmp_ss->info->type != GFC_SS_REFERENCE)
11743 : : {
11744 : 1628 : gcc_assert (tmp_ss->dimen == 2);
11745 : :
11746 : : /* We just invert dimensions. */
11747 : 1628 : std::swap (tmp_ss->dim[0], tmp_ss->dim[1]);
11748 : : }
11749 : :
11750 : : /* Stop when tmp_ss points to the last valid element of the chain... */
11751 : 1665 : if (tmp_ss->next == gfc_ss_terminator)
11752 : : break;
11753 : : }
11754 : :
11755 : : /* ... so that we can attach the rest of the chain to it. */
11756 : 1560 : tmp_ss->next = ss;
11757 : :
11758 : 1560 : return arg_ss;
11759 : : }
11760 : :
11761 : :
11762 : : /* Move the given dimension of the given gfc_ss list to a nested gfc_ss list.
11763 : : This has the side effect of reversing the nested list, so there is no
11764 : : need to call gfc_reverse_ss on it (the given list is assumed not to be
11765 : : reversed yet). */
11766 : :
11767 : : static gfc_ss *
11768 : 3371 : nest_loop_dimension (gfc_ss *ss, int dim)
11769 : : {
11770 : 3371 : int ss_dim, i;
11771 : 3371 : gfc_ss *new_ss, *prev_ss = gfc_ss_terminator;
11772 : 3371 : gfc_loopinfo *new_loop;
11773 : :
11774 : 3371 : gcc_assert (ss != gfc_ss_terminator);
11775 : :
11776 : 8118 : for (; ss != gfc_ss_terminator; ss = ss->next)
11777 : : {
11778 : 4747 : new_ss = gfc_get_ss ();
11779 : 4747 : new_ss->next = prev_ss;
11780 : 4747 : new_ss->parent = ss;
11781 : 4747 : new_ss->info = ss->info;
11782 : 4747 : new_ss->info->refcount++;
11783 : 4747 : if (ss->dimen != 0)
11784 : : {
11785 : 4684 : gcc_assert (ss->info->type != GFC_SS_SCALAR
11786 : : && ss->info->type != GFC_SS_REFERENCE);
11787 : :
11788 : 4684 : new_ss->dimen = 1;
11789 : 4684 : new_ss->dim[0] = ss->dim[dim];
11790 : :
11791 : 4684 : gcc_assert (dim < ss->dimen);
11792 : :
11793 : 4684 : ss_dim = --ss->dimen;
11794 : 10430 : for (i = dim; i < ss_dim; i++)
11795 : 5746 : ss->dim[i] = ss->dim[i + 1];
11796 : :
11797 : 4684 : ss->dim[ss_dim] = 0;
11798 : : }
11799 : 4747 : prev_ss = new_ss;
11800 : :
11801 : 4747 : if (ss->nested_ss)
11802 : : {
11803 : 81 : ss->nested_ss->parent = new_ss;
11804 : 81 : new_ss->nested_ss = ss->nested_ss;
11805 : : }
11806 : 4747 : ss->nested_ss = new_ss;
11807 : : }
11808 : :
11809 : 3371 : new_loop = gfc_get_loopinfo ();
11810 : 3371 : gfc_init_loopinfo (new_loop);
11811 : :
11812 : 3371 : gcc_assert (prev_ss != NULL);
11813 : 3371 : gcc_assert (prev_ss != gfc_ss_terminator);
11814 : 3371 : gfc_add_ss_to_loop (new_loop, prev_ss);
11815 : 3371 : return new_ss->parent;
11816 : : }
11817 : :
11818 : :
11819 : : /* Create the gfc_ss list for the SUM/PRODUCT arguments when the function
11820 : : is to be inlined. */
11821 : :
11822 : : static gfc_ss *
11823 : 575 : walk_inline_intrinsic_arith (gfc_ss *ss, gfc_expr *expr)
11824 : : {
11825 : 575 : gfc_ss *tmp_ss, *tail, *array_ss;
11826 : 575 : gfc_actual_arglist *arg1, *arg2, *arg3;
11827 : 575 : int sum_dim;
11828 : 575 : bool scalar_mask = false;
11829 : :
11830 : : /* The rank of the result will be determined later. */
11831 : 575 : arg1 = expr->value.function.actual;
11832 : 575 : arg2 = arg1->next;
11833 : 575 : arg3 = arg2->next;
11834 : 575 : gcc_assert (arg3 != NULL);
11835 : :
11836 : 575 : if (expr->rank == 0)
11837 : : return ss;
11838 : :
11839 : 575 : tmp_ss = gfc_ss_terminator;
11840 : :
11841 : 575 : if (arg3->expr)
11842 : : {
11843 : 118 : gfc_ss *mask_ss;
11844 : :
11845 : 118 : mask_ss = gfc_walk_subexpr (tmp_ss, arg3->expr);
11846 : 118 : if (mask_ss == tmp_ss)
11847 : 34 : scalar_mask = 1;
11848 : :
11849 : : tmp_ss = mask_ss;
11850 : : }
11851 : :
11852 : 575 : array_ss = gfc_walk_subexpr (tmp_ss, arg1->expr);
11853 : 575 : gcc_assert (array_ss != tmp_ss);
11854 : :
11855 : : /* Odd thing: If the mask is scalar, it is used by the frontend after
11856 : : the array (to make an if around the nested loop). Thus it shall
11857 : : be after array_ss once the gfc_ss list is reversed. */
11858 : 575 : if (scalar_mask)
11859 : 34 : tmp_ss = gfc_get_scalar_ss (array_ss, arg3->expr);
11860 : : else
11861 : : tmp_ss = array_ss;
11862 : :
11863 : : /* "Hide" the dimension on which we will sum in the first arg's scalarization
11864 : : chain. */
11865 : 575 : sum_dim = mpz_get_si (arg2->expr->value.integer) - 1;
11866 : 575 : tail = nest_loop_dimension (tmp_ss, sum_dim);
11867 : 575 : tail->next = ss;
11868 : :
11869 : 575 : return tmp_ss;
11870 : : }
11871 : :
11872 : :
11873 : : /* Create the gfc_ss list for the arguments to MINLOC or MAXLOC when the
11874 : : function is to be inlined. */
11875 : :
11876 : : static gfc_ss *
11877 : 6085 : walk_inline_intrinsic_minmaxloc (gfc_ss *ss, gfc_expr *expr ATTRIBUTE_UNUSED)
11878 : : {
11879 : 6085 : if (expr->rank == 0)
11880 : : return ss;
11881 : :
11882 : 6085 : gfc_actual_arglist *array_arg = expr->value.function.actual;
11883 : 6085 : gfc_actual_arglist *dim_arg = array_arg->next;
11884 : 6085 : gfc_actual_arglist *mask_arg = dim_arg->next;
11885 : 6085 : gfc_actual_arglist *kind_arg = mask_arg->next;
11886 : 6085 : gfc_actual_arglist *back_arg = kind_arg->next;
11887 : :
11888 : 6085 : gfc_expr *array = array_arg->expr;
11889 : 6085 : gfc_expr *dim = dim_arg->expr;
11890 : 6085 : gfc_expr *mask = mask_arg->expr;
11891 : 6085 : gfc_expr *back = back_arg->expr;
11892 : :
11893 : 6085 : if (dim == nullptr)
11894 : 3289 : return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
11895 : :
11896 : 2796 : gfc_ss *tmp_ss = gfc_ss_terminator;
11897 : :
11898 : 2796 : bool scalar_mask = false;
11899 : 2796 : if (mask)
11900 : : {
11901 : 1866 : gfc_ss *mask_ss = gfc_walk_subexpr (tmp_ss, mask);
11902 : 1866 : if (mask_ss == tmp_ss)
11903 : : scalar_mask = true;
11904 : 1174 : else if (maybe_absent_optional_variable (mask))
11905 : 20 : mask_ss->info->can_be_null_ref = true;
11906 : :
11907 : : tmp_ss = mask_ss;
11908 : : }
11909 : :
11910 : 2796 : gfc_ss *array_ss = gfc_walk_subexpr (tmp_ss, array);
11911 : 2796 : gcc_assert (array_ss != tmp_ss);
11912 : :
11913 : 2796 : tmp_ss = array_ss;
11914 : :
11915 : : /* Move the dimension on which we will sum to a separate nested scalarization
11916 : : chain, "hiding" that dimension from the outer scalarization. */
11917 : 2796 : int dim_val = mpz_get_si (dim->value.integer);
11918 : 2796 : gfc_ss *tail = nest_loop_dimension (tmp_ss, dim_val - 1);
11919 : :
11920 : 2796 : if (back && array->rank > 1)
11921 : : {
11922 : : /* If there are nested scalarization loops, include BACK in the
11923 : : scalarization chains to avoid evaluating it multiple times in a loop.
11924 : : Otherwise, prefer to handle it outside of scalarization. */
11925 : 2796 : gfc_ss *back_ss = gfc_get_scalar_ss (ss, back);
11926 : 2796 : back_ss->info->type = GFC_SS_REFERENCE;
11927 : 2796 : if (maybe_absent_optional_variable (back))
11928 : 16 : back_ss->info->can_be_null_ref = true;
11929 : :
11930 : 2796 : tail->next = back_ss;
11931 : 2796 : }
11932 : : else
11933 : 0 : tail->next = ss;
11934 : :
11935 : 2796 : if (scalar_mask)
11936 : : {
11937 : 692 : tmp_ss = gfc_get_scalar_ss (tmp_ss, mask);
11938 : : /* MASK can be a forwarded optional argument, so make the necessary setup
11939 : : to avoid the scalarizer generating any unguarded pointer dereference in
11940 : : that case. */
11941 : 692 : tmp_ss->info->type = GFC_SS_REFERENCE;
11942 : 692 : if (maybe_absent_optional_variable (mask))
11943 : 4 : tmp_ss->info->can_be_null_ref = true;
11944 : : }
11945 : :
11946 : : return tmp_ss;
11947 : : }
11948 : :
11949 : :
11950 : : static gfc_ss *
11951 : 8220 : walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr)
11952 : : {
11953 : :
11954 : 8220 : switch (expr->value.function.isym->id)
11955 : : {
11956 : 575 : case GFC_ISYM_PRODUCT:
11957 : 575 : case GFC_ISYM_SUM:
11958 : 575 : return walk_inline_intrinsic_arith (ss, expr);
11959 : :
11960 : 1560 : case GFC_ISYM_TRANSPOSE:
11961 : 1560 : return walk_inline_intrinsic_transpose (ss, expr);
11962 : :
11963 : 6085 : case GFC_ISYM_MAXLOC:
11964 : 6085 : case GFC_ISYM_MINLOC:
11965 : 6085 : return walk_inline_intrinsic_minmaxloc (ss, expr);
11966 : :
11967 : 0 : default:
11968 : 0 : gcc_unreachable ();
11969 : : }
11970 : : gcc_unreachable ();
11971 : : }
11972 : :
11973 : :
11974 : : /* This generates code to execute before entering the scalarization loop.
11975 : : Currently does nothing. */
11976 : :
11977 : : void
11978 : 11333 : gfc_add_intrinsic_ss_code (gfc_loopinfo * loop ATTRIBUTE_UNUSED, gfc_ss * ss)
11979 : : {
11980 : 11333 : switch (ss->info->expr->value.function.isym->id)
11981 : : {
11982 : 11333 : case GFC_ISYM_UBOUND:
11983 : 11333 : case GFC_ISYM_LBOUND:
11984 : 11333 : case GFC_ISYM_UCOBOUND:
11985 : 11333 : case GFC_ISYM_LCOBOUND:
11986 : 11333 : case GFC_ISYM_MAXLOC:
11987 : 11333 : case GFC_ISYM_MINLOC:
11988 : 11333 : case GFC_ISYM_THIS_IMAGE:
11989 : 11333 : case GFC_ISYM_SHAPE:
11990 : 11333 : break;
11991 : :
11992 : 0 : default:
11993 : 0 : gcc_unreachable ();
11994 : : }
11995 : 11333 : }
11996 : :
11997 : :
11998 : : /* The LBOUND, LCOBOUND, UBOUND, UCOBOUND, and SHAPE intrinsics with
11999 : : one parameter are expanded into code inside the scalarization loop. */
12000 : :
12001 : : static gfc_ss *
12002 : 9886 : gfc_walk_intrinsic_bound (gfc_ss * ss, gfc_expr * expr)
12003 : : {
12004 : 9886 : if (expr->value.function.actual->expr->ts.type == BT_CLASS)
12005 : 410 : gfc_add_class_array_ref (expr->value.function.actual->expr);
12006 : :
12007 : : /* The two argument version returns a scalar. */
12008 : 9886 : if (expr->value.function.isym->id != GFC_ISYM_SHAPE
12009 : 3439 : && expr->value.function.actual->next->expr)
12010 : : return ss;
12011 : :
12012 : 9886 : return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
12013 : : }
12014 : :
12015 : :
12016 : : /* Walk an intrinsic array libcall. */
12017 : :
12018 : : static gfc_ss *
12019 : 14563 : gfc_walk_intrinsic_libfunc (gfc_ss * ss, gfc_expr * expr)
12020 : : {
12021 : 14563 : gcc_assert (expr->rank > 0);
12022 : 14563 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
12023 : : }
12024 : :
12025 : :
12026 : : /* Return whether the function call expression EXPR will be expanded
12027 : : inline by gfc_conv_intrinsic_function. */
12028 : :
12029 : : bool
12030 : 294889 : gfc_inline_intrinsic_function_p (gfc_expr *expr)
12031 : : {
12032 : 294889 : gfc_actual_arglist *args, *dim_arg, *mask_arg;
12033 : 294889 : gfc_expr *maskexpr;
12034 : :
12035 : 294889 : gfc_intrinsic_sym *isym = expr->value.function.isym;
12036 : 294889 : if (!isym)
12037 : : return false;
12038 : :
12039 : 294847 : switch (isym->id)
12040 : : {
12041 : 5092 : case GFC_ISYM_PRODUCT:
12042 : 5092 : case GFC_ISYM_SUM:
12043 : : /* Disable inline expansion if code size matters. */
12044 : 5092 : if (optimize_size)
12045 : : return false;
12046 : :
12047 : 4239 : args = expr->value.function.actual;
12048 : 4239 : dim_arg = args->next;
12049 : :
12050 : : /* We need to be able to subset the SUM argument at compile-time. */
12051 : 4239 : if (dim_arg->expr && dim_arg->expr->expr_type != EXPR_CONSTANT)
12052 : : return false;
12053 : :
12054 : : /* FIXME: If MASK is optional for a more than two-dimensional
12055 : : argument, the scalarizer gets confused if the mask is
12056 : : absent. See PR 82995. For now, fall back to the library
12057 : : function. */
12058 : :
12059 : 3627 : mask_arg = dim_arg->next;
12060 : 3627 : maskexpr = mask_arg->expr;
12061 : :
12062 : 3627 : if (expr->rank > 0 && maskexpr && maskexpr->expr_type == EXPR_VARIABLE
12063 : 276 : && maskexpr->symtree->n.sym->attr.dummy
12064 : 48 : && maskexpr->symtree->n.sym->attr.optional)
12065 : : return false;
12066 : :
12067 : : return true;
12068 : :
12069 : : case GFC_ISYM_TRANSPOSE:
12070 : : return true;
12071 : :
12072 : 57188 : case GFC_ISYM_MINLOC:
12073 : 57188 : case GFC_ISYM_MAXLOC:
12074 : 57188 : {
12075 : 57188 : if ((isym->id == GFC_ISYM_MINLOC
12076 : 30521 : && (flag_inline_intrinsics
12077 : 30521 : & GFC_FLAG_INLINE_INTRINSIC_MINLOC) == 0)
12078 : 46611 : || (isym->id == GFC_ISYM_MAXLOC
12079 : 26667 : && (flag_inline_intrinsics
12080 : 26667 : & GFC_FLAG_INLINE_INTRINSIC_MAXLOC) == 0))
12081 : : return false;
12082 : :
12083 : 37638 : gfc_actual_arglist *array_arg = expr->value.function.actual;
12084 : 37638 : gfc_actual_arglist *dim_arg = array_arg->next;
12085 : :
12086 : 37638 : gfc_expr *array = array_arg->expr;
12087 : 37638 : gfc_expr *dim = dim_arg->expr;
12088 : :
12089 : 37638 : if (!(array->ts.type == BT_INTEGER
12090 : : || array->ts.type == BT_REAL))
12091 : : return false;
12092 : :
12093 : 34658 : if (array->rank == 1)
12094 : : return true;
12095 : :
12096 : 20711 : if (dim != nullptr
12097 : 13372 : && dim->expr_type != EXPR_CONSTANT)
12098 : : return false;
12099 : :
12100 : : return true;
12101 : : }
12102 : :
12103 : : default:
12104 : : return false;
12105 : : }
12106 : : }
12107 : :
12108 : :
12109 : : /* Returns nonzero if the specified intrinsic function call maps directly to
12110 : : an external library call. Should only be used for functions that return
12111 : : arrays. */
12112 : :
12113 : : int
12114 : 88361 : gfc_is_intrinsic_libcall (gfc_expr * expr)
12115 : : {
12116 : 88361 : gcc_assert (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym);
12117 : 88361 : gcc_assert (expr->rank > 0);
12118 : :
12119 : 88361 : if (gfc_inline_intrinsic_function_p (expr))
12120 : : return 0;
12121 : :
12122 : 73779 : switch (expr->value.function.isym->id)
12123 : : {
12124 : : case GFC_ISYM_ALL:
12125 : : case GFC_ISYM_ANY:
12126 : : case GFC_ISYM_COUNT:
12127 : : case GFC_ISYM_FINDLOC:
12128 : : case GFC_ISYM_JN2:
12129 : : case GFC_ISYM_IANY:
12130 : : case GFC_ISYM_IALL:
12131 : : case GFC_ISYM_IPARITY:
12132 : : case GFC_ISYM_MATMUL:
12133 : : case GFC_ISYM_MAXLOC:
12134 : : case GFC_ISYM_MAXVAL:
12135 : : case GFC_ISYM_MINLOC:
12136 : : case GFC_ISYM_MINVAL:
12137 : : case GFC_ISYM_NORM2:
12138 : : case GFC_ISYM_PARITY:
12139 : : case GFC_ISYM_PRODUCT:
12140 : : case GFC_ISYM_SUM:
12141 : : case GFC_ISYM_SPREAD:
12142 : : case GFC_ISYM_YN2:
12143 : : /* Ignore absent optional parameters. */
12144 : : return 1;
12145 : :
12146 : 16471 : case GFC_ISYM_CSHIFT:
12147 : 16471 : case GFC_ISYM_EOSHIFT:
12148 : 16471 : case GFC_ISYM_GET_TEAM:
12149 : 16471 : case GFC_ISYM_FAILED_IMAGES:
12150 : 16471 : case GFC_ISYM_STOPPED_IMAGES:
12151 : 16471 : case GFC_ISYM_PACK:
12152 : 16471 : case GFC_ISYM_REDUCE:
12153 : 16471 : case GFC_ISYM_RESHAPE:
12154 : 16471 : case GFC_ISYM_UNPACK:
12155 : : /* Pass absent optional parameters. */
12156 : 16471 : return 2;
12157 : :
12158 : : default:
12159 : : return 0;
12160 : : }
12161 : : }
12162 : :
12163 : : /* Walk an intrinsic function. */
12164 : : gfc_ss *
12165 : 55977 : gfc_walk_intrinsic_function (gfc_ss * ss, gfc_expr * expr,
12166 : : gfc_intrinsic_sym * isym)
12167 : : {
12168 : 55977 : gcc_assert (isym);
12169 : :
12170 : 55977 : if (isym->elemental)
12171 : 18932 : return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
12172 : : expr->value.function.isym,
12173 : 18932 : GFC_SS_SCALAR);
12174 : :
12175 : 37045 : if (expr->rank == 0 && expr->corank == 0)
12176 : : return ss;
12177 : :
12178 : 32669 : if (gfc_inline_intrinsic_function_p (expr))
12179 : 8220 : return walk_inline_intrinsic_function (ss, expr);
12180 : :
12181 : 24449 : if (expr->rank != 0 && gfc_is_intrinsic_libcall (expr))
12182 : 13625 : return gfc_walk_intrinsic_libfunc (ss, expr);
12183 : :
12184 : : /* Special cases. */
12185 : 10824 : switch (isym->id)
12186 : : {
12187 : 9886 : case GFC_ISYM_LBOUND:
12188 : 9886 : case GFC_ISYM_LCOBOUND:
12189 : 9886 : case GFC_ISYM_UBOUND:
12190 : 9886 : case GFC_ISYM_UCOBOUND:
12191 : 9886 : case GFC_ISYM_THIS_IMAGE:
12192 : 9886 : case GFC_ISYM_SHAPE:
12193 : 9886 : return gfc_walk_intrinsic_bound (ss, expr);
12194 : :
12195 : 938 : case GFC_ISYM_TRANSFER:
12196 : 938 : case GFC_ISYM_CAF_GET:
12197 : 938 : return gfc_walk_intrinsic_libfunc (ss, expr);
12198 : :
12199 : 0 : default:
12200 : : /* This probably meant someone forgot to add an intrinsic to the above
12201 : : list(s) when they implemented it, or something's gone horribly
12202 : : wrong. */
12203 : 0 : gcc_unreachable ();
12204 : : }
12205 : : }
12206 : :
12207 : : static tree
12208 : 63 : conv_co_collective (gfc_code *code)
12209 : : {
12210 : 63 : gfc_se argse;
12211 : 63 : stmtblock_t block, post_block;
12212 : 63 : tree fndecl, array = NULL_TREE, strlen, image_index, stat, errmsg, errmsg_len;
12213 : 63 : gfc_expr *image_idx_expr, *stat_expr, *errmsg_expr, *opr_expr;
12214 : :
12215 : 63 : gfc_start_block (&block);
12216 : 63 : gfc_init_block (&post_block);
12217 : :
12218 : 63 : if (code->resolved_isym->id == GFC_ISYM_CO_REDUCE)
12219 : : {
12220 : 7 : opr_expr = code->ext.actual->next->expr;
12221 : 7 : image_idx_expr = code->ext.actual->next->next->expr;
12222 : 7 : stat_expr = code->ext.actual->next->next->next->expr;
12223 : 7 : errmsg_expr = code->ext.actual->next->next->next->next->expr;
12224 : : }
12225 : : else
12226 : : {
12227 : 56 : opr_expr = NULL;
12228 : 56 : image_idx_expr = code->ext.actual->next->expr;
12229 : 56 : stat_expr = code->ext.actual->next->next->expr;
12230 : 56 : errmsg_expr = code->ext.actual->next->next->next->expr;
12231 : : }
12232 : :
12233 : : /* stat. */
12234 : 63 : if (stat_expr)
12235 : : {
12236 : 49 : gfc_init_se (&argse, NULL);
12237 : 49 : gfc_conv_expr (&argse, stat_expr);
12238 : 49 : gfc_add_block_to_block (&block, &argse.pre);
12239 : 49 : gfc_add_block_to_block (&post_block, &argse.post);
12240 : 49 : stat = argse.expr;
12241 : 49 : if (flag_coarray != GFC_FCOARRAY_SINGLE)
12242 : 22 : stat = gfc_build_addr_expr (NULL_TREE, stat);
12243 : : }
12244 : 14 : else if (flag_coarray == GFC_FCOARRAY_SINGLE)
12245 : : stat = NULL_TREE;
12246 : : else
12247 : 8 : stat = null_pointer_node;
12248 : :
12249 : : /* Early exit for GFC_FCOARRAY_SINGLE. */
12250 : 63 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
12251 : : {
12252 : 33 : if (stat != NULL_TREE)
12253 : : {
12254 : : /* For optional stats, check the pointer is valid before zero'ing. */
12255 : 27 : if (gfc_expr_attr (stat_expr).optional)
12256 : : {
12257 : 12 : tree tmp;
12258 : 12 : stmtblock_t ass_block;
12259 : 12 : gfc_start_block (&ass_block);
12260 : 12 : gfc_add_modify (&ass_block, stat,
12261 : 12 : fold_convert (TREE_TYPE (stat),
12262 : : integer_zero_node));
12263 : 12 : tmp = fold_build2 (NE_EXPR, logical_type_node,
12264 : : gfc_build_addr_expr (NULL_TREE, stat),
12265 : : null_pointer_node);
12266 : 12 : tmp = fold_build3 (COND_EXPR, void_type_node, tmp,
12267 : : gfc_finish_block (&ass_block),
12268 : : build_empty_stmt (input_location));
12269 : 12 : gfc_add_expr_to_block (&block, tmp);
12270 : : }
12271 : : else
12272 : 15 : gfc_add_modify (&block, stat,
12273 : 15 : fold_convert (TREE_TYPE (stat), integer_zero_node));
12274 : : }
12275 : 33 : return gfc_finish_block (&block);
12276 : : }
12277 : :
12278 : 3 : gfc_symbol *derived = code->ext.actual->expr->ts.type == BT_DERIVED
12279 : 30 : ? code->ext.actual->expr->ts.u.derived : NULL;
12280 : :
12281 : : /* Handle the array. */
12282 : 30 : gfc_init_se (&argse, NULL);
12283 : 30 : if (!derived || !derived->attr.alloc_comp
12284 : 1 : || code->resolved_isym->id != GFC_ISYM_CO_BROADCAST)
12285 : : {
12286 : 29 : if (code->ext.actual->expr->rank == 0)
12287 : : {
12288 : 16 : symbol_attribute attr;
12289 : 16 : gfc_clear_attr (&attr);
12290 : 16 : gfc_init_se (&argse, NULL);
12291 : 16 : gfc_conv_expr (&argse, code->ext.actual->expr);
12292 : 16 : gfc_add_block_to_block (&block, &argse.pre);
12293 : 16 : gfc_add_block_to_block (&post_block, &argse.post);
12294 : 16 : array = gfc_conv_scalar_to_descriptor (&argse, argse.expr, attr);
12295 : 16 : array = gfc_build_addr_expr (NULL_TREE, array);
12296 : : }
12297 : : else
12298 : : {
12299 : 13 : argse.want_pointer = 1;
12300 : 13 : gfc_conv_expr_descriptor (&argse, code->ext.actual->expr);
12301 : 13 : array = argse.expr;
12302 : : }
12303 : : }
12304 : :
12305 : 30 : gfc_add_block_to_block (&block, &argse.pre);
12306 : 30 : gfc_add_block_to_block (&post_block, &argse.post);
12307 : :
12308 : 30 : if (code->ext.actual->expr->ts.type == BT_CHARACTER)
12309 : 6 : strlen = argse.string_length;
12310 : : else
12311 : 24 : strlen = integer_zero_node;
12312 : :
12313 : : /* image_index. */
12314 : 30 : if (image_idx_expr)
12315 : : {
12316 : 22 : gfc_init_se (&argse, NULL);
12317 : 22 : gfc_conv_expr (&argse, image_idx_expr);
12318 : 22 : gfc_add_block_to_block (&block, &argse.pre);
12319 : 22 : gfc_add_block_to_block (&post_block, &argse.post);
12320 : 22 : image_index = fold_convert (integer_type_node, argse.expr);
12321 : : }
12322 : : else
12323 : 8 : image_index = integer_zero_node;
12324 : :
12325 : : /* errmsg. */
12326 : 30 : if (errmsg_expr)
12327 : : {
12328 : 17 : gfc_init_se (&argse, NULL);
12329 : 17 : gfc_conv_expr (&argse, errmsg_expr);
12330 : 17 : gfc_add_block_to_block (&block, &argse.pre);
12331 : 17 : gfc_add_block_to_block (&post_block, &argse.post);
12332 : 17 : errmsg = argse.expr;
12333 : 17 : errmsg_len = fold_convert (size_type_node, argse.string_length);
12334 : : }
12335 : : else
12336 : : {
12337 : 13 : errmsg = null_pointer_node;
12338 : 13 : errmsg_len = build_zero_cst (size_type_node);
12339 : : }
12340 : :
12341 : : /* Generate the function call. */
12342 : 30 : switch (code->resolved_isym->id)
12343 : : {
12344 : 12 : case GFC_ISYM_CO_BROADCAST:
12345 : 12 : fndecl = gfor_fndecl_co_broadcast;
12346 : 12 : break;
12347 : 5 : case GFC_ISYM_CO_MAX:
12348 : 5 : fndecl = gfor_fndecl_co_max;
12349 : 5 : break;
12350 : 4 : case GFC_ISYM_CO_MIN:
12351 : 4 : fndecl = gfor_fndecl_co_min;
12352 : 4 : break;
12353 : 5 : case GFC_ISYM_CO_REDUCE:
12354 : 5 : fndecl = gfor_fndecl_co_reduce;
12355 : 5 : break;
12356 : 4 : case GFC_ISYM_CO_SUM:
12357 : 4 : fndecl = gfor_fndecl_co_sum;
12358 : 4 : break;
12359 : 0 : default:
12360 : 0 : gcc_unreachable ();
12361 : : }
12362 : :
12363 : 30 : if (derived && derived->attr.alloc_comp
12364 : 1 : && code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12365 : : /* The derived type has the attribute 'alloc_comp'. */
12366 : : {
12367 : 2 : tree tmp = gfc_bcast_alloc_comp (derived, code->ext.actual->expr,
12368 : 1 : code->ext.actual->expr->rank,
12369 : : image_index, stat, errmsg, errmsg_len);
12370 : 1 : gfc_add_expr_to_block (&block, tmp);
12371 : 1 : }
12372 : : else
12373 : : {
12374 : 29 : if (code->resolved_isym->id == GFC_ISYM_CO_SUM
12375 : 25 : || code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12376 : 15 : fndecl = build_call_expr_loc (input_location, fndecl, 5, array,
12377 : : image_index, stat, errmsg, errmsg_len);
12378 : 14 : else if (code->resolved_isym->id != GFC_ISYM_CO_REDUCE)
12379 : 9 : fndecl = build_call_expr_loc (input_location, fndecl, 6, array,
12380 : : image_index, stat, errmsg,
12381 : : strlen, errmsg_len);
12382 : : else
12383 : : {
12384 : 5 : tree opr, opr_flags;
12385 : :
12386 : : // FIXME: Handle TS29113's bind(C) strings with descriptor.
12387 : 5 : int opr_flag_int;
12388 : 5 : if (gfc_is_proc_ptr_comp (opr_expr))
12389 : : {
12390 : 0 : gfc_symbol *sym = gfc_get_proc_ptr_comp (opr_expr)->ts.interface;
12391 : 0 : opr_flag_int = sym->attr.dimension
12392 : 0 : || (sym->ts.type == BT_CHARACTER
12393 : 0 : && !sym->attr.is_bind_c)
12394 : 0 : ? GFC_CAF_BYREF : 0;
12395 : 0 : opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12396 : 0 : && !sym->attr.is_bind_c
12397 : 0 : ? GFC_CAF_HIDDENLEN : 0;
12398 : 0 : opr_flag_int |= sym->formal->sym->attr.value
12399 : 0 : ? GFC_CAF_ARG_VALUE : 0;
12400 : : }
12401 : : else
12402 : : {
12403 : 5 : opr_flag_int = gfc_return_by_reference (opr_expr->symtree->n.sym)
12404 : 5 : ? GFC_CAF_BYREF : 0;
12405 : 10 : opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12406 : 0 : && !opr_expr->symtree->n.sym->attr.is_bind_c
12407 : 5 : ? GFC_CAF_HIDDENLEN : 0;
12408 : 5 : opr_flag_int |= opr_expr->symtree->n.sym->formal->sym->attr.value
12409 : 5 : ? GFC_CAF_ARG_VALUE : 0;
12410 : : }
12411 : 5 : opr_flags = build_int_cst (integer_type_node, opr_flag_int);
12412 : 5 : gfc_conv_expr (&argse, opr_expr);
12413 : 5 : opr = argse.expr;
12414 : 5 : fndecl = build_call_expr_loc (input_location, fndecl, 8, array, opr,
12415 : : opr_flags, image_index, stat, errmsg,
12416 : : strlen, errmsg_len);
12417 : : }
12418 : : }
12419 : :
12420 : 30 : gfc_add_expr_to_block (&block, fndecl);
12421 : 30 : gfc_add_block_to_block (&block, &post_block);
12422 : :
12423 : 30 : return gfc_finish_block (&block);
12424 : : }
12425 : :
12426 : :
12427 : : static tree
12428 : 68 : conv_intrinsic_atomic_op (gfc_code *code)
12429 : : {
12430 : 68 : gfc_se argse;
12431 : 68 : tree tmp, atom, value, old = NULL_TREE, stat = NULL_TREE;
12432 : 68 : stmtblock_t block, post_block;
12433 : 68 : gfc_expr *atom_expr = code->ext.actual->expr;
12434 : 68 : gfc_expr *stat_expr;
12435 : 68 : built_in_function fn;
12436 : :
12437 : 68 : if (atom_expr->expr_type == EXPR_FUNCTION
12438 : 0 : && atom_expr->value.function.isym
12439 : 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12440 : 0 : atom_expr = atom_expr->value.function.actual->expr;
12441 : :
12442 : 68 : gfc_start_block (&block);
12443 : 68 : gfc_init_block (&post_block);
12444 : :
12445 : 68 : gfc_init_se (&argse, NULL);
12446 : 68 : argse.want_pointer = 1;
12447 : 68 : gfc_conv_expr (&argse, atom_expr);
12448 : 68 : gfc_add_block_to_block (&block, &argse.pre);
12449 : 68 : gfc_add_block_to_block (&post_block, &argse.post);
12450 : 68 : atom = argse.expr;
12451 : :
12452 : 68 : gfc_init_se (&argse, NULL);
12453 : 68 : if (flag_coarray == GFC_FCOARRAY_LIB
12454 : 29 : && code->ext.actual->next->expr->ts.kind == atom_expr->ts.kind)
12455 : 28 : argse.want_pointer = 1;
12456 : 68 : gfc_conv_expr (&argse, code->ext.actual->next->expr);
12457 : 68 : gfc_add_block_to_block (&block, &argse.pre);
12458 : 68 : gfc_add_block_to_block (&post_block, &argse.post);
12459 : 68 : value = argse.expr;
12460 : :
12461 : 68 : switch (code->resolved_isym->id)
12462 : : {
12463 : 42 : case GFC_ISYM_ATOMIC_ADD:
12464 : 42 : case GFC_ISYM_ATOMIC_AND:
12465 : 42 : case GFC_ISYM_ATOMIC_DEF:
12466 : 42 : case GFC_ISYM_ATOMIC_OR:
12467 : 42 : case GFC_ISYM_ATOMIC_XOR:
12468 : 42 : stat_expr = code->ext.actual->next->next->expr;
12469 : 42 : if (flag_coarray == GFC_FCOARRAY_LIB)
12470 : 18 : old = null_pointer_node;
12471 : : break;
12472 : 26 : default:
12473 : 26 : gfc_init_se (&argse, NULL);
12474 : 26 : if (flag_coarray == GFC_FCOARRAY_LIB)
12475 : 11 : argse.want_pointer = 1;
12476 : 26 : gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
12477 : 26 : gfc_add_block_to_block (&block, &argse.pre);
12478 : 26 : gfc_add_block_to_block (&post_block, &argse.post);
12479 : 26 : old = argse.expr;
12480 : 26 : stat_expr = code->ext.actual->next->next->next->expr;
12481 : : }
12482 : :
12483 : : /* STAT= */
12484 : 68 : if (stat_expr != NULL)
12485 : : {
12486 : 58 : gcc_assert (stat_expr->expr_type == EXPR_VARIABLE);
12487 : 58 : gfc_init_se (&argse, NULL);
12488 : 58 : if (flag_coarray == GFC_FCOARRAY_LIB)
12489 : 24 : argse.want_pointer = 1;
12490 : 58 : gfc_conv_expr_val (&argse, stat_expr);
12491 : 58 : gfc_add_block_to_block (&block, &argse.pre);
12492 : 58 : gfc_add_block_to_block (&post_block, &argse.post);
12493 : 58 : stat = argse.expr;
12494 : : }
12495 : 10 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12496 : 5 : stat = null_pointer_node;
12497 : :
12498 : 68 : if (flag_coarray == GFC_FCOARRAY_LIB)
12499 : : {
12500 : 29 : tree image_index, caf_decl, offset, token;
12501 : 29 : int op;
12502 : :
12503 : 29 : switch (code->resolved_isym->id)
12504 : : {
12505 : : case GFC_ISYM_ATOMIC_ADD:
12506 : : case GFC_ISYM_ATOMIC_FETCH_ADD:
12507 : : op = (int) GFC_CAF_ATOMIC_ADD;
12508 : : break;
12509 : 6 : case GFC_ISYM_ATOMIC_AND:
12510 : 6 : case GFC_ISYM_ATOMIC_FETCH_AND:
12511 : 6 : op = (int) GFC_CAF_ATOMIC_AND;
12512 : 6 : break;
12513 : 6 : case GFC_ISYM_ATOMIC_OR:
12514 : 6 : case GFC_ISYM_ATOMIC_FETCH_OR:
12515 : 6 : op = (int) GFC_CAF_ATOMIC_OR;
12516 : 6 : break;
12517 : 6 : case GFC_ISYM_ATOMIC_XOR:
12518 : 6 : case GFC_ISYM_ATOMIC_FETCH_XOR:
12519 : 6 : op = (int) GFC_CAF_ATOMIC_XOR;
12520 : 6 : break;
12521 : 6 : case GFC_ISYM_ATOMIC_DEF:
12522 : 6 : op = 0; /* Unused. */
12523 : 6 : break;
12524 : 0 : default:
12525 : 0 : gcc_unreachable ();
12526 : : }
12527 : :
12528 : 29 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12529 : 29 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12530 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12531 : :
12532 : 29 : if (gfc_is_coindexed (atom_expr))
12533 : 25 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12534 : : else
12535 : 4 : image_index = integer_zero_node;
12536 : :
12537 : 29 : if (!POINTER_TYPE_P (TREE_TYPE (value)))
12538 : : {
12539 : 28 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
12540 : 28 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), value));
12541 : 28 : value = gfc_build_addr_expr (NULL_TREE, tmp);
12542 : : }
12543 : :
12544 : 29 : gfc_init_se (&argse, NULL);
12545 : 29 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12546 : : atom_expr);
12547 : :
12548 : 29 : gfc_add_block_to_block (&block, &argse.pre);
12549 : 29 : if (code->resolved_isym->id == GFC_ISYM_ATOMIC_DEF)
12550 : 6 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_def, 7,
12551 : : token, offset, image_index, value, stat,
12552 : : build_int_cst (integer_type_node,
12553 : 6 : (int) atom_expr->ts.type),
12554 : : build_int_cst (integer_type_node,
12555 : 6 : (int) atom_expr->ts.kind));
12556 : : else
12557 : 23 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_op, 9,
12558 : 23 : build_int_cst (integer_type_node, op),
12559 : : token, offset, image_index, value, old, stat,
12560 : : build_int_cst (integer_type_node,
12561 : 23 : (int) atom_expr->ts.type),
12562 : : build_int_cst (integer_type_node,
12563 : 23 : (int) atom_expr->ts.kind));
12564 : :
12565 : 29 : gfc_add_expr_to_block (&block, tmp);
12566 : 29 : gfc_add_block_to_block (&block, &argse.post);
12567 : 29 : gfc_add_block_to_block (&block, &post_block);
12568 : 29 : return gfc_finish_block (&block);
12569 : : }
12570 : :
12571 : :
12572 : 39 : switch (code->resolved_isym->id)
12573 : : {
12574 : : case GFC_ISYM_ATOMIC_ADD:
12575 : : case GFC_ISYM_ATOMIC_FETCH_ADD:
12576 : : fn = BUILT_IN_ATOMIC_FETCH_ADD_N;
12577 : : break;
12578 : 8 : case GFC_ISYM_ATOMIC_AND:
12579 : 8 : case GFC_ISYM_ATOMIC_FETCH_AND:
12580 : 8 : fn = BUILT_IN_ATOMIC_FETCH_AND_N;
12581 : 8 : break;
12582 : 9 : case GFC_ISYM_ATOMIC_DEF:
12583 : 9 : fn = BUILT_IN_ATOMIC_STORE_N;
12584 : 9 : break;
12585 : 8 : case GFC_ISYM_ATOMIC_OR:
12586 : 8 : case GFC_ISYM_ATOMIC_FETCH_OR:
12587 : 8 : fn = BUILT_IN_ATOMIC_FETCH_OR_N;
12588 : 8 : break;
12589 : 8 : case GFC_ISYM_ATOMIC_XOR:
12590 : 8 : case GFC_ISYM_ATOMIC_FETCH_XOR:
12591 : 8 : fn = BUILT_IN_ATOMIC_FETCH_XOR_N;
12592 : 8 : break;
12593 : 0 : default:
12594 : 0 : gcc_unreachable ();
12595 : : }
12596 : :
12597 : 39 : tmp = TREE_TYPE (TREE_TYPE (atom));
12598 : 78 : fn = (built_in_function) ((int) fn
12599 : 39 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12600 : 39 : + 1);
12601 : 39 : tree itype = TREE_TYPE (TREE_TYPE (atom));
12602 : 39 : tmp = builtin_decl_explicit (fn);
12603 : :
12604 : 39 : switch (code->resolved_isym->id)
12605 : : {
12606 : 24 : case GFC_ISYM_ATOMIC_ADD:
12607 : 24 : case GFC_ISYM_ATOMIC_AND:
12608 : 24 : case GFC_ISYM_ATOMIC_DEF:
12609 : 24 : case GFC_ISYM_ATOMIC_OR:
12610 : 24 : case GFC_ISYM_ATOMIC_XOR:
12611 : 24 : tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12612 : : fold_convert (itype, value),
12613 : : build_int_cst (NULL, MEMMODEL_RELAXED));
12614 : 24 : gfc_add_expr_to_block (&block, tmp);
12615 : 24 : break;
12616 : 15 : default:
12617 : 15 : tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12618 : : fold_convert (itype, value),
12619 : : build_int_cst (NULL, MEMMODEL_RELAXED));
12620 : 15 : gfc_add_modify (&block, old, fold_convert (TREE_TYPE (old), tmp));
12621 : 15 : break;
12622 : : }
12623 : :
12624 : 39 : if (stat != NULL_TREE)
12625 : 34 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12626 : 39 : gfc_add_block_to_block (&block, &post_block);
12627 : 39 : return gfc_finish_block (&block);
12628 : : }
12629 : :
12630 : :
12631 : : static tree
12632 : 119 : conv_intrinsic_atomic_ref (gfc_code *code)
12633 : : {
12634 : 119 : gfc_se argse;
12635 : 119 : tree tmp, atom, value, stat = NULL_TREE;
12636 : 119 : stmtblock_t block, post_block;
12637 : 119 : built_in_function fn;
12638 : 119 : gfc_expr *atom_expr = code->ext.actual->next->expr;
12639 : :
12640 : 119 : if (atom_expr->expr_type == EXPR_FUNCTION
12641 : 0 : && atom_expr->value.function.isym
12642 : 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12643 : 0 : atom_expr = atom_expr->value.function.actual->expr;
12644 : :
12645 : 119 : gfc_start_block (&block);
12646 : 119 : gfc_init_block (&post_block);
12647 : 119 : gfc_init_se (&argse, NULL);
12648 : 119 : argse.want_pointer = 1;
12649 : 119 : gfc_conv_expr (&argse, atom_expr);
12650 : 119 : gfc_add_block_to_block (&block, &argse.pre);
12651 : 119 : gfc_add_block_to_block (&post_block, &argse.post);
12652 : 119 : atom = argse.expr;
12653 : :
12654 : 119 : gfc_init_se (&argse, NULL);
12655 : 119 : if (flag_coarray == GFC_FCOARRAY_LIB
12656 : 58 : && code->ext.actual->expr->ts.kind == atom_expr->ts.kind)
12657 : 55 : argse.want_pointer = 1;
12658 : 119 : gfc_conv_expr (&argse, code->ext.actual->expr);
12659 : 119 : gfc_add_block_to_block (&block, &argse.pre);
12660 : 119 : gfc_add_block_to_block (&post_block, &argse.post);
12661 : 119 : value = argse.expr;
12662 : :
12663 : : /* STAT= */
12664 : 119 : if (code->ext.actual->next->next->expr != NULL)
12665 : : {
12666 : 110 : gcc_assert (code->ext.actual->next->next->expr->expr_type
12667 : : == EXPR_VARIABLE);
12668 : 110 : gfc_init_se (&argse, NULL);
12669 : 110 : if (flag_coarray == GFC_FCOARRAY_LIB)
12670 : 54 : argse.want_pointer = 1;
12671 : 110 : gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
12672 : 110 : gfc_add_block_to_block (&block, &argse.pre);
12673 : 110 : gfc_add_block_to_block (&post_block, &argse.post);
12674 : 110 : stat = argse.expr;
12675 : : }
12676 : 9 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12677 : 4 : stat = null_pointer_node;
12678 : :
12679 : 119 : if (flag_coarray == GFC_FCOARRAY_LIB)
12680 : : {
12681 : 58 : tree image_index, caf_decl, offset, token;
12682 : 58 : tree orig_value = NULL_TREE, vardecl = NULL_TREE;
12683 : :
12684 : 58 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12685 : 58 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12686 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12687 : :
12688 : 58 : if (gfc_is_coindexed (atom_expr))
12689 : 52 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12690 : : else
12691 : 6 : image_index = integer_zero_node;
12692 : :
12693 : 58 : gfc_init_se (&argse, NULL);
12694 : 58 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12695 : : atom_expr);
12696 : 58 : gfc_add_block_to_block (&block, &argse.pre);
12697 : :
12698 : : /* Different type, need type conversion. */
12699 : 58 : if (!POINTER_TYPE_P (TREE_TYPE (value)))
12700 : : {
12701 : 3 : vardecl = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
12702 : 3 : orig_value = value;
12703 : 3 : value = gfc_build_addr_expr (NULL_TREE, vardecl);
12704 : : }
12705 : :
12706 : 58 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_ref, 7,
12707 : : token, offset, image_index, value, stat,
12708 : : build_int_cst (integer_type_node,
12709 : 58 : (int) atom_expr->ts.type),
12710 : : build_int_cst (integer_type_node,
12711 : 58 : (int) atom_expr->ts.kind));
12712 : 58 : gfc_add_expr_to_block (&block, tmp);
12713 : 58 : if (vardecl != NULL_TREE)
12714 : 3 : gfc_add_modify (&block, orig_value,
12715 : 3 : fold_convert (TREE_TYPE (orig_value), vardecl));
12716 : 58 : gfc_add_block_to_block (&block, &argse.post);
12717 : 58 : gfc_add_block_to_block (&block, &post_block);
12718 : 58 : return gfc_finish_block (&block);
12719 : : }
12720 : :
12721 : 61 : tmp = TREE_TYPE (TREE_TYPE (atom));
12722 : 122 : fn = (built_in_function) ((int) BUILT_IN_ATOMIC_LOAD_N
12723 : 61 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12724 : 61 : + 1);
12725 : 61 : tmp = builtin_decl_explicit (fn);
12726 : 61 : tmp = build_call_expr_loc (input_location, tmp, 2, atom,
12727 : : build_int_cst (integer_type_node,
12728 : : MEMMODEL_RELAXED));
12729 : 61 : gfc_add_modify (&block, value, fold_convert (TREE_TYPE (value), tmp));
12730 : :
12731 : 61 : if (stat != NULL_TREE)
12732 : 56 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12733 : 61 : gfc_add_block_to_block (&block, &post_block);
12734 : 61 : return gfc_finish_block (&block);
12735 : : }
12736 : :
12737 : :
12738 : : static tree
12739 : 10 : conv_intrinsic_atomic_cas (gfc_code *code)
12740 : : {
12741 : 10 : gfc_se argse;
12742 : 10 : tree tmp, atom, old, new_val, comp, stat = NULL_TREE;
12743 : 10 : stmtblock_t block, post_block;
12744 : 10 : built_in_function fn;
12745 : 10 : gfc_expr *atom_expr = code->ext.actual->expr;
12746 : :
12747 : 10 : if (atom_expr->expr_type == EXPR_FUNCTION
12748 : 0 : && atom_expr->value.function.isym
12749 : 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12750 : 0 : atom_expr = atom_expr->value.function.actual->expr;
12751 : :
12752 : 10 : gfc_init_block (&block);
12753 : 10 : gfc_init_block (&post_block);
12754 : 10 : gfc_init_se (&argse, NULL);
12755 : 10 : argse.want_pointer = 1;
12756 : 10 : gfc_conv_expr (&argse, atom_expr);
12757 : 10 : atom = argse.expr;
12758 : :
12759 : 10 : gfc_init_se (&argse, NULL);
12760 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12761 : 4 : argse.want_pointer = 1;
12762 : 10 : gfc_conv_expr (&argse, code->ext.actual->next->expr);
12763 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12764 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12765 : 10 : old = argse.expr;
12766 : :
12767 : 10 : gfc_init_se (&argse, NULL);
12768 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12769 : 4 : argse.want_pointer = 1;
12770 : 10 : gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
12771 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12772 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12773 : 10 : comp = argse.expr;
12774 : :
12775 : 10 : gfc_init_se (&argse, NULL);
12776 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB
12777 : 4 : && code->ext.actual->next->next->next->expr->ts.kind
12778 : 4 : == atom_expr->ts.kind)
12779 : 4 : argse.want_pointer = 1;
12780 : 10 : gfc_conv_expr (&argse, code->ext.actual->next->next->next->expr);
12781 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12782 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12783 : 10 : new_val = argse.expr;
12784 : :
12785 : : /* STAT= */
12786 : 10 : if (code->ext.actual->next->next->next->next->expr != NULL)
12787 : : {
12788 : 10 : gcc_assert (code->ext.actual->next->next->next->next->expr->expr_type
12789 : : == EXPR_VARIABLE);
12790 : 10 : gfc_init_se (&argse, NULL);
12791 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12792 : 4 : argse.want_pointer = 1;
12793 : 10 : gfc_conv_expr_val (&argse,
12794 : 10 : code->ext.actual->next->next->next->next->expr);
12795 : 10 : gfc_add_block_to_block (&block, &argse.pre);
12796 : 10 : gfc_add_block_to_block (&post_block, &argse.post);
12797 : 10 : stat = argse.expr;
12798 : : }
12799 : 0 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12800 : 0 : stat = null_pointer_node;
12801 : :
12802 : 10 : if (flag_coarray == GFC_FCOARRAY_LIB)
12803 : : {
12804 : 4 : tree image_index, caf_decl, offset, token;
12805 : :
12806 : 4 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12807 : 4 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12808 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12809 : :
12810 : 4 : if (gfc_is_coindexed (atom_expr))
12811 : 4 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12812 : : else
12813 : 0 : image_index = integer_zero_node;
12814 : :
12815 : 4 : if (TREE_TYPE (TREE_TYPE (new_val)) != TREE_TYPE (TREE_TYPE (old)))
12816 : : {
12817 : 4 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "new");
12818 : 4 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), new_val));
12819 : 4 : new_val = gfc_build_addr_expr (NULL_TREE, tmp);
12820 : : }
12821 : :
12822 : : /* Convert a constant to a pointer. */
12823 : 4 : if (!POINTER_TYPE_P (TREE_TYPE (comp)))
12824 : : {
12825 : 4 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "comp");
12826 : 4 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), comp));
12827 : 4 : comp = gfc_build_addr_expr (NULL_TREE, tmp);
12828 : : }
12829 : :
12830 : 4 : gfc_init_se (&argse, NULL);
12831 : 4 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12832 : : atom_expr);
12833 : 4 : gfc_add_block_to_block (&block, &argse.pre);
12834 : :
12835 : 4 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_cas, 9,
12836 : : token, offset, image_index, old, comp, new_val,
12837 : : stat, build_int_cst (integer_type_node,
12838 : 4 : (int) atom_expr->ts.type),
12839 : : build_int_cst (integer_type_node,
12840 : 4 : (int) atom_expr->ts.kind));
12841 : 4 : gfc_add_expr_to_block (&block, tmp);
12842 : 4 : gfc_add_block_to_block (&block, &argse.post);
12843 : 4 : gfc_add_block_to_block (&block, &post_block);
12844 : 4 : return gfc_finish_block (&block);
12845 : : }
12846 : :
12847 : 6 : tmp = TREE_TYPE (TREE_TYPE (atom));
12848 : 12 : fn = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
12849 : 6 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12850 : 6 : + 1);
12851 : 6 : tmp = builtin_decl_explicit (fn);
12852 : :
12853 : 6 : gfc_add_modify (&block, old, comp);
12854 : 12 : tmp = build_call_expr_loc (input_location, tmp, 6, atom,
12855 : : gfc_build_addr_expr (NULL, old),
12856 : 6 : fold_convert (TREE_TYPE (old), new_val),
12857 : : boolean_false_node,
12858 : : build_int_cst (NULL, MEMMODEL_RELAXED),
12859 : : build_int_cst (NULL, MEMMODEL_RELAXED));
12860 : 6 : gfc_add_expr_to_block (&block, tmp);
12861 : :
12862 : 6 : if (stat != NULL_TREE)
12863 : 6 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12864 : 6 : gfc_add_block_to_block (&block, &post_block);
12865 : 6 : return gfc_finish_block (&block);
12866 : : }
12867 : :
12868 : : static tree
12869 : 70 : conv_intrinsic_event_query (gfc_code *code)
12870 : : {
12871 : 70 : gfc_se se, argse;
12872 : 70 : tree stat = NULL_TREE, stat2 = NULL_TREE;
12873 : 70 : tree count = NULL_TREE, count2 = NULL_TREE;
12874 : :
12875 : 70 : gfc_expr *event_expr = code->ext.actual->expr;
12876 : :
12877 : 70 : if (code->ext.actual->next->next->expr)
12878 : : {
12879 : 12 : gcc_assert (code->ext.actual->next->next->expr->expr_type
12880 : : == EXPR_VARIABLE);
12881 : 12 : gfc_init_se (&argse, NULL);
12882 : 12 : gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
12883 : 12 : stat = argse.expr;
12884 : : }
12885 : 58 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12886 : 29 : stat = null_pointer_node;
12887 : :
12888 : 70 : if (code->ext.actual->next->expr)
12889 : : {
12890 : 70 : gcc_assert (code->ext.actual->next->expr->expr_type == EXPR_VARIABLE);
12891 : 70 : gfc_init_se (&argse, NULL);
12892 : 70 : gfc_conv_expr_val (&argse, code->ext.actual->next->expr);
12893 : 70 : count = argse.expr;
12894 : : }
12895 : :
12896 : 70 : gfc_start_block (&se.pre);
12897 : 70 : if (flag_coarray == GFC_FCOARRAY_LIB)
12898 : : {
12899 : 35 : tree tmp, token, image_index;
12900 : 35 : tree index = build_zero_cst (gfc_array_index_type);
12901 : :
12902 : 35 : if (event_expr->expr_type == EXPR_FUNCTION
12903 : 0 : && event_expr->value.function.isym
12904 : 0 : && event_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12905 : 0 : event_expr = event_expr->value.function.actual->expr;
12906 : :
12907 : 35 : tree caf_decl = gfc_get_tree_for_caf_expr (event_expr);
12908 : :
12909 : 35 : if (event_expr->symtree->n.sym->ts.type != BT_DERIVED
12910 : 35 : || event_expr->symtree->n.sym->ts.u.derived->from_intmod
12911 : : != INTMOD_ISO_FORTRAN_ENV
12912 : 35 : || event_expr->symtree->n.sym->ts.u.derived->intmod_sym_id
12913 : : != ISOFORTRAN_EVENT_TYPE)
12914 : : {
12915 : 0 : gfc_error ("Sorry, the event component of derived type at %L is not "
12916 : : "yet supported", &event_expr->where);
12917 : 0 : return NULL_TREE;
12918 : : }
12919 : :
12920 : 35 : if (gfc_is_coindexed (event_expr))
12921 : : {
12922 : 0 : gfc_error ("The event variable at %L shall not be coindexed",
12923 : : &event_expr->where);
12924 : 0 : return NULL_TREE;
12925 : : }
12926 : :
12927 : 35 : image_index = integer_zero_node;
12928 : :
12929 : 35 : gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
12930 : : event_expr);
12931 : :
12932 : : /* For arrays, obtain the array index. */
12933 : 35 : if (gfc_expr_attr (event_expr).dimension)
12934 : : {
12935 : 26 : tree desc, tmp, extent, lbound, ubound;
12936 : 26 : gfc_array_ref *ar, ar2;
12937 : 26 : int i;
12938 : :
12939 : : /* TODO: Extend this, once DT components are supported. */
12940 : 26 : ar = &event_expr->ref->u.ar;
12941 : 26 : ar2 = *ar;
12942 : 26 : memset (ar, '\0', sizeof (*ar));
12943 : 26 : ar->as = ar2.as;
12944 : 26 : ar->type = AR_FULL;
12945 : :
12946 : 26 : gfc_init_se (&argse, NULL);
12947 : 26 : argse.descriptor_only = 1;
12948 : 26 : gfc_conv_expr_descriptor (&argse, event_expr);
12949 : 26 : gfc_add_block_to_block (&se.pre, &argse.pre);
12950 : 26 : desc = argse.expr;
12951 : 26 : *ar = ar2;
12952 : :
12953 : 26 : extent = build_one_cst (gfc_array_index_type);
12954 : 78 : for (i = 0; i < ar->dimen; i++)
12955 : : {
12956 : 26 : gfc_init_se (&argse, NULL);
12957 : 26 : gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
12958 : 26 : gfc_add_block_to_block (&argse.pre, &argse.pre);
12959 : 26 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
12960 : 26 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
12961 : 26 : TREE_TYPE (lbound), argse.expr, lbound);
12962 : 26 : tmp = fold_build2_loc (input_location, MULT_EXPR,
12963 : 26 : TREE_TYPE (tmp), extent, tmp);
12964 : 26 : index = fold_build2_loc (input_location, PLUS_EXPR,
12965 : 26 : TREE_TYPE (tmp), index, tmp);
12966 : 26 : if (i < ar->dimen - 1)
12967 : : {
12968 : 0 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
12969 : 0 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
12970 : 0 : extent = fold_build2_loc (input_location, MULT_EXPR,
12971 : 0 : TREE_TYPE (tmp), extent, tmp);
12972 : : }
12973 : : }
12974 : : }
12975 : :
12976 : 35 : if (count != null_pointer_node && TREE_TYPE (count) != integer_type_node)
12977 : : {
12978 : 0 : count2 = count;
12979 : 0 : count = gfc_create_var (integer_type_node, "count");
12980 : : }
12981 : :
12982 : 35 : if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
12983 : : {
12984 : 0 : stat2 = stat;
12985 : 0 : stat = gfc_create_var (integer_type_node, "stat");
12986 : : }
12987 : :
12988 : 35 : index = fold_convert (size_type_node, index);
12989 : 70 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_query, 5,
12990 : : token, index, image_index, count
12991 : 35 : ? gfc_build_addr_expr (NULL, count) : count,
12992 : 35 : stat != null_pointer_node
12993 : 6 : ? gfc_build_addr_expr (NULL, stat) : stat);
12994 : 35 : gfc_add_expr_to_block (&se.pre, tmp);
12995 : :
12996 : 35 : if (count2 != NULL_TREE)
12997 : 0 : gfc_add_modify (&se.pre, count2,
12998 : 0 : fold_convert (TREE_TYPE (count2), count));
12999 : :
13000 : 35 : if (stat2 != NULL_TREE)
13001 : 0 : gfc_add_modify (&se.pre, stat2,
13002 : 0 : fold_convert (TREE_TYPE (stat2), stat));
13003 : :
13004 : 35 : return gfc_finish_block (&se.pre);
13005 : : }
13006 : :
13007 : 35 : gfc_init_se (&argse, NULL);
13008 : 35 : gfc_conv_expr_val (&argse, code->ext.actual->expr);
13009 : 35 : gfc_add_modify (&se.pre, count, fold_convert (TREE_TYPE (count), argse.expr));
13010 : :
13011 : 35 : if (stat != NULL_TREE)
13012 : 6 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
13013 : :
13014 : 35 : return gfc_finish_block (&se.pre);
13015 : : }
13016 : :
13017 : :
13018 : : /* This is a peculiar case because of the need to do dependency checking.
13019 : : It is called via trans-stmt.cc(gfc_trans_call), where it is picked out as
13020 : : a special case and this function called instead of
13021 : : gfc_conv_procedure_call. */
13022 : : void
13023 : 197 : gfc_conv_intrinsic_mvbits (gfc_se *se, gfc_actual_arglist *actual_args,
13024 : : gfc_loopinfo *loop)
13025 : : {
13026 : 197 : gfc_actual_arglist *actual;
13027 : 197 : gfc_se argse[5];
13028 : 197 : gfc_expr *arg[5];
13029 : 197 : gfc_ss *lss;
13030 : 197 : int n;
13031 : :
13032 : 197 : tree from, frompos, len, to, topos;
13033 : 197 : tree lenmask, oldbits, newbits, bitsize;
13034 : 197 : tree type, utype, above, mask1, mask2;
13035 : :
13036 : 197 : if (loop)
13037 : 67 : lss = loop->ss;
13038 : : else
13039 : 130 : lss = gfc_ss_terminator;
13040 : :
13041 : : actual = actual_args;
13042 : 1182 : for (n = 0; n < 5; n++, actual = actual->next)
13043 : : {
13044 : 985 : arg[n] = actual->expr;
13045 : 985 : gfc_init_se (&argse[n], NULL);
13046 : :
13047 : 985 : if (lss != gfc_ss_terminator)
13048 : : {
13049 : 335 : gfc_copy_loopinfo_to_se (&argse[n], loop);
13050 : : /* Find the ss for the expression if it is there. */
13051 : 335 : argse[n].ss = lss;
13052 : 335 : gfc_mark_ss_chain_used (lss, 1);
13053 : : }
13054 : :
13055 : 985 : gfc_conv_expr (&argse[n], arg[n]);
13056 : :
13057 : 985 : if (loop)
13058 : 335 : lss = argse[n].ss;
13059 : : }
13060 : :
13061 : 197 : from = argse[0].expr;
13062 : 197 : frompos = argse[1].expr;
13063 : 197 : len = argse[2].expr;
13064 : 197 : to = argse[3].expr;
13065 : 197 : topos = argse[4].expr;
13066 : :
13067 : : /* The type of the result (TO). */
13068 : 197 : type = TREE_TYPE (to);
13069 : 197 : bitsize = build_int_cst (integer_type_node, TYPE_PRECISION (type));
13070 : :
13071 : : /* Optionally generate code for runtime argument check. */
13072 : 197 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
13073 : : {
13074 : 18 : tree nbits, below, ccond;
13075 : 18 : tree fp = fold_convert (long_integer_type_node, frompos);
13076 : 18 : tree ln = fold_convert (long_integer_type_node, len);
13077 : 18 : tree tp = fold_convert (long_integer_type_node, topos);
13078 : 18 : below = fold_build2_loc (input_location, LT_EXPR,
13079 : : logical_type_node, frompos,
13080 : 18 : build_int_cst (TREE_TYPE (frompos), 0));
13081 : 18 : above = fold_build2_loc (input_location, GT_EXPR,
13082 : : logical_type_node, frompos,
13083 : 18 : fold_convert (TREE_TYPE (frompos), bitsize));
13084 : 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
13085 : : logical_type_node, below, above);
13086 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
13087 : 18 : &arg[1]->where,
13088 : : "FROMPOS argument (%ld) out of range 0:%d "
13089 : : "in intrinsic MVBITS", fp, bitsize);
13090 : 18 : below = fold_build2_loc (input_location, LT_EXPR,
13091 : : logical_type_node, len,
13092 : 18 : build_int_cst (TREE_TYPE (len), 0));
13093 : 18 : above = fold_build2_loc (input_location, GT_EXPR,
13094 : : logical_type_node, len,
13095 : 18 : fold_convert (TREE_TYPE (len), bitsize));
13096 : 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
13097 : : logical_type_node, below, above);
13098 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[2].pre,
13099 : 18 : &arg[2]->where,
13100 : : "LEN argument (%ld) out of range 0:%d "
13101 : : "in intrinsic MVBITS", ln, bitsize);
13102 : 18 : below = fold_build2_loc (input_location, LT_EXPR,
13103 : : logical_type_node, topos,
13104 : 18 : build_int_cst (TREE_TYPE (topos), 0));
13105 : 18 : above = fold_build2_loc (input_location, GT_EXPR,
13106 : : logical_type_node, topos,
13107 : 18 : fold_convert (TREE_TYPE (topos), bitsize));
13108 : 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
13109 : : logical_type_node, below, above);
13110 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
13111 : 18 : &arg[4]->where,
13112 : : "TOPOS argument (%ld) out of range 0:%d "
13113 : : "in intrinsic MVBITS", tp, bitsize);
13114 : :
13115 : : /* The tests above ensure that FROMPOS, LEN and TOPOS fit into short
13116 : : integers. Additions below cannot overflow. */
13117 : 18 : nbits = fold_convert (long_integer_type_node, bitsize);
13118 : 18 : above = fold_build2_loc (input_location, PLUS_EXPR,
13119 : : long_integer_type_node, fp, ln);
13120 : 18 : ccond = fold_build2_loc (input_location, GT_EXPR,
13121 : : logical_type_node, above, nbits);
13122 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
13123 : : &arg[1]->where,
13124 : : "FROMPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
13125 : : "in intrinsic MVBITS", fp, ln, bitsize);
13126 : 18 : above = fold_build2_loc (input_location, PLUS_EXPR,
13127 : : long_integer_type_node, tp, ln);
13128 : 18 : ccond = fold_build2_loc (input_location, GT_EXPR,
13129 : : logical_type_node, above, nbits);
13130 : 18 : gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
13131 : : &arg[4]->where,
13132 : : "TOPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
13133 : : "in intrinsic MVBITS", tp, ln, bitsize);
13134 : : }
13135 : :
13136 : 1182 : for (n = 0; n < 5; n++)
13137 : : {
13138 : 985 : gfc_add_block_to_block (&se->pre, &argse[n].pre);
13139 : 985 : gfc_add_block_to_block (&se->post, &argse[n].post);
13140 : : }
13141 : :
13142 : : /* lenmask = (LEN >= bit_size (TYPE)) ? ~(TYPE)0 : ((TYPE)1 << LEN) - 1 */
13143 : 197 : above = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
13144 : 197 : len, fold_convert (TREE_TYPE (len), bitsize));
13145 : 197 : mask1 = build_int_cst (type, -1);
13146 : 197 : mask2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
13147 : : build_int_cst (type, 1), len);
13148 : 197 : mask2 = fold_build2_loc (input_location, MINUS_EXPR, type,
13149 : : mask2, build_int_cst (type, 1));
13150 : 197 : lenmask = fold_build3_loc (input_location, COND_EXPR, type,
13151 : : above, mask1, mask2);
13152 : :
13153 : : /* newbits = (((UTYPE)(FROM) >> FROMPOS) & lenmask) << TOPOS.
13154 : : * For valid frompos+len <= bit_size(FROM) the conversion to unsigned is
13155 : : * not strictly necessary; artificial bits from rshift will be masked. */
13156 : 197 : utype = unsigned_type_for (type);
13157 : 197 : newbits = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
13158 : : fold_convert (utype, from), frompos);
13159 : 197 : newbits = fold_build2_loc (input_location, BIT_AND_EXPR, type,
13160 : : fold_convert (type, newbits), lenmask);
13161 : 197 : newbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
13162 : : newbits, topos);
13163 : :
13164 : : /* oldbits = TO & (~(lenmask << TOPOS)). */
13165 : 197 : oldbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
13166 : : lenmask, topos);
13167 : 197 : oldbits = fold_build1_loc (input_location, BIT_NOT_EXPR, type, oldbits);
13168 : 197 : oldbits = fold_build2_loc (input_location, BIT_AND_EXPR, type, oldbits, to);
13169 : :
13170 : : /* TO = newbits | oldbits. */
13171 : 197 : se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
13172 : : oldbits, newbits);
13173 : :
13174 : : /* Return the assignment. */
13175 : 197 : se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
13176 : : void_type_node, to, se->expr);
13177 : 197 : }
13178 : :
13179 : : /* Comes from trans-stmt.cc, but we don't want the whole header included. */
13180 : : extern void gfc_trans_sync_stat (struct sync_stat *sync_stat, gfc_se *se,
13181 : : tree *stat, tree *errmsg, tree *errmsg_len);
13182 : :
13183 : : static tree
13184 : 260 : conv_intrinsic_move_alloc (gfc_code *code)
13185 : : {
13186 : 260 : stmtblock_t block;
13187 : 260 : gfc_expr *from_expr, *to_expr;
13188 : 260 : gfc_se from_se, to_se;
13189 : 260 : tree tmp, to_tree, from_tree, stat, errmsg, errmsg_len, fin_label = NULL_TREE;
13190 : 260 : bool coarray, from_is_class, from_is_scalar;
13191 : 260 : gfc_actual_arglist *arg = code->ext.actual;
13192 : 260 : sync_stat tmp_sync_stat = {nullptr, nullptr};
13193 : :
13194 : 260 : gfc_start_block (&block);
13195 : :
13196 : 260 : from_expr = arg->expr;
13197 : 260 : arg = arg->next;
13198 : 260 : to_expr = arg->expr;
13199 : 260 : arg = arg->next;
13200 : :
13201 : 780 : while (arg)
13202 : : {
13203 : 520 : if (arg->expr)
13204 : : {
13205 : 0 : if (!strcmp ("stat", arg->name))
13206 : 0 : tmp_sync_stat.stat = arg->expr;
13207 : 0 : else if (!strcmp ("errmsg", arg->name))
13208 : 0 : tmp_sync_stat.errmsg = arg->expr;
13209 : : }
13210 : 520 : arg = arg->next;
13211 : : }
13212 : :
13213 : 260 : gfc_init_se (&from_se, NULL);
13214 : 260 : gfc_init_se (&to_se, NULL);
13215 : :
13216 : 260 : gfc_trans_sync_stat (&tmp_sync_stat, &from_se, &stat, &errmsg, &errmsg_len);
13217 : 260 : if (stat != null_pointer_node)
13218 : 0 : fin_label = gfc_build_label_decl (NULL_TREE);
13219 : :
13220 : 260 : gcc_assert (from_expr->ts.type != BT_CLASS || to_expr->ts.type == BT_CLASS);
13221 : 260 : coarray = from_expr->corank != 0;
13222 : :
13223 : 260 : from_is_class = from_expr->ts.type == BT_CLASS;
13224 : 260 : from_is_scalar = from_expr->rank == 0 && !coarray;
13225 : 260 : if (to_expr->ts.type == BT_CLASS || from_is_scalar)
13226 : : {
13227 : 163 : from_se.want_pointer = 1;
13228 : 163 : if (from_is_scalar)
13229 : 115 : gfc_conv_expr (&from_se, from_expr);
13230 : : else
13231 : 48 : gfc_conv_expr_descriptor (&from_se, from_expr);
13232 : 163 : if (from_is_class)
13233 : 64 : from_tree = gfc_class_data_get (from_se.expr);
13234 : : else
13235 : : {
13236 : 99 : gfc_symbol *vtab;
13237 : 99 : from_tree = from_se.expr;
13238 : :
13239 : 99 : if (to_expr->ts.type == BT_CLASS)
13240 : : {
13241 : 36 : vtab = gfc_find_vtab (&from_expr->ts);
13242 : 36 : gcc_assert (vtab);
13243 : 36 : from_se.expr = gfc_get_symbol_decl (vtab);
13244 : : }
13245 : : }
13246 : 163 : gfc_add_block_to_block (&block, &from_se.pre);
13247 : :
13248 : 163 : to_se.want_pointer = 1;
13249 : 163 : if (to_expr->rank == 0)
13250 : 115 : gfc_conv_expr (&to_se, to_expr);
13251 : : else
13252 : 48 : gfc_conv_expr_descriptor (&to_se, to_expr);
13253 : 163 : if (to_expr->ts.type == BT_CLASS)
13254 : 100 : to_tree = gfc_class_data_get (to_se.expr);
13255 : : else
13256 : 63 : to_tree = to_se.expr;
13257 : 163 : gfc_add_block_to_block (&block, &to_se.pre);
13258 : :
13259 : : /* Deallocate "to". */
13260 : 163 : if (to_expr->rank == 0)
13261 : : {
13262 : 115 : tmp = gfc_deallocate_scalar_with_status (to_tree, stat, fin_label,
13263 : : true, to_expr, to_expr->ts,
13264 : : NULL_TREE, false, true,
13265 : : errmsg, errmsg_len);
13266 : 115 : gfc_add_expr_to_block (&block, tmp);
13267 : : }
13268 : :
13269 : 163 : if (from_is_scalar)
13270 : : {
13271 : : /* Assign (_data) pointers. */
13272 : 115 : gfc_add_modify_loc (input_location, &block, to_tree,
13273 : 115 : fold_convert (TREE_TYPE (to_tree), from_tree));
13274 : :
13275 : : /* Set "from" to NULL. */
13276 : 115 : gfc_add_modify_loc (input_location, &block, from_tree,
13277 : 115 : fold_convert (TREE_TYPE (from_tree),
13278 : : null_pointer_node));
13279 : :
13280 : 115 : gfc_add_block_to_block (&block, &from_se.post);
13281 : : }
13282 : 163 : gfc_add_block_to_block (&block, &to_se.post);
13283 : :
13284 : : /* Set _vptr. */
13285 : 163 : if (to_expr->ts.type == BT_CLASS)
13286 : : {
13287 : 100 : gfc_class_set_vptr (&block, to_se.expr, from_se.expr);
13288 : 100 : if (from_is_class)
13289 : 64 : gfc_reset_vptr (&block, from_expr);
13290 : 100 : if (UNLIMITED_POLY (to_expr))
13291 : : {
13292 : 20 : tree to_len = gfc_class_len_get (to_se.class_container);
13293 : 20 : tmp = from_expr->ts.type == BT_CHARACTER && from_se.string_length
13294 : 20 : ? from_se.string_length
13295 : : : size_zero_node;
13296 : 20 : gfc_add_modify_loc (input_location, &block, to_len,
13297 : 20 : fold_convert (TREE_TYPE (to_len), tmp));
13298 : : }
13299 : : }
13300 : :
13301 : 163 : if (from_is_scalar)
13302 : : {
13303 : 115 : if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13304 : : {
13305 : 6 : gfc_add_modify_loc (input_location, &block, to_se.string_length,
13306 : 6 : fold_convert (TREE_TYPE (to_se.string_length),
13307 : : from_se.string_length));
13308 : 6 : if (from_expr->ts.deferred)
13309 : 6 : gfc_add_modify_loc (
13310 : : input_location, &block, from_se.string_length,
13311 : 6 : build_int_cst (TREE_TYPE (from_se.string_length), 0));
13312 : : }
13313 : 115 : if (UNLIMITED_POLY (from_expr))
13314 : 2 : gfc_reset_len (&block, from_expr);
13315 : :
13316 : 115 : return gfc_finish_block (&block);
13317 : : }
13318 : :
13319 : 48 : gfc_init_se (&to_se, NULL);
13320 : 48 : gfc_init_se (&from_se, NULL);
13321 : : }
13322 : :
13323 : : /* Deallocate "to". */
13324 : 145 : if (from_expr->rank == 0)
13325 : : {
13326 : 3 : to_se.want_coarray = 1;
13327 : 3 : from_se.want_coarray = 1;
13328 : : }
13329 : 145 : gfc_conv_expr_descriptor (&to_se, to_expr);
13330 : 145 : gfc_conv_expr_descriptor (&from_se, from_expr);
13331 : 145 : gfc_add_block_to_block (&block, &to_se.pre);
13332 : 145 : gfc_add_block_to_block (&block, &from_se.pre);
13333 : :
13334 : : /* For coarrays, call SYNC ALL if TO is already deallocated as MOVE_ALLOC
13335 : : is an image control "statement", cf. IR F08/0040 in 12-006A. */
13336 : 145 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
13337 : : {
13338 : 4 : tree cond;
13339 : :
13340 : 4 : tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
13341 : : fin_label, true, to_expr,
13342 : : GFC_CAF_COARRAY_DEALLOCATE_ONLY,
13343 : : NULL_TREE, NULL_TREE,
13344 : : gfc_conv_descriptor_token (to_se.expr),
13345 : : true);
13346 : 4 : gfc_add_expr_to_block (&block, tmp);
13347 : :
13348 : 4 : tmp = gfc_conv_descriptor_data_get (to_se.expr);
13349 : 4 : cond = fold_build2_loc (input_location, EQ_EXPR,
13350 : : logical_type_node, tmp,
13351 : 4 : fold_convert (TREE_TYPE (tmp),
13352 : : null_pointer_node));
13353 : 4 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
13354 : : 3, null_pointer_node, null_pointer_node,
13355 : : integer_zero_node);
13356 : :
13357 : 4 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
13358 : : tmp, build_empty_stmt (input_location));
13359 : 4 : gfc_add_expr_to_block (&block, tmp);
13360 : 4 : }
13361 : : else
13362 : : {
13363 : 141 : if (to_expr->ts.type == BT_DERIVED
13364 : 25 : && to_expr->ts.u.derived->attr.alloc_comp)
13365 : : {
13366 : 19 : tmp = gfc_deallocate_alloc_comp (to_expr->ts.u.derived,
13367 : : to_se.expr, to_expr->rank);
13368 : 19 : gfc_add_expr_to_block (&block, tmp);
13369 : : }
13370 : :
13371 : 141 : tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
13372 : : fin_label, true, to_expr,
13373 : : GFC_CAF_COARRAY_NOCOARRAY, NULL_TREE,
13374 : : NULL_TREE, NULL_TREE, true);
13375 : 141 : gfc_add_expr_to_block (&block, tmp);
13376 : : }
13377 : :
13378 : : /* Copy the array descriptor data. */
13379 : 145 : gfc_add_modify_loc (input_location, &block, to_se.expr, from_se.expr);
13380 : :
13381 : : /* Set "from" to NULL. */
13382 : 145 : tmp = gfc_conv_descriptor_data_get (from_se.expr);
13383 : 145 : gfc_add_modify_loc (input_location, &block, tmp,
13384 : 145 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
13385 : :
13386 : 145 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
13387 : : {
13388 : : /* Copy the array descriptor data has overwritten the to-token and cleared
13389 : : from.data. Now also clear the from.token. */
13390 : 4 : gfc_add_modify (&block, gfc_conv_descriptor_token (from_se.expr),
13391 : : null_pointer_node);
13392 : : }
13393 : :
13394 : 145 : if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13395 : : {
13396 : 7 : gfc_add_modify_loc (input_location, &block, to_se.string_length,
13397 : 7 : fold_convert (TREE_TYPE (to_se.string_length),
13398 : : from_se.string_length));
13399 : 7 : if (from_expr->ts.deferred)
13400 : 6 : gfc_add_modify_loc (input_location, &block, from_se.string_length,
13401 : 6 : build_int_cst (TREE_TYPE (from_se.string_length), 0));
13402 : : }
13403 : 145 : if (fin_label)
13404 : 0 : gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, fin_label));
13405 : :
13406 : 145 : gfc_add_block_to_block (&block, &to_se.post);
13407 : 145 : gfc_add_block_to_block (&block, &from_se.post);
13408 : :
13409 : 145 : return gfc_finish_block (&block);
13410 : : }
13411 : :
13412 : :
13413 : : tree
13414 : 6064 : gfc_conv_intrinsic_subroutine (gfc_code *code)
13415 : : {
13416 : 6064 : tree res;
13417 : :
13418 : 6064 : gcc_assert (code->resolved_isym);
13419 : :
13420 : 6064 : switch (code->resolved_isym->id)
13421 : : {
13422 : 260 : case GFC_ISYM_MOVE_ALLOC:
13423 : 260 : res = conv_intrinsic_move_alloc (code);
13424 : 260 : break;
13425 : :
13426 : 10 : case GFC_ISYM_ATOMIC_CAS:
13427 : 10 : res = conv_intrinsic_atomic_cas (code);
13428 : 10 : break;
13429 : :
13430 : 68 : case GFC_ISYM_ATOMIC_ADD:
13431 : 68 : case GFC_ISYM_ATOMIC_AND:
13432 : 68 : case GFC_ISYM_ATOMIC_DEF:
13433 : 68 : case GFC_ISYM_ATOMIC_OR:
13434 : 68 : case GFC_ISYM_ATOMIC_XOR:
13435 : 68 : case GFC_ISYM_ATOMIC_FETCH_ADD:
13436 : 68 : case GFC_ISYM_ATOMIC_FETCH_AND:
13437 : 68 : case GFC_ISYM_ATOMIC_FETCH_OR:
13438 : 68 : case GFC_ISYM_ATOMIC_FETCH_XOR:
13439 : 68 : res = conv_intrinsic_atomic_op (code);
13440 : 68 : break;
13441 : :
13442 : 119 : case GFC_ISYM_ATOMIC_REF:
13443 : 119 : res = conv_intrinsic_atomic_ref (code);
13444 : 119 : break;
13445 : :
13446 : 70 : case GFC_ISYM_EVENT_QUERY:
13447 : 70 : res = conv_intrinsic_event_query (code);
13448 : 70 : break;
13449 : :
13450 : 2794 : case GFC_ISYM_C_F_POINTER:
13451 : 2794 : case GFC_ISYM_C_F_PROCPOINTER:
13452 : 2794 : res = conv_isocbinding_subroutine (code);
13453 : 2794 : break;
13454 : :
13455 : 293 : case GFC_ISYM_CAF_SEND:
13456 : 293 : res = conv_caf_send_to_remote (code);
13457 : 293 : break;
13458 : :
13459 : 97 : case GFC_ISYM_CAF_SENDGET:
13460 : 97 : res = conv_caf_sendget (code);
13461 : 97 : break;
13462 : :
13463 : 63 : case GFC_ISYM_CO_BROADCAST:
13464 : 63 : case GFC_ISYM_CO_MIN:
13465 : 63 : case GFC_ISYM_CO_MAX:
13466 : 63 : case GFC_ISYM_CO_REDUCE:
13467 : 63 : case GFC_ISYM_CO_SUM:
13468 : 63 : res = conv_co_collective (code);
13469 : 63 : break;
13470 : :
13471 : 10 : case GFC_ISYM_FREE:
13472 : 10 : res = conv_intrinsic_free (code);
13473 : 10 : break;
13474 : :
13475 : 55 : case GFC_ISYM_FSTAT:
13476 : 55 : case GFC_ISYM_LSTAT:
13477 : 55 : case GFC_ISYM_STAT:
13478 : 55 : res = conv_intrinsic_fstat_lstat_stat_sub (code);
13479 : 55 : break;
13480 : :
13481 : 90 : case GFC_ISYM_RANDOM_INIT:
13482 : 90 : res = conv_intrinsic_random_init (code);
13483 : 90 : break;
13484 : :
13485 : 15 : case GFC_ISYM_KILL:
13486 : 15 : res = conv_intrinsic_kill_sub (code);
13487 : 15 : break;
13488 : :
13489 : : case GFC_ISYM_MVBITS:
13490 : : res = NULL_TREE;
13491 : : break;
13492 : :
13493 : 194 : case GFC_ISYM_SYSTEM_CLOCK:
13494 : 194 : res = conv_intrinsic_system_clock (code);
13495 : 194 : break;
13496 : :
13497 : 102 : case GFC_ISYM_SPLIT:
13498 : 102 : res = conv_intrinsic_split (code);
13499 : 102 : break;
13500 : :
13501 : : default:
13502 : : res = NULL_TREE;
13503 : : break;
13504 : : }
13505 : :
13506 : 6064 : return res;
13507 : : }
13508 : :
13509 : : #include "gt-fortran-trans-intrinsic.h"
|