Debian Codegraph @

gcc/fortran/trans-intrinsic.cc

raw
1 /* Intrinsic translation
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 Contributed by Paul Brook <[email protected]>
4 and Steven Bosscher <[email protected]>
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 builtin_decl_for_precision (enum built_in_function base_built_in,
147 int precision)
148 {
149 enum built_in_function i = END_BUILTINS;
150
151 gfc_intrinsic_map_t *m;
152 for (m = gfc_intrinsic_map; m->double_built_in != base_built_in ; m++)
153 ;
154
155 if (precision == TYPE_PRECISION (float_type_node))
156 i = m->float_built_in;
157 else if (precision == TYPE_PRECISION (double_type_node))
158 i = m->double_built_in;
159 else if (precision == TYPE_PRECISION (long_double_type_node)
160 && (!gfc_real16_is_float128
161 || long_double_type_node != gfc_float128_type_node))
162 i = m->long_double_built_in;
163 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 return m->real16_decl;
168 }
169
170 return (i == END_BUILTINS ? NULL_TREE : builtin_decl_explicit (i));
171 }
172
173
174 tree
175 gfc_builtin_decl_for_float_kind (enum built_in_function double_built_in,
176 int kind)
177 {
178 int i = gfc_validate_kind (BT_REAL, kind, false);
179
180 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 for (m = gfc_intrinsic_map; m->double_built_in != double_built_in ; m++)
186 ;
187
188 return m->real16_decl;
189 }
190
191 return builtin_decl_for_precision (double_built_in,
192 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 gfc_conv_intrinsic_function_args (gfc_se *se, gfc_expr *expr,
203 tree *argarray, int nargs)
204 {
205 gfc_actual_arglist *actual;
206 gfc_expr *e;
207 gfc_intrinsic_arg *formal;
208 gfc_se argse;
209 int curr_arg;
210
211 formal = expr->value.function.isym->formal;
212 actual = expr->value.function.actual;
213
214 for (curr_arg = 0; curr_arg < nargs; curr_arg++,
215 actual = actual->next,
216 formal = formal ? formal->next : NULL)
217 {
218 gcc_assert (actual);
219 e = actual->expr;
220 /* Skip omitted optional arguments. */
221 if (!e)
222 {
223 --curr_arg;
224 continue;
225 }
226
227 /* Evaluate the parameter. This will substitute scalarized
228 references automatically. */
229 gfc_init_se (&argse, se);
230
231 if (e->ts.type == BT_CHARACTER)
232 {
233 gfc_conv_expr (&argse, e);
234 gfc_conv_string_parameter (&argse);
235 argarray[curr_arg++] = argse.string_length;
236 gcc_assert (curr_arg < nargs);
237 }
238 else
239 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 if (e->expr_type == EXPR_VARIABLE
244 && e->symtree->n.sym->attr.optional
245 && formal
246 && formal->optional)
247 gfc_conv_missing_dummy (&argse, e, formal->ts, 0);
248
249 gfc_add_block_to_block (&se->pre, &argse.pre);
250 gfc_add_block_to_block (&se->post, &argse.post);
251 argarray[curr_arg] = argse.expr;
252 }
253 }
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 gfc_intrinsic_argument_list_length (gfc_expr *expr)
260 {
261 int n = 0;
262 gfc_actual_arglist *actual;
263
264 for (actual = expr->value.function.actual; actual; actual = actual->next)
265 {
266 if (!actual->expr)
267 continue;
268
269 if (actual->expr->ts.type == BT_CHARACTER)
270 n += 2;
271 else
272 n++;
273 }
274
275 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 gfc_conv_intrinsic_conversion (gfc_se * se, gfc_expr * expr)
284 {
285 tree type;
286 tree *args;
287 int nargs;
288
289 nargs = gfc_intrinsic_argument_list_length (expr);
290 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 type = gfc_typenode_for_spec (&expr->ts);
296 gcc_assert (expr->value.function.actual->expr);
297 gfc_conv_intrinsic_function_args (se, expr, args, nargs);
298
299 /* Conversion between character kinds involves a call to a library
300 function. */
301 if (expr->ts.type == BT_CHARACTER)
302 {
303 tree fndecl, var, addr, tmp;
304
305 if (expr->ts.kind == 1
306 && expr->value.function.actual->expr->ts.kind == 4)
307 fndecl = gfor_fndecl_convert_char4_to_char1;
308 else if (expr->ts.kind == 4
309 && expr->value.function.actual->expr->ts.kind == 1)
310 fndecl = gfor_fndecl_convert_char1_to_char4;
311 else
312 gcc_unreachable ();
313
314 /* Create the variable storing the converted value. */
315 type = gfc_get_pchar_type (expr->ts.kind);
316 var = gfc_create_var (type, "str");
317 addr = gfc_build_addr_expr (build_pointer_type (type), var);
318
319 /* Call the library function that will perform the conversion. */
320 gcc_assert (nargs >= 2);
321 tmp = build_call_expr_loc (input_location,
322 fndecl, 3, addr, args[0], args[1]);
323 gfc_add_expr_to_block (&se->pre, tmp);
324
325 /* Free the temporary afterwards. */
326 tmp = gfc_call_free (var);
327 gfc_add_expr_to_block (&se->post, tmp);
328
329 se->expr = var;
330 se->string_length = args[0];
331
332 return;
333 }
334
335 /* Conversion from complex to non-complex involves taking the real
336 component of the value. */
337 if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
338 && expr->ts.type != BT_COMPLEX)
339 {
340 tree artype;
341
342 artype = TREE_TYPE (TREE_TYPE (args[0]));
343 args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
344 args[0]);
345 }
346
347 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 build_fixbound_expr (stmtblock_t * pblock, tree arg, tree type, int up)
357 {
358 tree tmp;
359 tree cond;
360 tree argtype;
361 tree intval;
362
363 argtype = TREE_TYPE (arg);
364 arg = gfc_evaluate_now (arg, pblock);
365
366 intval = convert (type, arg);
367 intval = gfc_evaluate_now (intval, pblock);
368
369 tmp = convert (argtype, intval);
370 cond = fold_build2_loc (input_location, up ? GE_EXPR : LE_EXPR,
371 logical_type_node, tmp, arg);
372
373 tmp = fold_build2_loc (input_location, up ? PLUS_EXPR : MINUS_EXPR, type,
374 intval, build_int_cst (type, 1));
375 tmp = fold_build3_loc (input_location, COND_EXPR, type, cond, intval, tmp);
376 return tmp;
377 }
378
379
380 /* Round to nearest integer, away from zero. */
381
382 static tree
383 build_round_expr (tree arg, tree restype)
384 {
385 tree argtype;
386 tree fn;
387 int argprec, resprec;
388
389 argtype = TREE_TYPE (arg);
390 argprec = TYPE_PRECISION (argtype);
391 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 if (resprec <= INT_TYPE_SIZE
400 && argprec <= TYPE_PRECISION (long_double_type_node))
401 fn = builtin_decl_for_precision (BUILT_IN_IROUND, argprec);
402 else if (resprec <= LONG_TYPE_SIZE)
403 fn = builtin_decl_for_precision (BUILT_IN_LROUND, argprec);
404 else if (resprec <= LONG_LONG_TYPE_SIZE)
405 fn = builtin_decl_for_precision (BUILT_IN_LLROUND, argprec);
406 else if (resprec >= argprec)
407 fn = builtin_decl_for_precision (BUILT_IN_ROUND, argprec);
408 else
409 gcc_unreachable ();
410
411 return convert (restype, build_call_expr_loc (input_location,
412 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 build_fix_expr (stmtblock_t * pblock, tree arg, tree type,
422 enum rounding_mode op)
423 {
424 switch (op)
425 {
426 case RND_FLOOR:
427 return build_fixbound_expr (pblock, arg, type, 0);
428
429 case RND_CEIL:
430 return build_fixbound_expr (pblock, arg, type, 1);
431
432 case RND_ROUND:
433 return build_round_expr (arg, type);
434
435 case RND_TRUNC:
436 return fold_build1_loc (input_location, FIX_TRUNC_EXPR, type, arg);
437
438 default:
439 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 gfc_conv_intrinsic_aint (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
455 {
456 tree type;
457 tree itype;
458 tree arg[2];
459 tree tmp;
460 tree cond;
461 tree decl;
462 mpfr_t huge;
463 int n, nargs;
464 int kind;
465
466 kind = expr->ts.kind;
467 nargs = gfc_intrinsic_argument_list_length (expr);
468
469 decl = NULL_TREE;
470 /* We have builtin functions for some cases. */
471 switch (op)
472 {
473 case RND_ROUND:
474 decl = gfc_builtin_decl_for_float_kind (BUILT_IN_ROUND, kind);
475 break;
476
477 case RND_TRUNC:
478 decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, kind);
479 break;
480
481 default:
482 gcc_unreachable ();
483 }
484
485 /* Evaluate the argument. */
486 gcc_assert (expr->value.function.actual->expr);
487 gfc_conv_intrinsic_function_args (se, expr, arg, nargs);
488
489 /* Use a builtin function if one exists. */
490 if (decl != NULL_TREE)
491 {
492 se->expr = build_call_expr_loc (input_location, decl, 1, arg[0]);
493 return;
494 }
495
496 /* This code is probably redundant, but we'll keep it lying around just
497 in case. */
498 type = gfc_typenode_for_spec (&expr->ts);
499 arg[0] = gfc_evaluate_now (arg[0], &se->pre);
500
501 /* Test if the value is too large to handle sensibly. */
502 gfc_set_model_kind (kind);
503 mpfr_init (huge);
504 n = gfc_validate_kind (BT_INTEGER, kind, false);
505 mpfr_set_z (huge, gfc_integer_kinds[n].huge, GFC_RND_MODE);
506 tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
507 cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, arg[0],
508 tmp);
509
510 mpfr_neg (huge, huge, GFC_RND_MODE);
511 tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
512 tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, arg[0],
513 tmp);
514 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
515 cond, tmp);
516 itype = gfc_get_int_type (kind);
517
518 tmp = build_fix_expr (&se->pre, arg[0], itype, op);
519 tmp = convert (type, tmp);
520 se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
521 arg[0]);
522 mpfr_clear (huge);
523 }
524
525
526 /* Convert to an integer using the specified rounding mode. */
527
528 static void
529 gfc_conv_intrinsic_int (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
530 {
531 tree type;
532 tree *args;
533 int nargs;
534
535 nargs = gfc_intrinsic_argument_list_length (expr);
536 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 type = gfc_typenode_for_spec (&expr->ts);
541 gcc_assert (expr->value.function.actual->expr);
542 gfc_conv_intrinsic_function_args (se, expr, args, nargs);
543
544 if (TREE_CODE (TREE_TYPE (args[0])) == INTEGER_TYPE)
545 {
546 /* Conversion to a different integer kind. */
547 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 if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
554 && expr->ts.type != BT_COMPLEX)
555 {
556 tree artype;
557
558 artype = TREE_TYPE (TREE_TYPE (args[0]));
559 args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
560 args[0]);
561 }
562
563 se->expr = build_fix_expr (&se->pre, args[0], type, op);
564 }
565 }
566
567
568 /* Get the imaginary component of a value. */
569
570 static void
571 gfc_conv_intrinsic_imagpart (gfc_se * se, gfc_expr * expr)
572 {
573 tree arg;
574
575 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
576 se->expr = fold_build1_loc (input_location, IMAGPART_EXPR,
577 TREE_TYPE (TREE_TYPE (arg)), arg);
578 }
579
580
581 /* Get the complex conjugate of a value. */
582
583 static void
584 gfc_conv_intrinsic_conjg (gfc_se * se, gfc_expr * expr)
585 {
586 tree arg;
587
588 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
589 se->expr = fold_build1_loc (input_location, CONJ_EXPR, TREE_TYPE (arg), arg);
590 }
591
592
593
594 static tree
595 define_quad_builtin (const char *name, tree type, bool is_const)
596 {
597 tree fndecl;
598 fndecl = build_decl (input_location, FUNCTION_DECL, get_identifier (name),
599 type);
600
601 /* Mark the decl as external. */
602 DECL_EXTERNAL (fndecl) = 1;
603 TREE_PUBLIC (fndecl) = 1;
604
605 /* Mark it __attribute__((const)). */
606 TREE_READONLY (fndecl) = is_const;
607
608 rest_of_decl_compilation (fndecl, 1, 0);
609
610 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 add_simd_flag_for_built_in (tree fndecl)
618 {
619 if (gfc_vectorized_builtins == NULL
620 || fndecl == NULL_TREE)
621 return;
622
623 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
624 int *clauses = gfc_vectorized_builtins->get (name);
625 if (clauses)
626 {
627 for (unsigned i = 0; i < 3; i++)
628 if (*clauses & (1 << i))
629 {
630 gfc_simd_clause simd_type = (gfc_simd_clause)*clauses;
631 tree omp_clause = NULL_TREE;
632 if (simd_type == SIMD_NONE)
633 ; /* No SIMD clause. */
634 else
635 {
636 omp_clause_code code
637 = (simd_type == SIMD_INBRANCH
638 ? OMP_CLAUSE_INBRANCH : OMP_CLAUSE_NOTINBRANCH);
639 omp_clause = build_omp_clause (UNKNOWN_LOCATION, code);
640 omp_clause = build_tree_list (NULL_TREE, omp_clause);
641 }
642
643 DECL_ATTRIBUTES (fndecl)
644 = tree_cons (get_identifier ("omp declare simd"), omp_clause,
645 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 gfc_adjust_builtins (void)
655 {
656 gfc_intrinsic_map_t *m;
657 for (m = gfc_intrinsic_map;
658 m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
659 {
660 add_simd_flag_for_built_in (m->real4_decl);
661 add_simd_flag_for_built_in (m->complex4_decl);
662 add_simd_flag_for_built_in (m->real8_decl);
663 add_simd_flag_for_built_in (m->complex8_decl);
664 add_simd_flag_for_built_in (m->real10_decl);
665 add_simd_flag_for_built_in (m->complex10_decl);
666 add_simd_flag_for_built_in (m->real16_decl);
667 add_simd_flag_for_built_in (m->complex16_decl);
668 add_simd_flag_for_built_in (m->real16_decl);
669 add_simd_flag_for_built_in (m->complex16_decl);
670 }
671
672 /* Release all strings. */
673 if (gfc_vectorized_builtins != NULL)
674 {
675 for (hash_map<nofree_string_hash, int>::iterator it
676 = gfc_vectorized_builtins->begin ();
677 it != gfc_vectorized_builtins->end (); ++it)
678 free (CONST_CAST (char *, (*it).first));
679
680 delete gfc_vectorized_builtins;
681 gfc_vectorized_builtins = NULL;
682 }
683 }
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 gfc_build_intrinsic_lib_fndecls (void)
690 {
691 gfc_intrinsic_map_t *m;
692 tree quad_decls[END_BUILTINS + 1];
693
694 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 tree type, complex_type, func_1, func_2, func_3, func_cabs, func_frexp;
701 tree func_iround, func_lround, func_llround, func_scalbn, func_cpow;
702
703 memset (quad_decls, 0, sizeof(tree) * (END_BUILTINS + 1));
704
705 type = gfc_float128_type_node;
706 complex_type = gfc_complex_float128_type_node;
707 /* type (*) (type) */
708 func_1 = build_function_type_list (type, type, NULL_TREE);
709 /* int (*) (type) */
710 func_iround = build_function_type_list (integer_type_node,
711 type, NULL_TREE);
712 /* long (*) (type) */
713 func_lround = build_function_type_list (long_integer_type_node,
714 type, NULL_TREE);
715 /* long long (*) (type) */
716 func_llround = build_function_type_list (long_long_integer_type_node,
717 type, NULL_TREE);
718 /* type (*) (type, type) */
719 func_2 = build_function_type_list (type, type, type, NULL_TREE);
720 /* type (*) (type, type, type) */
721 func_3 = build_function_type_list (type, type, type, type, NULL_TREE);
722 /* type (*) (type, &int) */
723 func_frexp
724 = build_function_type_list (type,
725 type,
726 build_pointer_type (integer_type_node),
727 NULL_TREE);
728 /* type (*) (type, int) */
729 func_scalbn = build_function_type_list (type,
730 type, integer_type_node, NULL_TREE);
731 /* type (*) (complex type) */
732 func_cabs = build_function_type_list (type, complex_type, NULL_TREE);
733 /* complex type (*) (complex type, complex type) */
734 func_cpow
735 = 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 quad_decls[BUILT_IN_SQRT]
763 = define_quad_builtin (gfc_real16_use_iec_60559
764 ? "sqrtf128" : "sqrtq", func_1, true);
765 }
766
767 /* Add GCC builtin functions. */
768 for (m = gfc_intrinsic_map;
769 m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
770 {
771 if (m->float_built_in != END_BUILTINS)
772 m->real4_decl = builtin_decl_explicit (m->float_built_in);
773 if (m->complex_float_built_in != END_BUILTINS)
774 m->complex4_decl = builtin_decl_explicit (m->complex_float_built_in);
775 if (m->double_built_in != END_BUILTINS)
776 m->real8_decl = builtin_decl_explicit (m->double_built_in);
777 if (m->complex_double_built_in != END_BUILTINS)
778 m->complex8_decl = builtin_decl_explicit (m->complex_double_built_in);
779
780 /* If real(kind=10) exists, it is always long double. */
781 if (m->long_double_built_in != END_BUILTINS)
782 m->real10_decl = builtin_decl_explicit (m->long_double_built_in);
783 if (m->complex_long_double_built_in != END_BUILTINS)
784 m->complex10_decl
785 = builtin_decl_explicit (m->complex_long_double_built_in);
786
787 if (!gfc_real16_is_float128)
788 {
789 if (m->long_double_built_in != END_BUILTINS)
790 m->real16_decl = builtin_decl_explicit (m->long_double_built_in);
791 if (m->complex_long_double_built_in != END_BUILTINS)
792 m->complex16_decl
793 = builtin_decl_explicit (m->complex_long_double_built_in);
794 }
795 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 m->real16_decl = quad_decls[m->double_built_in];
801 }
802 else if (quad_decls[m->complex_double_built_in] != NULL_TREE)
803 {
804 /* Same thing for the complex ones. */
805 m->complex16_decl = quad_decls[m->double_built_in];
806 }
807 }
808 }
809
810
811 /* Create a fndecl for a simple intrinsic library function. */
812
813 static tree
814 gfc_get_intrinsic_lib_fndecl (gfc_intrinsic_map_t * m, gfc_expr * expr)
815 {
816 tree type;
817 vec<tree, va_gc> *argtypes;
818 tree fndecl;
819 gfc_actual_arglist *actual;
820 tree *pdecl;
821 gfc_typespec *ts;
822 char name[GFC_MAX_SYMBOL_LEN + 3];
823
824 ts = &expr->ts;
825 if (ts->type == BT_REAL)
826 {
827 switch (ts->kind)
828 {
829 case 4:
830 pdecl = &m->real4_decl;
831 break;
832 case 8:
833 pdecl = &m->real8_decl;
834 break;
835 case 10:
836 pdecl = &m->real10_decl;
837 break;
838 case 16:
839 pdecl = &m->real16_decl;
840 break;
841 default:
842 gcc_unreachable ();
843 }
844 }
845 else if (ts->type == BT_COMPLEX)
846 {
847 gcc_assert (m->complex_available);
848
849 switch (ts->kind)
850 {
851 case 4:
852 pdecl = &m->complex4_decl;
853 break;
854 case 8:
855 pdecl = &m->complex8_decl;
856 break;
857 case 10:
858 pdecl = &m->complex10_decl;
859 break;
860 case 16:
861 pdecl = &m->complex16_decl;
862 break;
863 default:
864 gcc_unreachable ();
865 }
866 }
867 else
868 gcc_unreachable ();
869
870 if (*pdecl)
871 return *pdecl;
872
873 if (m->libm_name)
874 {
875 int n = gfc_validate_kind (BT_REAL, ts->kind, false);
876 if (gfc_real_kinds[n].c_float)
877 snprintf (name, sizeof (name), "%s%s%s",
878 ts->type == BT_COMPLEX ? "c" : "", m->name, "f");
879 else if (gfc_real_kinds[n].c_double)
880 snprintf (name, sizeof (name), "%s%s",
881 ts->type == BT_COMPLEX ? "c" : "", m->name);
882 else if (gfc_real_kinds[n].c_long_double)
883 snprintf (name, sizeof (name), "%s%s%s",
884 ts->type == BT_COMPLEX ? "c" : "", m->name, "l");
885 else if (gfc_real_kinds[n].c_float128)
886 snprintf (name, sizeof (name), "%s%s%s",
887 ts->type == BT_COMPLEX ? "c" : "", m->name,
888 gfc_real_kinds[n].use_iec_60559 ? "f128" : "q");
889 else
890 gcc_unreachable ();
891 }
892 else
893 {
894 snprintf (name, sizeof (name), PREFIX ("%s_%c%d"), m->name,
895 ts->type == BT_COMPLEX ? 'c' : 'r',
896 gfc_type_abi_kind (ts));
897 }
898
899 argtypes = NULL;
900 for (actual = expr->value.function.actual; actual; actual = actual->next)
901 {
902 type = gfc_typenode_for_spec (&actual->expr->ts);
903 vec_safe_push (argtypes, type);
904 }
905 type = build_function_type_vec (gfc_typenode_for_spec (ts), argtypes);
906 fndecl = build_decl (input_location,
907 FUNCTION_DECL, get_identifier (name), type);
908
909 /* Mark the decl as external. */
910 DECL_EXTERNAL (fndecl) = 1;
911 TREE_PUBLIC (fndecl) = 1;
912
913 /* Mark it __attribute__((const)), if possible. */
914 TREE_READONLY (fndecl) = m->is_constant;
915
916 rest_of_decl_compilation (fndecl, 1, 0);
917
918 (*pdecl) = fndecl;
919 return fndecl;
920 }
921
922
923 /* Convert an intrinsic function into an external or builtin call. */
924
925 static void
926 gfc_conv_intrinsic_lib_function (gfc_se * se, gfc_expr * expr)
927 {
928 gfc_intrinsic_map_t *m;
929 tree fndecl;
930 tree rettype;
931 tree *args;
932 unsigned int num_args;
933 gfc_isym_id id;
934
935 id = expr->value.function.isym->id;
936 /* Find the entry for this function. */
937 for (m = gfc_intrinsic_map;
938 m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
939 {
940 if (id == m->id)
941 break;
942 }
943
944 if (m->id == GFC_ISYM_NONE)
945 {
946 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 num_args = gfc_intrinsic_argument_list_length (expr);
952 args = XALLOCAVEC (tree, num_args);
953
954 gfc_conv_intrinsic_function_args (se, expr, args, num_args);
955 fndecl = gfc_get_intrinsic_lib_fndecl (m, expr);
956 rettype = TREE_TYPE (TREE_TYPE (fndecl));
957
958 fndecl = build_addr (fndecl);
959 se->expr = build_call_array_loc (input_location, rettype, fndecl, num_args, args);
960 }
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 gfc_trans_same_strlen_check (const char* intr_name, locus* where,
969 tree a, tree b, stmtblock_t* target)
970 {
971 tree cond;
972 tree name;
973
974 /* If bounds-checking is disabled, do nothing. */
975 if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
976 return;
977
978 /* Compare the two string lengths. */
979 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, a, b);
980
981 /* Output the runtime-check. */
982 name = gfc_build_cstring_const (intr_name);
983 name = gfc_build_addr_expr (pchar_type_node, name);
984 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 gfc_conv_intrinsic_exponent (gfc_se *se, gfc_expr *expr)
999 {
1000 tree arg, type, res, tmp, frexp, cond, huge;
1001 int i;
1002
1003 frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP,
1004 expr->value.function.actual->expr->ts.kind);
1005
1006 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
1007 arg = gfc_evaluate_now (arg, &se->pre);
1008
1009 i = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
1010 huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[i].huge, gfc_c_int_kind);
1011 cond = build_call_expr_loc (input_location,
1012 builtin_decl_explicit (BUILT_IN_ISFINITE),
1013 1, arg);
1014
1015 res = gfc_create_var (integer_type_node, NULL);
1016 tmp = build_call_expr_loc (input_location, frexp, 2, arg,
1017 gfc_build_addr_expr (NULL_TREE, res));
1018 tmp = fold_build2_loc (input_location, COMPOUND_EXPR, integer_type_node,
1019 tmp, res);
1020 se->expr = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
1021 cond, tmp, huge);
1022
1023 type = gfc_typenode_for_spec (&expr->ts);
1024 se->expr = fold_convert (type, se->expr);
1025 }
1026
1027
1028 static int caf_call_cnt = 0;
1029
1030 static tree
1031 conv_caf_func_index (stmtblock_t *block, gfc_namespace *ns, const char *pat,
1032 gfc_expr *hash)
1033 {
1034 char *name;
1035 gfc_se argse;
1036 gfc_expr func_index;
1037 gfc_symtree *index_st;
1038 tree func_index_tree;
1039 stmtblock_t blk;
1040
1041 /* Need to get namespace where static variables are possible. */
1042 while (ns && ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
1043 ns = ns->parent;
1044 gcc_assert (ns);
1045
1046 name = xasprintf (pat, caf_call_cnt);
1047 gcc_assert (!gfc_get_sym_tree (name, ns, &index_st, false));
1048 free (name);
1049
1050 index_st->n.sym->attr.flavor = FL_VARIABLE;
1051 index_st->n.sym->attr.save = SAVE_EXPLICIT;
1052 index_st->n.sym->value
1053 = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
1054 &gfc_current_locus);
1055 mpz_init_set_si (index_st->n.sym->value->value.integer, -1);
1056 index_st->n.sym->ts.type = BT_INTEGER;
1057 index_st->n.sym->ts.kind = gfc_default_integer_kind;
1058 gfc_set_sym_referenced (index_st->n.sym);
1059 memset (&func_index, 0, sizeof (gfc_expr));
1060 gfc_clear_ts (&func_index.ts);
1061 func_index.expr_type = EXPR_VARIABLE;
1062 func_index.symtree = index_st;
1063 func_index.ts = index_st->n.sym->ts;
1064 gfc_commit_symbol (index_st->n.sym);
1065
1066 gfc_init_se (&argse, NULL);
1067 gfc_conv_expr (&argse, &func_index);
1068 gfc_add_block_to_block (block, &argse.pre);
1069 func_index_tree = argse.expr;
1070
1071 gfc_init_se (&argse, NULL);
1072 gfc_conv_expr (&argse, hash);
1073
1074 gfc_init_block (&blk);
1075 gfc_add_modify (&blk, func_index_tree,
1076 build_call_expr (gfor_fndecl_caf_get_remote_function_index, 1,
1077 argse.expr));
1078 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 return func_index_tree;
1087 }
1088
1089 static tree
1090 conv_caf_add_call_data (stmtblock_t *blk, gfc_namespace *ns, const char *pat,
1091 gfc_symbol *data_sym, tree *data_size)
1092 {
1093 char *name;
1094 gfc_symtree *data_st;
1095 gfc_constructor *con;
1096 gfc_expr data, data_init;
1097 gfc_se argse;
1098 tree data_tree;
1099
1100 memset (&data, 0, sizeof (gfc_expr));
1101 gfc_clear_ts (&data.ts);
1102 data.expr_type = EXPR_VARIABLE;
1103 name = xasprintf (pat, caf_call_cnt);
1104 gcc_assert (!gfc_get_sym_tree (name, ns, &data_st, false));
1105 free (name);
1106 data_st->n.sym->attr.flavor = FL_VARIABLE;
1107 data_st->n.sym->ts = data_sym->ts;
1108 data.symtree = data_st;
1109 gfc_set_sym_referenced (data.symtree->n.sym);
1110 data.ts = data_st->n.sym->ts;
1111 gfc_commit_symbol (data_st->n.sym);
1112
1113 memset (&data_init, 0, sizeof (gfc_expr));
1114 gfc_clear_ts (&data_init.ts);
1115 data_init.expr_type = EXPR_STRUCTURE;
1116 data_init.ts = data.ts;
1117 for (gfc_component *comp = data.ts.u.derived->components; comp;
1118 comp = comp->next)
1119 {
1120 con = gfc_constructor_get ();
1121 con->expr = comp->initializer;
1122 comp->initializer = NULL;
1123 gfc_constructor_append (&data_init.value.constructor, con);
1124 }
1125
1126 if (data.ts.u.derived->components)
1127 {
1128 gfc_init_se (&argse, NULL);
1129 gfc_conv_expr (&argse, &data);
1130 data_tree = argse.expr;
1131 gfc_add_expr_to_block (blk,
1132 gfc_trans_structure_assign (data_tree, &data_init,
1133 true, true));
1134 gfc_constructor_free (data_init.value.constructor);
1135 *data_size = TREE_TYPE (data_tree)->type_common.size_unit;
1136 data_tree = gfc_build_addr_expr (pvoid_type_node, data_tree);
1137 }
1138 else
1139 {
1140 data_tree = build_zero_cst (pvoid_type_node);
1141 *data_size = build_zero_cst (size_type_node);
1142 }
1143
1144 return data_tree;
1145 }
1146
1147 static tree
1148 conv_shape_to_cst (gfc_expr *e)
1149 {
1150 tree tmp = NULL;
1151 for (int d = 0; d < e->rank; ++d)
1152 {
1153 if (!tmp)
1154 tmp = gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind);
1155 else
1156 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (tmp), tmp,
1157 gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind));
1158 }
1159 return fold_convert (size_type_node, tmp);
1160 }
1161
1162 static void
1163 conv_stat_and_team (stmtblock_t *block, gfc_expr *expr, tree *stat, tree *team,
1164 tree *team_no)
1165 {
1166 gfc_expr *stat_e, *team_e;
1167
1168 stat_e = gfc_find_stat_co (expr);
1169 if (stat_e)
1170 {
1171 gfc_se stat_se;
1172 gfc_init_se (&stat_se, NULL);
1173 gfc_conv_expr_reference (&stat_se, stat_e);
1174 *stat = stat_se.expr;
1175 gfc_add_block_to_block (block, &stat_se.pre);
1176 gfc_add_block_to_block (block, &stat_se.post);
1177 }
1178 else
1179 *stat = null_pointer_node;
1180
1181 team_e = gfc_find_team_co (expr, TEAM_TEAM);
1182 if (team_e)
1183 {
1184 gfc_se team_se;
1185 gfc_init_se (&team_se, NULL);
1186 gfc_conv_expr_reference (&team_se, team_e);
1187 *team = team_se.expr;
1188 gfc_add_block_to_block (block, &team_se.pre);
1189 gfc_add_block_to_block (block, &team_se.post);
1190 }
1191 else
1192 *team = null_pointer_node;
1193
1194 team_e = gfc_find_team_co (expr, TEAM_NUMBER);
1195 if (team_e)
1196 {
1197 gfc_se team_se;
1198 gfc_init_se (&team_se, NULL);
1199 gfc_conv_expr_reference (&team_se, team_e);
1200 *team_no = team_se.expr;
1201 gfc_add_block_to_block (block, &team_se.pre);
1202 gfc_add_block_to_block (block, &team_se.post);
1203 }
1204 else
1205 *team_no = null_pointer_node;
1206 }
1207
1208 /* Get data from a remote coarray. */
1209
1210 static void
1211 gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs,
1212 bool may_realloc, symbol_attribute *caf_attr)
1213 {
1214 gfc_expr *array_expr;
1215 tree caf_decl, token, image_index, tmp, res_var, type, stat, dest_size,
1216 dest_data, opt_dest_desc, get_fn_index_tree, add_data_tree, add_data_size,
1217 opt_src_desc, opt_src_charlen, opt_dest_charlen, team, team_no;
1218 symbol_attribute caf_attr_store;
1219 gfc_namespace *ns;
1220 gfc_expr *get_fn_hash = expr->value.function.actual->next->expr,
1221 *get_fn_expr = expr->value.function.actual->next->next->expr;
1222 gfc_symbol *add_data_sym = get_fn_expr->symtree->n.sym->formal->sym;
1223
1224 gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1225
1226 if (se->ss && se->ss->info->useflags)
1227 {
1228 /* Access the previously obtained result. */
1229 gfc_conv_tmp_array_ref (se);
1230 return;
1231 }
1232
1233 array_expr = expr->value.function.actual->expr;
1234 ns = array_expr->expr_type == EXPR_VARIABLE
1235 && !array_expr->symtree->n.sym->attr.associate_var
1236 ? array_expr->symtree->n.sym->ns
1237 : gfc_current_ns;
1238 type = gfc_typenode_for_spec (&array_expr->ts);
1239
1240 if (caf_attr == NULL)
1241 {
1242 caf_attr_store = gfc_caf_attr (array_expr);
1243 caf_attr = &caf_attr_store;
1244 }
1245
1246 res_var = lhs;
1247
1248 conv_stat_and_team (&se->pre, expr, &stat, &team, &team_no);
1249
1250 get_fn_index_tree
1251 = conv_caf_func_index (&se->pre, ns, "__caf_get_from_remote_fn_index_%d",
1252 get_fn_hash);
1253 add_data_tree
1254 = conv_caf_add_call_data (&se->pre, ns, "__caf_get_from_remote_add_data_%d",
1255 add_data_sym, &add_data_size);
1256 ++caf_call_cnt;
1257
1258 if (array_expr->rank == 0)
1259 {
1260 res_var = gfc_create_var (type, "caf_res");
1261 if (array_expr->ts.type == BT_CHARACTER)
1262 {
1263 gfc_conv_string_length (array_expr->ts.u.cl, array_expr, &se->pre);
1264 se->string_length = array_expr->ts.u.cl->backend_decl;
1265 opt_src_charlen = gfc_build_addr_expr (
1266 NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1267 dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1268 }
1269 else
1270 {
1271 dest_size = res_var->typed.type->type_common.size_unit;
1272 opt_src_charlen
1273 = build_zero_cst (build_pointer_type (size_type_node));
1274 }
1275 dest_data
1276 = gfc_evaluate_now (gfc_build_addr_expr (NULL_TREE, res_var), &se->pre);
1277 res_var = build_fold_indirect_ref (dest_data);
1278 dest_data = gfc_build_addr_expr (pvoid_type_node, dest_data);
1279 opt_dest_desc = build_zero_cst (pvoid_type_node);
1280 }
1281 else
1282 {
1283 /* Create temporary. */
1284 may_realloc = gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
1285 type, NULL_TREE, false, false,
1286 false, &array_expr->where)
1287 == NULL_TREE;
1288 res_var = se->ss->info->data.array.descriptor;
1289 if (array_expr->ts.type == BT_CHARACTER)
1290 {
1291 se->string_length = array_expr->ts.u.cl->backend_decl;
1292 opt_src_charlen = gfc_build_addr_expr (
1293 NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1294 dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1295 }
1296 else
1297 {
1298 opt_src_charlen
1299 = build_zero_cst (build_pointer_type (size_type_node));
1300 dest_size = fold_build2 (
1301 MULT_EXPR, size_type_node,
1302 fold_convert (size_type_node,
1303 array_expr->shape
1304 ? conv_shape_to_cst (array_expr)
1305 : gfc_conv_descriptor_size (res_var,
1306 array_expr->rank)),
1307 fold_convert (size_type_node,
1308 gfc_conv_descriptor_span_get (res_var)));
1309 }
1310 opt_dest_desc = res_var;
1311 dest_data = gfc_conv_descriptor_data_get (res_var);
1312 opt_dest_desc = gfc_build_addr_expr (NULL_TREE, opt_dest_desc);
1313 if (may_realloc)
1314 {
1315 tmp = gfc_conv_descriptor_data_get (res_var);
1316 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
1317 NULL_TREE, NULL_TREE, true, NULL,
1318 GFC_CAF_COARRAY_NOCOARRAY);
1319 gfc_add_expr_to_block (&se->post, tmp);
1320 }
1321 dest_data
1322 = gfc_build_addr_expr (NULL_TREE,
1323 gfc_trans_force_lval (&se->pre, dest_data));
1324 }
1325
1326 opt_dest_charlen = opt_src_charlen;
1327 caf_decl = gfc_get_tree_for_caf_expr (array_expr);
1328 if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1329 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1330
1331 if (!TYPE_LANG_SPECIFIC (TREE_TYPE (caf_decl))->rank
1332 || GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl)))
1333 opt_src_desc = build_zero_cst (pvoid_type_node);
1334 else
1335 opt_src_desc = gfc_build_addr_expr (pvoid_type_node, caf_decl);
1336
1337 image_index = gfc_caf_get_image_index (&se->pre, array_expr, caf_decl);
1338 gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, array_expr);
1339
1340 /* It guarantees memory consistency within the same segment. */
1341 tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1342 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1343 gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1344 tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1345 ASM_VOLATILE_P (tmp) = 1;
1346 gfc_add_expr_to_block (&se->pre, tmp);
1347
1348 tmp = build_call_expr_loc (
1349 input_location, gfor_fndecl_caf_get_from_remote, 15, token, opt_src_desc,
1350 opt_src_charlen, image_index, dest_size, dest_data, opt_dest_charlen,
1351 opt_dest_desc, constant_boolean_node (may_realloc, boolean_type_node),
1352 get_fn_index_tree, add_data_tree, add_data_size, stat, team, team_no);
1353
1354 gfc_add_expr_to_block (&se->pre, tmp);
1355
1356 if (se->ss)
1357 gfc_advance_se_ss_chain (se);
1358
1359 se->expr = res_var;
1360
1361 return;
1362 }
1363
1364 /* Generate call to caf_is_present_on_remote for allocated (coarrary[...])
1365 calls. */
1366
1367 static void
1368 gfc_conv_intrinsic_caf_is_present_remote (gfc_se *se, gfc_expr *e)
1369 {
1370 gfc_expr *caf_expr, *hash, *present_fn;
1371 gfc_symbol *add_data_sym;
1372 tree fn_index, add_data_tree, add_data_size, caf_decl, image_index, token;
1373
1374 gcc_assert (e->expr_type == EXPR_FUNCTION
1375 && e->value.function.isym->id
1376 == GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE);
1377 caf_expr = e->value.function.actual->expr;
1378 hash = e->value.function.actual->next->expr;
1379 present_fn = e->value.function.actual->next->next->expr;
1380 add_data_sym = present_fn->symtree->n.sym->formal->sym;
1381
1382 fn_index = conv_caf_func_index (&se->pre, gfc_current_ns,
1383 "__caf_present_on_remote_fn_index_%d", hash);
1384 add_data_tree = conv_caf_add_call_data (&se->pre, gfc_current_ns,
1385 "__caf_present_on_remote_add_data_%d",
1386 add_data_sym, &add_data_size);
1387 ++caf_call_cnt;
1388
1389 caf_decl = gfc_get_tree_for_caf_expr (caf_expr);
1390 if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1391 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1392
1393 image_index = gfc_caf_get_image_index (&se->pre, caf_expr, caf_decl);
1394 gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, caf_expr);
1395
1396 se->expr
1397 = fold_convert (logical_type_node,
1398 build_call_expr_loc (input_location,
1399 gfor_fndecl_caf_is_present_on_remote,
1400 5, token, image_index, fn_index,
1401 add_data_tree, add_data_size));
1402 }
1403
1404 static tree
1405 conv_caf_send_to_remote (gfc_code *code)
1406 {
1407 gfc_expr *lhs_expr, *rhs_expr, *lhs_hash, *receiver_fn_expr;
1408 gfc_symbol *add_data_sym;
1409 gfc_se lhs_se, rhs_se;
1410 stmtblock_t block;
1411 gfc_namespace *ns;
1412 tree caf_decl, token, rhs_size, image_index, tmp, rhs_data;
1413 tree lhs_stat, lhs_team, lhs_team_no, opt_lhs_charlen, opt_rhs_charlen;
1414 tree opt_lhs_desc = NULL_TREE, opt_rhs_desc = NULL_TREE;
1415 tree receiver_fn_index_tree, add_data_tree, add_data_size;
1416
1417 gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1418 gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SEND);
1419
1420 lhs_expr = code->ext.actual->expr;
1421 rhs_expr = code->ext.actual->next->expr;
1422 lhs_hash = code->ext.actual->next->next->expr;
1423 receiver_fn_expr = code->ext.actual->next->next->next->expr;
1424 add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1425
1426 ns = lhs_expr->expr_type == EXPR_VARIABLE
1427 && !lhs_expr->symtree->n.sym->attr.associate_var
1428 ? lhs_expr->symtree->n.sym->ns
1429 : gfc_current_ns;
1430
1431 gfc_init_block (&block);
1432
1433 /* LHS. */
1434 gfc_init_se (&lhs_se, NULL);
1435 caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1436 if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1437 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1438 if (lhs_expr->rank == 0)
1439 {
1440 if (lhs_expr->ts.type == BT_CHARACTER)
1441 {
1442 gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1443 lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1444 opt_lhs_charlen = gfc_build_addr_expr (
1445 NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1446 }
1447 else
1448 opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1449 opt_lhs_desc = null_pointer_node;
1450 }
1451 else
1452 {
1453 gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1454 gfc_add_block_to_block (&block, &lhs_se.pre);
1455 opt_lhs_desc = lhs_se.expr;
1456 if (lhs_expr->ts.type == BT_CHARACTER)
1457 opt_lhs_charlen = gfc_build_addr_expr (
1458 NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1459 else
1460 opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1461 /* Get the third formal argument of the receiver function. (This is the
1462 location where to put the data on the remote image.) Need to look at
1463 the argument in the function decl, because in the gfc_symbol's formal
1464 argument an array may have no descriptor while in the generated
1465 function decl it has. */
1466 tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1467 TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1468 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1469 opt_lhs_desc = null_pointer_node;
1470 else
1471 opt_lhs_desc
1472 = gfc_build_addr_expr (NULL_TREE,
1473 gfc_trans_force_lval (&block, opt_lhs_desc));
1474 }
1475
1476 /* Obtain token, offset and image index for the LHS. */
1477 image_index = gfc_caf_get_image_index (&block, lhs_expr, caf_decl);
1478 gfc_get_caf_token_offset (&lhs_se, &token, NULL, caf_decl, NULL, lhs_expr);
1479
1480 /* RHS. */
1481 gfc_init_se (&rhs_se, NULL);
1482 if (rhs_expr->rank == 0)
1483 {
1484 rhs_se.want_pointer = rhs_expr->ts.type == BT_CHARACTER;
1485 gfc_conv_expr (&rhs_se, rhs_expr);
1486 gfc_add_block_to_block (&block, &rhs_se.pre);
1487 opt_rhs_desc = null_pointer_node;
1488 if (rhs_expr->ts.type == BT_CHARACTER)
1489 {
1490 rhs_data
1491 = rhs_expr->expr_type == EXPR_CONSTANT
1492 ? gfc_build_addr_expr (NULL_TREE,
1493 gfc_trans_force_lval (&block,
1494 rhs_se.expr))
1495 : rhs_se.expr;
1496 opt_rhs_charlen = gfc_build_addr_expr (
1497 NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1498 rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1499 }
1500 else
1501 {
1502 rhs_data
1503 = gfc_build_addr_expr (NULL_TREE,
1504 gfc_trans_force_lval (&block, rhs_se.expr));
1505 opt_rhs_charlen
1506 = build_zero_cst (build_pointer_type (size_type_node));
1507 rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1508 }
1509 }
1510 else
1511 {
1512 rhs_se.force_tmp = rhs_expr->shape == NULL
1513 || !gfc_is_simply_contiguous (rhs_expr, false, false);
1514 gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1515 gfc_add_block_to_block (&block, &rhs_se.pre);
1516 opt_rhs_desc = rhs_se.expr;
1517 if (rhs_expr->ts.type == BT_CHARACTER)
1518 {
1519 opt_rhs_charlen = gfc_build_addr_expr (
1520 NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1521 rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1522 }
1523 else
1524 {
1525 opt_rhs_charlen
1526 = build_zero_cst (build_pointer_type (size_type_node));
1527 rhs_size = fold_build2 (
1528 MULT_EXPR, size_type_node,
1529 fold_convert (size_type_node,
1530 rhs_expr->shape
1531 ? conv_shape_to_cst (rhs_expr)
1532 : gfc_conv_descriptor_size (rhs_se.expr,
1533 rhs_expr->rank)),
1534 fold_convert (size_type_node,
1535 gfc_conv_descriptor_span_get (rhs_se.expr)));
1536 }
1537
1538 rhs_data = gfc_build_addr_expr (
1539 NULL_TREE, gfc_trans_force_lval (&block, gfc_conv_descriptor_data_get (
1540 opt_rhs_desc)));
1541 opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1542 }
1543 gfc_add_block_to_block (&block, &rhs_se.pre);
1544
1545 conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1546
1547 receiver_fn_index_tree
1548 = conv_caf_func_index (&block, ns, "__caf_send_to_remote_fn_index_%d",
1549 lhs_hash);
1550 add_data_tree
1551 = conv_caf_add_call_data (&block, ns, "__caf_send_to_remote_add_data_%d",
1552 add_data_sym, &add_data_size);
1553 ++caf_call_cnt;
1554
1555 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_send_to_remote, 14,
1556 token, opt_lhs_desc, opt_lhs_charlen, image_index,
1557 rhs_size, rhs_data, opt_rhs_charlen, opt_rhs_desc,
1558 receiver_fn_index_tree, add_data_tree,
1559 add_data_size, lhs_stat, lhs_team, lhs_team_no);
1560
1561 gfc_add_expr_to_block (&block, tmp);
1562 gfc_add_block_to_block (&block, &lhs_se.post);
1563 gfc_add_block_to_block (&block, &rhs_se.post);
1564
1565 /* It guarantees memory consistency within the same segment. */
1566 tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1567 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1568 gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1569 tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1570 ASM_VOLATILE_P (tmp) = 1;
1571 gfc_add_expr_to_block (&block, tmp);
1572
1573 return gfc_finish_block (&block);
1574 }
1575
1576 /* Send-get data to a remote coarray. */
1577
1578 static tree
1579 conv_caf_sendget (gfc_code *code)
1580 {
1581 /* lhs stuff */
1582 gfc_expr *lhs_expr, *lhs_hash, *receiver_fn_expr;
1583 gfc_symbol *lhs_add_data_sym;
1584 gfc_se lhs_se;
1585 tree lhs_caf_decl, lhs_token, opt_lhs_charlen,
1586 opt_lhs_desc = NULL_TREE, receiver_fn_index_tree, lhs_image_index,
1587 lhs_add_data_tree, lhs_add_data_size, lhs_stat, lhs_team, lhs_team_no;
1588 int transfer_rank;
1589
1590 /* rhs stuff */
1591 gfc_expr *rhs_expr, *rhs_hash, *sender_fn_expr;
1592 gfc_symbol *rhs_add_data_sym;
1593 gfc_se rhs_se;
1594 tree rhs_caf_decl, rhs_token, opt_rhs_charlen,
1595 opt_rhs_desc = NULL_TREE, sender_fn_index_tree, rhs_image_index,
1596 rhs_add_data_tree, rhs_add_data_size, rhs_stat, rhs_team, rhs_team_no;
1597
1598 /* shared */
1599 stmtblock_t block;
1600 gfc_namespace *ns;
1601 tree tmp, rhs_size;
1602
1603 gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1604 gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SENDGET);
1605
1606 lhs_expr = code->ext.actual->expr;
1607 rhs_expr = code->ext.actual->next->expr;
1608 lhs_hash = code->ext.actual->next->next->expr;
1609 receiver_fn_expr = code->ext.actual->next->next->next->expr;
1610 rhs_hash = code->ext.actual->next->next->next->next->expr;
1611 sender_fn_expr = code->ext.actual->next->next->next->next->next->expr;
1612
1613 lhs_add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1614 rhs_add_data_sym = sender_fn_expr->symtree->n.sym->formal->sym;
1615
1616 ns = lhs_expr->expr_type == EXPR_VARIABLE
1617 && !lhs_expr->symtree->n.sym->attr.associate_var
1618 ? lhs_expr->symtree->n.sym->ns
1619 : gfc_current_ns;
1620
1621 gfc_init_block (&block);
1622
1623 lhs_stat = null_pointer_node;
1624 lhs_team = null_pointer_node;
1625 rhs_stat = null_pointer_node;
1626 rhs_team = null_pointer_node;
1627
1628 /* LHS. */
1629 gfc_init_se (&lhs_se, NULL);
1630 lhs_caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1631 if (TREE_CODE (TREE_TYPE (lhs_caf_decl)) == REFERENCE_TYPE)
1632 lhs_caf_decl = build_fold_indirect_ref_loc (input_location, lhs_caf_decl);
1633 if (lhs_expr->rank == 0)
1634 {
1635 if (lhs_expr->ts.type == BT_CHARACTER)
1636 {
1637 gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1638 lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1639 opt_lhs_charlen = gfc_build_addr_expr (
1640 NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1641 }
1642 else
1643 opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1644 opt_lhs_desc = null_pointer_node;
1645 }
1646 else
1647 {
1648 gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1649 gfc_add_block_to_block (&block, &lhs_se.pre);
1650 opt_lhs_desc = lhs_se.expr;
1651 if (lhs_expr->ts.type == BT_CHARACTER)
1652 opt_lhs_charlen = gfc_build_addr_expr (
1653 NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1654 else
1655 opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1656 /* Get the third formal argument of the receiver function. (This is the
1657 location where to put the data on the remote image.) Need to look at
1658 the argument in the function decl, because in the gfc_symbol's formal
1659 argument an array may have no descriptor while in the generated
1660 function decl it has. */
1661 tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1662 TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1663 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1664 opt_lhs_desc = null_pointer_node;
1665 else
1666 opt_lhs_desc
1667 = gfc_build_addr_expr (NULL_TREE,
1668 gfc_trans_force_lval (&block, opt_lhs_desc));
1669 }
1670
1671 /* Obtain token, offset and image index for the LHS. */
1672 lhs_image_index = gfc_caf_get_image_index (&block, lhs_expr, lhs_caf_decl);
1673 gfc_get_caf_token_offset (&lhs_se, &lhs_token, NULL, lhs_caf_decl, NULL,
1674 lhs_expr);
1675
1676 /* RHS. */
1677 rhs_caf_decl = gfc_get_tree_for_caf_expr (rhs_expr);
1678 if (TREE_CODE (TREE_TYPE (rhs_caf_decl)) == REFERENCE_TYPE)
1679 rhs_caf_decl = build_fold_indirect_ref_loc (input_location, rhs_caf_decl);
1680 transfer_rank = rhs_expr->rank;
1681 gfc_expression_rank (rhs_expr);
1682 gfc_init_se (&rhs_se, NULL);
1683 if (rhs_expr->rank == 0)
1684 {
1685 opt_rhs_desc = null_pointer_node;
1686 if (rhs_expr->ts.type == BT_CHARACTER)
1687 {
1688 gfc_conv_expr (&rhs_se, rhs_expr);
1689 gfc_add_block_to_block (&block, &rhs_se.pre);
1690 opt_rhs_charlen = gfc_build_addr_expr (
1691 NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1692 rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1693 }
1694 else
1695 {
1696 gfc_typespec *ts
1697 = &sender_fn_expr->symtree->n.sym->formal->next->next->sym->ts;
1698
1699 opt_rhs_charlen
1700 = build_zero_cst (build_pointer_type (size_type_node));
1701 rhs_size = gfc_typenode_for_spec (ts)->type_common.size_unit;
1702 }
1703 }
1704 /* Get the fifth formal argument of the getter function. This is the argument
1705 pointing to the data to get on the remote image. Need to look at the
1706 argument in the function decl, because in the gfc_symbol's formal argument
1707 an array may have no descriptor while in the generated function decl it
1708 has. */
1709 else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_VALUE (
1710 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1711 TREE_TYPE (sender_fn_expr->symtree->n.sym->backend_decl))))))))))
1712 {
1713 rhs_se.data_not_needed = 1;
1714 gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1715 gfc_add_block_to_block (&block, &rhs_se.pre);
1716 if (rhs_expr->ts.type == BT_CHARACTER)
1717 {
1718 opt_rhs_charlen = gfc_build_addr_expr (
1719 NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1720 rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1721 }
1722 else
1723 {
1724 opt_rhs_charlen
1725 = build_zero_cst (build_pointer_type (size_type_node));
1726 rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1727 }
1728 opt_rhs_desc = null_pointer_node;
1729 }
1730 else
1731 {
1732 gfc_ref *arr_ref = rhs_expr->ref;
1733 while (arr_ref && arr_ref->type != REF_ARRAY)
1734 arr_ref = arr_ref->next;
1735 rhs_se.force_tmp
1736 = (rhs_expr->shape == NULL
1737 && (!arr_ref || !gfc_full_array_ref_p (arr_ref, nullptr)))
1738 || !gfc_is_simply_contiguous (rhs_expr, false, false);
1739 gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1740 gfc_add_block_to_block (&block, &rhs_se.pre);
1741 opt_rhs_desc = rhs_se.expr;
1742 if (rhs_expr->ts.type == BT_CHARACTER)
1743 {
1744 opt_rhs_charlen = gfc_build_addr_expr (
1745 NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1746 rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1747 }
1748 else
1749 {
1750 opt_rhs_charlen
1751 = build_zero_cst (build_pointer_type (size_type_node));
1752 rhs_size = fold_build2 (
1753 MULT_EXPR, size_type_node,
1754 fold_convert (size_type_node,
1755 rhs_expr->shape
1756 ? conv_shape_to_cst (rhs_expr)
1757 : gfc_conv_descriptor_size (rhs_se.expr,
1758 rhs_expr->rank)),
1759 fold_convert (size_type_node,
1760 gfc_conv_descriptor_span_get (rhs_se.expr)));
1761 }
1762
1763 opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1764 }
1765 gfc_add_block_to_block (&block, &rhs_se.pre);
1766
1767 /* Obtain token, offset and image index for the RHS. */
1768 rhs_image_index = gfc_caf_get_image_index (&block, rhs_expr, rhs_caf_decl);
1769 gfc_get_caf_token_offset (&rhs_se, &rhs_token, NULL, rhs_caf_decl, NULL,
1770 rhs_expr);
1771
1772 /* stat and team. */
1773 conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1774 conv_stat_and_team (&block, rhs_expr, &rhs_stat, &rhs_team, &rhs_team_no);
1775
1776 sender_fn_index_tree
1777 = conv_caf_func_index (&block, ns, "__caf_transfer_from_fn_index_%d",
1778 rhs_hash);
1779 rhs_add_data_tree
1780 = conv_caf_add_call_data (&block, ns,
1781 "__caf_transfer_from_remote_add_data_%d",
1782 rhs_add_data_sym, &rhs_add_data_size);
1783 receiver_fn_index_tree
1784 = conv_caf_func_index (&block, ns, "__caf_transfer_to_remote_fn_index_%d",
1785 lhs_hash);
1786 lhs_add_data_tree
1787 = conv_caf_add_call_data (&block, ns,
1788 "__caf_transfer_to_remote_add_data_%d",
1789 lhs_add_data_sym, &lhs_add_data_size);
1790 ++caf_call_cnt;
1791
1792 tmp = build_call_expr_loc (
1793 input_location, gfor_fndecl_caf_transfer_between_remotes, 20, lhs_token,
1794 opt_lhs_desc, opt_lhs_charlen, lhs_image_index, receiver_fn_index_tree,
1795 lhs_add_data_tree, lhs_add_data_size, rhs_token, opt_rhs_desc,
1796 opt_rhs_charlen, rhs_image_index, sender_fn_index_tree, rhs_add_data_tree,
1797 rhs_add_data_size, rhs_size,
1798 transfer_rank == 0 ? boolean_true_node : boolean_false_node, lhs_stat,
1799 lhs_team, lhs_team_no, rhs_stat, rhs_team, rhs_team_no);
1800
1801 gfc_add_expr_to_block (&block, tmp);
1802 gfc_add_block_to_block (&block, &lhs_se.post);
1803 gfc_add_block_to_block (&block, &rhs_se.post);
1804
1805 /* It guarantees memory consistency within the same segment. */
1806 tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1807 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1808 gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1809 tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1810 ASM_VOLATILE_P (tmp) = 1;
1811 gfc_add_expr_to_block (&block, tmp);
1812
1813 return gfc_finish_block (&block);
1814 }
1815
1816
1817 static void
1818 trans_this_image (gfc_se * se, gfc_expr *expr)
1819 {
1820 stmtblock_t loop;
1821 tree type, desc, dim_arg, cond, tmp, m, loop_var, exit_label, min_var,
1822 lbound, ubound, extent, ml;
1823 gfc_se argse;
1824 int rank, corank;
1825 gfc_expr *distance = expr->value.function.actual->next->next->expr;
1826
1827 if (expr->value.function.actual->expr
1828 && !gfc_is_coarray (expr->value.function.actual->expr))
1829 distance = expr->value.function.actual->expr;
1830
1831 /* The case -fcoarray=single is handled elsewhere. */
1832 gcc_assert (flag_coarray != GFC_FCOARRAY_SINGLE);
1833
1834 /* Argument-free version: THIS_IMAGE(). */
1835 if (distance || expr->value.function.actual->expr == NULL)
1836 {
1837 if (distance)
1838 {
1839 gfc_init_se (&argse, NULL);
1840 gfc_conv_expr_val (&argse, distance);
1841 gfc_add_block_to_block (&se->pre, &argse.pre);
1842 gfc_add_block_to_block (&se->post, &argse.post);
1843 tmp = fold_convert (integer_type_node, argse.expr);
1844 }
1845 else
1846 tmp = integer_zero_node;
1847 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
1848 tmp);
1849 se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
1850 tmp);
1851 return;
1852 }
1853
1854 /* Coarray-argument version: THIS_IMAGE(coarray [, dim]). */
1855
1856 type = gfc_get_int_type (gfc_default_integer_kind);
1857 corank = expr->value.function.actual->expr->corank;
1858 rank = expr->value.function.actual->expr->rank;
1859
1860 /* Obtain the descriptor of the COARRAY. */
1861 gfc_init_se (&argse, NULL);
1862 argse.want_coarray = 1;
1863 gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
1864 gfc_add_block_to_block (&se->pre, &argse.pre);
1865 gfc_add_block_to_block (&se->post, &argse.post);
1866 desc = argse.expr;
1867
1868 if (se->ss)
1869 {
1870 /* Create an implicit second parameter from the loop variable. */
1871 gcc_assert (!expr->value.function.actual->next->expr);
1872 gcc_assert (corank > 0);
1873 gcc_assert (se->loop->dimen == 1);
1874 gcc_assert (se->ss->info->expr == expr);
1875
1876 dim_arg = se->loop->loopvar[0];
1877 dim_arg = fold_build2_loc (input_location, PLUS_EXPR,
1878 gfc_array_index_type, dim_arg,
1879 build_int_cst (TREE_TYPE (dim_arg), 1));
1880 gfc_advance_se_ss_chain (se);
1881 }
1882 else
1883 {
1884 /* Use the passed DIM= argument. */
1885 gcc_assert (expr->value.function.actual->next->expr);
1886 gfc_init_se (&argse, NULL);
1887 gfc_conv_expr_type (&argse, expr->value.function.actual->next->expr,
1888 gfc_array_index_type);
1889 gfc_add_block_to_block (&se->pre, &argse.pre);
1890 dim_arg = argse.expr;
1891
1892 if (INTEGER_CST_P (dim_arg))
1893 {
1894 if (wi::ltu_p (wi::to_wide (dim_arg), 1)
1895 || wi::gtu_p (wi::to_wide (dim_arg),
1896 GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
1897 gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
1898 "dimension index", expr->value.function.isym->name,
1899 &expr->where);
1900 }
1901 else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1902 {
1903 dim_arg = gfc_evaluate_now (dim_arg, &se->pre);
1904 cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1905 dim_arg,
1906 build_int_cst (TREE_TYPE (dim_arg), 1));
1907 tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
1908 tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
1909 dim_arg, tmp);
1910 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
1911 logical_type_node, cond, tmp);
1912 gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
1913 gfc_msg_fault);
1914 }
1915 }
1916
1917 /* Used algorithm; cf. Fortran 2008, C.10. Note, due to the scalarizer,
1918 one always has a dim_arg argument.
1919
1920 m = this_image() - 1
1921 if (corank == 1)
1922 {
1923 sub(1) = m + lcobound(corank)
1924 return;
1925 }
1926 i = rank
1927 min_var = min (rank + corank - 2, rank + dim_arg - 1)
1928 for (;;)
1929 {
1930 extent = gfc_extent(i)
1931 ml = m
1932 m = m/extent
1933 if (i >= min_var)
1934 goto exit_label
1935 i++
1936 }
1937 exit_label:
1938 sub(dim_arg) = (dim_arg < corank) ? ml - m*extent + lcobound(dim_arg)
1939 : m + lcobound(corank)
1940 */
1941
1942 /* this_image () - 1. */
1943 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
1944 integer_zero_node);
1945 tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
1946 fold_convert (type, tmp), build_int_cst (type, 1));
1947 if (corank == 1)
1948 {
1949 /* sub(1) = m + lcobound(corank). */
1950 lbound = gfc_conv_descriptor_lbound_get (desc,
1951 build_int_cst (TREE_TYPE (gfc_array_index_type),
1952 corank+rank-1));
1953 lbound = fold_convert (type, lbound);
1954 tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
1955
1956 se->expr = tmp;
1957 return;
1958 }
1959
1960 m = gfc_create_var (type, NULL);
1961 ml = gfc_create_var (type, NULL);
1962 loop_var = gfc_create_var (integer_type_node, NULL);
1963 min_var = gfc_create_var (integer_type_node, NULL);
1964
1965 /* m = this_image () - 1. */
1966 gfc_add_modify (&se->pre, m, tmp);
1967
1968 /* min_var = min (rank + corank-2, rank + dim_arg - 1). */
1969 tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1970 fold_convert (integer_type_node, dim_arg),
1971 build_int_cst (integer_type_node, rank - 1));
1972 tmp = fold_build2_loc (input_location, MIN_EXPR, integer_type_node,
1973 build_int_cst (integer_type_node, rank + corank - 2),
1974 tmp);
1975 gfc_add_modify (&se->pre, min_var, tmp);
1976
1977 /* i = rank. */
1978 tmp = build_int_cst (integer_type_node, rank);
1979 gfc_add_modify (&se->pre, loop_var, tmp);
1980
1981 exit_label = gfc_build_label_decl (NULL_TREE);
1982 TREE_USED (exit_label) = 1;
1983
1984 /* Loop body. */
1985 gfc_init_block (&loop);
1986
1987 /* ml = m. */
1988 gfc_add_modify (&loop, ml, m);
1989
1990 /* extent = ... */
1991 lbound = gfc_conv_descriptor_lbound_get (desc, loop_var);
1992 ubound = gfc_conv_descriptor_ubound_get (desc, loop_var);
1993 extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1994 extent = fold_convert (type, extent);
1995
1996 /* m = m/extent. */
1997 gfc_add_modify (&loop, m,
1998 fold_build2_loc (input_location, TRUNC_DIV_EXPR, type,
1999 m, extent));
2000
2001 /* Exit condition: if (i >= min_var) goto exit_label. */
2002 cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, loop_var,
2003 min_var);
2004 tmp = build1_v (GOTO_EXPR, exit_label);
2005 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
2006 build_empty_stmt (input_location));
2007 gfc_add_expr_to_block (&loop, tmp);
2008
2009 /* Increment loop variable: i++. */
2010 gfc_add_modify (&loop, loop_var,
2011 fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
2012 loop_var,
2013 integer_one_node));
2014
2015 /* Making the loop... actually loop! */
2016 tmp = gfc_finish_block (&loop);
2017 tmp = build1_v (LOOP_EXPR, tmp);
2018 gfc_add_expr_to_block (&se->pre, tmp);
2019
2020 /* The exit label. */
2021 tmp = build1_v (LABEL_EXPR, exit_label);
2022 gfc_add_expr_to_block (&se->pre, tmp);
2023
2024 /* sub(co_dim) = (co_dim < corank) ? ml - m*extent + lcobound(dim_arg)
2025 : m + lcobound(corank) */
2026
2027 cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, dim_arg,
2028 build_int_cst (TREE_TYPE (dim_arg), corank));
2029
2030 lbound = gfc_conv_descriptor_lbound_get (desc,
2031 fold_build2_loc (input_location, PLUS_EXPR,
2032 gfc_array_index_type, dim_arg,
2033 build_int_cst (TREE_TYPE (dim_arg), rank-1)));
2034 lbound = fold_convert (type, lbound);
2035
2036 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, ml,
2037 fold_build2_loc (input_location, MULT_EXPR, type,
2038 m, extent));
2039 tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
2040
2041 se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
2042 fold_build2_loc (input_location, PLUS_EXPR, type,
2043 m, lbound));
2044 }
2045
2046
2047 /* Convert a call to image_status. */
2048
2049 static void
2050 conv_intrinsic_image_status (gfc_se *se, gfc_expr *expr)
2051 {
2052 unsigned int num_args;
2053 tree *args, tmp;
2054
2055 num_args = gfc_intrinsic_argument_list_length (expr);
2056 args = XALLOCAVEC (tree, num_args);
2057 gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2058 /* In args[0] the number of the image the status is desired for has to be
2059 given. */
2060
2061 if (flag_coarray == GFC_FCOARRAY_SINGLE)
2062 {
2063 tree arg;
2064 arg = gfc_evaluate_now (args[0], &se->pre);
2065 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2066 fold_convert (integer_type_node, arg),
2067 integer_one_node);
2068 tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
2069 tmp, integer_zero_node,
2070 build_int_cst (integer_type_node,
2071 GFC_STAT_STOPPED_IMAGE));
2072 }
2073 else if (flag_coarray == GFC_FCOARRAY_LIB)
2074 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_image_status, 2,
2075 args[0], build_int_cst (integer_type_node, -1));
2076 else
2077 gcc_unreachable ();
2078
2079 se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2080 }
2081
2082 static void
2083 conv_intrinsic_team_number (gfc_se *se, gfc_expr *expr)
2084 {
2085 unsigned int num_args;
2086
2087 tree *args, tmp;
2088
2089 num_args = gfc_intrinsic_argument_list_length (expr);
2090 args = XALLOCAVEC (tree, num_args);
2091 gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2092
2093 if (flag_coarray ==
2094 GFC_FCOARRAY_SINGLE && expr->value.function.actual->expr)
2095 {
2096 tree arg;
2097
2098 arg = gfc_evaluate_now (args[0], &se->pre);
2099 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2100 fold_convert (integer_type_node, arg),
2101 integer_one_node);
2102 tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
2103 tmp, integer_zero_node,
2104 build_int_cst (integer_type_node,
2105 GFC_STAT_STOPPED_IMAGE));
2106 }
2107 else if (flag_coarray == GFC_FCOARRAY_SINGLE)
2108 {
2109 // the value -1 represents that no team has been created yet
2110 tmp = build_int_cst (integer_type_node, -1);
2111 }
2112 else if (flag_coarray == GFC_FCOARRAY_LIB && expr->value.function.actual->expr)
2113 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
2114 args[0], build_int_cst (integer_type_node, -1));
2115 else if (flag_coarray == GFC_FCOARRAY_LIB)
2116 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
2117 integer_zero_node, build_int_cst (integer_type_node, -1));
2118 else
2119 gcc_unreachable ();
2120
2121 se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2122 }
2123
2124
2125 static void
2126 trans_image_index (gfc_se * se, gfc_expr *expr)
2127 {
2128 tree num_images, cond, coindex, type, lbound, ubound, desc, subdesc,
2129 tmp, invalid_bound;
2130 gfc_se argse, subse;
2131 int rank, corank, codim;
2132
2133 type = gfc_get_int_type (gfc_default_integer_kind);
2134 corank = expr->value.function.actual->expr->corank;
2135 rank = expr->value.function.actual->expr->rank;
2136
2137 /* Obtain the descriptor of the COARRAY. */
2138 gfc_init_se (&argse, NULL);
2139 argse.want_coarray = 1;
2140 gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2141 gfc_add_block_to_block (&se->pre, &argse.pre);
2142 gfc_add_block_to_block (&se->post, &argse.post);
2143 desc = argse.expr;
2144
2145 /* Obtain a handle to the SUB argument. */
2146 gfc_init_se (&subse, NULL);
2147 gfc_conv_expr_descriptor (&subse, expr->value.function.actual->next->expr);
2148 gfc_add_block_to_block (&se->pre, &subse.pre);
2149 gfc_add_block_to_block (&se->post, &subse.post);
2150 subdesc = build_fold_indirect_ref_loc (input_location,
2151 gfc_conv_descriptor_data_get (subse.expr));
2152
2153 /* Fortran 2008 does not require that the values remain in the cobounds,
2154 thus we need explicitly check this - and return 0 if they are exceeded. */
2155
2156 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2157 tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1], NULL);
2158 invalid_bound = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2159 fold_convert (gfc_array_index_type, tmp),
2160 lbound);
2161
2162 for (codim = corank + rank - 2; codim >= rank; codim--)
2163 {
2164 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2165 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2166 tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2167 cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2168 fold_convert (gfc_array_index_type, tmp),
2169 lbound);
2170 invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2171 logical_type_node, invalid_bound, cond);
2172 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2173 fold_convert (gfc_array_index_type, tmp),
2174 ubound);
2175 invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2176 logical_type_node, invalid_bound, cond);
2177 }
2178
2179 invalid_bound = gfc_unlikely (invalid_bound, PRED_FORTRAN_INVALID_BOUND);
2180
2181 /* See Fortran 2008, C.10 for the following algorithm. */
2182
2183 /* coindex = sub(corank) - lcobound(n). */
2184 coindex = fold_convert (gfc_array_index_type,
2185 gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1],
2186 NULL));
2187 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2188 coindex = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2189 fold_convert (gfc_array_index_type, coindex),
2190 lbound);
2191
2192 for (codim = corank + rank - 2; codim >= rank; codim--)
2193 {
2194 tree extent, ubound;
2195
2196 /* coindex = coindex*extent(codim) + sub(codim) - lcobound(codim). */
2197 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2198 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2199 extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2200
2201 /* coindex *= extent. */
2202 coindex = fold_build2_loc (input_location, MULT_EXPR,
2203 gfc_array_index_type, coindex, extent);
2204
2205 /* coindex += sub(codim). */
2206 tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2207 coindex = fold_build2_loc (input_location, PLUS_EXPR,
2208 gfc_array_index_type, coindex,
2209 fold_convert (gfc_array_index_type, tmp));
2210
2211 /* coindex -= lbound(codim). */
2212 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2213 coindex = fold_build2_loc (input_location, MINUS_EXPR,
2214 gfc_array_index_type, coindex, lbound);
2215 }
2216
2217 coindex = fold_build2_loc (input_location, PLUS_EXPR, type,
2218 fold_convert(type, coindex),
2219 build_int_cst (type, 1));
2220
2221 /* Return 0 if "coindex" exceeds num_images(). */
2222
2223 if (flag_coarray == GFC_FCOARRAY_SINGLE)
2224 num_images = build_int_cst (type, 1);
2225 else
2226 {
2227 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2228 integer_zero_node,
2229 build_int_cst (integer_type_node, -1));
2230 num_images = fold_convert (type, tmp);
2231 }
2232
2233 tmp = gfc_create_var (type, NULL);
2234 gfc_add_modify (&se->pre, tmp, coindex);
2235
2236 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, tmp,
2237 num_images);
2238 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
2239 cond,
2240 fold_convert (logical_type_node, invalid_bound));
2241 se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
2242 build_int_cst (type, 0), tmp);
2243 }
2244
2245 static void
2246 trans_num_images (gfc_se * se, gfc_expr *expr)
2247 {
2248 tree tmp, distance, failed;
2249 gfc_se argse;
2250
2251 if (expr->value.function.actual->expr)
2252 {
2253 gfc_init_se (&argse, NULL);
2254 gfc_conv_expr_val (&argse, expr->value.function.actual->expr);
2255 gfc_add_block_to_block (&se->pre, &argse.pre);
2256 gfc_add_block_to_block (&se->post, &argse.post);
2257 distance = fold_convert (integer_type_node, argse.expr);
2258 }
2259 else
2260 distance = integer_zero_node;
2261
2262 if (expr->value.function.actual->next->expr)
2263 {
2264 gfc_init_se (&argse, NULL);
2265 gfc_conv_expr_val (&argse, expr->value.function.actual->next->expr);
2266 gfc_add_block_to_block (&se->pre, &argse.pre);
2267 gfc_add_block_to_block (&se->post, &argse.post);
2268 failed = fold_convert (integer_type_node, argse.expr);
2269 }
2270 else
2271 failed = build_int_cst (integer_type_node, -1);
2272 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2273 distance, failed);
2274 se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2275 }
2276
2277
2278 static void
2279 gfc_conv_intrinsic_rank (gfc_se *se, gfc_expr *expr)
2280 {
2281 gfc_se argse;
2282
2283 gfc_init_se (&argse, NULL);
2284 argse.data_not_needed = 1;
2285 argse.descriptor_only = 1;
2286
2287 gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2288 gfc_add_block_to_block (&se->pre, &argse.pre);
2289 gfc_add_block_to_block (&se->post, &argse.post);
2290
2291 se->expr = gfc_conv_descriptor_rank (argse.expr);
2292 se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
2293 se->expr);
2294 }
2295
2296
2297 static void
2298 gfc_conv_intrinsic_is_contiguous (gfc_se * se, gfc_expr * expr)
2299 {
2300 gfc_expr *arg;
2301 arg = expr->value.function.actual->expr;
2302 gfc_conv_is_contiguous_expr (se, arg);
2303 se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
2304 }
2305
2306 /* This function does the work for gfc_conv_intrinsic_is_contiguous,
2307 plus it can be called directly. */
2308
2309 void
2310 gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
2311 {
2312 gfc_ss *ss;
2313 gfc_se argse;
2314 tree desc, tmp, stride, extent, cond;
2315 int i;
2316 tree fncall0;
2317 gfc_array_spec *as;
2318 gfc_symbol *sym = NULL;
2319
2320 if (arg->ts.type == BT_CLASS)
2321 gfc_add_class_array_ref (arg);
2322
2323 if (arg->expr_type == EXPR_VARIABLE)
2324 sym = arg->symtree->n.sym;
2325
2326 ss = gfc_walk_expr (arg);
2327 gcc_assert (ss != gfc_ss_terminator);
2328 gfc_init_se (&argse, NULL);
2329 argse.data_not_needed = 1;
2330 gfc_conv_expr_descriptor (&argse, arg);
2331
2332 as = gfc_get_full_arrayspec_from_expr (arg);
2333
2334 /* Create: stride[0] == 1 && stride[1] == extend[0]*stride[0] && ...
2335 Note in addition that zero-sized arrays don't count as contiguous. */
2336
2337 if (as && as->type == AS_ASSUMED_RANK)
2338 {
2339 /* Build the call to is_contiguous0. */
2340 argse.want_pointer = 1;
2341 gfc_conv_expr_descriptor (&argse, arg);
2342 gfc_add_block_to_block (&se->pre, &argse.pre);
2343 gfc_add_block_to_block (&se->post, &argse.post);
2344 desc = gfc_evaluate_now (argse.expr, &se->pre);
2345 fncall0 = build_call_expr_loc (input_location,
2346 gfor_fndecl_is_contiguous0, 1, desc);
2347 se->expr = fncall0;
2348 se->expr = convert (boolean_type_node, se->expr);
2349 }
2350 else
2351 {
2352 gfc_add_block_to_block (&se->pre, &argse.pre);
2353 gfc_add_block_to_block (&se->post, &argse.post);
2354 desc = gfc_evaluate_now (argse.expr, &se->pre);
2355
2356 stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[0]);
2357 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2358 stride, build_int_cst (TREE_TYPE (stride), 1));
2359
2360 for (i = 0; i < arg->rank - 1; i++)
2361 {
2362 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2363 extent = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2364 extent = fold_build2_loc (input_location, MINUS_EXPR,
2365 gfc_array_index_type, extent, tmp);
2366 extent = fold_build2_loc (input_location, PLUS_EXPR,
2367 gfc_array_index_type, extent,
2368 gfc_index_one_node);
2369 tmp = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i]);
2370 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2371 tmp, extent);
2372 stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i+1]);
2373 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2374 stride, tmp);
2375 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2376 boolean_type_node, cond, tmp);
2377 }
2378 se->expr = cond;
2379 }
2380
2381 /* A pointer that does not have the CONTIGUOUS attribute needs to be checked
2382 if it points to an array whose span differs from the element size. */
2383 if (as && sym && IS_POINTER(sym) && !sym->attr.contiguous)
2384 {
2385 tree span = gfc_conv_descriptor_span_get (desc);
2386 tmp = fold_convert (TREE_TYPE (span),
2387 gfc_conv_descriptor_elem_len (desc));
2388 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2389 span, tmp);
2390 se->expr = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2391 boolean_type_node, cond,
2392 convert (boolean_type_node, se->expr));
2393 }
2394
2395 gfc_free_ss_chain (ss);
2396 }
2397
2398
2399 /* Evaluate a single upper or lower bound. */
2400 /* TODO: bound intrinsic generates way too much unnecessary code. */
2401
2402 static void
2403 gfc_conv_intrinsic_bound (gfc_se * se, gfc_expr * expr, enum gfc_isym_id op)
2404 {
2405 gfc_actual_arglist *arg;
2406 gfc_actual_arglist *arg2;
2407 tree desc;
2408 tree type;
2409 tree bound;
2410 tree tmp;
2411 tree cond, cond1;
2412 tree ubound;
2413 tree lbound;
2414 tree size;
2415 gfc_se argse;
2416 gfc_array_spec * as;
2417 bool assumed_rank_lb_one;
2418
2419 arg = expr->value.function.actual;
2420 arg2 = arg->next;
2421
2422 if (se->ss)
2423 {
2424 /* Create an implicit second parameter from the loop variable. */
2425 gcc_assert (!arg2->expr || op == GFC_ISYM_SHAPE);
2426 gcc_assert (se->loop->dimen == 1);
2427 gcc_assert (se->ss->info->expr == expr);
2428 gfc_advance_se_ss_chain (se);
2429 bound = se->loop->loopvar[0];
2430 bound = fold_build2_loc (input_location, MINUS_EXPR,
2431 gfc_array_index_type, bound,
2432 se->loop->from[0]);
2433 }
2434 else
2435 {
2436 /* use the passed argument. */
2437 gcc_assert (arg2->expr);
2438 gfc_init_se (&argse, NULL);
2439 gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
2440 gfc_add_block_to_block (&se->pre, &argse.pre);
2441 bound = argse.expr;
2442 /* Convert from one based to zero based. */
2443 bound = fold_build2_loc (input_location, MINUS_EXPR,
2444 gfc_array_index_type, bound,
2445 gfc_index_one_node);
2446 }
2447
2448 /* TODO: don't re-evaluate the descriptor on each iteration. */
2449 /* Get a descriptor for the first parameter. */
2450 gfc_init_se (&argse, NULL);
2451 gfc_conv_expr_descriptor (&argse, arg->expr);
2452 gfc_add_block_to_block (&se->pre, &argse.pre);
2453 gfc_add_block_to_block (&se->post, &argse.post);
2454
2455 desc = argse.expr;
2456
2457 as = gfc_get_full_arrayspec_from_expr (arg->expr);
2458
2459 if (INTEGER_CST_P (bound))
2460 {
2461 gcc_assert (op != GFC_ISYM_SHAPE);
2462 if (((!as || as->type != AS_ASSUMED_RANK)
2463 && wi::geu_p (wi::to_wide (bound),
2464 GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))))
2465 || wi::gtu_p (wi::to_wide (bound), GFC_MAX_DIMENSIONS))
2466 gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2467 "dimension index",
2468 (op == GFC_ISYM_UBOUND) ? "UBOUND" : "LBOUND",
2469 &expr->where);
2470 }
2471
2472 if (!INTEGER_CST_P (bound) || (as && as->type == AS_ASSUMED_RANK))
2473 {
2474 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2475 {
2476 bound = gfc_evaluate_now (bound, &se->pre);
2477 cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2478 bound, build_int_cst (TREE_TYPE (bound), 0));
2479 if (as && as->type == AS_ASSUMED_RANK)
2480 tmp = gfc_conv_descriptor_rank (desc);
2481 else
2482 tmp = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))];
2483 tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
2484 bound, fold_convert(TREE_TYPE (bound), tmp));
2485 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2486 logical_type_node, cond, tmp);
2487 gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2488 gfc_msg_fault);
2489 }
2490 }
2491
2492 /* Take care of the lbound shift for assumed-rank arrays that are
2493 nonallocatable and nonpointers. Those have a lbound of 1. */
2494 assumed_rank_lb_one = as && as->type == AS_ASSUMED_RANK
2495 && ((arg->expr->ts.type != BT_CLASS
2496 && !arg->expr->symtree->n.sym->attr.allocatable
2497 && !arg->expr->symtree->n.sym->attr.pointer)
2498 || (arg->expr->ts.type == BT_CLASS
2499 && !CLASS_DATA (arg->expr)->attr.allocatable
2500 && !CLASS_DATA (arg->expr)->attr.class_pointer));
2501
2502 ubound = gfc_conv_descriptor_ubound_get (desc, bound);
2503 lbound = gfc_conv_descriptor_lbound_get (desc, bound);
2504 size = fold_build2_loc (input_location, MINUS_EXPR,
2505 gfc_array_index_type, ubound, lbound);
2506 size = fold_build2_loc (input_location, PLUS_EXPR,
2507 gfc_array_index_type, size, gfc_index_one_node);
2508
2509 /* 13.14.53: Result value for LBOUND
2510
2511 Case (i): For an array section or for an array expression other than a
2512 whole array or array structure component, LBOUND(ARRAY, DIM)
2513 has the value 1. For a whole array or array structure
2514 component, LBOUND(ARRAY, DIM) has the value:
2515 (a) equal to the lower bound for subscript DIM of ARRAY if
2516 dimension DIM of ARRAY does not have extent zero
2517 or if ARRAY is an assumed-size array of rank DIM,
2518 or (b) 1 otherwise.
2519
2520 13.14.113: Result value for UBOUND
2521
2522 Case (i): For an array section or for an array expression other than a
2523 whole array or array structure component, UBOUND(ARRAY, DIM)
2524 has the value equal to the number of elements in the given
2525 dimension; otherwise, it has a value equal to the upper bound
2526 for subscript DIM of ARRAY if dimension DIM of ARRAY does
2527 not have size zero and has value zero if dimension DIM has
2528 size zero. */
2529
2530 if (op == GFC_ISYM_LBOUND && assumed_rank_lb_one)
2531 se->expr = gfc_index_one_node;
2532 else if (as)
2533 {
2534 if (op == GFC_ISYM_UBOUND)
2535 {
2536 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2537 size, gfc_index_zero_node);
2538 se->expr = fold_build3_loc (input_location, COND_EXPR,
2539 gfc_array_index_type, cond,
2540 (assumed_rank_lb_one ? size : ubound),
2541 gfc_index_zero_node);
2542 }
2543 else if (op == GFC_ISYM_LBOUND)
2544 {
2545 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2546 size, gfc_index_zero_node);
2547 if (as->type == AS_ASSUMED_SIZE)
2548 {
2549 cond1 = fold_build2_loc (input_location, EQ_EXPR,
2550 logical_type_node, bound,
2551 build_int_cst (TREE_TYPE (bound),
2552 arg->expr->rank - 1));
2553 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2554 logical_type_node, cond, cond1);
2555 }
2556 se->expr = fold_build3_loc (input_location, COND_EXPR,
2557 gfc_array_index_type, cond,
2558 lbound, gfc_index_one_node);
2559 }
2560 else if (op == GFC_ISYM_SHAPE)
2561 se->expr = fold_build2_loc (input_location, MAX_EXPR,
2562 gfc_array_index_type, size,
2563 gfc_index_zero_node);
2564 else
2565 gcc_unreachable ();
2566
2567 /* According to F2018 16.9.172, para 5, an assumed rank object,
2568 argument associated with and assumed size array, has the ubound
2569 of the final dimension set to -1 and UBOUND must return this.
2570 Similarly for the SHAPE intrinsic. */
2571 if (op != GFC_ISYM_LBOUND && assumed_rank_lb_one)
2572 {
2573 tree minus_one = build_int_cst (gfc_array_index_type, -1);
2574 tree rank = fold_convert (gfc_array_index_type,
2575 gfc_conv_descriptor_rank (desc));
2576 rank = fold_build2_loc (input_location, PLUS_EXPR,
2577 gfc_array_index_type, rank, minus_one);
2578
2579 /* Fix the expression to stop it from becoming even more
2580 complicated. */
2581 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2582
2583 /* Descriptors for assumed-size arrays have ubound = -1
2584 in the last dimension. */
2585 cond1 = fold_build2_loc (input_location, EQ_EXPR,
2586 logical_type_node, ubound, minus_one);
2587 cond = fold_build2_loc (input_location, EQ_EXPR,
2588 logical_type_node, bound, rank);
2589 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2590 logical_type_node, cond, cond1);
2591 se->expr = fold_build3_loc (input_location, COND_EXPR,
2592 gfc_array_index_type, cond,
2593 minus_one, se->expr);
2594 }
2595 }
2596 else /* as is null; this is an old-fashioned 1-based array. */
2597 {
2598 if (op != GFC_ISYM_LBOUND)
2599 {
2600 se->expr = fold_build2_loc (input_location, MAX_EXPR,
2601 gfc_array_index_type, size,
2602 gfc_index_zero_node);
2603 }
2604 else
2605 se->expr = gfc_index_one_node;
2606 }
2607
2608
2609 type = gfc_typenode_for_spec (&expr->ts);
2610 se->expr = convert (type, se->expr);
2611 }
2612
2613
2614 static void
2615 conv_intrinsic_cobound (gfc_se * se, gfc_expr * expr)
2616 {
2617 gfc_actual_arglist *arg;
2618 gfc_actual_arglist *arg2;
2619 gfc_se argse;
2620 tree bound, resbound, resbound2, desc, cond, tmp;
2621 tree type;
2622 int corank;
2623
2624 gcc_assert (expr->value.function.isym->id == GFC_ISYM_LCOBOUND
2625 || expr->value.function.isym->id == GFC_ISYM_UCOBOUND
2626 || expr->value.function.isym->id == GFC_ISYM_THIS_IMAGE);
2627
2628 arg = expr->value.function.actual;
2629 arg2 = arg->next;
2630
2631 gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
2632 corank = arg->expr->corank;
2633
2634 gfc_init_se (&argse, NULL);
2635 argse.want_coarray = 1;
2636
2637 gfc_conv_expr_descriptor (&argse, arg->expr);
2638 gfc_add_block_to_block (&se->pre, &argse.pre);
2639 gfc_add_block_to_block (&se->post, &argse.post);
2640 desc = argse.expr;
2641
2642 if (se->ss)
2643 {
2644 /* Create an implicit second parameter from the loop variable. */
2645 gcc_assert (!arg2->expr);
2646 gcc_assert (corank > 0);
2647 gcc_assert (se->loop->dimen == 1);
2648 gcc_assert (se->ss->info->expr == expr);
2649
2650 bound = se->loop->loopvar[0];
2651 bound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2652 bound, gfc_rank_cst[arg->expr->rank]);
2653 gfc_advance_se_ss_chain (se);
2654 }
2655 else
2656 {
2657 /* use the passed argument. */
2658 gcc_assert (arg2->expr);
2659 gfc_init_se (&argse, NULL);
2660 gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
2661 gfc_add_block_to_block (&se->pre, &argse.pre);
2662 bound = argse.expr;
2663
2664 if (INTEGER_CST_P (bound))
2665 {
2666 if (wi::ltu_p (wi::to_wide (bound), 1)
2667 || wi::gtu_p (wi::to_wide (bound),
2668 GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
2669 gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2670 "dimension index", expr->value.function.isym->name,
2671 &expr->where);
2672 }
2673 else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2674 {
2675 bound = gfc_evaluate_now (bound, &se->pre);
2676 cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2677 bound, build_int_cst (TREE_TYPE (bound), 1));
2678 tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
2679 tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2680 bound, tmp);
2681 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2682 logical_type_node, cond, tmp);
2683 gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2684 gfc_msg_fault);
2685 }
2686
2687
2688 /* Subtract 1 to get to zero based and add dimensions. */
2689 switch (arg->expr->rank)
2690 {
2691 case 0:
2692 bound = fold_build2_loc (input_location, MINUS_EXPR,
2693 gfc_array_index_type, bound,
2694 gfc_index_one_node);
2695 case 1:
2696 break;
2697 default:
2698 bound = fold_build2_loc (input_location, PLUS_EXPR,
2699 gfc_array_index_type, bound,
2700 gfc_rank_cst[arg->expr->rank - 1]);
2701 }
2702 }
2703
2704 resbound = gfc_conv_descriptor_lbound_get (desc, bound);
2705
2706 /* Handle UCOBOUND with special handling of the last codimension. */
2707 if (expr->value.function.isym->id == GFC_ISYM_UCOBOUND)
2708 {
2709 /* Last codimension: For -fcoarray=single just return
2710 the lcobound - otherwise add
2711 ceiling (real (num_images ()) / real (size)) - 1
2712 = (num_images () + size - 1) / size - 1
2713 = (num_images - 1) / size(),
2714 where size is the product of the extent of all but the last
2715 codimension. */
2716
2717 if (flag_coarray != GFC_FCOARRAY_SINGLE && corank > 1)
2718 {
2719 tree cosize;
2720
2721 cosize = gfc_conv_descriptor_cosize (desc, arg->expr->rank, corank);
2722 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
2723 2, integer_zero_node,
2724 build_int_cst (integer_type_node, -1));
2725 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2726 gfc_array_index_type,
2727 fold_convert (gfc_array_index_type, tmp),
2728 build_int_cst (gfc_array_index_type, 1));
2729 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
2730 gfc_array_index_type, tmp,
2731 fold_convert (gfc_array_index_type, cosize));
2732 resbound = fold_build2_loc (input_location, PLUS_EXPR,
2733 gfc_array_index_type, resbound, tmp);
2734 }
2735 else if (flag_coarray != GFC_FCOARRAY_SINGLE)
2736 {
2737 /* ubound = lbound + num_images() - 1. */
2738 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
2739 2, integer_zero_node,
2740 build_int_cst (integer_type_node, -1));
2741 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2742 gfc_array_index_type,
2743 fold_convert (gfc_array_index_type, tmp),
2744 build_int_cst (gfc_array_index_type, 1));
2745 resbound = fold_build2_loc (input_location, PLUS_EXPR,
2746 gfc_array_index_type, resbound, tmp);
2747 }
2748
2749 if (corank > 1)
2750 {
2751 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2752 bound,
2753 build_int_cst (TREE_TYPE (bound),
2754 arg->expr->rank + corank - 1));
2755
2756 resbound2 = gfc_conv_descriptor_ubound_get (desc, bound);
2757 se->expr = fold_build3_loc (input_location, COND_EXPR,
2758 gfc_array_index_type, cond,
2759 resbound, resbound2);
2760 }
2761 else
2762 se->expr = resbound;
2763 }
2764 else
2765 se->expr = resbound;
2766
2767 type = gfc_typenode_for_spec (&expr->ts);
2768 se->expr = convert (type, se->expr);
2769 }
2770
2771
2772 static void
2773 conv_intrinsic_stride (gfc_se * se, gfc_expr * expr)
2774 {
2775 gfc_actual_arglist *array_arg;
2776 gfc_actual_arglist *dim_arg;
2777 gfc_se argse;
2778 tree desc, tmp;
2779
2780 array_arg = expr->value.function.actual;
2781 dim_arg = array_arg->next;
2782
2783 gcc_assert (array_arg->expr->expr_type == EXPR_VARIABLE);
2784
2785 gfc_init_se (&argse, NULL);
2786 gfc_conv_expr_descriptor (&argse, array_arg->expr);
2787 gfc_add_block_to_block (&se->pre, &argse.pre);
2788 gfc_add_block_to_block (&se->post, &argse.post);
2789 desc = argse.expr;
2790
2791 gcc_assert (dim_arg->expr);
2792 gfc_init_se (&argse, NULL);
2793 gfc_conv_expr_type (&argse, dim_arg->expr, gfc_array_index_type);
2794 gfc_add_block_to_block (&se->pre, &argse.pre);
2795 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2796 argse.expr, gfc_index_one_node);
2797 se->expr = gfc_conv_descriptor_stride_get (desc, tmp);
2798 }
2799
2800 static void
2801 gfc_conv_intrinsic_abs (gfc_se * se, gfc_expr * expr)
2802 {
2803 tree arg, cabs;
2804
2805 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
2806
2807 switch (expr->value.function.actual->expr->ts.type)
2808 {
2809 case BT_INTEGER:
2810 case BT_REAL:
2811 se->expr = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (arg),
2812 arg);
2813 break;
2814
2815 case BT_COMPLEX:
2816 cabs = gfc_builtin_decl_for_float_kind (BUILT_IN_CABS, expr->ts.kind);
2817 se->expr = build_call_expr_loc (input_location, cabs, 1, arg);
2818 break;
2819
2820 default:
2821 gcc_unreachable ();
2822 }
2823 }
2824
2825
2826 /* Create a complex value from one or two real components. */
2827
2828 static void
2829 gfc_conv_intrinsic_cmplx (gfc_se * se, gfc_expr * expr, int both)
2830 {
2831 tree real;
2832 tree imag;
2833 tree type;
2834 tree *args;
2835 unsigned int num_args;
2836
2837 num_args = gfc_intrinsic_argument_list_length (expr);
2838 args = XALLOCAVEC (tree, num_args);
2839
2840 type = gfc_typenode_for_spec (&expr->ts);
2841 gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2842 real = convert (TREE_TYPE (type), args[0]);
2843 if (both)
2844 imag = convert (TREE_TYPE (type), args[1]);
2845 else if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE)
2846 {
2847 imag = fold_build1_loc (input_location, IMAGPART_EXPR,
2848 TREE_TYPE (TREE_TYPE (args[0])), args[0]);
2849 imag = convert (TREE_TYPE (type), imag);
2850 }
2851 else
2852 imag = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
2853
2854 se->expr = fold_build2_loc (input_location, COMPLEX_EXPR, type, real, imag);
2855 }
2856
2857
2858 /* Remainder function MOD(A, P) = A - INT(A / P) * P
2859 MODULO(A, P) = A - FLOOR (A / P) * P
2860
2861 The obvious algorithms above are numerically instable for large
2862 arguments, hence these intrinsics are instead implemented via calls
2863 to the fmod family of functions. It is the responsibility of the
2864 user to ensure that the second argument is non-zero. */
2865
2866 static void
2867 gfc_conv_intrinsic_mod (gfc_se * se, gfc_expr * expr, int modulo)
2868 {
2869 tree type;
2870 tree tmp;
2871 tree test;
2872 tree test2;
2873 tree fmod;
2874 tree zero;
2875 tree args[2];
2876
2877 gfc_conv_intrinsic_function_args (se, expr, args, 2);
2878
2879 switch (expr->ts.type)
2880 {
2881 case BT_INTEGER:
2882 /* Integer case is easy, we've got a builtin op. */
2883 type = TREE_TYPE (args[0]);
2884
2885 if (modulo)
2886 se->expr = fold_build2_loc (input_location, FLOOR_MOD_EXPR, type,
2887 args[0], args[1]);
2888 else
2889 se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2890 args[0], args[1]);
2891 break;
2892
2893 case BT_UNSIGNED:
2894 /* Even easier, we only need one. */
2895 type = TREE_TYPE (args[0]);
2896 se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2897 args[0], args[1]);
2898 break;
2899
2900 case BT_REAL:
2901 fmod = NULL_TREE;
2902 /* Check if we have a builtin fmod. */
2903 fmod = gfc_builtin_decl_for_float_kind (BUILT_IN_FMOD, expr->ts.kind);
2904
2905 /* The builtin should always be available. */
2906 gcc_assert (fmod != NULL_TREE);
2907
2908 tmp = build_addr (fmod);
2909 se->expr = build_call_array_loc (input_location,
2910 TREE_TYPE (TREE_TYPE (fmod)),
2911 tmp, 2, args);
2912 if (modulo == 0)
2913 return;
2914
2915 type = TREE_TYPE (args[0]);
2916
2917 args[0] = gfc_evaluate_now (args[0], &se->pre);
2918 args[1] = gfc_evaluate_now (args[1], &se->pre);
2919
2920 /* Definition:
2921 modulo = arg - floor (arg/arg2) * arg2
2922
2923 In order to calculate the result accurately, we use the fmod
2924 function as follows.
2925
2926 res = fmod (arg, arg2);
2927 if (res)
2928 {
2929 if ((arg < 0) xor (arg2 < 0))
2930 res += arg2;
2931 }
2932 else
2933 res = copysign (0., arg2);
2934
2935 => As two nested ternary exprs:
2936
2937 res = res ? (((arg < 0) xor (arg2 < 0)) ? res + arg2 : res)
2938 : copysign (0., arg2);
2939
2940 */
2941
2942 zero = gfc_build_const (type, integer_zero_node);
2943 tmp = gfc_evaluate_now (se->expr, &se->pre);
2944 if (!flag_signed_zeros)
2945 {
2946 test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2947 args[0], zero);
2948 test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2949 args[1], zero);
2950 test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
2951 logical_type_node, test, test2);
2952 test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2953 tmp, zero);
2954 test = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2955 logical_type_node, test, test2);
2956 test = gfc_evaluate_now (test, &se->pre);
2957 se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
2958 fold_build2_loc (input_location,
2959 PLUS_EXPR,
2960 type, tmp, args[1]),
2961 tmp);
2962 }
2963 else
2964 {
2965 tree expr1, copysign, cscall;
2966 copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN,
2967 expr->ts.kind);
2968 test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2969 args[0], zero);
2970 test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2971 args[1], zero);
2972 test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
2973 logical_type_node, test, test2);
2974 expr1 = fold_build3_loc (input_location, COND_EXPR, type, test2,
2975 fold_build2_loc (input_location,
2976 PLUS_EXPR,
2977 type, tmp, args[1]),
2978 tmp);
2979 test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2980 tmp, zero);
2981 cscall = build_call_expr_loc (input_location, copysign, 2, zero,
2982 args[1]);
2983 se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
2984 expr1, cscall);
2985 }
2986 return;
2987
2988 default:
2989 gcc_unreachable ();
2990 }
2991 }
2992
2993 /* DSHIFTL(I,J,S) = (I << S) | (J >> (BITSIZE(J) - S))
2994 DSHIFTR(I,J,S) = (I << (BITSIZE(I) - S)) | (J >> S)
2995 where the right shifts are logical (i.e. 0's are shifted in).
2996 Because SHIFT_EXPR's want shifts strictly smaller than the integral
2997 type width, we have to special-case both S == 0 and S == BITSIZE(J):
2998 DSHIFTL(I,J,0) = I
2999 DSHIFTL(I,J,BITSIZE) = J
3000 DSHIFTR(I,J,0) = J
3001 DSHIFTR(I,J,BITSIZE) = I. */
3002
3003 static void
3004 gfc_conv_intrinsic_dshift (gfc_se * se, gfc_expr * expr, bool dshiftl)
3005 {
3006 tree type, utype, stype, arg1, arg2, shift, res, left, right;
3007 tree args[3], cond, tmp;
3008 int bitsize;
3009
3010 gfc_conv_intrinsic_function_args (se, expr, args, 3);
3011
3012 gcc_assert (TREE_TYPE (args[0]) == TREE_TYPE (args[1]));
3013 type = TREE_TYPE (args[0]);
3014 bitsize = TYPE_PRECISION (type);
3015 utype = unsigned_type_for (type);
3016 stype = TREE_TYPE (args[2]);
3017
3018 arg1 = gfc_evaluate_now (args[0], &se->pre);
3019 arg2 = gfc_evaluate_now (args[1], &se->pre);
3020 shift = gfc_evaluate_now (args[2], &se->pre);
3021
3022 /* The generic case. */
3023 tmp = fold_build2_loc (input_location, MINUS_EXPR, stype,
3024 build_int_cst (stype, bitsize), shift);
3025 left = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3026 arg1, dshiftl ? shift : tmp);
3027
3028 right = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
3029 fold_convert (utype, arg2), dshiftl ? tmp : shift);
3030 right = fold_convert (type, right);
3031
3032 res = fold_build2_loc (input_location, BIT_IOR_EXPR, type, left, right);
3033
3034 /* Special cases. */
3035 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3036 build_int_cst (stype, 0));
3037 res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3038 dshiftl ? arg1 : arg2, res);
3039
3040 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3041 build_int_cst (stype, bitsize));
3042 res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3043 dshiftl ? arg2 : arg1, res);
3044
3045 se->expr = res;
3046 }
3047
3048
3049 /* Positive difference DIM (x, y) = ((x - y) < 0) ? 0 : x - y. */
3050
3051 static void
3052 gfc_conv_intrinsic_dim (gfc_se * se, gfc_expr * expr)
3053 {
3054 tree val;
3055 tree tmp;
3056 tree type;
3057 tree zero;
3058 tree args[2];
3059
3060 gfc_conv_intrinsic_function_args (se, expr, args, 2);
3061 type = TREE_TYPE (args[0]);
3062
3063 val = fold_build2_loc (input_location, MINUS_EXPR, type, args[0], args[1]);
3064 val = gfc_evaluate_now (val, &se->pre);
3065
3066 zero = gfc_build_const (type, integer_zero_node);
3067 tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node, val, zero);
3068 se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, zero, val);
3069 }
3070
3071
3072 /* SIGN(A, B) is absolute value of A times sign of B.
3073 The real value versions use library functions to ensure the correct
3074 handling of negative zero. Integer case implemented as:
3075 SIGN(A, B) = { tmp = (A ^ B) >> C; (A + tmp) ^ tmp }
3076 */
3077
3078 static void
3079 gfc_conv_intrinsic_sign (gfc_se * se, gfc_expr * expr)
3080 {
3081 tree tmp;
3082 tree type;
3083 tree args[2];
3084
3085 gfc_conv_intrinsic_function_args (se, expr, args, 2);
3086 if (expr->ts.type == BT_REAL)
3087 {
3088 tree abs;
3089
3090 tmp = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
3091 abs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
3092
3093 /* We explicitly have to ignore the minus sign. We do so by using
3094 result = (arg1 == 0) ? abs(arg0) : copysign(arg0, arg1). */
3095 if (!flag_sign_zero
3096 && MODE_HAS_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (args[1]))))
3097 {
3098 tree cond, zero;
3099 zero = build_real_from_int_cst (TREE_TYPE (args[1]), integer_zero_node);
3100 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3101 args[1], zero);
3102 se->expr = fold_build3_loc (input_location, COND_EXPR,
3103 TREE_TYPE (args[0]), cond,
3104 build_call_expr_loc (input_location, abs, 1,
3105 args[0]),
3106 build_call_expr_loc (input_location, tmp, 2,
3107 args[0], args[1]));
3108 }
3109 else
3110 se->expr = build_call_expr_loc (input_location, tmp, 2,
3111 args[0], args[1]);
3112 return;
3113 }
3114
3115 /* Having excluded floating point types, we know we are now dealing
3116 with signed integer types. */
3117 type = TREE_TYPE (args[0]);
3118
3119 /* Args[0] is used multiple times below. */
3120 args[0] = gfc_evaluate_now (args[0], &se->pre);
3121
3122 /* Construct (A ^ B) >> 31, which generates a bit mask of all zeros if
3123 the signs of A and B are the same, and of all ones if they differ. */
3124 tmp = fold_build2_loc (input_location, BIT_XOR_EXPR, type, args[0], args[1]);
3125 tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, tmp,
3126 build_int_cst (type, TYPE_PRECISION (type) - 1));
3127 tmp = gfc_evaluate_now (tmp, &se->pre);
3128
3129 /* Construct (A + tmp) ^ tmp, which is A if tmp is zero, and -A if tmp]
3130 is all ones (i.e. -1). */
3131 se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR, type,
3132 fold_build2_loc (input_location, PLUS_EXPR,
3133 type, args[0], tmp), tmp);
3134 }
3135
3136
3137 /* Test for the presence of an optional argument. */
3138
3139 static void
3140 gfc_conv_intrinsic_present (gfc_se * se, gfc_expr * expr)
3141 {
3142 gfc_expr *arg;
3143
3144 arg = expr->value.function.actual->expr;
3145 gcc_assert (arg->expr_type == EXPR_VARIABLE);
3146 se->expr = gfc_conv_expr_present (arg->symtree->n.sym);
3147 se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
3148 }
3149
3150
3151 /* Calculate the double precision product of two single precision values. */
3152
3153 static void
3154 gfc_conv_intrinsic_dprod (gfc_se * se, gfc_expr * expr)
3155 {
3156 tree type;
3157 tree args[2];
3158
3159 gfc_conv_intrinsic_function_args (se, expr, args, 2);
3160
3161 /* Convert the args to double precision before multiplying. */
3162 type = gfc_typenode_for_spec (&expr->ts);
3163 args[0] = convert (type, args[0]);
3164 args[1] = convert (type, args[1]);
3165 se->expr = fold_build2_loc (input_location, MULT_EXPR, type, args[0],
3166 args[1]);
3167 }
3168
3169
3170 /* Return a length one character string containing an ascii character. */
3171
3172 static void
3173 gfc_conv_intrinsic_char (gfc_se * se, gfc_expr * expr)
3174 {
3175 tree arg[2];
3176 tree var;
3177 tree type;
3178 unsigned int num_args;
3179
3180 num_args = gfc_intrinsic_argument_list_length (expr);
3181 gfc_conv_intrinsic_function_args (se, expr, arg, num_args);
3182
3183 type = gfc_get_char_type (expr->ts.kind);
3184 var = gfc_create_var (type, "char");
3185
3186 arg[0] = fold_build1_loc (input_location, NOP_EXPR, type, arg[0]);
3187 gfc_add_modify (&se->pre, var, arg[0]);
3188 se->expr = gfc_build_addr_expr (build_pointer_type (type), var);
3189 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3190 }
3191
3192
3193 static void
3194 gfc_conv_intrinsic_ctime (gfc_se * se, gfc_expr * expr)
3195 {
3196 tree var;
3197 tree len;
3198 tree tmp;
3199 tree cond;
3200 tree fndecl;
3201 tree *args;
3202 unsigned int num_args;
3203
3204 num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3205 args = XALLOCAVEC (tree, num_args);
3206
3207 var = gfc_create_var (pchar_type_node, "pstr");
3208 len = gfc_create_var (gfc_charlen_type_node, "len");
3209
3210 gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3211 args[0] = gfc_build_addr_expr (NULL_TREE, var);
3212 args[1] = gfc_build_addr_expr (NULL_TREE, len);
3213
3214 fndecl = build_addr (gfor_fndecl_ctime);
3215 tmp = build_call_array_loc (input_location,
3216 TREE_TYPE (TREE_TYPE (gfor_fndecl_ctime)),
3217 fndecl, num_args, args);
3218 gfc_add_expr_to_block (&se->pre, tmp);
3219
3220 /* Free the temporary afterwards, if necessary. */
3221 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3222 len, build_int_cst (TREE_TYPE (len), 0));
3223 tmp = gfc_call_free (var);
3224 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3225 gfc_add_expr_to_block (&se->post, tmp);
3226
3227 se->expr = var;
3228 se->string_length = len;
3229 }
3230
3231
3232 static void
3233 gfc_conv_intrinsic_fdate (gfc_se * se, gfc_expr * expr)
3234 {
3235 tree var;
3236 tree len;
3237 tree tmp;
3238 tree cond;
3239 tree fndecl;
3240 tree *args;
3241 unsigned int num_args;
3242
3243 num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3244 args = XALLOCAVEC (tree, num_args);
3245
3246 var = gfc_create_var (pchar_type_node, "pstr");
3247 len = gfc_create_var (gfc_charlen_type_node, "len");
3248
3249 gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3250 args[0] = gfc_build_addr_expr (NULL_TREE, var);
3251 args[1] = gfc_build_addr_expr (NULL_TREE, len);
3252
3253 fndecl = build_addr (gfor_fndecl_fdate);
3254 tmp = build_call_array_loc (input_location,
3255 TREE_TYPE (TREE_TYPE (gfor_fndecl_fdate)),
3256 fndecl, num_args, args);
3257 gfc_add_expr_to_block (&se->pre, tmp);
3258
3259 /* Free the temporary afterwards, if necessary. */
3260 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3261 len, build_int_cst (TREE_TYPE (len), 0));
3262 tmp = gfc_call_free (var);
3263 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3264 gfc_add_expr_to_block (&se->post, tmp);
3265
3266 se->expr = var;
3267 se->string_length = len;
3268 }
3269
3270
3271 /* Generate a direct call to free() for the FREE subroutine. */
3272
3273 static tree
3274 conv_intrinsic_free (gfc_code *code)
3275 {
3276 stmtblock_t block;
3277 gfc_se argse;
3278 tree arg, call;
3279
3280 gfc_init_se (&argse, NULL);
3281 gfc_conv_expr (&argse, code->ext.actual->expr);
3282 arg = fold_convert (ptr_type_node, argse.expr);
3283
3284 gfc_init_block (&block);
3285 call = build_call_expr_loc (input_location,
3286 builtin_decl_explicit (BUILT_IN_FREE), 1, arg);
3287 gfc_add_expr_to_block (&block, call);
3288 return gfc_finish_block (&block);
3289 }
3290
3291
3292 /* Call the RANDOM_INIT library subroutine with a hidden argument for
3293 handling seeding on coarray images. */
3294
3295 static tree
3296 conv_intrinsic_random_init (gfc_code *code)
3297 {
3298 stmtblock_t block;
3299 gfc_se se;
3300 tree arg1, arg2, tmp;
3301 /* On none coarray == lib compiles use LOGICAL(4) else regular LOGICAL. */
3302 tree used_bool_type_node = flag_coarray == GFC_FCOARRAY_LIB
3303 ? logical_type_node
3304 : gfc_get_logical_type (4);
3305
3306 /* Make the function call. */
3307 gfc_init_block (&block);
3308 gfc_init_se (&se, NULL);
3309
3310 /* Convert REPEATABLE to the desired LOGICAL entity. */
3311 gfc_conv_expr (&se, code->ext.actual->expr);
3312 gfc_add_block_to_block (&block, &se.pre);
3313 arg1 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3314 gfc_add_block_to_block (&block, &se.post);
3315
3316 /* Convert IMAGE_DISTINCT to the desired LOGICAL entity. */
3317 gfc_conv_expr (&se, code->ext.actual->next->expr);
3318 gfc_add_block_to_block (&block, &se.pre);
3319 arg2 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3320 gfc_add_block_to_block (&block, &se.post);
3321
3322 if (flag_coarray == GFC_FCOARRAY_LIB)
3323 {
3324 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
3325 2, arg1, arg2);
3326 }
3327 else
3328 {
3329 /* The ABI for libgfortran needs to be maintained, so a hidden
3330 argument must be include if code is compiled with -fcoarray=single
3331 or without the option. Set to 0. */
3332 tree arg3 = build_int_cst (gfc_get_int_type (4), 0);
3333 tmp = build_call_expr_loc (input_location, gfor_fndecl_random_init,
3334 3, arg1, arg2, arg3);
3335 }
3336
3337 gfc_add_expr_to_block (&block, tmp);
3338
3339 return gfc_finish_block (&block);
3340 }
3341
3342
3343 /* Call the SYSTEM_CLOCK library functions, handling the type and kind
3344 conversions. */
3345
3346 static tree
3347 conv_intrinsic_system_clock (gfc_code *code)
3348 {
3349 stmtblock_t block;
3350 gfc_se count_se, count_rate_se, count_max_se;
3351 tree arg1 = NULL_TREE, arg2 = NULL_TREE, arg3 = NULL_TREE;
3352 tree tmp;
3353 int least;
3354
3355 gfc_expr *count = code->ext.actual->expr;
3356 gfc_expr *count_rate = code->ext.actual->next->expr;
3357 gfc_expr *count_max = code->ext.actual->next->next->expr;
3358
3359 /* Evaluate our arguments. */
3360 if (count)
3361 {
3362 gfc_init_se (&count_se, NULL);
3363 gfc_conv_expr (&count_se, count);
3364 }
3365
3366 if (count_rate)
3367 {
3368 gfc_init_se (&count_rate_se, NULL);
3369 gfc_conv_expr (&count_rate_se, count_rate);
3370 }
3371
3372 if (count_max)
3373 {
3374 gfc_init_se (&count_max_se, NULL);
3375 gfc_conv_expr (&count_max_se, count_max);
3376 }
3377
3378 /* Find the smallest kind found of the arguments. */
3379 least = 16;
3380 least = (count && count->ts.kind < least) ? count->ts.kind : least;
3381 least = (count_rate && count_rate->ts.kind < least) ? count_rate->ts.kind
3382 : least;
3383 least = (count_max && count_max->ts.kind < least) ? count_max->ts.kind
3384 : least;
3385
3386 /* Prepare temporary variables. */
3387
3388 if (count)
3389 {
3390 if (least >= 8)
3391 arg1 = gfc_create_var (gfc_get_int_type (8), "count");
3392 else if (least == 4)
3393 arg1 = gfc_create_var (gfc_get_int_type (4), "count");
3394 else if (count->ts.kind == 1)
3395 arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[0].pedantic_min_int,
3396 count->ts.kind);
3397 else
3398 arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[1].pedantic_min_int,
3399 count->ts.kind);
3400 }
3401
3402 if (count_rate)
3403 {
3404 if (least >= 8)
3405 arg2 = gfc_create_var (gfc_get_int_type (8), "count_rate");
3406 else if (least == 4)
3407 arg2 = gfc_create_var (gfc_get_int_type (4), "count_rate");
3408 else
3409 arg2 = integer_zero_node;
3410 }
3411
3412 if (count_max)
3413 {
3414 if (least >= 8)
3415 arg3 = gfc_create_var (gfc_get_int_type (8), "count_max");
3416 else if (least == 4)
3417 arg3 = gfc_create_var (gfc_get_int_type (4), "count_max");
3418 else
3419 arg3 = integer_zero_node;
3420 }
3421
3422 /* Make the function call. */
3423 gfc_init_block (&block);
3424
3425 if (least <= 2)
3426 {
3427 if (least == 1)
3428 {
3429 arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3430 : null_pointer_node;
3431 arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3432 : null_pointer_node;
3433 arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3434 : null_pointer_node;
3435 }
3436
3437 if (least == 2)
3438 {
3439 arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3440 : null_pointer_node;
3441 arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3442 : null_pointer_node;
3443 arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3444 : null_pointer_node;
3445 }
3446 }
3447 else
3448 {
3449 if (least == 4)
3450 {
3451 tmp = build_call_expr_loc (input_location,
3452 gfor_fndecl_system_clock4, 3,
3453 arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3454 : null_pointer_node,
3455 arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3456 : null_pointer_node,
3457 arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3458 : null_pointer_node);
3459 gfc_add_expr_to_block (&block, tmp);
3460 }
3461 /* Handle kind>=8, 10, or 16 arguments */
3462 if (least >= 8)
3463 {
3464 tmp = build_call_expr_loc (input_location,
3465 gfor_fndecl_system_clock8, 3,
3466 arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3467 : null_pointer_node,
3468 arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3469 : null_pointer_node,
3470 arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3471 : null_pointer_node);
3472 gfc_add_expr_to_block (&block, tmp);
3473 }
3474 }
3475
3476 /* And store values back if needed. */
3477 if (arg1 && arg1 != count_se.expr)
3478 gfc_add_modify (&block, count_se.expr,
3479 fold_convert (TREE_TYPE (count_se.expr), arg1));
3480 if (arg2 && arg2 != count_rate_se.expr)
3481 gfc_add_modify (&block, count_rate_se.expr,
3482 fold_convert (TREE_TYPE (count_rate_se.expr), arg2));
3483 if (arg3 && arg3 != count_max_se.expr)
3484 gfc_add_modify (&block, count_max_se.expr,
3485 fold_convert (TREE_TYPE (count_max_se.expr), arg3));
3486
3487 return gfc_finish_block (&block);
3488 }
3489
3490
3491 /* Return a character string containing the tty name. */
3492
3493 static void
3494 gfc_conv_intrinsic_ttynam (gfc_se * se, gfc_expr * expr)
3495 {
3496 tree var;
3497 tree len;
3498 tree tmp;
3499 tree cond;
3500 tree fndecl;
3501 tree *args;
3502 unsigned int num_args;
3503
3504 num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3505 args = XALLOCAVEC (tree, num_args);
3506
3507 var = gfc_create_var (pchar_type_node, "pstr");
3508 len = gfc_create_var (gfc_charlen_type_node, "len");
3509
3510 gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3511 args[0] = gfc_build_addr_expr (NULL_TREE, var);
3512 args[1] = gfc_build_addr_expr (NULL_TREE, len);
3513
3514 fndecl = build_addr (gfor_fndecl_ttynam);
3515 tmp = build_call_array_loc (input_location,
3516 TREE_TYPE (TREE_TYPE (gfor_fndecl_ttynam)),
3517 fndecl, num_args, args);
3518 gfc_add_expr_to_block (&se->pre, tmp);
3519
3520 /* Free the temporary afterwards, if necessary. */
3521 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3522 len, build_int_cst (TREE_TYPE (len), 0));
3523 tmp = gfc_call_free (var);
3524 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3525 gfc_add_expr_to_block (&se->post, tmp);
3526
3527 se->expr = var;
3528 se->string_length = len;
3529 }
3530
3531
3532 /* Get the minimum/maximum value of all the parameters.
3533 minmax (a1, a2, a3, ...)
3534 {
3535 mvar = a1;
3536 mvar = COMP (mvar, a2)
3537 mvar = COMP (mvar, a3)
3538 ...
3539 return mvar;
3540 }
3541 Where COMP is MIN/MAX_EXPR for integral types or when we don't
3542 care about NaNs, or IFN_FMIN/MAX when the target has support for
3543 fast NaN-honouring min/max. When neither holds expand a sequence
3544 of explicit comparisons. */
3545
3546 /* TODO: Mismatching types can occur when specific names are used.
3547 These should be handled during resolution. */
3548 static void
3549 gfc_conv_intrinsic_minmax (gfc_se * se, gfc_expr * expr, enum tree_code op)
3550 {
3551 tree tmp;
3552 tree mvar;
3553 tree val;
3554 tree *args;
3555 tree type;
3556 tree argtype;
3557 gfc_actual_arglist *argexpr;
3558 unsigned int i, nargs;
3559
3560 nargs = gfc_intrinsic_argument_list_length (expr);
3561 args = XALLOCAVEC (tree, nargs);
3562
3563 gfc_conv_intrinsic_function_args (se, expr, args, nargs);
3564 type = gfc_typenode_for_spec (&expr->ts);
3565
3566 /* Only evaluate the argument once. */
3567 if (!VAR_P (args[0]) && !TREE_CONSTANT (args[0]))
3568 args[0] = gfc_evaluate_now (args[0], &se->pre);
3569
3570 /* Determine suitable type of temporary, as a GNU extension allows
3571 different argument kinds. */
3572 argtype = TREE_TYPE (args[0]);
3573 argexpr = expr->value.function.actual;
3574 for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3575 {
3576 tree tmptype = TREE_TYPE (args[i]);
3577 if (TYPE_PRECISION (tmptype) > TYPE_PRECISION (argtype))
3578 argtype = tmptype;
3579 }
3580 mvar = gfc_create_var (argtype, "M");
3581 gfc_add_modify (&se->pre, mvar, convert (argtype, args[0]));
3582
3583 argexpr = expr->value.function.actual;
3584 for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3585 {
3586 tree cond = NULL_TREE;
3587 val = args[i];
3588
3589 /* Handle absent optional arguments by ignoring the comparison. */
3590 if (argexpr->expr->expr_type == EXPR_VARIABLE
3591 && argexpr->expr->symtree->n.sym->attr.optional
3592 && INDIRECT_REF_P (val))
3593 {
3594 cond = fold_build2_loc (input_location,
3595 NE_EXPR, logical_type_node,
3596 TREE_OPERAND (val, 0),
3597 build_int_cst (TREE_TYPE (TREE_OPERAND (val, 0)), 0));
3598 }
3599 else if (!VAR_P (val) && !TREE_CONSTANT (val))
3600 /* Only evaluate the argument once. */
3601 val = gfc_evaluate_now (val, &se->pre);
3602
3603 tree calc;
3604 /* For floating point types, the question is what MAX(a, NaN) or
3605 MIN(a, NaN) should return (where "a" is a normal number).
3606 There are valid use case for returning either one, but the
3607 Fortran standard doesn't specify which one should be chosen.
3608 Also, there is no consensus among other tested compilers. In
3609 short, it's a mess. So lets just do whatever is fastest. */
3610 tree_code code = op == GT_EXPR ? MAX_EXPR : MIN_EXPR;
3611 calc = fold_build2_loc (input_location, code, argtype,
3612 convert (argtype, val), mvar);
3613 tmp = build2_v (MODIFY_EXPR, mvar, calc);
3614
3615 if (cond != NULL_TREE)
3616 tmp = build3_v (COND_EXPR, cond, tmp,
3617 build_empty_stmt (input_location));
3618 gfc_add_expr_to_block (&se->pre, tmp);
3619 }
3620 se->expr = convert (type, mvar);
3621 }
3622
3623
3624 /* Generate library calls for MIN and MAX intrinsics for character
3625 variables. */
3626 static void
3627 gfc_conv_intrinsic_minmax_char (gfc_se * se, gfc_expr * expr, int op)
3628 {
3629 tree *args;
3630 tree var, len, fndecl, tmp, cond, function;
3631 unsigned int nargs;
3632
3633 nargs = gfc_intrinsic_argument_list_length (expr);
3634 args = XALLOCAVEC (tree, nargs + 4);
3635 gfc_conv_intrinsic_function_args (se, expr, &args[4], nargs);
3636
3637 /* Create the result variables. */
3638 len = gfc_create_var (gfc_charlen_type_node, "len");
3639 args[0] = gfc_build_addr_expr (NULL_TREE, len);
3640 var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
3641 args[1] = gfc_build_addr_expr (ppvoid_type_node, var);
3642 args[2] = build_int_cst (integer_type_node, op);
3643 args[3] = build_int_cst (integer_type_node, nargs / 2);
3644
3645 if (expr->ts.kind == 1)
3646 function = gfor_fndecl_string_minmax;
3647 else if (expr->ts.kind == 4)
3648 function = gfor_fndecl_string_minmax_char4;
3649 else
3650 gcc_unreachable ();
3651
3652 /* Make the function call. */
3653 fndecl = build_addr (function);
3654 tmp = build_call_array_loc (input_location,
3655 TREE_TYPE (TREE_TYPE (function)), fndecl,
3656 nargs + 4, args);
3657 gfc_add_expr_to_block (&se->pre, tmp);
3658
3659 /* Free the temporary afterwards, if necessary. */
3660 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3661 len, build_int_cst (TREE_TYPE (len), 0));
3662 tmp = gfc_call_free (var);
3663 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3664 gfc_add_expr_to_block (&se->post, tmp);
3665
3666 se->expr = var;
3667 se->string_length = len;
3668 }
3669
3670
3671 /* Create a symbol node for this intrinsic. The symbol from the frontend
3672 has the generic name. */
3673
3674 static gfc_symbol *
3675 gfc_get_symbol_for_expr (gfc_expr * expr, bool ignore_optional)
3676 {
3677 gfc_symbol *sym;
3678
3679 /* TODO: Add symbols for intrinsic function to the global namespace. */
3680 gcc_assert (strlen (expr->value.function.name) <= GFC_MAX_SYMBOL_LEN - 5);
3681 sym = gfc_new_symbol (expr->value.function.name, NULL);
3682
3683 sym->ts = expr->ts;
3684 if (sym->ts.type == BT_CHARACTER)
3685 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3686 sym->attr.external = 1;
3687 sym->attr.function = 1;
3688 sym->attr.always_explicit = 1;
3689 sym->attr.proc = PROC_INTRINSIC;
3690 sym->attr.flavor = FL_PROCEDURE;
3691 sym->result = sym;
3692 if (expr->rank > 0)
3693 {
3694 sym->attr.dimension = 1;
3695 sym->as = gfc_get_array_spec ();
3696 sym->as->type = AS_ASSUMED_SHAPE;
3697 sym->as->rank = expr->rank;
3698 }
3699
3700 gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3701 ignore_optional ? expr->value.function.actual
3702 : NULL);
3703
3704 return sym;
3705 }
3706
3707 /* Remove empty actual arguments. */
3708
3709 static void
3710 remove_empty_actual_arguments (gfc_actual_arglist **ap)
3711 {
3712 while (*ap)
3713 {
3714 if ((*ap)->expr == NULL)
3715 {
3716 gfc_actual_arglist *r = *ap;
3717 *ap = r->next;
3718 r->next = NULL;
3719 gfc_free_actual_arglist (r);
3720 }
3721 else
3722 ap = &((*ap)->next);
3723 }
3724 }
3725
3726 #define MAX_SPEC_ARG 12
3727
3728 /* Make up an fn spec that's right for intrinsic functions that we
3729 want to call. */
3730
3731 static char *
3732 intrinsic_fnspec (gfc_expr *expr)
3733 {
3734 static char fnspec_buf[MAX_SPEC_ARG*2+1];
3735 char *fp;
3736 int i;
3737 int num_char_args;
3738
3739 #define ADD_CHAR(c) do { *fp++ = c; *fp++ = ' '; } while(0)
3740
3741 /* Set the fndecl. */
3742 fp = fnspec_buf;
3743 /* Function return value. FIXME: Check if the second letter could
3744 be something other than a space, for further optimization. */
3745 ADD_CHAR ('.');
3746 if (expr->rank == 0)
3747 {
3748 if (expr->ts.type == BT_CHARACTER)
3749 {
3750 ADD_CHAR ('w'); /* Address of character. */
3751 ADD_CHAR ('.'); /* Length of character. */
3752 }
3753 }
3754 else
3755 ADD_CHAR ('w'); /* Return value is a descriptor. */
3756
3757 num_char_args = 0;
3758 for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
3759 {
3760 if (a->expr == NULL)
3761 continue;
3762
3763 if (a->name && strcmp (a->name,"%VAL") == 0)
3764 ADD_CHAR ('.');
3765 else
3766 {
3767 if (a->expr->rank > 0)
3768 ADD_CHAR ('r');
3769 else
3770 ADD_CHAR ('R');
3771 }
3772 num_char_args += a->expr->ts.type == BT_CHARACTER;
3773 gcc_assert (fp - fnspec_buf + num_char_args <= MAX_SPEC_ARG*2);
3774 }
3775
3776 for (i = 0; i < num_char_args; i++)
3777 ADD_CHAR ('.');
3778
3779 *fp = '\0';
3780 return fnspec_buf;
3781 }
3782
3783 #undef MAX_SPEC_ARG
3784 #undef ADD_CHAR
3785
3786 /* Generate the right symbol for the specific intrinsic function and
3787 modify the expr accordingly. This assumes that absent optional
3788 arguments should be removed. */
3789
3790 gfc_symbol *
3791 specific_intrinsic_symbol (gfc_expr *expr)
3792 {
3793 gfc_symbol *sym;
3794
3795 sym = gfc_find_intrinsic_symbol (expr);
3796 if (sym == NULL)
3797 {
3798 sym = gfc_get_intrinsic_function_symbol (expr);
3799 sym->ts = expr->ts;
3800 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl)
3801 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
3802
3803 gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3804 expr->value.function.actual, true);
3805 sym->backend_decl
3806 = gfc_get_extern_function_decl (sym, expr->value.function.actual,
3807 intrinsic_fnspec (expr));
3808 }
3809
3810 remove_empty_actual_arguments (&(expr->value.function.actual));
3811
3812 return sym;
3813 }
3814
3815 /* Generate a call to an external intrinsic function. FIXME: So far,
3816 this only works for functions which are called with well-defined
3817 types; CSHIFT and friends will come later. */
3818
3819 static void
3820 gfc_conv_intrinsic_funcall (gfc_se * se, gfc_expr * expr)
3821 {
3822 gfc_symbol *sym;
3823 vec<tree, va_gc> *append_args;
3824 bool specific_symbol;
3825
3826 gcc_assert (!se->ss || se->ss->info->expr == expr);
3827
3828 if (se->ss)
3829 gcc_assert (expr->rank > 0);
3830 else
3831 gcc_assert (expr->rank == 0);
3832
3833 switch (expr->value.function.isym->id)
3834 {
3835 case GFC_ISYM_ANY:
3836 case GFC_ISYM_ALL:
3837 case GFC_ISYM_FINDLOC:
3838 case GFC_ISYM_MAXLOC:
3839 case GFC_ISYM_MINLOC:
3840 case GFC_ISYM_MAXVAL:
3841 case GFC_ISYM_MINVAL:
3842 case GFC_ISYM_NORM2:
3843 case GFC_ISYM_PRODUCT:
3844 case GFC_ISYM_SUM:
3845 specific_symbol = true;
3846 break;
3847 default:
3848 specific_symbol = false;
3849 }
3850
3851 if (specific_symbol)
3852 {
3853 /* Need to copy here because specific_intrinsic_symbol modifies
3854 expr to omit the absent optional arguments. */
3855 expr = gfc_copy_expr (expr);
3856 sym = specific_intrinsic_symbol (expr);
3857 }
3858 else
3859 sym = gfc_get_symbol_for_expr (expr, se->ignore_optional);
3860
3861 /* Calls to libgfortran_matmul need to be appended special arguments,
3862 to be able to call the BLAS ?gemm functions if required and possible. */
3863 append_args = NULL;
3864 if (expr->value.function.isym->id == GFC_ISYM_MATMUL
3865 && !expr->external_blas
3866 && sym->ts.type != BT_LOGICAL)
3867 {
3868 tree cint = gfc_get_int_type (gfc_c_int_kind);
3869
3870 if (flag_external_blas
3871 && (sym->ts.type == BT_REAL || sym->ts.type == BT_COMPLEX)
3872 && (sym->ts.kind == 4 || sym->ts.kind == 8))
3873 {
3874 tree gemm_fndecl;
3875
3876 if (sym->ts.type == BT_REAL)
3877 {
3878 if (sym->ts.kind == 4)
3879 gemm_fndecl = gfor_fndecl_sgemm;
3880 else
3881 gemm_fndecl = gfor_fndecl_dgemm;
3882 }
3883 else
3884 {
3885 if (sym->ts.kind == 4)
3886 gemm_fndecl = gfor_fndecl_cgemm;
3887 else
3888 gemm_fndecl = gfor_fndecl_zgemm;
3889 }
3890
3891 vec_alloc (append_args, 3);
3892 append_args->quick_push (build_int_cst (cint, 1));
3893 append_args->quick_push (build_int_cst (cint,
3894 flag_blas_matmul_limit));
3895 append_args->quick_push (gfc_build_addr_expr (NULL_TREE,
3896 gemm_fndecl));
3897 }
3898 else
3899 {
3900 vec_alloc (append_args, 3);
3901 append_args->quick_push (build_int_cst (cint, 0));
3902 append_args->quick_push (build_int_cst (cint, 0));
3903 append_args->quick_push (null_pointer_node);
3904 }
3905 }
3906 /* Non-character scalar reduce returns a pointer to a result of size set by
3907 the element size of 'array'. Setting 'sym' allocatable ensures that the
3908 result is deallocated at the appropriate time. */
3909 else if (expr->value.function.isym->id == GFC_ISYM_REDUCE
3910 && expr->rank == 0 && expr->ts.type != BT_CHARACTER)
3911 sym->attr.allocatable = 1;
3912
3913
3914 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
3915 append_args);
3916
3917 if (specific_symbol)
3918 gfc_free_expr (expr);
3919 else
3920 gfc_free_symbol (sym);
3921 }
3922
3923 /* ANY and ALL intrinsics. ANY->op == NE_EXPR, ALL->op == EQ_EXPR.
3924 Implemented as
3925 any(a)
3926 {
3927 forall (i=...)
3928 if (a[i] != 0)
3929 return 1
3930 end forall
3931 return 0
3932 }
3933 all(a)
3934 {
3935 forall (i=...)
3936 if (a[i] == 0)
3937 return 0
3938 end forall
3939 return 1
3940 }
3941 */
3942 static void
3943 gfc_conv_intrinsic_anyall (gfc_se * se, gfc_expr * expr, enum tree_code op)
3944 {
3945 tree resvar;
3946 stmtblock_t block;
3947 stmtblock_t body;
3948 tree type;
3949 tree tmp;
3950 tree found;
3951 gfc_loopinfo loop;
3952 gfc_actual_arglist *actual;
3953 gfc_ss *arrayss;
3954 gfc_se arrayse;
3955 tree exit_label;
3956
3957 if (se->ss)
3958 {
3959 gfc_conv_intrinsic_funcall (se, expr);
3960 return;
3961 }
3962
3963 actual = expr->value.function.actual;
3964 type = gfc_typenode_for_spec (&expr->ts);
3965 /* Initialize the result. */
3966 resvar = gfc_create_var (type, "test");
3967 if (op == EQ_EXPR)
3968 tmp = convert (type, boolean_true_node);
3969 else
3970 tmp = convert (type, boolean_false_node);
3971 gfc_add_modify (&se->pre, resvar, tmp);
3972
3973 /* Walk the arguments. */
3974 arrayss = gfc_walk_expr (actual->expr);
3975 gcc_assert (arrayss != gfc_ss_terminator);
3976
3977 /* Initialize the scalarizer. */
3978 gfc_init_loopinfo (&loop);
3979 exit_label = gfc_build_label_decl (NULL_TREE);
3980 TREE_USED (exit_label) = 1;
3981 gfc_add_ss_to_loop (&loop, arrayss);
3982
3983 /* Initialize the loop. */
3984 gfc_conv_ss_startstride (&loop);
3985 gfc_conv_loop_setup (&loop, &expr->where);
3986
3987 gfc_mark_ss_chain_used (arrayss, 1);
3988 /* Generate the loop body. */
3989 gfc_start_scalarized_body (&loop, &body);
3990
3991 /* If the condition matches then set the return value. */
3992 gfc_start_block (&block);
3993 if (op == EQ_EXPR)
3994 tmp = convert (type, boolean_false_node);
3995 else
3996 tmp = convert (type, boolean_true_node);
3997 gfc_add_modify (&block, resvar, tmp);
3998
3999 /* And break out of the loop. */
4000 tmp = build1_v (GOTO_EXPR, exit_label);
4001 gfc_add_expr_to_block (&block, tmp);
4002
4003 found = gfc_finish_block (&block);
4004
4005 /* Check this element. */
4006 gfc_init_se (&arrayse, NULL);
4007 gfc_copy_loopinfo_to_se (&arrayse, &loop);
4008 arrayse.ss = arrayss;
4009 gfc_conv_expr_val (&arrayse, actual->expr);
4010
4011 gfc_add_block_to_block (&body, &arrayse.pre);
4012 tmp = fold_build2_loc (input_location, op, logical_type_node, arrayse.expr,
4013 build_int_cst (TREE_TYPE (arrayse.expr), 0));
4014 tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
4015 gfc_add_expr_to_block (&body, tmp);
4016 gfc_add_block_to_block (&body, &arrayse.post);
4017
4018 gfc_trans_scalarizing_loops (&loop, &body);
4019
4020 /* Add the exit label. */
4021 tmp = build1_v (LABEL_EXPR, exit_label);
4022 gfc_add_expr_to_block (&loop.pre, tmp);
4023
4024 gfc_add_block_to_block (&se->pre, &loop.pre);
4025 gfc_add_block_to_block (&se->pre, &loop.post);
4026 gfc_cleanup_loop (&loop);
4027
4028 se->expr = resvar;
4029 }
4030
4031
4032 /* Generate the constant 180 / pi, which is used in the conversion
4033 of acosd(), asind(), atand(), atan2d(). */
4034
4035 static tree
4036 rad2deg (int kind)
4037 {
4038 tree retval;
4039 mpfr_t pi, t0;
4040
4041 gfc_set_model_kind (kind);
4042 mpfr_init (pi);
4043 mpfr_init (t0);
4044 mpfr_set_si (t0, 180, GFC_RND_MODE);
4045 mpfr_const_pi (pi, GFC_RND_MODE);
4046 mpfr_div (t0, t0, pi, GFC_RND_MODE);
4047 retval = gfc_conv_mpfr_to_tree (t0, kind, 0);
4048 mpfr_clear (t0);
4049 mpfr_clear (pi);
4050 return retval;
4051 }
4052
4053
4054 static gfc_intrinsic_map_t *
4055 gfc_lookup_intrinsic (gfc_isym_id id)
4056 {
4057 gfc_intrinsic_map_t *m = gfc_intrinsic_map;
4058 for (; m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
4059 if (id == m->id)
4060 break;
4061 gcc_assert (id == m->id);
4062 return m;
4063 }
4064
4065
4066 /* ACOSD(x) is translated into ACOS(x) * 180 / pi.
4067 ASIND(x) is translated into ASIN(x) * 180 / pi.
4068 ATAND(x) is translated into ATAN(x) * 180 / pi. */
4069
4070 static void
4071 gfc_conv_intrinsic_atrigd (gfc_se * se, gfc_expr * expr, gfc_isym_id id)
4072 {
4073 tree arg;
4074 tree atrigd;
4075 tree type;
4076 gfc_intrinsic_map_t *m;
4077
4078 type = gfc_typenode_for_spec (&expr->ts);
4079
4080 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4081
4082 switch (id)
4083 {
4084 case GFC_ISYM_ACOSD:
4085 m = gfc_lookup_intrinsic (GFC_ISYM_ACOS);
4086 break;
4087 case GFC_ISYM_ASIND:
4088 m = gfc_lookup_intrinsic (GFC_ISYM_ASIN);
4089 break;
4090 case GFC_ISYM_ATAND:
4091 m = gfc_lookup_intrinsic (GFC_ISYM_ATAN);
4092 break;
4093 default:
4094 gcc_unreachable ();
4095 }
4096 atrigd = gfc_get_intrinsic_lib_fndecl (m, expr);
4097 atrigd = build_call_expr_loc (input_location, atrigd, 1, arg);
4098
4099 se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atrigd,
4100 fold_convert (type, rad2deg (expr->ts.kind)));
4101 }
4102
4103
4104 /* COTAN(X) is translated into -TAN(X+PI/2) for REAL argument and
4105 COS(X) / SIN(X) for COMPLEX argument. */
4106
4107 static void
4108 gfc_conv_intrinsic_cotan (gfc_se *se, gfc_expr *expr)
4109 {
4110 gfc_intrinsic_map_t *m;
4111 tree arg;
4112 tree type;
4113
4114 type = gfc_typenode_for_spec (&expr->ts);
4115 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4116
4117 if (expr->ts.type == BT_REAL)
4118 {
4119 tree tan;
4120 tree tmp;
4121 mpfr_t pio2;
4122
4123 /* Create pi/2. */
4124 gfc_set_model_kind (expr->ts.kind);
4125 mpfr_init (pio2);
4126 mpfr_const_pi (pio2, GFC_RND_MODE);
4127 mpfr_div_ui (pio2, pio2, 2, GFC_RND_MODE);
4128 tmp = gfc_conv_mpfr_to_tree (pio2, expr->ts.kind, 0);
4129 mpfr_clear (pio2);
4130
4131 /* Find tan builtin function. */
4132 m = gfc_lookup_intrinsic (GFC_ISYM_TAN);
4133 tan = gfc_get_intrinsic_lib_fndecl (m, expr);
4134 tmp = fold_build2_loc (input_location, PLUS_EXPR, type, arg, tmp);
4135 tan = build_call_expr_loc (input_location, tan, 1, tmp);
4136 se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tan);
4137 }
4138 else
4139 {
4140 tree sin;
4141 tree cos;
4142
4143 /* Find cos builtin function. */
4144 m = gfc_lookup_intrinsic (GFC_ISYM_COS);
4145 cos = gfc_get_intrinsic_lib_fndecl (m, expr);
4146 cos = build_call_expr_loc (input_location, cos, 1, arg);
4147
4148 /* Find sin builtin function. */
4149 m = gfc_lookup_intrinsic (GFC_ISYM_SIN);
4150 sin = gfc_get_intrinsic_lib_fndecl (m, expr);
4151 sin = build_call_expr_loc (input_location, sin, 1, arg);
4152
4153 /* Divide cos by sin. */
4154 se->expr = fold_build2_loc (input_location, RDIV_EXPR, type, cos, sin);
4155 }
4156 }
4157
4158
4159 /* COTAND(X) is translated into -TAND(X+90) for REAL argument. */
4160
4161 static void
4162 gfc_conv_intrinsic_cotand (gfc_se *se, gfc_expr *expr)
4163 {
4164 tree arg;
4165 tree type;
4166 tree ninety_tree;
4167 mpfr_t ninety;
4168
4169 type = gfc_typenode_for_spec (&expr->ts);
4170 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4171
4172 gfc_set_model_kind (expr->ts.kind);
4173
4174 /* Build the tree for x + 90. */
4175 mpfr_init_set_ui (ninety, 90, GFC_RND_MODE);
4176 ninety_tree = gfc_conv_mpfr_to_tree (ninety, expr->ts.kind, 0);
4177 arg = fold_build2_loc (input_location, PLUS_EXPR, type, arg, ninety_tree);
4178 mpfr_clear (ninety);
4179
4180 /* Find tand. */
4181 gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_TAND);
4182 tree tand = gfc_get_intrinsic_lib_fndecl (m, expr);
4183 tand = build_call_expr_loc (input_location, tand, 1, arg);
4184
4185 se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tand);
4186 }
4187
4188
4189 /* ATAN2D(Y,X) is translated into ATAN2(Y,X) * 180 / PI. */
4190
4191 static void
4192 gfc_conv_intrinsic_atan2d (gfc_se *se, gfc_expr *expr)
4193 {
4194 tree args[2];
4195 tree atan2d;
4196 tree type;
4197
4198 gfc_conv_intrinsic_function_args (se, expr, args, 2);
4199 type = TREE_TYPE (args[0]);
4200
4201 gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_ATAN2);
4202 atan2d = gfc_get_intrinsic_lib_fndecl (m, expr);
4203 atan2d = build_call_expr_loc (input_location, atan2d, 2, args[0], args[1]);
4204
4205 se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atan2d,
4206 rad2deg (expr->ts.kind));
4207 }
4208
4209
4210 /* COUNT(A) = Number of true elements in A. */
4211 static void
4212 gfc_conv_intrinsic_count (gfc_se * se, gfc_expr * expr)
4213 {
4214 tree resvar;
4215 tree type;
4216 stmtblock_t body;
4217 tree tmp;
4218 gfc_loopinfo loop;
4219 gfc_actual_arglist *actual;
4220 gfc_ss *arrayss;
4221 gfc_se arrayse;
4222
4223 if (se->ss)
4224 {
4225 gfc_conv_intrinsic_funcall (se, expr);
4226 return;
4227 }
4228
4229 actual = expr->value.function.actual;
4230
4231 type = gfc_typenode_for_spec (&expr->ts);
4232 /* Initialize the result. */
4233 resvar = gfc_create_var (type, "count");
4234 gfc_add_modify (&se->pre, resvar, build_int_cst (type, 0));
4235
4236 /* Walk the arguments. */
4237 arrayss = gfc_walk_expr (actual->expr);
4238 gcc_assert (arrayss != gfc_ss_terminator);
4239
4240 /* Initialize the scalarizer. */
4241 gfc_init_loopinfo (&loop);
4242 gfc_add_ss_to_loop (&loop, arrayss);
4243
4244 /* Initialize the loop. */
4245 gfc_conv_ss_startstride (&loop);
4246 gfc_conv_loop_setup (&loop, &expr->where);
4247
4248 gfc_mark_ss_chain_used (arrayss, 1);
4249 /* Generate the loop body. */
4250 gfc_start_scalarized_body (&loop, &body);
4251
4252 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (resvar),
4253 resvar, build_int_cst (TREE_TYPE (resvar), 1));
4254 tmp = build2_v (MODIFY_EXPR, resvar, tmp);
4255
4256 gfc_init_se (&arrayse, NULL);
4257 gfc_copy_loopinfo_to_se (&arrayse, &loop);
4258 arrayse.ss = arrayss;
4259 gfc_conv_expr_val (&arrayse, actual->expr);
4260 tmp = build3_v (COND_EXPR, arrayse.expr, tmp,
4261 build_empty_stmt (input_location));
4262
4263 gfc_add_block_to_block (&body, &arrayse.pre);
4264 gfc_add_expr_to_block (&body, tmp);
4265 gfc_add_block_to_block (&body, &arrayse.post);
4266
4267 gfc_trans_scalarizing_loops (&loop, &body);
4268
4269 gfc_add_block_to_block (&se->pre, &loop.pre);
4270 gfc_add_block_to_block (&se->pre, &loop.post);
4271 gfc_cleanup_loop (&loop);
4272
4273 se->expr = resvar;
4274 }
4275
4276
4277 /* Update given gfc_se to have ss component pointing to the nested gfc_ss
4278 struct and return the corresponding loopinfo. */
4279
4280 static gfc_loopinfo *
4281 enter_nested_loop (gfc_se *se)
4282 {
4283 se->ss = se->ss->nested_ss;
4284 gcc_assert (se->ss == se->ss->loop->ss);
4285
4286 return se->ss->loop;
4287 }
4288
4289 /* Build the condition for a mask, which may be optional. */
4290
4291 static tree
4292 conv_mask_condition (gfc_se *maskse, gfc_expr *maskexpr,
4293 bool optional_mask)
4294 {
4295 tree present;
4296 tree type;
4297
4298 if (optional_mask)
4299 {
4300 type = TREE_TYPE (maskse->expr);
4301 present = gfc_conv_expr_present (maskexpr->symtree->n.sym);
4302 present = convert (type, present);
4303 present = fold_build1_loc (input_location, TRUTH_NOT_EXPR, type,
4304 present);
4305 return fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
4306 type, present, maskse->expr);
4307 }
4308 else
4309 return maskse->expr;
4310 }
4311
4312 /* Inline implementation of the sum and product intrinsics. */
4313 static void
4314 gfc_conv_intrinsic_arith (gfc_se * se, gfc_expr * expr, enum tree_code op,
4315 bool norm2)
4316 {
4317 tree resvar;
4318 tree scale = NULL_TREE;
4319 tree type;
4320 stmtblock_t body;
4321 stmtblock_t block;
4322 tree tmp;
4323 gfc_loopinfo loop, *ploop;
4324 gfc_actual_arglist *arg_array, *arg_mask;
4325 gfc_ss *arrayss = NULL;
4326 gfc_ss *maskss = NULL;
4327 gfc_se arrayse;
4328 gfc_se maskse;
4329 gfc_se *parent_se;
4330 gfc_expr *arrayexpr;
4331 gfc_expr *maskexpr;
4332 bool optional_mask;
4333
4334 if (expr->rank > 0)
4335 {
4336 gcc_assert (gfc_inline_intrinsic_function_p (expr));
4337 parent_se = se;
4338 }
4339 else
4340 parent_se = NULL;
4341
4342 type = gfc_typenode_for_spec (&expr->ts);
4343 /* Initialize the result. */
4344 resvar = gfc_create_var (type, "val");
4345 if (norm2)
4346 {
4347 /* result = 0.0;
4348 scale = 1.0. */
4349 scale = gfc_create_var (type, "scale");
4350 gfc_add_modify (&se->pre, scale,
4351 gfc_build_const (type, integer_one_node));
4352 tmp = gfc_build_const (type, integer_zero_node);
4353 }
4354 else if (op == PLUS_EXPR || op == BIT_IOR_EXPR || op == BIT_XOR_EXPR)
4355 tmp = gfc_build_const (type, integer_zero_node);
4356 else if (op == NE_EXPR)
4357 /* PARITY. */
4358 tmp = convert (type, boolean_false_node);
4359 else if (op == BIT_AND_EXPR)
4360 tmp = gfc_build_const (type, fold_build1_loc (input_location, NEGATE_EXPR,
4361 type, integer_one_node));
4362 else
4363 tmp = gfc_build_const (type, integer_one_node);
4364
4365 gfc_add_modify (&se->pre, resvar, tmp);
4366
4367 arg_array = expr->value.function.actual;
4368
4369 arrayexpr = arg_array->expr;
4370
4371 if (op == NE_EXPR || norm2)
4372 {
4373 /* PARITY and NORM2. */
4374 maskexpr = NULL;
4375 optional_mask = false;
4376 }
4377 else
4378 {
4379 arg_mask = arg_array->next->next;
4380 gcc_assert (arg_mask != NULL);
4381 maskexpr = arg_mask->expr;
4382 optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
4383 && maskexpr->symtree->n.sym->attr.dummy
4384 && maskexpr->symtree->n.sym->attr.optional;
4385 }
4386
4387 if (expr->rank == 0)
4388 {
4389 /* Walk the arguments. */
4390 arrayss = gfc_walk_expr (arrayexpr);
4391 gcc_assert (arrayss != gfc_ss_terminator);
4392
4393 if (maskexpr && maskexpr->rank > 0)
4394 {
4395 maskss = gfc_walk_expr (maskexpr);
4396 gcc_assert (maskss != gfc_ss_terminator);
4397 }
4398 else
4399 maskss = NULL;
4400
4401 /* Initialize the scalarizer. */
4402 gfc_init_loopinfo (&loop);
4403
4404 /* We add the mask first because the number of iterations is
4405 taken from the last ss, and this breaks if an absent
4406 optional argument is used for mask. */
4407
4408 if (maskexpr && maskexpr->rank > 0)
4409 gfc_add_ss_to_loop (&loop, maskss);
4410 gfc_add_ss_to_loop (&loop, arrayss);
4411
4412 /* Initialize the loop. */
4413 gfc_conv_ss_startstride (&loop);
4414 gfc_conv_loop_setup (&loop, &expr->where);
4415
4416 if (maskexpr && maskexpr->rank > 0)
4417 gfc_mark_ss_chain_used (maskss, 1);
4418 gfc_mark_ss_chain_used (arrayss, 1);
4419
4420 ploop = &loop;
4421 }
4422 else
4423 /* All the work has been done in the parent loops. */
4424 ploop = enter_nested_loop (se);
4425
4426 gcc_assert (ploop);
4427
4428 /* Generate the loop body. */
4429 gfc_start_scalarized_body (ploop, &body);
4430
4431 /* If we have a mask, only add this element if the mask is set. */
4432 if (maskexpr && maskexpr->rank > 0)
4433 {
4434 gfc_init_se (&maskse, parent_se);
4435 gfc_copy_loopinfo_to_se (&maskse, ploop);
4436 if (expr->rank == 0)
4437 maskse.ss = maskss;
4438 gfc_conv_expr_val (&maskse, maskexpr);
4439 gfc_add_block_to_block (&body, &maskse.pre);
4440
4441 gfc_start_block (&block);
4442 }
4443 else
4444 gfc_init_block (&block);
4445
4446 /* Do the actual summation/product. */
4447 gfc_init_se (&arrayse, parent_se);
4448 gfc_copy_loopinfo_to_se (&arrayse, ploop);
4449 if (expr->rank == 0)
4450 arrayse.ss = arrayss;
4451 gfc_conv_expr_val (&arrayse, arrayexpr);
4452 gfc_add_block_to_block (&block, &arrayse.pre);
4453
4454 if (norm2)
4455 {
4456 /* if (x (i) != 0.0)
4457 {
4458 absX = abs(x(i))
4459 if (absX > scale)
4460 {
4461 val = scale/absX;
4462 result = 1.0 + result * val * val;
4463 scale = absX;
4464 }
4465 else
4466 {
4467 val = absX/scale;
4468 result += val * val;
4469 }
4470 } */
4471 tree res1, res2, cond, absX, val;
4472 stmtblock_t ifblock1, ifblock2, ifblock3;
4473
4474 gfc_init_block (&ifblock1);
4475
4476 absX = gfc_create_var (type, "absX");
4477 gfc_add_modify (&ifblock1, absX,
4478 fold_build1_loc (input_location, ABS_EXPR, type,
4479 arrayse.expr));
4480 val = gfc_create_var (type, "val");
4481 gfc_add_expr_to_block (&ifblock1, val);
4482
4483 gfc_init_block (&ifblock2);
4484 gfc_add_modify (&ifblock2, val,
4485 fold_build2_loc (input_location, RDIV_EXPR, type, scale,
4486 absX));
4487 res1 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4488 res1 = fold_build2_loc (input_location, MULT_EXPR, type, resvar, res1);
4489 res1 = fold_build2_loc (input_location, PLUS_EXPR, type, res1,
4490 gfc_build_const (type, integer_one_node));
4491 gfc_add_modify (&ifblock2, resvar, res1);
4492 gfc_add_modify (&ifblock2, scale, absX);
4493 res1 = gfc_finish_block (&ifblock2);
4494
4495 gfc_init_block (&ifblock3);
4496 gfc_add_modify (&ifblock3, val,
4497 fold_build2_loc (input_location, RDIV_EXPR, type, absX,
4498 scale));
4499 res2 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4500 res2 = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, res2);
4501 gfc_add_modify (&ifblock3, resvar, res2);
4502 res2 = gfc_finish_block (&ifblock3);
4503
4504 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
4505 absX, scale);
4506 tmp = build3_v (COND_EXPR, cond, res1, res2);
4507 gfc_add_expr_to_block (&ifblock1, tmp);
4508 tmp = gfc_finish_block (&ifblock1);
4509
4510 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
4511 arrayse.expr,
4512 gfc_build_const (type, integer_zero_node));
4513
4514 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
4515 gfc_add_expr_to_block (&block, tmp);
4516 }
4517 else
4518 {
4519 tmp = fold_build2_loc (input_location, op, type, resvar, arrayse.expr);
4520 gfc_add_modify (&block, resvar, tmp);
4521 }
4522
4523 gfc_add_block_to_block (&block, &arrayse.post);
4524
4525 if (maskexpr && maskexpr->rank > 0)
4526 {
4527 /* We enclose the above in if (mask) {...} . If the mask is an
4528 optional argument, generate
4529 IF (.NOT. PRESENT(MASK) .OR. MASK(I)). */
4530 tree ifmask;
4531 tmp = gfc_finish_block (&block);
4532 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4533 tmp = build3_v (COND_EXPR, ifmask, tmp,
4534 build_empty_stmt (input_location));
4535 }
4536 else
4537 tmp = gfc_finish_block (&block);
4538 gfc_add_expr_to_block (&body, tmp);
4539
4540 gfc_trans_scalarizing_loops (ploop, &body);
4541
4542 /* For a scalar mask, enclose the loop in an if statement. */
4543 if (maskexpr && maskexpr->rank == 0)
4544 {
4545 gfc_init_block (&block);
4546 gfc_add_block_to_block (&block, &ploop->pre);
4547 gfc_add_block_to_block (&block, &ploop->post);
4548 tmp = gfc_finish_block (&block);
4549
4550 if (expr->rank > 0)
4551 {
4552 tmp = build3_v (COND_EXPR, se->ss->info->data.scalar.value, tmp,
4553 build_empty_stmt (input_location));
4554 gfc_advance_se_ss_chain (se);
4555 }
4556 else
4557 {
4558 tree ifmask;
4559
4560 gcc_assert (expr->rank == 0);
4561 gfc_init_se (&maskse, NULL);
4562 gfc_conv_expr_val (&maskse, maskexpr);
4563 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4564 tmp = build3_v (COND_EXPR, ifmask, tmp,
4565 build_empty_stmt (input_location));
4566 }
4567
4568 gfc_add_expr_to_block (&block, tmp);
4569 gfc_add_block_to_block (&se->pre, &block);
4570 gcc_assert (se->post.head == NULL);
4571 }
4572 else
4573 {
4574 gfc_add_block_to_block (&se->pre, &ploop->pre);
4575 gfc_add_block_to_block (&se->pre, &ploop->post);
4576 }
4577
4578 if (expr->rank == 0)
4579 gfc_cleanup_loop (ploop);
4580
4581 if (norm2)
4582 {
4583 /* result = scale * sqrt(result). */
4584 tree sqrt;
4585 sqrt = gfc_builtin_decl_for_float_kind (BUILT_IN_SQRT, expr->ts.kind);
4586 resvar = build_call_expr_loc (input_location,
4587 sqrt, 1, resvar);
4588 resvar = fold_build2_loc (input_location, MULT_EXPR, type, scale, resvar);
4589 }
4590
4591 se->expr = resvar;
4592 }
4593
4594
4595 /* Inline implementation of the dot_product intrinsic. This function
4596 is based on gfc_conv_intrinsic_arith (the previous function). */
4597 static void
4598 gfc_conv_intrinsic_dot_product (gfc_se * se, gfc_expr * expr)
4599 {
4600 tree resvar;
4601 tree type;
4602 stmtblock_t body;
4603 stmtblock_t block;
4604 tree tmp;
4605 gfc_loopinfo loop;
4606 gfc_actual_arglist *actual;
4607 gfc_ss *arrayss1, *arrayss2;
4608 gfc_se arrayse1, arrayse2;
4609 gfc_expr *arrayexpr1, *arrayexpr2;
4610
4611 type = gfc_typenode_for_spec (&expr->ts);
4612
4613 /* Initialize the result. */
4614 resvar = gfc_create_var (type, "val");
4615 if (expr->ts.type == BT_LOGICAL)
4616 tmp = build_int_cst (type, 0);
4617 else
4618 tmp = gfc_build_const (type, integer_zero_node);
4619
4620 gfc_add_modify (&se->pre, resvar, tmp);
4621
4622 /* Walk argument #1. */
4623 actual = expr->value.function.actual;
4624 arrayexpr1 = actual->expr;
4625 arrayss1 = gfc_walk_expr (arrayexpr1);
4626 gcc_assert (arrayss1 != gfc_ss_terminator);
4627
4628 /* Walk argument #2. */
4629 actual = actual->next;
4630 arrayexpr2 = actual->expr;
4631 arrayss2 = gfc_walk_expr (arrayexpr2);
4632 gcc_assert (arrayss2 != gfc_ss_terminator);
4633
4634 /* Initialize the scalarizer. */
4635 gfc_init_loopinfo (&loop);
4636 gfc_add_ss_to_loop (&loop, arrayss1);
4637 gfc_add_ss_to_loop (&loop, arrayss2);
4638
4639 /* Initialize the loop. */
4640 gfc_conv_ss_startstride (&loop);
4641 gfc_conv_loop_setup (&loop, &expr->where);
4642
4643 gfc_mark_ss_chain_used (arrayss1, 1);
4644 gfc_mark_ss_chain_used (arrayss2, 1);
4645
4646 /* Generate the loop body. */
4647 gfc_start_scalarized_body (&loop, &body);
4648 gfc_init_block (&block);
4649
4650 /* Make the tree expression for [conjg(]array1[)]. */
4651 gfc_init_se (&arrayse1, NULL);
4652 gfc_copy_loopinfo_to_se (&arrayse1, &loop);
4653 arrayse1.ss = arrayss1;
4654 gfc_conv_expr_val (&arrayse1, arrayexpr1);
4655 if (expr->ts.type == BT_COMPLEX)
4656 arrayse1.expr = fold_build1_loc (input_location, CONJ_EXPR, type,
4657 arrayse1.expr);
4658 gfc_add_block_to_block (&block, &arrayse1.pre);
4659
4660 /* Make the tree expression for array2. */
4661 gfc_init_se (&arrayse2, NULL);
4662 gfc_copy_loopinfo_to_se (&arrayse2, &loop);
4663 arrayse2.ss = arrayss2;
4664 gfc_conv_expr_val (&arrayse2, arrayexpr2);
4665 gfc_add_block_to_block (&block, &arrayse2.pre);
4666
4667 /* Do the actual product and sum. */
4668 if (expr->ts.type == BT_LOGICAL)
4669 {
4670 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, type,
4671 arrayse1.expr, arrayse2.expr);
4672 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, type, resvar, tmp);
4673 }
4674 else
4675 {
4676 tmp = fold_build2_loc (input_location, MULT_EXPR, type, arrayse1.expr,
4677 arrayse2.expr);
4678 tmp = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, tmp);
4679 }
4680 gfc_add_modify (&block, resvar, tmp);
4681
4682 /* Finish up the loop block and the loop. */
4683 tmp = gfc_finish_block (&block);
4684 gfc_add_expr_to_block (&body, tmp);
4685
4686 gfc_trans_scalarizing_loops (&loop, &body);
4687 gfc_add_block_to_block (&se->pre, &loop.pre);
4688 gfc_add_block_to_block (&se->pre, &loop.post);
4689 gfc_cleanup_loop (&loop);
4690
4691 se->expr = resvar;
4692 }
4693
4694
4695 /* Tells whether the expression E is a reference to an optional variable whose
4696 presence is not known at compile time. Those are variable references without
4697 subreference; if there is a subreference, we can assume the variable is
4698 present. We have to special case full arrays, which we represent with a fake
4699 "full" reference, and class descriptors for which a reference to data is not
4700 really a subreference. */
4701
4702 bool
4703 maybe_absent_optional_variable (gfc_expr *e)
4704 {
4705 if (!(e && e->expr_type == EXPR_VARIABLE))
4706 return false;
4707
4708 gfc_symbol *sym = e->symtree->n.sym;
4709 if (!sym->attr.optional)
4710 return false;
4711
4712 gfc_ref *ref = e->ref;
4713 if (ref == nullptr)
4714 return true;
4715
4716 if (ref->type == REF_ARRAY
4717 && ref->u.ar.type == AR_FULL
4718 && ref->next == nullptr)
4719 return true;
4720
4721 if (!(sym->ts.type == BT_CLASS
4722 && ref->type == REF_COMPONENT
4723 && ref->u.c.component == CLASS_DATA (sym)))
4724 return false;
4725
4726 gfc_ref *next_ref = ref->next;
4727 if (next_ref == nullptr)
4728 return true;
4729
4730 if (next_ref->type == REF_ARRAY
4731 && next_ref->u.ar.type == AR_FULL
4732 && next_ref->next == nullptr)
4733 return true;
4734
4735 return false;
4736 }
4737
4738
4739 /* Emit code for minloc or maxloc intrinsic. There are many different cases
4740 we need to handle. For performance reasons we sometimes create two
4741 loops instead of one, where the second one is much simpler.
4742 Examples for minloc intrinsic:
4743 A: Result is scalar.
4744 1) Array mask is used and NaNs need to be supported:
4745 limit = Infinity;
4746 pos = 0;
4747 S = from;
4748 while (S <= to) {
4749 if (mask[S]) {
4750 if (pos == 0) pos = S + (1 - from);
4751 if (a[S] <= limit) {
4752 limit = a[S];
4753 pos = S + (1 - from);
4754 goto lab1;
4755 }
4756 }
4757 S++;
4758 }
4759 goto lab2;
4760 lab1:;
4761 while (S <= to) {
4762 if (mask[S])
4763 if (a[S] < limit) {
4764 limit = a[S];
4765 pos = S + (1 - from);
4766 }
4767 S++;
4768 }
4769 lab2:;
4770 2) NaNs need to be supported, but it is known at compile time or cheaply
4771 at runtime whether array is nonempty or not:
4772 limit = Infinity;
4773 pos = 0;
4774 S = from;
4775 while (S <= to) {
4776 if (a[S] <= limit) {
4777 limit = a[S];
4778 pos = S + (1 - from);
4779 goto lab1;
4780 }
4781 S++;
4782 }
4783 if (from <= to) pos = 1;
4784 goto lab2;
4785 lab1:;
4786 while (S <= to) {
4787 if (a[S] < limit) {
4788 limit = a[S];
4789 pos = S + (1 - from);
4790 }
4791 S++;
4792 }
4793 lab2:;
4794 3) NaNs aren't supported, array mask is used:
4795 limit = infinities_supported ? Infinity : huge (limit);
4796 pos = 0;
4797 S = from;
4798 while (S <= to) {
4799 if (mask[S]) {
4800 limit = a[S];
4801 pos = S + (1 - from);
4802 goto lab1;
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 4) Same without array mask:
4818 limit = infinities_supported ? Infinity : huge (limit);
4819 pos = (from <= to) ? 1 : 0;
4820 S = from;
4821 while (S <= to) {
4822 if (a[S] < limit) {
4823 limit = a[S];
4824 pos = S + (1 - from);
4825 }
4826 S++;
4827 }
4828 B: Array result, non-CHARACTER type, DIM absent
4829 Generate similar code as in the scalar case, using a collection of
4830 variables (one per dimension) instead of a single variable as result.
4831 Picking only cases 1) and 4) with ARRAY of rank 2, the generated code
4832 becomes:
4833 1) Array mask is used and NaNs need to be supported:
4834 limit = Infinity;
4835 pos0 = 0;
4836 pos1 = 0;
4837 S1 = from1;
4838 second_loop_entry = false;
4839 while (S1 <= to1) {
4840 S0 = from0;
4841 while (s0 <= to0 {
4842 if (mask[S1][S0]) {
4843 if (pos0 == 0) {
4844 pos0 = S0 + (1 - from0);
4845 pos1 = S1 + (1 - from1);
4846 }
4847 if (a[S1][S0] <= limit) {
4848 limit = a[S1][S0];
4849 pos0 = S0 + (1 - from0);
4850 pos1 = S1 + (1 - from1);
4851 second_loop_entry = true;
4852 goto lab1;
4853 }
4854 }
4855 S0++;
4856 }
4857 S1++;
4858 }
4859 goto lab2;
4860 lab1:;
4861 S1 = second_loop_entry ? S1 : from1;
4862 while (S1 <= to1) {
4863 S0 = second_loop_entry ? S0 : from0;
4864 while (S0 <= to0) {
4865 if (mask[S1][S0])
4866 if (a[S1][S0] < limit) {
4867 limit = a[S1][S0];
4868 pos0 = S + (1 - from0);
4869 pos1 = S + (1 - from1);
4870 }
4871 second_loop_entry = false;
4872 S0++;
4873 }
4874 S1++;
4875 }
4876 lab2:;
4877 result = { pos0, pos1 };
4878 ...
4879 4) NANs aren't supported, no array mask.
4880 limit = infinities_supported ? Infinity : huge (limit);
4881 pos0 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4882 pos1 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4883 S1 = from1;
4884 while (S1 <= to1) {
4885 S0 = from0;
4886 while (S0 <= to0) {
4887 if (a[S1][S0] < limit) {
4888 limit = a[S1][S0];
4889 pos0 = S + (1 - from0);
4890 pos1 = S + (1 - from1);
4891 }
4892 S0++;
4893 }
4894 S1++;
4895 }
4896 result = { pos0, pos1 };
4897 C: Otherwise, a call is generated.
4898 For 2) and 4), if mask is scalar, this all goes into a conditional,
4899 setting pos = 0; in the else branch.
4900
4901 Since we now also support the BACK argument, instead of using
4902 if (a[S] < limit), we now use
4903
4904 if (back)
4905 cond = a[S] <= limit;
4906 else
4907 cond = a[S] < limit;
4908 if (cond) {
4909 ....
4910
4911 The optimizer is smart enough to move the condition out of the loop.
4912 They are now marked as unlikely too for further speedup. */
4913
4914 static void
4915 gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op)
4916 {
4917 stmtblock_t body;
4918 stmtblock_t block;
4919 stmtblock_t ifblock;
4920 stmtblock_t elseblock;
4921 tree limit;
4922 tree type;
4923 tree tmp;
4924 tree cond;
4925 tree elsetmp;
4926 tree ifbody;
4927 tree offset[GFC_MAX_DIMENSIONS];
4928 tree nonempty;
4929 tree lab1, lab2;
4930 tree b_if, b_else;
4931 tree back;
4932 gfc_loopinfo loop, *ploop;
4933 gfc_actual_arglist *array_arg, *dim_arg, *mask_arg, *kind_arg;
4934 gfc_actual_arglist *back_arg;
4935 gfc_ss *arrayss = nullptr;
4936 gfc_ss *maskss = nullptr;
4937 gfc_ss *orig_ss = nullptr;
4938 gfc_se arrayse;
4939 gfc_se maskse;
4940 gfc_se nested_se;
4941 gfc_se *base_se;
4942 gfc_expr *arrayexpr;
4943 gfc_expr *maskexpr;
4944 gfc_expr *backexpr;
4945 gfc_se backse;
4946 tree pos[GFC_MAX_DIMENSIONS];
4947 tree idx[GFC_MAX_DIMENSIONS];
4948 tree result_var = NULL_TREE;
4949 int n;
4950 bool optional_mask;
4951
4952 array_arg = expr->value.function.actual;
4953 dim_arg = array_arg->next;
4954 mask_arg = dim_arg->next;
4955 kind_arg = mask_arg->next;
4956 back_arg = kind_arg->next;
4957
4958 bool dim_present = dim_arg->expr != nullptr;
4959 bool nested_loop = dim_present && expr->rank > 0;
4960
4961 /* Remove kind. */
4962 if (kind_arg->expr)
4963 {
4964 gfc_free_expr (kind_arg->expr);
4965 kind_arg->expr = NULL;
4966 }
4967
4968 /* Pass BACK argument by value. */
4969 back_arg->name = "%VAL";
4970
4971 if (se->ss)
4972 {
4973 if (se->ss->info->useflags)
4974 {
4975 if (!dim_present || !gfc_inline_intrinsic_function_p (expr))
4976 {
4977 /* The code generating and initializing the result array has been
4978 generated already before the scalarization loop, either with a
4979 library function call or with inline code; now we can just use
4980 the result. */
4981 gfc_conv_tmp_array_ref (se);
4982 return;
4983 }
4984 }
4985 else if (!gfc_inline_intrinsic_function_p (expr))
4986 {
4987 gfc_conv_intrinsic_funcall (se, expr);
4988 return;
4989 }
4990 }
4991
4992 arrayexpr = array_arg->expr;
4993
4994 /* Special case for character maxloc. Remove unneeded "dim" actual
4995 argument, then call a library function. */
4996
4997 if (arrayexpr->ts.type == BT_CHARACTER)
4998 {
4999 gcc_assert (expr->rank == 0);
5000
5001 if (dim_arg->expr)
5002 {
5003 gfc_free_expr (dim_arg->expr);
5004 dim_arg->expr = NULL;
5005 }
5006 gfc_conv_intrinsic_funcall (se, expr);
5007 return;
5008 }
5009
5010 type = gfc_typenode_for_spec (&expr->ts);
5011
5012 if (expr->rank > 0 && !dim_present)
5013 {
5014 gfc_array_spec as;
5015 memset (&as, 0, sizeof (as));
5016
5017 as.rank = 1;
5018 as.lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
5019 &arrayexpr->where,
5020 HOST_WIDE_INT_1);
5021 as.upper[0] = gfc_get_int_expr (gfc_index_integer_kind,
5022 &arrayexpr->where,
5023 arrayexpr->rank);
5024
5025 tree array = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
5026
5027 result_var = gfc_create_var (array, "loc_result");
5028 }
5029
5030 const int reduction_dimensions = dim_present ? 1 : arrayexpr->rank;
5031
5032 /* Initialize the result. */
5033 for (int i = 0; i < reduction_dimensions; i++)
5034 {
5035 pos[i] = gfc_create_var (gfc_array_index_type,
5036 gfc_get_string ("pos%d", i));
5037 offset[i] = gfc_create_var (gfc_array_index_type,
5038 gfc_get_string ("offset%d", i));
5039 idx[i] = gfc_create_var (gfc_array_index_type,
5040 gfc_get_string ("idx%d", i));
5041 }
5042
5043 maskexpr = mask_arg->expr;
5044 optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5045 && maskexpr->symtree->n.sym->attr.dummy
5046 && maskexpr->symtree->n.sym->attr.optional;
5047 backexpr = back_arg->expr;
5048
5049 gfc_init_se (&backse, nested_loop ? se : nullptr);
5050 if (backexpr == nullptr)
5051 back = logical_false_node;
5052 else if (maybe_absent_optional_variable (backexpr))
5053 {
5054 /* This should have been checked already by
5055 maybe_absent_optional_variable. */
5056 gcc_checking_assert (backexpr->expr_type == EXPR_VARIABLE);
5057
5058 gfc_conv_expr (&backse, backexpr);
5059 tree present = gfc_conv_expr_present (backexpr->symtree->n.sym, false);
5060 back = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5061 logical_type_node, present, backse.expr);
5062 }
5063 else
5064 {
5065 gfc_conv_expr (&backse, backexpr);
5066 back = backse.expr;
5067 }
5068 gfc_add_block_to_block (&se->pre, &backse.pre);
5069 back = gfc_evaluate_now_loc (input_location, back, &se->pre);
5070 gfc_add_block_to_block (&se->pre, &backse.post);
5071
5072 if (nested_loop)
5073 {
5074 gfc_init_se (&nested_se, se);
5075 base_se = &nested_se;
5076 }
5077 else
5078 {
5079 /* Walk the arguments. */
5080 arrayss = gfc_walk_expr (arrayexpr);
5081 gcc_assert (arrayss != gfc_ss_terminator);
5082
5083 if (maskexpr && maskexpr->rank != 0)
5084 {
5085 maskss = gfc_walk_expr (maskexpr);
5086 gcc_assert (maskss != gfc_ss_terminator);
5087 }
5088
5089 base_se = nullptr;
5090 }
5091
5092 nonempty = nullptr;
5093 if (!(maskexpr && maskexpr->rank > 0))
5094 {
5095 mpz_t asize;
5096 bool reduction_size_known;
5097
5098 if (dim_present)
5099 {
5100 int reduction_dim;
5101 if (dim_arg->expr->expr_type == EXPR_CONSTANT)
5102 reduction_dim = mpz_get_si (dim_arg->expr->value.integer) - 1;
5103 else if (arrayexpr->rank == 1)
5104 reduction_dim = 0;
5105 else
5106 gcc_unreachable ();
5107 reduction_size_known = gfc_array_dimen_size (arrayexpr, reduction_dim,
5108 &asize);
5109 }
5110 else
5111 reduction_size_known = gfc_array_size (arrayexpr, &asize);
5112
5113 if (reduction_size_known)
5114 {
5115 nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
5116 mpz_clear (asize);
5117 nonempty = fold_build2_loc (input_location, GT_EXPR,
5118 logical_type_node, nonempty,
5119 gfc_index_zero_node);
5120 }
5121 maskss = NULL;
5122 }
5123
5124 limit = gfc_create_var (gfc_typenode_for_spec (&arrayexpr->ts), "limit");
5125 switch (arrayexpr->ts.type)
5126 {
5127 case BT_REAL:
5128 tmp = gfc_build_inf_or_huge (TREE_TYPE (limit), arrayexpr->ts.kind);
5129 break;
5130
5131 case BT_INTEGER:
5132 n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5133 tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
5134 arrayexpr->ts.kind);
5135 break;
5136
5137 case BT_UNSIGNED:
5138 /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
5139 if (op == GT_EXPR)
5140 {
5141 tmp = gfc_get_unsigned_type (arrayexpr->ts.kind);
5142 tmp = build_int_cst (tmp, 0);
5143 }
5144 else
5145 {
5146 n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5147 tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
5148 expr->ts.kind);
5149 }
5150 break;
5151
5152 default:
5153 gcc_unreachable ();
5154 }
5155
5156 /* We start with the most negative possible value for MAXLOC, and the most
5157 positive possible value for MINLOC. The most negative possible value is
5158 -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
5159 possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
5160 with above. */
5161 if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
5162 tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
5163 if (op == GT_EXPR && arrayexpr->ts.type == BT_INTEGER)
5164 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp), tmp,
5165 build_int_cst (TREE_TYPE (tmp), 1));
5166
5167 gfc_add_modify (&se->pre, limit, tmp);
5168
5169 /* If we are in a case where we generate two sets of loops, the second one
5170 should continue where the first stopped instead of restarting from the
5171 beginning. So nested loops in the second set should have a partial range
5172 on the first iteration, but they should start from the beginning and span
5173 their full range on the following iterations. So we use conditionals in
5174 the loops lower bounds, and use the following variable in those
5175 conditionals to decide whether to use the original loop bound or to use
5176 the index at which the loop from the first set stopped. */
5177 tree second_loop_entry = gfc_create_var (logical_type_node,
5178 "second_loop_entry");
5179 gfc_add_modify (&se->pre, second_loop_entry, logical_false_node);
5180
5181 if (nested_loop)
5182 {
5183 ploop = enter_nested_loop (&nested_se);
5184 orig_ss = nested_se.ss;
5185 ploop->temp_dim = 1;
5186 }
5187 else
5188 {
5189 /* Initialize the scalarizer. */
5190 gfc_init_loopinfo (&loop);
5191
5192 /* We add the mask first because the number of iterations is taken
5193 from the last ss, and this breaks if an absent optional argument
5194 is used for mask. */
5195
5196 if (maskss)
5197 gfc_add_ss_to_loop (&loop, maskss);
5198
5199 gfc_add_ss_to_loop (&loop, arrayss);
5200
5201 /* Initialize the loop. */
5202 gfc_conv_ss_startstride (&loop);
5203
5204 /* The code generated can have more than one loop in sequence (see the
5205 comment at the function header). This doesn't work well with the
5206 scalarizer, which changes arrays' offset when the scalarization loops
5207 are generated (see gfc_trans_preloop_setup). Fortunately, we can use
5208 the scalarizer temporary code to handle multiple loops. Thus, we set
5209 temp_dim here, we call gfc_mark_ss_chain_used with flag=3 later, and
5210 we use gfc_trans_scalarized_loop_boundary even later to restore
5211 offset. */
5212 loop.temp_dim = loop.dimen;
5213 gfc_conv_loop_setup (&loop, &expr->where);
5214
5215 ploop = &loop;
5216 }
5217
5218 gcc_assert (reduction_dimensions == ploop->dimen);
5219
5220 if (nonempty == NULL && !(maskexpr && maskexpr->rank > 0))
5221 {
5222 nonempty = logical_true_node;
5223
5224 for (int i = 0; i < ploop->dimen; i++)
5225 {
5226 if (!(ploop->from[i] && ploop->to[i]))
5227 {
5228 nonempty = NULL;
5229 break;
5230 }
5231
5232 tree tmp = fold_build2_loc (input_location, LE_EXPR,
5233 logical_type_node, ploop->from[i],
5234 ploop->to[i]);
5235
5236 nonempty = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5237 logical_type_node, nonempty, tmp);
5238 }
5239 }
5240
5241 lab1 = NULL;
5242 lab2 = NULL;
5243 /* Initialize the position to zero, following Fortran 2003. We are free
5244 to do this because Fortran 95 allows the result of an entirely false
5245 mask to be processor dependent. If we know at compile time the array
5246 is non-empty and no MASK is used, we can initialize to 1 to simplify
5247 the inner loop. */
5248 if (nonempty != NULL && !HONOR_NANS (DECL_MODE (limit)))
5249 {
5250 tree init = fold_build3_loc (input_location, COND_EXPR,
5251 gfc_array_index_type, nonempty,
5252 gfc_index_one_node,
5253 gfc_index_zero_node);
5254 for (int i = 0; i < ploop->dimen; i++)
5255 gfc_add_modify (&ploop->pre, pos[i], init);
5256 }
5257 else
5258 {
5259 for (int i = 0; i < ploop->dimen; i++)
5260 gfc_add_modify (&ploop->pre, pos[i], gfc_index_zero_node);
5261 lab1 = gfc_build_label_decl (NULL_TREE);
5262 TREE_USED (lab1) = 1;
5263 lab2 = gfc_build_label_decl (NULL_TREE);
5264 TREE_USED (lab2) = 1;
5265 }
5266
5267 /* An offset must be added to the loop
5268 counter to obtain the required position. */
5269 for (int i = 0; i < ploop->dimen; i++)
5270 {
5271 gcc_assert (ploop->from[i]);
5272
5273 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5274 gfc_index_one_node, ploop->from[i]);
5275 gfc_add_modify (&ploop->pre, offset[i], tmp);
5276 }
5277
5278 if (!nested_loop)
5279 {
5280 gfc_mark_ss_chain_used (arrayss, lab1 ? 3 : 1);
5281 if (maskss)
5282 gfc_mark_ss_chain_used (maskss, lab1 ? 3 : 1);
5283 }
5284
5285 /* Generate the loop body. */
5286 gfc_start_scalarized_body (ploop, &body);
5287
5288 /* If we have a mask, only check this element if the mask is set. */
5289 if (maskexpr && maskexpr->rank > 0)
5290 {
5291 gfc_init_se (&maskse, base_se);
5292 gfc_copy_loopinfo_to_se (&maskse, ploop);
5293 if (!nested_loop)
5294 maskse.ss = maskss;
5295 gfc_conv_expr_val (&maskse, maskexpr);
5296 gfc_add_block_to_block (&body, &maskse.pre);
5297
5298 gfc_start_block (&block);
5299 }
5300 else
5301 gfc_init_block (&block);
5302
5303 /* Compare with the current limit. */
5304 gfc_init_se (&arrayse, base_se);
5305 gfc_copy_loopinfo_to_se (&arrayse, ploop);
5306 if (!nested_loop)
5307 arrayse.ss = arrayss;
5308 gfc_conv_expr_val (&arrayse, arrayexpr);
5309 gfc_add_block_to_block (&block, &arrayse.pre);
5310
5311 /* We do the following if this is a more extreme value. */
5312 gfc_start_block (&ifblock);
5313
5314 /* Assign the value to the limit... */
5315 gfc_add_modify (&ifblock, limit, arrayse.expr);
5316
5317 if (nonempty == NULL && HONOR_NANS (DECL_MODE (limit)))
5318 {
5319 stmtblock_t ifblock2;
5320 tree ifbody2;
5321
5322 gfc_start_block (&ifblock2);
5323 for (int i = 0; i < ploop->dimen; i++)
5324 {
5325 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5326 ploop->loopvar[i], offset[i]);
5327 gfc_add_modify (&ifblock2, pos[i], tmp);
5328 }
5329 ifbody2 = gfc_finish_block (&ifblock2);
5330
5331 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5332 pos[0], gfc_index_zero_node);
5333 tmp = build3_v (COND_EXPR, cond, ifbody2,
5334 build_empty_stmt (input_location));
5335 gfc_add_expr_to_block (&block, tmp);
5336 }
5337
5338 for (int i = 0; i < ploop->dimen; i++)
5339 {
5340 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5341 ploop->loopvar[i], offset[i]);
5342 gfc_add_modify (&ifblock, pos[i], tmp);
5343 gfc_add_modify (&ifblock, idx[i], ploop->loopvar[i]);
5344 }
5345
5346 gfc_add_modify (&ifblock, second_loop_entry, logical_true_node);
5347
5348 if (lab1)
5349 gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab1));
5350
5351 ifbody = gfc_finish_block (&ifblock);
5352
5353 if (!lab1 || HONOR_NANS (DECL_MODE (limit)))
5354 {
5355 if (lab1)
5356 cond = fold_build2_loc (input_location,
5357 op == GT_EXPR ? GE_EXPR : LE_EXPR,
5358 logical_type_node, arrayse.expr, limit);
5359 else
5360 {
5361 tree ifbody2, elsebody2;
5362
5363 /* We switch to > or >= depending on the value of the BACK argument. */
5364 cond = gfc_create_var (logical_type_node, "cond");
5365
5366 gfc_start_block (&ifblock);
5367 b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5368 logical_type_node, arrayse.expr, limit);
5369
5370 gfc_add_modify (&ifblock, cond, b_if);
5371 ifbody2 = gfc_finish_block (&ifblock);
5372
5373 gfc_start_block (&elseblock);
5374 b_else = fold_build2_loc (input_location, op, logical_type_node,
5375 arrayse.expr, limit);
5376
5377 gfc_add_modify (&elseblock, cond, b_else);
5378 elsebody2 = gfc_finish_block (&elseblock);
5379
5380 tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5381 back, ifbody2, elsebody2);
5382
5383 gfc_add_expr_to_block (&block, tmp);
5384 }
5385
5386 cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5387 ifbody = build3_v (COND_EXPR, cond, ifbody,
5388 build_empty_stmt (input_location));
5389 }
5390 gfc_add_expr_to_block (&block, ifbody);
5391
5392 if (maskexpr && maskexpr->rank > 0)
5393 {
5394 /* We enclose the above in if (mask) {...}. If the mask is an
5395 optional argument, generate IF (.NOT. PRESENT(MASK)
5396 .OR. MASK(I)). */
5397
5398 tree ifmask;
5399 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5400 tmp = gfc_finish_block (&block);
5401 tmp = build3_v (COND_EXPR, ifmask, tmp,
5402 build_empty_stmt (input_location));
5403 }
5404 else
5405 tmp = gfc_finish_block (&block);
5406 gfc_add_expr_to_block (&body, tmp);
5407
5408 if (lab1)
5409 {
5410 for (int i = 0; i < ploop->dimen; i++)
5411 ploop->from[i] = fold_build3_loc (input_location, COND_EXPR,
5412 TREE_TYPE (ploop->from[i]),
5413 second_loop_entry, idx[i],
5414 ploop->from[i]);
5415
5416 gfc_trans_scalarized_loop_boundary (ploop, &body);
5417
5418 if (nested_loop)
5419 {
5420 /* The first loop already advanced the parent se'ss chain, so clear
5421 the parent now to avoid doing it a second time, making the chain
5422 out of sync. */
5423 nested_se.parent = nullptr;
5424 nested_se.ss = orig_ss;
5425 }
5426
5427 stmtblock_t * const outer_block = &ploop->code[ploop->dimen - 1];
5428
5429 if (HONOR_NANS (DECL_MODE (limit)))
5430 {
5431 if (nonempty != NULL)
5432 {
5433 stmtblock_t init_block;
5434 gfc_init_block (&init_block);
5435
5436 for (int i = 0; i < ploop->dimen; i++)
5437 gfc_add_modify (&init_block, pos[i], gfc_index_one_node);
5438
5439 tree ifbody = gfc_finish_block (&init_block);
5440 tmp = build3_v (COND_EXPR, nonempty, ifbody,
5441 build_empty_stmt (input_location));
5442 gfc_add_expr_to_block (outer_block, tmp);
5443 }
5444 }
5445
5446 gfc_add_expr_to_block (outer_block, build1_v (GOTO_EXPR, lab2));
5447 gfc_add_expr_to_block (outer_block, build1_v (LABEL_EXPR, lab1));
5448
5449 /* If we have a mask, only check this element if the mask is set. */
5450 if (maskexpr && maskexpr->rank > 0)
5451 {
5452 gfc_init_se (&maskse, base_se);
5453 gfc_copy_loopinfo_to_se (&maskse, ploop);
5454 if (!nested_loop)
5455 maskse.ss = maskss;
5456 gfc_conv_expr_val (&maskse, maskexpr);
5457 gfc_add_block_to_block (&body, &maskse.pre);
5458
5459 gfc_start_block (&block);
5460 }
5461 else
5462 gfc_init_block (&block);
5463
5464 /* Compare with the current limit. */
5465 gfc_init_se (&arrayse, base_se);
5466 gfc_copy_loopinfo_to_se (&arrayse, ploop);
5467 if (!nested_loop)
5468 arrayse.ss = arrayss;
5469 gfc_conv_expr_val (&arrayse, arrayexpr);
5470 gfc_add_block_to_block (&block, &arrayse.pre);
5471
5472 /* We do the following if this is a more extreme value. */
5473 gfc_start_block (&ifblock);
5474
5475 /* Assign the value to the limit... */
5476 gfc_add_modify (&ifblock, limit, arrayse.expr);
5477
5478 for (int i = 0; i < ploop->dimen; i++)
5479 {
5480 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5481 ploop->loopvar[i], offset[i]);
5482 gfc_add_modify (&ifblock, pos[i], tmp);
5483 }
5484
5485 ifbody = gfc_finish_block (&ifblock);
5486
5487 /* We switch to > or >= depending on the value of the BACK argument. */
5488 {
5489 tree ifbody2, elsebody2;
5490
5491 cond = gfc_create_var (logical_type_node, "cond");
5492
5493 gfc_start_block (&ifblock);
5494 b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5495 logical_type_node, arrayse.expr, limit);
5496
5497 gfc_add_modify (&ifblock, cond, b_if);
5498 ifbody2 = gfc_finish_block (&ifblock);
5499
5500 gfc_start_block (&elseblock);
5501 b_else = fold_build2_loc (input_location, op, logical_type_node,
5502 arrayse.expr, limit);
5503
5504 gfc_add_modify (&elseblock, cond, b_else);
5505 elsebody2 = gfc_finish_block (&elseblock);
5506
5507 tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5508 back, ifbody2, elsebody2);
5509 }
5510
5511 gfc_add_expr_to_block (&block, tmp);
5512 cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5513 tmp = build3_v (COND_EXPR, cond, ifbody,
5514 build_empty_stmt (input_location));
5515
5516 gfc_add_expr_to_block (&block, tmp);
5517
5518 if (maskexpr && maskexpr->rank > 0)
5519 {
5520 /* We enclose the above in if (mask) {...}. If the mask is
5521 an optional argument, generate IF (.NOT. PRESENT(MASK)
5522 .OR. MASK(I)).*/
5523
5524 tree ifmask;
5525 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5526 tmp = gfc_finish_block (&block);
5527 tmp = build3_v (COND_EXPR, ifmask, tmp,
5528 build_empty_stmt (input_location));
5529 }
5530 else
5531 tmp = gfc_finish_block (&block);
5532
5533 gfc_add_expr_to_block (&body, tmp);
5534 gfc_add_modify (&body, second_loop_entry, logical_false_node);
5535 }
5536
5537 gfc_trans_scalarizing_loops (ploop, &body);
5538
5539 if (lab2)
5540 gfc_add_expr_to_block (&ploop->pre, build1_v (LABEL_EXPR, lab2));
5541
5542 /* For a scalar mask, enclose the loop in an if statement. */
5543 if (maskexpr && maskexpr->rank == 0)
5544 {
5545 tree ifmask;
5546
5547 gfc_init_se (&maskse, nested_loop ? se : nullptr);
5548 gfc_conv_expr_val (&maskse, maskexpr);
5549 gfc_add_block_to_block (&se->pre, &maskse.pre);
5550 gfc_init_block (&block);
5551 gfc_add_block_to_block (&block, &ploop->pre);
5552 gfc_add_block_to_block (&block, &ploop->post);
5553 tmp = gfc_finish_block (&block);
5554
5555 /* For the else part of the scalar mask, just initialize
5556 the pos variable the same way as above. */
5557
5558 gfc_init_block (&elseblock);
5559 for (int i = 0; i < ploop->dimen; i++)
5560 gfc_add_modify (&elseblock, pos[i], gfc_index_zero_node);
5561 elsetmp = gfc_finish_block (&elseblock);
5562 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5563 tmp = build3_v (COND_EXPR, ifmask, tmp, elsetmp);
5564 gfc_add_expr_to_block (&block, tmp);
5565 gfc_add_block_to_block (&se->pre, &block);
5566 }
5567 else
5568 {
5569 gfc_add_block_to_block (&se->pre, &ploop->pre);
5570 gfc_add_block_to_block (&se->pre, &ploop->post);
5571 }
5572
5573 if (!nested_loop)
5574 gfc_cleanup_loop (&loop);
5575
5576 if (!dim_present)
5577 {
5578 for (int i = 0; i < arrayexpr->rank; i++)
5579 {
5580 tree res_idx = build_int_cst (gfc_array_index_type, i);
5581 tree res_arr_ref = gfc_build_array_ref (result_var, res_idx,
5582 NULL_TREE, true);
5583
5584 tree value = convert (type, pos[i]);
5585 gfc_add_modify (&se->pre, res_arr_ref, value);
5586 }
5587
5588 se->expr = result_var;
5589 }
5590 else
5591 se->expr = convert (type, pos[0]);
5592 }
5593
5594 /* Emit code for findloc. */
5595
5596 static void
5597 gfc_conv_intrinsic_findloc (gfc_se *se, gfc_expr *expr)
5598 {
5599 gfc_actual_arglist *array_arg, *value_arg, *dim_arg, *mask_arg,
5600 *kind_arg, *back_arg;
5601 gfc_expr *value_expr;
5602 int ikind;
5603 tree resvar;
5604 stmtblock_t block;
5605 stmtblock_t body;
5606 stmtblock_t loopblock;
5607 tree type;
5608 tree tmp;
5609 tree found;
5610 tree forward_branch = NULL_TREE;
5611 tree back_branch;
5612 gfc_loopinfo loop;
5613 gfc_ss *arrayss;
5614 gfc_ss *maskss;
5615 gfc_se arrayse;
5616 gfc_se valuese;
5617 gfc_se maskse;
5618 gfc_se backse;
5619 tree exit_label;
5620 gfc_expr *maskexpr;
5621 tree offset;
5622 int i;
5623 bool optional_mask;
5624
5625 array_arg = expr->value.function.actual;
5626 value_arg = array_arg->next;
5627 dim_arg = value_arg->next;
5628 mask_arg = dim_arg->next;
5629 kind_arg = mask_arg->next;
5630 back_arg = kind_arg->next;
5631
5632 /* Remove kind and set ikind. */
5633 if (kind_arg->expr)
5634 {
5635 ikind = mpz_get_si (kind_arg->expr->value.integer);
5636 gfc_free_expr (kind_arg->expr);
5637 kind_arg->expr = NULL;
5638 }
5639 else
5640 ikind = gfc_default_integer_kind;
5641
5642 value_expr = value_arg->expr;
5643
5644 /* Unless it's a string, pass VALUE by value. */
5645 if (value_expr->ts.type != BT_CHARACTER)
5646 value_arg->name = "%VAL";
5647
5648 /* Pass BACK argument by value. */
5649 back_arg->name = "%VAL";
5650
5651 /* Call the library if we have a character function or if
5652 rank > 0. */
5653 if (se->ss || array_arg->expr->ts.type == BT_CHARACTER)
5654 {
5655 se->ignore_optional = 1;
5656 if (expr->rank == 0)
5657 {
5658 /* Remove dim argument. */
5659 gfc_free_expr (dim_arg->expr);
5660 dim_arg->expr = NULL;
5661 }
5662 gfc_conv_intrinsic_funcall (se, expr);
5663 return;
5664 }
5665
5666 type = gfc_get_int_type (ikind);
5667
5668 /* Initialize the result. */
5669 resvar = gfc_create_var (gfc_array_index_type, "pos");
5670 gfc_add_modify (&se->pre, resvar, build_int_cst (gfc_array_index_type, 0));
5671 offset = gfc_create_var (gfc_array_index_type, "offset");
5672
5673 maskexpr = mask_arg->expr;
5674 optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5675 && maskexpr->symtree->n.sym->attr.dummy
5676 && maskexpr->symtree->n.sym->attr.optional;
5677
5678 /* Generate two loops, one for BACK=.true. and one for BACK=.false. */
5679
5680 for (i = 0 ; i < 2; i++)
5681 {
5682 /* Walk the arguments. */
5683 arrayss = gfc_walk_expr (array_arg->expr);
5684 gcc_assert (arrayss != gfc_ss_terminator);
5685
5686 if (maskexpr && maskexpr->rank != 0)
5687 {
5688 maskss = gfc_walk_expr (maskexpr);
5689 gcc_assert (maskss != gfc_ss_terminator);
5690 }
5691 else
5692 maskss = NULL;
5693
5694 /* Initialize the scalarizer. */
5695 gfc_init_loopinfo (&loop);
5696 exit_label = gfc_build_label_decl (NULL_TREE);
5697 TREE_USED (exit_label) = 1;
5698
5699 /* We add the mask first because the number of iterations is
5700 taken from the last ss, and this breaks if an absent
5701 optional argument is used for mask. */
5702
5703 if (maskss)
5704 gfc_add_ss_to_loop (&loop, maskss);
5705 gfc_add_ss_to_loop (&loop, arrayss);
5706
5707 /* Initialize the loop. */
5708 gfc_conv_ss_startstride (&loop);
5709 gfc_conv_loop_setup (&loop, &expr->where);
5710
5711 /* Calculate the offset. */
5712 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5713 gfc_index_one_node, loop.from[0]);
5714 gfc_add_modify (&loop.pre, offset, tmp);
5715
5716 gfc_mark_ss_chain_used (arrayss, 1);
5717 if (maskss)
5718 gfc_mark_ss_chain_used (maskss, 1);
5719
5720 /* The first loop is for BACK=.true. */
5721 if (i == 0)
5722 loop.reverse[0] = GFC_REVERSE_SET;
5723
5724 /* Generate the loop body. */
5725 gfc_start_scalarized_body (&loop, &body);
5726
5727 /* If we have an array mask, only add the element if it is
5728 set. */
5729 if (maskss)
5730 {
5731 gfc_init_se (&maskse, NULL);
5732 gfc_copy_loopinfo_to_se (&maskse, &loop);
5733 maskse.ss = maskss;
5734 gfc_conv_expr_val (&maskse, maskexpr);
5735 gfc_add_block_to_block (&body, &maskse.pre);
5736 }
5737
5738 /* If the condition matches then set the return value. */
5739 gfc_start_block (&block);
5740
5741 /* Add the offset. */
5742 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5743 TREE_TYPE (resvar),
5744 loop.loopvar[0], offset);
5745 gfc_add_modify (&block, resvar, tmp);
5746 /* And break out of the loop. */
5747 tmp = build1_v (GOTO_EXPR, exit_label);
5748 gfc_add_expr_to_block (&block, tmp);
5749
5750 found = gfc_finish_block (&block);
5751
5752 /* Check this element. */
5753 gfc_init_se (&arrayse, NULL);
5754 gfc_copy_loopinfo_to_se (&arrayse, &loop);
5755 arrayse.ss = arrayss;
5756 gfc_conv_expr_val (&arrayse, array_arg->expr);
5757 gfc_add_block_to_block (&body, &arrayse.pre);
5758
5759 gfc_init_se (&valuese, NULL);
5760 gfc_conv_expr_val (&valuese, value_arg->expr);
5761 gfc_add_block_to_block (&body, &valuese.pre);
5762
5763 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5764 arrayse.expr, valuese.expr);
5765
5766 tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
5767 if (maskss)
5768 {
5769 /* We enclose the above in if (mask) {...}. If the mask is
5770 an optional argument, generate IF (.NOT. PRESENT(MASK)
5771 .OR. MASK(I)). */
5772
5773 tree ifmask;
5774 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5775 tmp = build3_v (COND_EXPR, ifmask, tmp,
5776 build_empty_stmt (input_location));
5777 }
5778
5779 gfc_add_expr_to_block (&body, tmp);
5780 gfc_add_block_to_block (&body, &arrayse.post);
5781
5782 gfc_trans_scalarizing_loops (&loop, &body);
5783
5784 /* Add the exit label. */
5785 tmp = build1_v (LABEL_EXPR, exit_label);
5786 gfc_add_expr_to_block (&loop.pre, tmp);
5787 gfc_start_block (&loopblock);
5788 gfc_add_block_to_block (&loopblock, &loop.pre);
5789 gfc_add_block_to_block (&loopblock, &loop.post);
5790 if (i == 0)
5791 forward_branch = gfc_finish_block (&loopblock);
5792 else
5793 back_branch = gfc_finish_block (&loopblock);
5794
5795 gfc_cleanup_loop (&loop);
5796 }
5797
5798 /* Enclose the two loops in an IF statement. */
5799
5800 gfc_init_se (&backse, NULL);
5801 gfc_conv_expr_val (&backse, back_arg->expr);
5802 gfc_add_block_to_block (&se->pre, &backse.pre);
5803 tmp = build3_v (COND_EXPR, backse.expr, forward_branch, back_branch);
5804
5805 /* For a scalar mask, enclose the loop in an if statement. */
5806 if (maskexpr && maskss == NULL)
5807 {
5808 tree ifmask;
5809 tree if_stmt;
5810
5811 gfc_init_se (&maskse, NULL);
5812 gfc_conv_expr_val (&maskse, maskexpr);
5813 gfc_init_block (&block);
5814 gfc_add_expr_to_block (&block, maskse.expr);
5815 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5816 if_stmt = build3_v (COND_EXPR, ifmask, tmp,
5817 build_empty_stmt (input_location));
5818 gfc_add_expr_to_block (&block, if_stmt);
5819 tmp = gfc_finish_block (&block);
5820 }
5821
5822 gfc_add_expr_to_block (&se->pre, tmp);
5823 se->expr = convert (type, resvar);
5824
5825 }
5826
5827 /* Emit code for minval or maxval intrinsic. There are many different cases
5828 we need to handle. For performance reasons we sometimes create two
5829 loops instead of one, where the second one is much simpler.
5830 Examples for minval intrinsic:
5831 1) Result is an array, a call is generated
5832 2) Array mask is used and NaNs need to be supported, rank 1:
5833 limit = Infinity;
5834 nonempty = false;
5835 S = from;
5836 while (S <= to) {
5837 if (mask[S]) {
5838 nonempty = true;
5839 if (a[S] <= limit) {
5840 limit = a[S];
5841 S++;
5842 goto lab;
5843 }
5844 else
5845 S++;
5846 }
5847 }
5848 limit = nonempty ? NaN : huge (limit);
5849 lab:
5850 while (S <= to) { if(mask[S]) limit = min (a[S], limit); S++; }
5851 3) NaNs need to be supported, but it is known at compile time or cheaply
5852 at runtime whether array is nonempty or not, rank 1:
5853 limit = Infinity;
5854 S = from;
5855 while (S <= to) {
5856 if (a[S] <= limit) {
5857 limit = a[S];
5858 S++;
5859 goto lab;
5860 }
5861 else
5862 S++;
5863 }
5864 limit = (from <= to) ? NaN : huge (limit);
5865 lab:
5866 while (S <= to) { limit = min (a[S], limit); S++; }
5867 4) Array mask is used and NaNs need to be supported, rank > 1:
5868 limit = Infinity;
5869 nonempty = false;
5870 fast = false;
5871 S1 = from1;
5872 while (S1 <= to1) {
5873 S2 = from2;
5874 while (S2 <= to2) {
5875 if (mask[S1][S2]) {
5876 if (fast) limit = min (a[S1][S2], limit);
5877 else {
5878 nonempty = true;
5879 if (a[S1][S2] <= limit) {
5880 limit = a[S1][S2];
5881 fast = true;
5882 }
5883 }
5884 }
5885 S2++;
5886 }
5887 S1++;
5888 }
5889 if (!fast)
5890 limit = nonempty ? NaN : huge (limit);
5891 5) NaNs need to be supported, but it is known at compile time or cheaply
5892 at runtime whether array is nonempty or not, rank > 1:
5893 limit = Infinity;
5894 fast = false;
5895 S1 = from1;
5896 while (S1 <= to1) {
5897 S2 = from2;
5898 while (S2 <= to2) {
5899 if (fast) limit = min (a[S1][S2], limit);
5900 else {
5901 if (a[S1][S2] <= limit) {
5902 limit = a[S1][S2];
5903 fast = true;
5904 }
5905 }
5906 S2++;
5907 }
5908 S1++;
5909 }
5910 if (!fast)
5911 limit = (nonempty_array) ? NaN : huge (limit);
5912 6) NaNs aren't supported, but infinities are. Array mask is used:
5913 limit = Infinity;
5914 nonempty = false;
5915 S = from;
5916 while (S <= to) {
5917 if (mask[S]) { nonempty = true; limit = min (a[S], limit); }
5918 S++;
5919 }
5920 limit = nonempty ? limit : huge (limit);
5921 7) Same without array mask:
5922 limit = Infinity;
5923 S = from;
5924 while (S <= to) { limit = min (a[S], limit); S++; }
5925 limit = (from <= to) ? limit : huge (limit);
5926 8) Neither NaNs nor infinities are supported (-ffast-math or BT_INTEGER):
5927 limit = huge (limit);
5928 S = from;
5929 while (S <= to) { limit = min (a[S], limit); S++); }
5930 (or
5931 while (S <= to) { if (mask[S]) limit = min (a[S], limit); S++; }
5932 with array mask instead).
5933 For 3), 5), 7) and 8), if mask is scalar, this all goes into a conditional,
5934 setting limit = huge (limit); in the else branch. */
5935
5936 static void
5937 gfc_conv_intrinsic_minmaxval (gfc_se * se, gfc_expr * expr, enum tree_code op)
5938 {
5939 tree limit;
5940 tree type;
5941 tree tmp;
5942 tree ifbody;
5943 tree nonempty;
5944 tree nonempty_var;
5945 tree lab;
5946 tree fast;
5947 tree huge_cst = NULL, nan_cst = NULL;
5948 stmtblock_t body;
5949 stmtblock_t block, block2;
5950 gfc_loopinfo loop;
5951 gfc_actual_arglist *actual;
5952 gfc_ss *arrayss;
5953 gfc_ss *maskss;
5954 gfc_se arrayse;
5955 gfc_se maskse;
5956 gfc_expr *arrayexpr;
5957 gfc_expr *maskexpr;
5958 int n;
5959 bool optional_mask;
5960
5961 if (se->ss)
5962 {
5963 gfc_conv_intrinsic_funcall (se, expr);
5964 return;
5965 }
5966
5967 actual = expr->value.function.actual;
5968 arrayexpr = actual->expr;
5969
5970 if (arrayexpr->ts.type == BT_CHARACTER)
5971 {
5972 gfc_actual_arglist *dim = actual->next;
5973 if (expr->rank == 0 && dim->expr != 0)
5974 {
5975 gfc_free_expr (dim->expr);
5976 dim->expr = NULL;
5977 }
5978 gfc_conv_intrinsic_funcall (se, expr);
5979 return;
5980 }
5981
5982 type = gfc_typenode_for_spec (&expr->ts);
5983 /* Initialize the result. */
5984 limit = gfc_create_var (type, "limit");
5985 n = gfc_validate_kind (expr->ts.type, expr->ts.kind, false);
5986 switch (expr->ts.type)
5987 {
5988 case BT_REAL:
5989 huge_cst = gfc_conv_mpfr_to_tree (gfc_real_kinds[n].huge,
5990 expr->ts.kind, 0);
5991 if (HONOR_INFINITIES (DECL_MODE (limit)))
5992 {
5993 REAL_VALUE_TYPE real;
5994 real_inf (&real);
5995 tmp = build_real (type, real);
5996 }
5997 else
5998 tmp = huge_cst;
5999 if (HONOR_NANS (DECL_MODE (limit)))
6000 nan_cst = gfc_build_nan (type, "");
6001 break;
6002
6003 case BT_INTEGER:
6004 tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge, expr->ts.kind);
6005 break;
6006
6007 case BT_UNSIGNED:
6008 /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
6009 if (op == GT_EXPR)
6010 tmp = build_int_cst (type, 0);
6011 else
6012 tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
6013 expr->ts.kind);
6014 break;
6015
6016 default:
6017 gcc_unreachable ();
6018 }
6019
6020 /* We start with the most negative possible value for MAXVAL, and the most
6021 positive possible value for MINVAL. The most negative possible value is
6022 -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
6023 possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
6024 with above. */
6025 if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
6026 {
6027 tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
6028 if (huge_cst)
6029 huge_cst = fold_build1_loc (input_location, NEGATE_EXPR,
6030 TREE_TYPE (huge_cst), huge_cst);
6031 }
6032
6033 if (op == GT_EXPR && expr->ts.type == BT_INTEGER)
6034 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
6035 tmp, build_int_cst (type, 1));
6036
6037 gfc_add_modify (&se->pre, limit, tmp);
6038
6039 /* Walk the arguments. */
6040 arrayss = gfc_walk_expr (arrayexpr);
6041 gcc_assert (arrayss != gfc_ss_terminator);
6042
6043 actual = actual->next->next;
6044 gcc_assert (actual);
6045 maskexpr = actual->expr;
6046 optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
6047 && maskexpr->symtree->n.sym->attr.dummy
6048 && maskexpr->symtree->n.sym->attr.optional;
6049 nonempty = NULL;
6050 if (maskexpr && maskexpr->rank != 0)
6051 {
6052 maskss = gfc_walk_expr (maskexpr);
6053 gcc_assert (maskss != gfc_ss_terminator);
6054 }
6055 else
6056 {
6057 mpz_t asize;
6058 if (gfc_array_size (arrayexpr, &asize))
6059 {
6060 nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
6061 mpz_clear (asize);
6062 nonempty = fold_build2_loc (input_location, GT_EXPR,
6063 logical_type_node, nonempty,
6064 gfc_index_zero_node);
6065 }
6066 maskss = NULL;
6067 }
6068
6069 /* Initialize the scalarizer. */
6070 gfc_init_loopinfo (&loop);
6071
6072 /* We add the mask first because the number of iterations is taken
6073 from the last ss, and this breaks if an absent optional argument
6074 is used for mask. */
6075
6076 if (maskss)
6077 gfc_add_ss_to_loop (&loop, maskss);
6078 gfc_add_ss_to_loop (&loop, arrayss);
6079
6080 /* Initialize the loop. */
6081 gfc_conv_ss_startstride (&loop);
6082
6083 /* The code generated can have more than one loop in sequence (see the
6084 comment at the function header). This doesn't work well with the
6085 scalarizer, which changes arrays' offset when the scalarization loops
6086 are generated (see gfc_trans_preloop_setup). Fortunately, {min,max}val
6087 are currently inlined in the scalar case only. As there is no dependency
6088 to care about in that case, there is no temporary, so that we can use the
6089 scalarizer temporary code to handle multiple loops. Thus, we set temp_dim
6090 here, we call gfc_mark_ss_chain_used with flag=3 later, and we use
6091 gfc_trans_scalarized_loop_boundary even later to restore offset.
6092 TODO: this prevents inlining of rank > 0 minmaxval calls, so this
6093 should eventually go away. We could either create two loops properly,
6094 or find another way to save/restore the array offsets between the two
6095 loops (without conflicting with temporary management), or use a single
6096 loop minmaxval implementation. See PR 31067. */
6097 loop.temp_dim = loop.dimen;
6098 gfc_conv_loop_setup (&loop, &expr->where);
6099
6100 if (nonempty == NULL && maskss == NULL
6101 && loop.dimen == 1 && loop.from[0] && loop.to[0])
6102 nonempty = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
6103 loop.from[0], loop.to[0]);
6104 nonempty_var = NULL;
6105 if (nonempty == NULL
6106 && (HONOR_INFINITIES (DECL_MODE (limit))
6107 || HONOR_NANS (DECL_MODE (limit))))
6108 {
6109 nonempty_var = gfc_create_var (logical_type_node, "nonempty");
6110 gfc_add_modify (&se->pre, nonempty_var, logical_false_node);
6111 nonempty = nonempty_var;
6112 }
6113 lab = NULL;
6114 fast = NULL;
6115 if (HONOR_NANS (DECL_MODE (limit)))
6116 {
6117 if (loop.dimen == 1)
6118 {
6119 lab = gfc_build_label_decl (NULL_TREE);
6120 TREE_USED (lab) = 1;
6121 }
6122 else
6123 {
6124 fast = gfc_create_var (logical_type_node, "fast");
6125 gfc_add_modify (&se->pre, fast, logical_false_node);
6126 }
6127 }
6128
6129 gfc_mark_ss_chain_used (arrayss, lab ? 3 : 1);
6130 if (maskss)
6131 gfc_mark_ss_chain_used (maskss, lab ? 3 : 1);
6132 /* Generate the loop body. */
6133 gfc_start_scalarized_body (&loop, &body);
6134
6135 /* If we have a mask, only add this element if the mask is set. */
6136 if (maskss)
6137 {
6138 gfc_init_se (&maskse, NULL);
6139 gfc_copy_loopinfo_to_se (&maskse, &loop);
6140 maskse.ss = maskss;
6141 gfc_conv_expr_val (&maskse, maskexpr);
6142 gfc_add_block_to_block (&body, &maskse.pre);
6143
6144 gfc_start_block (&block);
6145 }
6146 else
6147 gfc_init_block (&block);
6148
6149 /* Compare with the current limit. */
6150 gfc_init_se (&arrayse, NULL);
6151 gfc_copy_loopinfo_to_se (&arrayse, &loop);
6152 arrayse.ss = arrayss;
6153 gfc_conv_expr_val (&arrayse, arrayexpr);
6154 arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6155 gfc_add_block_to_block (&block, &arrayse.pre);
6156
6157 gfc_init_block (&block2);
6158
6159 if (nonempty_var)
6160 gfc_add_modify (&block2, nonempty_var, logical_true_node);
6161
6162 if (HONOR_NANS (DECL_MODE (limit)))
6163 {
6164 tmp = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
6165 logical_type_node, arrayse.expr, limit);
6166 if (lab)
6167 {
6168 stmtblock_t ifblock;
6169 tree inc_loop;
6170 inc_loop = fold_build2_loc (input_location, PLUS_EXPR,
6171 TREE_TYPE (loop.loopvar[0]),
6172 loop.loopvar[0], gfc_index_one_node);
6173 gfc_init_block (&ifblock);
6174 gfc_add_modify (&ifblock, limit, arrayse.expr);
6175 gfc_add_modify (&ifblock, loop.loopvar[0], inc_loop);
6176 gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab));
6177 ifbody = gfc_finish_block (&ifblock);
6178 }
6179 else
6180 {
6181 stmtblock_t ifblock;
6182
6183 gfc_init_block (&ifblock);
6184 gfc_add_modify (&ifblock, limit, arrayse.expr);
6185 gfc_add_modify (&ifblock, fast, logical_true_node);
6186 ifbody = gfc_finish_block (&ifblock);
6187 }
6188 tmp = build3_v (COND_EXPR, tmp, ifbody,
6189 build_empty_stmt (input_location));
6190 gfc_add_expr_to_block (&block2, tmp);
6191 }
6192 else
6193 {
6194 /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6195 signed zeros. */
6196 tmp = fold_build2_loc (input_location,
6197 op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6198 type, arrayse.expr, limit);
6199 gfc_add_modify (&block2, limit, tmp);
6200 }
6201
6202 if (fast)
6203 {
6204 tree elsebody = gfc_finish_block (&block2);
6205
6206 /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6207 signed zeros. */
6208 if (HONOR_NANS (DECL_MODE (limit)))
6209 {
6210 tmp = fold_build2_loc (input_location, op, logical_type_node,
6211 arrayse.expr, limit);
6212 ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6213 ifbody = build3_v (COND_EXPR, tmp, ifbody,
6214 build_empty_stmt (input_location));
6215 }
6216 else
6217 {
6218 tmp = fold_build2_loc (input_location,
6219 op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6220 type, arrayse.expr, limit);
6221 ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6222 }
6223 tmp = build3_v (COND_EXPR, fast, ifbody, elsebody);
6224 gfc_add_expr_to_block (&block, tmp);
6225 }
6226 else
6227 gfc_add_block_to_block (&block, &block2);
6228
6229 gfc_add_block_to_block (&block, &arrayse.post);
6230
6231 tmp = gfc_finish_block (&block);
6232 if (maskss)
6233 {
6234 /* We enclose the above in if (mask) {...}. If the mask is an
6235 optional argument, generate IF (.NOT. PRESENT(MASK)
6236 .OR. MASK(I)). */
6237 tree ifmask;
6238 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6239 tmp = build3_v (COND_EXPR, ifmask, tmp,
6240 build_empty_stmt (input_location));
6241 }
6242 gfc_add_expr_to_block (&body, tmp);
6243
6244 if (lab)
6245 {
6246 gfc_trans_scalarized_loop_boundary (&loop, &body);
6247
6248 tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6249 nan_cst, huge_cst);
6250 gfc_add_modify (&loop.code[0], limit, tmp);
6251 gfc_add_expr_to_block (&loop.code[0], build1_v (LABEL_EXPR, lab));
6252
6253 /* If we have a mask, only add this element if the mask is set. */
6254 if (maskss)
6255 {
6256 gfc_init_se (&maskse, NULL);
6257 gfc_copy_loopinfo_to_se (&maskse, &loop);
6258 maskse.ss = maskss;
6259 gfc_conv_expr_val (&maskse, maskexpr);
6260 gfc_add_block_to_block (&body, &maskse.pre);
6261
6262 gfc_start_block (&block);
6263 }
6264 else
6265 gfc_init_block (&block);
6266
6267 /* Compare with the current limit. */
6268 gfc_init_se (&arrayse, NULL);
6269 gfc_copy_loopinfo_to_se (&arrayse, &loop);
6270 arrayse.ss = arrayss;
6271 gfc_conv_expr_val (&arrayse, arrayexpr);
6272 arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6273 gfc_add_block_to_block (&block, &arrayse.pre);
6274
6275 /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6276 signed zeros. */
6277 if (HONOR_NANS (DECL_MODE (limit)))
6278 {
6279 tmp = fold_build2_loc (input_location, op, logical_type_node,
6280 arrayse.expr, limit);
6281 ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6282 tmp = build3_v (COND_EXPR, tmp, ifbody,
6283 build_empty_stmt (input_location));
6284 gfc_add_expr_to_block (&block, tmp);
6285 }
6286 else
6287 {
6288 tmp = fold_build2_loc (input_location,
6289 op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6290 type, arrayse.expr, limit);
6291 gfc_add_modify (&block, limit, tmp);
6292 }
6293
6294 gfc_add_block_to_block (&block, &arrayse.post);
6295
6296 tmp = gfc_finish_block (&block);
6297 if (maskss)
6298 /* We enclose the above in if (mask) {...}. */
6299 {
6300 tree ifmask;
6301 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6302 tmp = build3_v (COND_EXPR, ifmask, tmp,
6303 build_empty_stmt (input_location));
6304 }
6305
6306 gfc_add_expr_to_block (&body, tmp);
6307 /* Avoid initializing loopvar[0] again, it should be left where
6308 it finished by the first loop. */
6309 loop.from[0] = loop.loopvar[0];
6310 }
6311 gfc_trans_scalarizing_loops (&loop, &body);
6312
6313 if (fast)
6314 {
6315 tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6316 nan_cst, huge_cst);
6317 ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6318 tmp = build3_v (COND_EXPR, fast, build_empty_stmt (input_location),
6319 ifbody);
6320 gfc_add_expr_to_block (&loop.pre, tmp);
6321 }
6322 else if (HONOR_INFINITIES (DECL_MODE (limit)) && !lab)
6323 {
6324 tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty, limit,
6325 huge_cst);
6326 gfc_add_modify (&loop.pre, limit, tmp);
6327 }
6328
6329 /* For a scalar mask, enclose the loop in an if statement. */
6330 if (maskexpr && maskss == NULL)
6331 {
6332 tree else_stmt;
6333 tree ifmask;
6334
6335 gfc_init_se (&maskse, NULL);
6336 gfc_conv_expr_val (&maskse, maskexpr);
6337 gfc_init_block (&block);
6338 gfc_add_block_to_block (&block, &loop.pre);
6339 gfc_add_block_to_block (&block, &loop.post);
6340 tmp = gfc_finish_block (&block);
6341
6342 if (HONOR_INFINITIES (DECL_MODE (limit)))
6343 else_stmt = build2_v (MODIFY_EXPR, limit, huge_cst);
6344 else
6345 else_stmt = build_empty_stmt (input_location);
6346
6347 ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6348 tmp = build3_v (COND_EXPR, ifmask, tmp, else_stmt);
6349 gfc_add_expr_to_block (&block, tmp);
6350 gfc_add_block_to_block (&se->pre, &block);
6351 }
6352 else
6353 {
6354 gfc_add_block_to_block (&se->pre, &loop.pre);
6355 gfc_add_block_to_block (&se->pre, &loop.post);
6356 }
6357
6358 gfc_cleanup_loop (&loop);
6359
6360 se->expr = limit;
6361 }
6362
6363 /* BTEST (i, pos) = (i & (1 << pos)) != 0. */
6364 static void
6365 gfc_conv_intrinsic_btest (gfc_se * se, gfc_expr * expr)
6366 {
6367 tree args[2];
6368 tree type;
6369 tree tmp;
6370
6371 gfc_conv_intrinsic_function_args (se, expr, args, 2);
6372 type = TREE_TYPE (args[0]);
6373
6374 /* Optionally generate code for runtime argument check. */
6375 if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6376 {
6377 tree below = fold_build2_loc (input_location, LT_EXPR,
6378 logical_type_node, args[1],
6379 build_int_cst (TREE_TYPE (args[1]), 0));
6380 tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6381 tree above = fold_build2_loc (input_location, GE_EXPR,
6382 logical_type_node, args[1], nbits);
6383 tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6384 logical_type_node, below, above);
6385 gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6386 "POS argument (%ld) out of range 0:%ld "
6387 "in intrinsic BTEST",
6388 fold_convert (long_integer_type_node, args[1]),
6389 fold_convert (long_integer_type_node, nbits));
6390 }
6391
6392 tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6393 build_int_cst (type, 1), args[1]);
6394 tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], tmp);
6395 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
6396 build_int_cst (type, 0));
6397 type = gfc_typenode_for_spec (&expr->ts);
6398 se->expr = convert (type, tmp);
6399 }
6400
6401
6402 /* Generate code for BGE, BGT, BLE and BLT intrinsics. */
6403 static void
6404 gfc_conv_intrinsic_bitcomp (gfc_se * se, gfc_expr * expr, enum tree_code op)
6405 {
6406 tree args[2];
6407
6408 gfc_conv_intrinsic_function_args (se, expr, args, 2);
6409
6410 /* Convert both arguments to the unsigned type of the same size. */
6411 args[0] = fold_convert (unsigned_type_for (TREE_TYPE (args[0])), args[0]);
6412 args[1] = fold_convert (unsigned_type_for (TREE_TYPE (args[1])), args[1]);
6413
6414 /* If they have unequal type size, convert to the larger one. */
6415 if (TYPE_PRECISION (TREE_TYPE (args[0]))
6416 > TYPE_PRECISION (TREE_TYPE (args[1])))
6417 args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
6418 else if (TYPE_PRECISION (TREE_TYPE (args[1]))
6419 > TYPE_PRECISION (TREE_TYPE (args[0])))
6420 args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
6421
6422 /* Now, we compare them. */
6423 se->expr = fold_build2_loc (input_location, op, logical_type_node,
6424 args[0], args[1]);
6425 }
6426
6427
6428 /* Generate code to perform the specified operation. */
6429 static void
6430 gfc_conv_intrinsic_bitop (gfc_se * se, gfc_expr * expr, enum tree_code op)
6431 {
6432 tree args[2];
6433
6434 gfc_conv_intrinsic_function_args (se, expr, args, 2);
6435 se->expr = fold_build2_loc (input_location, op, TREE_TYPE (args[0]),
6436 args[0], args[1]);
6437 }
6438
6439 /* Bitwise not. */
6440 static void
6441 gfc_conv_intrinsic_not (gfc_se * se, gfc_expr * expr)
6442 {
6443 tree arg;
6444
6445 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
6446 se->expr = fold_build1_loc (input_location, BIT_NOT_EXPR,
6447 TREE_TYPE (arg), arg);
6448 }
6449
6450
6451 /* Generate code for OUT_OF_RANGE. */
6452 static void
6453 gfc_conv_intrinsic_out_of_range (gfc_se * se, gfc_expr * expr)
6454 {
6455 tree *args;
6456 tree type;
6457 tree tmp = NULL_TREE, tmp1, tmp2;
6458 unsigned int num_args;
6459 int k;
6460 gfc_se rnd_se;
6461 gfc_actual_arglist *arg = expr->value.function.actual;
6462 gfc_expr *x = arg->expr;
6463 gfc_expr *mold = arg->next->expr;
6464
6465 num_args = gfc_intrinsic_argument_list_length (expr);
6466 args = XALLOCAVEC (tree, num_args);
6467
6468 gfc_conv_intrinsic_function_args (se, expr, args, num_args);
6469
6470 gfc_init_se (&rnd_se, NULL);
6471
6472 if (num_args == 3)
6473 {
6474 /* The ROUND argument is optional and shall appear only if X is
6475 of type real and MOLD is of type integer (see edit F23/004). */
6476 gfc_expr *round = arg->next->next->expr;
6477 gfc_conv_expr (&rnd_se, round);
6478
6479 if (round->expr_type == EXPR_VARIABLE
6480 && round->symtree->n.sym->attr.dummy
6481 && round->symtree->n.sym->attr.optional)
6482 {
6483 tree present = gfc_conv_expr_present (round->symtree->n.sym);
6484 rnd_se.expr = build3_loc (input_location, COND_EXPR,
6485 logical_type_node, present,
6486 rnd_se.expr, logical_false_node);
6487 gfc_add_block_to_block (&se->pre, &rnd_se.pre);
6488 }
6489 }
6490 else
6491 {
6492 /* If ROUND is absent, it is equivalent to having the value false. */
6493 rnd_se.expr = logical_false_node;
6494 }
6495
6496 type = TREE_TYPE (args[0]);
6497 k = gfc_validate_kind (mold->ts.type, mold->ts.kind, false);
6498
6499 switch (x->ts.type)
6500 {
6501 case BT_REAL:
6502 /* X may be IEEE infinity or NaN, but the representation of MOLD may not
6503 support infinity or NaN. */
6504 tree finite;
6505 finite = build_call_expr_loc (input_location,
6506 builtin_decl_explicit (BUILT_IN_ISFINITE),
6507 1, args[0]);
6508 finite = convert (logical_type_node, finite);
6509
6510 if (mold->ts.type == BT_REAL)
6511 {
6512 tmp1 = build1 (ABS_EXPR, type, args[0]);
6513 tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6514 mold->ts.kind, 0);
6515 tmp = build2 (GT_EXPR, logical_type_node, tmp1,
6516 convert (type, tmp2));
6517
6518 /* Check if MOLD representation supports infinity or NaN. */
6519 bool infnan = (HONOR_INFINITIES (TREE_TYPE (args[1]))
6520 || HONOR_NANS (TREE_TYPE (args[1])));
6521 tmp = build3 (COND_EXPR, logical_type_node, finite, tmp,
6522 infnan ? logical_false_node : logical_true_node);
6523 }
6524 else
6525 {
6526 tree rounded;
6527 tree decl;
6528
6529 decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, x->ts.kind);
6530 gcc_assert (decl != NULL_TREE);
6531
6532 /* Round or truncate argument X, depending on the optional argument
6533 ROUND (default: .false.). */
6534 tmp1 = build_round_expr (args[0], type);
6535 tmp2 = build_call_expr_loc (input_location, decl, 1, args[0]);
6536 rounded = build3 (COND_EXPR, type, rnd_se.expr, tmp1, tmp2);
6537
6538 if (mold->ts.type == BT_INTEGER)
6539 {
6540 tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6541 x->ts.kind);
6542 tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6543 x->ts.kind);
6544 }
6545 else if (mold->ts.type == BT_UNSIGNED)
6546 {
6547 tmp1 = build_real_from_int_cst (type, integer_zero_node);
6548 tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6549 x->ts.kind);
6550 }
6551 else
6552 gcc_unreachable ();
6553
6554 tmp1 = build2 (LT_EXPR, logical_type_node, rounded,
6555 convert (type, tmp1));
6556 tmp2 = build2 (GT_EXPR, logical_type_node, rounded,
6557 convert (type, tmp2));
6558 tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6559 tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node,
6560 build1 (TRUTH_NOT_EXPR, logical_type_node, finite),
6561 tmp);
6562 }
6563 break;
6564
6565 case BT_INTEGER:
6566 if (mold->ts.type == BT_INTEGER)
6567 {
6568 tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6569 x->ts.kind);
6570 tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6571 x->ts.kind);
6572 tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6573 convert (type, tmp1));
6574 tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6575 convert (type, tmp2));
6576 tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6577 }
6578 else if (mold->ts.type == BT_UNSIGNED)
6579 {
6580 int i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
6581 tmp = build_int_cst (type, 0);
6582 tmp = build2 (LT_EXPR, logical_type_node, args[0], tmp);
6583 if (mpz_cmp (gfc_integer_kinds[i].huge,
6584 gfc_unsigned_kinds[k].huge) > 0)
6585 {
6586 tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6587 x->ts.kind);
6588 tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6589 convert (type, tmp2));
6590 tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp, tmp2);
6591 }
6592 }
6593 else if (mold->ts.type == BT_REAL)
6594 {
6595 tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6596 mold->ts.kind, 0);
6597 tmp1 = build1 (NEGATE_EXPR, TREE_TYPE (tmp2), tmp2);
6598 tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6599 convert (type, tmp1));
6600 tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6601 convert (type, tmp2));
6602 tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6603 }
6604 else
6605 gcc_unreachable ();
6606 break;
6607
6608 case BT_UNSIGNED:
6609 if (mold->ts.type == BT_UNSIGNED)
6610 {
6611 tmp = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6612 x->ts.kind);
6613 tmp = build2 (GT_EXPR, logical_type_node, args[0],
6614 convert (type, tmp));
6615 }
6616 else if (mold->ts.type == BT_INTEGER)
6617 {
6618 tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6619 x->ts.kind);
6620 tmp = build2 (GT_EXPR, logical_type_node, args[0],
6621 convert (type, tmp));
6622 }
6623 else if (mold->ts.type == BT_REAL)
6624 {
6625 tmp = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6626 mold->ts.kind, 0);
6627 tmp = build2 (GT_EXPR, logical_type_node, args[0],
6628 convert (type, tmp));
6629 }
6630 else
6631 gcc_unreachable ();
6632 break;
6633
6634 default:
6635 gcc_unreachable ();
6636 }
6637
6638 se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
6639 }
6640
6641
6642 /* Set or clear a single bit. */
6643 static void
6644 gfc_conv_intrinsic_singlebitop (gfc_se * se, gfc_expr * expr, int set)
6645 {
6646 tree args[2];
6647 tree type;
6648 tree tmp;
6649 enum tree_code op;
6650
6651 gfc_conv_intrinsic_function_args (se, expr, args, 2);
6652 type = TREE_TYPE (args[0]);
6653
6654 /* Optionally generate code for runtime argument check. */
6655 if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6656 {
6657 tree below = fold_build2_loc (input_location, LT_EXPR,
6658 logical_type_node, args[1],
6659 build_int_cst (TREE_TYPE (args[1]), 0));
6660 tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6661 tree above = fold_build2_loc (input_location, GE_EXPR,
6662 logical_type_node, args[1], nbits);
6663 tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6664 logical_type_node, below, above);
6665 size_t len_name = strlen (expr->value.function.isym->name);
6666 char *name = XALLOCAVEC (char, len_name + 1);
6667 for (size_t i = 0; i < len_name; i++)
6668 name[i] = TOUPPER (expr->value.function.isym->name[i]);
6669 name[len_name] = '\0';
6670 tree iname = gfc_build_addr_expr (pchar_type_node,
6671 gfc_build_cstring_const (name));
6672 gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6673 "POS argument (%ld) out of range 0:%ld "
6674 "in intrinsic %s",
6675 fold_convert (long_integer_type_node, args[1]),
6676 fold_convert (long_integer_type_node, nbits),
6677 iname);
6678 }
6679
6680 tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6681 build_int_cst (type, 1), args[1]);
6682 if (set)
6683 op = BIT_IOR_EXPR;
6684 else
6685 {
6686 op = BIT_AND_EXPR;
6687 tmp = fold_build1_loc (input_location, BIT_NOT_EXPR, type, tmp);
6688 }
6689 se->expr = fold_build2_loc (input_location, op, type, args[0], tmp);
6690 }
6691
6692 /* Extract a sequence of bits.
6693 IBITS(I, POS, LEN) = (I >> POS) & ~((~0) << LEN). */
6694 static void
6695 gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr)
6696 {
6697 tree args[3];
6698 tree type;
6699 tree tmp;
6700 tree mask;
6701 tree num_bits, cond;
6702
6703 gfc_conv_intrinsic_function_args (se, expr, args, 3);
6704 type = TREE_TYPE (args[0]);
6705
6706 /* Optionally generate code for runtime argument check. */
6707 if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6708 {
6709 tree tmp1 = fold_convert (long_integer_type_node, args[1]);
6710 tree tmp2 = fold_convert (long_integer_type_node, args[2]);
6711 tree nbits = build_int_cst (long_integer_type_node,
6712 TYPE_PRECISION (type));
6713 tree below = fold_build2_loc (input_location, LT_EXPR,
6714 logical_type_node, args[1],
6715 build_int_cst (TREE_TYPE (args[1]), 0));
6716 tree above = fold_build2_loc (input_location, GT_EXPR,
6717 logical_type_node, tmp1, nbits);
6718 tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6719 logical_type_node, below, above);
6720 gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6721 "POS argument (%ld) out of range 0:%ld "
6722 "in intrinsic IBITS", tmp1, nbits);
6723 below = fold_build2_loc (input_location, LT_EXPR,
6724 logical_type_node, args[2],
6725 build_int_cst (TREE_TYPE (args[2]), 0));
6726 above = fold_build2_loc (input_location, GT_EXPR,
6727 logical_type_node, tmp2, nbits);
6728 scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6729 logical_type_node, below, above);
6730 gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6731 "LEN argument (%ld) out of range 0:%ld "
6732 "in intrinsic IBITS", tmp2, nbits);
6733 above = fold_build2_loc (input_location, PLUS_EXPR,
6734 long_integer_type_node, tmp1, tmp2);
6735 scond = fold_build2_loc (input_location, GT_EXPR,
6736 logical_type_node, above, nbits);
6737 gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6738 "POS(%ld)+LEN(%ld)>BIT_SIZE(%ld) "
6739 "in intrinsic IBITS", tmp1, tmp2, nbits);
6740 }
6741
6742 /* The Fortran standard allows (shift width) LEN <= BIT_SIZE(I), whereas
6743 gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6744 special case. See also gfc_conv_intrinsic_ishft (). */
6745 num_bits = build_int_cst (TREE_TYPE (args[2]), TYPE_PRECISION (type));
6746
6747 mask = build_int_cst (type, -1);
6748 mask = fold_build2_loc (input_location, LSHIFT_EXPR, type, mask, args[2]);
6749 cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[2],
6750 num_bits);
6751 mask = fold_build3_loc (input_location, COND_EXPR, type, cond,
6752 build_int_cst (type, 0), mask);
6753 mask = fold_build1_loc (input_location, BIT_NOT_EXPR, type, mask);
6754
6755 tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, args[0], args[1]);
6756
6757 se->expr = fold_build2_loc (input_location, BIT_AND_EXPR, type, tmp, mask);
6758 }
6759
6760 static void
6761 gfc_conv_intrinsic_shift (gfc_se * se, gfc_expr * expr, bool right_shift,
6762 bool arithmetic)
6763 {
6764 tree args[2], type, num_bits, cond;
6765 tree bigshift;
6766 bool do_convert = false;
6767
6768 gfc_conv_intrinsic_function_args (se, expr, args, 2);
6769
6770 args[0] = gfc_evaluate_now (args[0], &se->pre);
6771 args[1] = gfc_evaluate_now (args[1], &se->pre);
6772 type = TREE_TYPE (args[0]);
6773
6774 if (!arithmetic)
6775 {
6776 args[0] = fold_convert (unsigned_type_for (type), args[0]);
6777 do_convert = true;
6778 }
6779 else
6780 gcc_assert (right_shift);
6781
6782 if (flag_unsigned && arithmetic && expr->ts.type == BT_UNSIGNED)
6783 {
6784 do_convert = true;
6785 args[0] = fold_convert (signed_type_for (type), args[0]);
6786 }
6787
6788 se->expr = fold_build2_loc (input_location,
6789 right_shift ? RSHIFT_EXPR : LSHIFT_EXPR,
6790 TREE_TYPE (args[0]), args[0], args[1]);
6791
6792 if (do_convert)
6793 se->expr = fold_convert (type, se->expr);
6794
6795 if (!arithmetic)
6796 bigshift = build_int_cst (type, 0);
6797 else
6798 {
6799 tree nonneg = fold_build2_loc (input_location, GE_EXPR,
6800 logical_type_node, args[0],
6801 build_int_cst (TREE_TYPE (args[0]), 0));
6802 bigshift = fold_build3_loc (input_location, COND_EXPR, type, nonneg,
6803 build_int_cst (type, 0),
6804 build_int_cst (type, -1));
6805 }
6806
6807 /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
6808 gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6809 special case. */
6810 num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6811
6812 /* Optionally generate code for runtime argument check. */
6813 if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6814 {
6815 tree below = fold_build2_loc (input_location, LT_EXPR,
6816 logical_type_node, args[1],
6817 build_int_cst (TREE_TYPE (args[1]), 0));
6818 tree above = fold_build2_loc (input_location, GT_EXPR,
6819 logical_type_node, args[1], num_bits);
6820 tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6821 logical_type_node, below, above);
6822 size_t len_name = strlen (expr->value.function.isym->name);
6823 char *name = XALLOCAVEC (char, len_name + 1);
6824 for (size_t i = 0; i < len_name; i++)
6825 name[i] = TOUPPER (expr->value.function.isym->name[i]);
6826 name[len_name] = '\0';
6827 tree iname = gfc_build_addr_expr (pchar_type_node,
6828 gfc_build_cstring_const (name));
6829 gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6830 "SHIFT argument (%ld) out of range 0:%ld "
6831 "in intrinsic %s",
6832 fold_convert (long_integer_type_node, args[1]),
6833 fold_convert (long_integer_type_node, num_bits),
6834 iname);
6835 }
6836
6837 cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
6838 args[1], num_bits);
6839
6840 se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
6841 bigshift, se->expr);
6842 }
6843
6844 /* ISHFT (I, SHIFT) = (abs (shift) >= BIT_SIZE (i))
6845 ? 0
6846 : ((shift >= 0) ? i << shift : i >> -shift)
6847 where all shifts are logical shifts. */
6848 static void
6849 gfc_conv_intrinsic_ishft (gfc_se * se, gfc_expr * expr)
6850 {
6851 tree args[2];
6852 tree type;
6853 tree utype;
6854 tree tmp;
6855 tree width;
6856 tree num_bits;
6857 tree cond;
6858 tree lshift;
6859 tree rshift;
6860
6861 gfc_conv_intrinsic_function_args (se, expr, args, 2);
6862
6863 args[0] = gfc_evaluate_now (args[0], &se->pre);
6864 args[1] = gfc_evaluate_now (args[1], &se->pre);
6865
6866 type = TREE_TYPE (args[0]);
6867 utype = unsigned_type_for (type);
6868
6869 width = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (args[1]),
6870 args[1]);
6871
6872 /* Left shift if positive. */
6873 lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type, args[0], width);
6874
6875 /* Right shift if negative.
6876 We convert to an unsigned type because we want a logical shift.
6877 The standard doesn't define the case of shifting negative
6878 numbers, and we try to be compatible with other compilers, most
6879 notably g77, here. */
6880 rshift = fold_convert (type, fold_build2_loc (input_location, RSHIFT_EXPR,
6881 utype, convert (utype, args[0]), width));
6882
6883 tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[1],
6884 build_int_cst (TREE_TYPE (args[1]), 0));
6885 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp, lshift, rshift);
6886
6887 /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
6888 gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6889 special case. */
6890 num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6891
6892 /* Optionally generate code for runtime argument check. */
6893 if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6894 {
6895 tree outside = fold_build2_loc (input_location, GT_EXPR,
6896 logical_type_node, width, num_bits);
6897 gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
6898 "SHIFT argument (%ld) out of range -%ld:%ld "
6899 "in intrinsic ISHFT",
6900 fold_convert (long_integer_type_node, args[1]),
6901 fold_convert (long_integer_type_node, num_bits),
6902 fold_convert (long_integer_type_node, num_bits));
6903 }
6904
6905 cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, width,
6906 num_bits);
6907 se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
6908 build_int_cst (type, 0), tmp);
6909 }
6910
6911
6912 /* Circular shift. AKA rotate or barrel shift. */
6913
6914 static void
6915 gfc_conv_intrinsic_ishftc (gfc_se * se, gfc_expr * expr)
6916 {
6917 tree *args;
6918 tree type;
6919 tree tmp;
6920 tree lrot;
6921 tree rrot;
6922 tree zero;
6923 tree nbits;
6924 unsigned int num_args;
6925
6926 num_args = gfc_intrinsic_argument_list_length (expr);
6927 args = XALLOCAVEC (tree, num_args);
6928
6929 gfc_conv_intrinsic_function_args (se, expr, args, num_args);
6930
6931 type = TREE_TYPE (args[0]);
6932 nbits = build_int_cst (long_integer_type_node, TYPE_PRECISION (type));
6933
6934 if (num_args == 3)
6935 {
6936 gfc_expr *size = expr->value.function.actual->next->next->expr;
6937
6938 /* Use a library function for the 3 parameter version. */
6939 tree int4type = gfc_get_int_type (4);
6940
6941 /* Treat optional SIZE argument when it is passed as an optional
6942 dummy. If SIZE is absent, the default value is BIT_SIZE(I). */
6943 if (size->expr_type == EXPR_VARIABLE
6944 && size->symtree->n.sym->attr.dummy
6945 && size->symtree->n.sym->attr.optional)
6946 {
6947 tree type_of_size = TREE_TYPE (args[2]);
6948 args[2] = build3_loc (input_location, COND_EXPR, type_of_size,
6949 gfc_conv_expr_present (size->symtree->n.sym),
6950 args[2], fold_convert (type_of_size, nbits));
6951 }
6952
6953 /* We convert the first argument to at least 4 bytes, and
6954 convert back afterwards. This removes the need for library
6955 functions for all argument sizes, and function will be
6956 aligned to at least 32 bits, so there's no loss. */
6957 if (expr->ts.kind < 4)
6958 args[0] = convert (int4type, args[0]);
6959
6960 /* Convert the SHIFT and SIZE args to INTEGER*4 otherwise we would
6961 need loads of library functions. They cannot have values >
6962 BIT_SIZE (I) so the conversion is safe. */
6963 args[1] = convert (int4type, args[1]);
6964 args[2] = convert (int4type, args[2]);
6965
6966 /* Optionally generate code for runtime argument check. */
6967 if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6968 {
6969 tree size = fold_convert (long_integer_type_node, args[2]);
6970 tree below = fold_build2_loc (input_location, LE_EXPR,
6971 logical_type_node, size,
6972 build_int_cst (TREE_TYPE (args[1]), 0));
6973 tree above = fold_build2_loc (input_location, GT_EXPR,
6974 logical_type_node, size, nbits);
6975 tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6976 logical_type_node, below, above);
6977 gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6978 "SIZE argument (%ld) out of range 1:%ld "
6979 "in intrinsic ISHFTC", size, nbits);
6980 tree width = fold_convert (long_integer_type_node, args[1]);
6981 width = fold_build1_loc (input_location, ABS_EXPR,
6982 long_integer_type_node, width);
6983 scond = fold_build2_loc (input_location, GT_EXPR,
6984 logical_type_node, width, size);
6985 gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6986 "SHIFT argument (%ld) out of range -%ld:%ld "
6987 "in intrinsic ISHFTC",
6988 fold_convert (long_integer_type_node, args[1]),
6989 size, size);
6990 }
6991
6992 switch (expr->ts.kind)
6993 {
6994 case 1:
6995 case 2:
6996 case 4:
6997 tmp = gfor_fndecl_math_ishftc4;
6998 break;
6999 case 8:
7000 tmp = gfor_fndecl_math_ishftc8;
7001 break;
7002 case 16:
7003 tmp = gfor_fndecl_math_ishftc16;
7004 break;
7005 default:
7006 gcc_unreachable ();
7007 }
7008 se->expr = build_call_expr_loc (input_location,
7009 tmp, 3, args[0], args[1], args[2]);
7010 /* Convert the result back to the original type, if we extended
7011 the first argument's width above. */
7012 if (expr->ts.kind < 4)
7013 se->expr = convert (type, se->expr);
7014
7015 return;
7016 }
7017
7018 /* Evaluate arguments only once. */
7019 args[0] = gfc_evaluate_now (args[0], &se->pre);
7020 args[1] = gfc_evaluate_now (args[1], &se->pre);
7021
7022 /* Optionally generate code for runtime argument check. */
7023 if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7024 {
7025 tree width = fold_convert (long_integer_type_node, args[1]);
7026 width = fold_build1_loc (input_location, ABS_EXPR,
7027 long_integer_type_node, width);
7028 tree outside = fold_build2_loc (input_location, GT_EXPR,
7029 logical_type_node, width, nbits);
7030 gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
7031 "SHIFT argument (%ld) out of range -%ld:%ld "
7032 "in intrinsic ISHFTC",
7033 fold_convert (long_integer_type_node, args[1]),
7034 nbits, nbits);
7035 }
7036
7037 /* Rotate left if positive. */
7038 lrot = fold_build2_loc (input_location, LROTATE_EXPR, type, args[0], args[1]);
7039
7040 /* Rotate right if negative. */
7041 tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (args[1]),
7042 args[1]);
7043 rrot = fold_build2_loc (input_location,RROTATE_EXPR, type, args[0], tmp);
7044
7045 zero = build_int_cst (TREE_TYPE (args[1]), 0);
7046 tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, args[1],
7047 zero);
7048 rrot = fold_build3_loc (input_location, COND_EXPR, type, tmp, lrot, rrot);
7049
7050 /* Do nothing if shift == 0. */
7051 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, args[1],
7052 zero);
7053 se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, args[0],
7054 rrot);
7055 }
7056
7057
7058 /* LEADZ (i) = (i == 0) ? BIT_SIZE (i)
7059 : __builtin_clz(i) - (BIT_SIZE('int') - BIT_SIZE(i))
7060
7061 The conditional expression is necessary because the result of LEADZ(0)
7062 is defined, but the result of __builtin_clz(0) is undefined for most
7063 targets.
7064
7065 For INTEGER kinds smaller than the C 'int' type, we have to subtract the
7066 difference in bit size between the argument of LEADZ and the C int. */
7067
7068 static void
7069 gfc_conv_intrinsic_leadz (gfc_se * se, gfc_expr * expr)
7070 {
7071 tree arg;
7072 tree arg_type;
7073 tree cond;
7074 tree result_type;
7075 tree leadz;
7076 tree bit_size;
7077 tree tmp;
7078 tree func;
7079 int s, argsize;
7080
7081 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7082 argsize = TYPE_PRECISION (TREE_TYPE (arg));
7083
7084 /* Which variant of __builtin_clz* should we call? */
7085 if (argsize <= INT_TYPE_SIZE)
7086 {
7087 arg_type = unsigned_type_node;
7088 func = builtin_decl_explicit (BUILT_IN_CLZ);
7089 }
7090 else if (argsize <= LONG_TYPE_SIZE)
7091 {
7092 arg_type = long_unsigned_type_node;
7093 func = builtin_decl_explicit (BUILT_IN_CLZL);
7094 }
7095 else if (argsize <= LONG_LONG_TYPE_SIZE)
7096 {
7097 arg_type = long_long_unsigned_type_node;
7098 func = builtin_decl_explicit (BUILT_IN_CLZLL);
7099 }
7100 else
7101 {
7102 gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7103 arg_type = gfc_build_uint_type (argsize);
7104 func = NULL_TREE;
7105 }
7106
7107 /* Convert the actual argument twice: first, to the unsigned type of the
7108 same size; then, to the proper argument type for the built-in
7109 function. But the return type is of the default INTEGER kind. */
7110 arg = fold_convert (gfc_build_uint_type (argsize), arg);
7111 arg = fold_convert (arg_type, arg);
7112 arg = gfc_evaluate_now (arg, &se->pre);
7113 result_type = gfc_get_int_type (gfc_default_integer_kind);
7114
7115 /* Compute LEADZ for the case i .ne. 0. */
7116 if (func)
7117 {
7118 s = TYPE_PRECISION (arg_type) - argsize;
7119 tmp = fold_convert (result_type,
7120 build_call_expr_loc (input_location, func,
7121 1, arg));
7122 leadz = fold_build2_loc (input_location, MINUS_EXPR, result_type,
7123 tmp, build_int_cst (result_type, s));
7124 }
7125 else
7126 {
7127 /* We end up here if the argument type is larger than 'long long'.
7128 We generate this code:
7129
7130 if (x & (ULL_MAX << ULL_SIZE) != 0)
7131 return clzll ((unsigned long long) (x >> ULLSIZE));
7132 else
7133 return ULL_SIZE + clzll ((unsigned long long) x);
7134 where ULL_MAX is the largest value that a ULL_MAX can hold
7135 (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7136 is the bit-size of the long long type (64 in this example). */
7137 tree ullsize, ullmax, tmp1, tmp2, btmp;
7138
7139 ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7140 ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7141 long_long_unsigned_type_node,
7142 build_int_cst (long_long_unsigned_type_node,
7143 0));
7144
7145 cond = fold_build2_loc (input_location, LSHIFT_EXPR, arg_type,
7146 fold_convert (arg_type, ullmax), ullsize);
7147 cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type,
7148 arg, cond);
7149 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
7150 cond, build_int_cst (arg_type, 0));
7151
7152 tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7153 arg, ullsize);
7154 tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7155 btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7156 tmp1 = fold_convert (result_type,
7157 build_call_expr_loc (input_location, btmp, 1, tmp1));
7158
7159 tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7160 btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7161 tmp2 = fold_convert (result_type,
7162 build_call_expr_loc (input_location, btmp, 1, tmp2));
7163 tmp2 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7164 tmp2, ullsize);
7165
7166 leadz = fold_build3_loc (input_location, COND_EXPR, result_type,
7167 cond, tmp1, tmp2);
7168 }
7169
7170 /* Build BIT_SIZE. */
7171 bit_size = build_int_cst (result_type, argsize);
7172
7173 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7174 arg, build_int_cst (arg_type, 0));
7175 se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7176 bit_size, leadz);
7177 }
7178
7179
7180 /* TRAILZ(i) = (i == 0) ? BIT_SIZE (i) : __builtin_ctz(i)
7181
7182 The conditional expression is necessary because the result of TRAILZ(0)
7183 is defined, but the result of __builtin_ctz(0) is undefined for most
7184 targets. */
7185
7186 static void
7187 gfc_conv_intrinsic_trailz (gfc_se * se, gfc_expr *expr)
7188 {
7189 tree arg;
7190 tree arg_type;
7191 tree cond;
7192 tree result_type;
7193 tree trailz;
7194 tree bit_size;
7195 tree func;
7196 int argsize;
7197
7198 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7199 argsize = TYPE_PRECISION (TREE_TYPE (arg));
7200
7201 /* Which variant of __builtin_ctz* should we call? */
7202 if (argsize <= INT_TYPE_SIZE)
7203 {
7204 arg_type = unsigned_type_node;
7205 func = builtin_decl_explicit (BUILT_IN_CTZ);
7206 }
7207 else if (argsize <= LONG_TYPE_SIZE)
7208 {
7209 arg_type = long_unsigned_type_node;
7210 func = builtin_decl_explicit (BUILT_IN_CTZL);
7211 }
7212 else if (argsize <= LONG_LONG_TYPE_SIZE)
7213 {
7214 arg_type = long_long_unsigned_type_node;
7215 func = builtin_decl_explicit (BUILT_IN_CTZLL);
7216 }
7217 else
7218 {
7219 gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7220 arg_type = gfc_build_uint_type (argsize);
7221 func = NULL_TREE;
7222 }
7223
7224 /* Convert the actual argument twice: first, to the unsigned type of the
7225 same size; then, to the proper argument type for the built-in
7226 function. But the return type is of the default INTEGER kind. */
7227 arg = fold_convert (gfc_build_uint_type (argsize), arg);
7228 arg = fold_convert (arg_type, arg);
7229 arg = gfc_evaluate_now (arg, &se->pre);
7230 result_type = gfc_get_int_type (gfc_default_integer_kind);
7231
7232 /* Compute TRAILZ for the case i .ne. 0. */
7233 if (func)
7234 trailz = fold_convert (result_type, build_call_expr_loc (input_location,
7235 func, 1, arg));
7236 else
7237 {
7238 /* We end up here if the argument type is larger than 'long long'.
7239 We generate this code:
7240
7241 if ((x & ULL_MAX) == 0)
7242 return ULL_SIZE + ctzll ((unsigned long long) (x >> ULLSIZE));
7243 else
7244 return ctzll ((unsigned long long) x);
7245
7246 where ULL_MAX is the largest value that a ULL_MAX can hold
7247 (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7248 is the bit-size of the long long type (64 in this example). */
7249 tree ullsize, ullmax, tmp1, tmp2, btmp;
7250
7251 ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7252 ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7253 long_long_unsigned_type_node,
7254 build_int_cst (long_long_unsigned_type_node, 0));
7255
7256 cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type, arg,
7257 fold_convert (arg_type, ullmax));
7258 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, cond,
7259 build_int_cst (arg_type, 0));
7260
7261 tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7262 arg, ullsize);
7263 tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7264 btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7265 tmp1 = fold_convert (result_type,
7266 build_call_expr_loc (input_location, btmp, 1, tmp1));
7267 tmp1 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7268 tmp1, ullsize);
7269
7270 tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7271 btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7272 tmp2 = fold_convert (result_type,
7273 build_call_expr_loc (input_location, btmp, 1, tmp2));
7274
7275 trailz = fold_build3_loc (input_location, COND_EXPR, result_type,
7276 cond, tmp1, tmp2);
7277 }
7278
7279 /* Build BIT_SIZE. */
7280 bit_size = build_int_cst (result_type, argsize);
7281
7282 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7283 arg, build_int_cst (arg_type, 0));
7284 se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7285 bit_size, trailz);
7286 }
7287
7288 /* Using __builtin_popcount for POPCNT and __builtin_parity for POPPAR;
7289 for types larger than "long long", we call the long long built-in for
7290 the lower and higher bits and combine the result. */
7291
7292 static void
7293 gfc_conv_intrinsic_popcnt_poppar (gfc_se * se, gfc_expr *expr, int parity)
7294 {
7295 tree arg;
7296 tree arg_type;
7297 tree result_type;
7298 tree func;
7299 int argsize;
7300
7301 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7302 argsize = TYPE_PRECISION (TREE_TYPE (arg));
7303 result_type = gfc_get_int_type (gfc_default_integer_kind);
7304
7305 /* Which variant of the builtin should we call? */
7306 if (argsize <= INT_TYPE_SIZE)
7307 {
7308 arg_type = unsigned_type_node;
7309 func = builtin_decl_explicit (parity
7310 ? BUILT_IN_PARITY
7311 : BUILT_IN_POPCOUNT);
7312 }
7313 else if (argsize <= LONG_TYPE_SIZE)
7314 {
7315 arg_type = long_unsigned_type_node;
7316 func = builtin_decl_explicit (parity
7317 ? BUILT_IN_PARITYL
7318 : BUILT_IN_POPCOUNTL);
7319 }
7320 else if (argsize <= LONG_LONG_TYPE_SIZE)
7321 {
7322 arg_type = long_long_unsigned_type_node;
7323 func = builtin_decl_explicit (parity
7324 ? BUILT_IN_PARITYLL
7325 : BUILT_IN_POPCOUNTLL);
7326 }
7327 else
7328 {
7329 /* Our argument type is larger than 'long long', which mean none
7330 of the POPCOUNT builtins covers it. We thus call the 'long long'
7331 variant multiple times, and add the results. */
7332 tree utype, arg2, call1, call2;
7333
7334 /* For now, we only cover the case where argsize is twice as large
7335 as 'long long'. */
7336 gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7337
7338 func = builtin_decl_explicit (parity
7339 ? BUILT_IN_PARITYLL
7340 : BUILT_IN_POPCOUNTLL);
7341
7342 /* Convert it to an integer, and store into a variable. */
7343 utype = gfc_build_uint_type (argsize);
7344 arg = fold_convert (utype, arg);
7345 arg = gfc_evaluate_now (arg, &se->pre);
7346
7347 /* Call the builtin twice. */
7348 call1 = build_call_expr_loc (input_location, func, 1,
7349 fold_convert (long_long_unsigned_type_node,
7350 arg));
7351
7352 arg2 = fold_build2_loc (input_location, RSHIFT_EXPR, utype, arg,
7353 build_int_cst (utype, LONG_LONG_TYPE_SIZE));
7354 call2 = build_call_expr_loc (input_location, func, 1,
7355 fold_convert (long_long_unsigned_type_node,
7356 arg2));
7357
7358 /* Combine the results. */
7359 if (parity)
7360 se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR,
7361 integer_type_node, call1, call2);
7362 else
7363 se->expr = fold_build2_loc (input_location, PLUS_EXPR,
7364 integer_type_node, call1, call2);
7365
7366 se->expr = convert (result_type, se->expr);
7367 return;
7368 }
7369
7370 /* Convert the actual argument twice: first, to the unsigned type of the
7371 same size; then, to the proper argument type for the built-in
7372 function. */
7373 arg = fold_convert (gfc_build_uint_type (argsize), arg);
7374 arg = fold_convert (arg_type, arg);
7375
7376 se->expr = fold_convert (result_type,
7377 build_call_expr_loc (input_location, func, 1, arg));
7378 }
7379
7380
7381 /* Process an intrinsic with unspecified argument-types that has an optional
7382 argument (which could be of type character), e.g. EOSHIFT. For those, we
7383 need to append the string length of the optional argument if it is not
7384 present and the type is really character.
7385 primary specifies the position (starting at 1) of the non-optional argument
7386 specifying the type and optional gives the position of the optional
7387 argument in the arglist. */
7388
7389 static void
7390 conv_generic_with_optional_char_arg (gfc_se* se, gfc_expr* expr,
7391 unsigned primary, unsigned optional)
7392 {
7393 gfc_actual_arglist* prim_arg;
7394 gfc_actual_arglist* opt_arg;
7395 unsigned cur_pos;
7396 gfc_actual_arglist* arg;
7397 gfc_symbol* sym;
7398 vec<tree, va_gc> *append_args;
7399
7400 /* Find the two arguments given as position. */
7401 cur_pos = 0;
7402 prim_arg = NULL;
7403 opt_arg = NULL;
7404 for (arg = expr->value.function.actual; arg; arg = arg->next)
7405 {
7406 ++cur_pos;
7407
7408 if (cur_pos == primary)
7409 prim_arg = arg;
7410 if (cur_pos == optional)
7411 opt_arg = arg;
7412
7413 if (cur_pos >= primary && cur_pos >= optional)
7414 break;
7415 }
7416 gcc_assert (prim_arg);
7417 gcc_assert (prim_arg->expr);
7418 gcc_assert (opt_arg);
7419
7420 /* If we do have type CHARACTER and the optional argument is really absent,
7421 append a dummy 0 as string length. */
7422 append_args = NULL;
7423 if (prim_arg->expr->ts.type == BT_CHARACTER && !opt_arg->expr)
7424 {
7425 tree dummy;
7426
7427 dummy = build_int_cst (gfc_charlen_type_node, 0);
7428 vec_alloc (append_args, 1);
7429 append_args->quick_push (dummy);
7430 }
7431
7432 /* Build the call itself. */
7433 gcc_assert (!se->ignore_optional);
7434 sym = gfc_get_symbol_for_expr (expr, false);
7435 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
7436 append_args);
7437 gfc_free_symbol (sym);
7438 }
7439
7440 /* The length of a character string. */
7441 static void
7442 gfc_conv_intrinsic_len (gfc_se * se, gfc_expr * expr)
7443 {
7444 tree len;
7445 tree type;
7446 tree decl;
7447 gfc_symbol *sym;
7448 gfc_se argse;
7449 gfc_expr *arg;
7450
7451 gcc_assert (!se->ss);
7452
7453 arg = expr->value.function.actual->expr;
7454
7455 type = gfc_typenode_for_spec (&expr->ts);
7456 switch (arg->expr_type)
7457 {
7458 case EXPR_CONSTANT:
7459 len = build_int_cst (gfc_charlen_type_node, arg->value.character.length);
7460 break;
7461
7462 case EXPR_ARRAY:
7463 /* Obtain the string length from the function used by
7464 trans-array.cc(gfc_trans_array_constructor). */
7465 len = NULL_TREE;
7466 get_array_ctor_strlen (&se->pre, arg->value.constructor, &len);
7467 break;
7468
7469 case EXPR_VARIABLE:
7470 if (arg->ref == NULL
7471 || (arg->ref->next == NULL && arg->ref->type == REF_ARRAY))
7472 {
7473 /* This doesn't catch all cases.
7474 See http://gcc.gnu.org/ml/fortran/2004-06/msg00165.html
7475 and the surrounding thread. */
7476 sym = arg->symtree->n.sym;
7477 decl = gfc_get_symbol_decl (sym);
7478 if (decl == current_function_decl && sym->attr.function
7479 && (sym->result == sym))
7480 decl = gfc_get_fake_result_decl (sym, 0);
7481
7482 len = sym->ts.u.cl->backend_decl;
7483 gcc_assert (len);
7484 break;
7485 }
7486
7487 /* Fall through. */
7488
7489 default:
7490 gfc_init_se (&argse, se);
7491 if (arg->rank == 0)
7492 gfc_conv_expr (&argse, arg);
7493 else
7494 gfc_conv_expr_descriptor (&argse, arg);
7495 gfc_add_block_to_block (&se->pre, &argse.pre);
7496 gfc_add_block_to_block (&se->post, &argse.post);
7497 len = argse.string_length;
7498 break;
7499 }
7500 se->expr = convert (type, len);
7501 }
7502
7503 /* The length of a character string not including trailing blanks. */
7504 static void
7505 gfc_conv_intrinsic_len_trim (gfc_se * se, gfc_expr * expr)
7506 {
7507 int kind = expr->value.function.actual->expr->ts.kind;
7508 tree args[2], type, fndecl;
7509
7510 gfc_conv_intrinsic_function_args (se, expr, args, 2);
7511 type = gfc_typenode_for_spec (&expr->ts);
7512
7513 if (kind == 1)
7514 fndecl = gfor_fndecl_string_len_trim;
7515 else if (kind == 4)
7516 fndecl = gfor_fndecl_string_len_trim_char4;
7517 else
7518 gcc_unreachable ();
7519
7520 se->expr = build_call_expr_loc (input_location,
7521 fndecl, 2, args[0], args[1]);
7522 se->expr = convert (type, se->expr);
7523 }
7524
7525
7526 /* Returns the starting position of a substring within a string. */
7527
7528 static void
7529 gfc_conv_intrinsic_index_scan_verify (gfc_se * se, gfc_expr * expr,
7530 tree function)
7531 {
7532 tree logical4_type_node = gfc_get_logical_type (4);
7533 tree type;
7534 tree fndecl;
7535 tree *args;
7536 unsigned int num_args;
7537
7538 args = XALLOCAVEC (tree, 5);
7539
7540 /* Get number of arguments; characters count double due to the
7541 string length argument. Kind= is not passed to the library
7542 and thus ignored. */
7543 if (expr->value.function.actual->next->next->expr == NULL)
7544 num_args = 4;
7545 else
7546 num_args = 5;
7547
7548 gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7549 type = gfc_typenode_for_spec (&expr->ts);
7550
7551 if (num_args == 4)
7552 args[4] = build_int_cst (logical4_type_node, 0);
7553 else
7554 args[4] = convert (logical4_type_node, args[4]);
7555
7556 fndecl = build_addr (function);
7557 se->expr = build_call_array_loc (input_location,
7558 TREE_TYPE (TREE_TYPE (function)), fndecl,
7559 5, args);
7560 se->expr = convert (type, se->expr);
7561
7562 }
7563
7564 /* The ascii value for a single character. */
7565 static void
7566 gfc_conv_intrinsic_ichar (gfc_se * se, gfc_expr * expr)
7567 {
7568 tree args[3], type, pchartype;
7569 int nargs;
7570
7571 nargs = gfc_intrinsic_argument_list_length (expr);
7572 gfc_conv_intrinsic_function_args (se, expr, args, nargs);
7573 gcc_assert (POINTER_TYPE_P (TREE_TYPE (args[1])));
7574 pchartype = gfc_get_pchar_type (expr->value.function.actual->expr->ts.kind);
7575 args[1] = fold_build1_loc (input_location, NOP_EXPR, pchartype, args[1]);
7576 type = gfc_typenode_for_spec (&expr->ts);
7577
7578 se->expr = build_fold_indirect_ref_loc (input_location,
7579 args[1]);
7580 se->expr = convert (type, se->expr);
7581 }
7582
7583
7584 /* Intrinsic ISNAN calls __builtin_isnan. */
7585
7586 static void
7587 gfc_conv_intrinsic_isnan (gfc_se * se, gfc_expr * expr)
7588 {
7589 tree arg;
7590
7591 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7592 se->expr = build_call_expr_loc (input_location,
7593 builtin_decl_explicit (BUILT_IN_ISNAN),
7594 1, arg);
7595 STRIP_TYPE_NOPS (se->expr);
7596 se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
7597 }
7598
7599
7600 /* Intrinsics IS_IOSTAT_END and IS_IOSTAT_EOR just need to compare
7601 their argument against a constant integer value. */
7602
7603 static void
7604 gfc_conv_has_intvalue (gfc_se * se, gfc_expr * expr, const int value)
7605 {
7606 tree arg;
7607
7608 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7609 se->expr = fold_build2_loc (input_location, EQ_EXPR,
7610 gfc_typenode_for_spec (&expr->ts),
7611 arg, build_int_cst (TREE_TYPE (arg), value));
7612 }
7613
7614
7615
7616 /* MERGE (tsource, fsource, mask) = mask ? tsource : fsource. */
7617
7618 static void
7619 gfc_conv_intrinsic_merge (gfc_se * se, gfc_expr * expr)
7620 {
7621 tree tsource;
7622 tree fsource;
7623 tree mask;
7624 tree type;
7625 tree len, len2;
7626 tree *args;
7627 unsigned int num_args;
7628
7629 num_args = gfc_intrinsic_argument_list_length (expr);
7630 args = XALLOCAVEC (tree, num_args);
7631
7632 gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7633 if (expr->ts.type != BT_CHARACTER)
7634 {
7635 tsource = args[0];
7636 fsource = args[1];
7637 mask = args[2];
7638 }
7639 else
7640 {
7641 /* We do the same as in the non-character case, but the argument
7642 list is different because of the string length arguments. We
7643 also have to set the string length for the result. */
7644 len = args[0];
7645 tsource = args[1];
7646 len2 = args[2];
7647 fsource = args[3];
7648 mask = args[4];
7649
7650 gfc_trans_same_strlen_check ("MERGE intrinsic", &expr->where, len, len2,
7651 &se->pre);
7652 se->string_length = len;
7653 }
7654 tsource = gfc_evaluate_now (tsource, &se->pre);
7655 fsource = gfc_evaluate_now (fsource, &se->pre);
7656 mask = gfc_evaluate_now (mask, &se->pre);
7657 type = TREE_TYPE (tsource);
7658 se->expr = fold_build3_loc (input_location, COND_EXPR, type, mask, tsource,
7659 fold_convert (type, fsource));
7660 }
7661
7662
7663 /* MERGE_BITS (I, J, MASK) = (I & MASK) | (I & (~MASK)). */
7664
7665 static void
7666 gfc_conv_intrinsic_merge_bits (gfc_se * se, gfc_expr * expr)
7667 {
7668 tree args[3], mask, type;
7669
7670 gfc_conv_intrinsic_function_args (se, expr, args, 3);
7671 mask = gfc_evaluate_now (args[2], &se->pre);
7672
7673 type = TREE_TYPE (args[0]);
7674 gcc_assert (TREE_TYPE (args[1]) == type);
7675 gcc_assert (TREE_TYPE (mask) == type);
7676
7677 args[0] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], mask);
7678 args[1] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[1],
7679 fold_build1_loc (input_location, BIT_NOT_EXPR,
7680 type, mask));
7681 se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
7682 args[0], args[1]);
7683 }
7684
7685
7686 /* MASKL(n) = n == 0 ? 0 : (~0) << (BIT_SIZE - n)
7687 MASKR(n) = n == BIT_SIZE ? ~0 : ~((~0) << n) */
7688
7689 static void
7690 gfc_conv_intrinsic_mask (gfc_se * se, gfc_expr * expr, int left)
7691 {
7692 tree arg, allones, type, utype, res, cond, bitsize;
7693 int i;
7694
7695 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7696 arg = gfc_evaluate_now (arg, &se->pre);
7697
7698 type = gfc_get_int_type (expr->ts.kind);
7699 utype = unsigned_type_for (type);
7700
7701 i = gfc_validate_kind (BT_INTEGER, expr->ts.kind, false);
7702 bitsize = build_int_cst (TREE_TYPE (arg), gfc_integer_kinds[i].bit_size);
7703
7704 allones = fold_build1_loc (input_location, BIT_NOT_EXPR, utype,
7705 build_int_cst (utype, 0));
7706
7707 if (left)
7708 {
7709 /* Left-justified mask. */
7710 res = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (arg),
7711 bitsize, arg);
7712 res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7713 fold_convert (utype, res));
7714
7715 /* Special case arg == 0, because SHIFT_EXPR wants a shift strictly
7716 smaller than type width. */
7717 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
7718 build_int_cst (TREE_TYPE (arg), 0));
7719 res = fold_build3_loc (input_location, COND_EXPR, utype, cond,
7720 build_int_cst (utype, 0), res);
7721 }
7722 else
7723 {
7724 /* Right-justified mask. */
7725 res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7726 fold_convert (utype, arg));
7727 res = fold_build1_loc (input_location, BIT_NOT_EXPR, utype, res);
7728
7729 /* Special case agr == bit_size, because SHIFT_EXPR wants a shift
7730 strictly smaller than type width. */
7731 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7732 arg, bitsize);
7733 res = fold_build3_loc (input_location, COND_EXPR, utype,
7734 cond, allones, res);
7735 }
7736
7737 se->expr = fold_convert (type, res);
7738 }
7739
7740
7741 /* FRACTION (s) is translated into:
7742 isfinite (s) ? frexp (s, &dummy_int) : NaN */
7743 static void
7744 gfc_conv_intrinsic_fraction (gfc_se * se, gfc_expr * expr)
7745 {
7746 tree arg, type, tmp, res, frexp, cond;
7747
7748 frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7749
7750 type = gfc_typenode_for_spec (&expr->ts);
7751 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7752 arg = gfc_evaluate_now (arg, &se->pre);
7753
7754 cond = build_call_expr_loc (input_location,
7755 builtin_decl_explicit (BUILT_IN_ISFINITE),
7756 1, arg);
7757
7758 tmp = gfc_create_var (integer_type_node, NULL);
7759 res = build_call_expr_loc (input_location, frexp, 2,
7760 fold_convert (type, arg),
7761 gfc_build_addr_expr (NULL_TREE, tmp));
7762 res = fold_convert (type, res);
7763
7764 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
7765 cond, res, gfc_build_nan (type, ""));
7766 }
7767
7768
7769 /* NEAREST (s, dir) is translated into
7770 tmp = copysign (HUGE_VAL, dir);
7771 return nextafter (s, tmp);
7772 */
7773 static void
7774 gfc_conv_intrinsic_nearest (gfc_se * se, gfc_expr * expr)
7775 {
7776 tree args[2], type, tmp, nextafter, copysign, huge_val;
7777
7778 nextafter = gfc_builtin_decl_for_float_kind (BUILT_IN_NEXTAFTER, expr->ts.kind);
7779 copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
7780
7781 type = gfc_typenode_for_spec (&expr->ts);
7782 gfc_conv_intrinsic_function_args (se, expr, args, 2);
7783
7784 huge_val = gfc_build_inf_or_huge (type, expr->ts.kind);
7785 tmp = build_call_expr_loc (input_location, copysign, 2, huge_val,
7786 fold_convert (type, args[1]));
7787 se->expr = build_call_expr_loc (input_location, nextafter, 2,
7788 fold_convert (type, args[0]), tmp);
7789 se->expr = fold_convert (type, se->expr);
7790 }
7791
7792
7793 /* SPACING (s) is translated into
7794 int e;
7795 if (!isfinite (s))
7796 res = NaN;
7797 else if (s == 0)
7798 res = tiny;
7799 else
7800 {
7801 frexp (s, &e);
7802 e = e - prec;
7803 e = MAX_EXPR (e, emin);
7804 res = scalbn (1., e);
7805 }
7806 return res;
7807
7808 where prec is the precision of s, gfc_real_kinds[k].digits,
7809 emin is min_exponent - 1, gfc_real_kinds[k].min_exponent - 1,
7810 and tiny is tiny(s), gfc_real_kinds[k].tiny. */
7811
7812 static void
7813 gfc_conv_intrinsic_spacing (gfc_se * se, gfc_expr * expr)
7814 {
7815 tree arg, type, prec, emin, tiny, res, e;
7816 tree cond, nan, tmp, frexp, scalbn;
7817 int k;
7818 stmtblock_t block;
7819
7820 k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
7821 prec = build_int_cst (integer_type_node, gfc_real_kinds[k].digits);
7822 emin = build_int_cst (integer_type_node, gfc_real_kinds[k].min_exponent - 1);
7823 tiny = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].tiny, expr->ts.kind, 0);
7824
7825 frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7826 scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7827
7828 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7829 arg = gfc_evaluate_now (arg, &se->pre);
7830
7831 type = gfc_typenode_for_spec (&expr->ts);
7832 e = gfc_create_var (integer_type_node, NULL);
7833 res = gfc_create_var (type, NULL);
7834
7835
7836 /* Build the block for s /= 0. */
7837 gfc_start_block (&block);
7838 tmp = build_call_expr_loc (input_location, frexp, 2, arg,
7839 gfc_build_addr_expr (NULL_TREE, e));
7840 gfc_add_expr_to_block (&block, tmp);
7841
7842 tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node, e,
7843 prec);
7844 gfc_add_modify (&block, e, fold_build2_loc (input_location, MAX_EXPR,
7845 integer_type_node, tmp, emin));
7846
7847 tmp = build_call_expr_loc (input_location, scalbn, 2,
7848 build_real_from_int_cst (type, integer_one_node), e);
7849 gfc_add_modify (&block, res, tmp);
7850
7851 /* Finish by building the IF statement for value zero. */
7852 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
7853 build_real_from_int_cst (type, integer_zero_node));
7854 tmp = build3_v (COND_EXPR, cond, build2_v (MODIFY_EXPR, res, tiny),
7855 gfc_finish_block (&block));
7856
7857 /* And deal with infinities and NaNs. */
7858 cond = build_call_expr_loc (input_location,
7859 builtin_decl_explicit (BUILT_IN_ISFINITE),
7860 1, arg);
7861 nan = gfc_build_nan (type, "");
7862 tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, res, nan));
7863
7864 gfc_add_expr_to_block (&se->pre, tmp);
7865 se->expr = res;
7866 }
7867
7868
7869 /* RRSPACING (s) is translated into
7870 int e;
7871 real x;
7872 x = fabs (s);
7873 if (isfinite (x))
7874 {
7875 if (x != 0)
7876 {
7877 frexp (s, &e);
7878 x = scalbn (x, precision - e);
7879 }
7880 }
7881 else
7882 x = NaN;
7883 return x;
7884
7885 where precision is gfc_real_kinds[k].digits. */
7886
7887 static void
7888 gfc_conv_intrinsic_rrspacing (gfc_se * se, gfc_expr * expr)
7889 {
7890 tree arg, type, e, x, cond, nan, stmt, tmp, frexp, scalbn, fabs;
7891 int prec, k;
7892 stmtblock_t block;
7893
7894 k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
7895 prec = gfc_real_kinds[k].digits;
7896
7897 frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7898 scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7899 fabs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
7900
7901 type = gfc_typenode_for_spec (&expr->ts);
7902 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7903 arg = gfc_evaluate_now (arg, &se->pre);
7904
7905 e = gfc_create_var (integer_type_node, NULL);
7906 x = gfc_create_var (type, NULL);
7907 gfc_add_modify (&se->pre, x,
7908 build_call_expr_loc (input_location, fabs, 1, arg));
7909
7910
7911 gfc_start_block (&block);
7912 tmp = build_call_expr_loc (input_location, frexp, 2, arg,
7913 gfc_build_addr_expr (NULL_TREE, e));
7914 gfc_add_expr_to_block (&block, tmp);
7915
7916 tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
7917 build_int_cst (integer_type_node, prec), e);
7918 tmp = build_call_expr_loc (input_location, scalbn, 2, x, tmp);
7919 gfc_add_modify (&block, x, tmp);
7920 stmt = gfc_finish_block (&block);
7921
7922 /* if (x != 0) */
7923 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, x,
7924 build_real_from_int_cst (type, integer_zero_node));
7925 tmp = build3_v (COND_EXPR, cond, stmt, build_empty_stmt (input_location));
7926
7927 /* And deal with infinities and NaNs. */
7928 cond = build_call_expr_loc (input_location,
7929 builtin_decl_explicit (BUILT_IN_ISFINITE),
7930 1, x);
7931 nan = gfc_build_nan (type, "");
7932 tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, x, nan));
7933
7934 gfc_add_expr_to_block (&se->pre, tmp);
7935 se->expr = fold_convert (type, x);
7936 }
7937
7938
7939 /* SCALE (s, i) is translated into scalbn (s, i). */
7940 static void
7941 gfc_conv_intrinsic_scale (gfc_se * se, gfc_expr * expr)
7942 {
7943 tree args[2], type, scalbn;
7944
7945 scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7946
7947 type = gfc_typenode_for_spec (&expr->ts);
7948 gfc_conv_intrinsic_function_args (se, expr, args, 2);
7949 se->expr = build_call_expr_loc (input_location, scalbn, 2,
7950 fold_convert (type, args[0]),
7951 fold_convert (integer_type_node, args[1]));
7952 se->expr = fold_convert (type, se->expr);
7953 }
7954
7955
7956 /* SET_EXPONENT (s, i) is translated into
7957 isfinite(s) ? scalbn (frexp (s, &dummy_int), i) : NaN */
7958 static void
7959 gfc_conv_intrinsic_set_exponent (gfc_se * se, gfc_expr * expr)
7960 {
7961 tree args[2], type, tmp, frexp, scalbn, cond, nan, res;
7962
7963 frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7964 scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
7965
7966 type = gfc_typenode_for_spec (&expr->ts);
7967 gfc_conv_intrinsic_function_args (se, expr, args, 2);
7968 args[0] = gfc_evaluate_now (args[0], &se->pre);
7969
7970 tmp = gfc_create_var (integer_type_node, NULL);
7971 tmp = build_call_expr_loc (input_location, frexp, 2,
7972 fold_convert (type, args[0]),
7973 gfc_build_addr_expr (NULL_TREE, tmp));
7974 res = build_call_expr_loc (input_location, scalbn, 2, tmp,
7975 fold_convert (integer_type_node, args[1]));
7976 res = fold_convert (type, res);
7977
7978 /* Call to isfinite */
7979 cond = build_call_expr_loc (input_location,
7980 builtin_decl_explicit (BUILT_IN_ISFINITE),
7981 1, args[0]);
7982 nan = gfc_build_nan (type, "");
7983
7984 se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
7985 res, nan);
7986 }
7987
7988
7989 static void
7990 gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
7991 {
7992 gfc_actual_arglist *actual;
7993 tree arg1;
7994 tree type;
7995 tree size;
7996 gfc_se argse;
7997 gfc_expr *e;
7998 gfc_symbol *sym = NULL;
7999
8000 gfc_init_se (&argse, NULL);
8001 actual = expr->value.function.actual;
8002
8003 if (actual->expr->ts.type == BT_CLASS)
8004 gfc_add_class_array_ref (actual->expr);
8005
8006 e = actual->expr;
8007
8008 /* These are emerging from the interface mapping, when a class valued
8009 function appears as the rhs in a realloc on assign statement, where
8010 the size of the result is that of one of the actual arguments. */
8011 if (e->expr_type == EXPR_VARIABLE
8012 && e->symtree->n.sym->ns == NULL /* This is distinctive! */
8013 && e->symtree->n.sym->ts.type == BT_CLASS
8014 && e->ref && e->ref->type == REF_COMPONENT
8015 && strcmp (e->ref->u.c.component->name, "_data") == 0)
8016 sym = e->symtree->n.sym;
8017
8018 if ((gfc_option.rtcheck & GFC_RTCHECK_POINTER)
8019 && e
8020 && (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION))
8021 {
8022 symbol_attribute attr;
8023 char *msg;
8024 tree temp;
8025 tree cond;
8026
8027 if (e->symtree->n.sym && IS_CLASS_ARRAY (e->symtree->n.sym))
8028 {
8029 attr = CLASS_DATA (e->symtree->n.sym)->attr;
8030 attr.pointer = attr.class_pointer;
8031 }
8032 else
8033 attr = gfc_expr_attr (e);
8034
8035 if (attr.allocatable)
8036 msg = xasprintf ("Allocatable argument '%s' is not allocated",
8037 e->symtree->n.sym->name);
8038 else if (attr.pointer)
8039 msg = xasprintf ("Pointer argument '%s' is not associated",
8040 e->symtree->n.sym->name);
8041 else
8042 goto end_arg_check;
8043
8044 if (sym)
8045 {
8046 temp = gfc_class_data_get (sym->backend_decl);
8047 temp = gfc_conv_descriptor_data_get (temp);
8048 }
8049 else
8050 {
8051 argse.descriptor_only = 1;
8052 gfc_conv_expr_descriptor (&argse, actual->expr);
8053 temp = gfc_conv_descriptor_data_get (argse.expr);
8054 }
8055
8056 cond = fold_build2_loc (input_location, EQ_EXPR,
8057 logical_type_node, temp,
8058 fold_convert (TREE_TYPE (temp),
8059 null_pointer_node));
8060 gfc_trans_runtime_check (true, false, cond, &argse.pre, &e->where, msg);
8061
8062 free (msg);
8063 }
8064 end_arg_check:
8065
8066 argse.data_not_needed = 1;
8067 if (gfc_is_class_array_function (e))
8068 {
8069 /* For functions that return a class array conv_expr_descriptor is not
8070 able to get the descriptor right. Therefore this special case. */
8071 gfc_conv_expr_reference (&argse, e);
8072 argse.expr = gfc_class_data_get (argse.expr);
8073 }
8074 else if (sym && sym->backend_decl)
8075 {
8076 gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (sym->backend_decl)));
8077 argse.expr = gfc_class_data_get (sym->backend_decl);
8078 }
8079 else
8080 gfc_conv_expr_descriptor (&argse, actual->expr);
8081 gfc_add_block_to_block (&se->pre, &argse.pre);
8082 gfc_add_block_to_block (&se->post, &argse.post);
8083 arg1 = argse.expr;
8084
8085 actual = actual->next;
8086 if (actual->expr)
8087 {
8088 stmtblock_t block;
8089 gfc_init_block (&block);
8090 gfc_init_se (&argse, NULL);
8091 gfc_conv_expr_type (&argse, actual->expr,
8092 gfc_array_index_type);
8093 gfc_add_block_to_block (&block, &argse.pre);
8094 tree tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8095 argse.expr, gfc_index_one_node);
8096 size = gfc_tree_array_size (&block, arg1, e, tmp);
8097
8098 /* Unusually, for an intrinsic, size does not exclude
8099 an optional arg2, so we must test for it. */
8100 if (actual->expr->expr_type == EXPR_VARIABLE
8101 && actual->expr->symtree->n.sym->attr.dummy
8102 && actual->expr->symtree->n.sym->attr.optional)
8103 {
8104 tree cond;
8105 stmtblock_t block2;
8106 gfc_init_block (&block2);
8107 gfc_init_se (&argse, NULL);
8108 argse.want_pointer = 1;
8109 argse.data_not_needed = 1;
8110 gfc_conv_expr (&argse, actual->expr);
8111 gfc_add_block_to_block (&se->pre, &argse.pre);
8112 /* 'block2' contains the arg2 absent case, 'block' the arg2 present
8113 case; size_var can be used in both blocks. */
8114 tree size_var = gfc_create_var (TREE_TYPE (size), "size");
8115 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8116 TREE_TYPE (size_var), size_var, size);
8117 gfc_add_expr_to_block (&block, tmp);
8118 size = gfc_tree_array_size (&block2, arg1, e, NULL_TREE);
8119 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8120 TREE_TYPE (size_var), size_var, size);
8121 gfc_add_expr_to_block (&block2, tmp);
8122 cond = gfc_conv_expr_present (actual->expr->symtree->n.sym);
8123 tmp = build3_v (COND_EXPR, cond, gfc_finish_block (&block),
8124 gfc_finish_block (&block2));
8125 gfc_add_expr_to_block (&se->pre, tmp);
8126 size = size_var;
8127 }
8128 else
8129 gfc_add_block_to_block (&se->pre, &block);
8130 }
8131 else
8132 size = gfc_tree_array_size (&se->pre, arg1, e, NULL_TREE);
8133 type = gfc_typenode_for_spec (&expr->ts);
8134 se->expr = convert (type, size);
8135 }
8136
8137
8138 /* Helper function to compute the size of a character variable,
8139 excluding the terminating null characters. The result has
8140 gfc_array_index_type type. */
8141
8142 tree
8143 size_of_string_in_bytes (int kind, tree string_length)
8144 {
8145 tree bytesize;
8146 int i = gfc_validate_kind (BT_CHARACTER, kind, false);
8147
8148 bytesize = build_int_cst (gfc_array_index_type,
8149 gfc_character_kinds[i].bit_size / 8);
8150
8151 return fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8152 bytesize,
8153 fold_convert (gfc_array_index_type, string_length));
8154 }
8155
8156
8157 static void
8158 gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr)
8159 {
8160 gfc_expr *arg;
8161 gfc_se argse;
8162 tree source_bytes;
8163 tree tmp;
8164 tree lower;
8165 tree upper;
8166 tree byte_size;
8167 tree field;
8168 int n;
8169
8170 gfc_init_se (&argse, NULL);
8171 arg = expr->value.function.actual->expr;
8172
8173 if (arg->rank || arg->ts.type == BT_ASSUMED)
8174 gfc_conv_expr_descriptor (&argse, arg);
8175 else
8176 gfc_conv_expr_reference (&argse, arg);
8177
8178 if (arg->ts.type == BT_ASSUMED)
8179 {
8180 /* This only works if an array descriptor has been passed; thus, extract
8181 the size from the descriptor. */
8182 gcc_assert (TYPE_PRECISION (gfc_array_index_type)
8183 == TYPE_PRECISION (size_type_node));
8184 tmp = arg->symtree->n.sym->backend_decl;
8185 tmp = DECL_LANG_SPECIFIC (tmp)
8186 && GFC_DECL_SAVED_DESCRIPTOR (tmp) != NULL_TREE
8187 ? GFC_DECL_SAVED_DESCRIPTOR (tmp) : tmp;
8188 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
8189 tmp = build_fold_indirect_ref_loc (input_location, tmp);
8190
8191 tmp = gfc_conv_descriptor_dtype (tmp);
8192 field = gfc_advance_chain (TYPE_FIELDS (get_dtype_type_node ()),
8193 GFC_DTYPE_ELEM_LEN);
8194 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
8195 tmp, field, NULL_TREE);
8196
8197 byte_size = fold_convert (gfc_array_index_type, tmp);
8198 }
8199 else if (arg->ts.type == BT_CLASS)
8200 {
8201 /* Conv_expr_descriptor returns a component_ref to _data component of the
8202 class object. The class object may be a non-pointer object, e.g.
8203 located on the stack, or a memory location pointed to, e.g. a
8204 parameter, i.e., an indirect_ref. */
8205 if (POINTER_TYPE_P (TREE_TYPE (argse.expr))
8206 && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (argse.expr))))
8207 byte_size
8208 = gfc_class_vtab_size_get (build_fold_indirect_ref (argse.expr));
8209 else if (GFC_CLASS_TYPE_P (TREE_TYPE (argse.expr)))
8210 byte_size = gfc_class_vtab_size_get (argse.expr);
8211 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (argse.expr))
8212 && TREE_CODE (argse.expr) == COMPONENT_REF)
8213 byte_size = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8214 else if (arg->rank > 0
8215 || (arg->rank == 0
8216 && arg->ref && arg->ref->type == REF_COMPONENT))
8217 {
8218 /* The scalarizer added an additional temp. To get the class' vptr
8219 one has to look at the original backend_decl. */
8220 if (argse.class_container)
8221 byte_size = gfc_class_vtab_size_get (argse.class_container);
8222 else if (DECL_LANG_SPECIFIC (arg->symtree->n.sym->backend_decl))
8223 byte_size = gfc_class_vtab_size_get (
8224 GFC_DECL_SAVED_DESCRIPTOR (arg->symtree->n.sym->backend_decl));
8225 else
8226 gcc_unreachable ();
8227 }
8228 else
8229 gcc_unreachable ();
8230 }
8231 else
8232 {
8233 if (arg->ts.type == BT_CHARACTER)
8234 byte_size = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8235 else
8236 {
8237 if (arg->rank == 0)
8238 byte_size = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8239 argse.expr));
8240 else
8241 byte_size = gfc_get_element_type (TREE_TYPE (argse.expr));
8242 byte_size = fold_convert (gfc_array_index_type,
8243 size_in_bytes (byte_size));
8244 }
8245 }
8246
8247 if (arg->rank == 0)
8248 se->expr = byte_size;
8249 else
8250 {
8251 source_bytes = gfc_create_var (gfc_array_index_type, "bytes");
8252 gfc_add_modify (&argse.pre, source_bytes, byte_size);
8253
8254 if (arg->rank == -1)
8255 {
8256 tree cond, loop_var, exit_label;
8257 stmtblock_t body;
8258
8259 tmp = fold_convert (gfc_array_index_type,
8260 gfc_conv_descriptor_rank (argse.expr));
8261 loop_var = gfc_create_var (gfc_array_index_type, "i");
8262 gfc_add_modify (&argse.pre, loop_var, gfc_index_zero_node);
8263 exit_label = gfc_build_label_decl (NULL_TREE);
8264
8265 /* Create loop:
8266 for (;;)
8267 {
8268 if (i >= rank)
8269 goto exit;
8270 source_bytes = source_bytes * array.dim[i].extent;
8271 i = i + 1;
8272 }
8273 exit: */
8274 gfc_start_block (&body);
8275 cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
8276 loop_var, tmp);
8277 tmp = build1_v (GOTO_EXPR, exit_label);
8278 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
8279 cond, tmp, build_empty_stmt (input_location));
8280 gfc_add_expr_to_block (&body, tmp);
8281
8282 lower = gfc_conv_descriptor_lbound_get (argse.expr, loop_var);
8283 upper = gfc_conv_descriptor_ubound_get (argse.expr, loop_var);
8284 tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8285 tmp = fold_build2_loc (input_location, MULT_EXPR,
8286 gfc_array_index_type, tmp, source_bytes);
8287 gfc_add_modify (&body, source_bytes, tmp);
8288
8289 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8290 gfc_array_index_type, loop_var,
8291 gfc_index_one_node);
8292 gfc_add_modify_loc (input_location, &body, loop_var, tmp);
8293
8294 tmp = gfc_finish_block (&body);
8295
8296 tmp = fold_build1_loc (input_location, LOOP_EXPR, void_type_node,
8297 tmp);
8298 gfc_add_expr_to_block (&argse.pre, tmp);
8299
8300 tmp = build1_v (LABEL_EXPR, exit_label);
8301 gfc_add_expr_to_block (&argse.pre, tmp);
8302 }
8303 else
8304 {
8305 /* Obtain the size of the array in bytes. */
8306 for (n = 0; n < arg->rank; n++)
8307 {
8308 tree idx;
8309 idx = gfc_rank_cst[n];
8310 lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8311 upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8312 tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8313 tmp = fold_build2_loc (input_location, MULT_EXPR,
8314 gfc_array_index_type, tmp, source_bytes);
8315 gfc_add_modify (&argse.pre, source_bytes, tmp);
8316 }
8317 }
8318 se->expr = source_bytes;
8319 }
8320
8321 gfc_add_block_to_block (&se->pre, &argse.pre);
8322 }
8323
8324
8325 static void
8326 gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
8327 {
8328 gfc_expr *arg;
8329 gfc_se argse;
8330 tree type, result_type, tmp, class_decl = NULL;
8331 gfc_symbol *sym;
8332 bool unlimited = false;
8333
8334 arg = expr->value.function.actual->expr;
8335
8336 gfc_init_se (&argse, NULL);
8337 result_type = gfc_get_int_type (expr->ts.kind);
8338
8339 if (arg->rank == 0)
8340 {
8341 if (arg->ts.type == BT_CLASS)
8342 {
8343 unlimited = UNLIMITED_POLY (arg);
8344 gfc_add_vptr_component (arg);
8345 gfc_add_size_component (arg);
8346 gfc_conv_expr (&argse, arg);
8347 tmp = fold_convert (result_type, argse.expr);
8348 class_decl = gfc_get_class_from_expr (argse.expr);
8349 goto done;
8350 }
8351
8352 gfc_conv_expr_reference (&argse, arg);
8353 type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8354 argse.expr));
8355 }
8356 else
8357 {
8358 argse.want_pointer = 0;
8359 gfc_conv_expr_descriptor (&argse, arg);
8360 sym = arg->expr_type == EXPR_VARIABLE ? arg->symtree->n.sym : NULL;
8361 if (arg->ts.type == BT_CLASS)
8362 {
8363 unlimited = UNLIMITED_POLY (arg);
8364 if (TREE_CODE (argse.expr) == COMPONENT_REF)
8365 tmp = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8366 else if (arg->rank > 0 && sym
8367 && DECL_LANG_SPECIFIC (sym->backend_decl))
8368 tmp = gfc_class_vtab_size_get (
8369 GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl));
8370 else
8371 gcc_unreachable ();
8372 tmp = fold_convert (result_type, tmp);
8373 class_decl = gfc_get_class_from_expr (argse.expr);
8374 goto done;
8375 }
8376 type = gfc_get_element_type (TREE_TYPE (argse.expr));
8377 }
8378
8379 /* Obtain the argument's word length. */
8380 if (arg->ts.type == BT_CHARACTER)
8381 tmp = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8382 else
8383 tmp = size_in_bytes (type);
8384 tmp = fold_convert (result_type, tmp);
8385
8386 done:
8387 if (unlimited && class_decl)
8388 tmp = gfc_resize_class_size_with_len (NULL, class_decl, tmp);
8389
8390 se->expr = fold_build2_loc (input_location, MULT_EXPR, result_type, tmp,
8391 build_int_cst (result_type, BITS_PER_UNIT));
8392 gfc_add_block_to_block (&se->pre, &argse.pre);
8393 }
8394
8395
8396 /* Intrinsic string comparison functions. */
8397
8398 static void
8399 gfc_conv_intrinsic_strcmp (gfc_se * se, gfc_expr * expr, enum tree_code op)
8400 {
8401 tree args[4];
8402
8403 gfc_conv_intrinsic_function_args (se, expr, args, 4);
8404
8405 se->expr
8406 = gfc_build_compare_string (args[0], args[1], args[2], args[3],
8407 expr->value.function.actual->expr->ts.kind,
8408 op);
8409 se->expr = fold_build2_loc (input_location, op,
8410 gfc_typenode_for_spec (&expr->ts), se->expr,
8411 build_int_cst (TREE_TYPE (se->expr), 0));
8412 }
8413
8414 /* Generate a call to the adjustl/adjustr library function. */
8415 static void
8416 gfc_conv_intrinsic_adjust (gfc_se * se, gfc_expr * expr, tree fndecl)
8417 {
8418 tree args[3];
8419 tree len;
8420 tree type;
8421 tree var;
8422 tree tmp;
8423
8424 gfc_conv_intrinsic_function_args (se, expr, &args[1], 2);
8425 len = args[1];
8426
8427 type = TREE_TYPE (args[2]);
8428 var = gfc_conv_string_tmp (se, type, len);
8429 args[0] = var;
8430
8431 tmp = build_call_expr_loc (input_location,
8432 fndecl, 3, args[0], args[1], args[2]);
8433 gfc_add_expr_to_block (&se->pre, tmp);
8434 se->expr = var;
8435 se->string_length = len;
8436 }
8437
8438
8439 /* Generate code for the TRANSFER intrinsic:
8440 For scalar results:
8441 DEST = TRANSFER (SOURCE, MOLD)
8442 where:
8443 typeof<DEST> = typeof<MOLD>
8444 and:
8445 MOLD is scalar.
8446
8447 For array results:
8448 DEST(1:N) = TRANSFER (SOURCE, MOLD[, SIZE])
8449 where:
8450 typeof<DEST> = typeof<MOLD>
8451 and:
8452 N = min (sizeof (SOURCE(:)), sizeof (DEST(:)),
8453 sizeof (DEST(0) * SIZE). */
8454 static void
8455 gfc_conv_intrinsic_transfer (gfc_se * se, gfc_expr * expr)
8456 {
8457 tree tmp;
8458 tree tmpdecl;
8459 tree ptr;
8460 tree extent;
8461 tree source;
8462 tree source_type;
8463 tree source_bytes;
8464 tree mold_type;
8465 tree dest_word_len;
8466 tree size_words;
8467 tree size_bytes;
8468 tree upper;
8469 tree lower;
8470 tree stmt;
8471 tree class_ref = NULL_TREE;
8472 gfc_actual_arglist *arg;
8473 gfc_se argse;
8474 gfc_array_info *info;
8475 stmtblock_t block;
8476 int n;
8477 bool scalar_mold;
8478 gfc_expr *source_expr, *mold_expr, *class_expr;
8479
8480 info = NULL;
8481 if (se->loop)
8482 info = &se->ss->info->data.array;
8483
8484 /* Convert SOURCE. The output from this stage is:-
8485 source_bytes = length of the source in bytes
8486 source = pointer to the source data. */
8487 arg = expr->value.function.actual;
8488 source_expr = arg->expr;
8489
8490 /* Ensure double transfer through LOGICAL preserves all
8491 the needed bits. */
8492 if (arg->expr->expr_type == EXPR_FUNCTION
8493 && arg->expr->value.function.esym == NULL
8494 && arg->expr->value.function.isym != NULL
8495 && arg->expr->value.function.isym->id == GFC_ISYM_TRANSFER
8496 && arg->expr->ts.type == BT_LOGICAL
8497 && expr->ts.type != arg->expr->ts.type)
8498 arg->expr->value.function.name = "__transfer_in_transfer";
8499
8500 gfc_init_se (&argse, NULL);
8501
8502 source_bytes = gfc_create_var (gfc_array_index_type, NULL);
8503
8504 /* Obtain the pointer to source and the length of source in bytes. */
8505 if (arg->expr->rank == 0)
8506 {
8507 gfc_conv_expr_reference (&argse, arg->expr);
8508 if (arg->expr->ts.type == BT_CLASS)
8509 {
8510 tmp = build_fold_indirect_ref_loc (input_location, argse.expr);
8511 if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
8512 {
8513 source = gfc_class_data_get (tmp);
8514 class_ref = tmp;
8515 }
8516 else
8517 {
8518 /* Array elements are evaluated as a reference to the data.
8519 To obtain the vptr for the element size, the argument
8520 expression must be stripped to the class reference and
8521 re-evaluated. The pre and post blocks are not needed. */
8522 gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
8523 source = argse.expr;
8524 class_expr = gfc_find_and_cut_at_last_class_ref (arg->expr);
8525 gfc_init_se (&argse, NULL);
8526 gfc_conv_expr (&argse, class_expr);
8527 class_ref = argse.expr;
8528 }
8529 }
8530 else
8531 source = argse.expr;
8532
8533 /* Obtain the source word length. */
8534 switch (arg->expr->ts.type)
8535 {
8536 case BT_CHARACTER:
8537 tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8538 argse.string_length);
8539 break;
8540 case BT_CLASS:
8541 if (class_ref != NULL_TREE)
8542 {
8543 tmp = gfc_class_vtab_size_get (class_ref);
8544 if (UNLIMITED_POLY (source_expr))
8545 tmp = gfc_resize_class_size_with_len (NULL, class_ref, tmp);
8546 }
8547 else
8548 {
8549 tmp = gfc_class_vtab_size_get (argse.expr);
8550 if (UNLIMITED_POLY (source_expr))
8551 tmp = gfc_resize_class_size_with_len (NULL, argse.expr, tmp);
8552 }
8553 break;
8554 default:
8555 source_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8556 source));
8557 tmp = fold_convert (gfc_array_index_type,
8558 size_in_bytes (source_type));
8559 break;
8560 }
8561 }
8562 else
8563 {
8564 bool simply_contiguous = gfc_is_simply_contiguous (arg->expr,
8565 false, true);
8566 argse.want_pointer = 0;
8567 /* A non-contiguous SOURCE needs packing. */
8568 if (!simply_contiguous)
8569 argse.force_tmp = 1;
8570 gfc_conv_expr_descriptor (&argse, arg->expr);
8571 source = gfc_conv_descriptor_data_get (argse.expr);
8572 source_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8573
8574 /* Repack the source if not simply contiguous. */
8575 if (!simply_contiguous)
8576 {
8577 tmp = gfc_build_addr_expr (NULL_TREE, argse.expr);
8578
8579 if (warn_array_temporaries)
8580 gfc_warning (OPT_Warray_temporaries,
8581 "Creating array temporary at %L", &expr->where);
8582
8583 source = build_call_expr_loc (input_location,
8584 gfor_fndecl_in_pack, 1, tmp);
8585 source = gfc_evaluate_now (source, &argse.pre);
8586
8587 /* Free the temporary. */
8588 gfc_start_block (&block);
8589 tmp = gfc_call_free (source);
8590 gfc_add_expr_to_block (&block, tmp);
8591 stmt = gfc_finish_block (&block);
8592
8593 /* Clean up if it was repacked. */
8594 gfc_init_block (&block);
8595 tmp = gfc_conv_array_data (argse.expr);
8596 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
8597 source, tmp);
8598 tmp = build3_v (COND_EXPR, tmp, stmt,
8599 build_empty_stmt (input_location));
8600 gfc_add_expr_to_block (&block, tmp);
8601 gfc_add_block_to_block (&block, &se->post);
8602 gfc_init_block (&se->post);
8603 gfc_add_block_to_block (&se->post, &block);
8604 }
8605
8606 /* Obtain the source word length. */
8607 if (arg->expr->ts.type == BT_CHARACTER)
8608 tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8609 argse.string_length);
8610 else if (arg->expr->ts.type == BT_CLASS)
8611 {
8612 if (UNLIMITED_POLY (source_expr)
8613 && DECL_LANG_SPECIFIC (source_expr->symtree->n.sym->backend_decl))
8614 class_ref = GFC_DECL_SAVED_DESCRIPTOR
8615 (source_expr->symtree->n.sym->backend_decl);
8616 else
8617 class_ref = TREE_OPERAND (argse.expr, 0);
8618 tmp = gfc_class_vtab_size_get (class_ref);
8619 if (UNLIMITED_POLY (arg->expr))
8620 tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8621 }
8622 else
8623 tmp = fold_convert (gfc_array_index_type,
8624 size_in_bytes (source_type));
8625
8626 /* Obtain the size of the array in bytes. */
8627 extent = gfc_create_var (gfc_array_index_type, NULL);
8628 for (n = 0; n < arg->expr->rank; n++)
8629 {
8630 tree idx;
8631 idx = gfc_rank_cst[n];
8632 gfc_add_modify (&argse.pre, source_bytes, tmp);
8633 lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8634 upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8635 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8636 gfc_array_index_type, upper, lower);
8637 gfc_add_modify (&argse.pre, extent, tmp);
8638 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8639 gfc_array_index_type, extent,
8640 gfc_index_one_node);
8641 tmp = fold_build2_loc (input_location, MULT_EXPR,
8642 gfc_array_index_type, tmp, source_bytes);
8643 }
8644 }
8645
8646 gfc_add_modify (&argse.pre, source_bytes, tmp);
8647 gfc_add_block_to_block (&se->pre, &argse.pre);
8648 gfc_add_block_to_block (&se->post, &argse.post);
8649
8650 /* Now convert MOLD. The outputs are:
8651 mold_type = the TREE type of MOLD
8652 dest_word_len = destination word length in bytes. */
8653 arg = arg->next;
8654 mold_expr = arg->expr;
8655
8656 gfc_init_se (&argse, NULL);
8657
8658 scalar_mold = arg->expr->rank == 0;
8659
8660 if (arg->expr->rank == 0)
8661 {
8662 gfc_conv_expr_reference (&argse, mold_expr);
8663 mold_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8664 argse.expr));
8665 }
8666 else
8667 {
8668 argse.want_pointer = 0;
8669 gfc_conv_expr_descriptor (&argse, mold_expr);
8670 mold_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8671 }
8672
8673 gfc_add_block_to_block (&se->pre, &argse.pre);
8674 gfc_add_block_to_block (&se->post, &argse.post);
8675
8676 if (strcmp (expr->value.function.name, "__transfer_in_transfer") == 0)
8677 {
8678 /* If this TRANSFER is nested in another TRANSFER, use a type
8679 that preserves all bits. */
8680 if (mold_expr->ts.type == BT_LOGICAL)
8681 mold_type = gfc_get_int_type (mold_expr->ts.kind);
8682 }
8683
8684 /* Obtain the destination word length. */
8685 switch (mold_expr->ts.type)
8686 {
8687 case BT_CHARACTER:
8688 tmp = size_of_string_in_bytes (mold_expr->ts.kind, argse.string_length);
8689 mold_type = gfc_get_character_type_len (mold_expr->ts.kind,
8690 argse.string_length);
8691 break;
8692 case BT_CLASS:
8693 if (scalar_mold)
8694 class_ref = argse.expr;
8695 else
8696 class_ref = TREE_OPERAND (argse.expr, 0);
8697 tmp = gfc_class_vtab_size_get (class_ref);
8698 if (UNLIMITED_POLY (arg->expr))
8699 tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8700 break;
8701 default:
8702 tmp = fold_convert (gfc_array_index_type, size_in_bytes (mold_type));
8703 break;
8704 }
8705
8706 /* Do not fix dest_word_len if it is a variable, since the temporary can wind
8707 up being used before the assignment. */
8708 if (mold_expr->ts.type == BT_CHARACTER && mold_expr->ts.deferred)
8709 dest_word_len = tmp;
8710 else
8711 {
8712 dest_word_len = gfc_create_var (gfc_array_index_type, NULL);
8713 gfc_add_modify (&se->pre, dest_word_len, tmp);
8714 }
8715
8716 /* Finally convert SIZE, if it is present. */
8717 arg = arg->next;
8718 size_words = gfc_create_var (gfc_array_index_type, NULL);
8719
8720 if (arg->expr)
8721 {
8722 gfc_init_se (&argse, NULL);
8723 gfc_conv_expr_reference (&argse, arg->expr);
8724 tmp = convert (gfc_array_index_type,
8725 build_fold_indirect_ref_loc (input_location,
8726 argse.expr));
8727 gfc_add_block_to_block (&se->pre, &argse.pre);
8728 gfc_add_block_to_block (&se->post, &argse.post);
8729 }
8730 else
8731 tmp = NULL_TREE;
8732
8733 /* Separate array and scalar results. */
8734 if (scalar_mold && tmp == NULL_TREE)
8735 goto scalar_transfer;
8736
8737 size_bytes = gfc_create_var (gfc_array_index_type, NULL);
8738 if (tmp != NULL_TREE)
8739 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8740 tmp, dest_word_len);
8741 else
8742 tmp = source_bytes;
8743
8744 gfc_add_modify (&se->pre, size_bytes, tmp);
8745 gfc_add_modify (&se->pre, size_words,
8746 fold_build2_loc (input_location, CEIL_DIV_EXPR,
8747 gfc_array_index_type,
8748 size_bytes, dest_word_len));
8749
8750 /* Evaluate the bounds of the result. If the loop range exists, we have
8751 to check if it is too large. If so, we modify loop->to be consistent
8752 with min(size, size(source)). Otherwise, size is made consistent with
8753 the loop range, so that the right number of bytes is transferred.*/
8754 n = se->loop->order[0];
8755 if (se->loop->to[n] != NULL_TREE)
8756 {
8757 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8758 se->loop->to[n], se->loop->from[n]);
8759 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8760 tmp, gfc_index_one_node);
8761 tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
8762 tmp, size_words);
8763 gfc_add_modify (&se->pre, size_words, tmp);
8764 gfc_add_modify (&se->pre, size_bytes,
8765 fold_build2_loc (input_location, MULT_EXPR,
8766 gfc_array_index_type,
8767 size_words, dest_word_len));
8768 upper = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8769 size_words, se->loop->from[n]);
8770 upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8771 upper, gfc_index_one_node);
8772 }
8773 else
8774 {
8775 upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8776 size_words, gfc_index_one_node);
8777 se->loop->from[n] = gfc_index_zero_node;
8778 }
8779
8780 se->loop->to[n] = upper;
8781
8782 /* Build a destination descriptor, using the pointer, source, as the
8783 data field. */
8784 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss, mold_type,
8785 NULL_TREE, false, true, false, &expr->where);
8786
8787 /* Cast the pointer to the result. */
8788 tmp = gfc_conv_descriptor_data_get (info->descriptor);
8789 tmp = fold_convert (pvoid_type_node, tmp);
8790
8791 /* Use memcpy to do the transfer. */
8792 tmp
8793 = build_call_expr_loc (input_location,
8794 builtin_decl_explicit (BUILT_IN_MEMCPY), 3, tmp,
8795 fold_convert (pvoid_type_node, source),
8796 fold_convert (size_type_node,
8797 fold_build2_loc (input_location,
8798 MIN_EXPR,
8799 gfc_array_index_type,
8800 size_bytes,
8801 source_bytes)));
8802 gfc_add_expr_to_block (&se->pre, tmp);
8803
8804 se->expr = info->descriptor;
8805 if (expr->ts.type == BT_CHARACTER)
8806 {
8807 tmp = fold_convert (gfc_charlen_type_node,
8808 TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
8809 se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
8810 gfc_charlen_type_node,
8811 dest_word_len, tmp);
8812 }
8813
8814 return;
8815
8816 /* Deal with scalar results. */
8817 scalar_transfer:
8818 extent = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
8819 dest_word_len, source_bytes);
8820 extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
8821 extent, gfc_index_zero_node);
8822
8823 if (expr->ts.type == BT_CHARACTER)
8824 {
8825 tree direct, indirect, free;
8826
8827 ptr = convert (gfc_get_pchar_type (expr->ts.kind), source);
8828 tmpdecl = gfc_create_var (gfc_get_pchar_type (expr->ts.kind),
8829 "transfer");
8830
8831 /* If source is longer than the destination, use a pointer to
8832 the source directly. */
8833 gfc_init_block (&block);
8834 gfc_add_modify (&block, tmpdecl, ptr);
8835 direct = gfc_finish_block (&block);
8836
8837 /* Otherwise, allocate a string with the length of the destination
8838 and copy the source into it. */
8839 gfc_init_block (&block);
8840 tmp = gfc_get_pchar_type (expr->ts.kind);
8841 tmp = gfc_call_malloc (&block, tmp, dest_word_len);
8842 gfc_add_modify (&block, tmpdecl,
8843 fold_convert (TREE_TYPE (ptr), tmp));
8844 tmp = build_call_expr_loc (input_location,
8845 builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
8846 fold_convert (pvoid_type_node, tmpdecl),
8847 fold_convert (pvoid_type_node, ptr),
8848 fold_convert (size_type_node, extent));
8849 gfc_add_expr_to_block (&block, tmp);
8850 indirect = gfc_finish_block (&block);
8851
8852 /* Wrap it up with the condition. */
8853 tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
8854 dest_word_len, source_bytes);
8855 tmp = build3_v (COND_EXPR, tmp, direct, indirect);
8856 gfc_add_expr_to_block (&se->pre, tmp);
8857
8858 /* Free the temporary string, if necessary. */
8859 free = gfc_call_free (tmpdecl);
8860 tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
8861 dest_word_len, source_bytes);
8862 tmp = build3_v (COND_EXPR, tmp, free, build_empty_stmt (input_location));
8863 gfc_add_expr_to_block (&se->post, tmp);
8864
8865 se->expr = tmpdecl;
8866 tmp = fold_convert (gfc_charlen_type_node,
8867 TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
8868 se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
8869 gfc_charlen_type_node,
8870 dest_word_len, tmp);
8871 }
8872 else
8873 {
8874 tmpdecl = gfc_create_var (mold_type, "transfer");
8875
8876 ptr = convert (build_pointer_type (mold_type), source);
8877
8878 /* For CLASS results, allocate the needed memory first. */
8879 if (mold_expr->ts.type == BT_CLASS)
8880 {
8881 tree cdata;
8882 cdata = gfc_class_data_get (tmpdecl);
8883 tmp = gfc_call_malloc (&se->pre, TREE_TYPE (cdata), dest_word_len);
8884 gfc_add_modify (&se->pre, cdata, tmp);
8885 }
8886
8887 /* Use memcpy to do the transfer. */
8888 if (mold_expr->ts.type == BT_CLASS)
8889 tmp = gfc_class_data_get (tmpdecl);
8890 else
8891 tmp = gfc_build_addr_expr (NULL_TREE, tmpdecl);
8892
8893 tmp = build_call_expr_loc (input_location,
8894 builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
8895 fold_convert (pvoid_type_node, tmp),
8896 fold_convert (pvoid_type_node, ptr),
8897 fold_convert (size_type_node, extent));
8898 gfc_add_expr_to_block (&se->pre, tmp);
8899
8900 /* For CLASS results, set the _vptr. */
8901 if (mold_expr->ts.type == BT_CLASS)
8902 gfc_reset_vptr (&se->pre, nullptr, tmpdecl, source_expr->ts.u.derived);
8903
8904 se->expr = tmpdecl;
8905 }
8906 }
8907
8908
8909 /* Generate code for the ALLOCATED intrinsic.
8910 Generate inline code that directly check the address of the argument. */
8911
8912 static void
8913 gfc_conv_allocated (gfc_se *se, gfc_expr *expr)
8914 {
8915 gfc_se arg1se;
8916 tree tmp;
8917 gfc_expr *e = expr->value.function.actual->expr;
8918
8919 gfc_init_se (&arg1se, NULL);
8920 if (e->ts.type == BT_CLASS)
8921 {
8922 /* Make sure that class array expressions have both a _data
8923 component reference and an array reference.... */
8924 if (CLASS_DATA (e)->attr.dimension)
8925 gfc_add_class_array_ref (e);
8926 /* .... whilst scalars only need the _data component. */
8927 else
8928 gfc_add_data_component (e);
8929 }
8930
8931 gcc_assert (flag_coarray != GFC_FCOARRAY_LIB || !gfc_is_coindexed (e));
8932
8933 if (e->rank == 0)
8934 {
8935 /* Allocatable scalar. */
8936 arg1se.want_pointer = 1;
8937 gfc_conv_expr (&arg1se, e);
8938 tmp = arg1se.expr;
8939 }
8940 else
8941 {
8942 /* Allocatable array. */
8943 arg1se.descriptor_only = 1;
8944 gfc_conv_expr_descriptor (&arg1se, e);
8945 tmp = gfc_conv_descriptor_data_get (arg1se.expr);
8946 }
8947
8948 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
8949 fold_convert (TREE_TYPE (tmp), null_pointer_node));
8950
8951 /* Components of pointer array references sometimes come back with a pre block. */
8952 if (arg1se.pre.head)
8953 gfc_add_block_to_block (&se->pre, &arg1se.pre);
8954
8955 se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
8956 }
8957
8958
8959 /* Generate code for the ASSOCIATED intrinsic.
8960 If both POINTER and TARGET are arrays, generate a call to library function
8961 _gfor_associated, and pass descriptors of POINTER and TARGET to it.
8962 In other cases, generate inline code that directly compare the address of
8963 POINTER with the address of TARGET. */
8964
8965 static void
8966 gfc_conv_associated (gfc_se *se, gfc_expr *expr)
8967 {
8968 gfc_actual_arglist *arg1;
8969 gfc_actual_arglist *arg2;
8970 gfc_se arg1se;
8971 gfc_se arg2se;
8972 tree tmp2;
8973 tree tmp;
8974 tree nonzero_arraylen = NULL_TREE;
8975 gfc_ss *ss;
8976 bool scalar;
8977
8978 gfc_init_se (&arg1se, NULL);
8979 gfc_init_se (&arg2se, NULL);
8980 arg1 = expr->value.function.actual;
8981 arg2 = arg1->next;
8982
8983 /* Check whether the expression is a scalar or not; we cannot use
8984 arg1->expr->rank as it can be nonzero for proc pointers. */
8985 ss = gfc_walk_expr (arg1->expr);
8986 scalar = ss == gfc_ss_terminator;
8987 if (!scalar)
8988 gfc_free_ss_chain (ss);
8989
8990 if (!arg2->expr)
8991 {
8992 /* No optional target. */
8993 if (scalar)
8994 {
8995 /* A pointer to a scalar. */
8996 arg1se.want_pointer = 1;
8997 gfc_conv_expr (&arg1se, arg1->expr);
8998 if (arg1->expr->symtree->n.sym->attr.proc_pointer
8999 && arg1->expr->symtree->n.sym->attr.dummy)
9000 arg1se.expr = build_fold_indirect_ref_loc (input_location,
9001 arg1se.expr);
9002 if (arg1->expr->ts.type == BT_CLASS)
9003 {
9004 tmp2 = gfc_class_data_get (arg1se.expr);
9005 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
9006 tmp2 = gfc_conv_descriptor_data_get (tmp2);
9007 }
9008 else
9009 tmp2 = arg1se.expr;
9010 }
9011 else
9012 {
9013 /* A pointer to an array. */
9014 gfc_conv_expr_descriptor (&arg1se, arg1->expr);
9015 tmp2 = gfc_conv_descriptor_data_get (arg1se.expr);
9016 }
9017 gfc_add_block_to_block (&se->pre, &arg1se.pre);
9018 gfc_add_block_to_block (&se->post, &arg1se.post);
9019 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp2,
9020 fold_convert (TREE_TYPE (tmp2), null_pointer_node));
9021 se->expr = tmp;
9022 }
9023 else
9024 {
9025 /* An optional target. */
9026 if (arg2->expr->ts.type == BT_CLASS
9027 && arg2->expr->expr_type != EXPR_FUNCTION)
9028 gfc_add_data_component (arg2->expr);
9029
9030 if (scalar)
9031 {
9032 /* A pointer to a scalar. */
9033 arg1se.want_pointer = 1;
9034 gfc_conv_expr (&arg1se, arg1->expr);
9035 if (arg1->expr->symtree->n.sym->attr.proc_pointer
9036 && arg1->expr->symtree->n.sym->attr.dummy)
9037 arg1se.expr = build_fold_indirect_ref_loc (input_location,
9038 arg1se.expr);
9039 if (arg1->expr->ts.type == BT_CLASS)
9040 arg1se.expr = gfc_class_data_get (arg1se.expr);
9041
9042 arg2se.want_pointer = 1;
9043 gfc_conv_expr (&arg2se, arg2->expr);
9044 if (arg2->expr->symtree->n.sym->attr.proc_pointer
9045 && arg2->expr->symtree->n.sym->attr.dummy)
9046 arg2se.expr = build_fold_indirect_ref_loc (input_location,
9047 arg2se.expr);
9048 if (arg2->expr->ts.type == BT_CLASS)
9049 {
9050 arg2se.expr = gfc_evaluate_now (arg2se.expr, &arg2se.pre);
9051 arg2se.expr = gfc_class_data_get (arg2se.expr);
9052 }
9053 gfc_add_block_to_block (&se->pre, &arg1se.pre);
9054 gfc_add_block_to_block (&se->post, &arg1se.post);
9055 gfc_add_block_to_block (&se->pre, &arg2se.pre);
9056 gfc_add_block_to_block (&se->post, &arg2se.post);
9057 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9058 arg1se.expr, arg2se.expr);
9059 tmp2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9060 arg1se.expr, null_pointer_node);
9061 se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9062 logical_type_node, tmp, tmp2);
9063 }
9064 else
9065 {
9066 /* An array pointer of zero length is not associated if target is
9067 present. */
9068 arg1se.descriptor_only = 1;
9069 gfc_conv_expr_lhs (&arg1se, arg1->expr);
9070 if (arg1->expr->rank == -1)
9071 {
9072 tmp = gfc_conv_descriptor_rank (arg1se.expr);
9073 tmp = fold_build2_loc (input_location, MINUS_EXPR,
9074 TREE_TYPE (tmp), tmp,
9075 build_int_cst (TREE_TYPE (tmp), 1));
9076 }
9077 else
9078 tmp = gfc_rank_cst[arg1->expr->rank - 1];
9079 tmp = gfc_conv_descriptor_stride_get (arg1se.expr, tmp);
9080 if (arg2->expr->rank != 0)
9081 nonzero_arraylen = fold_build2_loc (input_location, NE_EXPR,
9082 logical_type_node, tmp,
9083 build_int_cst (TREE_TYPE (tmp), 0));
9084
9085 /* A pointer to an array, call library function _gfor_associated. */
9086 arg1se.want_pointer = 1;
9087 gfc_conv_expr_descriptor (&arg1se, arg1->expr);
9088 gfc_add_block_to_block (&se->pre, &arg1se.pre);
9089 gfc_add_block_to_block (&se->post, &arg1se.post);
9090
9091 arg2se.want_pointer = 1;
9092 arg2se.force_no_tmp = 1;
9093 if (arg2->expr->rank != 0)
9094 gfc_conv_expr_descriptor (&arg2se, arg2->expr);
9095 else
9096 {
9097 gfc_conv_expr (&arg2se, arg2->expr);
9098 arg2se.expr
9099 = gfc_conv_scalar_to_descriptor (&arg2se, arg2se.expr,
9100 gfc_expr_attr (arg2->expr));
9101 arg2se.expr = gfc_build_addr_expr (NULL_TREE, arg2se.expr);
9102 }
9103 gfc_add_block_to_block (&se->pre, &arg2se.pre);
9104 gfc_add_block_to_block (&se->post, &arg2se.post);
9105 se->expr = build_call_expr_loc (input_location,
9106 gfor_fndecl_associated, 2,
9107 arg1se.expr, arg2se.expr);
9108 se->expr = convert (logical_type_node, se->expr);
9109 if (arg2->expr->rank != 0)
9110 se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9111 logical_type_node, se->expr,
9112 nonzero_arraylen);
9113 }
9114
9115 /* If target is present zero character length pointers cannot
9116 be associated. */
9117 if (arg1->expr->ts.type == BT_CHARACTER)
9118 {
9119 tmp = arg1se.string_length;
9120 tmp = fold_build2_loc (input_location, NE_EXPR,
9121 logical_type_node, tmp,
9122 build_zero_cst (TREE_TYPE (tmp)));
9123 se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9124 logical_type_node, se->expr, tmp);
9125 }
9126 }
9127
9128 se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9129 }
9130
9131
9132 /* Generate code for the SAME_TYPE_AS intrinsic.
9133 Generate inline code that directly checks the vindices. */
9134
9135 static void
9136 gfc_conv_same_type_as (gfc_se *se, gfc_expr *expr)
9137 {
9138 gfc_expr *a, *b;
9139 gfc_se se1, se2;
9140 tree tmp;
9141 tree conda = NULL_TREE, condb = NULL_TREE;
9142
9143 gfc_init_se (&se1, NULL);
9144 gfc_init_se (&se2, NULL);
9145
9146 a = expr->value.function.actual->expr;
9147 b = expr->value.function.actual->next->expr;
9148
9149 bool unlimited_poly_a = UNLIMITED_POLY (a);
9150 bool unlimited_poly_b = UNLIMITED_POLY (b);
9151 if (unlimited_poly_a)
9152 {
9153 se1.want_pointer = 1;
9154 gfc_add_vptr_component (a);
9155 }
9156 else if (a->ts.type == BT_CLASS)
9157 {
9158 gfc_add_vptr_component (a);
9159 gfc_add_hash_component (a);
9160 }
9161 else if (a->ts.type == BT_DERIVED)
9162 a = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9163 a->ts.u.derived->hash_value);
9164
9165 if (unlimited_poly_b)
9166 {
9167 se2.want_pointer = 1;
9168 gfc_add_vptr_component (b);
9169 }
9170 else if (b->ts.type == BT_CLASS)
9171 {
9172 gfc_add_vptr_component (b);
9173 gfc_add_hash_component (b);
9174 }
9175 else if (b->ts.type == BT_DERIVED)
9176 b = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9177 b->ts.u.derived->hash_value);
9178
9179 gfc_conv_expr (&se1, a);
9180 gfc_conv_expr (&se2, b);
9181
9182 if (unlimited_poly_a)
9183 {
9184 conda = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9185 se1.expr,
9186 build_int_cst (TREE_TYPE (se1.expr), 0));
9187 se1.expr = gfc_vptr_hash_get (se1.expr);
9188 }
9189
9190 if (unlimited_poly_b)
9191 {
9192 condb = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9193 se2.expr,
9194 build_int_cst (TREE_TYPE (se2.expr), 0));
9195 se2.expr = gfc_vptr_hash_get (se2.expr);
9196 }
9197
9198 tmp = fold_build2_loc (input_location, EQ_EXPR,
9199 logical_type_node, se1.expr,
9200 fold_convert (TREE_TYPE (se1.expr), se2.expr));
9201
9202 if (conda)
9203 tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9204 logical_type_node, conda, tmp);
9205
9206 if (condb)
9207 tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9208 logical_type_node, condb, tmp);
9209
9210 se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
9211 }
9212
9213
9214 /* Generate code for SELECTED_CHAR_KIND (NAME) intrinsic function. */
9215
9216 static void
9217 gfc_conv_intrinsic_sc_kind (gfc_se *se, gfc_expr *expr)
9218 {
9219 tree args[2];
9220
9221 gfc_conv_intrinsic_function_args (se, expr, args, 2);
9222 se->expr = build_call_expr_loc (input_location,
9223 gfor_fndecl_sc_kind, 2, args[0], args[1]);
9224 se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9225 }
9226
9227
9228 /* Generate code for SELECTED_INT_KIND (R) intrinsic function. */
9229
9230 static void
9231 gfc_conv_intrinsic_si_kind (gfc_se *se, gfc_expr *expr)
9232 {
9233 tree arg, type;
9234
9235 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9236
9237 /* The argument to SELECTED_INT_KIND is INTEGER(4). */
9238 type = gfc_get_int_type (4);
9239 arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9240
9241 /* Convert it to the required type. */
9242 type = gfc_typenode_for_spec (&expr->ts);
9243 se->expr = build_call_expr_loc (input_location,
9244 gfor_fndecl_si_kind, 1, arg);
9245 se->expr = fold_convert (type, se->expr);
9246 }
9247
9248
9249 /* Generate code for SELECTED_LOGICAL_KIND (BITS) intrinsic function. */
9250
9251 static void
9252 gfc_conv_intrinsic_sl_kind (gfc_se *se, gfc_expr *expr)
9253 {
9254 tree arg, type;
9255
9256 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9257
9258 /* The argument to SELECTED_LOGICAL_KIND is INTEGER(4). */
9259 type = gfc_get_int_type (4);
9260 arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9261
9262 /* Convert it to the required type. */
9263 type = gfc_typenode_for_spec (&expr->ts);
9264 se->expr = build_call_expr_loc (input_location,
9265 gfor_fndecl_sl_kind, 1, arg);
9266 se->expr = fold_convert (type, se->expr);
9267 }
9268
9269
9270 /* Generate code for SELECTED_REAL_KIND (P, R, RADIX) intrinsic function. */
9271
9272 static void
9273 gfc_conv_intrinsic_sr_kind (gfc_se *se, gfc_expr *expr)
9274 {
9275 gfc_actual_arglist *actual;
9276 tree type;
9277 gfc_se argse;
9278 vec<tree, va_gc> *args = NULL;
9279
9280 for (actual = expr->value.function.actual; actual; actual = actual->next)
9281 {
9282 gfc_init_se (&argse, se);
9283
9284 /* Pass a NULL pointer for an absent arg. */
9285 if (actual->expr == NULL)
9286 argse.expr = null_pointer_node;
9287 else
9288 {
9289 gfc_typespec ts;
9290 gfc_clear_ts (&ts);
9291
9292 if (actual->expr->ts.kind != gfc_c_int_kind)
9293 {
9294 /* The arguments to SELECTED_REAL_KIND are INTEGER(4). */
9295 ts.type = BT_INTEGER;
9296 ts.kind = gfc_c_int_kind;
9297 gfc_convert_type (actual->expr, &ts, 2);
9298 }
9299 gfc_conv_expr_reference (&argse, actual->expr);
9300 }
9301
9302 gfc_add_block_to_block (&se->pre, &argse.pre);
9303 gfc_add_block_to_block (&se->post, &argse.post);
9304 vec_safe_push (args, argse.expr);
9305 }
9306
9307 /* Convert it to the required type. */
9308 type = gfc_typenode_for_spec (&expr->ts);
9309 se->expr = build_call_expr_loc_vec (input_location,
9310 gfor_fndecl_sr_kind, args);
9311 se->expr = fold_convert (type, se->expr);
9312 }
9313
9314
9315 /* Generate code for TRIM (A) intrinsic function. */
9316
9317 static void
9318 gfc_conv_intrinsic_trim (gfc_se * se, gfc_expr * expr)
9319 {
9320 tree var;
9321 tree len;
9322 tree addr;
9323 tree tmp;
9324 tree cond;
9325 tree fndecl;
9326 tree function;
9327 tree *args;
9328 unsigned int num_args;
9329
9330 num_args = gfc_intrinsic_argument_list_length (expr) + 2;
9331 args = XALLOCAVEC (tree, num_args);
9332
9333 var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
9334 addr = gfc_build_addr_expr (ppvoid_type_node, var);
9335 len = gfc_create_var (gfc_charlen_type_node, "len");
9336
9337 gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
9338 args[0] = gfc_build_addr_expr (NULL_TREE, len);
9339 args[1] = addr;
9340
9341 if (expr->ts.kind == 1)
9342 function = gfor_fndecl_string_trim;
9343 else if (expr->ts.kind == 4)
9344 function = gfor_fndecl_string_trim_char4;
9345 else
9346 gcc_unreachable ();
9347
9348 fndecl = build_addr (function);
9349 tmp = build_call_array_loc (input_location,
9350 TREE_TYPE (TREE_TYPE (function)), fndecl,
9351 num_args, args);
9352 gfc_add_expr_to_block (&se->pre, tmp);
9353
9354 /* Free the temporary afterwards, if necessary. */
9355 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9356 len, build_int_cst (TREE_TYPE (len), 0));
9357 tmp = gfc_call_free (var);
9358 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
9359 gfc_add_expr_to_block (&se->post, tmp);
9360
9361 se->expr = var;
9362 se->string_length = len;
9363 }
9364
9365
9366 /* Generate code for REPEAT (STRING, NCOPIES) intrinsic function. */
9367
9368 static void
9369 gfc_conv_intrinsic_repeat (gfc_se * se, gfc_expr * expr)
9370 {
9371 tree args[3], ncopies, dest, dlen, src, slen, ncopies_type;
9372 tree type, cond, tmp, count, exit_label, n, max, largest;
9373 tree size;
9374 stmtblock_t block, body;
9375 int i;
9376
9377 /* We store in charsize the size of a character. */
9378 i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
9379 size = build_int_cst (sizetype, gfc_character_kinds[i].bit_size / 8);
9380
9381 /* Get the arguments. */
9382 gfc_conv_intrinsic_function_args (se, expr, args, 3);
9383 slen = fold_convert (sizetype, gfc_evaluate_now (args[0], &se->pre));
9384 src = args[1];
9385 ncopies = gfc_evaluate_now (args[2], &se->pre);
9386 ncopies_type = TREE_TYPE (ncopies);
9387
9388 /* Check that NCOPIES is not negative. */
9389 cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, ncopies,
9390 build_int_cst (ncopies_type, 0));
9391 gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9392 "Argument NCOPIES of REPEAT intrinsic is negative "
9393 "(its value is %ld)",
9394 fold_convert (long_integer_type_node, ncopies));
9395
9396 /* If the source length is zero, any non negative value of NCOPIES
9397 is valid, and nothing happens. */
9398 n = gfc_create_var (ncopies_type, "ncopies");
9399 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9400 size_zero_node);
9401 tmp = fold_build3_loc (input_location, COND_EXPR, ncopies_type, cond,
9402 build_int_cst (ncopies_type, 0), ncopies);
9403 gfc_add_modify (&se->pre, n, tmp);
9404 ncopies = n;
9405
9406 /* Check that ncopies is not too large: ncopies should be less than
9407 (or equal to) MAX / slen, where MAX is the maximal integer of
9408 the gfc_charlen_type_node type. If slen == 0, we need a special
9409 case to avoid the division by zero. */
9410 max = fold_build2_loc (input_location, TRUNC_DIV_EXPR, sizetype,
9411 fold_convert (sizetype,
9412 TYPE_MAX_VALUE (gfc_charlen_type_node)),
9413 slen);
9414 largest = TYPE_PRECISION (sizetype) > TYPE_PRECISION (ncopies_type)
9415 ? sizetype : ncopies_type;
9416 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9417 fold_convert (largest, ncopies),
9418 fold_convert (largest, max));
9419 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9420 size_zero_node);
9421 cond = fold_build3_loc (input_location, COND_EXPR, logical_type_node, tmp,
9422 logical_false_node, cond);
9423 gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9424 "Argument NCOPIES of REPEAT intrinsic is too large");
9425
9426 /* Compute the destination length. */
9427 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
9428 fold_convert (gfc_charlen_type_node, slen),
9429 fold_convert (gfc_charlen_type_node, ncopies));
9430 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
9431 dest = gfc_conv_string_tmp (se, build_pointer_type (type), dlen);
9432
9433 /* Generate the code to do the repeat operation:
9434 for (i = 0; i < ncopies; i++)
9435 memmove (dest + (i * slen * size), src, slen*size); */
9436 gfc_start_block (&block);
9437 count = gfc_create_var (sizetype, "count");
9438 gfc_add_modify (&block, count, size_zero_node);
9439 exit_label = gfc_build_label_decl (NULL_TREE);
9440
9441 /* Start the loop body. */
9442 gfc_start_block (&body);
9443
9444 /* Exit the loop if count >= ncopies. */
9445 cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, count,
9446 fold_convert (sizetype, ncopies));
9447 tmp = build1_v (GOTO_EXPR, exit_label);
9448 TREE_USED (exit_label) = 1;
9449 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9450 build_empty_stmt (input_location));
9451 gfc_add_expr_to_block (&body, tmp);
9452
9453 /* Call memmove (dest + (i*slen*size), src, slen*size). */
9454 tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, slen,
9455 count);
9456 tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, tmp,
9457 size);
9458 tmp = fold_build_pointer_plus_loc (input_location,
9459 fold_convert (pvoid_type_node, dest), tmp);
9460 tmp = build_call_expr_loc (input_location,
9461 builtin_decl_explicit (BUILT_IN_MEMMOVE),
9462 3, tmp, src,
9463 fold_build2_loc (input_location, MULT_EXPR,
9464 size_type_node, slen, size));
9465 gfc_add_expr_to_block (&body, tmp);
9466
9467 /* Increment count. */
9468 tmp = fold_build2_loc (input_location, PLUS_EXPR, sizetype,
9469 count, size_one_node);
9470 gfc_add_modify (&body, count, tmp);
9471
9472 /* Build the loop. */
9473 tmp = build1_v (LOOP_EXPR, gfc_finish_block (&body));
9474 gfc_add_expr_to_block (&block, tmp);
9475
9476 /* Add the exit label. */
9477 tmp = build1_v (LABEL_EXPR, exit_label);
9478 gfc_add_expr_to_block (&block, tmp);
9479
9480 /* Finish the block. */
9481 tmp = gfc_finish_block (&block);
9482 gfc_add_expr_to_block (&se->pre, tmp);
9483
9484 /* Set the result value. */
9485 se->expr = dest;
9486 se->string_length = dlen;
9487 }
9488
9489
9490 /* Generate code for the IARGC intrinsic. */
9491
9492 static void
9493 gfc_conv_intrinsic_iargc (gfc_se * se, gfc_expr * expr)
9494 {
9495 tree tmp;
9496 tree fndecl;
9497 tree type;
9498
9499 /* Call the library function. This always returns an INTEGER(4). */
9500 fndecl = gfor_fndecl_iargc;
9501 tmp = build_call_expr_loc (input_location,
9502 fndecl, 0);
9503
9504 /* Convert it to the required type. */
9505 type = gfc_typenode_for_spec (&expr->ts);
9506 tmp = fold_convert (type, tmp);
9507
9508 se->expr = tmp;
9509 }
9510
9511
9512 /* Generate code for the KILL intrinsic. */
9513
9514 static void
9515 conv_intrinsic_kill (gfc_se *se, gfc_expr *expr)
9516 {
9517 tree *args;
9518 tree int4_type_node = gfc_get_int_type (4);
9519 tree pid;
9520 tree sig;
9521 tree tmp;
9522 unsigned int num_args;
9523
9524 num_args = gfc_intrinsic_argument_list_length (expr);
9525 args = XALLOCAVEC (tree, num_args);
9526 gfc_conv_intrinsic_function_args (se, expr, args, num_args);
9527
9528 /* Convert PID to a INTEGER(4) entity. */
9529 pid = convert (int4_type_node, args[0]);
9530
9531 /* Convert SIG to a INTEGER(4) entity. */
9532 sig = convert (int4_type_node, args[1]);
9533
9534 tmp = build_call_expr_loc (input_location, gfor_fndecl_kill, 2, pid, sig);
9535
9536 se->expr = fold_convert (TREE_TYPE (args[0]), tmp);
9537 }
9538
9539
9540 static tree
9541 conv_intrinsic_kill_sub (gfc_code *code)
9542 {
9543 stmtblock_t block;
9544 gfc_se se, se_stat;
9545 tree int4_type_node = gfc_get_int_type (4);
9546 tree pid;
9547 tree sig;
9548 tree statp;
9549 tree tmp;
9550
9551 /* Make the function call. */
9552 gfc_init_block (&block);
9553 gfc_init_se (&se, NULL);
9554
9555 /* Convert PID to a INTEGER(4) entity. */
9556 gfc_conv_expr (&se, code->ext.actual->expr);
9557 gfc_add_block_to_block (&block, &se.pre);
9558 pid = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9559 gfc_add_block_to_block (&block, &se.post);
9560
9561 /* Convert SIG to a INTEGER(4) entity. */
9562 gfc_conv_expr (&se, code->ext.actual->next->expr);
9563 gfc_add_block_to_block (&block, &se.pre);
9564 sig = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9565 gfc_add_block_to_block (&block, &se.post);
9566
9567 /* Deal with an optional STATUS. */
9568 if (code->ext.actual->next->next->expr)
9569 {
9570 gfc_init_se (&se_stat, NULL);
9571 gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
9572 statp = gfc_create_var (gfc_get_int_type (4), "_statp");
9573 }
9574 else
9575 statp = NULL_TREE;
9576
9577 tmp = build_call_expr_loc (input_location, gfor_fndecl_kill_sub, 3, pid, sig,
9578 statp ? gfc_build_addr_expr (NULL_TREE, statp) : null_pointer_node);
9579
9580 gfc_add_expr_to_block (&block, tmp);
9581
9582 if (statp && statp != se_stat.expr)
9583 gfc_add_modify (&block, se_stat.expr,
9584 fold_convert (TREE_TYPE (se_stat.expr), statp));
9585
9586 return gfc_finish_block (&block);
9587 }
9588
9589
9590
9591 /* The loc intrinsic returns the address of its argument as
9592 gfc_index_integer_kind integer. */
9593
9594 static void
9595 gfc_conv_intrinsic_loc (gfc_se * se, gfc_expr * expr)
9596 {
9597 tree temp_var;
9598 gfc_expr *arg_expr;
9599
9600 gcc_assert (!se->ss);
9601
9602 arg_expr = expr->value.function.actual->expr;
9603 if (arg_expr->rank == 0)
9604 {
9605 if (arg_expr->ts.type == BT_CLASS)
9606 gfc_add_data_component (arg_expr);
9607 gfc_conv_expr_reference (se, arg_expr);
9608 }
9609 else if (gfc_is_simply_contiguous (arg_expr, false, false))
9610 gfc_conv_array_parameter (se, arg_expr, true, NULL, NULL, NULL);
9611 else
9612 {
9613 gfc_conv_expr_descriptor (se, arg_expr);
9614 se->expr = gfc_conv_descriptor_data_get (se->expr);
9615 }
9616 se->expr = convert (gfc_get_int_type (gfc_index_integer_kind), se->expr);
9617 se->expr = gfc_evaluate_now (se->expr, &se->pre);
9618
9619 /* Create a temporary variable for loc return value. Without this,
9620 we get an error an ICE in gcc/expr.cc(expand_expr_addr_expr_1). */
9621 temp_var = gfc_create_var (gfc_get_int_type (gfc_index_integer_kind), NULL);
9622 gfc_add_modify (&se->pre, temp_var, se->expr);
9623 se->expr = temp_var;
9624 }
9625
9626
9627 /* Specialized trim for f_c_string. */
9628
9629 static void
9630 conv_trim (gfc_se *tse, gfc_se *str)
9631 {
9632 tree cond, plen, pvar, tlen, ttmp, tvar;
9633
9634 tlen = gfc_create_var (gfc_charlen_type_node, "tlen");
9635 plen = gfc_build_addr_expr (NULL_TREE, tlen);
9636
9637 tvar = gfc_create_var (pchar_type_node, "tstr");
9638 pvar = gfc_build_addr_expr (ppvoid_type_node, tvar);
9639
9640 ttmp = build_call_expr_loc (input_location, gfor_fndecl_string_trim, 4,
9641 plen, pvar, str->string_length, str->expr);
9642
9643 gfc_add_expr_to_block (&tse->pre, ttmp);
9644
9645 /* Free the temporary afterwards, if necessary. */
9646 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9647 tlen, build_int_cst (TREE_TYPE (tlen), 0));
9648 ttmp = gfc_call_free (tvar);
9649 ttmp = build3_v (COND_EXPR, cond, ttmp, build_empty_stmt (input_location));
9650 gfc_add_expr_to_block (&tse->post, ttmp);
9651
9652 tse->expr = tvar;
9653 tse->string_length = tlen;
9654 }
9655
9656
9657 /* The following routine generates code for the intrinsic functions from
9658 the ISO_C_BINDING module: C_LOC, C_FUNLOC, C_ASSOCIATED, and
9659 F_C_STRING. */
9660
9661 static void
9662 conv_isocbinding_function (gfc_se *se, gfc_expr *expr)
9663 {
9664 gfc_actual_arglist *arg = expr->value.function.actual;
9665
9666 if (expr->value.function.isym->id == GFC_ISYM_C_LOC)
9667 {
9668 if (arg->expr->rank == 0)
9669 gfc_conv_expr_reference (se, arg->expr);
9670 else if (gfc_is_simply_contiguous (arg->expr, false, false))
9671 gfc_conv_array_parameter (se, arg->expr, true, NULL, NULL, NULL);
9672 else
9673 {
9674 gfc_conv_expr_descriptor (se, arg->expr);
9675 se->expr = gfc_conv_descriptor_data_get (se->expr);
9676 }
9677
9678 /* TODO -- the following two lines shouldn't be necessary, but if
9679 they're removed, a bug is exposed later in the code path.
9680 This workaround was thus introduced, but will have to be
9681 removed; please see PR 35150 for details about the issue. */
9682 se->expr = convert (pvoid_type_node, se->expr);
9683 se->expr = gfc_evaluate_now (se->expr, &se->pre);
9684 }
9685 else if (expr->value.function.isym->id == GFC_ISYM_C_FUNLOC)
9686 {
9687 gfc_conv_expr_reference (se, arg->expr);
9688 if (arg->expr->symtree->n.sym->attr.proc_pointer
9689 && arg->expr->symtree->n.sym->attr.dummy)
9690 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
9691 /* The code below is necessary to create a reference from the calling
9692 subprogram to the argument of C_FUNLOC() in the call graph.
9693 Please see PR 117303 for more details. */
9694 se->expr = convert (pvoid_type_node, se->expr);
9695 se->expr = gfc_evaluate_now (se->expr, &se->pre);
9696 }
9697 else if (expr->value.function.isym->id == GFC_ISYM_C_ASSOCIATED)
9698 {
9699 gfc_se arg1se;
9700 gfc_se arg2se;
9701
9702 /* Build the addr_expr for the first argument. The argument is
9703 already an *address* so we don't need to set want_pointer in
9704 the gfc_se. */
9705 gfc_init_se (&arg1se, NULL);
9706 gfc_conv_expr (&arg1se, arg->expr);
9707 gfc_add_block_to_block (&se->pre, &arg1se.pre);
9708 gfc_add_block_to_block (&se->post, &arg1se.post);
9709
9710 /* See if we were given two arguments. */
9711 if (arg->next->expr == NULL)
9712 /* Only given one arg so generate a null and do a
9713 not-equal comparison against the first arg. */
9714 se->expr = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9715 arg1se.expr,
9716 fold_convert (TREE_TYPE (arg1se.expr),
9717 null_pointer_node));
9718 else
9719 {
9720 tree eq_expr;
9721 tree not_null_expr;
9722
9723 /* Given two arguments so build the arg2se from second arg. */
9724 gfc_init_se (&arg2se, NULL);
9725 gfc_conv_expr (&arg2se, arg->next->expr);
9726 gfc_add_block_to_block (&se->pre, &arg2se.pre);
9727 gfc_add_block_to_block (&se->post, &arg2se.post);
9728
9729 /* Generate test to compare that the two args are equal. */
9730 eq_expr = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9731 arg1se.expr, arg2se.expr);
9732 /* Generate test to ensure that the first arg is not null. */
9733 not_null_expr = fold_build2_loc (input_location, NE_EXPR,
9734 logical_type_node,
9735 arg1se.expr, null_pointer_node);
9736
9737 /* Finally, the generated test must check that both arg1 is not
9738 NULL and that it is equal to the second arg. */
9739 se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9740 logical_type_node,
9741 not_null_expr, eq_expr);
9742 }
9743 }
9744 else if (expr->value.function.isym->id == GFC_ISYM_F_C_STRING)
9745 {
9746 /* There are three cases:
9747 f_c_string(string) -> trim(string) // c_null_char
9748 f_c_string(string, .false.) -> trim(string) // c_null_char
9749 f_c_string(string, .true.) -> string // c_null_char */
9750
9751 gfc_se lse, rse, tse;
9752 tree len, tmp, var;
9753 gfc_expr *string = arg->expr;
9754 gfc_expr *asis = arg->next->expr;
9755 gfc_expr *cnc;
9756
9757 /* Convert string. */
9758 gfc_init_se (&lse, se);
9759 gfc_conv_expr (&lse, string);
9760 gfc_conv_string_parameter (&lse);
9761
9762 /* Create a string for C_NULL_CHAR and convert it. */
9763 cnc = gfc_get_character_expr (gfc_default_character_kind,
9764 &string->where, "\0", 1);
9765 gfc_init_se (&rse, se);
9766 gfc_conv_expr (&rse, cnc);
9767 gfc_conv_string_parameter (&rse);
9768 gfc_free_expr (cnc);
9769
9770 #ifdef cnode
9771 #undef cnode
9772 #endif
9773 #define cnode gfc_charlen_type_node
9774 if (asis)
9775 {
9776 stmtblock_t block;
9777 gfc_se asis_se, vse;
9778 tree elen, evar, tlen, tvar;
9779 tree else_branch, then_branch;
9780
9781 elen = evar = tlen = tvar = NULL_TREE;
9782
9783 /* f_c_string(string, .true.) -> string // c_null_char */
9784
9785 gfc_init_block (&block);
9786
9787 gfc_add_block_to_block (&block, &lse.pre);
9788 gfc_add_block_to_block (&block, &rse.pre);
9789
9790 tlen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9791 fold_convert (cnode, lse.string_length),
9792 fold_convert (cnode, rse.string_length));
9793
9794 gfc_init_se (&vse, se);
9795 tvar = gfc_conv_string_tmp (&vse, pchar_type_node, tlen);
9796 gfc_add_block_to_block (&block, &vse.pre);
9797
9798 tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9799 6, tlen, tvar,
9800 lse.string_length, lse.expr,
9801 rse.string_length, rse.expr);
9802 gfc_add_expr_to_block (&block, tmp);
9803
9804 then_branch = gfc_finish_block (&block);
9805
9806 /* f_c_string(string, .false.) = trim(string) // c_null_char */
9807
9808 gfc_init_block (&block);
9809
9810 gfc_init_se (&tse, se);
9811 conv_trim (&tse, &lse);
9812 gfc_add_block_to_block (&block, &tse.pre);
9813 gfc_add_block_to_block (&block, &rse.pre);
9814
9815 elen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9816 fold_convert (cnode, tse.string_length),
9817 fold_convert (cnode, rse.string_length));
9818
9819 gfc_init_se (&vse, se);
9820 evar = gfc_conv_string_tmp (&vse, pchar_type_node, elen);
9821 gfc_add_block_to_block (&block, &vse.pre);
9822
9823 tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9824 6, elen, evar,
9825 tse.string_length, tse.expr,
9826 rse.string_length, rse.expr);
9827 gfc_add_expr_to_block (&block, tmp);
9828
9829 else_branch = gfc_finish_block (&block);
9830
9831 gfc_init_se (&asis_se, se);
9832 gfc_conv_expr (&asis_se, asis);
9833 if (asis->expr_type == EXPR_VARIABLE
9834 && asis->symtree->n.sym->attr.dummy
9835 && asis->symtree->n.sym->attr.optional)
9836 {
9837 tree present = gfc_conv_expr_present (asis->symtree->n.sym);
9838 asis_se.expr = build3_loc (input_location, COND_EXPR,
9839 logical_type_node, present,
9840 asis_se.expr,
9841 build_int_cst (logical_type_node, 0));
9842 }
9843 gfc_add_block_to_block (&se->pre, &asis_se.pre);
9844 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
9845 asis_se.expr, then_branch, else_branch);
9846
9847 gfc_add_expr_to_block (&se->pre, tmp);
9848
9849 var = fold_build3_loc (input_location, COND_EXPR, pchar_type_node,
9850 asis_se.expr, tvar, evar);
9851 gfc_add_expr_to_block (&se->pre, var);
9852
9853 len = fold_build3_loc (input_location, COND_EXPR, cnode,
9854 asis_se.expr, tlen, elen);
9855 gfc_add_expr_to_block (&se->pre, len);
9856 }
9857 else
9858 {
9859 /* f_c_string(string) = trim(string) // c_null_char */
9860
9861 gfc_add_block_to_block (&se->pre, &lse.pre);
9862 gfc_add_block_to_block (&se->pre, &rse.pre);
9863
9864 gfc_init_se (&tse, se);
9865 conv_trim (&tse, &lse);
9866 gfc_add_block_to_block (&se->pre, &tse.pre);
9867 gfc_add_block_to_block (&se->post, &tse.post);
9868
9869 len = fold_build2_loc (input_location, PLUS_EXPR, cnode,
9870 fold_convert (cnode, tse.string_length),
9871 fold_convert (cnode, rse.string_length));
9872
9873 var = gfc_conv_string_tmp (se, pchar_type_node, len);
9874
9875 tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
9876 6, len, var,
9877 tse.string_length, tse.expr,
9878 rse.string_length, rse.expr);
9879 gfc_add_expr_to_block (&se->pre, tmp);
9880 }
9881
9882 se->expr = var;
9883 se->string_length = len;
9884
9885 #undef cnode
9886 }
9887 else
9888 gcc_unreachable ();
9889 }
9890
9891
9892 /* The following routine generates code for the intrinsic
9893 subroutines from the ISO_C_BINDING module:
9894 * C_F_POINTER
9895 * C_F_PROCPOINTER. */
9896
9897 static tree
9898 conv_isocbinding_subroutine (gfc_code *code)
9899 {
9900 gfc_se se;
9901 gfc_se cptrse;
9902 gfc_se fptrse;
9903 gfc_se shapese;
9904 gfc_ss *shape_ss;
9905 tree desc, dim, tmp, stride, offset;
9906 stmtblock_t body, block;
9907 gfc_loopinfo loop;
9908 gfc_actual_arglist *arg = code->ext.actual;
9909
9910 gfc_init_se (&se, NULL);
9911 gfc_init_se (&cptrse, NULL);
9912 gfc_conv_expr (&cptrse, arg->expr);
9913 gfc_add_block_to_block (&se.pre, &cptrse.pre);
9914 gfc_add_block_to_block (&se.post, &cptrse.post);
9915
9916 gfc_init_se (&fptrse, NULL);
9917 if (arg->next->expr->rank == 0)
9918 {
9919 fptrse.want_pointer = 1;
9920 gfc_conv_expr (&fptrse, arg->next->expr);
9921 gfc_add_block_to_block (&se.pre, &fptrse.pre);
9922 gfc_add_block_to_block (&se.post, &fptrse.post);
9923 if (arg->next->expr->symtree->n.sym->attr.proc_pointer
9924 && arg->next->expr->symtree->n.sym->attr.dummy)
9925 fptrse.expr = build_fold_indirect_ref_loc (input_location,
9926 fptrse.expr);
9927 se.expr = fold_build2_loc (input_location, MODIFY_EXPR,
9928 TREE_TYPE (fptrse.expr),
9929 fptrse.expr,
9930 fold_convert (TREE_TYPE (fptrse.expr),
9931 cptrse.expr));
9932 gfc_add_expr_to_block (&se.pre, se.expr);
9933 gfc_add_block_to_block (&se.pre, &se.post);
9934 return gfc_finish_block (&se.pre);
9935 }
9936
9937 gfc_start_block (&block);
9938
9939 /* Get the descriptor of the Fortran pointer. */
9940 fptrse.descriptor_only = 1;
9941 gfc_conv_expr_descriptor (&fptrse, arg->next->expr);
9942 gfc_add_block_to_block (&block, &fptrse.pre);
9943 desc = fptrse.expr;
9944
9945 /* Set the span field. */
9946 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
9947 tmp = fold_convert (gfc_array_index_type, tmp);
9948 gfc_conv_descriptor_span_set (&block, desc, tmp);
9949
9950 /* Set data value, dtype, and offset. */
9951 tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc));
9952 gfc_conv_descriptor_data_set (&block, desc, fold_convert (tmp, cptrse.expr));
9953 gfc_add_modify (&block, gfc_conv_descriptor_dtype (desc),
9954 gfc_get_dtype (TREE_TYPE (desc)));
9955
9956 /* Start scalarization of the bounds, using the shape argument. */
9957
9958 shape_ss = gfc_walk_expr (arg->next->next->expr);
9959 gcc_assert (shape_ss != gfc_ss_terminator);
9960 gfc_init_se (&shapese, NULL);
9961
9962 gfc_init_loopinfo (&loop);
9963 gfc_add_ss_to_loop (&loop, shape_ss);
9964 gfc_conv_ss_startstride (&loop);
9965 gfc_conv_loop_setup (&loop, &arg->next->expr->where);
9966 gfc_mark_ss_chain_used (shape_ss, 1);
9967
9968 gfc_copy_loopinfo_to_se (&shapese, &loop);
9969 shapese.ss = shape_ss;
9970
9971 stride = gfc_create_var (gfc_array_index_type, "stride");
9972 offset = gfc_create_var (gfc_array_index_type, "offset");
9973 gfc_add_modify (&block, stride, gfc_index_one_node);
9974 gfc_add_modify (&block, offset, gfc_index_zero_node);
9975
9976 /* Loop body. */
9977 gfc_start_scalarized_body (&loop, &body);
9978
9979 dim = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9980 loop.loopvar[0], loop.from[0]);
9981
9982 /* Set bounds and stride. */
9983 gfc_conv_descriptor_lbound_set (&body, desc, dim, gfc_index_one_node);
9984 gfc_conv_descriptor_stride_set (&body, desc, dim, stride);
9985
9986 gfc_conv_expr (&shapese, arg->next->next->expr);
9987 gfc_add_block_to_block (&body, &shapese.pre);
9988 gfc_conv_descriptor_ubound_set (&body, desc, dim, shapese.expr);
9989 gfc_add_block_to_block (&body, &shapese.post);
9990
9991 /* Calculate offset. */
9992 gfc_add_modify (&body, offset,
9993 fold_build2_loc (input_location, PLUS_EXPR,
9994 gfc_array_index_type, offset, stride));
9995 /* Update stride. */
9996 gfc_add_modify (&body, stride,
9997 fold_build2_loc (input_location, MULT_EXPR,
9998 gfc_array_index_type, stride,
9999 fold_convert (gfc_array_index_type,
10000 shapese.expr)));
10001 /* Finish scalarization loop. */
10002 gfc_trans_scalarizing_loops (&loop, &body);
10003 gfc_add_block_to_block (&block, &loop.pre);
10004 gfc_add_block_to_block (&block, &loop.post);
10005 gfc_add_block_to_block (&block, &fptrse.post);
10006 gfc_cleanup_loop (&loop);
10007
10008 gfc_add_modify (&block, offset,
10009 fold_build1_loc (input_location, NEGATE_EXPR,
10010 gfc_array_index_type, offset));
10011 gfc_conv_descriptor_offset_set (&block, desc, offset);
10012
10013 gfc_add_expr_to_block (&se.pre, gfc_finish_block (&block));
10014 gfc_add_block_to_block (&se.pre, &se.post);
10015 return gfc_finish_block (&se.pre);
10016 }
10017
10018
10019 /* Save and restore floating-point state. */
10020
10021 tree
10022 gfc_save_fp_state (stmtblock_t *block)
10023 {
10024 tree type, fpstate, tmp;
10025
10026 type = build_array_type (char_type_node,
10027 build_range_type (size_type_node, size_zero_node,
10028 size_int (GFC_FPE_STATE_BUFFER_SIZE)));
10029 fpstate = gfc_create_var (type, "fpstate");
10030 fpstate = gfc_build_addr_expr (pvoid_type_node, fpstate);
10031
10032 tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_entry,
10033 1, fpstate);
10034 gfc_add_expr_to_block (block, tmp);
10035
10036 return fpstate;
10037 }
10038
10039
10040 void
10041 gfc_restore_fp_state (stmtblock_t *block, tree fpstate)
10042 {
10043 tree tmp;
10044
10045 tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_exit,
10046 1, fpstate);
10047 gfc_add_expr_to_block (block, tmp);
10048 }
10049
10050
10051 /* Generate code for arguments of IEEE functions. */
10052
10053 static void
10054 conv_ieee_function_args (gfc_se *se, gfc_expr *expr, tree *argarray,
10055 int nargs)
10056 {
10057 gfc_actual_arglist *actual;
10058 gfc_expr *e;
10059 gfc_se argse;
10060 int arg;
10061
10062 actual = expr->value.function.actual;
10063 for (arg = 0; arg < nargs; arg++, actual = actual->next)
10064 {
10065 gcc_assert (actual);
10066 e = actual->expr;
10067
10068 gfc_init_se (&argse, se);
10069 gfc_conv_expr_val (&argse, e);
10070
10071 gfc_add_block_to_block (&se->pre, &argse.pre);
10072 gfc_add_block_to_block (&se->post, &argse.post);
10073 argarray[arg] = argse.expr;
10074 }
10075 }
10076
10077
10078 /* Generate code for intrinsics IEEE_IS_NAN, IEEE_IS_FINITE
10079 and IEEE_UNORDERED, which translate directly to GCC type-generic
10080 built-ins. */
10081
10082 static void
10083 conv_intrinsic_ieee_builtin (gfc_se * se, gfc_expr * expr,
10084 enum built_in_function code, int nargs)
10085 {
10086 tree args[2];
10087 gcc_assert ((unsigned) nargs <= ARRAY_SIZE (args));
10088
10089 conv_ieee_function_args (se, expr, args, nargs);
10090 se->expr = build_call_expr_loc_array (input_location,
10091 builtin_decl_explicit (code),
10092 nargs, args);
10093 STRIP_TYPE_NOPS (se->expr);
10094 se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10095 }
10096
10097
10098 /* Generate code for intrinsics IEEE_SIGNBIT. */
10099
10100 static void
10101 conv_intrinsic_ieee_signbit (gfc_se * se, gfc_expr * expr)
10102 {
10103 tree arg, signbit;
10104
10105 conv_ieee_function_args (se, expr, &arg, 1);
10106 signbit = build_call_expr_loc (input_location,
10107 builtin_decl_explicit (BUILT_IN_SIGNBIT),
10108 1, arg);
10109 signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10110 signbit, integer_zero_node);
10111 se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), signbit);
10112 }
10113
10114
10115 /* Generate code for IEEE_IS_NORMAL intrinsic:
10116 IEEE_IS_NORMAL(x) --> (__builtin_isnormal(x) || x == 0) */
10117
10118 static void
10119 conv_intrinsic_ieee_is_normal (gfc_se * se, gfc_expr * expr)
10120 {
10121 tree arg, isnormal, iszero;
10122
10123 /* Convert arg, evaluate it only once. */
10124 conv_ieee_function_args (se, expr, &arg, 1);
10125 arg = gfc_evaluate_now (arg, &se->pre);
10126
10127 isnormal = build_call_expr_loc (input_location,
10128 builtin_decl_explicit (BUILT_IN_ISNORMAL),
10129 1, arg);
10130 iszero = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
10131 build_real_from_int_cst (TREE_TYPE (arg),
10132 integer_zero_node));
10133 se->expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
10134 logical_type_node, isnormal, iszero);
10135 se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10136 }
10137
10138
10139 /* Generate code for IEEE_IS_NEGATIVE intrinsic:
10140 IEEE_IS_NEGATIVE(x) --> (__builtin_signbit(x) && !__builtin_isnan(x)) */
10141
10142 static void
10143 conv_intrinsic_ieee_is_negative (gfc_se * se, gfc_expr * expr)
10144 {
10145 tree arg, signbit, isnan;
10146
10147 /* Convert arg, evaluate it only once. */
10148 conv_ieee_function_args (se, expr, &arg, 1);
10149 arg = gfc_evaluate_now (arg, &se->pre);
10150
10151 isnan = build_call_expr_loc (input_location,
10152 builtin_decl_explicit (BUILT_IN_ISNAN),
10153 1, arg);
10154 STRIP_TYPE_NOPS (isnan);
10155
10156 signbit = build_call_expr_loc (input_location,
10157 builtin_decl_explicit (BUILT_IN_SIGNBIT),
10158 1, arg);
10159 signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10160 signbit, integer_zero_node);
10161
10162 se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10163 logical_type_node, signbit,
10164 fold_build1_loc (input_location, TRUTH_NOT_EXPR,
10165 TREE_TYPE(isnan), isnan));
10166
10167 se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10168 }
10169
10170
10171 /* Generate code for IEEE_LOGB and IEEE_RINT. */
10172
10173 static void
10174 conv_intrinsic_ieee_logb_rint (gfc_se * se, gfc_expr * expr,
10175 enum built_in_function code)
10176 {
10177 tree arg, decl, call, fpstate;
10178 int argprec;
10179
10180 conv_ieee_function_args (se, expr, &arg, 1);
10181 argprec = TYPE_PRECISION (TREE_TYPE (arg));
10182 decl = builtin_decl_for_precision (code, argprec);
10183
10184 /* Save floating-point state. */
10185 fpstate = gfc_save_fp_state (&se->pre);
10186
10187 /* Make the function call. */
10188 call = build_call_expr_loc (input_location, decl, 1, arg);
10189 se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), call);
10190
10191 /* Restore floating-point state. */
10192 gfc_restore_fp_state (&se->post, fpstate);
10193 }
10194
10195
10196 /* Generate code for IEEE_REM. */
10197
10198 static void
10199 conv_intrinsic_ieee_rem (gfc_se * se, gfc_expr * expr)
10200 {
10201 tree args[2], decl, call, fpstate;
10202 int argprec;
10203
10204 conv_ieee_function_args (se, expr, args, 2);
10205
10206 /* If arguments have unequal size, convert them to the larger. */
10207 if (TYPE_PRECISION (TREE_TYPE (args[0]))
10208 > TYPE_PRECISION (TREE_TYPE (args[1])))
10209 args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10210 else if (TYPE_PRECISION (TREE_TYPE (args[1]))
10211 > TYPE_PRECISION (TREE_TYPE (args[0])))
10212 args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
10213
10214 argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10215 decl = builtin_decl_for_precision (BUILT_IN_REMAINDER, argprec);
10216
10217 /* Save floating-point state. */
10218 fpstate = gfc_save_fp_state (&se->pre);
10219
10220 /* Make the function call. */
10221 call = build_call_expr_loc_array (input_location, decl, 2, args);
10222 se->expr = fold_convert (TREE_TYPE (args[0]), call);
10223
10224 /* Restore floating-point state. */
10225 gfc_restore_fp_state (&se->post, fpstate);
10226 }
10227
10228
10229 /* Generate code for IEEE_NEXT_AFTER. */
10230
10231 static void
10232 conv_intrinsic_ieee_next_after (gfc_se * se, gfc_expr * expr)
10233 {
10234 tree args[2], decl, call, fpstate;
10235 int argprec;
10236
10237 conv_ieee_function_args (se, expr, args, 2);
10238
10239 /* Result has the characteristics of first argument. */
10240 args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10241 argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10242 decl = builtin_decl_for_precision (BUILT_IN_NEXTAFTER, argprec);
10243
10244 /* Save floating-point state. */
10245 fpstate = gfc_save_fp_state (&se->pre);
10246
10247 /* Make the function call. */
10248 call = build_call_expr_loc_array (input_location, decl, 2, args);
10249 se->expr = fold_convert (TREE_TYPE (args[0]), call);
10250
10251 /* Restore floating-point state. */
10252 gfc_restore_fp_state (&se->post, fpstate);
10253 }
10254
10255
10256 /* Generate code for IEEE_SCALB. */
10257
10258 static void
10259 conv_intrinsic_ieee_scalb (gfc_se * se, gfc_expr * expr)
10260 {
10261 tree args[2], decl, call, huge, type;
10262 int argprec, n;
10263
10264 conv_ieee_function_args (se, expr, args, 2);
10265
10266 /* Result has the characteristics of first argument. */
10267 argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10268 decl = builtin_decl_for_precision (BUILT_IN_SCALBN, argprec);
10269
10270 if (TYPE_PRECISION (TREE_TYPE (args[1])) > TYPE_PRECISION (integer_type_node))
10271 {
10272 /* We need to fold the integer into the range of a C int. */
10273 args[1] = gfc_evaluate_now (args[1], &se->pre);
10274 type = TREE_TYPE (args[1]);
10275
10276 n = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
10277 huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
10278 gfc_c_int_kind);
10279 huge = fold_convert (type, huge);
10280 args[1] = fold_build2_loc (input_location, MIN_EXPR, type, args[1],
10281 huge);
10282 args[1] = fold_build2_loc (input_location, MAX_EXPR, type, args[1],
10283 fold_build1_loc (input_location, NEGATE_EXPR,
10284 type, huge));
10285 }
10286
10287 args[1] = fold_convert (integer_type_node, args[1]);
10288
10289 /* Make the function call. */
10290 call = build_call_expr_loc_array (input_location, decl, 2, args);
10291 se->expr = fold_convert (TREE_TYPE (args[0]), call);
10292 }
10293
10294
10295 /* Generate code for IEEE_COPY_SIGN. */
10296
10297 static void
10298 conv_intrinsic_ieee_copy_sign (gfc_se * se, gfc_expr * expr)
10299 {
10300 tree args[2], decl, sign;
10301 int argprec;
10302
10303 conv_ieee_function_args (se, expr, args, 2);
10304
10305 /* Get the sign of the second argument. */
10306 sign = build_call_expr_loc (input_location,
10307 builtin_decl_explicit (BUILT_IN_SIGNBIT),
10308 1, args[1]);
10309 sign = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10310 sign, integer_zero_node);
10311
10312 /* Create a value of one, with the right sign. */
10313 sign = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
10314 sign,
10315 fold_build1_loc (input_location, NEGATE_EXPR,
10316 integer_type_node,
10317 integer_one_node),
10318 integer_one_node);
10319 args[1] = fold_convert (TREE_TYPE (args[0]), sign);
10320
10321 argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10322 decl = builtin_decl_for_precision (BUILT_IN_COPYSIGN, argprec);
10323
10324 se->expr = build_call_expr_loc_array (input_location, decl, 2, args);
10325 }
10326
10327
10328 /* Generate code for IEEE_CLASS. */
10329
10330 static void
10331 conv_intrinsic_ieee_class (gfc_se *se, gfc_expr *expr)
10332 {
10333 tree arg, c, t1, t2, t3, t4;
10334
10335 /* Convert arg, evaluate it only once. */
10336 conv_ieee_function_args (se, expr, &arg, 1);
10337 arg = gfc_evaluate_now (arg, &se->pre);
10338
10339 c = build_call_expr_loc (input_location,
10340 builtin_decl_explicit (BUILT_IN_FPCLASSIFY), 6,
10341 build_int_cst (integer_type_node, IEEE_QUIET_NAN),
10342 build_int_cst (integer_type_node,
10343 IEEE_POSITIVE_INF),
10344 build_int_cst (integer_type_node,
10345 IEEE_POSITIVE_NORMAL),
10346 build_int_cst (integer_type_node,
10347 IEEE_POSITIVE_DENORMAL),
10348 build_int_cst (integer_type_node,
10349 IEEE_POSITIVE_ZERO),
10350 arg);
10351 c = gfc_evaluate_now (c, &se->pre);
10352 t1 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10353 c, build_int_cst (integer_type_node,
10354 IEEE_QUIET_NAN));
10355 t2 = build_call_expr_loc (input_location,
10356 builtin_decl_explicit (BUILT_IN_ISSIGNALING), 1,
10357 arg);
10358 t2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10359 t2, build_zero_cst (TREE_TYPE (t2)));
10360 t1 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10361 logical_type_node, t1, t2);
10362 t3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10363 c, build_int_cst (integer_type_node,
10364 IEEE_POSITIVE_ZERO));
10365 t4 = build_call_expr_loc (input_location,
10366 builtin_decl_explicit (BUILT_IN_SIGNBIT), 1,
10367 arg);
10368 t4 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10369 t4, build_zero_cst (TREE_TYPE (t4)));
10370 t3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10371 logical_type_node, t3, t4);
10372 int s = IEEE_NEGATIVE_ZERO + IEEE_POSITIVE_ZERO;
10373 gcc_assert (IEEE_NEGATIVE_INF == s - IEEE_POSITIVE_INF);
10374 gcc_assert (IEEE_NEGATIVE_NORMAL == s - IEEE_POSITIVE_NORMAL);
10375 gcc_assert (IEEE_NEGATIVE_DENORMAL == s - IEEE_POSITIVE_DENORMAL);
10376 gcc_assert (IEEE_NEGATIVE_SUBNORMAL == s - IEEE_POSITIVE_SUBNORMAL);
10377 gcc_assert (IEEE_NEGATIVE_ZERO == s - IEEE_POSITIVE_ZERO);
10378 t4 = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (c),
10379 build_int_cst (TREE_TYPE (c), s), c);
10380 t3 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c),
10381 t3, t4, c);
10382 t1 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c), t1,
10383 build_int_cst (TREE_TYPE (c), IEEE_SIGNALING_NAN),
10384 t3);
10385 tree type = gfc_typenode_for_spec (&expr->ts);
10386 /* Perform a quick sanity check that the return type is
10387 IEEE_CLASS_TYPE derived type defined in
10388 libgfortran/ieee/ieee_arithmetic.F90
10389 Primarily check that it is a derived type with a single
10390 member in it. */
10391 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10392 tree field = NULL_TREE;
10393 for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10394 if (TREE_CODE (f) == FIELD_DECL)
10395 {
10396 gcc_assert (field == NULL_TREE);
10397 field = f;
10398 }
10399 gcc_assert (field);
10400 t1 = fold_convert (TREE_TYPE (field), t1);
10401 se->expr = build_constructor_single (type, field, t1);
10402 }
10403
10404
10405 /* Generate code for IEEE_VALUE. */
10406
10407 static void
10408 conv_intrinsic_ieee_value (gfc_se *se, gfc_expr *expr)
10409 {
10410 tree args[2], arg, ret, tmp;
10411 stmtblock_t body;
10412
10413 /* Convert args, evaluate the second one only once. */
10414 conv_ieee_function_args (se, expr, args, 2);
10415 arg = gfc_evaluate_now (args[1], &se->pre);
10416
10417 tree type = TREE_TYPE (arg);
10418 /* Perform a quick sanity check that the second argument's type is
10419 IEEE_CLASS_TYPE derived type defined in
10420 libgfortran/ieee/ieee_arithmetic.F90
10421 Primarily check that it is a derived type with a single
10422 member in it. */
10423 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10424 tree field = NULL_TREE;
10425 for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10426 if (TREE_CODE (f) == FIELD_DECL)
10427 {
10428 gcc_assert (field == NULL_TREE);
10429 field = f;
10430 }
10431 gcc_assert (field);
10432 arg = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
10433 arg, field, NULL_TREE);
10434 arg = gfc_evaluate_now (arg, &se->pre);
10435
10436 type = gfc_typenode_for_spec (&expr->ts);
10437 gcc_assert (SCALAR_FLOAT_TYPE_P (type));
10438 ret = gfc_create_var (type, NULL);
10439
10440 gfc_init_block (&body);
10441
10442 tree end_label = gfc_build_label_decl (NULL_TREE);
10443 for (int c = IEEE_SIGNALING_NAN; c <= IEEE_POSITIVE_INF; ++c)
10444 {
10445 tree label = gfc_build_label_decl (NULL_TREE);
10446 tree low = build_int_cst (TREE_TYPE (arg), c);
10447 tmp = build_case_label (low, low, label);
10448 gfc_add_expr_to_block (&body, tmp);
10449
10450 REAL_VALUE_TYPE real;
10451 int k;
10452 switch (c)
10453 {
10454 case IEEE_SIGNALING_NAN:
10455 real_nan (&real, "", 0, TYPE_MODE (type));
10456 break;
10457 case IEEE_QUIET_NAN:
10458 real_nan (&real, "", 1, TYPE_MODE (type));
10459 break;
10460 case IEEE_NEGATIVE_INF:
10461 real_inf (&real);
10462 real = real_value_negate (&real);
10463 break;
10464 case IEEE_NEGATIVE_NORMAL:
10465 real_from_integer (&real, TYPE_MODE (type), -42, SIGNED);
10466 break;
10467 case IEEE_NEGATIVE_DENORMAL:
10468 k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10469 real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10470 type, GFC_RND_MODE);
10471 real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10472 real = real_value_negate (&real);
10473 break;
10474 case IEEE_NEGATIVE_ZERO:
10475 real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10476 real = real_value_negate (&real);
10477 break;
10478 case IEEE_POSITIVE_ZERO:
10479 /* Make this also the default: label. The other possibility
10480 would be to add a separate default: label followed by
10481 __builtin_unreachable (). */
10482 label = gfc_build_label_decl (NULL_TREE);
10483 tmp = build_case_label (NULL_TREE, NULL_TREE, label);
10484 gfc_add_expr_to_block (&body, tmp);
10485 real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10486 break;
10487 case IEEE_POSITIVE_DENORMAL:
10488 k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10489 real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10490 type, GFC_RND_MODE);
10491 real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10492 break;
10493 case IEEE_POSITIVE_NORMAL:
10494 real_from_integer (&real, TYPE_MODE (type), 42, SIGNED);
10495 break;
10496 case IEEE_POSITIVE_INF:
10497 real_inf (&real);
10498 break;
10499 default:
10500 gcc_unreachable ();
10501 }
10502
10503 tree val = build_real (type, real);
10504 gfc_add_modify (&body, ret, val);
10505
10506 tmp = build1_v (GOTO_EXPR, end_label);
10507 gfc_add_expr_to_block (&body, tmp);
10508 }
10509
10510 tmp = gfc_finish_block (&body);
10511 tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, arg, tmp);
10512 gfc_add_expr_to_block (&se->pre, tmp);
10513
10514 tmp = build1_v (LABEL_EXPR, end_label);
10515 gfc_add_expr_to_block (&se->pre, tmp);
10516
10517 se->expr = ret;
10518 }
10519
10520
10521 /* Generate code for IEEE_FMA. */
10522
10523 static void
10524 conv_intrinsic_ieee_fma (gfc_se * se, gfc_expr * expr)
10525 {
10526 tree args[3], decl, call;
10527 int argprec;
10528
10529 conv_ieee_function_args (se, expr, args, 3);
10530
10531 /* All three arguments should have the same type. */
10532 gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10533 gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[2])));
10534
10535 /* Call the type-generic FMA built-in. */
10536 argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10537 decl = builtin_decl_for_precision (BUILT_IN_FMA, argprec);
10538 call = build_call_expr_loc_array (input_location, decl, 3, args);
10539
10540 /* Convert to the final type. */
10541 se->expr = fold_convert (TREE_TYPE (args[0]), call);
10542 }
10543
10544
10545 /* Generate code for IEEE_{MIN,MAX}_NUM{,_MAG}. */
10546
10547 static void
10548 conv_intrinsic_ieee_minmax (gfc_se * se, gfc_expr * expr, int max,
10549 const char *name)
10550 {
10551 tree args[2], func;
10552 built_in_function fn;
10553
10554 conv_ieee_function_args (se, expr, args, 2);
10555 gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10556 args[0] = gfc_evaluate_now (args[0], &se->pre);
10557 args[1] = gfc_evaluate_now (args[1], &se->pre);
10558
10559 if (startswith (name, "mag"))
10560 {
10561 /* IEEE_MIN_NUM_MAG and IEEE_MAX_NUM_MAG translate to C functions
10562 fminmag() and fmaxmag(), which do not exist as built-ins.
10563
10564 Following glibc, we emit this:
10565
10566 fminmag (x, y) {
10567 ax = ABS (x);
10568 ay = ABS (y);
10569 if (isless (ax, ay))
10570 return x;
10571 else if (isgreater (ax, ay))
10572 return y;
10573 else if (ax == ay)
10574 return x < y ? x : y;
10575 else if (issignaling (x) || issignaling (y))
10576 return x + y;
10577 else
10578 return isnan (y) ? x : y;
10579 }
10580
10581 fmaxmag (x, y) {
10582 ax = ABS (x);
10583 ay = ABS (y);
10584 if (isgreater (ax, ay))
10585 return x;
10586 else if (isless (ax, ay))
10587 return y;
10588 else if (ax == ay)
10589 return x > y ? x : y;
10590 else if (issignaling (x) || issignaling (y))
10591 return x + y;
10592 else
10593 return isnan (y) ? x : y;
10594 }
10595
10596 */
10597
10598 tree abs0, abs1, sig0, sig1;
10599 tree cond1, cond2, cond3, cond4, cond5;
10600 tree res;
10601 tree type = TREE_TYPE (args[0]);
10602
10603 func = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
10604 abs0 = build_call_expr_loc (input_location, func, 1, args[0]);
10605 abs1 = build_call_expr_loc (input_location, func, 1, args[1]);
10606 abs0 = gfc_evaluate_now (abs0, &se->pre);
10607 abs1 = gfc_evaluate_now (abs1, &se->pre);
10608
10609 cond5 = build_call_expr_loc (input_location,
10610 builtin_decl_explicit (BUILT_IN_ISNAN),
10611 1, args[1]);
10612 res = fold_build3_loc (input_location, COND_EXPR, type, cond5,
10613 args[0], args[1]);
10614
10615 sig0 = build_call_expr_loc (input_location,
10616 builtin_decl_explicit (BUILT_IN_ISSIGNALING),
10617 1, args[0]);
10618 sig1 = build_call_expr_loc (input_location,
10619 builtin_decl_explicit (BUILT_IN_ISSIGNALING),
10620 1, args[1]);
10621 cond4 = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
10622 logical_type_node, sig0, sig1);
10623 res = fold_build3_loc (input_location, COND_EXPR, type, cond4,
10624 fold_build2_loc (input_location, PLUS_EXPR,
10625 type, args[0], args[1]),
10626 res);
10627
10628 cond3 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10629 abs0, abs1);
10630 res = fold_build3_loc (input_location, COND_EXPR, type, cond3,
10631 fold_build2_loc (input_location,
10632 max ? MAX_EXPR : MIN_EXPR,
10633 type, args[0], args[1]),
10634 res);
10635
10636 func = builtin_decl_explicit (max ? BUILT_IN_ISLESS : BUILT_IN_ISGREATER);
10637 cond2 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
10638 res = fold_build3_loc (input_location, COND_EXPR, type, cond2,
10639 args[1], res);
10640
10641 func = builtin_decl_explicit (max ? BUILT_IN_ISGREATER : BUILT_IN_ISLESS);
10642 cond1 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
10643 res = fold_build3_loc (input_location, COND_EXPR, type, cond1,
10644 args[0], res);
10645
10646 se->expr = res;
10647 }
10648 else
10649 {
10650 /* IEEE_MIN_NUM and IEEE_MAX_NUM translate to fmin() and fmax(). */
10651 fn = max ? BUILT_IN_FMAX : BUILT_IN_FMIN;
10652 func = gfc_builtin_decl_for_float_kind (fn, expr->ts.kind);
10653 se->expr = build_call_expr_loc_array (input_location, func, 2, args);
10654 }
10655 }
10656
10657
10658 /* Generate code for comparison functions IEEE_QUIET_* and
10659 IEEE_SIGNALING_*. */
10660
10661 static void
10662 conv_intrinsic_ieee_comparison (gfc_se * se, gfc_expr * expr, int signaling,
10663 const char *name)
10664 {
10665 tree args[2];
10666 tree arg1, arg2, res;
10667
10668 /* Evaluate arguments only once. */
10669 conv_ieee_function_args (se, expr, args, 2);
10670 arg1 = gfc_evaluate_now (args[0], &se->pre);
10671 arg2 = gfc_evaluate_now (args[1], &se->pre);
10672
10673 if (startswith (name, "eq"))
10674 {
10675 if (signaling)
10676 res = build_call_expr_loc (input_location,
10677 builtin_decl_explicit (BUILT_IN_ISEQSIG),
10678 2, arg1, arg2);
10679 else
10680 res = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10681 arg1, arg2);
10682 }
10683 else if (startswith (name, "ne"))
10684 {
10685 if (signaling)
10686 {
10687 res = build_call_expr_loc (input_location,
10688 builtin_decl_explicit (BUILT_IN_ISEQSIG),
10689 2, arg1, arg2);
10690 res = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
10691 logical_type_node, res);
10692 }
10693 else
10694 res = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10695 arg1, arg2);
10696 }
10697 else if (startswith (name, "ge"))
10698 {
10699 if (signaling)
10700 res = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10701 arg1, arg2);
10702 else
10703 res = build_call_expr_loc (input_location,
10704 builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL),
10705 2, arg1, arg2);
10706 }
10707 else if (startswith (name, "gt"))
10708 {
10709 if (signaling)
10710 res = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
10711 arg1, arg2);
10712 else
10713 res = build_call_expr_loc (input_location,
10714 builtin_decl_explicit (BUILT_IN_ISGREATER),
10715 2, arg1, arg2);
10716 }
10717 else if (startswith (name, "le"))
10718 {
10719 if (signaling)
10720 res = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
10721 arg1, arg2);
10722 else
10723 res = build_call_expr_loc (input_location,
10724 builtin_decl_explicit (BUILT_IN_ISLESSEQUAL),
10725 2, arg1, arg2);
10726 }
10727 else if (startswith (name, "lt"))
10728 {
10729 if (signaling)
10730 res = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
10731 arg1, arg2);
10732 else
10733 res = build_call_expr_loc (input_location,
10734 builtin_decl_explicit (BUILT_IN_ISLESS),
10735 2, arg1, arg2);
10736 }
10737 else
10738 gcc_unreachable ();
10739
10740 se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), res);
10741 }
10742
10743
10744 /* Generate code for an intrinsic function from the IEEE_ARITHMETIC
10745 module. */
10746
10747 bool
10748 gfc_conv_ieee_arithmetic_function (gfc_se * se, gfc_expr * expr)
10749 {
10750 const char *name = expr->value.function.name;
10751
10752 if (startswith (name, "_gfortran_ieee_is_nan"))
10753 conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISNAN, 1);
10754 else if (startswith (name, "_gfortran_ieee_is_finite"))
10755 conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISFINITE, 1);
10756 else if (startswith (name, "_gfortran_ieee_unordered"))
10757 conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISUNORDERED, 2);
10758 else if (startswith (name, "_gfortran_ieee_signbit"))
10759 conv_intrinsic_ieee_signbit (se, expr);
10760 else if (startswith (name, "_gfortran_ieee_is_normal"))
10761 conv_intrinsic_ieee_is_normal (se, expr);
10762 else if (startswith (name, "_gfortran_ieee_is_negative"))
10763 conv_intrinsic_ieee_is_negative (se, expr);
10764 else if (startswith (name, "_gfortran_ieee_copy_sign"))
10765 conv_intrinsic_ieee_copy_sign (se, expr);
10766 else if (startswith (name, "_gfortran_ieee_scalb"))
10767 conv_intrinsic_ieee_scalb (se, expr);
10768 else if (startswith (name, "_gfortran_ieee_next_after"))
10769 conv_intrinsic_ieee_next_after (se, expr);
10770 else if (startswith (name, "_gfortran_ieee_rem"))
10771 conv_intrinsic_ieee_rem (se, expr);
10772 else if (startswith (name, "_gfortran_ieee_logb"))
10773 conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_LOGB);
10774 else if (startswith (name, "_gfortran_ieee_rint"))
10775 conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_RINT);
10776 else if (startswith (name, "ieee_class_") && ISDIGIT (name[11]))
10777 conv_intrinsic_ieee_class (se, expr);
10778 else if (startswith (name, "ieee_value_") && ISDIGIT (name[11]))
10779 conv_intrinsic_ieee_value (se, expr);
10780 else if (startswith (name, "_gfortran_ieee_fma"))
10781 conv_intrinsic_ieee_fma (se, expr);
10782 else if (startswith (name, "_gfortran_ieee_min_num_"))
10783 conv_intrinsic_ieee_minmax (se, expr, 0, name + 23);
10784 else if (startswith (name, "_gfortran_ieee_max_num_"))
10785 conv_intrinsic_ieee_minmax (se, expr, 1, name + 23);
10786 else if (startswith (name, "_gfortran_ieee_quiet_"))
10787 conv_intrinsic_ieee_comparison (se, expr, 0, name + 21);
10788 else if (startswith (name, "_gfortran_ieee_signaling_"))
10789 conv_intrinsic_ieee_comparison (se, expr, 1, name + 25);
10790 else
10791 /* It is not among the functions we translate directly. We return
10792 false, so a library function call is emitted. */
10793 return false;
10794
10795 return true;
10796 }
10797
10798
10799 /* Generate a direct call to malloc() for the MALLOC intrinsic. */
10800
10801 static void
10802 gfc_conv_intrinsic_malloc (gfc_se * se, gfc_expr * expr)
10803 {
10804 tree arg, res, restype;
10805
10806 gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
10807 arg = fold_convert (size_type_node, arg);
10808 res = build_call_expr_loc (input_location,
10809 builtin_decl_explicit (BUILT_IN_MALLOC), 1, arg);
10810 restype = gfc_typenode_for_spec (&expr->ts);
10811 se->expr = fold_convert (restype, res);
10812 }
10813
10814
10815 /* Generate code for an intrinsic function. Some map directly to library
10816 calls, others get special handling. In some cases the name of the function
10817 used depends on the type specifiers. */
10818
10819 void
10820 gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
10821 {
10822 const char *name;
10823 int lib, kind;
10824 tree fndecl;
10825
10826 name = &expr->value.function.name[2];
10827
10828 if (expr->rank > 0)
10829 {
10830 lib = gfc_is_intrinsic_libcall (expr);
10831 if (lib != 0)
10832 {
10833 if (lib == 1)
10834 se->ignore_optional = 1;
10835
10836 switch (expr->value.function.isym->id)
10837 {
10838 case GFC_ISYM_EOSHIFT:
10839 case GFC_ISYM_PACK:
10840 case GFC_ISYM_RESHAPE:
10841 case GFC_ISYM_REDUCE:
10842 /* For all of those the first argument specifies the type and the
10843 third is optional. */
10844 conv_generic_with_optional_char_arg (se, expr, 1, 3);
10845 break;
10846
10847 case GFC_ISYM_FINDLOC:
10848 gfc_conv_intrinsic_findloc (se, expr);
10849 break;
10850
10851 case GFC_ISYM_MINLOC:
10852 gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
10853 break;
10854
10855 case GFC_ISYM_MAXLOC:
10856 gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
10857 break;
10858
10859 default:
10860 gfc_conv_intrinsic_funcall (se, expr);
10861 break;
10862 }
10863
10864 return;
10865 }
10866 }
10867
10868 switch (expr->value.function.isym->id)
10869 {
10870 case GFC_ISYM_NONE:
10871 gcc_unreachable ();
10872
10873 case GFC_ISYM_REPEAT:
10874 gfc_conv_intrinsic_repeat (se, expr);
10875 break;
10876
10877 case GFC_ISYM_TRIM:
10878 gfc_conv_intrinsic_trim (se, expr);
10879 break;
10880
10881 case GFC_ISYM_SC_KIND:
10882 gfc_conv_intrinsic_sc_kind (se, expr);
10883 break;
10884
10885 case GFC_ISYM_SI_KIND:
10886 gfc_conv_intrinsic_si_kind (se, expr);
10887 break;
10888
10889 case GFC_ISYM_SL_KIND:
10890 gfc_conv_intrinsic_sl_kind (se, expr);
10891 break;
10892
10893 case GFC_ISYM_SR_KIND:
10894 gfc_conv_intrinsic_sr_kind (se, expr);
10895 break;
10896
10897 case GFC_ISYM_EXPONENT:
10898 gfc_conv_intrinsic_exponent (se, expr);
10899 break;
10900
10901 case GFC_ISYM_SCAN:
10902 kind = expr->value.function.actual->expr->ts.kind;
10903 if (kind == 1)
10904 fndecl = gfor_fndecl_string_scan;
10905 else if (kind == 4)
10906 fndecl = gfor_fndecl_string_scan_char4;
10907 else
10908 gcc_unreachable ();
10909
10910 gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
10911 break;
10912
10913 case GFC_ISYM_VERIFY:
10914 kind = expr->value.function.actual->expr->ts.kind;
10915 if (kind == 1)
10916 fndecl = gfor_fndecl_string_verify;
10917 else if (kind == 4)
10918 fndecl = gfor_fndecl_string_verify_char4;
10919 else
10920 gcc_unreachable ();
10921
10922 gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
10923 break;
10924
10925 case GFC_ISYM_ALLOCATED:
10926 gfc_conv_allocated (se, expr);
10927 break;
10928
10929 case GFC_ISYM_ASSOCIATED:
10930 gfc_conv_associated(se, expr);
10931 break;
10932
10933 case GFC_ISYM_SAME_TYPE_AS:
10934 gfc_conv_same_type_as (se, expr);
10935 break;
10936
10937 case GFC_ISYM_ABS:
10938 gfc_conv_intrinsic_abs (se, expr);
10939 break;
10940
10941 case GFC_ISYM_ADJUSTL:
10942 if (expr->ts.kind == 1)
10943 fndecl = gfor_fndecl_adjustl;
10944 else if (expr->ts.kind == 4)
10945 fndecl = gfor_fndecl_adjustl_char4;
10946 else
10947 gcc_unreachable ();
10948
10949 gfc_conv_intrinsic_adjust (se, expr, fndecl);
10950 break;
10951
10952 case GFC_ISYM_ADJUSTR:
10953 if (expr->ts.kind == 1)
10954 fndecl = gfor_fndecl_adjustr;
10955 else if (expr->ts.kind == 4)
10956 fndecl = gfor_fndecl_adjustr_char4;
10957 else
10958 gcc_unreachable ();
10959
10960 gfc_conv_intrinsic_adjust (se, expr, fndecl);
10961 break;
10962
10963 case GFC_ISYM_AIMAG:
10964 gfc_conv_intrinsic_imagpart (se, expr);
10965 break;
10966
10967 case GFC_ISYM_AINT:
10968 gfc_conv_intrinsic_aint (se, expr, RND_TRUNC);
10969 break;
10970
10971 case GFC_ISYM_ALL:
10972 gfc_conv_intrinsic_anyall (se, expr, EQ_EXPR);
10973 break;
10974
10975 case GFC_ISYM_ANINT:
10976 gfc_conv_intrinsic_aint (se, expr, RND_ROUND);
10977 break;
10978
10979 case GFC_ISYM_AND:
10980 gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
10981 break;
10982
10983 case GFC_ISYM_ANY:
10984 gfc_conv_intrinsic_anyall (se, expr, NE_EXPR);
10985 break;
10986
10987 case GFC_ISYM_ACOSD:
10988 case GFC_ISYM_ASIND:
10989 case GFC_ISYM_ATAND:
10990 gfc_conv_intrinsic_atrigd (se, expr, expr->value.function.isym->id);
10991 break;
10992
10993 case GFC_ISYM_COTAN:
10994 gfc_conv_intrinsic_cotan (se, expr);
10995 break;
10996
10997 case GFC_ISYM_COTAND:
10998 gfc_conv_intrinsic_cotand (se, expr);
10999 break;
11000
11001 case GFC_ISYM_ATAN2D:
11002 gfc_conv_intrinsic_atan2d (se, expr);
11003 break;
11004
11005 case GFC_ISYM_BTEST:
11006 gfc_conv_intrinsic_btest (se, expr);
11007 break;
11008
11009 case GFC_ISYM_BGE:
11010 gfc_conv_intrinsic_bitcomp (se, expr, GE_EXPR);
11011 break;
11012
11013 case GFC_ISYM_BGT:
11014 gfc_conv_intrinsic_bitcomp (se, expr, GT_EXPR);
11015 break;
11016
11017 case GFC_ISYM_BLE:
11018 gfc_conv_intrinsic_bitcomp (se, expr, LE_EXPR);
11019 break;
11020
11021 case GFC_ISYM_BLT:
11022 gfc_conv_intrinsic_bitcomp (se, expr, LT_EXPR);
11023 break;
11024
11025 case GFC_ISYM_C_ASSOCIATED:
11026 case GFC_ISYM_C_FUNLOC:
11027 case GFC_ISYM_C_LOC:
11028 case GFC_ISYM_F_C_STRING:
11029 conv_isocbinding_function (se, expr);
11030 break;
11031
11032 case GFC_ISYM_ACHAR:
11033 case GFC_ISYM_CHAR:
11034 gfc_conv_intrinsic_char (se, expr);
11035 break;
11036
11037 case GFC_ISYM_CONVERSION:
11038 case GFC_ISYM_DBLE:
11039 case GFC_ISYM_DFLOAT:
11040 case GFC_ISYM_FLOAT:
11041 case GFC_ISYM_LOGICAL:
11042 case GFC_ISYM_REAL:
11043 case GFC_ISYM_REALPART:
11044 case GFC_ISYM_SNGL:
11045 gfc_conv_intrinsic_conversion (se, expr);
11046 break;
11047
11048 /* Integer conversions are handled separately to make sure we get the
11049 correct rounding mode. */
11050 case GFC_ISYM_INT:
11051 case GFC_ISYM_INT2:
11052 case GFC_ISYM_INT8:
11053 case GFC_ISYM_LONG:
11054 case GFC_ISYM_UINT:
11055 gfc_conv_intrinsic_int (se, expr, RND_TRUNC);
11056 break;
11057
11058 case GFC_ISYM_NINT:
11059 gfc_conv_intrinsic_int (se, expr, RND_ROUND);
11060 break;
11061
11062 case GFC_ISYM_CEILING:
11063 gfc_conv_intrinsic_int (se, expr, RND_CEIL);
11064 break;
11065
11066 case GFC_ISYM_FLOOR:
11067 gfc_conv_intrinsic_int (se, expr, RND_FLOOR);
11068 break;
11069
11070 case GFC_ISYM_MOD:
11071 gfc_conv_intrinsic_mod (se, expr, 0);
11072 break;
11073
11074 case GFC_ISYM_MODULO:
11075 gfc_conv_intrinsic_mod (se, expr, 1);
11076 break;
11077
11078 case GFC_ISYM_CAF_GET:
11079 gfc_conv_intrinsic_caf_get (se, expr, NULL_TREE, false, NULL);
11080 break;
11081
11082 case GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE:
11083 gfc_conv_intrinsic_caf_is_present_remote (se, expr);
11084 break;
11085
11086 case GFC_ISYM_CMPLX:
11087 gfc_conv_intrinsic_cmplx (se, expr, name[5] == '1');
11088 break;
11089
11090 case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
11091 gfc_conv_intrinsic_iargc (se, expr);
11092 break;
11093
11094 case GFC_ISYM_COMPLEX:
11095 gfc_conv_intrinsic_cmplx (se, expr, 1);
11096 break;
11097
11098 case GFC_ISYM_CONJG:
11099 gfc_conv_intrinsic_conjg (se, expr);
11100 break;
11101
11102 case GFC_ISYM_COUNT:
11103 gfc_conv_intrinsic_count (se, expr);
11104 break;
11105
11106 case GFC_ISYM_CTIME:
11107 gfc_conv_intrinsic_ctime (se, expr);
11108 break;
11109
11110 case GFC_ISYM_DIM:
11111 gfc_conv_intrinsic_dim (se, expr);
11112 break;
11113
11114 case GFC_ISYM_DOT_PRODUCT:
11115 gfc_conv_intrinsic_dot_product (se, expr);
11116 break;
11117
11118 case GFC_ISYM_DPROD:
11119 gfc_conv_intrinsic_dprod (se, expr);
11120 break;
11121
11122 case GFC_ISYM_DSHIFTL:
11123 gfc_conv_intrinsic_dshift (se, expr, true);
11124 break;
11125
11126 case GFC_ISYM_DSHIFTR:
11127 gfc_conv_intrinsic_dshift (se, expr, false);
11128 break;
11129
11130 case GFC_ISYM_FDATE:
11131 gfc_conv_intrinsic_fdate (se, expr);
11132 break;
11133
11134 case GFC_ISYM_FRACTION:
11135 gfc_conv_intrinsic_fraction (se, expr);
11136 break;
11137
11138 case GFC_ISYM_IALL:
11139 gfc_conv_intrinsic_arith (se, expr, BIT_AND_EXPR, false);
11140 break;
11141
11142 case GFC_ISYM_IAND:
11143 gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
11144 break;
11145
11146 case GFC_ISYM_IANY:
11147 gfc_conv_intrinsic_arith (se, expr, BIT_IOR_EXPR, false);
11148 break;
11149
11150 case GFC_ISYM_IBCLR:
11151 gfc_conv_intrinsic_singlebitop (se, expr, 0);
11152 break;
11153
11154 case GFC_ISYM_IBITS:
11155 gfc_conv_intrinsic_ibits (se, expr);
11156 break;
11157
11158 case GFC_ISYM_IBSET:
11159 gfc_conv_intrinsic_singlebitop (se, expr, 1);
11160 break;
11161
11162 case GFC_ISYM_IACHAR:
11163 case GFC_ISYM_ICHAR:
11164 /* We assume ASCII character sequence. */
11165 gfc_conv_intrinsic_ichar (se, expr);
11166 break;
11167
11168 case GFC_ISYM_IARGC:
11169 gfc_conv_intrinsic_iargc (se, expr);
11170 break;
11171
11172 case GFC_ISYM_IEOR:
11173 gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11174 break;
11175
11176 case GFC_ISYM_INDEX:
11177 kind = expr->value.function.actual->expr->ts.kind;
11178 if (kind == 1)
11179 fndecl = gfor_fndecl_string_index;
11180 else if (kind == 4)
11181 fndecl = gfor_fndecl_string_index_char4;
11182 else
11183 gcc_unreachable ();
11184
11185 gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
11186 break;
11187
11188 case GFC_ISYM_IOR:
11189 gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11190 break;
11191
11192 case GFC_ISYM_IPARITY:
11193 gfc_conv_intrinsic_arith (se, expr, BIT_XOR_EXPR, false);
11194 break;
11195
11196 case GFC_ISYM_IS_IOSTAT_END:
11197 gfc_conv_has_intvalue (se, expr, LIBERROR_END);
11198 break;
11199
11200 case GFC_ISYM_IS_IOSTAT_EOR:
11201 gfc_conv_has_intvalue (se, expr, LIBERROR_EOR);
11202 break;
11203
11204 case GFC_ISYM_IS_CONTIGUOUS:
11205 gfc_conv_intrinsic_is_contiguous (se, expr);
11206 break;
11207
11208 case GFC_ISYM_ISNAN:
11209 gfc_conv_intrinsic_isnan (se, expr);
11210 break;
11211
11212 case GFC_ISYM_KILL:
11213 conv_intrinsic_kill (se, expr);
11214 break;
11215
11216 case GFC_ISYM_LSHIFT:
11217 gfc_conv_intrinsic_shift (se, expr, false, false);
11218 break;
11219
11220 case GFC_ISYM_RSHIFT:
11221 gfc_conv_intrinsic_shift (se, expr, true, true);
11222 break;
11223
11224 case GFC_ISYM_SHIFTA:
11225 gfc_conv_intrinsic_shift (se, expr, true, true);
11226 break;
11227
11228 case GFC_ISYM_SHIFTL:
11229 gfc_conv_intrinsic_shift (se, expr, false, false);
11230 break;
11231
11232 case GFC_ISYM_SHIFTR:
11233 gfc_conv_intrinsic_shift (se, expr, true, false);
11234 break;
11235
11236 case GFC_ISYM_ISHFT:
11237 gfc_conv_intrinsic_ishft (se, expr);
11238 break;
11239
11240 case GFC_ISYM_ISHFTC:
11241 gfc_conv_intrinsic_ishftc (se, expr);
11242 break;
11243
11244 case GFC_ISYM_LEADZ:
11245 gfc_conv_intrinsic_leadz (se, expr);
11246 break;
11247
11248 case GFC_ISYM_TRAILZ:
11249 gfc_conv_intrinsic_trailz (se, expr);
11250 break;
11251
11252 case GFC_ISYM_POPCNT:
11253 gfc_conv_intrinsic_popcnt_poppar (se, expr, 0);
11254 break;
11255
11256 case GFC_ISYM_POPPAR:
11257 gfc_conv_intrinsic_popcnt_poppar (se, expr, 1);
11258 break;
11259
11260 case GFC_ISYM_LBOUND:
11261 gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_LBOUND);
11262 break;
11263
11264 case GFC_ISYM_LCOBOUND:
11265 conv_intrinsic_cobound (se, expr);
11266 break;
11267
11268 case GFC_ISYM_TRANSPOSE:
11269 /* The scalarizer has already been set up for reversed dimension access
11270 order ; now we just get the argument value normally. */
11271 gfc_conv_expr (se, expr->value.function.actual->expr);
11272 break;
11273
11274 case GFC_ISYM_LEN:
11275 gfc_conv_intrinsic_len (se, expr);
11276 break;
11277
11278 case GFC_ISYM_LEN_TRIM:
11279 gfc_conv_intrinsic_len_trim (se, expr);
11280 break;
11281
11282 case GFC_ISYM_LGE:
11283 gfc_conv_intrinsic_strcmp (se, expr, GE_EXPR);
11284 break;
11285
11286 case GFC_ISYM_LGT:
11287 gfc_conv_intrinsic_strcmp (se, expr, GT_EXPR);
11288 break;
11289
11290 case GFC_ISYM_LLE:
11291 gfc_conv_intrinsic_strcmp (se, expr, LE_EXPR);
11292 break;
11293
11294 case GFC_ISYM_LLT:
11295 gfc_conv_intrinsic_strcmp (se, expr, LT_EXPR);
11296 break;
11297
11298 case GFC_ISYM_MALLOC:
11299 gfc_conv_intrinsic_malloc (se, expr);
11300 break;
11301
11302 case GFC_ISYM_MASKL:
11303 gfc_conv_intrinsic_mask (se, expr, 1);
11304 break;
11305
11306 case GFC_ISYM_MASKR:
11307 gfc_conv_intrinsic_mask (se, expr, 0);
11308 break;
11309
11310 case GFC_ISYM_MAX:
11311 if (expr->ts.type == BT_CHARACTER)
11312 gfc_conv_intrinsic_minmax_char (se, expr, 1);
11313 else
11314 gfc_conv_intrinsic_minmax (se, expr, GT_EXPR);
11315 break;
11316
11317 case GFC_ISYM_MAXLOC:
11318 gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
11319 break;
11320
11321 case GFC_ISYM_FINDLOC:
11322 gfc_conv_intrinsic_findloc (se, expr);
11323 break;
11324
11325 case GFC_ISYM_MAXVAL:
11326 gfc_conv_intrinsic_minmaxval (se, expr, GT_EXPR);
11327 break;
11328
11329 case GFC_ISYM_MERGE:
11330 gfc_conv_intrinsic_merge (se, expr);
11331 break;
11332
11333 case GFC_ISYM_MERGE_BITS:
11334 gfc_conv_intrinsic_merge_bits (se, expr);
11335 break;
11336
11337 case GFC_ISYM_MIN:
11338 if (expr->ts.type == BT_CHARACTER)
11339 gfc_conv_intrinsic_minmax_char (se, expr, -1);
11340 else
11341 gfc_conv_intrinsic_minmax (se, expr, LT_EXPR);
11342 break;
11343
11344 case GFC_ISYM_MINLOC:
11345 gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
11346 break;
11347
11348 case GFC_ISYM_MINVAL:
11349 gfc_conv_intrinsic_minmaxval (se, expr, LT_EXPR);
11350 break;
11351
11352 case GFC_ISYM_NEAREST:
11353 gfc_conv_intrinsic_nearest (se, expr);
11354 break;
11355
11356 case GFC_ISYM_NORM2:
11357 gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, true);
11358 break;
11359
11360 case GFC_ISYM_NOT:
11361 gfc_conv_intrinsic_not (se, expr);
11362 break;
11363
11364 case GFC_ISYM_OR:
11365 gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11366 break;
11367
11368 case GFC_ISYM_OUT_OF_RANGE:
11369 gfc_conv_intrinsic_out_of_range (se, expr);
11370 break;
11371
11372 case GFC_ISYM_PARITY:
11373 gfc_conv_intrinsic_arith (se, expr, NE_EXPR, false);
11374 break;
11375
11376 case GFC_ISYM_PRESENT:
11377 gfc_conv_intrinsic_present (se, expr);
11378 break;
11379
11380 case GFC_ISYM_PRODUCT:
11381 gfc_conv_intrinsic_arith (se, expr, MULT_EXPR, false);
11382 break;
11383
11384 case GFC_ISYM_RANK:
11385 gfc_conv_intrinsic_rank (se, expr);
11386 break;
11387
11388 case GFC_ISYM_RRSPACING:
11389 gfc_conv_intrinsic_rrspacing (se, expr);
11390 break;
11391
11392 case GFC_ISYM_SET_EXPONENT:
11393 gfc_conv_intrinsic_set_exponent (se, expr);
11394 break;
11395
11396 case GFC_ISYM_SCALE:
11397 gfc_conv_intrinsic_scale (se, expr);
11398 break;
11399
11400 case GFC_ISYM_SHAPE:
11401 gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_SHAPE);
11402 break;
11403
11404 case GFC_ISYM_SIGN:
11405 gfc_conv_intrinsic_sign (se, expr);
11406 break;
11407
11408 case GFC_ISYM_SIZE:
11409 gfc_conv_intrinsic_size (se, expr);
11410 break;
11411
11412 case GFC_ISYM_SIZEOF:
11413 case GFC_ISYM_C_SIZEOF:
11414 gfc_conv_intrinsic_sizeof (se, expr);
11415 break;
11416
11417 case GFC_ISYM_STORAGE_SIZE:
11418 gfc_conv_intrinsic_storage_size (se, expr);
11419 break;
11420
11421 case GFC_ISYM_SPACING:
11422 gfc_conv_intrinsic_spacing (se, expr);
11423 break;
11424
11425 case GFC_ISYM_STRIDE:
11426 conv_intrinsic_stride (se, expr);
11427 break;
11428
11429 case GFC_ISYM_SUM:
11430 gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, false);
11431 break;
11432
11433 case GFC_ISYM_TEAM_NUMBER:
11434 conv_intrinsic_team_number (se, expr);
11435 break;
11436
11437 case GFC_ISYM_TRANSFER:
11438 if (se->ss && se->ss->info->useflags)
11439 /* Access the previously obtained result. */
11440 gfc_conv_tmp_array_ref (se);
11441 else
11442 gfc_conv_intrinsic_transfer (se, expr);
11443 break;
11444
11445 case GFC_ISYM_TTYNAM:
11446 gfc_conv_intrinsic_ttynam (se, expr);
11447 break;
11448
11449 case GFC_ISYM_UBOUND:
11450 gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_UBOUND);
11451 break;
11452
11453 case GFC_ISYM_UCOBOUND:
11454 conv_intrinsic_cobound (se, expr);
11455 break;
11456
11457 case GFC_ISYM_XOR:
11458 gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11459 break;
11460
11461 case GFC_ISYM_LOC:
11462 gfc_conv_intrinsic_loc (se, expr);
11463 break;
11464
11465 case GFC_ISYM_THIS_IMAGE:
11466 /* For num_images() == 1, handle as LCOBOUND. */
11467 if (expr->value.function.actual->expr
11468 && flag_coarray == GFC_FCOARRAY_SINGLE)
11469 conv_intrinsic_cobound (se, expr);
11470 else
11471 trans_this_image (se, expr);
11472 break;
11473
11474 case GFC_ISYM_IMAGE_INDEX:
11475 trans_image_index (se, expr);
11476 break;
11477
11478 case GFC_ISYM_IMAGE_STATUS:
11479 conv_intrinsic_image_status (se, expr);
11480 break;
11481
11482 case GFC_ISYM_NUM_IMAGES:
11483 trans_num_images (se, expr);
11484 break;
11485
11486 case GFC_ISYM_ACCESS:
11487 case GFC_ISYM_CHDIR:
11488 case GFC_ISYM_CHMOD:
11489 case GFC_ISYM_DTIME:
11490 case GFC_ISYM_ETIME:
11491 case GFC_ISYM_EXTENDS_TYPE_OF:
11492 case GFC_ISYM_FGET:
11493 case GFC_ISYM_FGETC:
11494 case GFC_ISYM_FNUM:
11495 case GFC_ISYM_FPUT:
11496 case GFC_ISYM_FPUTC:
11497 case GFC_ISYM_FSTAT:
11498 case GFC_ISYM_FTELL:
11499 case GFC_ISYM_GETCWD:
11500 case GFC_ISYM_GETGID:
11501 case GFC_ISYM_GETPID:
11502 case GFC_ISYM_GETUID:
11503 case GFC_ISYM_HOSTNM:
11504 case GFC_ISYM_IERRNO:
11505 case GFC_ISYM_IRAND:
11506 case GFC_ISYM_ISATTY:
11507 case GFC_ISYM_JN2:
11508 case GFC_ISYM_LINK:
11509 case GFC_ISYM_LSTAT:
11510 case GFC_ISYM_MATMUL:
11511 case GFC_ISYM_MCLOCK:
11512 case GFC_ISYM_MCLOCK8:
11513 case GFC_ISYM_RAND:
11514 case GFC_ISYM_REDUCE:
11515 case GFC_ISYM_RENAME:
11516 case GFC_ISYM_SECOND:
11517 case GFC_ISYM_SECNDS:
11518 case GFC_ISYM_SIGNAL:
11519 case GFC_ISYM_STAT:
11520 case GFC_ISYM_SYMLNK:
11521 case GFC_ISYM_SYSTEM:
11522 case GFC_ISYM_TIME:
11523 case GFC_ISYM_TIME8:
11524 case GFC_ISYM_UMASK:
11525 case GFC_ISYM_UNLINK:
11526 case GFC_ISYM_YN2:
11527 gfc_conv_intrinsic_funcall (se, expr);
11528 break;
11529
11530 case GFC_ISYM_EOSHIFT:
11531 case GFC_ISYM_PACK:
11532 case GFC_ISYM_RESHAPE:
11533 /* For those, expr->rank should always be >0 and thus the if above the
11534 switch should have matched. */
11535 gcc_unreachable ();
11536 break;
11537
11538 default:
11539 gfc_conv_intrinsic_lib_function (se, expr);
11540 break;
11541 }
11542 }
11543
11544
11545 static gfc_ss *
11546 walk_inline_intrinsic_transpose (gfc_ss *ss, gfc_expr *expr)
11547 {
11548 gfc_ss *arg_ss, *tmp_ss;
11549 gfc_actual_arglist *arg;
11550
11551 arg = expr->value.function.actual;
11552
11553 gcc_assert (arg->expr);
11554
11555 arg_ss = gfc_walk_subexpr (gfc_ss_terminator, arg->expr);
11556 gcc_assert (arg_ss != gfc_ss_terminator);
11557
11558 for (tmp_ss = arg_ss; ; tmp_ss = tmp_ss->next)
11559 {
11560 if (tmp_ss->info->type != GFC_SS_SCALAR
11561 && tmp_ss->info->type != GFC_SS_REFERENCE)
11562 {
11563 gcc_assert (tmp_ss->dimen == 2);
11564
11565 /* We just invert dimensions. */
11566 std::swap (tmp_ss->dim[0], tmp_ss->dim[1]);
11567 }
11568
11569 /* Stop when tmp_ss points to the last valid element of the chain... */
11570 if (tmp_ss->next == gfc_ss_terminator)
11571 break;
11572 }
11573
11574 /* ... so that we can attach the rest of the chain to it. */
11575 tmp_ss->next = ss;
11576
11577 return arg_ss;
11578 }
11579
11580
11581 /* Move the given dimension of the given gfc_ss list to a nested gfc_ss list.
11582 This has the side effect of reversing the nested list, so there is no
11583 need to call gfc_reverse_ss on it (the given list is assumed not to be
11584 reversed yet). */
11585
11586 static gfc_ss *
11587 nest_loop_dimension (gfc_ss *ss, int dim)
11588 {
11589 int ss_dim, i;
11590 gfc_ss *new_ss, *prev_ss = gfc_ss_terminator;
11591 gfc_loopinfo *new_loop;
11592
11593 gcc_assert (ss != gfc_ss_terminator);
11594
11595 for (; ss != gfc_ss_terminator; ss = ss->next)
11596 {
11597 new_ss = gfc_get_ss ();
11598 new_ss->next = prev_ss;
11599 new_ss->parent = ss;
11600 new_ss->info = ss->info;
11601 new_ss->info->refcount++;
11602 if (ss->dimen != 0)
11603 {
11604 gcc_assert (ss->info->type != GFC_SS_SCALAR
11605 && ss->info->type != GFC_SS_REFERENCE);
11606
11607 new_ss->dimen = 1;
11608 new_ss->dim[0] = ss->dim[dim];
11609
11610 gcc_assert (dim < ss->dimen);
11611
11612 ss_dim = --ss->dimen;
11613 for (i = dim; i < ss_dim; i++)
11614 ss->dim[i] = ss->dim[i + 1];
11615
11616 ss->dim[ss_dim] = 0;
11617 }
11618 prev_ss = new_ss;
11619
11620 if (ss->nested_ss)
11621 {
11622 ss->nested_ss->parent = new_ss;
11623 new_ss->nested_ss = ss->nested_ss;
11624 }
11625 ss->nested_ss = new_ss;
11626 }
11627
11628 new_loop = gfc_get_loopinfo ();
11629 gfc_init_loopinfo (new_loop);
11630
11631 gcc_assert (prev_ss != NULL);
11632 gcc_assert (prev_ss != gfc_ss_terminator);
11633 gfc_add_ss_to_loop (new_loop, prev_ss);
11634 return new_ss->parent;
11635 }
11636
11637
11638 /* Create the gfc_ss list for the SUM/PRODUCT arguments when the function
11639 is to be inlined. */
11640
11641 static gfc_ss *
11642 walk_inline_intrinsic_arith (gfc_ss *ss, gfc_expr *expr)
11643 {
11644 gfc_ss *tmp_ss, *tail, *array_ss;
11645 gfc_actual_arglist *arg1, *arg2, *arg3;
11646 int sum_dim;
11647 bool scalar_mask = false;
11648
11649 /* The rank of the result will be determined later. */
11650 arg1 = expr->value.function.actual;
11651 arg2 = arg1->next;
11652 arg3 = arg2->next;
11653 gcc_assert (arg3 != NULL);
11654
11655 if (expr->rank == 0)
11656 return ss;
11657
11658 tmp_ss = gfc_ss_terminator;
11659
11660 if (arg3->expr)
11661 {
11662 gfc_ss *mask_ss;
11663
11664 mask_ss = gfc_walk_subexpr (tmp_ss, arg3->expr);
11665 if (mask_ss == tmp_ss)
11666 scalar_mask = 1;
11667
11668 tmp_ss = mask_ss;
11669 }
11670
11671 array_ss = gfc_walk_subexpr (tmp_ss, arg1->expr);
11672 gcc_assert (array_ss != tmp_ss);
11673
11674 /* Odd thing: If the mask is scalar, it is used by the frontend after
11675 the array (to make an if around the nested loop). Thus it shall
11676 be after array_ss once the gfc_ss list is reversed. */
11677 if (scalar_mask)
11678 tmp_ss = gfc_get_scalar_ss (array_ss, arg3->expr);
11679 else
11680 tmp_ss = array_ss;
11681
11682 /* "Hide" the dimension on which we will sum in the first arg's scalarization
11683 chain. */
11684 sum_dim = mpz_get_si (arg2->expr->value.integer) - 1;
11685 tail = nest_loop_dimension (tmp_ss, sum_dim);
11686 tail->next = ss;
11687
11688 return tmp_ss;
11689 }
11690
11691
11692 /* Create the gfc_ss list for the arguments to MINLOC or MAXLOC when the
11693 function is to be inlined. */
11694
11695 static gfc_ss *
11696 walk_inline_intrinsic_minmaxloc (gfc_ss *ss, gfc_expr *expr ATTRIBUTE_UNUSED)
11697 {
11698 if (expr->rank == 0)
11699 return ss;
11700
11701 gfc_actual_arglist *array_arg = expr->value.function.actual;
11702 gfc_actual_arglist *dim_arg = array_arg->next;
11703 gfc_actual_arglist *mask_arg = dim_arg->next;
11704 gfc_actual_arglist *kind_arg = mask_arg->next;
11705 gfc_actual_arglist *back_arg = kind_arg->next;
11706
11707 gfc_expr *array = array_arg->expr;
11708 gfc_expr *dim = dim_arg->expr;
11709 gfc_expr *mask = mask_arg->expr;
11710 gfc_expr *back = back_arg->expr;
11711
11712 if (dim == nullptr)
11713 return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
11714
11715 gfc_ss *tmp_ss = gfc_ss_terminator;
11716
11717 bool scalar_mask = false;
11718 if (mask)
11719 {
11720 gfc_ss *mask_ss = gfc_walk_subexpr (tmp_ss, mask);
11721 if (mask_ss == tmp_ss)
11722 scalar_mask = true;
11723 else if (maybe_absent_optional_variable (mask))
11724 mask_ss->info->can_be_null_ref = true;
11725
11726 tmp_ss = mask_ss;
11727 }
11728
11729 gfc_ss *array_ss = gfc_walk_subexpr (tmp_ss, array);
11730 gcc_assert (array_ss != tmp_ss);
11731
11732 tmp_ss = array_ss;
11733
11734 /* Move the dimension on which we will sum to a separate nested scalarization
11735 chain, "hiding" that dimension from the outer scalarization. */
11736 int dim_val = mpz_get_si (dim->value.integer);
11737 gfc_ss *tail = nest_loop_dimension (tmp_ss, dim_val - 1);
11738
11739 if (back && array->rank > 1)
11740 {
11741 /* If there are nested scalarization loops, include BACK in the
11742 scalarization chains to avoid evaluating it multiple times in a loop.
11743 Otherwise, prefer to handle it outside of scalarization. */
11744 gfc_ss *back_ss = gfc_get_scalar_ss (ss, back);
11745 back_ss->info->type = GFC_SS_REFERENCE;
11746 if (maybe_absent_optional_variable (back))
11747 back_ss->info->can_be_null_ref = true;
11748
11749 tail->next = back_ss;
11750 }
11751 else
11752 tail->next = ss;
11753
11754 if (scalar_mask)
11755 {
11756 tmp_ss = gfc_get_scalar_ss (tmp_ss, mask);
11757 /* MASK can be a forwarded optional argument, so make the necessary setup
11758 to avoid the scalarizer generating any unguarded pointer dereference in
11759 that case. */
11760 tmp_ss->info->type = GFC_SS_REFERENCE;
11761 if (maybe_absent_optional_variable (mask))
11762 tmp_ss->info->can_be_null_ref = true;
11763 }
11764
11765 return tmp_ss;
11766 }
11767
11768
11769 static gfc_ss *
11770 walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr)
11771 {
11772
11773 switch (expr->value.function.isym->id)
11774 {
11775 case GFC_ISYM_PRODUCT:
11776 case GFC_ISYM_SUM:
11777 return walk_inline_intrinsic_arith (ss, expr);
11778
11779 case GFC_ISYM_TRANSPOSE:
11780 return walk_inline_intrinsic_transpose (ss, expr);
11781
11782 case GFC_ISYM_MAXLOC:
11783 case GFC_ISYM_MINLOC:
11784 return walk_inline_intrinsic_minmaxloc (ss, expr);
11785
11786 default:
11787 gcc_unreachable ();
11788 }
11789 gcc_unreachable ();
11790 }
11791
11792
11793 /* This generates code to execute before entering the scalarization loop.
11794 Currently does nothing. */
11795
11796 void
11797 gfc_add_intrinsic_ss_code (gfc_loopinfo * loop ATTRIBUTE_UNUSED, gfc_ss * ss)
11798 {
11799 switch (ss->info->expr->value.function.isym->id)
11800 {
11801 case GFC_ISYM_UBOUND:
11802 case GFC_ISYM_LBOUND:
11803 case GFC_ISYM_UCOBOUND:
11804 case GFC_ISYM_LCOBOUND:
11805 case GFC_ISYM_MAXLOC:
11806 case GFC_ISYM_MINLOC:
11807 case GFC_ISYM_THIS_IMAGE:
11808 case GFC_ISYM_SHAPE:
11809 break;
11810
11811 default:
11812 gcc_unreachable ();
11813 }
11814 }
11815
11816
11817 /* The LBOUND, LCOBOUND, UBOUND, UCOBOUND, and SHAPE intrinsics with
11818 one parameter are expanded into code inside the scalarization loop. */
11819
11820 static gfc_ss *
11821 gfc_walk_intrinsic_bound (gfc_ss * ss, gfc_expr * expr)
11822 {
11823 if (expr->value.function.actual->expr->ts.type == BT_CLASS)
11824 gfc_add_class_array_ref (expr->value.function.actual->expr);
11825
11826 /* The two argument version returns a scalar. */
11827 if (expr->value.function.isym->id != GFC_ISYM_SHAPE
11828 && expr->value.function.actual->next->expr)
11829 return ss;
11830
11831 return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
11832 }
11833
11834
11835 /* Walk an intrinsic array libcall. */
11836
11837 static gfc_ss *
11838 gfc_walk_intrinsic_libfunc (gfc_ss * ss, gfc_expr * expr)
11839 {
11840 gcc_assert (expr->rank > 0);
11841 return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
11842 }
11843
11844
11845 /* Return whether the function call expression EXPR will be expanded
11846 inline by gfc_conv_intrinsic_function. */
11847
11848 bool
11849 gfc_inline_intrinsic_function_p (gfc_expr *expr)
11850 {
11851 gfc_actual_arglist *args, *dim_arg, *mask_arg;
11852 gfc_expr *maskexpr;
11853
11854 gfc_intrinsic_sym *isym = expr->value.function.isym;
11855 if (!isym)
11856 return false;
11857
11858 switch (isym->id)
11859 {
11860 case GFC_ISYM_PRODUCT:
11861 case GFC_ISYM_SUM:
11862 /* Disable inline expansion if code size matters. */
11863 if (optimize_size)
11864 return false;
11865
11866 args = expr->value.function.actual;
11867 dim_arg = args->next;
11868
11869 /* We need to be able to subset the SUM argument at compile-time. */
11870 if (dim_arg->expr && dim_arg->expr->expr_type != EXPR_CONSTANT)
11871 return false;
11872
11873 /* FIXME: If MASK is optional for a more than two-dimensional
11874 argument, the scalarizer gets confused if the mask is
11875 absent. See PR 82995. For now, fall back to the library
11876 function. */
11877
11878 mask_arg = dim_arg->next;
11879 maskexpr = mask_arg->expr;
11880
11881 if (expr->rank > 0 && maskexpr && maskexpr->expr_type == EXPR_VARIABLE
11882 && maskexpr->symtree->n.sym->attr.dummy
11883 && maskexpr->symtree->n.sym->attr.optional)
11884 return false;
11885
11886 return true;
11887
11888 case GFC_ISYM_TRANSPOSE:
11889 return true;
11890
11891 case GFC_ISYM_MINLOC:
11892 case GFC_ISYM_MAXLOC:
11893 {
11894 if ((isym->id == GFC_ISYM_MINLOC
11895 && (flag_inline_intrinsics
11896 & GFC_FLAG_INLINE_INTRINSIC_MINLOC) == 0)
11897 || (isym->id == GFC_ISYM_MAXLOC
11898 && (flag_inline_intrinsics
11899 & GFC_FLAG_INLINE_INTRINSIC_MAXLOC) == 0))
11900 return false;
11901
11902 gfc_actual_arglist *array_arg = expr->value.function.actual;
11903 gfc_actual_arglist *dim_arg = array_arg->next;
11904
11905 gfc_expr *array = array_arg->expr;
11906 gfc_expr *dim = dim_arg->expr;
11907
11908 if (!(array->ts.type == BT_INTEGER
11909 || array->ts.type == BT_REAL))
11910 return false;
11911
11912 if (array->rank == 1)
11913 return true;
11914
11915 if (dim != nullptr
11916 && dim->expr_type != EXPR_CONSTANT)
11917 return false;
11918
11919 return true;
11920 }
11921
11922 default:
11923 return false;
11924 }
11925 }
11926
11927
11928 /* Returns nonzero if the specified intrinsic function call maps directly to
11929 an external library call. Should only be used for functions that return
11930 arrays. */
11931
11932 int
11933 gfc_is_intrinsic_libcall (gfc_expr * expr)
11934 {
11935 gcc_assert (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym);
11936 gcc_assert (expr->rank > 0);
11937
11938 if (gfc_inline_intrinsic_function_p (expr))
11939 return 0;
11940
11941 switch (expr->value.function.isym->id)
11942 {
11943 case GFC_ISYM_ALL:
11944 case GFC_ISYM_ANY:
11945 case GFC_ISYM_COUNT:
11946 case GFC_ISYM_FINDLOC:
11947 case GFC_ISYM_JN2:
11948 case GFC_ISYM_IANY:
11949 case GFC_ISYM_IALL:
11950 case GFC_ISYM_IPARITY:
11951 case GFC_ISYM_MATMUL:
11952 case GFC_ISYM_MAXLOC:
11953 case GFC_ISYM_MAXVAL:
11954 case GFC_ISYM_MINLOC:
11955 case GFC_ISYM_MINVAL:
11956 case GFC_ISYM_NORM2:
11957 case GFC_ISYM_PARITY:
11958 case GFC_ISYM_PRODUCT:
11959 case GFC_ISYM_SUM:
11960 case GFC_ISYM_SPREAD:
11961 case GFC_ISYM_YN2:
11962 /* Ignore absent optional parameters. */
11963 return 1;
11964
11965 case GFC_ISYM_CSHIFT:
11966 case GFC_ISYM_EOSHIFT:
11967 case GFC_ISYM_GET_TEAM:
11968 case GFC_ISYM_FAILED_IMAGES:
11969 case GFC_ISYM_STOPPED_IMAGES:
11970 case GFC_ISYM_PACK:
11971 case GFC_ISYM_REDUCE:
11972 case GFC_ISYM_RESHAPE:
11973 case GFC_ISYM_UNPACK:
11974 /* Pass absent optional parameters. */
11975 return 2;
11976
11977 default:
11978 return 0;
11979 }
11980 }
11981
11982 /* Walk an intrinsic function. */
11983 gfc_ss *
11984 gfc_walk_intrinsic_function (gfc_ss * ss, gfc_expr * expr,
11985 gfc_intrinsic_sym * isym)
11986 {
11987 gcc_assert (isym);
11988
11989 if (isym->elemental)
11990 return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
11991 expr->value.function.isym,
11992 GFC_SS_SCALAR);
11993
11994 if (expr->rank == 0 && expr->corank == 0)
11995 return ss;
11996
11997 if (gfc_inline_intrinsic_function_p (expr))
11998 return walk_inline_intrinsic_function (ss, expr);
11999
12000 if (expr->rank != 0 && gfc_is_intrinsic_libcall (expr))
12001 return gfc_walk_intrinsic_libfunc (ss, expr);
12002
12003 /* Special cases. */
12004 switch (isym->id)
12005 {
12006 case GFC_ISYM_LBOUND:
12007 case GFC_ISYM_LCOBOUND:
12008 case GFC_ISYM_UBOUND:
12009 case GFC_ISYM_UCOBOUND:
12010 case GFC_ISYM_THIS_IMAGE:
12011 case GFC_ISYM_SHAPE:
12012 return gfc_walk_intrinsic_bound (ss, expr);
12013
12014 case GFC_ISYM_TRANSFER:
12015 case GFC_ISYM_CAF_GET:
12016 return gfc_walk_intrinsic_libfunc (ss, expr);
12017
12018 default:
12019 /* This probably meant someone forgot to add an intrinsic to the above
12020 list(s) when they implemented it, or something's gone horribly
12021 wrong. */
12022 gcc_unreachable ();
12023 }
12024 }
12025
12026 static tree
12027 conv_co_collective (gfc_code *code)
12028 {
12029 gfc_se argse;
12030 stmtblock_t block, post_block;
12031 tree fndecl, array = NULL_TREE, strlen, image_index, stat, errmsg, errmsg_len;
12032 gfc_expr *image_idx_expr, *stat_expr, *errmsg_expr, *opr_expr;
12033
12034 gfc_start_block (&block);
12035 gfc_init_block (&post_block);
12036
12037 if (code->resolved_isym->id == GFC_ISYM_CO_REDUCE)
12038 {
12039 opr_expr = code->ext.actual->next->expr;
12040 image_idx_expr = code->ext.actual->next->next->expr;
12041 stat_expr = code->ext.actual->next->next->next->expr;
12042 errmsg_expr = code->ext.actual->next->next->next->next->expr;
12043 }
12044 else
12045 {
12046 opr_expr = NULL;
12047 image_idx_expr = code->ext.actual->next->expr;
12048 stat_expr = code->ext.actual->next->next->expr;
12049 errmsg_expr = code->ext.actual->next->next->next->expr;
12050 }
12051
12052 /* stat. */
12053 if (stat_expr)
12054 {
12055 gfc_init_se (&argse, NULL);
12056 gfc_conv_expr (&argse, stat_expr);
12057 gfc_add_block_to_block (&block, &argse.pre);
12058 gfc_add_block_to_block (&post_block, &argse.post);
12059 stat = argse.expr;
12060 if (flag_coarray != GFC_FCOARRAY_SINGLE)
12061 stat = gfc_build_addr_expr (NULL_TREE, stat);
12062 }
12063 else if (flag_coarray == GFC_FCOARRAY_SINGLE)
12064 stat = NULL_TREE;
12065 else
12066 stat = null_pointer_node;
12067
12068 /* Early exit for GFC_FCOARRAY_SINGLE. */
12069 if (flag_coarray == GFC_FCOARRAY_SINGLE)
12070 {
12071 if (stat != NULL_TREE)
12072 {
12073 /* For optional stats, check the pointer is valid before zero'ing. */
12074 if (gfc_expr_attr (stat_expr).optional)
12075 {
12076 tree tmp;
12077 stmtblock_t ass_block;
12078 gfc_start_block (&ass_block);
12079 gfc_add_modify (&ass_block, stat,
12080 fold_convert (TREE_TYPE (stat),
12081 integer_zero_node));
12082 tmp = fold_build2 (NE_EXPR, logical_type_node,
12083 gfc_build_addr_expr (NULL_TREE, stat),
12084 null_pointer_node);
12085 tmp = fold_build3 (COND_EXPR, void_type_node, tmp,
12086 gfc_finish_block (&ass_block),
12087 build_empty_stmt (input_location));
12088 gfc_add_expr_to_block (&block, tmp);
12089 }
12090 else
12091 gfc_add_modify (&block, stat,
12092 fold_convert (TREE_TYPE (stat), integer_zero_node));
12093 }
12094 return gfc_finish_block (&block);
12095 }
12096
12097 gfc_symbol *derived = code->ext.actual->expr->ts.type == BT_DERIVED
12098 ? code->ext.actual->expr->ts.u.derived : NULL;
12099
12100 /* Handle the array. */
12101 gfc_init_se (&argse, NULL);
12102 if (!derived || !derived->attr.alloc_comp
12103 || code->resolved_isym->id != GFC_ISYM_CO_BROADCAST)
12104 {
12105 if (code->ext.actual->expr->rank == 0)
12106 {
12107 symbol_attribute attr;
12108 gfc_clear_attr (&attr);
12109 gfc_init_se (&argse, NULL);
12110 gfc_conv_expr (&argse, code->ext.actual->expr);
12111 gfc_add_block_to_block (&block, &argse.pre);
12112 gfc_add_block_to_block (&post_block, &argse.post);
12113 array = gfc_conv_scalar_to_descriptor (&argse, argse.expr, attr);
12114 array = gfc_build_addr_expr (NULL_TREE, array);
12115 }
12116 else
12117 {
12118 argse.want_pointer = 1;
12119 gfc_conv_expr_descriptor (&argse, code->ext.actual->expr);
12120 array = argse.expr;
12121 }
12122 }
12123
12124 gfc_add_block_to_block (&block, &argse.pre);
12125 gfc_add_block_to_block (&post_block, &argse.post);
12126
12127 if (code->ext.actual->expr->ts.type == BT_CHARACTER)
12128 strlen = argse.string_length;
12129 else
12130 strlen = integer_zero_node;
12131
12132 /* image_index. */
12133 if (image_idx_expr)
12134 {
12135 gfc_init_se (&argse, NULL);
12136 gfc_conv_expr (&argse, image_idx_expr);
12137 gfc_add_block_to_block (&block, &argse.pre);
12138 gfc_add_block_to_block (&post_block, &argse.post);
12139 image_index = fold_convert (integer_type_node, argse.expr);
12140 }
12141 else
12142 image_index = integer_zero_node;
12143
12144 /* errmsg. */
12145 if (errmsg_expr)
12146 {
12147 gfc_init_se (&argse, NULL);
12148 gfc_conv_expr (&argse, errmsg_expr);
12149 gfc_add_block_to_block (&block, &argse.pre);
12150 gfc_add_block_to_block (&post_block, &argse.post);
12151 errmsg = argse.expr;
12152 errmsg_len = fold_convert (size_type_node, argse.string_length);
12153 }
12154 else
12155 {
12156 errmsg = null_pointer_node;
12157 errmsg_len = build_zero_cst (size_type_node);
12158 }
12159
12160 /* Generate the function call. */
12161 switch (code->resolved_isym->id)
12162 {
12163 case GFC_ISYM_CO_BROADCAST:
12164 fndecl = gfor_fndecl_co_broadcast;
12165 break;
12166 case GFC_ISYM_CO_MAX:
12167 fndecl = gfor_fndecl_co_max;
12168 break;
12169 case GFC_ISYM_CO_MIN:
12170 fndecl = gfor_fndecl_co_min;
12171 break;
12172 case GFC_ISYM_CO_REDUCE:
12173 fndecl = gfor_fndecl_co_reduce;
12174 break;
12175 case GFC_ISYM_CO_SUM:
12176 fndecl = gfor_fndecl_co_sum;
12177 break;
12178 default:
12179 gcc_unreachable ();
12180 }
12181
12182 if (derived && derived->attr.alloc_comp
12183 && code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12184 /* The derived type has the attribute 'alloc_comp'. */
12185 {
12186 tree tmp = gfc_bcast_alloc_comp (derived, code->ext.actual->expr,
12187 code->ext.actual->expr->rank,
12188 image_index, stat, errmsg, errmsg_len);
12189 gfc_add_expr_to_block (&block, tmp);
12190 }
12191 else
12192 {
12193 if (code->resolved_isym->id == GFC_ISYM_CO_SUM
12194 || code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12195 fndecl = build_call_expr_loc (input_location, fndecl, 5, array,
12196 image_index, stat, errmsg, errmsg_len);
12197 else if (code->resolved_isym->id != GFC_ISYM_CO_REDUCE)
12198 fndecl = build_call_expr_loc (input_location, fndecl, 6, array,
12199 image_index, stat, errmsg,
12200 strlen, errmsg_len);
12201 else
12202 {
12203 tree opr, opr_flags;
12204
12205 // FIXME: Handle TS29113's bind(C) strings with descriptor.
12206 int opr_flag_int;
12207 if (gfc_is_proc_ptr_comp (opr_expr))
12208 {
12209 gfc_symbol *sym = gfc_get_proc_ptr_comp (opr_expr)->ts.interface;
12210 opr_flag_int = sym->attr.dimension
12211 || (sym->ts.type == BT_CHARACTER
12212 && !sym->attr.is_bind_c)
12213 ? GFC_CAF_BYREF : 0;
12214 opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12215 && !sym->attr.is_bind_c
12216 ? GFC_CAF_HIDDENLEN : 0;
12217 opr_flag_int |= sym->formal->sym->attr.value
12218 ? GFC_CAF_ARG_VALUE : 0;
12219 }
12220 else
12221 {
12222 opr_flag_int = gfc_return_by_reference (opr_expr->symtree->n.sym)
12223 ? GFC_CAF_BYREF : 0;
12224 opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12225 && !opr_expr->symtree->n.sym->attr.is_bind_c
12226 ? GFC_CAF_HIDDENLEN : 0;
12227 opr_flag_int |= opr_expr->symtree->n.sym->formal->sym->attr.value
12228 ? GFC_CAF_ARG_VALUE : 0;
12229 }
12230 opr_flags = build_int_cst (integer_type_node, opr_flag_int);
12231 gfc_conv_expr (&argse, opr_expr);
12232 opr = argse.expr;
12233 fndecl = build_call_expr_loc (input_location, fndecl, 8, array, opr,
12234 opr_flags, image_index, stat, errmsg,
12235 strlen, errmsg_len);
12236 }
12237 }
12238
12239 gfc_add_expr_to_block (&block, fndecl);
12240 gfc_add_block_to_block (&block, &post_block);
12241
12242 return gfc_finish_block (&block);
12243 }
12244
12245
12246 static tree
12247 conv_intrinsic_atomic_op (gfc_code *code)
12248 {
12249 gfc_se argse;
12250 tree tmp, atom, value, old = NULL_TREE, stat = NULL_TREE;
12251 stmtblock_t block, post_block;
12252 gfc_expr *atom_expr = code->ext.actual->expr;
12253 gfc_expr *stat_expr;
12254 built_in_function fn;
12255
12256 if (atom_expr->expr_type == EXPR_FUNCTION
12257 && atom_expr->value.function.isym
12258 && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12259 atom_expr = atom_expr->value.function.actual->expr;
12260
12261 gfc_start_block (&block);
12262 gfc_init_block (&post_block);
12263
12264 gfc_init_se (&argse, NULL);
12265 argse.want_pointer = 1;
12266 gfc_conv_expr (&argse, atom_expr);
12267 gfc_add_block_to_block (&block, &argse.pre);
12268 gfc_add_block_to_block (&post_block, &argse.post);
12269 atom = argse.expr;
12270
12271 gfc_init_se (&argse, NULL);
12272 if (flag_coarray == GFC_FCOARRAY_LIB
12273 && code->ext.actual->next->expr->ts.kind == atom_expr->ts.kind)
12274 argse.want_pointer = 1;
12275 gfc_conv_expr (&argse, code->ext.actual->next->expr);
12276 gfc_add_block_to_block (&block, &argse.pre);
12277 gfc_add_block_to_block (&post_block, &argse.post);
12278 value = argse.expr;
12279
12280 switch (code->resolved_isym->id)
12281 {
12282 case GFC_ISYM_ATOMIC_ADD:
12283 case GFC_ISYM_ATOMIC_AND:
12284 case GFC_ISYM_ATOMIC_DEF:
12285 case GFC_ISYM_ATOMIC_OR:
12286 case GFC_ISYM_ATOMIC_XOR:
12287 stat_expr = code->ext.actual->next->next->expr;
12288 if (flag_coarray == GFC_FCOARRAY_LIB)
12289 old = null_pointer_node;
12290 break;
12291 default:
12292 gfc_init_se (&argse, NULL);
12293 if (flag_coarray == GFC_FCOARRAY_LIB)
12294 argse.want_pointer = 1;
12295 gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
12296 gfc_add_block_to_block (&block, &argse.pre);
12297 gfc_add_block_to_block (&post_block, &argse.post);
12298 old = argse.expr;
12299 stat_expr = code->ext.actual->next->next->next->expr;
12300 }
12301
12302 /* STAT= */
12303 if (stat_expr != NULL)
12304 {
12305 gcc_assert (stat_expr->expr_type == EXPR_VARIABLE);
12306 gfc_init_se (&argse, NULL);
12307 if (flag_coarray == GFC_FCOARRAY_LIB)
12308 argse.want_pointer = 1;
12309 gfc_conv_expr_val (&argse, stat_expr);
12310 gfc_add_block_to_block (&block, &argse.pre);
12311 gfc_add_block_to_block (&post_block, &argse.post);
12312 stat = argse.expr;
12313 }
12314 else if (flag_coarray == GFC_FCOARRAY_LIB)
12315 stat = null_pointer_node;
12316
12317 if (flag_coarray == GFC_FCOARRAY_LIB)
12318 {
12319 tree image_index, caf_decl, offset, token;
12320 int op;
12321
12322 switch (code->resolved_isym->id)
12323 {
12324 case GFC_ISYM_ATOMIC_ADD:
12325 case GFC_ISYM_ATOMIC_FETCH_ADD:
12326 op = (int) GFC_CAF_ATOMIC_ADD;
12327 break;
12328 case GFC_ISYM_ATOMIC_AND:
12329 case GFC_ISYM_ATOMIC_FETCH_AND:
12330 op = (int) GFC_CAF_ATOMIC_AND;
12331 break;
12332 case GFC_ISYM_ATOMIC_OR:
12333 case GFC_ISYM_ATOMIC_FETCH_OR:
12334 op = (int) GFC_CAF_ATOMIC_OR;
12335 break;
12336 case GFC_ISYM_ATOMIC_XOR:
12337 case GFC_ISYM_ATOMIC_FETCH_XOR:
12338 op = (int) GFC_CAF_ATOMIC_XOR;
12339 break;
12340 case GFC_ISYM_ATOMIC_DEF:
12341 op = 0; /* Unused. */
12342 break;
12343 default:
12344 gcc_unreachable ();
12345 }
12346
12347 caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12348 if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12349 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12350
12351 if (gfc_is_coindexed (atom_expr))
12352 image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12353 else
12354 image_index = integer_zero_node;
12355
12356 if (!POINTER_TYPE_P (TREE_TYPE (value)))
12357 {
12358 tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
12359 gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), value));
12360 value = gfc_build_addr_expr (NULL_TREE, tmp);
12361 }
12362
12363 gfc_init_se (&argse, NULL);
12364 gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12365 atom_expr);
12366
12367 gfc_add_block_to_block (&block, &argse.pre);
12368 if (code->resolved_isym->id == GFC_ISYM_ATOMIC_DEF)
12369 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_def, 7,
12370 token, offset, image_index, value, stat,
12371 build_int_cst (integer_type_node,
12372 (int) atom_expr->ts.type),
12373 build_int_cst (integer_type_node,
12374 (int) atom_expr->ts.kind));
12375 else
12376 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_op, 9,
12377 build_int_cst (integer_type_node, op),
12378 token, offset, image_index, value, old, stat,
12379 build_int_cst (integer_type_node,
12380 (int) atom_expr->ts.type),
12381 build_int_cst (integer_type_node,
12382 (int) atom_expr->ts.kind));
12383
12384 gfc_add_expr_to_block (&block, tmp);
12385 gfc_add_block_to_block (&block, &argse.post);
12386 gfc_add_block_to_block (&block, &post_block);
12387 return gfc_finish_block (&block);
12388 }
12389
12390
12391 switch (code->resolved_isym->id)
12392 {
12393 case GFC_ISYM_ATOMIC_ADD:
12394 case GFC_ISYM_ATOMIC_FETCH_ADD:
12395 fn = BUILT_IN_ATOMIC_FETCH_ADD_N;
12396 break;
12397 case GFC_ISYM_ATOMIC_AND:
12398 case GFC_ISYM_ATOMIC_FETCH_AND:
12399 fn = BUILT_IN_ATOMIC_FETCH_AND_N;
12400 break;
12401 case GFC_ISYM_ATOMIC_DEF:
12402 fn = BUILT_IN_ATOMIC_STORE_N;
12403 break;
12404 case GFC_ISYM_ATOMIC_OR:
12405 case GFC_ISYM_ATOMIC_FETCH_OR:
12406 fn = BUILT_IN_ATOMIC_FETCH_OR_N;
12407 break;
12408 case GFC_ISYM_ATOMIC_XOR:
12409 case GFC_ISYM_ATOMIC_FETCH_XOR:
12410 fn = BUILT_IN_ATOMIC_FETCH_XOR_N;
12411 break;
12412 default:
12413 gcc_unreachable ();
12414 }
12415
12416 tmp = TREE_TYPE (TREE_TYPE (atom));
12417 fn = (built_in_function) ((int) fn
12418 + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12419 + 1);
12420 tree itype = TREE_TYPE (TREE_TYPE (atom));
12421 tmp = builtin_decl_explicit (fn);
12422
12423 switch (code->resolved_isym->id)
12424 {
12425 case GFC_ISYM_ATOMIC_ADD:
12426 case GFC_ISYM_ATOMIC_AND:
12427 case GFC_ISYM_ATOMIC_DEF:
12428 case GFC_ISYM_ATOMIC_OR:
12429 case GFC_ISYM_ATOMIC_XOR:
12430 tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12431 fold_convert (itype, value),
12432 build_int_cst (NULL, MEMMODEL_RELAXED));
12433 gfc_add_expr_to_block (&block, tmp);
12434 break;
12435 default:
12436 tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12437 fold_convert (itype, value),
12438 build_int_cst (NULL, MEMMODEL_RELAXED));
12439 gfc_add_modify (&block, old, fold_convert (TREE_TYPE (old), tmp));
12440 break;
12441 }
12442
12443 if (stat != NULL_TREE)
12444 gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12445 gfc_add_block_to_block (&block, &post_block);
12446 return gfc_finish_block (&block);
12447 }
12448
12449
12450 static tree
12451 conv_intrinsic_atomic_ref (gfc_code *code)
12452 {
12453 gfc_se argse;
12454 tree tmp, atom, value, stat = NULL_TREE;
12455 stmtblock_t block, post_block;
12456 built_in_function fn;
12457 gfc_expr *atom_expr = code->ext.actual->next->expr;
12458
12459 if (atom_expr->expr_type == EXPR_FUNCTION
12460 && atom_expr->value.function.isym
12461 && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12462 atom_expr = atom_expr->value.function.actual->expr;
12463
12464 gfc_start_block (&block);
12465 gfc_init_block (&post_block);
12466 gfc_init_se (&argse, NULL);
12467 argse.want_pointer = 1;
12468 gfc_conv_expr (&argse, atom_expr);
12469 gfc_add_block_to_block (&block, &argse.pre);
12470 gfc_add_block_to_block (&post_block, &argse.post);
12471 atom = argse.expr;
12472
12473 gfc_init_se (&argse, NULL);
12474 if (flag_coarray == GFC_FCOARRAY_LIB
12475 && code->ext.actual->expr->ts.kind == atom_expr->ts.kind)
12476 argse.want_pointer = 1;
12477 gfc_conv_expr (&argse, code->ext.actual->expr);
12478 gfc_add_block_to_block (&block, &argse.pre);
12479 gfc_add_block_to_block (&post_block, &argse.post);
12480 value = argse.expr;
12481
12482 /* STAT= */
12483 if (code->ext.actual->next->next->expr != NULL)
12484 {
12485 gcc_assert (code->ext.actual->next->next->expr->expr_type
12486 == EXPR_VARIABLE);
12487 gfc_init_se (&argse, NULL);
12488 if (flag_coarray == GFC_FCOARRAY_LIB)
12489 argse.want_pointer = 1;
12490 gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
12491 gfc_add_block_to_block (&block, &argse.pre);
12492 gfc_add_block_to_block (&post_block, &argse.post);
12493 stat = argse.expr;
12494 }
12495 else if (flag_coarray == GFC_FCOARRAY_LIB)
12496 stat = null_pointer_node;
12497
12498 if (flag_coarray == GFC_FCOARRAY_LIB)
12499 {
12500 tree image_index, caf_decl, offset, token;
12501 tree orig_value = NULL_TREE, vardecl = NULL_TREE;
12502
12503 caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12504 if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12505 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12506
12507 if (gfc_is_coindexed (atom_expr))
12508 image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12509 else
12510 image_index = integer_zero_node;
12511
12512 gfc_init_se (&argse, NULL);
12513 gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12514 atom_expr);
12515 gfc_add_block_to_block (&block, &argse.pre);
12516
12517 /* Different type, need type conversion. */
12518 if (!POINTER_TYPE_P (TREE_TYPE (value)))
12519 {
12520 vardecl = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
12521 orig_value = value;
12522 value = gfc_build_addr_expr (NULL_TREE, vardecl);
12523 }
12524
12525 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_ref, 7,
12526 token, offset, image_index, value, stat,
12527 build_int_cst (integer_type_node,
12528 (int) atom_expr->ts.type),
12529 build_int_cst (integer_type_node,
12530 (int) atom_expr->ts.kind));
12531 gfc_add_expr_to_block (&block, tmp);
12532 if (vardecl != NULL_TREE)
12533 gfc_add_modify (&block, orig_value,
12534 fold_convert (TREE_TYPE (orig_value), vardecl));
12535 gfc_add_block_to_block (&block, &argse.post);
12536 gfc_add_block_to_block (&block, &post_block);
12537 return gfc_finish_block (&block);
12538 }
12539
12540 tmp = TREE_TYPE (TREE_TYPE (atom));
12541 fn = (built_in_function) ((int) BUILT_IN_ATOMIC_LOAD_N
12542 + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12543 + 1);
12544 tmp = builtin_decl_explicit (fn);
12545 tmp = build_call_expr_loc (input_location, tmp, 2, atom,
12546 build_int_cst (integer_type_node,
12547 MEMMODEL_RELAXED));
12548 gfc_add_modify (&block, value, fold_convert (TREE_TYPE (value), tmp));
12549
12550 if (stat != NULL_TREE)
12551 gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12552 gfc_add_block_to_block (&block, &post_block);
12553 return gfc_finish_block (&block);
12554 }
12555
12556
12557 static tree
12558 conv_intrinsic_atomic_cas (gfc_code *code)
12559 {
12560 gfc_se argse;
12561 tree tmp, atom, old, new_val, comp, stat = NULL_TREE;
12562 stmtblock_t block, post_block;
12563 built_in_function fn;
12564 gfc_expr *atom_expr = code->ext.actual->expr;
12565
12566 if (atom_expr->expr_type == EXPR_FUNCTION
12567 && atom_expr->value.function.isym
12568 && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12569 atom_expr = atom_expr->value.function.actual->expr;
12570
12571 gfc_init_block (&block);
12572 gfc_init_block (&post_block);
12573 gfc_init_se (&argse, NULL);
12574 argse.want_pointer = 1;
12575 gfc_conv_expr (&argse, atom_expr);
12576 atom = argse.expr;
12577
12578 gfc_init_se (&argse, NULL);
12579 if (flag_coarray == GFC_FCOARRAY_LIB)
12580 argse.want_pointer = 1;
12581 gfc_conv_expr (&argse, code->ext.actual->next->expr);
12582 gfc_add_block_to_block (&block, &argse.pre);
12583 gfc_add_block_to_block (&post_block, &argse.post);
12584 old = argse.expr;
12585
12586 gfc_init_se (&argse, NULL);
12587 if (flag_coarray == GFC_FCOARRAY_LIB)
12588 argse.want_pointer = 1;
12589 gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
12590 gfc_add_block_to_block (&block, &argse.pre);
12591 gfc_add_block_to_block (&post_block, &argse.post);
12592 comp = argse.expr;
12593
12594 gfc_init_se (&argse, NULL);
12595 if (flag_coarray == GFC_FCOARRAY_LIB
12596 && code->ext.actual->next->next->next->expr->ts.kind
12597 == atom_expr->ts.kind)
12598 argse.want_pointer = 1;
12599 gfc_conv_expr (&argse, code->ext.actual->next->next->next->expr);
12600 gfc_add_block_to_block (&block, &argse.pre);
12601 gfc_add_block_to_block (&post_block, &argse.post);
12602 new_val = argse.expr;
12603
12604 /* STAT= */
12605 if (code->ext.actual->next->next->next->next->expr != NULL)
12606 {
12607 gcc_assert (code->ext.actual->next->next->next->next->expr->expr_type
12608 == EXPR_VARIABLE);
12609 gfc_init_se (&argse, NULL);
12610 if (flag_coarray == GFC_FCOARRAY_LIB)
12611 argse.want_pointer = 1;
12612 gfc_conv_expr_val (&argse,
12613 code->ext.actual->next->next->next->next->expr);
12614 gfc_add_block_to_block (&block, &argse.pre);
12615 gfc_add_block_to_block (&post_block, &argse.post);
12616 stat = argse.expr;
12617 }
12618 else if (flag_coarray == GFC_FCOARRAY_LIB)
12619 stat = null_pointer_node;
12620
12621 if (flag_coarray == GFC_FCOARRAY_LIB)
12622 {
12623 tree image_index, caf_decl, offset, token;
12624
12625 caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12626 if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12627 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12628
12629 if (gfc_is_coindexed (atom_expr))
12630 image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12631 else
12632 image_index = integer_zero_node;
12633
12634 if (TREE_TYPE (TREE_TYPE (new_val)) != TREE_TYPE (TREE_TYPE (old)))
12635 {
12636 tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "new");
12637 gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), new_val));
12638 new_val = gfc_build_addr_expr (NULL_TREE, tmp);
12639 }
12640
12641 /* Convert a constant to a pointer. */
12642 if (!POINTER_TYPE_P (TREE_TYPE (comp)))
12643 {
12644 tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "comp");
12645 gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), comp));
12646 comp = gfc_build_addr_expr (NULL_TREE, tmp);
12647 }
12648
12649 gfc_init_se (&argse, NULL);
12650 gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12651 atom_expr);
12652 gfc_add_block_to_block (&block, &argse.pre);
12653
12654 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_cas, 9,
12655 token, offset, image_index, old, comp, new_val,
12656 stat, build_int_cst (integer_type_node,
12657 (int) atom_expr->ts.type),
12658 build_int_cst (integer_type_node,
12659 (int) atom_expr->ts.kind));
12660 gfc_add_expr_to_block (&block, tmp);
12661 gfc_add_block_to_block (&block, &argse.post);
12662 gfc_add_block_to_block (&block, &post_block);
12663 return gfc_finish_block (&block);
12664 }
12665
12666 tmp = TREE_TYPE (TREE_TYPE (atom));
12667 fn = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
12668 + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12669 + 1);
12670 tmp = builtin_decl_explicit (fn);
12671
12672 gfc_add_modify (&block, old, comp);
12673 tmp = build_call_expr_loc (input_location, tmp, 6, atom,
12674 gfc_build_addr_expr (NULL, old),
12675 fold_convert (TREE_TYPE (old), new_val),
12676 boolean_false_node,
12677 build_int_cst (NULL, MEMMODEL_RELAXED),
12678 build_int_cst (NULL, MEMMODEL_RELAXED));
12679 gfc_add_expr_to_block (&block, tmp);
12680
12681 if (stat != NULL_TREE)
12682 gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12683 gfc_add_block_to_block (&block, &post_block);
12684 return gfc_finish_block (&block);
12685 }
12686
12687 static tree
12688 conv_intrinsic_event_query (gfc_code *code)
12689 {
12690 gfc_se se, argse;
12691 tree stat = NULL_TREE, stat2 = NULL_TREE;
12692 tree count = NULL_TREE, count2 = NULL_TREE;
12693
12694 gfc_expr *event_expr = code->ext.actual->expr;
12695
12696 if (code->ext.actual->next->next->expr)
12697 {
12698 gcc_assert (code->ext.actual->next->next->expr->expr_type
12699 == EXPR_VARIABLE);
12700 gfc_init_se (&argse, NULL);
12701 gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
12702 stat = argse.expr;
12703 }
12704 else if (flag_coarray == GFC_FCOARRAY_LIB)
12705 stat = null_pointer_node;
12706
12707 if (code->ext.actual->next->expr)
12708 {
12709 gcc_assert (code->ext.actual->next->expr->expr_type == EXPR_VARIABLE);
12710 gfc_init_se (&argse, NULL);
12711 gfc_conv_expr_val (&argse, code->ext.actual->next->expr);
12712 count = argse.expr;
12713 }
12714
12715 gfc_start_block (&se.pre);
12716 if (flag_coarray == GFC_FCOARRAY_LIB)
12717 {
12718 tree tmp, token, image_index;
12719 tree index = build_zero_cst (gfc_array_index_type);
12720
12721 if (event_expr->expr_type == EXPR_FUNCTION
12722 && event_expr->value.function.isym
12723 && event_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12724 event_expr = event_expr->value.function.actual->expr;
12725
12726 tree caf_decl = gfc_get_tree_for_caf_expr (event_expr);
12727
12728 if (event_expr->symtree->n.sym->ts.type != BT_DERIVED
12729 || event_expr->symtree->n.sym->ts.u.derived->from_intmod
12730 != INTMOD_ISO_FORTRAN_ENV
12731 || event_expr->symtree->n.sym->ts.u.derived->intmod_sym_id
12732 != ISOFORTRAN_EVENT_TYPE)
12733 {
12734 gfc_error ("Sorry, the event component of derived type at %L is not "
12735 "yet supported", &event_expr->where);
12736 return NULL_TREE;
12737 }
12738
12739 if (gfc_is_coindexed (event_expr))
12740 {
12741 gfc_error ("The event variable at %L shall not be coindexed",
12742 &event_expr->where);
12743 return NULL_TREE;
12744 }
12745
12746 image_index = integer_zero_node;
12747
12748 gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
12749 event_expr);
12750
12751 /* For arrays, obtain the array index. */
12752 if (gfc_expr_attr (event_expr).dimension)
12753 {
12754 tree desc, tmp, extent, lbound, ubound;
12755 gfc_array_ref *ar, ar2;
12756 int i;
12757
12758 /* TODO: Extend this, once DT components are supported. */
12759 ar = &event_expr->ref->u.ar;
12760 ar2 = *ar;
12761 memset (ar, '\0', sizeof (*ar));
12762 ar->as = ar2.as;
12763 ar->type = AR_FULL;
12764
12765 gfc_init_se (&argse, NULL);
12766 argse.descriptor_only = 1;
12767 gfc_conv_expr_descriptor (&argse, event_expr);
12768 gfc_add_block_to_block (&se.pre, &argse.pre);
12769 desc = argse.expr;
12770 *ar = ar2;
12771
12772 extent = build_one_cst (gfc_array_index_type);
12773 for (i = 0; i < ar->dimen; i++)
12774 {
12775 gfc_init_se (&argse, NULL);
12776 gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
12777 gfc_add_block_to_block (&argse.pre, &argse.pre);
12778 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
12779 tmp = fold_build2_loc (input_location, MINUS_EXPR,
12780 TREE_TYPE (lbound), argse.expr, lbound);
12781 tmp = fold_build2_loc (input_location, MULT_EXPR,
12782 TREE_TYPE (tmp), extent, tmp);
12783 index = fold_build2_loc (input_location, PLUS_EXPR,
12784 TREE_TYPE (tmp), index, tmp);
12785 if (i < ar->dimen - 1)
12786 {
12787 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
12788 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
12789 extent = fold_build2_loc (input_location, MULT_EXPR,
12790 TREE_TYPE (tmp), extent, tmp);
12791 }
12792 }
12793 }
12794
12795 if (count != null_pointer_node && TREE_TYPE (count) != integer_type_node)
12796 {
12797 count2 = count;
12798 count = gfc_create_var (integer_type_node, "count");
12799 }
12800
12801 if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
12802 {
12803 stat2 = stat;
12804 stat = gfc_create_var (integer_type_node, "stat");
12805 }
12806
12807 index = fold_convert (size_type_node, index);
12808 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_query, 5,
12809 token, index, image_index, count
12810 ? gfc_build_addr_expr (NULL, count) : count,
12811 stat != null_pointer_node
12812 ? gfc_build_addr_expr (NULL, stat) : stat);
12813 gfc_add_expr_to_block (&se.pre, tmp);
12814
12815 if (count2 != NULL_TREE)
12816 gfc_add_modify (&se.pre, count2,
12817 fold_convert (TREE_TYPE (count2), count));
12818
12819 if (stat2 != NULL_TREE)
12820 gfc_add_modify (&se.pre, stat2,
12821 fold_convert (TREE_TYPE (stat2), stat));
12822
12823 return gfc_finish_block (&se.pre);
12824 }
12825
12826 gfc_init_se (&argse, NULL);
12827 gfc_conv_expr_val (&argse, code->ext.actual->expr);
12828 gfc_add_modify (&se.pre, count, fold_convert (TREE_TYPE (count), argse.expr));
12829
12830 if (stat != NULL_TREE)
12831 gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
12832
12833 return gfc_finish_block (&se.pre);
12834 }
12835
12836
12837 /* This is a peculiar case because of the need to do dependency checking.
12838 It is called via trans-stmt.cc(gfc_trans_call), where it is picked out as
12839 a special case and this function called instead of
12840 gfc_conv_procedure_call. */
12841 void
12842 gfc_conv_intrinsic_mvbits (gfc_se *se, gfc_actual_arglist *actual_args,
12843 gfc_loopinfo *loop)
12844 {
12845 gfc_actual_arglist *actual;
12846 gfc_se argse[5];
12847 gfc_expr *arg[5];
12848 gfc_ss *lss;
12849 int n;
12850
12851 tree from, frompos, len, to, topos;
12852 tree lenmask, oldbits, newbits, bitsize;
12853 tree type, utype, above, mask1, mask2;
12854
12855 if (loop)
12856 lss = loop->ss;
12857 else
12858 lss = gfc_ss_terminator;
12859
12860 actual = actual_args;
12861 for (n = 0; n < 5; n++, actual = actual->next)
12862 {
12863 arg[n] = actual->expr;
12864 gfc_init_se (&argse[n], NULL);
12865
12866 if (lss != gfc_ss_terminator)
12867 {
12868 gfc_copy_loopinfo_to_se (&argse[n], loop);
12869 /* Find the ss for the expression if it is there. */
12870 argse[n].ss = lss;
12871 gfc_mark_ss_chain_used (lss, 1);
12872 }
12873
12874 gfc_conv_expr (&argse[n], arg[n]);
12875
12876 if (loop)
12877 lss = argse[n].ss;
12878 }
12879
12880 from = argse[0].expr;
12881 frompos = argse[1].expr;
12882 len = argse[2].expr;
12883 to = argse[3].expr;
12884 topos = argse[4].expr;
12885
12886 /* The type of the result (TO). */
12887 type = TREE_TYPE (to);
12888 bitsize = build_int_cst (integer_type_node, TYPE_PRECISION (type));
12889
12890 /* Optionally generate code for runtime argument check. */
12891 if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
12892 {
12893 tree nbits, below, ccond;
12894 tree fp = fold_convert (long_integer_type_node, frompos);
12895 tree ln = fold_convert (long_integer_type_node, len);
12896 tree tp = fold_convert (long_integer_type_node, topos);
12897 below = fold_build2_loc (input_location, LT_EXPR,
12898 logical_type_node, frompos,
12899 build_int_cst (TREE_TYPE (frompos), 0));
12900 above = fold_build2_loc (input_location, GT_EXPR,
12901 logical_type_node, frompos,
12902 fold_convert (TREE_TYPE (frompos), bitsize));
12903 ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
12904 logical_type_node, below, above);
12905 gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
12906 &arg[1]->where,
12907 "FROMPOS argument (%ld) out of range 0:%d "
12908 "in intrinsic MVBITS", fp, bitsize);
12909 below = fold_build2_loc (input_location, LT_EXPR,
12910 logical_type_node, len,
12911 build_int_cst (TREE_TYPE (len), 0));
12912 above = fold_build2_loc (input_location, GT_EXPR,
12913 logical_type_node, len,
12914 fold_convert (TREE_TYPE (len), bitsize));
12915 ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
12916 logical_type_node, below, above);
12917 gfc_trans_runtime_check (true, false, ccond, &argse[2].pre,
12918 &arg[2]->where,
12919 "LEN argument (%ld) out of range 0:%d "
12920 "in intrinsic MVBITS", ln, bitsize);
12921 below = fold_build2_loc (input_location, LT_EXPR,
12922 logical_type_node, topos,
12923 build_int_cst (TREE_TYPE (topos), 0));
12924 above = fold_build2_loc (input_location, GT_EXPR,
12925 logical_type_node, topos,
12926 fold_convert (TREE_TYPE (topos), bitsize));
12927 ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
12928 logical_type_node, below, above);
12929 gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
12930 &arg[4]->where,
12931 "TOPOS argument (%ld) out of range 0:%d "
12932 "in intrinsic MVBITS", tp, bitsize);
12933
12934 /* The tests above ensure that FROMPOS, LEN and TOPOS fit into short
12935 integers. Additions below cannot overflow. */
12936 nbits = fold_convert (long_integer_type_node, bitsize);
12937 above = fold_build2_loc (input_location, PLUS_EXPR,
12938 long_integer_type_node, fp, ln);
12939 ccond = fold_build2_loc (input_location, GT_EXPR,
12940 logical_type_node, above, nbits);
12941 gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
12942 &arg[1]->where,
12943 "FROMPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
12944 "in intrinsic MVBITS", fp, ln, bitsize);
12945 above = fold_build2_loc (input_location, PLUS_EXPR,
12946 long_integer_type_node, tp, ln);
12947 ccond = fold_build2_loc (input_location, GT_EXPR,
12948 logical_type_node, above, nbits);
12949 gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
12950 &arg[4]->where,
12951 "TOPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
12952 "in intrinsic MVBITS", tp, ln, bitsize);
12953 }
12954
12955 for (n = 0; n < 5; n++)
12956 {
12957 gfc_add_block_to_block (&se->pre, &argse[n].pre);
12958 gfc_add_block_to_block (&se->post, &argse[n].post);
12959 }
12960
12961 /* lenmask = (LEN >= bit_size (TYPE)) ? ~(TYPE)0 : ((TYPE)1 << LEN) - 1 */
12962 above = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
12963 len, fold_convert (TREE_TYPE (len), bitsize));
12964 mask1 = build_int_cst (type, -1);
12965 mask2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
12966 build_int_cst (type, 1), len);
12967 mask2 = fold_build2_loc (input_location, MINUS_EXPR, type,
12968 mask2, build_int_cst (type, 1));
12969 lenmask = fold_build3_loc (input_location, COND_EXPR, type,
12970 above, mask1, mask2);
12971
12972 /* newbits = (((UTYPE)(FROM) >> FROMPOS) & lenmask) << TOPOS.
12973 * For valid frompos+len <= bit_size(FROM) the conversion to unsigned is
12974 * not strictly necessary; artificial bits from rshift will be masked. */
12975 utype = unsigned_type_for (type);
12976 newbits = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
12977 fold_convert (utype, from), frompos);
12978 newbits = fold_build2_loc (input_location, BIT_AND_EXPR, type,
12979 fold_convert (type, newbits), lenmask);
12980 newbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
12981 newbits, topos);
12982
12983 /* oldbits = TO & (~(lenmask << TOPOS)). */
12984 oldbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
12985 lenmask, topos);
12986 oldbits = fold_build1_loc (input_location, BIT_NOT_EXPR, type, oldbits);
12987 oldbits = fold_build2_loc (input_location, BIT_AND_EXPR, type, oldbits, to);
12988
12989 /* TO = newbits | oldbits. */
12990 se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
12991 oldbits, newbits);
12992
12993 /* Return the assignment. */
12994 se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
12995 void_type_node, to, se->expr);
12996 }
12997
12998
12999 static tree
13000 conv_intrinsic_move_alloc (gfc_code *code)
13001 {
13002 stmtblock_t block;
13003 gfc_expr *from_expr, *to_expr;
13004 gfc_se from_se, to_se;
13005 tree tmp, to_tree, from_tree;
13006 bool coarray, from_is_class, from_is_scalar;
13007
13008 gfc_start_block (&block);
13009
13010 from_expr = code->ext.actual->expr;
13011 to_expr = code->ext.actual->next->expr;
13012
13013 gfc_init_se (&from_se, NULL);
13014 gfc_init_se (&to_se, NULL);
13015
13016 gcc_assert (from_expr->ts.type != BT_CLASS || to_expr->ts.type == BT_CLASS);
13017 coarray = from_expr->corank != 0;
13018
13019 from_is_class = from_expr->ts.type == BT_CLASS;
13020 from_is_scalar = from_expr->rank == 0 && !coarray;
13021 if (to_expr->ts.type == BT_CLASS || from_is_scalar)
13022 {
13023 from_se.want_pointer = 1;
13024 if (from_is_scalar)
13025 gfc_conv_expr (&from_se, from_expr);
13026 else
13027 gfc_conv_expr_descriptor (&from_se, from_expr);
13028 if (from_is_class)
13029 from_tree = gfc_class_data_get (from_se.expr);
13030 else
13031 {
13032 gfc_symbol *vtab;
13033 from_tree = from_se.expr;
13034
13035 if (to_expr->ts.type == BT_CLASS)
13036 {
13037 vtab = gfc_find_vtab (&from_expr->ts);
13038 gcc_assert (vtab);
13039 from_se.expr = gfc_get_symbol_decl (vtab);
13040 }
13041 }
13042 gfc_add_block_to_block (&block, &from_se.pre);
13043
13044 to_se.want_pointer = 1;
13045 if (to_expr->rank == 0)
13046 gfc_conv_expr (&to_se, to_expr);
13047 else
13048 gfc_conv_expr_descriptor (&to_se, to_expr);
13049 if (to_expr->ts.type == BT_CLASS)
13050 to_tree = gfc_class_data_get (to_se.expr);
13051 else
13052 to_tree = to_se.expr;
13053 gfc_add_block_to_block (&block, &to_se.pre);
13054
13055 /* Deallocate "to". */
13056 if (to_expr->rank == 0)
13057 {
13058 tmp
13059 = gfc_deallocate_scalar_with_status (to_tree, NULL_TREE, NULL_TREE,
13060 true, to_expr, to_expr->ts);
13061 gfc_add_expr_to_block (&block, tmp);
13062 }
13063
13064 if (from_is_scalar)
13065 {
13066 /* Assign (_data) pointers. */
13067 gfc_add_modify_loc (input_location, &block, to_tree,
13068 fold_convert (TREE_TYPE (to_tree), from_tree));
13069
13070 /* Set "from" to NULL. */
13071 gfc_add_modify_loc (input_location, &block, from_tree,
13072 fold_convert (TREE_TYPE (from_tree),
13073 null_pointer_node));
13074
13075 gfc_add_block_to_block (&block, &from_se.post);
13076 }
13077 gfc_add_block_to_block (&block, &to_se.post);
13078
13079 /* Set _vptr. */
13080 if (to_expr->ts.type == BT_CLASS)
13081 {
13082 gfc_class_set_vptr (&block, to_se.expr, from_se.expr);
13083 if (from_is_class)
13084 gfc_reset_vptr (&block, from_expr);
13085 if (UNLIMITED_POLY (to_expr))
13086 {
13087 tree to_len = gfc_class_len_get (to_se.class_container);
13088 tmp = from_expr->ts.type == BT_CHARACTER && from_se.string_length
13089 ? from_se.string_length
13090 : size_zero_node;
13091 gfc_add_modify_loc (input_location, &block, to_len,
13092 fold_convert (TREE_TYPE (to_len), tmp));
13093 }
13094 }
13095
13096 if (from_is_scalar)
13097 {
13098 if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13099 {
13100 gfc_add_modify_loc (input_location, &block, to_se.string_length,
13101 fold_convert (TREE_TYPE (to_se.string_length),
13102 from_se.string_length));
13103 if (from_expr->ts.deferred)
13104 gfc_add_modify_loc (
13105 input_location, &block, from_se.string_length,
13106 build_int_cst (TREE_TYPE (from_se.string_length), 0));
13107 }
13108 if (UNLIMITED_POLY (from_expr))
13109 gfc_reset_len (&block, from_expr);
13110
13111 return gfc_finish_block (&block);
13112 }
13113
13114 gfc_init_se (&to_se, NULL);
13115 gfc_init_se (&from_se, NULL);
13116 }
13117
13118 /* Deallocate "to". */
13119 if (from_expr->rank == 0)
13120 {
13121 to_se.want_coarray = 1;
13122 from_se.want_coarray = 1;
13123 }
13124 gfc_conv_expr_descriptor (&to_se, to_expr);
13125 gfc_conv_expr_descriptor (&from_se, from_expr);
13126
13127 /* For coarrays, call SYNC ALL if TO is already deallocated as MOVE_ALLOC
13128 is an image control "statement", cf. IR F08/0040 in 12-006A. */
13129 if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
13130 {
13131 tree cond;
13132
13133 tmp = gfc_deallocate_with_status (to_se.expr, NULL_TREE, NULL_TREE,
13134 NULL_TREE, NULL_TREE, true, to_expr,
13135 GFC_CAF_COARRAY_DEALLOCATE_ONLY);
13136 gfc_add_expr_to_block (&block, tmp);
13137
13138 tmp = gfc_conv_descriptor_data_get (to_se.expr);
13139 cond = fold_build2_loc (input_location, EQ_EXPR,
13140 logical_type_node, tmp,
13141 fold_convert (TREE_TYPE (tmp),
13142 null_pointer_node));
13143 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
13144 3, null_pointer_node, null_pointer_node,
13145 integer_zero_node);
13146
13147 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
13148 tmp, build_empty_stmt (input_location));
13149 gfc_add_expr_to_block (&block, tmp);
13150 }
13151 else
13152 {
13153 if (to_expr->ts.type == BT_DERIVED
13154 && to_expr->ts.u.derived->attr.alloc_comp)
13155 {
13156 tmp = gfc_deallocate_alloc_comp (to_expr->ts.u.derived,
13157 to_se.expr, to_expr->rank);
13158 gfc_add_expr_to_block (&block, tmp);
13159 }
13160
13161 tmp = gfc_deallocate_with_status (to_se.expr, NULL_TREE, NULL_TREE,
13162 NULL_TREE, NULL_TREE, true, to_expr,
13163 GFC_CAF_COARRAY_NOCOARRAY);
13164 gfc_add_expr_to_block (&block, tmp);
13165 }
13166
13167 /* Copy the array descriptor data. */
13168 gfc_add_modify_loc (input_location, &block, to_se.expr, from_se.expr);
13169
13170 /* Set "from" to NULL. */
13171 tmp = gfc_conv_descriptor_data_get (from_se.expr);
13172 gfc_add_modify_loc (input_location, &block, tmp,
13173 fold_convert (TREE_TYPE (tmp), null_pointer_node));
13174
13175
13176 if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13177 {
13178 gfc_add_modify_loc (input_location, &block, to_se.string_length,
13179 fold_convert (TREE_TYPE (to_se.string_length),
13180 from_se.string_length));
13181 if (from_expr->ts.deferred)
13182 gfc_add_modify_loc (input_location, &block, from_se.string_length,
13183 build_int_cst (TREE_TYPE (from_se.string_length), 0));
13184 }
13185
13186 return gfc_finish_block (&block);
13187 }
13188
13189
13190 tree
13191 gfc_conv_intrinsic_subroutine (gfc_code *code)
13192 {
13193 tree res;
13194
13195 gcc_assert (code->resolved_isym);
13196
13197 switch (code->resolved_isym->id)
13198 {
13199 case GFC_ISYM_MOVE_ALLOC:
13200 res = conv_intrinsic_move_alloc (code);
13201 break;
13202
13203 case GFC_ISYM_ATOMIC_CAS:
13204 res = conv_intrinsic_atomic_cas (code);
13205 break;
13206
13207 case GFC_ISYM_ATOMIC_ADD:
13208 case GFC_ISYM_ATOMIC_AND:
13209 case GFC_ISYM_ATOMIC_DEF:
13210 case GFC_ISYM_ATOMIC_OR:
13211 case GFC_ISYM_ATOMIC_XOR:
13212 case GFC_ISYM_ATOMIC_FETCH_ADD:
13213 case GFC_ISYM_ATOMIC_FETCH_AND:
13214 case GFC_ISYM_ATOMIC_FETCH_OR:
13215 case GFC_ISYM_ATOMIC_FETCH_XOR:
13216 res = conv_intrinsic_atomic_op (code);
13217 break;
13218
13219 case GFC_ISYM_ATOMIC_REF:
13220 res = conv_intrinsic_atomic_ref (code);
13221 break;
13222
13223 case GFC_ISYM_EVENT_QUERY:
13224 res = conv_intrinsic_event_query (code);
13225 break;
13226
13227 case GFC_ISYM_C_F_POINTER:
13228 case GFC_ISYM_C_F_PROCPOINTER:
13229 res = conv_isocbinding_subroutine (code);
13230 break;
13231
13232 case GFC_ISYM_CAF_SEND:
13233 res = conv_caf_send_to_remote (code);
13234 break;
13235
13236 case GFC_ISYM_CAF_SENDGET:
13237 res = conv_caf_sendget (code);
13238 break;
13239
13240 case GFC_ISYM_CO_BROADCAST:
13241 case GFC_ISYM_CO_MIN:
13242 case GFC_ISYM_CO_MAX:
13243 case GFC_ISYM_CO_REDUCE:
13244 case GFC_ISYM_CO_SUM:
13245 res = conv_co_collective (code);
13246 break;
13247
13248 case GFC_ISYM_FREE:
13249 res = conv_intrinsic_free (code);
13250 break;
13251
13252 case GFC_ISYM_RANDOM_INIT:
13253 res = conv_intrinsic_random_init (code);
13254 break;
13255
13256 case GFC_ISYM_KILL:
13257 res = conv_intrinsic_kill_sub (code);
13258 break;
13259
13260 case GFC_ISYM_MVBITS:
13261 res = NULL_TREE;
13262 break;
13263
13264 case GFC_ISYM_SYSTEM_CLOCK:
13265 res = conv_intrinsic_system_clock (code);
13266 break;
13267
13268 default:
13269 res = NULL_TREE;
13270 break;
13271 }
13272
13273 return res;
13274 }
13275
13276 #include "gt-fortran-trans-intrinsic.h"