LCOV - code coverage report
Current view: top level - gcc/fortran - trans-intrinsic.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.7 % 7007 6633
Test Date: 2026-05-11 19:44:49 Functions: 98.2 % 170 167
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Intrinsic translation
       2              :    Copyright (C) 2002-2026 Free Software Foundation, Inc.
       3              :    Contributed by Paul Brook <paul@nowt.org>
       4              :    and Steven Bosscher <s.bosscher@student.tudelft.nl>
       5              : 
       6              : This file is part of GCC.
       7              : 
       8              : GCC is free software; you can redistribute it and/or modify it under
       9              : the terms of the GNU General Public License as published by the Free
      10              : Software Foundation; either version 3, or (at your option) any later
      11              : version.
      12              : 
      13              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      14              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      15              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      16              : for more details.
      17              : 
      18              : You should have received a copy of the GNU General Public License
      19              : along with GCC; see the file COPYING3.  If not see
      20              : <http://www.gnu.org/licenses/>.  */
      21              : 
      22              : /* trans-intrinsic.cc-- generate GENERIC trees for calls to intrinsics.  */
      23              : 
      24              : #include "config.h"
      25              : #include "system.h"
      26              : #include "coretypes.h"
      27              : #include "memmodel.h"
      28              : #include "tm.h"               /* For UNITS_PER_WORD.  */
      29              : #include "tree.h"
      30              : #include "gfortran.h"
      31              : #include "trans.h"
      32              : #include "stringpool.h"
      33              : #include "fold-const.h"
      34              : #include "internal-fn.h"
      35              : #include "tree-nested.h"
      36              : #include "stor-layout.h"
      37              : #include "toplev.h"   /* For rest_of_decl_compilation.  */
      38              : #include "arith.h"
      39              : #include "trans-const.h"
      40              : #include "trans-types.h"
      41              : #include "trans-array.h"
      42              : #include "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        11454 : builtin_decl_for_precision (enum built_in_function base_built_in,
     147              :                             int precision)
     148              : {
     149        11454 :   enum built_in_function i = END_BUILTINS;
     150              : 
     151        11454 :   gfc_intrinsic_map_t *m;
     152       490551 :   for (m = gfc_intrinsic_map; m->double_built_in != base_built_in ; m++)
     153              :     ;
     154              : 
     155        11454 :   if (precision == TYPE_PRECISION (float_type_node))
     156         5814 :     i = m->float_built_in;
     157         5640 :   else if (precision == TYPE_PRECISION (double_type_node))
     158              :     i = m->double_built_in;
     159         1695 :   else if (precision == TYPE_PRECISION (long_double_type_node)
     160         1695 :            && (!gfc_real16_is_float128
     161         1571 :                || long_double_type_node != gfc_float128_type_node))
     162         1571 :     i = m->long_double_built_in;
     163          124 :   else if (precision == TYPE_PRECISION (gfc_float128_type_node))
     164              :     {
     165              :       /* Special treatment, because it is not exactly a built-in, but
     166              :          a library function.  */
     167          124 :       return m->real16_decl;
     168              :     }
     169              : 
     170        11330 :   return (i == END_BUILTINS ? NULL_TREE : builtin_decl_explicit (i));
     171              : }
     172              : 
     173              : 
     174              : tree
     175        10415 : gfc_builtin_decl_for_float_kind (enum built_in_function double_built_in,
     176              :                                  int kind)
     177              : {
     178        10415 :   int i = gfc_validate_kind (BT_REAL, kind, false);
     179              : 
     180        10415 :   if (gfc_real_kinds[i].c_float128)
     181              :     {
     182              :       /* For _Float128, the story is a bit different, because we return
     183              :          a decl to a library function rather than a built-in.  */
     184              :       gfc_intrinsic_map_t *m;
     185        36328 :       for (m = gfc_intrinsic_map; m->double_built_in != double_built_in ; m++)
     186              :         ;
     187              : 
     188          905 :       return m->real16_decl;
     189              :     }
     190              : 
     191         9510 :   return builtin_decl_for_precision (double_built_in,
     192         9510 :                                      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        80805 : gfc_conv_intrinsic_function_args (gfc_se *se, gfc_expr *expr,
     203              :                                   tree *argarray, int nargs)
     204              : {
     205        80805 :   gfc_actual_arglist *actual;
     206        80805 :   gfc_expr *e;
     207        80805 :   gfc_intrinsic_arg  *formal;
     208        80805 :   gfc_se argse;
     209        80805 :   int curr_arg;
     210              : 
     211        80805 :   formal = expr->value.function.isym->formal;
     212        80805 :   actual = expr->value.function.actual;
     213              : 
     214       182196 :    for (curr_arg = 0; curr_arg < nargs; curr_arg++,
     215        63035 :         actual = actual->next,
     216       101391 :         formal = formal ? formal->next : NULL)
     217              :     {
     218       101391 :       gcc_assert (actual);
     219       101391 :       e = actual->expr;
     220              :       /* Skip omitted optional arguments.  */
     221       101391 :       if (!e)
     222              :         {
     223           31 :           --curr_arg;
     224           31 :           continue;
     225              :         }
     226              : 
     227              :       /* Evaluate the parameter.  This will substitute scalarized
     228              :          references automatically.  */
     229       101360 :       gfc_init_se (&argse, se);
     230              : 
     231       101360 :       if (e->ts.type == BT_CHARACTER)
     232              :         {
     233         9629 :           gfc_conv_expr (&argse, e);
     234         9629 :           gfc_conv_string_parameter (&argse);
     235         9629 :           argarray[curr_arg++] = argse.string_length;
     236         9629 :           gcc_assert (curr_arg < nargs);
     237              :         }
     238              :       else
     239        91731 :         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       101360 :       if (e->expr_type == EXPR_VARIABLE
     244        51769 :             && e->symtree->n.sym->attr.optional
     245          203 :             && formal
     246          153 :             && formal->optional)
     247           80 :         gfc_conv_missing_dummy (&argse, e, formal->ts, 0);
     248              : 
     249       101360 :       gfc_add_block_to_block (&se->pre, &argse.pre);
     250       101360 :       gfc_add_block_to_block (&se->post, &argse.post);
     251       101360 :       argarray[curr_arg] = argse.expr;
     252              :     }
     253        80805 : }
     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        55865 : gfc_intrinsic_argument_list_length (gfc_expr *expr)
     260              : {
     261        55865 :   int n = 0;
     262        55865 :   gfc_actual_arglist *actual;
     263              : 
     264       126923 :   for (actual = expr->value.function.actual; actual; actual = actual->next)
     265              :     {
     266        71058 :       if (!actual->expr)
     267         6334 :         continue;
     268              : 
     269        64724 :       if (actual->expr->ts.type == BT_CHARACTER)
     270         4549 :         n += 2;
     271              :       else
     272        60175 :         n++;
     273              :     }
     274              : 
     275        55865 :   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        39742 : gfc_conv_intrinsic_conversion (gfc_se * se, gfc_expr * expr)
     284              : {
     285        39742 :   tree type;
     286        39742 :   tree *args;
     287        39742 :   int nargs;
     288              : 
     289        39742 :   nargs = gfc_intrinsic_argument_list_length (expr);
     290        39742 :   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        39742 :   type = gfc_typenode_for_spec (&expr->ts);
     296        39742 :   gcc_assert (expr->value.function.actual->expr);
     297        39742 :   gfc_conv_intrinsic_function_args (se, expr, args, nargs);
     298              : 
     299              :   /* Conversion between character kinds involves a call to a library
     300              :      function.  */
     301        39742 :   if (expr->ts.type == BT_CHARACTER)
     302              :     {
     303          248 :       tree fndecl, var, addr, tmp;
     304              : 
     305          248 :       if (expr->ts.kind == 1
     306           97 :           && expr->value.function.actual->expr->ts.kind == 4)
     307           97 :         fndecl = gfor_fndecl_convert_char4_to_char1;
     308          151 :       else if (expr->ts.kind == 4
     309          151 :                && expr->value.function.actual->expr->ts.kind == 1)
     310          151 :         fndecl = gfor_fndecl_convert_char1_to_char4;
     311              :       else
     312            0 :         gcc_unreachable ();
     313              : 
     314              :       /* Create the variable storing the converted value.  */
     315          248 :       type = gfc_get_pchar_type (expr->ts.kind);
     316          248 :       var = gfc_create_var (type, "str");
     317          248 :       addr = gfc_build_addr_expr (build_pointer_type (type), var);
     318              : 
     319              :       /* Call the library function that will perform the conversion.  */
     320          248 :       gcc_assert (nargs >= 2);
     321          248 :       tmp = build_call_expr_loc (input_location,
     322              :                              fndecl, 3, addr, args[0], args[1]);
     323          248 :       gfc_add_expr_to_block (&se->pre, tmp);
     324              : 
     325              :       /* Free the temporary afterwards.  */
     326          248 :       tmp = gfc_call_free (var);
     327          248 :       gfc_add_expr_to_block (&se->post, tmp);
     328              : 
     329          248 :       se->expr = var;
     330          248 :       se->string_length = args[0];
     331              : 
     332          248 :       return;
     333              :     }
     334              : 
     335              :   /* Conversion from complex to non-complex involves taking the real
     336              :      component of the value.  */
     337        39494 :   if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
     338        39494 :       && expr->ts.type != BT_COMPLEX)
     339              :     {
     340          583 :       tree artype;
     341              : 
     342          583 :       artype = TREE_TYPE (TREE_TYPE (args[0]));
     343          583 :       args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
     344              :                                  args[0]);
     345              :     }
     346              : 
     347        39494 :   se->expr = convert (type, args[0]);
     348              : }
     349              : 
     350              : /* This is needed because the gcc backend only implements
     351              :    FIX_TRUNC_EXPR, which is the same as INT() in Fortran.
     352              :    FLOOR(x) = INT(x) <= x ? INT(x) : INT(x) - 1
     353              :    Similarly for CEILING.  */
     354              : 
     355              : static tree
     356          132 : build_fixbound_expr (stmtblock_t * pblock, tree arg, tree type, int up)
     357              : {
     358          132 :   tree tmp;
     359          132 :   tree cond;
     360          132 :   tree argtype;
     361          132 :   tree intval;
     362              : 
     363          132 :   argtype = TREE_TYPE (arg);
     364          132 :   arg = gfc_evaluate_now (arg, pblock);
     365              : 
     366          132 :   intval = convert (type, arg);
     367          132 :   intval = gfc_evaluate_now (intval, pblock);
     368              : 
     369          132 :   tmp = convert (argtype, intval);
     370          248 :   cond = fold_build2_loc (input_location, up ? GE_EXPR : LE_EXPR,
     371              :                           logical_type_node, tmp, arg);
     372              : 
     373          248 :   tmp = fold_build2_loc (input_location, up ? PLUS_EXPR : MINUS_EXPR, type,
     374              :                          intval, build_int_cst (type, 1));
     375          132 :   tmp = fold_build3_loc (input_location, COND_EXPR, type, cond, intval, tmp);
     376          132 :   return tmp;
     377              : }
     378              : 
     379              : 
     380              : /* Round to nearest integer, away from zero.  */
     381              : 
     382              : static tree
     383          516 : build_round_expr (tree arg, tree restype)
     384              : {
     385          516 :   tree argtype;
     386          516 :   tree fn;
     387          516 :   int argprec, resprec;
     388              : 
     389          516 :   argtype = TREE_TYPE (arg);
     390          516 :   argprec = TYPE_PRECISION (argtype);
     391          516 :   resprec = TYPE_PRECISION (restype);
     392              : 
     393              :   /* Depending on the type of the result, choose the int intrinsic (iround,
     394              :      available only as a builtin, therefore cannot use it for _Float128), long
     395              :      int intrinsic (lround family) or long long intrinsic (llround).  If we
     396              :      don't have an appropriate function that converts directly to the integer
     397              :      type (such as kind == 16), just use ROUND, and then convert the result to
     398              :      an integer.  We might also need to convert the result afterwards.  */
     399          516 :   if (resprec <= INT_TYPE_SIZE
     400          516 :       && argprec <= TYPE_PRECISION (long_double_type_node))
     401          458 :     fn = builtin_decl_for_precision (BUILT_IN_IROUND, argprec);
     402           62 :   else if (resprec <= LONG_TYPE_SIZE)
     403           46 :     fn = builtin_decl_for_precision (BUILT_IN_LROUND, argprec);
     404           12 :   else if (resprec <= LONG_LONG_TYPE_SIZE)
     405            0 :     fn = builtin_decl_for_precision (BUILT_IN_LLROUND, argprec);
     406           12 :   else if (resprec >= argprec)
     407           12 :     fn = builtin_decl_for_precision (BUILT_IN_ROUND, argprec);
     408              :   else
     409            0 :     gcc_unreachable ();
     410              : 
     411          516 :   return convert (restype, build_call_expr_loc (input_location,
     412          516 :                                                 fn, 1, arg));
     413              : }
     414              : 
     415              : 
     416              : /* Convert a real to an integer using a specific rounding mode.
     417              :    Ideally we would just build the corresponding GENERIC node,
     418              :    however the RTL expander only actually supports FIX_TRUNC_EXPR.  */
     419              : 
     420              : static tree
     421         1603 : build_fix_expr (stmtblock_t * pblock, tree arg, tree type,
     422              :                enum rounding_mode op)
     423              : {
     424         1603 :   switch (op)
     425              :     {
     426          116 :     case RND_FLOOR:
     427          116 :       return build_fixbound_expr (pblock, arg, type, 0);
     428              : 
     429           16 :     case RND_CEIL:
     430           16 :       return build_fixbound_expr (pblock, arg, type, 1);
     431              : 
     432          162 :     case RND_ROUND:
     433          162 :       return build_round_expr (arg, type);
     434              : 
     435         1309 :     case RND_TRUNC:
     436         1309 :       return fold_build1_loc (input_location, FIX_TRUNC_EXPR, type, arg);
     437              : 
     438            0 :     default:
     439            0 :       gcc_unreachable ();
     440              :     }
     441              : }
     442              : 
     443              : 
     444              : /* Round a real value using the specified rounding mode.
     445              :    We use a temporary integer of that same kind size as the result.
     446              :    Values larger than those that can be represented by this kind are
     447              :    unchanged, as they will not be accurate enough to represent the
     448              :    rounding.
     449              :     huge = HUGE (KIND (a))
     450              :     aint (a) = ((a > huge) || (a < -huge)) ? a : (real)(int)a
     451              :    */
     452              : 
     453              : static void
     454          220 : gfc_conv_intrinsic_aint (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
     455              : {
     456          220 :   tree type;
     457          220 :   tree itype;
     458          220 :   tree arg[2];
     459          220 :   tree tmp;
     460          220 :   tree cond;
     461          220 :   tree decl;
     462          220 :   mpfr_t huge;
     463          220 :   int n, nargs;
     464          220 :   int kind;
     465              : 
     466          220 :   kind = expr->ts.kind;
     467          220 :   nargs = gfc_intrinsic_argument_list_length (expr);
     468              : 
     469          220 :   decl = NULL_TREE;
     470              :   /* We have builtin functions for some cases.  */
     471          220 :   switch (op)
     472              :     {
     473           74 :     case RND_ROUND:
     474           74 :       decl = gfc_builtin_decl_for_float_kind (BUILT_IN_ROUND, kind);
     475           74 :       break;
     476              : 
     477          146 :     case RND_TRUNC:
     478          146 :       decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, kind);
     479          146 :       break;
     480              : 
     481            0 :     default:
     482            0 :       gcc_unreachable ();
     483              :     }
     484              : 
     485              :   /* Evaluate the argument.  */
     486          220 :   gcc_assert (expr->value.function.actual->expr);
     487          220 :   gfc_conv_intrinsic_function_args (se, expr, arg, nargs);
     488              : 
     489              :   /* Use a builtin function if one exists.  */
     490          220 :   if (decl != NULL_TREE)
     491              :     {
     492          220 :       se->expr = build_call_expr_loc (input_location, decl, 1, arg[0]);
     493          220 :       return;
     494              :     }
     495              : 
     496              :   /* This code is probably redundant, but we'll keep it lying around just
     497              :      in case.  */
     498            0 :   type = gfc_typenode_for_spec (&expr->ts);
     499            0 :   arg[0] = gfc_evaluate_now (arg[0], &se->pre);
     500              : 
     501              :   /* Test if the value is too large to handle sensibly.  */
     502            0 :   gfc_set_model_kind (kind);
     503            0 :   mpfr_init (huge);
     504            0 :   n = gfc_validate_kind (BT_INTEGER, kind, false);
     505            0 :   mpfr_set_z (huge, gfc_integer_kinds[n].huge, GFC_RND_MODE);
     506            0 :   tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
     507            0 :   cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, arg[0],
     508              :                           tmp);
     509              : 
     510            0 :   mpfr_neg (huge, huge, GFC_RND_MODE);
     511            0 :   tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
     512            0 :   tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, arg[0],
     513              :                          tmp);
     514            0 :   cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
     515              :                           cond, tmp);
     516            0 :   itype = gfc_get_int_type (kind);
     517              : 
     518            0 :   tmp = build_fix_expr (&se->pre, arg[0], itype, op);
     519            0 :   tmp = convert (type, tmp);
     520            0 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
     521              :                               arg[0]);
     522            0 :   mpfr_clear (huge);
     523              : }
     524              : 
     525              : 
     526              : /* Convert to an integer using the specified rounding mode.  */
     527              : 
     528              : static void
     529         3130 : gfc_conv_intrinsic_int (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
     530              : {
     531         3130 :   tree type;
     532         3130 :   tree *args;
     533         3130 :   int nargs;
     534              : 
     535         3130 :   nargs = gfc_intrinsic_argument_list_length (expr);
     536         3130 :   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         3130 :   type = gfc_typenode_for_spec (&expr->ts);
     541         3130 :   gcc_assert (expr->value.function.actual->expr);
     542         3130 :   gfc_conv_intrinsic_function_args (se, expr, args, nargs);
     543              : 
     544         3130 :   if (TREE_CODE (TREE_TYPE (args[0])) == INTEGER_TYPE)
     545              :     {
     546              :       /* Conversion to a different integer kind.  */
     547         1527 :       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         1603 :       if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
     554         1603 :           && expr->ts.type != BT_COMPLEX)
     555              :         {
     556          192 :           tree artype;
     557              : 
     558          192 :           artype = TREE_TYPE (TREE_TYPE (args[0]));
     559          192 :           args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
     560              :                                      args[0]);
     561              :         }
     562              : 
     563         1603 :       se->expr = build_fix_expr (&se->pre, args[0], type, op);
     564              :     }
     565         3130 : }
     566              : 
     567              : 
     568              : /* Get the imaginary component of a value.  */
     569              : 
     570              : static void
     571          440 : gfc_conv_intrinsic_imagpart (gfc_se * se, gfc_expr * expr)
     572              : {
     573          440 :   tree arg;
     574              : 
     575          440 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
     576          440 :   se->expr = fold_build1_loc (input_location, IMAGPART_EXPR,
     577          440 :                               TREE_TYPE (TREE_TYPE (arg)), arg);
     578          440 : }
     579              : 
     580              : 
     581              : /* Get the complex conjugate of a value.  */
     582              : 
     583              : static void
     584          257 : gfc_conv_intrinsic_conjg (gfc_se * se, gfc_expr * expr)
     585              : {
     586          257 :   tree arg;
     587              : 
     588          257 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
     589          257 :   se->expr = fold_build1_loc (input_location, CONJ_EXPR, TREE_TYPE (arg), arg);
     590          257 : }
     591              : 
     592              : 
     593              : 
     594              : static tree
     595       660261 : define_quad_builtin (const char *name, tree type, bool is_const)
     596              : {
     597       660261 :   tree fndecl;
     598       660261 :   fndecl = build_decl (input_location, FUNCTION_DECL, get_identifier (name),
     599              :                        type);
     600              : 
     601              :   /* Mark the decl as external.  */
     602       660261 :   DECL_EXTERNAL (fndecl) = 1;
     603       660261 :   TREE_PUBLIC (fndecl) = 1;
     604              : 
     605              :   /* Mark it __attribute__((const)).  */
     606       660261 :   TREE_READONLY (fndecl) = is_const;
     607              : 
     608       660261 :   rest_of_decl_compilation (fndecl, 1, 0);
     609              : 
     610       660261 :   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     45155060 : add_simd_flag_for_built_in (tree fndecl)
     618              : {
     619     45155060 :   if (gfc_vectorized_builtins == NULL
     620     18131880 :       || fndecl == NULL_TREE)
     621     37318400 :     return;
     622              : 
     623      7836660 :   const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
     624      7836660 :   int *clauses = gfc_vectorized_builtins->get (name);
     625      7836660 :   if (clauses)
     626              :     {
     627      4916508 :       for (unsigned i = 0; i < 3; i++)
     628      3687381 :         if (*clauses & (1 << i))
     629              :           {
     630      1229132 :             gfc_simd_clause simd_type = (gfc_simd_clause)*clauses;
     631      1229132 :             tree omp_clause = NULL_TREE;
     632      1229132 :             if (simd_type == SIMD_NONE)
     633              :               ; /* No SIMD clause.  */
     634              :             else
     635              :               {
     636      1229132 :                 omp_clause_code code
     637              :                   = (simd_type == SIMD_INBRANCH
     638      1229132 :                      ? OMP_CLAUSE_INBRANCH : OMP_CLAUSE_NOTINBRANCH);
     639      1229132 :                 omp_clause = build_omp_clause (UNKNOWN_LOCATION, code);
     640      1229132 :                 omp_clause = build_tree_list (NULL_TREE, omp_clause);
     641              :               }
     642              : 
     643      1229132 :             DECL_ATTRIBUTES (fndecl)
     644      2458264 :               = tree_cons (get_identifier ("omp declare simd"), omp_clause,
     645      1229132 :                            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        76534 : gfc_adjust_builtins (void)
     655              : {
     656        76534 :   gfc_intrinsic_map_t *m;
     657      4592040 :   for (m = gfc_intrinsic_map;
     658      4592040 :        m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
     659              :     {
     660      4515506 :       add_simd_flag_for_built_in (m->real4_decl);
     661      4515506 :       add_simd_flag_for_built_in (m->complex4_decl);
     662      4515506 :       add_simd_flag_for_built_in (m->real8_decl);
     663      4515506 :       add_simd_flag_for_built_in (m->complex8_decl);
     664      4515506 :       add_simd_flag_for_built_in (m->real10_decl);
     665      4515506 :       add_simd_flag_for_built_in (m->complex10_decl);
     666      4515506 :       add_simd_flag_for_built_in (m->real16_decl);
     667      4515506 :       add_simd_flag_for_built_in (m->complex16_decl);
     668      4515506 :       add_simd_flag_for_built_in (m->real16_decl);
     669      4515506 :       add_simd_flag_for_built_in (m->complex16_decl);
     670              :     }
     671              : 
     672              :   /* Release all strings.  */
     673        76534 :   if (gfc_vectorized_builtins != NULL)
     674              :     {
     675      1690051 :       for (hash_map<nofree_string_hash, int>::iterator it
     676        30732 :            = gfc_vectorized_builtins->begin ();
     677      1690051 :            it != gfc_vectorized_builtins->end (); ++it)
     678      1659319 :         free (const_cast<char *> ((*it).first));
     679              : 
     680        61464 :       delete gfc_vectorized_builtins;
     681        30732 :       gfc_vectorized_builtins = NULL;
     682              :     }
     683        76534 : }
     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        31441 : gfc_build_intrinsic_lib_fndecls (void)
     690              : {
     691        31441 :   gfc_intrinsic_map_t *m;
     692        31441 :   tree quad_decls[END_BUILTINS + 1];
     693              : 
     694        31441 :   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        31441 :     tree type, complex_type, func_1, func_2, func_3, func_cabs, func_frexp;
     701        31441 :     tree func_iround, func_lround, func_llround, func_scalbn, func_cpow;
     702              : 
     703        31441 :     memset (quad_decls, 0, sizeof(tree) * (END_BUILTINS + 1));
     704              : 
     705        31441 :     type = gfc_float128_type_node;
     706        31441 :     complex_type = gfc_complex_float128_type_node;
     707              :     /* type (*) (type) */
     708        31441 :     func_1 = build_function_type_list (type, type, NULL_TREE);
     709              :     /* int (*) (type) */
     710        31441 :     func_iround = build_function_type_list (integer_type_node,
     711              :                                             type, NULL_TREE);
     712              :     /* long (*) (type) */
     713        31441 :     func_lround = build_function_type_list (long_integer_type_node,
     714              :                                             type, NULL_TREE);
     715              :     /* long long (*) (type) */
     716        31441 :     func_llround = build_function_type_list (long_long_integer_type_node,
     717              :                                              type, NULL_TREE);
     718              :     /* type (*) (type, type) */
     719        31441 :     func_2 = build_function_type_list (type, type, type, NULL_TREE);
     720              :     /* type (*) (type, type, type) */
     721        31441 :     func_3 = build_function_type_list (type, type, type, type, NULL_TREE);
     722              :     /* type (*) (type, &int) */
     723        31441 :     func_frexp
     724        31441 :       = build_function_type_list (type,
     725              :                                   type,
     726              :                                   build_pointer_type (integer_type_node),
     727              :                                   NULL_TREE);
     728              :     /* type (*) (type, int) */
     729        31441 :     func_scalbn = build_function_type_list (type,
     730              :                                             type, integer_type_node, NULL_TREE);
     731              :     /* type (*) (complex type) */
     732        31441 :     func_cabs = build_function_type_list (type, complex_type, NULL_TREE);
     733              :     /* complex type (*) (complex type, complex type) */
     734        31441 :     func_cpow
     735        31441 :       = 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        31441 :     quad_decls[BUILT_IN_SQRT]
     763        31441 :       = define_quad_builtin (gfc_real16_use_iec_60559
     764              :                              ? "sqrtf128" : "sqrtq", func_1, true);
     765              :   }
     766              : 
     767              :   /* Add GCC builtin functions.  */
     768      1855019 :   for (m = gfc_intrinsic_map;
     769      1886460 :        m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
     770              :     {
     771      1855019 :       if (m->float_built_in != END_BUILTINS)
     772      1729255 :         m->real4_decl = builtin_decl_explicit (m->float_built_in);
     773      1855019 :       if (m->complex_float_built_in != END_BUILTINS)
     774       503056 :         m->complex4_decl = builtin_decl_explicit (m->complex_float_built_in);
     775      1855019 :       if (m->double_built_in != END_BUILTINS)
     776      1729255 :         m->real8_decl = builtin_decl_explicit (m->double_built_in);
     777      1855019 :       if (m->complex_double_built_in != END_BUILTINS)
     778       503056 :         m->complex8_decl = builtin_decl_explicit (m->complex_double_built_in);
     779              : 
     780              :       /* If real(kind=10) exists, it is always long double.  */
     781      1855019 :       if (m->long_double_built_in != END_BUILTINS)
     782      1729255 :         m->real10_decl = builtin_decl_explicit (m->long_double_built_in);
     783      1855019 :       if (m->complex_long_double_built_in != END_BUILTINS)
     784       503056 :         m->complex10_decl
     785       503056 :           = builtin_decl_explicit (m->complex_long_double_built_in);
     786              : 
     787      1855019 :       if (!gfc_real16_is_float128)
     788              :         {
     789            0 :           if (m->long_double_built_in != END_BUILTINS)
     790            0 :             m->real16_decl = builtin_decl_explicit (m->long_double_built_in);
     791            0 :           if (m->complex_long_double_built_in != END_BUILTINS)
     792            0 :             m->complex16_decl
     793            0 :               = builtin_decl_explicit (m->complex_long_double_built_in);
     794              :         }
     795      1855019 :       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       660261 :           m->real16_decl = quad_decls[m->double_built_in];
     801              :         }
     802      1194758 :       else if (quad_decls[m->complex_double_built_in] != NULL_TREE)
     803              :         {
     804              :           /* Same thing for the complex ones.  */
     805            0 :           m->complex16_decl = quad_decls[m->double_built_in];
     806              :         }
     807              :     }
     808        31441 : }
     809              : 
     810              : 
     811              : /* Create a fndecl for a simple intrinsic library function.  */
     812              : 
     813              : static tree
     814         4421 : gfc_get_intrinsic_lib_fndecl (gfc_intrinsic_map_t * m, gfc_expr * expr)
     815              : {
     816         4421 :   tree type;
     817         4421 :   vec<tree, va_gc> *argtypes;
     818         4421 :   tree fndecl;
     819         4421 :   gfc_actual_arglist *actual;
     820         4421 :   tree *pdecl;
     821         4421 :   gfc_typespec *ts;
     822         4421 :   char name[GFC_MAX_SYMBOL_LEN + 3];
     823              : 
     824         4421 :   ts = &expr->ts;
     825         4421 :   if (ts->type == BT_REAL)
     826              :     {
     827         3559 :       switch (ts->kind)
     828              :         {
     829         1273 :         case 4:
     830         1273 :           pdecl = &m->real4_decl;
     831         1273 :           break;
     832         1272 :         case 8:
     833         1272 :           pdecl = &m->real8_decl;
     834         1272 :           break;
     835          572 :         case 10:
     836          572 :           pdecl = &m->real10_decl;
     837          572 :           break;
     838          442 :         case 16:
     839          442 :           pdecl = &m->real16_decl;
     840          442 :           break;
     841            0 :         default:
     842            0 :           gcc_unreachable ();
     843              :         }
     844              :     }
     845          862 :   else if (ts->type == BT_COMPLEX)
     846              :     {
     847          862 :       gcc_assert (m->complex_available);
     848              : 
     849          862 :       switch (ts->kind)
     850              :         {
     851          386 :         case 4:
     852          386 :           pdecl = &m->complex4_decl;
     853          386 :           break;
     854          405 :         case 8:
     855          405 :           pdecl = &m->complex8_decl;
     856          405 :           break;
     857           51 :         case 10:
     858           51 :           pdecl = &m->complex10_decl;
     859           51 :           break;
     860           20 :         case 16:
     861           20 :           pdecl = &m->complex16_decl;
     862           20 :           break;
     863            0 :         default:
     864            0 :           gcc_unreachable ();
     865              :         }
     866              :     }
     867              :   else
     868            0 :     gcc_unreachable ();
     869              : 
     870         4421 :   if (*pdecl)
     871         4082 :     return *pdecl;
     872              : 
     873          339 :   if (m->libm_name)
     874              :     {
     875          162 :       int n = gfc_validate_kind (BT_REAL, ts->kind, false);
     876          162 :       if (gfc_real_kinds[n].c_float)
     877            0 :         snprintf (name, sizeof (name), "%s%s%s",
     878            0 :                   ts->type == BT_COMPLEX ? "c" : "", m->name, "f");
     879          162 :       else if (gfc_real_kinds[n].c_double)
     880            0 :         snprintf (name, sizeof (name), "%s%s",
     881            0 :                   ts->type == BT_COMPLEX ? "c" : "", m->name);
     882          162 :       else if (gfc_real_kinds[n].c_long_double)
     883            0 :         snprintf (name, sizeof (name), "%s%s%s",
     884            0 :                   ts->type == BT_COMPLEX ? "c" : "", m->name, "l");
     885          162 :       else if (gfc_real_kinds[n].c_float128)
     886          162 :         snprintf (name, sizeof (name), "%s%s%s",
     887          162 :                   ts->type == BT_COMPLEX ? "c" : "", m->name,
     888          162 :                   gfc_real_kinds[n].use_iec_60559 ? "f128" : "q");
     889              :       else
     890            0 :         gcc_unreachable ();
     891              :     }
     892              :   else
     893              :     {
     894          354 :       snprintf (name, sizeof (name), PREFIX ("%s_%c%d"), m->name,
     895          177 :                 ts->type == BT_COMPLEX ? 'c' : 'r',
     896              :                 gfc_type_abi_kind (ts));
     897              :     }
     898              : 
     899          339 :   argtypes = NULL;
     900          694 :   for (actual = expr->value.function.actual; actual; actual = actual->next)
     901              :     {
     902          355 :       type = gfc_typenode_for_spec (&actual->expr->ts);
     903          355 :       vec_safe_push (argtypes, type);
     904              :     }
     905         1017 :   type = build_function_type_vec (gfc_typenode_for_spec (ts), argtypes);
     906          339 :   fndecl = build_decl (input_location,
     907              :                        FUNCTION_DECL, get_identifier (name), type);
     908              : 
     909              :   /* Mark the decl as external.  */
     910          339 :   DECL_EXTERNAL (fndecl) = 1;
     911          339 :   TREE_PUBLIC (fndecl) = 1;
     912              : 
     913              :   /* Mark it __attribute__((const)), if possible.  */
     914          339 :   TREE_READONLY (fndecl) = m->is_constant;
     915              : 
     916          339 :   rest_of_decl_compilation (fndecl, 1, 0);
     917              : 
     918          339 :   (*pdecl) = fndecl;
     919          339 :   return fndecl;
     920              : }
     921              : 
     922              : 
     923              : /* Convert an intrinsic function into an external or builtin call.  */
     924              : 
     925              : static void
     926         3875 : gfc_conv_intrinsic_lib_function (gfc_se * se, gfc_expr * expr)
     927              : {
     928         3875 :   gfc_intrinsic_map_t *m;
     929         3875 :   tree fndecl;
     930         3875 :   tree rettype;
     931         3875 :   tree *args;
     932         3875 :   unsigned int num_args;
     933         3875 :   gfc_isym_id id;
     934              : 
     935         3875 :   id = expr->value.function.isym->id;
     936              :   /* Find the entry for this function.  */
     937        79667 :   for (m = gfc_intrinsic_map;
     938        79667 :        m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
     939              :     {
     940        79667 :       if (id == m->id)
     941              :         break;
     942              :     }
     943              : 
     944         3875 :   if (m->id == GFC_ISYM_NONE)
     945              :     {
     946            0 :       gfc_internal_error ("Intrinsic function %qs (%d) not recognized",
     947              :                           expr->value.function.name, id);
     948              :     }
     949              : 
     950              :   /* Get the decl and generate the call.  */
     951         3875 :   num_args = gfc_intrinsic_argument_list_length (expr);
     952         3875 :   args = XALLOCAVEC (tree, num_args);
     953              : 
     954         3875 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
     955         3875 :   fndecl = gfc_get_intrinsic_lib_fndecl (m, expr);
     956         3875 :   rettype = TREE_TYPE (TREE_TYPE (fndecl));
     957              : 
     958         3875 :   fndecl = build_addr (fndecl);
     959         3875 :   se->expr = build_call_array_loc (input_location, rettype, fndecl, num_args, args);
     960         3875 : }
     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         1550 : gfc_trans_same_strlen_check (const char* intr_name, locus* where,
     969              :                              tree a, tree b, stmtblock_t* target)
     970              : {
     971         1550 :   tree cond;
     972         1550 :   tree name;
     973              : 
     974              :   /* If bounds-checking is disabled, do nothing.  */
     975         1550 :   if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
     976              :     return;
     977              : 
     978              :   /* Compare the two string lengths.  */
     979           94 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, a, b);
     980              : 
     981              :   /* Output the runtime-check.  */
     982           94 :   name = gfc_build_cstring_const (intr_name);
     983           94 :   name = gfc_build_addr_expr (pchar_type_node, name);
     984           94 :   gfc_trans_runtime_check (true, false, cond, target, where,
     985              :                            "Unequal character lengths (%ld/%ld) in %s",
     986              :                            fold_convert (long_integer_type_node, a),
     987              :                            fold_convert (long_integer_type_node, b), name);
     988              : }
     989              : 
     990              : 
     991              : /* The EXPONENT(X) intrinsic function is translated into
     992              :        int ret;
     993              :        return isfinite(X) ? (frexp (X, &ret) , ret) : huge
     994              :    so that if X is a NaN or infinity, the result is HUGE(0).
     995              :  */
     996              : 
     997              : static void
     998          228 : gfc_conv_intrinsic_exponent (gfc_se *se, gfc_expr *expr)
     999              : {
    1000          228 :   tree arg, type, res, tmp, frexp, cond, huge;
    1001          228 :   int i;
    1002              : 
    1003          456 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP,
    1004          228 :                                        expr->value.function.actual->expr->ts.kind);
    1005              : 
    1006          228 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    1007          228 :   arg = gfc_evaluate_now (arg, &se->pre);
    1008              : 
    1009          228 :   i = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
    1010          228 :   huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[i].huge, gfc_c_int_kind);
    1011          228 :   cond = build_call_expr_loc (input_location,
    1012              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    1013              :                               1, arg);
    1014              : 
    1015          228 :   res = gfc_create_var (integer_type_node, NULL);
    1016          228 :   tmp = build_call_expr_loc (input_location, frexp, 2, arg,
    1017              :                              gfc_build_addr_expr (NULL_TREE, res));
    1018          228 :   tmp = fold_build2_loc (input_location, COMPOUND_EXPR, integer_type_node,
    1019              :                          tmp, res);
    1020          228 :   se->expr = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
    1021              :                               cond, tmp, huge);
    1022              : 
    1023          228 :   type = gfc_typenode_for_spec (&expr->ts);
    1024          228 :   se->expr = fold_convert (type, se->expr);
    1025          228 : }
    1026              : 
    1027              : 
    1028              : static int caf_call_cnt = 0;
    1029              : 
    1030              : static tree
    1031         1434 : conv_caf_func_index (stmtblock_t *block, gfc_namespace *ns, const char *pat,
    1032              :                      gfc_expr *hash)
    1033              : {
    1034         1434 :   char *name;
    1035         1434 :   gfc_se argse;
    1036         1434 :   gfc_expr func_index;
    1037         1434 :   gfc_symtree *index_st;
    1038         1434 :   tree func_index_tree;
    1039         1434 :   stmtblock_t blk;
    1040              : 
    1041              :   /* Need to get namespace where static variables are possible.  */
    1042         1434 :   while (ns && ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
    1043            0 :     ns = ns->parent;
    1044         1434 :   gcc_assert (ns);
    1045              : 
    1046         1434 :   name = xasprintf (pat, caf_call_cnt);
    1047         1434 :   gcc_assert (!gfc_get_sym_tree (name, ns, &index_st, false));
    1048         1434 :   free (name);
    1049              : 
    1050         1434 :   index_st->n.sym->attr.flavor = FL_VARIABLE;
    1051         1434 :   index_st->n.sym->attr.save = SAVE_EXPLICIT;
    1052         1434 :   index_st->n.sym->value
    1053         1434 :     = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
    1054              :                              &gfc_current_locus);
    1055         1434 :   mpz_set_si (index_st->n.sym->value->value.integer, -1);
    1056         1434 :   index_st->n.sym->ts.type = BT_INTEGER;
    1057         1434 :   index_st->n.sym->ts.kind = gfc_default_integer_kind;
    1058         1434 :   gfc_set_sym_referenced (index_st->n.sym);
    1059         1434 :   memset (&func_index, 0, sizeof (gfc_expr));
    1060         1434 :   gfc_clear_ts (&func_index.ts);
    1061         1434 :   func_index.expr_type = EXPR_VARIABLE;
    1062         1434 :   func_index.symtree = index_st;
    1063         1434 :   func_index.ts = index_st->n.sym->ts;
    1064         1434 :   gfc_commit_symbol (index_st->n.sym);
    1065              : 
    1066         1434 :   gfc_init_se (&argse, NULL);
    1067         1434 :   gfc_conv_expr (&argse, &func_index);
    1068         1434 :   gfc_add_block_to_block (block, &argse.pre);
    1069         1434 :   func_index_tree = argse.expr;
    1070              : 
    1071         1434 :   gfc_init_se (&argse, NULL);
    1072         1434 :   gfc_conv_expr (&argse, hash);
    1073              : 
    1074         1434 :   gfc_init_block (&blk);
    1075         1434 :   gfc_add_modify (&blk, func_index_tree,
    1076              :                   build_call_expr (gfor_fndecl_caf_get_remote_function_index, 1,
    1077              :                                    argse.expr));
    1078         1434 :   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         1434 :   return func_index_tree;
    1087              : }
    1088              : 
    1089              : static tree
    1090         1434 : conv_caf_add_call_data (stmtblock_t *blk, gfc_namespace *ns, const char *pat,
    1091              :                         gfc_symbol *data_sym, tree *data_size)
    1092              : {
    1093         1434 :   char *name;
    1094         1434 :   gfc_symtree *data_st;
    1095         1434 :   gfc_constructor *con;
    1096         1434 :   gfc_expr data, data_init;
    1097         1434 :   gfc_se argse;
    1098         1434 :   tree data_tree;
    1099              : 
    1100         1434 :   memset (&data, 0, sizeof (gfc_expr));
    1101         1434 :   gfc_clear_ts (&data.ts);
    1102         1434 :   data.expr_type = EXPR_VARIABLE;
    1103         1434 :   name = xasprintf (pat, caf_call_cnt);
    1104         1434 :   gcc_assert (!gfc_get_sym_tree (name, ns, &data_st, false));
    1105         1434 :   free (name);
    1106         1434 :   data_st->n.sym->attr.flavor = FL_VARIABLE;
    1107         1434 :   data_st->n.sym->ts = data_sym->ts;
    1108         1434 :   data.symtree = data_st;
    1109         1434 :   gfc_set_sym_referenced (data.symtree->n.sym);
    1110         1434 :   data.ts = data_st->n.sym->ts;
    1111         1434 :   gfc_commit_symbol (data_st->n.sym);
    1112              : 
    1113         1434 :   memset (&data_init, 0, sizeof (gfc_expr));
    1114         1434 :   gfc_clear_ts (&data_init.ts);
    1115         1434 :   data_init.expr_type = EXPR_STRUCTURE;
    1116         1434 :   data_init.ts = data.ts;
    1117         1750 :   for (gfc_component *comp = data.ts.u.derived->components; comp;
    1118          316 :        comp = comp->next)
    1119              :     {
    1120          316 :       con = gfc_constructor_get ();
    1121          316 :       con->expr = comp->initializer;
    1122          316 :       comp->initializer = NULL;
    1123          316 :       gfc_constructor_append (&data_init.value.constructor, con);
    1124              :     }
    1125              : 
    1126         1434 :   if (data.ts.u.derived->components)
    1127              :     {
    1128          110 :       gfc_init_se (&argse, NULL);
    1129          110 :       gfc_conv_expr (&argse, &data);
    1130          110 :       data_tree = argse.expr;
    1131          110 :       gfc_add_expr_to_block (blk,
    1132              :                              gfc_trans_structure_assign (data_tree, &data_init,
    1133              :                                                          true, true));
    1134          110 :       gfc_constructor_free (data_init.value.constructor);
    1135          110 :       *data_size = TREE_TYPE (data_tree)->type_common.size_unit;
    1136          110 :       data_tree = gfc_build_addr_expr (pvoid_type_node, data_tree);
    1137              :     }
    1138              :   else
    1139              :     {
    1140         1324 :       data_tree = build_zero_cst (pvoid_type_node);
    1141         1324 :       *data_size = build_zero_cst (size_type_node);
    1142              :     }
    1143              : 
    1144         1434 :   return data_tree;
    1145              : }
    1146              : 
    1147              : static tree
    1148          251 : conv_shape_to_cst (gfc_expr *e)
    1149              : {
    1150          251 :   tree tmp = NULL;
    1151          690 :   for (int d = 0; d < e->rank; ++d)
    1152              :     {
    1153          439 :       if (!tmp)
    1154          251 :         tmp = gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind);
    1155              :       else
    1156          188 :         tmp = fold_build2 (MULT_EXPR, TREE_TYPE (tmp), tmp,
    1157              :                            gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind));
    1158              :     }
    1159          251 :   return fold_convert (size_type_node, tmp);
    1160              : }
    1161              : 
    1162              : static void
    1163         1267 : conv_stat_and_team (stmtblock_t *block, gfc_expr *expr, tree *stat, tree *team,
    1164              :                     tree *team_no)
    1165              : {
    1166         1267 :   gfc_expr *stat_e, *team_e;
    1167              : 
    1168         1267 :   stat_e = gfc_find_stat_co (expr);
    1169         1267 :   if (stat_e)
    1170              :     {
    1171           33 :       gfc_se stat_se;
    1172           33 :       gfc_init_se (&stat_se, NULL);
    1173           33 :       gfc_conv_expr_reference (&stat_se, stat_e);
    1174           33 :       *stat = stat_se.expr;
    1175           33 :       gfc_add_block_to_block (block, &stat_se.pre);
    1176           33 :       gfc_add_block_to_block (block, &stat_se.post);
    1177              :     }
    1178              :   else
    1179         1234 :     *stat = null_pointer_node;
    1180              : 
    1181         1267 :   team_e = gfc_find_team_co (expr, TEAM_TEAM);
    1182         1267 :   if (team_e)
    1183              :     {
    1184           18 :       gfc_se team_se;
    1185           18 :       gfc_init_se (&team_se, NULL);
    1186           18 :       gfc_conv_expr (&team_se, team_e);
    1187           18 :       *team
    1188           18 :         = gfc_build_addr_expr (NULL_TREE, gfc_trans_force_lval (&team_se.pre,
    1189              :                                                                 team_se.expr));
    1190           18 :       gfc_add_block_to_block (block, &team_se.pre);
    1191           18 :       gfc_add_block_to_block (block, &team_se.post);
    1192              :     }
    1193              :   else
    1194         1249 :     *team = null_pointer_node;
    1195              : 
    1196         1267 :   team_e = gfc_find_team_co (expr, TEAM_NUMBER);
    1197         1267 :   if (team_e)
    1198              :     {
    1199           30 :       gfc_se team_se;
    1200           30 :       gfc_init_se (&team_se, NULL);
    1201           30 :       gfc_conv_expr (&team_se, team_e);
    1202           30 :       *team_no = gfc_build_addr_expr (
    1203              :         NULL_TREE,
    1204              :         gfc_trans_force_lval (&team_se.pre,
    1205              :                               fold_convert (integer_type_node, team_se.expr)));
    1206           30 :       gfc_add_block_to_block (block, &team_se.pre);
    1207           30 :       gfc_add_block_to_block (block, &team_se.post);
    1208              :     }
    1209              :   else
    1210         1237 :     *team_no = null_pointer_node;
    1211         1267 : }
    1212              : 
    1213              : /* Get data from a remote coarray.  */
    1214              : 
    1215              : static void
    1216         1006 : gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs,
    1217              :                             bool may_realloc, symbol_attribute *caf_attr)
    1218              : {
    1219         1006 :   gfc_expr *array_expr;
    1220         1006 :   tree caf_decl, token, image_index, tmp, res_var, type, stat, dest_size,
    1221              :     dest_data, opt_dest_desc, get_fn_index_tree, add_data_tree, add_data_size,
    1222              :     opt_src_desc, opt_src_charlen, opt_dest_charlen, team, team_no;
    1223         1006 :   symbol_attribute caf_attr_store;
    1224         1006 :   gfc_namespace *ns;
    1225         1006 :   gfc_expr *get_fn_hash = expr->value.function.actual->next->expr,
    1226         1006 :            *get_fn_expr = expr->value.function.actual->next->next->expr;
    1227         1006 :   gfc_symbol *add_data_sym = get_fn_expr->symtree->n.sym->formal->sym;
    1228              : 
    1229         1006 :   gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
    1230              : 
    1231         1006 :   if (se->ss && se->ss->info->useflags)
    1232              :     {
    1233              :       /* Access the previously obtained result.  */
    1234          379 :       gfc_conv_tmp_array_ref (se);
    1235          379 :       return;
    1236              :     }
    1237              : 
    1238          627 :   array_expr = expr->value.function.actual->expr;
    1239          627 :   ns = array_expr->expr_type == EXPR_VARIABLE
    1240          627 :            && !array_expr->symtree->n.sym->attr.associate_var
    1241          571 :            && !array_expr->symtree->n.sym->module
    1242          627 :          ? array_expr->symtree->n.sym->ns
    1243              :          : gfc_current_ns;
    1244          627 :   type = gfc_typenode_for_spec (&array_expr->ts);
    1245              : 
    1246          627 :   if (caf_attr == NULL)
    1247              :     {
    1248          627 :       caf_attr_store = gfc_caf_attr (array_expr);
    1249          627 :       caf_attr = &caf_attr_store;
    1250              :     }
    1251              : 
    1252          627 :   res_var = lhs;
    1253              : 
    1254          627 :   conv_stat_and_team (&se->pre, expr, &stat, &team, &team_no);
    1255              : 
    1256          627 :   get_fn_index_tree
    1257          627 :     = conv_caf_func_index (&se->pre, ns, "__caf_get_from_remote_fn_index_%d",
    1258              :                            get_fn_hash);
    1259          627 :   add_data_tree
    1260          627 :     = conv_caf_add_call_data (&se->pre, ns, "__caf_get_from_remote_add_data_%d",
    1261              :                               add_data_sym, &add_data_size);
    1262          627 :   ++caf_call_cnt;
    1263              : 
    1264          627 :   if (array_expr->rank == 0)
    1265              :     {
    1266          246 :       res_var = gfc_create_var (type, "caf_res");
    1267          246 :       if (array_expr->ts.type == BT_CHARACTER)
    1268              :         {
    1269           33 :           gfc_conv_string_length (array_expr->ts.u.cl, array_expr, &se->pre);
    1270           33 :           se->string_length = array_expr->ts.u.cl->backend_decl;
    1271           33 :           opt_src_charlen = gfc_build_addr_expr (
    1272              :             NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
    1273           33 :           dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
    1274              :         }
    1275              :       else
    1276              :         {
    1277          213 :           dest_size = res_var->typed.type->type_common.size_unit;
    1278          213 :           opt_src_charlen
    1279          213 :             = build_zero_cst (build_pointer_type (size_type_node));
    1280              :         }
    1281          246 :       dest_data
    1282          246 :         = gfc_evaluate_now (gfc_build_addr_expr (NULL_TREE, res_var), &se->pre);
    1283          246 :       res_var = build_fold_indirect_ref (dest_data);
    1284          246 :       dest_data = gfc_build_addr_expr (pvoid_type_node, dest_data);
    1285          246 :       opt_dest_desc = build_zero_cst (pvoid_type_node);
    1286              :     }
    1287              :   else
    1288              :     {
    1289              :       /* Create temporary.  */
    1290          381 :       may_realloc = gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
    1291              :                                                  type, NULL_TREE, false, false,
    1292              :                                                  false, &array_expr->where)
    1293              :                     == NULL_TREE;
    1294          381 :       res_var = se->ss->info->data.array.descriptor;
    1295          381 :       if (array_expr->ts.type == BT_CHARACTER)
    1296              :         {
    1297           16 :           se->string_length = array_expr->ts.u.cl->backend_decl;
    1298           16 :           opt_src_charlen = gfc_build_addr_expr (
    1299              :             NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
    1300           16 :           dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
    1301              :         }
    1302              :       else
    1303              :         {
    1304          365 :           opt_src_charlen
    1305          365 :             = build_zero_cst (build_pointer_type (size_type_node));
    1306          365 :           dest_size = fold_build2 (
    1307              :             MULT_EXPR, size_type_node,
    1308              :             fold_convert (size_type_node,
    1309              :                           array_expr->shape
    1310              :                             ? conv_shape_to_cst (array_expr)
    1311              :                             : gfc_conv_descriptor_size (res_var,
    1312              :                                                         array_expr->rank)),
    1313              :             fold_convert (size_type_node,
    1314              :                           gfc_conv_descriptor_span_get (res_var)));
    1315              :         }
    1316          381 :       opt_dest_desc = res_var;
    1317          381 :       dest_data = gfc_conv_descriptor_data_get (res_var);
    1318          381 :       opt_dest_desc = gfc_build_addr_expr (NULL_TREE, opt_dest_desc);
    1319          381 :       if (may_realloc)
    1320              :         {
    1321           62 :           tmp = gfc_conv_descriptor_data_get (res_var);
    1322           62 :           tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
    1323              :                                             NULL_TREE, NULL_TREE, true, NULL,
    1324              :                                             GFC_CAF_COARRAY_NOCOARRAY);
    1325           62 :           gfc_add_expr_to_block (&se->post, tmp);
    1326              :         }
    1327          381 :       dest_data
    1328          381 :         = gfc_build_addr_expr (NULL_TREE,
    1329              :                                gfc_trans_force_lval (&se->pre, dest_data));
    1330              :     }
    1331              : 
    1332          627 :   opt_dest_charlen = opt_src_charlen;
    1333          627 :   caf_decl = gfc_get_tree_for_caf_expr (array_expr);
    1334          627 :   if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
    1335            2 :     caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
    1336              : 
    1337          627 :   if (!TYPE_LANG_SPECIFIC (TREE_TYPE (caf_decl))->rank
    1338          627 :       || GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl)))
    1339          546 :     opt_src_desc = build_zero_cst (pvoid_type_node);
    1340              :   else
    1341           81 :     opt_src_desc = gfc_build_addr_expr (pvoid_type_node, caf_decl);
    1342              : 
    1343          627 :   image_index = gfc_caf_get_image_index (&se->pre, array_expr, caf_decl);
    1344          627 :   gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, array_expr);
    1345              : 
    1346              :   /* It guarantees memory consistency within the same segment.  */
    1347          627 :   tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
    1348          627 :   tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1349              :                     gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    1350              :                     tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    1351          627 :   ASM_VOLATILE_P (tmp) = 1;
    1352          627 :   gfc_add_expr_to_block (&se->pre, tmp);
    1353              : 
    1354          627 :   tmp = build_call_expr_loc (
    1355              :     input_location, gfor_fndecl_caf_get_from_remote, 15, token, opt_src_desc,
    1356              :     opt_src_charlen, image_index, dest_size, dest_data, opt_dest_charlen,
    1357              :     opt_dest_desc, constant_boolean_node (may_realloc, boolean_type_node),
    1358              :     get_fn_index_tree, add_data_tree, add_data_size, stat, team, team_no);
    1359              : 
    1360          627 :   gfc_add_expr_to_block (&se->pre, tmp);
    1361              : 
    1362          627 :   if (se->ss)
    1363          381 :     gfc_advance_se_ss_chain (se);
    1364              : 
    1365          627 :   se->expr = res_var;
    1366              : 
    1367          627 :   return;
    1368              : }
    1369              : 
    1370              : /* Generate call to caf_is_present_on_remote for allocated (coarrary[...])
    1371              :    calls.  */
    1372              : 
    1373              : static void
    1374          167 : gfc_conv_intrinsic_caf_is_present_remote (gfc_se *se, gfc_expr *e)
    1375              : {
    1376          167 :   gfc_expr *caf_expr, *hash, *present_fn;
    1377          167 :   gfc_symbol *add_data_sym;
    1378          167 :   tree fn_index, add_data_tree, add_data_size, caf_decl, image_index, token;
    1379              : 
    1380          167 :   gcc_assert (e->expr_type == EXPR_FUNCTION
    1381              :               && e->value.function.isym->id
    1382              :                    == GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE);
    1383          167 :   caf_expr = e->value.function.actual->expr;
    1384          167 :   hash = e->value.function.actual->next->expr;
    1385          167 :   present_fn = e->value.function.actual->next->next->expr;
    1386          167 :   add_data_sym = present_fn->symtree->n.sym->formal->sym;
    1387              : 
    1388          167 :   fn_index = conv_caf_func_index (&se->pre, e->symtree->n.sym->ns,
    1389              :                                   "__caf_present_on_remote_fn_index_%d", hash);
    1390          167 :   add_data_tree = conv_caf_add_call_data (&se->pre, e->symtree->n.sym->ns,
    1391              :                                           "__caf_present_on_remote_add_data_%d",
    1392              :                                           add_data_sym, &add_data_size);
    1393          167 :   ++caf_call_cnt;
    1394              : 
    1395          167 :   caf_decl = gfc_get_tree_for_caf_expr (caf_expr);
    1396          167 :   if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
    1397            4 :     caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
    1398              : 
    1399          167 :   image_index = gfc_caf_get_image_index (&se->pre, caf_expr, caf_decl);
    1400          167 :   gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, caf_expr);
    1401              : 
    1402          167 :   se->expr
    1403          167 :     = fold_convert (logical_type_node,
    1404              :                     build_call_expr_loc (input_location,
    1405              :                                          gfor_fndecl_caf_is_present_on_remote,
    1406              :                                          5, token, image_index, fn_index,
    1407              :                                          add_data_tree, add_data_size));
    1408          167 : }
    1409              : 
    1410              : static tree
    1411          360 : conv_caf_send_to_remote (gfc_code *code)
    1412              : {
    1413          360 :   gfc_expr *lhs_expr, *rhs_expr, *lhs_hash, *receiver_fn_expr;
    1414          360 :   gfc_symbol *add_data_sym;
    1415          360 :   gfc_se lhs_se, rhs_se;
    1416          360 :   stmtblock_t block;
    1417          360 :   gfc_namespace *ns;
    1418          360 :   tree caf_decl, token, rhs_size, image_index, tmp, rhs_data;
    1419          360 :   tree lhs_stat, lhs_team, lhs_team_no, opt_lhs_charlen, opt_rhs_charlen;
    1420          360 :   tree opt_lhs_desc = NULL_TREE, opt_rhs_desc = NULL_TREE;
    1421          360 :   tree receiver_fn_index_tree, add_data_tree, add_data_size;
    1422              : 
    1423          360 :   gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
    1424          360 :   gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SEND);
    1425              : 
    1426          360 :   lhs_expr = code->ext.actual->expr;
    1427          360 :   rhs_expr = code->ext.actual->next->expr;
    1428          360 :   lhs_hash = code->ext.actual->next->next->expr;
    1429          360 :   receiver_fn_expr = code->ext.actual->next->next->next->expr;
    1430          360 :   add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
    1431              : 
    1432          360 :   ns = lhs_expr->expr_type == EXPR_VARIABLE
    1433          360 :            && !lhs_expr->symtree->n.sym->attr.associate_var
    1434          360 :          ? lhs_expr->symtree->n.sym->ns
    1435              :          : gfc_current_ns;
    1436              : 
    1437          360 :   gfc_init_block (&block);
    1438              : 
    1439              :   /* LHS.  */
    1440          360 :   gfc_init_se (&lhs_se, NULL);
    1441          360 :   caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
    1442          360 :   if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
    1443            0 :     caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
    1444          360 :   if (lhs_expr->rank == 0)
    1445              :     {
    1446          266 :       if (lhs_expr->ts.type == BT_CHARACTER)
    1447              :         {
    1448           24 :           gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
    1449           24 :           lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
    1450           24 :           opt_lhs_charlen = gfc_build_addr_expr (
    1451              :             NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
    1452              :         }
    1453              :       else
    1454          242 :         opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
    1455          266 :       opt_lhs_desc = null_pointer_node;
    1456              :     }
    1457              :   else
    1458              :     {
    1459           94 :       gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
    1460           94 :       gfc_add_block_to_block (&block, &lhs_se.pre);
    1461           94 :       opt_lhs_desc = lhs_se.expr;
    1462           94 :       if (lhs_expr->ts.type == BT_CHARACTER)
    1463           44 :         opt_lhs_charlen = gfc_build_addr_expr (
    1464              :           NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
    1465              :       else
    1466           50 :         opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
    1467              :       /* Get the third formal argument of the receiver function.  (This is the
    1468              :          location where to put the data on the remote image.)  Need to look at
    1469              :          the argument in the function decl, because in the gfc_symbol's formal
    1470              :          argument an array may have no descriptor while in the generated
    1471              :          function decl it has.  */
    1472           94 :       tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
    1473              :         TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
    1474           94 :       if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    1475           56 :         opt_lhs_desc = null_pointer_node;
    1476              :       else
    1477           38 :         opt_lhs_desc
    1478           38 :           = gfc_build_addr_expr (NULL_TREE,
    1479              :                                  gfc_trans_force_lval (&block, opt_lhs_desc));
    1480              :     }
    1481              : 
    1482              :   /* Obtain token, offset and image index for the LHS.  */
    1483          360 :   image_index = gfc_caf_get_image_index (&block, lhs_expr, caf_decl);
    1484          360 :   gfc_get_caf_token_offset (&lhs_se, &token, NULL, caf_decl, NULL, lhs_expr);
    1485              : 
    1486              :   /* RHS.  */
    1487          360 :   gfc_init_se (&rhs_se, NULL);
    1488          360 :   if (rhs_expr->rank == 0)
    1489              :     {
    1490          436 :       rhs_se.want_pointer = rhs_expr->ts.type == BT_CHARACTER
    1491          218 :                             && rhs_expr->expr_type != EXPR_CONSTANT;
    1492          218 :       gfc_conv_expr (&rhs_se, rhs_expr);
    1493          218 :       gfc_add_block_to_block (&block, &rhs_se.pre);
    1494          218 :       opt_rhs_desc = null_pointer_node;
    1495          218 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1496              :         {
    1497           40 :           rhs_data
    1498           40 :             = rhs_expr->expr_type == EXPR_CONSTANT
    1499           40 :                 ? gfc_build_addr_expr (NULL_TREE,
    1500              :                                        gfc_trans_force_lval (&block,
    1501              :                                                              rhs_se.expr))
    1502              :                 : rhs_se.expr;
    1503           40 :           opt_rhs_charlen = gfc_build_addr_expr (
    1504              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1505           40 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1506              :         }
    1507              :       else
    1508              :         {
    1509          178 :           rhs_data
    1510          178 :             = gfc_build_addr_expr (NULL_TREE,
    1511              :                                    gfc_trans_force_lval (&block, rhs_se.expr));
    1512          178 :           opt_rhs_charlen
    1513          178 :             = build_zero_cst (build_pointer_type (size_type_node));
    1514          178 :           rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
    1515              :         }
    1516              :     }
    1517              :   else
    1518              :     {
    1519          284 :       rhs_se.force_tmp = rhs_expr->shape == NULL
    1520          142 :                          || !gfc_is_simply_contiguous (rhs_expr, false, false);
    1521          142 :       gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
    1522          142 :       gfc_add_block_to_block (&block, &rhs_se.pre);
    1523          142 :       opt_rhs_desc = rhs_se.expr;
    1524          142 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1525              :         {
    1526           28 :           opt_rhs_charlen = gfc_build_addr_expr (
    1527              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1528           28 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1529              :         }
    1530              :       else
    1531              :         {
    1532          114 :           opt_rhs_charlen
    1533          114 :             = build_zero_cst (build_pointer_type (size_type_node));
    1534          114 :           rhs_size = fold_build2 (
    1535              :             MULT_EXPR, size_type_node,
    1536              :             fold_convert (size_type_node,
    1537              :                           rhs_expr->shape
    1538              :                             ? conv_shape_to_cst (rhs_expr)
    1539              :                             : gfc_conv_descriptor_size (rhs_se.expr,
    1540              :                                                         rhs_expr->rank)),
    1541              :             fold_convert (size_type_node,
    1542              :                           gfc_conv_descriptor_span_get (rhs_se.expr)));
    1543              :         }
    1544              : 
    1545          142 :       rhs_data = gfc_build_addr_expr (
    1546              :         NULL_TREE, gfc_trans_force_lval (&block, gfc_conv_descriptor_data_get (
    1547              :                                                    opt_rhs_desc)));
    1548          142 :       opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
    1549              :     }
    1550          360 :   gfc_add_block_to_block (&block, &rhs_se.pre);
    1551              : 
    1552          360 :   conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
    1553              : 
    1554          360 :   receiver_fn_index_tree
    1555          360 :     = conv_caf_func_index (&block, ns, "__caf_send_to_remote_fn_index_%d",
    1556              :                            lhs_hash);
    1557          360 :   add_data_tree
    1558          360 :     = conv_caf_add_call_data (&block, ns, "__caf_send_to_remote_add_data_%d",
    1559              :                               add_data_sym, &add_data_size);
    1560          360 :   ++caf_call_cnt;
    1561              : 
    1562          360 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_send_to_remote, 14,
    1563              :                              token, opt_lhs_desc, opt_lhs_charlen, image_index,
    1564              :                              rhs_size, rhs_data, opt_rhs_charlen, opt_rhs_desc,
    1565              :                              receiver_fn_index_tree, add_data_tree,
    1566              :                              add_data_size, lhs_stat, lhs_team, lhs_team_no);
    1567              : 
    1568          360 :   gfc_add_expr_to_block (&block, tmp);
    1569          360 :   gfc_add_block_to_block (&block, &lhs_se.post);
    1570          360 :   gfc_add_block_to_block (&block, &rhs_se.post);
    1571              : 
    1572              :   /* It guarantees memory consistency within the same segment.  */
    1573          360 :   tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
    1574          360 :   tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1575              :                     gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    1576              :                     tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    1577          360 :   ASM_VOLATILE_P (tmp) = 1;
    1578          360 :   gfc_add_expr_to_block (&block, tmp);
    1579              : 
    1580          360 :   return gfc_finish_block (&block);
    1581              : }
    1582              : 
    1583              : /* Send-get data to a remote coarray.  */
    1584              : 
    1585              : static tree
    1586          140 : conv_caf_sendget (gfc_code *code)
    1587              : {
    1588              :   /* lhs stuff  */
    1589          140 :   gfc_expr *lhs_expr, *lhs_hash, *receiver_fn_expr;
    1590          140 :   gfc_symbol *lhs_add_data_sym;
    1591          140 :   gfc_se lhs_se;
    1592          140 :   tree lhs_caf_decl, lhs_token, opt_lhs_charlen,
    1593          140 :     opt_lhs_desc = NULL_TREE, receiver_fn_index_tree, lhs_image_index,
    1594              :     lhs_add_data_tree, lhs_add_data_size, lhs_stat, lhs_team, lhs_team_no;
    1595          140 :   int transfer_rank;
    1596              : 
    1597              :   /* rhs stuff  */
    1598          140 :   gfc_expr *rhs_expr, *rhs_hash, *sender_fn_expr;
    1599          140 :   gfc_symbol *rhs_add_data_sym;
    1600          140 :   gfc_se rhs_se;
    1601          140 :   tree rhs_caf_decl, rhs_token, opt_rhs_charlen,
    1602          140 :     opt_rhs_desc = NULL_TREE, sender_fn_index_tree, rhs_image_index,
    1603              :     rhs_add_data_tree, rhs_add_data_size, rhs_stat, rhs_team, rhs_team_no;
    1604              : 
    1605              :   /* shared  */
    1606          140 :   stmtblock_t block;
    1607          140 :   gfc_namespace *ns;
    1608          140 :   tree tmp, rhs_size;
    1609              : 
    1610          140 :   gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
    1611          140 :   gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SENDGET);
    1612              : 
    1613          140 :   lhs_expr = code->ext.actual->expr;
    1614          140 :   rhs_expr = code->ext.actual->next->expr;
    1615          140 :   lhs_hash = code->ext.actual->next->next->expr;
    1616          140 :   receiver_fn_expr = code->ext.actual->next->next->next->expr;
    1617          140 :   rhs_hash = code->ext.actual->next->next->next->next->expr;
    1618          140 :   sender_fn_expr = code->ext.actual->next->next->next->next->next->expr;
    1619              : 
    1620          140 :   lhs_add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
    1621          140 :   rhs_add_data_sym = sender_fn_expr->symtree->n.sym->formal->sym;
    1622              : 
    1623          140 :   ns = lhs_expr->expr_type == EXPR_VARIABLE
    1624          140 :            && !lhs_expr->symtree->n.sym->attr.associate_var
    1625          140 :          ? lhs_expr->symtree->n.sym->ns
    1626              :          : gfc_current_ns;
    1627              : 
    1628          140 :   gfc_init_block (&block);
    1629              : 
    1630          140 :   lhs_stat = null_pointer_node;
    1631          140 :   lhs_team = null_pointer_node;
    1632          140 :   rhs_stat = null_pointer_node;
    1633          140 :   rhs_team = null_pointer_node;
    1634              : 
    1635              :   /* LHS.  */
    1636          140 :   gfc_init_se (&lhs_se, NULL);
    1637          140 :   lhs_caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
    1638          140 :   if (TREE_CODE (TREE_TYPE (lhs_caf_decl)) == REFERENCE_TYPE)
    1639            0 :     lhs_caf_decl = build_fold_indirect_ref_loc (input_location, lhs_caf_decl);
    1640          140 :   if (lhs_expr->rank == 0)
    1641              :     {
    1642           78 :       if (lhs_expr->ts.type == BT_CHARACTER)
    1643              :         {
    1644           16 :           gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
    1645           16 :           lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
    1646           16 :           opt_lhs_charlen = gfc_build_addr_expr (
    1647              :             NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
    1648              :         }
    1649              :       else
    1650           62 :         opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
    1651           78 :       opt_lhs_desc = null_pointer_node;
    1652              :     }
    1653              :   else
    1654              :     {
    1655           62 :       gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
    1656           62 :       gfc_add_block_to_block (&block, &lhs_se.pre);
    1657           62 :       opt_lhs_desc = lhs_se.expr;
    1658           62 :       if (lhs_expr->ts.type == BT_CHARACTER)
    1659           32 :         opt_lhs_charlen = gfc_build_addr_expr (
    1660              :           NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
    1661              :       else
    1662           30 :         opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
    1663              :       /* Get the third formal argument of the receiver function.  (This is the
    1664              :          location where to put the data on the remote image.)  Need to look at
    1665              :          the argument in the function decl, because in the gfc_symbol's formal
    1666              :          argument an array may have no descriptor while in the generated
    1667              :          function decl it has.  */
    1668           62 :       tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
    1669              :         TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
    1670           62 :       if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    1671           54 :         opt_lhs_desc = null_pointer_node;
    1672              :       else
    1673            8 :         opt_lhs_desc
    1674            8 :           = gfc_build_addr_expr (NULL_TREE,
    1675              :                                  gfc_trans_force_lval (&block, opt_lhs_desc));
    1676              :     }
    1677              : 
    1678              :   /* Obtain token, offset and image index for the LHS.  */
    1679          140 :   lhs_image_index = gfc_caf_get_image_index (&block, lhs_expr, lhs_caf_decl);
    1680          140 :   gfc_get_caf_token_offset (&lhs_se, &lhs_token, NULL, lhs_caf_decl, NULL,
    1681              :                             lhs_expr);
    1682              : 
    1683              :   /* RHS.  */
    1684          140 :   rhs_caf_decl = gfc_get_tree_for_caf_expr (rhs_expr);
    1685          140 :   if (TREE_CODE (TREE_TYPE (rhs_caf_decl)) == REFERENCE_TYPE)
    1686            0 :     rhs_caf_decl = build_fold_indirect_ref_loc (input_location, rhs_caf_decl);
    1687          140 :   transfer_rank = rhs_expr->rank;
    1688          140 :   gfc_expression_rank (rhs_expr);
    1689          140 :   gfc_init_se (&rhs_se, NULL);
    1690          140 :   if (rhs_expr->rank == 0)
    1691              :     {
    1692           80 :       opt_rhs_desc = null_pointer_node;
    1693           80 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1694              :         {
    1695           32 :           gfc_conv_expr (&rhs_se, rhs_expr);
    1696           32 :           gfc_add_block_to_block (&block, &rhs_se.pre);
    1697           32 :           opt_rhs_charlen = gfc_build_addr_expr (
    1698              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1699           32 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1700              :         }
    1701              :       else
    1702              :         {
    1703           48 :           gfc_typespec *ts
    1704           48 :             = &sender_fn_expr->symtree->n.sym->formal->next->next->sym->ts;
    1705              : 
    1706           48 :           opt_rhs_charlen
    1707           48 :             = build_zero_cst (build_pointer_type (size_type_node));
    1708           48 :           rhs_size = gfc_typenode_for_spec (ts)->type_common.size_unit;
    1709              :         }
    1710              :     }
    1711              :   /* Get the fifth formal argument of the getter function.  This is the argument
    1712              :      pointing to the data to get on the remote image.  Need to look at the
    1713              :      argument in the function decl, because in the gfc_symbol's formal argument
    1714              :      an array may have no descriptor while in the generated function decl it
    1715              :      has.  */
    1716           60 :   else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_VALUE (
    1717              :              TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
    1718              :                TREE_TYPE (sender_fn_expr->symtree->n.sym->backend_decl))))))))))
    1719              :     {
    1720           52 :       rhs_se.data_not_needed = 1;
    1721           52 :       gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
    1722           52 :       gfc_add_block_to_block (&block, &rhs_se.pre);
    1723           52 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1724              :         {
    1725           16 :           opt_rhs_charlen = gfc_build_addr_expr (
    1726              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1727           16 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1728              :         }
    1729              :       else
    1730              :         {
    1731           36 :           opt_rhs_charlen
    1732           36 :             = build_zero_cst (build_pointer_type (size_type_node));
    1733           36 :           rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
    1734              :         }
    1735           52 :       opt_rhs_desc = null_pointer_node;
    1736              :     }
    1737              :   else
    1738              :     {
    1739            8 :       gfc_ref *arr_ref = rhs_expr->ref;
    1740            8 :       while (arr_ref && arr_ref->type != REF_ARRAY)
    1741            0 :         arr_ref = arr_ref->next;
    1742            8 :       rhs_se.force_tmp
    1743           16 :         = (rhs_expr->shape == NULL
    1744            8 :            && (!arr_ref || !gfc_full_array_ref_p (arr_ref, nullptr)))
    1745           16 :           || !gfc_is_simply_contiguous (rhs_expr, false, false);
    1746            8 :       gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
    1747            8 :       gfc_add_block_to_block (&block, &rhs_se.pre);
    1748            8 :       opt_rhs_desc = rhs_se.expr;
    1749            8 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1750              :         {
    1751            0 :           opt_rhs_charlen = gfc_build_addr_expr (
    1752              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1753            0 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1754              :         }
    1755              :       else
    1756              :         {
    1757            8 :           opt_rhs_charlen
    1758            8 :             = build_zero_cst (build_pointer_type (size_type_node));
    1759            8 :           rhs_size = fold_build2 (
    1760              :             MULT_EXPR, size_type_node,
    1761              :             fold_convert (size_type_node,
    1762              :                           rhs_expr->shape
    1763              :                             ? conv_shape_to_cst (rhs_expr)
    1764              :                             : gfc_conv_descriptor_size (rhs_se.expr,
    1765              :                                                         rhs_expr->rank)),
    1766              :             fold_convert (size_type_node,
    1767              :                           gfc_conv_descriptor_span_get (rhs_se.expr)));
    1768              :         }
    1769              : 
    1770            8 :       opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
    1771              :     }
    1772          140 :   gfc_add_block_to_block (&block, &rhs_se.pre);
    1773              : 
    1774              :   /* Obtain token, offset and image index for the RHS.  */
    1775          140 :   rhs_image_index = gfc_caf_get_image_index (&block, rhs_expr, rhs_caf_decl);
    1776          140 :   gfc_get_caf_token_offset (&rhs_se, &rhs_token, NULL, rhs_caf_decl, NULL,
    1777              :                             rhs_expr);
    1778              : 
    1779              :   /* stat and team.  */
    1780          140 :   conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
    1781          140 :   conv_stat_and_team (&block, rhs_expr, &rhs_stat, &rhs_team, &rhs_team_no);
    1782              : 
    1783          140 :   sender_fn_index_tree
    1784          140 :     = conv_caf_func_index (&block, ns, "__caf_transfer_from_fn_index_%d",
    1785              :                            rhs_hash);
    1786          140 :   rhs_add_data_tree
    1787          140 :     = conv_caf_add_call_data (&block, ns,
    1788              :                               "__caf_transfer_from_remote_add_data_%d",
    1789              :                               rhs_add_data_sym, &rhs_add_data_size);
    1790          140 :   receiver_fn_index_tree
    1791          140 :     = conv_caf_func_index (&block, ns, "__caf_transfer_to_remote_fn_index_%d",
    1792              :                            lhs_hash);
    1793          140 :   lhs_add_data_tree
    1794          140 :     = conv_caf_add_call_data (&block, ns,
    1795              :                               "__caf_transfer_to_remote_add_data_%d",
    1796              :                               lhs_add_data_sym, &lhs_add_data_size);
    1797          140 :   ++caf_call_cnt;
    1798              : 
    1799          140 :   tmp = build_call_expr_loc (
    1800              :     input_location, gfor_fndecl_caf_transfer_between_remotes, 22, lhs_token,
    1801              :     opt_lhs_desc, opt_lhs_charlen, lhs_image_index, receiver_fn_index_tree,
    1802              :     lhs_add_data_tree, lhs_add_data_size, rhs_token, opt_rhs_desc,
    1803              :     opt_rhs_charlen, rhs_image_index, sender_fn_index_tree, rhs_add_data_tree,
    1804              :     rhs_add_data_size, rhs_size,
    1805              :     transfer_rank == 0 ? boolean_true_node : boolean_false_node, lhs_stat,
    1806              :     rhs_stat, lhs_team, lhs_team_no, rhs_team, rhs_team_no);
    1807              : 
    1808          140 :   gfc_add_expr_to_block (&block, tmp);
    1809          140 :   gfc_add_block_to_block (&block, &lhs_se.post);
    1810          140 :   gfc_add_block_to_block (&block, &rhs_se.post);
    1811              : 
    1812              :   /* It guarantees memory consistency within the same segment.  */
    1813          140 :   tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
    1814          140 :   tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1815              :                     gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    1816              :                     tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    1817          140 :   ASM_VOLATILE_P (tmp) = 1;
    1818          140 :   gfc_add_expr_to_block (&block, tmp);
    1819              : 
    1820          140 :   return gfc_finish_block (&block);
    1821              : }
    1822              : 
    1823              : 
    1824              : static void
    1825         1297 : trans_this_image (gfc_se * se, gfc_expr *expr)
    1826              : {
    1827         1297 :   stmtblock_t loop;
    1828         1297 :   tree type, desc, dim_arg, cond, tmp, m, loop_var, exit_label, min_var, lbound,
    1829              :     ubound, extent, ml, team;
    1830         1297 :   gfc_se argse;
    1831         1297 :   int rank, corank;
    1832              : 
    1833              :   /* The case -fcoarray=single is handled elsewhere.  */
    1834         1297 :   gcc_assert (flag_coarray != GFC_FCOARRAY_SINGLE);
    1835              : 
    1836              :   /* Translate team, if present.  */
    1837         1297 :   if (expr->value.function.actual->next->next->expr)
    1838              :     {
    1839           18 :       gfc_init_se (&argse, NULL);
    1840           18 :       gfc_conv_expr_val (&argse, expr->value.function.actual->next->next->expr);
    1841           18 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    1842           18 :       gfc_add_block_to_block (&se->post, &argse.post);
    1843           18 :       team = fold_convert (pvoid_type_node, argse.expr);
    1844              :     }
    1845              :   else
    1846         1279 :     team = null_pointer_node;
    1847              : 
    1848              :   /* Argument-free version: THIS_IMAGE().  */
    1849         1297 :   if (expr->value.function.actual->expr == NULL)
    1850              :     {
    1851          979 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
    1852              :                                  team);
    1853          979 :       se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
    1854              :                                tmp);
    1855          987 :       return;
    1856              :     }
    1857              : 
    1858              :   /* Coarray-argument version: THIS_IMAGE(coarray [, dim]).  */
    1859              : 
    1860          318 :   type = gfc_get_int_type (gfc_default_integer_kind);
    1861          318 :   corank = expr->value.function.actual->expr->corank;
    1862          318 :   rank = expr->value.function.actual->expr->rank;
    1863              : 
    1864              :   /* Obtain the descriptor of the COARRAY.  */
    1865          318 :   gfc_init_se (&argse, NULL);
    1866          318 :   argse.want_coarray = 1;
    1867          318 :   gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
    1868          318 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    1869          318 :   gfc_add_block_to_block (&se->post, &argse.post);
    1870          318 :   desc = argse.expr;
    1871              : 
    1872          318 :   if (se->ss)
    1873              :     {
    1874              :       /* Create an implicit second parameter from the loop variable.  */
    1875           70 :       gcc_assert (!expr->value.function.actual->next->expr);
    1876           70 :       gcc_assert (corank > 0);
    1877           70 :       gcc_assert (se->loop->dimen == 1);
    1878           70 :       gcc_assert (se->ss->info->expr == expr);
    1879              : 
    1880           70 :       dim_arg = se->loop->loopvar[0];
    1881           70 :       dim_arg = fold_build2_loc (input_location, PLUS_EXPR,
    1882              :                                  gfc_array_index_type, dim_arg,
    1883           70 :                                  build_int_cst (TREE_TYPE (dim_arg), 1));
    1884           70 :       gfc_advance_se_ss_chain (se);
    1885              :     }
    1886              :   else
    1887              :     {
    1888              :       /* Use the passed DIM= argument.  */
    1889          248 :       gcc_assert (expr->value.function.actual->next->expr);
    1890          248 :       gfc_init_se (&argse, NULL);
    1891          248 :       gfc_conv_expr_type (&argse, expr->value.function.actual->next->expr,
    1892              :                           gfc_array_index_type);
    1893          248 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    1894          248 :       dim_arg = argse.expr;
    1895              : 
    1896          248 :       if (INTEGER_CST_P (dim_arg))
    1897              :         {
    1898          132 :           if (wi::ltu_p (wi::to_wide (dim_arg), 1)
    1899          264 :               || wi::gtu_p (wi::to_wide (dim_arg),
    1900          132 :                             GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
    1901            0 :             gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
    1902            0 :                        "dimension index", expr->value.function.isym->name,
    1903              :                        &expr->where);
    1904              :         }
    1905          116 :      else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    1906              :         {
    1907            0 :           dim_arg = gfc_evaluate_now (dim_arg, &se->pre);
    1908            0 :           cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    1909              :                                   dim_arg,
    1910            0 :                                   build_int_cst (TREE_TYPE (dim_arg), 1));
    1911            0 :           tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
    1912            0 :           tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    1913              :                                  dim_arg, tmp);
    1914            0 :           cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    1915              :                                   logical_type_node, cond, tmp);
    1916            0 :           gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    1917              :                                    gfc_msg_fault);
    1918              :         }
    1919              :     }
    1920              : 
    1921              :   /* Used algorithm; cf. Fortran 2008, C.10. Note, due to the scalarizer,
    1922              :      one always has a dim_arg argument.
    1923              : 
    1924              :      m = this_image() - 1
    1925              :      if (corank == 1)
    1926              :        {
    1927              :          sub(1) = m + lcobound(corank)
    1928              :          return;
    1929              :        }
    1930              :      i = rank
    1931              :      min_var = min (rank + corank - 2, rank + dim_arg - 1)
    1932              :      for (;;)
    1933              :        {
    1934              :          extent = gfc_extent(i)
    1935              :          ml = m
    1936              :          m  = m/extent
    1937              :          if (i >= min_var)
    1938              :            goto exit_label
    1939              :          i++
    1940              :        }
    1941              :      exit_label:
    1942              :      sub(dim_arg) = (dim_arg < corank) ? ml - m*extent + lcobound(dim_arg)
    1943              :                                        : m + lcobound(corank)
    1944              :   */
    1945              : 
    1946              :   /* this_image () - 1.  */
    1947          318 :   tmp
    1948          318 :     = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1, team);
    1949          318 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
    1950              :                          fold_convert (type, tmp), build_int_cst (type, 1));
    1951          318 :   if (corank == 1)
    1952              :     {
    1953              :       /* sub(1) = m + lcobound(corank).  */
    1954            8 :       lbound = gfc_conv_descriptor_lbound_get (desc,
    1955            8 :                         build_int_cst (TREE_TYPE (gfc_array_index_type),
    1956            8 :                                        corank+rank-1));
    1957            8 :       lbound = fold_convert (type, lbound);
    1958            8 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
    1959              : 
    1960            8 :       se->expr = tmp;
    1961            8 :       return;
    1962              :     }
    1963              : 
    1964          310 :   m = gfc_create_var (type, NULL);
    1965          310 :   ml = gfc_create_var (type, NULL);
    1966          310 :   loop_var = gfc_create_var (integer_type_node, NULL);
    1967          310 :   min_var = gfc_create_var (integer_type_node, NULL);
    1968              : 
    1969              :   /* m = this_image () - 1.  */
    1970          310 :   gfc_add_modify (&se->pre, m, tmp);
    1971              : 
    1972              :   /* min_var = min (rank + corank-2, rank + dim_arg - 1).  */
    1973          310 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
    1974              :                          fold_convert (integer_type_node, dim_arg),
    1975          310 :                          build_int_cst (integer_type_node, rank - 1));
    1976          310 :   tmp = fold_build2_loc (input_location, MIN_EXPR, integer_type_node,
    1977          310 :                          build_int_cst (integer_type_node, rank + corank - 2),
    1978              :                          tmp);
    1979          310 :   gfc_add_modify (&se->pre, min_var, tmp);
    1980              : 
    1981              :   /* i = rank.  */
    1982          310 :   tmp = build_int_cst (integer_type_node, rank);
    1983          310 :   gfc_add_modify (&se->pre, loop_var, tmp);
    1984              : 
    1985          310 :   exit_label = gfc_build_label_decl (NULL_TREE);
    1986          310 :   TREE_USED (exit_label) = 1;
    1987              : 
    1988              :   /* Loop body.  */
    1989          310 :   gfc_init_block (&loop);
    1990              : 
    1991              :   /* ml = m.  */
    1992          310 :   gfc_add_modify (&loop, ml, m);
    1993              : 
    1994              :   /* extent = ...  */
    1995          310 :   lbound = gfc_conv_descriptor_lbound_get (desc, loop_var);
    1996          310 :   ubound = gfc_conv_descriptor_ubound_get (desc, loop_var);
    1997          310 :   extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
    1998          310 :   extent = fold_convert (type, extent);
    1999              : 
    2000              :   /* m = m/extent.  */
    2001          310 :   gfc_add_modify (&loop, m,
    2002              :                   fold_build2_loc (input_location, TRUNC_DIV_EXPR, type,
    2003              :                           m, extent));
    2004              : 
    2005              :   /* Exit condition:  if (i >= min_var) goto exit_label.  */
    2006          310 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, loop_var,
    2007              :                   min_var);
    2008          310 :   tmp = build1_v (GOTO_EXPR, exit_label);
    2009          310 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    2010              :                          build_empty_stmt (input_location));
    2011          310 :   gfc_add_expr_to_block (&loop, tmp);
    2012              : 
    2013              :   /* Increment loop variable: i++.  */
    2014          310 :   gfc_add_modify (&loop, loop_var,
    2015              :                   fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
    2016              :                                    loop_var,
    2017              :                                    integer_one_node));
    2018              : 
    2019              :   /* Making the loop... actually loop!  */
    2020          310 :   tmp = gfc_finish_block (&loop);
    2021          310 :   tmp = build1_v (LOOP_EXPR, tmp);
    2022          310 :   gfc_add_expr_to_block (&se->pre, tmp);
    2023              : 
    2024              :   /* The exit label.  */
    2025          310 :   tmp = build1_v (LABEL_EXPR, exit_label);
    2026          310 :   gfc_add_expr_to_block (&se->pre, tmp);
    2027              : 
    2028              :   /*  sub(co_dim) = (co_dim < corank) ? ml - m*extent + lcobound(dim_arg)
    2029              :                                       : m + lcobound(corank) */
    2030              : 
    2031          310 :   cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, dim_arg,
    2032          310 :                           build_int_cst (TREE_TYPE (dim_arg), corank));
    2033              : 
    2034          310 :   lbound = gfc_conv_descriptor_lbound_get (desc,
    2035              :                 fold_build2_loc (input_location, PLUS_EXPR,
    2036              :                                  gfc_array_index_type, dim_arg,
    2037          310 :                                  build_int_cst (TREE_TYPE (dim_arg), rank-1)));
    2038          310 :   lbound = fold_convert (type, lbound);
    2039              : 
    2040          310 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, type, ml,
    2041              :                          fold_build2_loc (input_location, MULT_EXPR, type,
    2042              :                                           m, extent));
    2043          310 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
    2044              : 
    2045          310 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
    2046              :                               fold_build2_loc (input_location, PLUS_EXPR, type,
    2047              :                                                m, lbound));
    2048              : }
    2049              : 
    2050              : 
    2051              : /* Convert a call to image_status.  */
    2052              : 
    2053              : static void
    2054           25 : conv_intrinsic_image_status (gfc_se *se, gfc_expr *expr)
    2055              : {
    2056           25 :   unsigned int num_args;
    2057           25 :   tree *args, tmp;
    2058              : 
    2059           25 :   num_args = gfc_intrinsic_argument_list_length (expr);
    2060           25 :   args = XALLOCAVEC (tree, num_args);
    2061           25 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    2062              :   /* In args[0] the number of the image the status is desired for has to be
    2063              :      given.  */
    2064              : 
    2065           25 :   if (flag_coarray == GFC_FCOARRAY_SINGLE)
    2066              :     {
    2067            0 :       tree arg;
    2068            0 :       arg = gfc_evaluate_now (args[0], &se->pre);
    2069            0 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    2070              :                              fold_convert (integer_type_node, arg),
    2071              :                              integer_one_node);
    2072            0 :       tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
    2073              :                              tmp, integer_zero_node,
    2074              :                              build_int_cst (integer_type_node,
    2075              :                                             GFC_STAT_STOPPED_IMAGE));
    2076              :     }
    2077           25 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
    2078              :     /* The team is optional and therefore needs to be a pointer to the opaque
    2079              :        pointer.  */
    2080           29 :     tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_image_status, 2,
    2081              :                                args[0],
    2082              :                                num_args < 2
    2083              :                                  ? null_pointer_node
    2084            4 :                                  : gfc_build_addr_expr (NULL_TREE, args[1]));
    2085              :   else
    2086            0 :     gcc_unreachable ();
    2087              : 
    2088           25 :   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
    2089           25 : }
    2090              : 
    2091              : static void
    2092           21 : conv_intrinsic_team_number (gfc_se *se, gfc_expr *expr)
    2093              : {
    2094           21 :   unsigned int num_args;
    2095              : 
    2096           21 :   tree *args, tmp;
    2097              : 
    2098           21 :   num_args = gfc_intrinsic_argument_list_length (expr);
    2099           21 :   args = XALLOCAVEC (tree, num_args);
    2100           21 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    2101              : 
    2102           21 :   if (flag_coarray ==
    2103           18 :       GFC_FCOARRAY_SINGLE && expr->value.function.actual->expr)
    2104            0 :     tmp = gfc_evaluate_now (args[0], &se->pre);
    2105           21 :   else if (flag_coarray == GFC_FCOARRAY_SINGLE)
    2106              :     {
    2107              :       // the value -1 represents that no team has been created yet
    2108           18 :       tmp = build_int_cst (integer_type_node, -1);
    2109              :     }
    2110            3 :   else if (flag_coarray == GFC_FCOARRAY_LIB && expr->value.function.actual->expr)
    2111            0 :     tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
    2112              :                                args[0]);
    2113            3 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
    2114            3 :     tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
    2115              :                                null_pointer_node);
    2116              :   else
    2117            0 :     gcc_unreachable ();
    2118              : 
    2119           21 :   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
    2120           21 : }
    2121              : 
    2122              : 
    2123              : static void
    2124          193 : trans_image_index (gfc_se * se, gfc_expr *expr)
    2125              : {
    2126          193 :   tree num_images, cond, coindex, type, lbound, ubound, desc, subdesc, tmp,
    2127          193 :     invalid_bound, team = null_pointer_node, team_number = null_pointer_node;
    2128          193 :   gfc_se argse, subse;
    2129          193 :   int rank, corank, codim;
    2130              : 
    2131          193 :   type = gfc_get_int_type (gfc_default_integer_kind);
    2132          193 :   corank = expr->value.function.actual->expr->corank;
    2133          193 :   rank = expr->value.function.actual->expr->rank;
    2134              : 
    2135              :   /* Obtain the descriptor of the COARRAY.  */
    2136          193 :   gfc_init_se (&argse, NULL);
    2137          193 :   argse.want_coarray = 1;
    2138          193 :   gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
    2139          193 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2140          193 :   gfc_add_block_to_block (&se->post, &argse.post);
    2141          193 :   desc = argse.expr;
    2142              : 
    2143              :   /* Obtain a handle to the SUB argument.  */
    2144          193 :   gfc_init_se (&subse, NULL);
    2145          193 :   gfc_conv_expr_descriptor (&subse, expr->value.function.actual->next->expr);
    2146          193 :   gfc_add_block_to_block (&se->pre, &subse.pre);
    2147          193 :   gfc_add_block_to_block (&se->post, &subse.post);
    2148          193 :   subdesc = build_fold_indirect_ref_loc (input_location,
    2149              :                         gfc_conv_descriptor_data_get (subse.expr));
    2150              : 
    2151          193 :   if (expr->value.function.actual->next->next->expr)
    2152              :     {
    2153            0 :       gfc_init_se (&argse, NULL);
    2154            0 :       gfc_conv_expr_descriptor (&argse,
    2155            0 :                                 expr->value.function.actual->next->next->expr);
    2156            0 :       if (expr->value.function.actual->next->next->expr->ts.type == BT_DERIVED)
    2157            0 :         team = argse.expr;
    2158              :       else
    2159            0 :         team_number = gfc_build_addr_expr (
    2160              :           NULL_TREE,
    2161              :           gfc_trans_force_lval (&argse.pre,
    2162              :                                 fold_convert (integer_type_node, argse.expr)));
    2163            0 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2164            0 :       gfc_add_block_to_block (&se->post, &argse.post);
    2165              :     }
    2166              : 
    2167              :   /* Fortran 2008 does not require that the values remain in the cobounds,
    2168              :      thus we need explicitly check this - and return 0 if they are exceeded.  */
    2169              : 
    2170          193 :   lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
    2171          193 :   tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1], NULL);
    2172          193 :   invalid_bound = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2173              :                                  fold_convert (gfc_array_index_type, tmp),
    2174              :                                  lbound);
    2175              : 
    2176          443 :   for (codim = corank + rank - 2; codim >= rank; codim--)
    2177              :     {
    2178          250 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
    2179          250 :       ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
    2180          250 :       tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
    2181          250 :       cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2182              :                               fold_convert (gfc_array_index_type, tmp),
    2183              :                               lbound);
    2184          250 :       invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    2185              :                                        logical_type_node, invalid_bound, cond);
    2186          250 :       cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2187              :                               fold_convert (gfc_array_index_type, tmp),
    2188              :                               ubound);
    2189          250 :       invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    2190              :                                        logical_type_node, invalid_bound, cond);
    2191              :     }
    2192              : 
    2193          193 :   invalid_bound = gfc_unlikely (invalid_bound, PRED_FORTRAN_INVALID_BOUND);
    2194              : 
    2195              :   /* See Fortran 2008, C.10 for the following algorithm.  */
    2196              : 
    2197              :   /* coindex = sub(corank) - lcobound(n).  */
    2198          193 :   coindex = fold_convert (gfc_array_index_type,
    2199              :                           gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1],
    2200              :                                                NULL));
    2201          193 :   lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
    2202          193 :   coindex = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    2203              :                              fold_convert (gfc_array_index_type, coindex),
    2204              :                              lbound);
    2205              : 
    2206          443 :   for (codim = corank + rank - 2; codim >= rank; codim--)
    2207              :     {
    2208          250 :       tree extent, ubound;
    2209              : 
    2210              :       /* coindex = coindex*extent(codim) + sub(codim) - lcobound(codim).  */
    2211          250 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
    2212          250 :       ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
    2213          250 :       extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
    2214              : 
    2215              :       /* coindex *= extent.  */
    2216          250 :       coindex = fold_build2_loc (input_location, MULT_EXPR,
    2217              :                                  gfc_array_index_type, coindex, extent);
    2218              : 
    2219              :       /* coindex += sub(codim).  */
    2220          250 :       tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
    2221          250 :       coindex = fold_build2_loc (input_location, PLUS_EXPR,
    2222              :                                  gfc_array_index_type, coindex,
    2223              :                                  fold_convert (gfc_array_index_type, tmp));
    2224              : 
    2225              :       /* coindex -= lbound(codim).  */
    2226          250 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
    2227          250 :       coindex = fold_build2_loc (input_location, MINUS_EXPR,
    2228              :                                  gfc_array_index_type, coindex, lbound);
    2229              :     }
    2230              : 
    2231          193 :   coindex = fold_build2_loc (input_location, PLUS_EXPR, type,
    2232              :                              fold_convert(type, coindex),
    2233              :                              build_int_cst (type, 1));
    2234              : 
    2235              :   /* Return 0 if "coindex" exceeds num_images().  */
    2236              : 
    2237          193 :   if (flag_coarray == GFC_FCOARRAY_SINGLE)
    2238          108 :     num_images = build_int_cst (type, 1);
    2239              :   else
    2240              :     {
    2241           85 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
    2242              :                                  team, team_number);
    2243           85 :       num_images = fold_convert (type, tmp);
    2244              :     }
    2245              : 
    2246          193 :   tmp = gfc_create_var (type, NULL);
    2247          193 :   gfc_add_modify (&se->pre, tmp, coindex);
    2248              : 
    2249          193 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, tmp,
    2250              :                           num_images);
    2251          193 :   cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
    2252              :                           cond,
    2253              :                           fold_convert (logical_type_node, invalid_bound));
    2254          193 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
    2255              :                               build_int_cst (type, 0), tmp);
    2256          193 : }
    2257              : 
    2258              : static void
    2259          810 : trans_num_images (gfc_se * se, gfc_expr *expr)
    2260              : {
    2261          810 :   tree tmp, team = null_pointer_node, team_number = null_pointer_node;
    2262          810 :   gfc_se argse;
    2263              : 
    2264          810 :   if (expr->value.function.actual->expr)
    2265              :     {
    2266           18 :       gfc_init_se (&argse, NULL);
    2267           18 :       gfc_conv_expr_val (&argse, expr->value.function.actual->expr);
    2268           18 :       if (expr->value.function.actual->expr->ts.type == BT_DERIVED)
    2269            6 :         team = argse.expr;
    2270              :       else
    2271           12 :         team_number = gfc_build_addr_expr (
    2272              :           NULL_TREE,
    2273              :           gfc_trans_force_lval (&se->pre,
    2274              :                                 fold_convert (integer_type_node, argse.expr)));
    2275           18 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2276           18 :       gfc_add_block_to_block (&se->post, &argse.post);
    2277              :     }
    2278              : 
    2279          810 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
    2280              :                              team, team_number);
    2281          810 :   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
    2282          810 : }
    2283              : 
    2284              : 
    2285              : static void
    2286        12457 : gfc_conv_intrinsic_rank (gfc_se *se, gfc_expr *expr)
    2287              : {
    2288        12457 :   gfc_se argse;
    2289              : 
    2290        12457 :   gfc_init_se (&argse, NULL);
    2291        12457 :   argse.data_not_needed = 1;
    2292        12457 :   argse.descriptor_only = 1;
    2293              : 
    2294        12457 :   gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
    2295        12457 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2296        12457 :   gfc_add_block_to_block (&se->post, &argse.post);
    2297              : 
    2298        12457 :   se->expr = gfc_conv_descriptor_rank (argse.expr);
    2299        12457 :   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
    2300              :                            se->expr);
    2301        12457 : }
    2302              : 
    2303              : 
    2304              : static void
    2305          735 : gfc_conv_intrinsic_is_contiguous (gfc_se * se, gfc_expr * expr)
    2306              : {
    2307          735 :   gfc_expr *arg;
    2308          735 :   arg = expr->value.function.actual->expr;
    2309          735 :   gfc_conv_is_contiguous_expr (se, arg);
    2310          735 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    2311          735 : }
    2312              : 
    2313              : /* This function does the work for gfc_conv_intrinsic_is_contiguous,
    2314              :    plus it can be called directly.  */
    2315              : 
    2316              : void
    2317         2088 : gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
    2318              : {
    2319         2088 :   gfc_ss *ss;
    2320         2088 :   gfc_se argse;
    2321         2088 :   tree desc, tmp, stride, extent, cond;
    2322         2088 :   int i;
    2323         2088 :   tree fncall0;
    2324         2088 :   gfc_array_spec *as;
    2325         2088 :   gfc_symbol *sym = NULL;
    2326              : 
    2327         2088 :   if (arg->ts.type == BT_CLASS)
    2328           90 :     gfc_add_class_array_ref (arg);
    2329              : 
    2330         2088 :   if (arg->expr_type == EXPR_VARIABLE)
    2331         2052 :     sym = arg->symtree->n.sym;
    2332              : 
    2333         2088 :   ss = gfc_walk_expr (arg);
    2334         2088 :   gcc_assert (ss != gfc_ss_terminator);
    2335         2088 :   gfc_init_se (&argse, NULL);
    2336         2088 :   argse.data_not_needed = 1;
    2337         2088 :   gfc_conv_expr_descriptor (&argse, arg);
    2338              : 
    2339         2088 :   as = gfc_get_full_arrayspec_from_expr (arg);
    2340              : 
    2341              :   /* Create:  stride[0] == 1 && stride[1] == extend[0]*stride[0] && ...
    2342              :      Note in addition that zero-sized arrays don't count as contiguous.  */
    2343              : 
    2344         2088 :   if (as && as->type == AS_ASSUMED_RANK)
    2345              :     {
    2346              :       /* Build the call to is_contiguous0.  */
    2347          243 :       argse.want_pointer = 1;
    2348          243 :       gfc_conv_expr_descriptor (&argse, arg);
    2349          243 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2350          243 :       gfc_add_block_to_block (&se->post, &argse.post);
    2351          243 :       desc = gfc_evaluate_now (argse.expr, &se->pre);
    2352          243 :       fncall0 = build_call_expr_loc (input_location,
    2353              :                                      gfor_fndecl_is_contiguous0, 1, desc);
    2354          243 :       se->expr = fncall0;
    2355          243 :       se->expr = convert (boolean_type_node, se->expr);
    2356              :     }
    2357              :   else
    2358              :     {
    2359         1845 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2360         1845 :       gfc_add_block_to_block (&se->post, &argse.post);
    2361         1845 :       desc = gfc_evaluate_now (argse.expr, &se->pre);
    2362              : 
    2363         1845 :       stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[0]);
    2364         1845 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    2365         1845 :                               stride, build_int_cst (TREE_TYPE (stride), 1));
    2366              : 
    2367         2177 :       for (i = 0; i < arg->rank - 1; i++)
    2368              :         {
    2369          332 :           tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
    2370          332 :           extent = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
    2371          332 :           extent = fold_build2_loc (input_location, MINUS_EXPR,
    2372              :                                     gfc_array_index_type, extent, tmp);
    2373          332 :           extent = fold_build2_loc (input_location, PLUS_EXPR,
    2374              :                                     gfc_array_index_type, extent,
    2375              :                                     gfc_index_one_node);
    2376          332 :           tmp = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i]);
    2377          332 :           tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    2378              :                                  tmp, extent);
    2379          332 :           stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i+1]);
    2380          332 :           tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    2381              :                                  stride, tmp);
    2382          332 :           cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    2383              :                                   boolean_type_node, cond, tmp);
    2384              :         }
    2385         1845 :       se->expr = cond;
    2386              :     }
    2387              : 
    2388              :   /* A pointer that does not have the CONTIGUOUS attribute needs to be checked
    2389              :      if it points to an array whose span differs from the element size.  */
    2390         2088 :   if (as && sym && IS_POINTER(sym) && !sym->attr.contiguous)
    2391              :     {
    2392          180 :       tree span = gfc_conv_descriptor_span_get (desc);
    2393          180 :       tmp = fold_convert (TREE_TYPE (span),
    2394              :                           gfc_conv_descriptor_elem_len (desc));
    2395          180 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    2396              :                               span, tmp);
    2397          180 :       se->expr = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2398              :                                   boolean_type_node, cond,
    2399              :                                   convert (boolean_type_node, se->expr));
    2400              :     }
    2401              : 
    2402         2088 :   gfc_free_ss_chain (ss);
    2403         2088 : }
    2404              : 
    2405              : 
    2406              : /* Evaluate a single upper or lower bound.  */
    2407              : /* TODO: bound intrinsic generates way too much unnecessary code.  */
    2408              : 
    2409              : static void
    2410        16178 : gfc_conv_intrinsic_bound (gfc_se * se, gfc_expr * expr, enum gfc_isym_id op)
    2411              : {
    2412        16178 :   gfc_actual_arglist *arg;
    2413        16178 :   gfc_actual_arglist *arg2;
    2414        16178 :   tree desc;
    2415        16178 :   tree type;
    2416        16178 :   tree bound;
    2417        16178 :   tree tmp;
    2418        16178 :   tree cond, cond1;
    2419        16178 :   tree ubound;
    2420        16178 :   tree lbound;
    2421        16178 :   tree size;
    2422        16178 :   gfc_se argse;
    2423        16178 :   gfc_array_spec * as;
    2424        16178 :   bool assumed_rank_lb_one;
    2425              : 
    2426        16178 :   arg = expr->value.function.actual;
    2427        16178 :   arg2 = arg->next;
    2428              : 
    2429        16178 :   if (se->ss)
    2430              :     {
    2431              :       /* Create an implicit second parameter from the loop variable.  */
    2432         7944 :       gcc_assert (!arg2->expr || op == GFC_ISYM_SHAPE);
    2433         7944 :       gcc_assert (se->loop->dimen == 1);
    2434         7944 :       gcc_assert (se->ss->info->expr == expr);
    2435         7944 :       gfc_advance_se_ss_chain (se);
    2436         7944 :       bound = se->loop->loopvar[0];
    2437         7944 :       bound = fold_build2_loc (input_location, MINUS_EXPR,
    2438              :                                gfc_array_index_type, bound,
    2439              :                                se->loop->from[0]);
    2440              :     }
    2441              :   else
    2442              :     {
    2443              :       /* use the passed argument.  */
    2444         8234 :       gcc_assert (arg2->expr);
    2445         8234 :       gfc_init_se (&argse, NULL);
    2446         8234 :       gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
    2447         8234 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2448         8234 :       bound = argse.expr;
    2449              :       /* Convert from one based to zero based.  */
    2450         8234 :       bound = fold_build2_loc (input_location, MINUS_EXPR,
    2451              :                                gfc_array_index_type, bound,
    2452              :                                gfc_index_one_node);
    2453              :     }
    2454              : 
    2455              :   /* TODO: don't re-evaluate the descriptor on each iteration.  */
    2456              :   /* Get a descriptor for the first parameter.  */
    2457        16178 :   gfc_init_se (&argse, NULL);
    2458        16178 :   gfc_conv_expr_descriptor (&argse, arg->expr);
    2459        16178 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2460        16178 :   gfc_add_block_to_block (&se->post, &argse.post);
    2461              : 
    2462        16178 :   desc = argse.expr;
    2463              : 
    2464        16178 :   as = gfc_get_full_arrayspec_from_expr (arg->expr);
    2465              : 
    2466        16178 :   if (INTEGER_CST_P (bound))
    2467              :     {
    2468         8114 :       gcc_assert (op != GFC_ISYM_SHAPE);
    2469         7877 :       if (((!as || as->type != AS_ASSUMED_RANK)
    2470         7254 :            && wi::geu_p (wi::to_wide (bound),
    2471         7254 :                          GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))))
    2472        16228 :           || wi::gtu_p (wi::to_wide (bound), GFC_MAX_DIMENSIONS))
    2473            0 :         gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
    2474              :                    "dimension index",
    2475              :                    (op == GFC_ISYM_UBOUND) ? "UBOUND" : "LBOUND",
    2476              :                    &expr->where);
    2477              :     }
    2478              : 
    2479        16178 :   if (!INTEGER_CST_P (bound) || (as && as->type == AS_ASSUMED_RANK))
    2480              :     {
    2481         8924 :       if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2482              :         {
    2483          651 :           bound = gfc_evaluate_now (bound, &se->pre);
    2484          651 :           cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2485          651 :                                   bound, build_int_cst (TREE_TYPE (bound), 0));
    2486          651 :           if (as && as->type == AS_ASSUMED_RANK)
    2487          546 :             tmp = gfc_conv_descriptor_rank (desc);
    2488              :           else
    2489          105 :             tmp = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))];
    2490          651 :           tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    2491          651 :                                  bound, fold_convert(TREE_TYPE (bound), tmp));
    2492          651 :           cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    2493              :                                   logical_type_node, cond, tmp);
    2494          651 :           gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    2495              :                                    gfc_msg_fault);
    2496              :         }
    2497              :     }
    2498              : 
    2499              :   /* Take care of the lbound shift for assumed-rank arrays that are
    2500              :      nonallocatable and nonpointers. Those have a lbound of 1.  */
    2501        15594 :   assumed_rank_lb_one = as && as->type == AS_ASSUMED_RANK
    2502        11061 :                         && ((arg->expr->ts.type != BT_CLASS
    2503         1963 :                              && !arg->expr->symtree->n.sym->attr.allocatable
    2504         1620 :                              && !arg->expr->symtree->n.sym->attr.pointer)
    2505          896 :                             || (arg->expr->ts.type == BT_CLASS
    2506          174 :                              && !CLASS_DATA (arg->expr)->attr.allocatable
    2507          138 :                              && !CLASS_DATA (arg->expr)->attr.class_pointer));
    2508              : 
    2509        16178 :   ubound = gfc_conv_descriptor_ubound_get (desc, bound);
    2510        16178 :   lbound = gfc_conv_descriptor_lbound_get (desc, bound);
    2511        16178 :   size = fold_build2_loc (input_location, MINUS_EXPR,
    2512              :                           gfc_array_index_type, ubound, lbound);
    2513        16178 :   size = fold_build2_loc (input_location, PLUS_EXPR,
    2514              :                           gfc_array_index_type, size, gfc_index_one_node);
    2515              : 
    2516              :   /* 13.14.53: Result value for LBOUND
    2517              : 
    2518              :      Case (i): For an array section or for an array expression other than a
    2519              :                whole array or array structure component, LBOUND(ARRAY, DIM)
    2520              :                has the value 1.  For a whole array or array structure
    2521              :                component, LBOUND(ARRAY, DIM) has the value:
    2522              :                  (a) equal to the lower bound for subscript DIM of ARRAY if
    2523              :                      dimension DIM of ARRAY does not have extent zero
    2524              :                      or if ARRAY is an assumed-size array of rank DIM,
    2525              :               or (b) 1 otherwise.
    2526              : 
    2527              :      13.14.113: Result value for UBOUND
    2528              : 
    2529              :      Case (i): For an array section or for an array expression other than a
    2530              :                whole array or array structure component, UBOUND(ARRAY, DIM)
    2531              :                has the value equal to the number of elements in the given
    2532              :                dimension; otherwise, it has a value equal to the upper bound
    2533              :                for subscript DIM of ARRAY if dimension DIM of ARRAY does
    2534              :                not have size zero and has value zero if dimension DIM has
    2535              :                size zero.  */
    2536              : 
    2537        16178 :   if (op == GFC_ISYM_LBOUND && assumed_rank_lb_one)
    2538          532 :     se->expr = gfc_index_one_node;
    2539        15646 :   else if (as)
    2540              :     {
    2541        15062 :       if (op == GFC_ISYM_UBOUND)
    2542              :         {
    2543         5370 :           cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2544              :                                   size, gfc_index_zero_node);
    2545        10136 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    2546              :                                       gfc_array_index_type, cond,
    2547              :                                       (assumed_rank_lb_one ? size : ubound),
    2548              :                                       gfc_index_zero_node);
    2549              :         }
    2550         9692 :       else if (op == GFC_ISYM_LBOUND)
    2551              :         {
    2552         4893 :           cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2553              :                                   size, gfc_index_zero_node);
    2554         4893 :           if (as->type == AS_ASSUMED_SIZE)
    2555              :             {
    2556           98 :               cond1 = fold_build2_loc (input_location, EQ_EXPR,
    2557              :                                        logical_type_node, bound,
    2558           98 :                                        build_int_cst (TREE_TYPE (bound),
    2559           98 :                                                       arg->expr->rank - 1));
    2560           98 :               cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    2561              :                                       logical_type_node, cond, cond1);
    2562              :             }
    2563         4893 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    2564              :                                       gfc_array_index_type, cond,
    2565              :                                       lbound, gfc_index_one_node);
    2566              :         }
    2567         4799 :       else if (op == GFC_ISYM_SHAPE)
    2568         4799 :         se->expr = fold_build2_loc (input_location, MAX_EXPR,
    2569              :                                     gfc_array_index_type, size,
    2570              :                                     gfc_index_zero_node);
    2571              :       else
    2572            0 :         gcc_unreachable ();
    2573              : 
    2574              :       /* According to F2018 16.9.172, para 5, an assumed rank object,
    2575              :          argument associated with and assumed size array, has the ubound
    2576              :          of the final dimension set to -1 and UBOUND must return this.
    2577              :          Similarly for the SHAPE intrinsic.  */
    2578        15062 :       if (op != GFC_ISYM_LBOUND && assumed_rank_lb_one)
    2579              :         {
    2580          811 :           tree minus_one = build_int_cst (gfc_array_index_type, -1);
    2581          811 :           tree rank = fold_convert (gfc_array_index_type,
    2582              :                                     gfc_conv_descriptor_rank (desc));
    2583          811 :           rank = fold_build2_loc (input_location, PLUS_EXPR,
    2584              :                                   gfc_array_index_type, rank, minus_one);
    2585              : 
    2586              :           /* Fix the expression to stop it from becoming even more
    2587              :              complicated.  */
    2588          811 :           se->expr = gfc_evaluate_now (se->expr, &se->pre);
    2589              : 
    2590              :           /* Descriptors for assumed-size arrays have ubound = -1
    2591              :              in the last dimension.  */
    2592          811 :           cond1 = fold_build2_loc (input_location, EQ_EXPR,
    2593              :                                    logical_type_node, ubound, minus_one);
    2594          811 :           cond = fold_build2_loc (input_location, EQ_EXPR,
    2595              :                                   logical_type_node, bound, rank);
    2596          811 :           cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    2597              :                                   logical_type_node, cond, cond1);
    2598          811 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    2599              :                                       gfc_array_index_type, cond,
    2600              :                                       minus_one, se->expr);
    2601              :         }
    2602              :     }
    2603              :   else   /* as is null; this is an old-fashioned 1-based array.  */
    2604              :     {
    2605          584 :       if (op != GFC_ISYM_LBOUND)
    2606              :         {
    2607          482 :           se->expr = fold_build2_loc (input_location, MAX_EXPR,
    2608              :                                       gfc_array_index_type, size,
    2609              :                                       gfc_index_zero_node);
    2610              :         }
    2611              :       else
    2612          102 :         se->expr = gfc_index_one_node;
    2613              :     }
    2614              : 
    2615              : 
    2616        16178 :   type = gfc_typenode_for_spec (&expr->ts);
    2617        16178 :   se->expr = convert (type, se->expr);
    2618        16178 : }
    2619              : 
    2620              : 
    2621              : static void
    2622          666 : conv_intrinsic_cobound (gfc_se * se, gfc_expr * expr)
    2623              : {
    2624          666 :   gfc_actual_arglist *arg;
    2625          666 :   gfc_actual_arglist *arg2;
    2626          666 :   gfc_se argse;
    2627          666 :   tree bound, lbound, resbound, resbound2, desc, cond, tmp;
    2628          666 :   tree type;
    2629          666 :   int corank;
    2630              : 
    2631          666 :   gcc_assert (expr->value.function.isym->id == GFC_ISYM_LCOBOUND
    2632              :               || expr->value.function.isym->id == GFC_ISYM_UCOBOUND
    2633              :               || expr->value.function.isym->id == GFC_ISYM_COSHAPE
    2634              :               || expr->value.function.isym->id == GFC_ISYM_THIS_IMAGE);
    2635              : 
    2636          666 :   arg = expr->value.function.actual;
    2637          666 :   arg2 = arg->next;
    2638              : 
    2639          666 :   gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
    2640          666 :   corank = arg->expr->corank;
    2641              : 
    2642          666 :   gfc_init_se (&argse, NULL);
    2643          666 :   argse.want_coarray = 1;
    2644              : 
    2645          666 :   gfc_conv_expr_descriptor (&argse, arg->expr);
    2646          666 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2647          666 :   gfc_add_block_to_block (&se->post, &argse.post);
    2648          666 :   desc = argse.expr;
    2649              : 
    2650          666 :   if (se->ss)
    2651              :     {
    2652              :       /* Create an implicit second parameter from the loop variable.  */
    2653          238 :       gcc_assert (!arg2->expr
    2654              :                   || expr->value.function.isym->id == GFC_ISYM_COSHAPE);
    2655          238 :       gcc_assert (corank > 0);
    2656          238 :       gcc_assert (se->loop->dimen == 1);
    2657          238 :       gcc_assert (se->ss->info->expr == expr);
    2658              : 
    2659          238 :       bound = se->loop->loopvar[0];
    2660          476 :       bound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    2661          238 :                                bound, gfc_rank_cst[arg->expr->rank]);
    2662          238 :       gfc_advance_se_ss_chain (se);
    2663              :     }
    2664          428 :   else if (expr->value.function.isym->id == GFC_ISYM_COSHAPE)
    2665            0 :     bound = gfc_index_zero_node;
    2666              :   else
    2667              :     {
    2668          428 :       gcc_assert (arg2->expr);
    2669          428 :       gfc_init_se (&argse, NULL);
    2670          428 :       gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
    2671          428 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2672          428 :       bound = argse.expr;
    2673              : 
    2674          428 :       if (INTEGER_CST_P (bound))
    2675              :         {
    2676          334 :           if (wi::ltu_p (wi::to_wide (bound), 1)
    2677          668 :               || wi::gtu_p (wi::to_wide (bound),
    2678          334 :                             GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
    2679            0 :             gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
    2680            0 :                        "dimension index", expr->value.function.isym->name,
    2681              :                        &expr->where);
    2682              :         }
    2683           94 :       else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2684              :         {
    2685           36 :           bound = gfc_evaluate_now (bound, &se->pre);
    2686           36 :           cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2687           36 :                                   bound, build_int_cst (TREE_TYPE (bound), 1));
    2688           36 :           tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
    2689           36 :           tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2690              :                                  bound, tmp);
    2691           36 :           cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    2692              :                                   logical_type_node, cond, tmp);
    2693           36 :           gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    2694              :                                    gfc_msg_fault);
    2695              :         }
    2696              : 
    2697              : 
    2698              :       /* Subtract 1 to get to zero based and add dimensions.  */
    2699          428 :       switch (arg->expr->rank)
    2700              :         {
    2701           70 :         case 0:
    2702           70 :           bound = fold_build2_loc (input_location, MINUS_EXPR,
    2703              :                                    gfc_array_index_type, bound,
    2704              :                                    gfc_index_one_node);
    2705              :         case 1:
    2706              :           break;
    2707           38 :         default:
    2708           38 :           bound = fold_build2_loc (input_location, PLUS_EXPR,
    2709              :                                    gfc_array_index_type, bound,
    2710           38 :                                    gfc_rank_cst[arg->expr->rank - 1]);
    2711              :         }
    2712              :     }
    2713              : 
    2714          666 :   resbound = gfc_conv_descriptor_lbound_get (desc, bound);
    2715              : 
    2716              :   /* COSHAPE needs the lower cobound and so it is stashed here before resbound
    2717              :      is overwritten.  */
    2718          666 :   lbound = NULL_TREE;
    2719          666 :   if (expr->value.function.isym->id == GFC_ISYM_COSHAPE)
    2720            4 :     lbound = resbound;
    2721              : 
    2722              :   /* Handle UCOBOUND with special handling of the last codimension.  */
    2723          666 :   if (expr->value.function.isym->id == GFC_ISYM_UCOBOUND
    2724          422 :       || expr->value.function.isym->id == GFC_ISYM_COSHAPE)
    2725              :     {
    2726              :       /* Last codimension: For -fcoarray=single just return
    2727              :          the lcobound - otherwise add
    2728              :            ceiling (real (num_images ()) / real (size)) - 1
    2729              :          = (num_images () + size - 1) / size - 1
    2730              :          = (num_images - 1) / size(),
    2731              :          where size is the product of the extent of all but the last
    2732              :          codimension.  */
    2733              : 
    2734          248 :       if (flag_coarray != GFC_FCOARRAY_SINGLE && corank > 1)
    2735              :         {
    2736           64 :           tree cosize;
    2737              : 
    2738           64 :           cosize = gfc_conv_descriptor_cosize (desc, arg->expr->rank, corank);
    2739           64 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
    2740              :                                      2, null_pointer_node, null_pointer_node);
    2741           64 :           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           64 :           tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    2746              :                                  gfc_array_index_type, tmp,
    2747              :                                  fold_convert (gfc_array_index_type, cosize));
    2748           64 :           resbound = fold_build2_loc (input_location, PLUS_EXPR,
    2749              :                                       gfc_array_index_type, resbound, tmp);
    2750           64 :         }
    2751          184 :       else if (flag_coarray != GFC_FCOARRAY_SINGLE)
    2752              :         {
    2753              :           /* ubound = lbound + num_images() - 1.  */
    2754           44 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
    2755              :                                      2, null_pointer_node, null_pointer_node);
    2756           44 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2757              :                                  gfc_array_index_type,
    2758              :                                  fold_convert (gfc_array_index_type, tmp),
    2759              :                                  build_int_cst (gfc_array_index_type, 1));
    2760           44 :           resbound = fold_build2_loc (input_location, PLUS_EXPR,
    2761              :                                       gfc_array_index_type, resbound, tmp);
    2762              :         }
    2763              : 
    2764          248 :       if (corank > 1)
    2765              :         {
    2766          171 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    2767              :                                   bound,
    2768          171 :                                   build_int_cst (TREE_TYPE (bound),
    2769          171 :                                                  arg->expr->rank + corank - 1));
    2770              : 
    2771          171 :           resbound2 = gfc_conv_descriptor_ubound_get (desc, bound);
    2772          171 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    2773              :                                       gfc_array_index_type, cond,
    2774              :                                       resbound, resbound2);
    2775              :         }
    2776              :       else
    2777           77 :         se->expr = resbound;
    2778              : 
    2779              :       /* Get the coshape for this dimension.  */
    2780          248 :       if (expr->value.function.isym->id == GFC_ISYM_COSHAPE)
    2781              :         {
    2782            4 :           gcc_assert (lbound != NULL_TREE);
    2783            4 :           se->expr = fold_build2_loc (input_location, MINUS_EXPR,
    2784              :                                       gfc_array_index_type,
    2785              :                                       se->expr, lbound);
    2786            4 :           se->expr = fold_build2_loc (input_location, PLUS_EXPR,
    2787              :                                       gfc_array_index_type,
    2788              :                                       se->expr, gfc_index_one_node);
    2789              :         }
    2790              :     }
    2791              :   else
    2792          418 :     se->expr = resbound;
    2793              : 
    2794          666 :   type = gfc_typenode_for_spec (&expr->ts);
    2795          666 :   se->expr = convert (type, se->expr);
    2796          666 : }
    2797              : 
    2798              : 
    2799              : static void
    2800         2256 : conv_intrinsic_stride (gfc_se * se, gfc_expr * expr)
    2801              : {
    2802         2256 :   gfc_actual_arglist *array_arg;
    2803         2256 :   gfc_actual_arglist *dim_arg;
    2804         2256 :   gfc_se argse;
    2805         2256 :   tree desc, tmp;
    2806              : 
    2807         2256 :   array_arg = expr->value.function.actual;
    2808         2256 :   dim_arg = array_arg->next;
    2809              : 
    2810         2256 :   gcc_assert (array_arg->expr->expr_type == EXPR_VARIABLE);
    2811              : 
    2812         2256 :   gfc_init_se (&argse, NULL);
    2813         2256 :   gfc_conv_expr_descriptor (&argse, array_arg->expr);
    2814         2256 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2815         2256 :   gfc_add_block_to_block (&se->post, &argse.post);
    2816         2256 :   desc = argse.expr;
    2817              : 
    2818         2256 :   gcc_assert (dim_arg->expr);
    2819         2256 :   gfc_init_se (&argse, NULL);
    2820         2256 :   gfc_conv_expr_type (&argse, dim_arg->expr, gfc_array_index_type);
    2821         2256 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2822         2256 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    2823              :                          argse.expr, gfc_index_one_node);
    2824         2256 :   se->expr = gfc_conv_descriptor_stride_get (desc, tmp);
    2825         2256 : }
    2826              : 
    2827              : static void
    2828         7866 : gfc_conv_intrinsic_abs (gfc_se * se, gfc_expr * expr)
    2829              : {
    2830         7866 :   tree arg, cabs;
    2831              : 
    2832         7866 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    2833              : 
    2834         7866 :   switch (expr->value.function.actual->expr->ts.type)
    2835              :     {
    2836         6860 :     case BT_INTEGER:
    2837         6860 :     case BT_REAL:
    2838         6860 :       se->expr = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (arg),
    2839              :                                   arg);
    2840         6860 :       break;
    2841              : 
    2842         1006 :     case BT_COMPLEX:
    2843         1006 :       cabs = gfc_builtin_decl_for_float_kind (BUILT_IN_CABS, expr->ts.kind);
    2844         1006 :       se->expr = build_call_expr_loc (input_location, cabs, 1, arg);
    2845         1006 :       break;
    2846              : 
    2847            0 :     default:
    2848            0 :       gcc_unreachable ();
    2849              :     }
    2850         7866 : }
    2851              : 
    2852              : 
    2853              : /* Create a complex value from one or two real components.  */
    2854              : 
    2855              : static void
    2856          491 : gfc_conv_intrinsic_cmplx (gfc_se * se, gfc_expr * expr, int both)
    2857              : {
    2858          491 :   tree real;
    2859          491 :   tree imag;
    2860          491 :   tree type;
    2861          491 :   tree *args;
    2862          491 :   unsigned int num_args;
    2863              : 
    2864          491 :   num_args = gfc_intrinsic_argument_list_length (expr);
    2865          491 :   args = XALLOCAVEC (tree, num_args);
    2866              : 
    2867          491 :   type = gfc_typenode_for_spec (&expr->ts);
    2868          491 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    2869          491 :   real = convert (TREE_TYPE (type), args[0]);
    2870          491 :   if (both)
    2871          447 :     imag = convert (TREE_TYPE (type), args[1]);
    2872           44 :   else if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE)
    2873              :     {
    2874           30 :       imag = fold_build1_loc (input_location, IMAGPART_EXPR,
    2875           30 :                               TREE_TYPE (TREE_TYPE (args[0])), args[0]);
    2876           30 :       imag = convert (TREE_TYPE (type), imag);
    2877              :     }
    2878              :   else
    2879           14 :     imag = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
    2880              : 
    2881          491 :   se->expr = fold_build2_loc (input_location, COMPLEX_EXPR, type, real, imag);
    2882          491 : }
    2883              : 
    2884              : 
    2885              : /* Remainder function MOD(A, P) = A - INT(A / P) * P
    2886              :                       MODULO(A, P) = A - FLOOR (A / P) * P
    2887              : 
    2888              :    The obvious algorithms above are numerically instable for large
    2889              :    arguments, hence these intrinsics are instead implemented via calls
    2890              :    to the fmod family of functions.  It is the responsibility of the
    2891              :    user to ensure that the second argument is non-zero.  */
    2892              : 
    2893              : static void
    2894         3638 : gfc_conv_intrinsic_mod (gfc_se * se, gfc_expr * expr, int modulo)
    2895              : {
    2896         3638 :   tree type;
    2897         3638 :   tree tmp;
    2898         3638 :   tree test;
    2899         3638 :   tree test2;
    2900         3638 :   tree fmod;
    2901         3638 :   tree zero;
    2902         3638 :   tree args[2];
    2903              : 
    2904         3638 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    2905              : 
    2906         3638 :   switch (expr->ts.type)
    2907              :     {
    2908         3485 :     case BT_INTEGER:
    2909              :       /* Integer case is easy, we've got a builtin op.  */
    2910         3485 :       type = TREE_TYPE (args[0]);
    2911              : 
    2912         3485 :       if (modulo)
    2913          411 :        se->expr = fold_build2_loc (input_location, FLOOR_MOD_EXPR, type,
    2914              :                                    args[0], args[1]);
    2915              :       else
    2916         3074 :        se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
    2917              :                                    args[0], args[1]);
    2918              :       break;
    2919              : 
    2920           30 :     case BT_UNSIGNED:
    2921              :       /* Even easier, we only need one.  */
    2922           30 :       type = TREE_TYPE (args[0]);
    2923           30 :       se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
    2924              :                                   args[0], args[1]);
    2925           30 :       break;
    2926              : 
    2927          123 :     case BT_REAL:
    2928          123 :       fmod = NULL_TREE;
    2929              :       /* Check if we have a builtin fmod.  */
    2930          123 :       fmod = gfc_builtin_decl_for_float_kind (BUILT_IN_FMOD, expr->ts.kind);
    2931              : 
    2932              :       /* The builtin should always be available.  */
    2933          123 :       gcc_assert (fmod != NULL_TREE);
    2934              : 
    2935          123 :       tmp = build_addr (fmod);
    2936          123 :       se->expr = build_call_array_loc (input_location,
    2937          123 :                                        TREE_TYPE (TREE_TYPE (fmod)),
    2938              :                                        tmp, 2, args);
    2939          123 :       if (modulo == 0)
    2940          123 :         return;
    2941              : 
    2942           25 :       type = TREE_TYPE (args[0]);
    2943              : 
    2944           25 :       args[0] = gfc_evaluate_now (args[0], &se->pre);
    2945           25 :       args[1] = gfc_evaluate_now (args[1], &se->pre);
    2946              : 
    2947              :       /* Definition:
    2948              :          modulo = arg - floor (arg/arg2) * arg2
    2949              : 
    2950              :          In order to calculate the result accurately, we use the fmod
    2951              :          function as follows.
    2952              : 
    2953              :          res = fmod (arg, arg2);
    2954              :          if (res)
    2955              :            {
    2956              :              if ((arg < 0) xor (arg2 < 0))
    2957              :                res += arg2;
    2958              :            }
    2959              :          else
    2960              :            res = copysign (0., arg2);
    2961              : 
    2962              :          => As two nested ternary exprs:
    2963              : 
    2964              :          res = res ? (((arg < 0) xor (arg2 < 0)) ? res + arg2 : res)
    2965              :                : copysign (0., arg2);
    2966              : 
    2967              :       */
    2968              : 
    2969           25 :       zero = gfc_build_const (type, integer_zero_node);
    2970           25 :       tmp = gfc_evaluate_now (se->expr, &se->pre);
    2971           25 :       if (!flag_signed_zeros)
    2972              :         {
    2973            1 :           test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2974              :                                   args[0], zero);
    2975            1 :           test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2976              :                                    args[1], zero);
    2977            1 :           test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
    2978              :                                    logical_type_node, test, test2);
    2979            1 :           test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    2980              :                                   tmp, zero);
    2981            1 :           test = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    2982              :                                   logical_type_node, test, test2);
    2983            1 :           test = gfc_evaluate_now (test, &se->pre);
    2984            1 :           se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
    2985              :                                       fold_build2_loc (input_location,
    2986              :                                                        PLUS_EXPR,
    2987              :                                                        type, tmp, args[1]),
    2988              :                                       tmp);
    2989              :         }
    2990              :       else
    2991              :         {
    2992           24 :           tree expr1, copysign, cscall;
    2993           24 :           copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN,
    2994              :                                                       expr->ts.kind);
    2995           24 :           test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2996              :                                   args[0], zero);
    2997           24 :           test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2998              :                                    args[1], zero);
    2999           24 :           test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
    3000              :                                    logical_type_node, test, test2);
    3001           24 :           expr1 = fold_build3_loc (input_location, COND_EXPR, type, test2,
    3002              :                                    fold_build2_loc (input_location,
    3003              :                                                     PLUS_EXPR,
    3004              :                                                     type, tmp, args[1]),
    3005              :                                    tmp);
    3006           24 :           test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    3007              :                                   tmp, zero);
    3008           24 :           cscall = build_call_expr_loc (input_location, copysign, 2, zero,
    3009              :                                         args[1]);
    3010           24 :           se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
    3011              :                                       expr1, cscall);
    3012              :         }
    3013              :       return;
    3014              : 
    3015            0 :     default:
    3016            0 :       gcc_unreachable ();
    3017              :     }
    3018              : }
    3019              : 
    3020              : /* DSHIFTL(I,J,S) = (I << S) | (J >> (BITSIZE(J) - S))
    3021              :    DSHIFTR(I,J,S) = (I << (BITSIZE(I) - S)) | (J >> S)
    3022              :    where the right shifts are logical (i.e. 0's are shifted in).
    3023              :    Because SHIFT_EXPR's want shifts strictly smaller than the integral
    3024              :    type width, we have to special-case both S == 0 and S == BITSIZE(J):
    3025              :      DSHIFTL(I,J,0) = I
    3026              :      DSHIFTL(I,J,BITSIZE) = J
    3027              :      DSHIFTR(I,J,0) = J
    3028              :      DSHIFTR(I,J,BITSIZE) = I.  */
    3029              : 
    3030              : static void
    3031          132 : gfc_conv_intrinsic_dshift (gfc_se * se, gfc_expr * expr, bool dshiftl)
    3032              : {
    3033          132 :   tree type, utype, stype, arg1, arg2, shift, res, left, right;
    3034          132 :   tree args[3], cond, tmp;
    3035          132 :   int bitsize;
    3036              : 
    3037          132 :   gfc_conv_intrinsic_function_args (se, expr, args, 3);
    3038              : 
    3039          132 :   gcc_assert (TREE_TYPE (args[0]) == TREE_TYPE (args[1]));
    3040          132 :   type = TREE_TYPE (args[0]);
    3041          132 :   bitsize = TYPE_PRECISION (type);
    3042          132 :   utype = unsigned_type_for (type);
    3043          132 :   stype = TREE_TYPE (args[2]);
    3044              : 
    3045          132 :   arg1 = gfc_evaluate_now (args[0], &se->pre);
    3046          132 :   arg2 = gfc_evaluate_now (args[1], &se->pre);
    3047          132 :   shift = gfc_evaluate_now (args[2], &se->pre);
    3048              : 
    3049              :   /* The generic case.  */
    3050          132 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, stype,
    3051          132 :                          build_int_cst (stype, bitsize), shift);
    3052          198 :   left = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3053              :                           arg1, dshiftl ? shift : tmp);
    3054              : 
    3055          198 :   right = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
    3056              :                            fold_convert (utype, arg2), dshiftl ? tmp : shift);
    3057          132 :   right = fold_convert (type, right);
    3058              : 
    3059          132 :   res = fold_build2_loc (input_location, BIT_IOR_EXPR, type, left, right);
    3060              : 
    3061              :   /* Special cases.  */
    3062          132 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
    3063              :                           build_int_cst (stype, 0));
    3064          198 :   res = fold_build3_loc (input_location, COND_EXPR, type, cond,
    3065              :                          dshiftl ? arg1 : arg2, res);
    3066              : 
    3067          132 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
    3068          132 :                           build_int_cst (stype, bitsize));
    3069          198 :   res = fold_build3_loc (input_location, COND_EXPR, type, cond,
    3070              :                          dshiftl ? arg2 : arg1, res);
    3071              : 
    3072          132 :   se->expr = res;
    3073          132 : }
    3074              : 
    3075              : 
    3076              : /* Positive difference DIM (x, y) = ((x - y) < 0) ? 0 : x - y.  */
    3077              : 
    3078              : static void
    3079           96 : gfc_conv_intrinsic_dim (gfc_se * se, gfc_expr * expr)
    3080              : {
    3081           96 :   tree val;
    3082           96 :   tree tmp;
    3083           96 :   tree type;
    3084           96 :   tree zero;
    3085           96 :   tree args[2];
    3086              : 
    3087           96 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    3088           96 :   type = TREE_TYPE (args[0]);
    3089              : 
    3090           96 :   val = fold_build2_loc (input_location, MINUS_EXPR, type, args[0], args[1]);
    3091           96 :   val = gfc_evaluate_now (val, &se->pre);
    3092              : 
    3093           96 :   zero = gfc_build_const (type, integer_zero_node);
    3094           96 :   tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node, val, zero);
    3095           96 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, zero, val);
    3096           96 : }
    3097              : 
    3098              : 
    3099              : /* SIGN(A, B) is absolute value of A times sign of B.
    3100              :    The real value versions use library functions to ensure the correct
    3101              :    handling of negative zero.  Integer case implemented as:
    3102              :    SIGN(A, B) = { tmp = (A ^ B) >> C; (A + tmp) ^ tmp }
    3103              :   */
    3104              : 
    3105              : static void
    3106          423 : gfc_conv_intrinsic_sign (gfc_se * se, gfc_expr * expr)
    3107              : {
    3108          423 :   tree tmp;
    3109          423 :   tree type;
    3110          423 :   tree args[2];
    3111              : 
    3112          423 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    3113          423 :   if (expr->ts.type == BT_REAL)
    3114              :     {
    3115          161 :       tree abs;
    3116              : 
    3117          161 :       tmp = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
    3118          161 :       abs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
    3119              : 
    3120              :       /* We explicitly have to ignore the minus sign. We do so by using
    3121              :          result = (arg1 == 0) ? abs(arg0) : copysign(arg0, arg1).  */
    3122          161 :       if (!flag_sign_zero
    3123          197 :           && MODE_HAS_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (args[1]))))
    3124              :         {
    3125           12 :           tree cond, zero;
    3126           12 :           zero = build_real_from_int_cst (TREE_TYPE (args[1]), integer_zero_node);
    3127           12 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3128              :                                   args[1], zero);
    3129           24 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    3130           12 :                                   TREE_TYPE (args[0]), cond,
    3131              :                                   build_call_expr_loc (input_location, abs, 1,
    3132              :                                                        args[0]),
    3133              :                                   build_call_expr_loc (input_location, tmp, 2,
    3134              :                                                        args[0], args[1]));
    3135              :         }
    3136              :       else
    3137          149 :         se->expr = build_call_expr_loc (input_location, tmp, 2,
    3138              :                                         args[0], args[1]);
    3139          161 :       return;
    3140              :     }
    3141              : 
    3142              :   /* Having excluded floating point types, we know we are now dealing
    3143              :      with signed integer types.  */
    3144          262 :   type = TREE_TYPE (args[0]);
    3145              : 
    3146              :   /* Args[0] is used multiple times below.  */
    3147          262 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    3148              : 
    3149              :   /* Construct (A ^ B) >> 31, which generates a bit mask of all zeros if
    3150              :      the signs of A and B are the same, and of all ones if they differ.  */
    3151          262 :   tmp = fold_build2_loc (input_location, BIT_XOR_EXPR, type, args[0], args[1]);
    3152          262 :   tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, tmp,
    3153          262 :                          build_int_cst (type, TYPE_PRECISION (type) - 1));
    3154          262 :   tmp = gfc_evaluate_now (tmp, &se->pre);
    3155              : 
    3156              :   /* Construct (A + tmp) ^ tmp, which is A if tmp is zero, and -A if tmp]
    3157              :      is all ones (i.e. -1).  */
    3158          262 :   se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR, type,
    3159              :                               fold_build2_loc (input_location, PLUS_EXPR,
    3160              :                                                type, args[0], tmp), tmp);
    3161              : }
    3162              : 
    3163              : 
    3164              : /* Test for the presence of an optional argument.  */
    3165              : 
    3166              : static void
    3167         5070 : gfc_conv_intrinsic_present (gfc_se * se, gfc_expr * expr)
    3168              : {
    3169         5070 :   gfc_expr *arg;
    3170              : 
    3171         5070 :   arg = expr->value.function.actual->expr;
    3172         5070 :   gcc_assert (arg->expr_type == EXPR_VARIABLE);
    3173         5070 :   se->expr = gfc_conv_expr_present (arg->symtree->n.sym);
    3174         5070 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    3175         5070 : }
    3176              : 
    3177              : 
    3178              : /* Calculate the double precision product of two single precision values.  */
    3179              : 
    3180              : static void
    3181           13 : gfc_conv_intrinsic_dprod (gfc_se * se, gfc_expr * expr)
    3182              : {
    3183           13 :   tree type;
    3184           13 :   tree args[2];
    3185              : 
    3186           13 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    3187              : 
    3188              :   /* Convert the args to double precision before multiplying.  */
    3189           13 :   type = gfc_typenode_for_spec (&expr->ts);
    3190           13 :   args[0] = convert (type, args[0]);
    3191           13 :   args[1] = convert (type, args[1]);
    3192           13 :   se->expr = fold_build2_loc (input_location, MULT_EXPR, type, args[0],
    3193              :                               args[1]);
    3194           13 : }
    3195              : 
    3196              : 
    3197              : /* Return a length one character string containing an ascii character.  */
    3198              : 
    3199              : static void
    3200         2020 : gfc_conv_intrinsic_char (gfc_se * se, gfc_expr * expr)
    3201              : {
    3202         2020 :   tree arg[2];
    3203         2020 :   tree var;
    3204         2020 :   tree type;
    3205         2020 :   unsigned int num_args;
    3206              : 
    3207         2020 :   num_args = gfc_intrinsic_argument_list_length (expr);
    3208         2020 :   gfc_conv_intrinsic_function_args (se, expr, arg, num_args);
    3209              : 
    3210         2020 :   type = gfc_get_char_type (expr->ts.kind);
    3211         2020 :   var = gfc_create_var (type, "char");
    3212              : 
    3213         2020 :   arg[0] = fold_build1_loc (input_location, NOP_EXPR, type, arg[0]);
    3214         2020 :   gfc_add_modify (&se->pre, var, arg[0]);
    3215         2020 :   se->expr = gfc_build_addr_expr (build_pointer_type (type), var);
    3216         2020 :   se->string_length = build_int_cst (gfc_charlen_type_node, 1);
    3217         2020 : }
    3218              : 
    3219              : 
    3220              : static void
    3221            0 : gfc_conv_intrinsic_ctime (gfc_se * se, gfc_expr * expr)
    3222              : {
    3223            0 :   tree var;
    3224            0 :   tree len;
    3225            0 :   tree tmp;
    3226            0 :   tree cond;
    3227            0 :   tree fndecl;
    3228            0 :   tree *args;
    3229            0 :   unsigned int num_args;
    3230              : 
    3231            0 :   num_args = gfc_intrinsic_argument_list_length (expr) + 2;
    3232            0 :   args = XALLOCAVEC (tree, num_args);
    3233              : 
    3234            0 :   var = gfc_create_var (pchar_type_node, "pstr");
    3235            0 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    3236              : 
    3237            0 :   gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
    3238            0 :   args[0] = gfc_build_addr_expr (NULL_TREE, var);
    3239            0 :   args[1] = gfc_build_addr_expr (NULL_TREE, len);
    3240              : 
    3241            0 :   fndecl = build_addr (gfor_fndecl_ctime);
    3242            0 :   tmp = build_call_array_loc (input_location,
    3243            0 :                           TREE_TYPE (TREE_TYPE (gfor_fndecl_ctime)),
    3244              :                           fndecl, num_args, args);
    3245            0 :   gfc_add_expr_to_block (&se->pre, tmp);
    3246              : 
    3247              :   /* Free the temporary afterwards, if necessary.  */
    3248            0 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3249            0 :                           len, build_int_cst (TREE_TYPE (len), 0));
    3250            0 :   tmp = gfc_call_free (var);
    3251            0 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    3252            0 :   gfc_add_expr_to_block (&se->post, tmp);
    3253              : 
    3254            0 :   se->expr = var;
    3255            0 :   se->string_length = len;
    3256            0 : }
    3257              : 
    3258              : 
    3259              : static void
    3260            0 : gfc_conv_intrinsic_fdate (gfc_se * se, gfc_expr * expr)
    3261              : {
    3262            0 :   tree var;
    3263            0 :   tree len;
    3264            0 :   tree tmp;
    3265            0 :   tree cond;
    3266            0 :   tree fndecl;
    3267            0 :   tree *args;
    3268            0 :   unsigned int num_args;
    3269              : 
    3270            0 :   num_args = gfc_intrinsic_argument_list_length (expr) + 2;
    3271            0 :   args = XALLOCAVEC (tree, num_args);
    3272              : 
    3273            0 :   var = gfc_create_var (pchar_type_node, "pstr");
    3274            0 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    3275              : 
    3276            0 :   gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
    3277            0 :   args[0] = gfc_build_addr_expr (NULL_TREE, var);
    3278            0 :   args[1] = gfc_build_addr_expr (NULL_TREE, len);
    3279              : 
    3280            0 :   fndecl = build_addr (gfor_fndecl_fdate);
    3281            0 :   tmp = build_call_array_loc (input_location,
    3282            0 :                           TREE_TYPE (TREE_TYPE (gfor_fndecl_fdate)),
    3283              :                           fndecl, num_args, args);
    3284            0 :   gfc_add_expr_to_block (&se->pre, tmp);
    3285              : 
    3286              :   /* Free the temporary afterwards, if necessary.  */
    3287            0 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3288            0 :                           len, build_int_cst (TREE_TYPE (len), 0));
    3289            0 :   tmp = gfc_call_free (var);
    3290            0 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    3291            0 :   gfc_add_expr_to_block (&se->post, tmp);
    3292              : 
    3293            0 :   se->expr = var;
    3294            0 :   se->string_length = len;
    3295            0 : }
    3296              : 
    3297              : 
    3298              : /* Generate a direct call to free() for the FREE subroutine.  */
    3299              : 
    3300              : static tree
    3301           10 : conv_intrinsic_free (gfc_code *code)
    3302              : {
    3303           10 :   stmtblock_t block;
    3304           10 :   gfc_se argse;
    3305           10 :   tree arg, call;
    3306              : 
    3307           10 :   gfc_init_se (&argse, NULL);
    3308           10 :   gfc_conv_expr (&argse, code->ext.actual->expr);
    3309           10 :   arg = fold_convert (ptr_type_node, argse.expr);
    3310              : 
    3311           10 :   gfc_init_block (&block);
    3312           10 :   call = build_call_expr_loc (input_location,
    3313              :                               builtin_decl_explicit (BUILT_IN_FREE), 1, arg);
    3314           10 :   gfc_add_expr_to_block (&block, call);
    3315           10 :   return gfc_finish_block (&block);
    3316              : }
    3317              : 
    3318              : 
    3319              : /* Call the RANDOM_INIT library subroutine with a hidden argument for
    3320              :    handling seeding on coarray images.  */
    3321              : 
    3322              : static tree
    3323           90 : conv_intrinsic_random_init (gfc_code *code)
    3324              : {
    3325           90 :   stmtblock_t block;
    3326           90 :   gfc_se se;
    3327           90 :   tree arg1, arg2, tmp;
    3328              :   /* On none coarray == lib compiles use LOGICAL(4) else regular LOGICAL.  */
    3329           90 :   tree used_bool_type_node = flag_coarray == GFC_FCOARRAY_LIB
    3330           90 :                              ? logical_type_node
    3331           90 :                              : gfc_get_logical_type (4);
    3332              : 
    3333              :   /* Make the function call.  */
    3334           90 :   gfc_init_block (&block);
    3335           90 :   gfc_init_se (&se, NULL);
    3336              : 
    3337              :   /* Convert REPEATABLE to the desired LOGICAL entity.  */
    3338           90 :   gfc_conv_expr (&se, code->ext.actual->expr);
    3339           90 :   gfc_add_block_to_block (&block, &se.pre);
    3340           90 :   arg1 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
    3341           90 :   gfc_add_block_to_block (&block, &se.post);
    3342              : 
    3343              :   /* Convert IMAGE_DISTINCT to the desired LOGICAL entity.  */
    3344           90 :   gfc_conv_expr (&se, code->ext.actual->next->expr);
    3345           90 :   gfc_add_block_to_block (&block, &se.pre);
    3346           90 :   arg2 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
    3347           90 :   gfc_add_block_to_block (&block, &se.post);
    3348              : 
    3349           90 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    3350              :     {
    3351            0 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
    3352              :                                  2, arg1, arg2);
    3353              :     }
    3354              :   else
    3355              :     {
    3356              :       /* The ABI for libgfortran needs to be maintained, so a hidden
    3357              :          argument must be include if code is compiled with -fcoarray=single
    3358              :          or without the option.  Set to 0.  */
    3359           90 :       tree arg3 = build_int_cst (gfc_get_int_type (4), 0);
    3360           90 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_random_init,
    3361              :                                  3, arg1, arg2, arg3);
    3362              :     }
    3363              : 
    3364           90 :   gfc_add_expr_to_block (&block, tmp);
    3365              : 
    3366           90 :   return gfc_finish_block (&block);
    3367              : }
    3368              : 
    3369              : 
    3370              : /* Call the SYSTEM_CLOCK library functions, handling the type and kind
    3371              :    conversions.  */
    3372              : 
    3373              : static tree
    3374          194 : conv_intrinsic_system_clock (gfc_code *code)
    3375              : {
    3376          194 :   stmtblock_t block;
    3377          194 :   gfc_se count_se, count_rate_se, count_max_se;
    3378          194 :   tree arg1 = NULL_TREE, arg2 = NULL_TREE, arg3 = NULL_TREE;
    3379          194 :   tree tmp;
    3380          194 :   int least;
    3381              : 
    3382          194 :   gfc_expr *count = code->ext.actual->expr;
    3383          194 :   gfc_expr *count_rate = code->ext.actual->next->expr;
    3384          194 :   gfc_expr *count_max = code->ext.actual->next->next->expr;
    3385              : 
    3386              :   /* Evaluate our arguments.  */
    3387          194 :   if (count)
    3388              :     {
    3389          194 :       gfc_init_se (&count_se, NULL);
    3390          194 :       gfc_conv_expr (&count_se, count);
    3391              :     }
    3392              : 
    3393          194 :   if (count_rate)
    3394              :     {
    3395          181 :       gfc_init_se (&count_rate_se, NULL);
    3396          181 :       gfc_conv_expr (&count_rate_se, count_rate);
    3397              :     }
    3398              : 
    3399          194 :   if (count_max)
    3400              :     {
    3401          180 :       gfc_init_se (&count_max_se, NULL);
    3402          180 :       gfc_conv_expr (&count_max_se, count_max);
    3403              :     }
    3404              : 
    3405              :   /* Find the smallest kind found of the arguments.  */
    3406          194 :   least = 16;
    3407          194 :   least = (count && count->ts.kind < least) ? count->ts.kind : least;
    3408          194 :   least = (count_rate && count_rate->ts.kind < least) ? count_rate->ts.kind
    3409              :                                                       : least;
    3410          194 :   least = (count_max && count_max->ts.kind < least) ? count_max->ts.kind
    3411              :                                                     : least;
    3412              : 
    3413              :   /* Prepare temporary variables.  */
    3414              : 
    3415          194 :   if (count)
    3416              :     {
    3417          194 :       if (least >= 8)
    3418           18 :         arg1 = gfc_create_var (gfc_get_int_type (8), "count");
    3419          176 :       else if (least == 4)
    3420          152 :         arg1 = gfc_create_var (gfc_get_int_type (4), "count");
    3421           24 :       else if (count->ts.kind == 1)
    3422           12 :         arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[0].pedantic_min_int,
    3423              :                                      count->ts.kind);
    3424              :       else
    3425           12 :         arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[1].pedantic_min_int,
    3426              :                                      count->ts.kind);
    3427              :     }
    3428              : 
    3429          194 :   if (count_rate)
    3430              :     {
    3431          181 :       if (least >= 8)
    3432           18 :         arg2 = gfc_create_var (gfc_get_int_type (8), "count_rate");
    3433          163 :       else if (least == 4)
    3434          139 :         arg2 = gfc_create_var (gfc_get_int_type (4), "count_rate");
    3435              :       else
    3436           24 :         arg2 = integer_zero_node;
    3437              :     }
    3438              : 
    3439          194 :   if (count_max)
    3440              :     {
    3441          180 :       if (least >= 8)
    3442           18 :         arg3 = gfc_create_var (gfc_get_int_type (8), "count_max");
    3443          162 :       else if (least == 4)
    3444          138 :         arg3 = gfc_create_var (gfc_get_int_type (4), "count_max");
    3445              :       else
    3446           24 :         arg3 = integer_zero_node;
    3447              :     }
    3448              : 
    3449              :   /* Make the function call.  */
    3450          194 :   gfc_init_block (&block);
    3451              : 
    3452          194 : if (least <= 2)
    3453              :   {
    3454           24 :     if (least == 1)
    3455              :       {
    3456           12 :         arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
    3457              :                : null_pointer_node;
    3458           12 :         arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
    3459              :                : null_pointer_node;
    3460           12 :         arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
    3461              :                : null_pointer_node;
    3462              :       }
    3463              : 
    3464           24 :     if (least == 2)
    3465              :       {
    3466           12 :         arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
    3467              :                : null_pointer_node;
    3468           12 :         arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
    3469              :                : null_pointer_node;
    3470           12 :         arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
    3471              :                : null_pointer_node;
    3472              :       }
    3473              :   }
    3474              : else
    3475              :   {
    3476          170 :     if (least == 4)
    3477              :       {
    3478          581 :         tmp = build_call_expr_loc (input_location,
    3479              :                 gfor_fndecl_system_clock4, 3,
    3480          152 :                 arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
    3481              :                        : null_pointer_node,
    3482          139 :                 arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
    3483              :                        : null_pointer_node,
    3484          138 :                 arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
    3485              :                        : null_pointer_node);
    3486          152 :         gfc_add_expr_to_block (&block, tmp);
    3487              :       }
    3488              :     /* Handle kind>=8, 10, or 16 arguments */
    3489          170 :     if (least >= 8)
    3490              :       {
    3491           72 :         tmp = build_call_expr_loc (input_location,
    3492              :                 gfor_fndecl_system_clock8, 3,
    3493           18 :                 arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
    3494              :                        : null_pointer_node,
    3495           18 :                 arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
    3496              :                        : null_pointer_node,
    3497           18 :                 arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
    3498              :                        : null_pointer_node);
    3499           18 :         gfc_add_expr_to_block (&block, tmp);
    3500              :       }
    3501              :   }
    3502              : 
    3503              :   /* And store values back if needed.  */
    3504          194 :   if (arg1 && arg1 != count_se.expr)
    3505          194 :     gfc_add_modify (&block, count_se.expr,
    3506          194 :                     fold_convert (TREE_TYPE (count_se.expr), arg1));
    3507          194 :   if (arg2 && arg2 != count_rate_se.expr)
    3508          181 :     gfc_add_modify (&block, count_rate_se.expr,
    3509          181 :                     fold_convert (TREE_TYPE (count_rate_se.expr), arg2));
    3510          194 :   if (arg3 && arg3 != count_max_se.expr)
    3511          180 :     gfc_add_modify (&block, count_max_se.expr,
    3512          180 :                     fold_convert (TREE_TYPE (count_max_se.expr), arg3));
    3513              : 
    3514          194 :   return gfc_finish_block (&block);
    3515              : }
    3516              : 
    3517              : static tree
    3518          102 : conv_intrinsic_split (gfc_code *code)
    3519              : {
    3520          102 :   stmtblock_t block, post_block;
    3521          102 :   gfc_se se;
    3522          102 :   gfc_expr *string_expr, *set_expr, *pos_expr, *back_expr;
    3523          102 :   tree string, string_len;
    3524          102 :   tree set, set_len;
    3525          102 :   tree pos, pos_for_call;
    3526          102 :   tree back;
    3527          102 :   tree fndecl, call;
    3528              : 
    3529          102 :   string_expr = code->ext.actual->expr;
    3530          102 :   set_expr = code->ext.actual->next->expr;
    3531          102 :   pos_expr = code->ext.actual->next->next->expr;
    3532          102 :   back_expr = code->ext.actual->next->next->next->expr;
    3533              : 
    3534          102 :   gfc_start_block (&block);
    3535          102 :   gfc_init_block (&post_block);
    3536              : 
    3537          102 :   gfc_init_se (&se, NULL);
    3538          102 :   gfc_conv_expr (&se, string_expr);
    3539          102 :   gfc_conv_string_parameter (&se);
    3540          102 :   gfc_add_block_to_block (&block, &se.pre);
    3541          102 :   gfc_add_block_to_block (&post_block, &se.post);
    3542          102 :   string = se.expr;
    3543          102 :   string_len = se.string_length;
    3544              : 
    3545          102 :   gfc_init_se (&se, NULL);
    3546          102 :   gfc_conv_expr (&se, set_expr);
    3547          102 :   gfc_conv_string_parameter (&se);
    3548          102 :   gfc_add_block_to_block (&block, &se.pre);
    3549          102 :   gfc_add_block_to_block (&post_block, &se.post);
    3550          102 :   set = se.expr;
    3551          102 :   set_len = se.string_length;
    3552              : 
    3553          102 :   gfc_init_se (&se, NULL);
    3554          102 :   gfc_conv_expr (&se, pos_expr);
    3555          102 :   gfc_add_block_to_block (&block, &se.pre);
    3556          102 :   gfc_add_block_to_block (&post_block, &se.post);
    3557          102 :   pos = se.expr;
    3558          102 :   pos_for_call = fold_convert (gfc_charlen_type_node, pos);
    3559              : 
    3560          102 :   if (back_expr)
    3561              :     {
    3562           48 :       gfc_init_se (&se, NULL);
    3563           48 :       gfc_conv_expr (&se, back_expr);
    3564           48 :       gfc_add_block_to_block (&block, &se.pre);
    3565           48 :       gfc_add_block_to_block (&post_block, &se.post);
    3566           48 :       back = se.expr;
    3567              :     }
    3568              :   else
    3569           54 :     back = logical_false_node;
    3570              : 
    3571          102 :   if (string_expr->ts.kind == 1)
    3572           66 :     fndecl = gfor_fndecl_string_split;
    3573           36 :   else if (string_expr->ts.kind == 4)
    3574           36 :     fndecl = gfor_fndecl_string_split_char4;
    3575              :   else
    3576            0 :     gcc_unreachable ();
    3577              : 
    3578          102 :   call = build_call_expr_loc (input_location, fndecl, 6, string_len, string,
    3579              :                               set_len, set, pos_for_call, back);
    3580          102 :   gfc_add_modify (&block, pos, fold_convert (TREE_TYPE (pos), call));
    3581              : 
    3582          102 :   gfc_add_block_to_block (&block, &post_block);
    3583          102 :   return gfc_finish_block (&block);
    3584              : }
    3585              : 
    3586              : /* Return a character string containing the tty name.  */
    3587              : 
    3588              : static void
    3589            0 : gfc_conv_intrinsic_ttynam (gfc_se * se, gfc_expr * expr)
    3590              : {
    3591            0 :   tree var;
    3592            0 :   tree len;
    3593            0 :   tree tmp;
    3594            0 :   tree cond;
    3595            0 :   tree fndecl;
    3596            0 :   tree *args;
    3597            0 :   unsigned int num_args;
    3598              : 
    3599            0 :   num_args = gfc_intrinsic_argument_list_length (expr) + 2;
    3600            0 :   args = XALLOCAVEC (tree, num_args);
    3601              : 
    3602            0 :   var = gfc_create_var (pchar_type_node, "pstr");
    3603            0 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    3604              : 
    3605            0 :   gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
    3606            0 :   args[0] = gfc_build_addr_expr (NULL_TREE, var);
    3607            0 :   args[1] = gfc_build_addr_expr (NULL_TREE, len);
    3608              : 
    3609            0 :   fndecl = build_addr (gfor_fndecl_ttynam);
    3610            0 :   tmp = build_call_array_loc (input_location,
    3611            0 :                           TREE_TYPE (TREE_TYPE (gfor_fndecl_ttynam)),
    3612              :                           fndecl, num_args, args);
    3613            0 :   gfc_add_expr_to_block (&se->pre, tmp);
    3614              : 
    3615              :   /* Free the temporary afterwards, if necessary.  */
    3616            0 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3617            0 :                           len, build_int_cst (TREE_TYPE (len), 0));
    3618            0 :   tmp = gfc_call_free (var);
    3619            0 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    3620            0 :   gfc_add_expr_to_block (&se->post, tmp);
    3621              : 
    3622            0 :   se->expr = var;
    3623            0 :   se->string_length = len;
    3624            0 : }
    3625              : 
    3626              : 
    3627              : /* Get the minimum/maximum value of all the parameters.
    3628              :     minmax (a1, a2, a3, ...)
    3629              :     {
    3630              :       mvar = a1;
    3631              :       mvar = COMP (mvar, a2)
    3632              :       mvar = COMP (mvar, a3)
    3633              :       ...
    3634              :       return mvar;
    3635              :     }
    3636              :     Where COMP is MIN/MAX_EXPR for integral types or when we don't
    3637              :     care about NaNs, or IFN_FMIN/MAX when the target has support for
    3638              :     fast NaN-honouring min/max.  When neither holds expand a sequence
    3639              :     of explicit comparisons.  */
    3640              : 
    3641              : /* TODO: Mismatching types can occur when specific names are used.
    3642              :    These should be handled during resolution.  */
    3643              : static void
    3644         1365 : gfc_conv_intrinsic_minmax (gfc_se * se, gfc_expr * expr, enum tree_code op)
    3645              : {
    3646         1365 :   tree tmp;
    3647         1365 :   tree mvar;
    3648         1365 :   tree val;
    3649         1365 :   tree *args;
    3650         1365 :   tree type;
    3651         1365 :   tree argtype;
    3652         1365 :   gfc_actual_arglist *argexpr;
    3653         1365 :   unsigned int i, nargs;
    3654              : 
    3655         1365 :   nargs = gfc_intrinsic_argument_list_length (expr);
    3656         1365 :   args = XALLOCAVEC (tree, nargs);
    3657              : 
    3658         1365 :   gfc_conv_intrinsic_function_args (se, expr, args, nargs);
    3659         1365 :   type = gfc_typenode_for_spec (&expr->ts);
    3660              : 
    3661              :   /* Only evaluate the argument once.  */
    3662         1365 :   if (!VAR_P (args[0]) && !TREE_CONSTANT (args[0]))
    3663          368 :     args[0] = gfc_evaluate_now (args[0], &se->pre);
    3664              : 
    3665              :   /* Determine suitable type of temporary, as a GNU extension allows
    3666              :      different argument kinds.  */
    3667         1365 :   argtype = TREE_TYPE (args[0]);
    3668         1365 :   argexpr = expr->value.function.actual;
    3669         2949 :   for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
    3670              :     {
    3671         1584 :       tree tmptype = TREE_TYPE (args[i]);
    3672         1584 :       if (TYPE_PRECISION (tmptype) > TYPE_PRECISION (argtype))
    3673            1 :         argtype = tmptype;
    3674              :     }
    3675         1365 :   mvar = gfc_create_var (argtype, "M");
    3676         1365 :   gfc_add_modify (&se->pre, mvar, convert (argtype, args[0]));
    3677              : 
    3678         1365 :   argexpr = expr->value.function.actual;
    3679         2949 :   for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
    3680              :     {
    3681         1584 :       tree cond = NULL_TREE;
    3682         1584 :       val = args[i];
    3683              : 
    3684              :       /* Handle absent optional arguments by ignoring the comparison.  */
    3685         1584 :       if (argexpr->expr->expr_type == EXPR_VARIABLE
    3686          920 :           && argexpr->expr->symtree->n.sym->attr.optional
    3687           45 :           && INDIRECT_REF_P (val))
    3688              :         {
    3689           84 :           cond = fold_build2_loc (input_location,
    3690              :                                 NE_EXPR, logical_type_node,
    3691           42 :                                 TREE_OPERAND (val, 0),
    3692           42 :                         build_int_cst (TREE_TYPE (TREE_OPERAND (val, 0)), 0));
    3693              :         }
    3694         1542 :       else if (!VAR_P (val) && !TREE_CONSTANT (val))
    3695              :         /* Only evaluate the argument once.  */
    3696          599 :         val = gfc_evaluate_now (val, &se->pre);
    3697              : 
    3698         1584 :       tree calc;
    3699              :       /* For floating point types, the question is what MAX(a, NaN) or
    3700              :          MIN(a, NaN) should return (where "a" is a normal number).
    3701              :          There are valid use case for returning either one, but the
    3702              :          Fortran standard doesn't specify which one should be chosen.
    3703              :          Also, there is no consensus among other tested compilers.  In
    3704              :          short, it's a mess.  So lets just do whatever is fastest.  */
    3705         1584 :       tree_code code = op == GT_EXPR ? MAX_EXPR : MIN_EXPR;
    3706         1584 :       calc = fold_build2_loc (input_location, code, argtype,
    3707              :                               convert (argtype, val), mvar);
    3708         1584 :       tmp = build2_v (MODIFY_EXPR, mvar, calc);
    3709              : 
    3710         1584 :       if (cond != NULL_TREE)
    3711           42 :         tmp = build3_v (COND_EXPR, cond, tmp,
    3712              :                         build_empty_stmt (input_location));
    3713         1584 :       gfc_add_expr_to_block (&se->pre, tmp);
    3714              :     }
    3715         1365 :   se->expr = convert (type, mvar);
    3716         1365 : }
    3717              : 
    3718              : 
    3719              : /* Generate library calls for MIN and MAX intrinsics for character
    3720              :    variables.  */
    3721              : static void
    3722          282 : gfc_conv_intrinsic_minmax_char (gfc_se * se, gfc_expr * expr, int op)
    3723              : {
    3724          282 :   tree *args;
    3725          282 :   tree var, len, fndecl, tmp, cond, function;
    3726          282 :   unsigned int nargs;
    3727              : 
    3728          282 :   nargs = gfc_intrinsic_argument_list_length (expr);
    3729          282 :   args = XALLOCAVEC (tree, nargs + 4);
    3730          282 :   gfc_conv_intrinsic_function_args (se, expr, &args[4], nargs);
    3731              : 
    3732              :   /* Create the result variables.  */
    3733          282 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    3734          282 :   args[0] = gfc_build_addr_expr (NULL_TREE, len);
    3735          282 :   var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
    3736          282 :   args[1] = gfc_build_addr_expr (ppvoid_type_node, var);
    3737          282 :   args[2] = build_int_cst (integer_type_node, op);
    3738          282 :   args[3] = build_int_cst (integer_type_node, nargs / 2);
    3739              : 
    3740          282 :   if (expr->ts.kind == 1)
    3741          210 :     function = gfor_fndecl_string_minmax;
    3742           72 :   else if (expr->ts.kind == 4)
    3743           72 :     function = gfor_fndecl_string_minmax_char4;
    3744              :   else
    3745            0 :     gcc_unreachable ();
    3746              : 
    3747              :   /* Make the function call.  */
    3748          282 :   fndecl = build_addr (function);
    3749          282 :   tmp = build_call_array_loc (input_location,
    3750          282 :                           TREE_TYPE (TREE_TYPE (function)), fndecl,
    3751              :                           nargs + 4, args);
    3752          282 :   gfc_add_expr_to_block (&se->pre, tmp);
    3753              : 
    3754              :   /* Free the temporary afterwards, if necessary.  */
    3755          282 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3756          282 :                           len, build_int_cst (TREE_TYPE (len), 0));
    3757          282 :   tmp = gfc_call_free (var);
    3758          282 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    3759          282 :   gfc_add_expr_to_block (&se->post, tmp);
    3760              : 
    3761          282 :   se->expr = var;
    3762          282 :   se->string_length = len;
    3763          282 : }
    3764              : 
    3765              : 
    3766              : /* Create a symbol node for this intrinsic.  The symbol from the frontend
    3767              :    has the generic name.  */
    3768              : 
    3769              : static gfc_symbol *
    3770        11270 : gfc_get_symbol_for_expr (gfc_expr * expr, bool ignore_optional)
    3771              : {
    3772        11270 :   gfc_symbol *sym;
    3773              : 
    3774              :   /* TODO: Add symbols for intrinsic function to the global namespace.  */
    3775        11270 :   gcc_assert (strlen (expr->value.function.name) <= GFC_MAX_SYMBOL_LEN - 5);
    3776        11270 :   sym = gfc_new_symbol (expr->value.function.name, NULL);
    3777              : 
    3778        11270 :   sym->ts = expr->ts;
    3779        11270 :   if (sym->ts.type == BT_CHARACTER)
    3780         1784 :     sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    3781        11270 :   sym->attr.external = 1;
    3782        11270 :   sym->attr.function = 1;
    3783        11270 :   sym->attr.always_explicit = 1;
    3784        11270 :   sym->attr.proc = PROC_INTRINSIC;
    3785        11270 :   sym->attr.flavor = FL_PROCEDURE;
    3786        11270 :   sym->result = sym;
    3787        11270 :   if (expr->rank > 0)
    3788              :     {
    3789         9878 :       sym->attr.dimension = 1;
    3790         9878 :       sym->as = gfc_get_array_spec ();
    3791         9878 :       sym->as->type = AS_ASSUMED_SHAPE;
    3792         9878 :       sym->as->rank = expr->rank;
    3793              :     }
    3794              : 
    3795        11270 :   gfc_copy_formal_args_intr (sym, expr->value.function.isym,
    3796              :                              ignore_optional ? expr->value.function.actual
    3797              :                                              : NULL);
    3798              : 
    3799        11270 :   return sym;
    3800              : }
    3801              : 
    3802              : /* Remove empty actual arguments.  */
    3803              : 
    3804              : static void
    3805         8277 : remove_empty_actual_arguments (gfc_actual_arglist **ap)
    3806              : {
    3807        44456 :   while (*ap)
    3808              :     {
    3809        36179 :       if ((*ap)->expr == NULL)
    3810              :         {
    3811        11076 :           gfc_actual_arglist *r = *ap;
    3812        11076 :           *ap = r->next;
    3813        11076 :           r->next = NULL;
    3814        11076 :           gfc_free_actual_arglist (r);
    3815              :         }
    3816              :       else
    3817        25103 :         ap = &((*ap)->next);
    3818              :     }
    3819         8277 : }
    3820              : 
    3821              : #define MAX_SPEC_ARG 12
    3822              : 
    3823              : /* Make up an fn spec that's right for intrinsic functions that we
    3824              :    want to call.  */
    3825              : 
    3826              : static char *
    3827         1939 : intrinsic_fnspec (gfc_expr *expr)
    3828              : {
    3829         1939 :   static char fnspec_buf[MAX_SPEC_ARG*2+1];
    3830         1939 :   char *fp;
    3831         1939 :   int i;
    3832         1939 :   int num_char_args;
    3833              : 
    3834              : #define ADD_CHAR(c) do { *fp++ = c; *fp++ = ' '; } while(0)
    3835              : 
    3836              :   /* Set the fndecl.  */
    3837         1939 :   fp = fnspec_buf;
    3838              :   /* Function return value.  FIXME: Check if the second letter could
    3839              :      be something other than a space, for further optimization.  */
    3840         1939 :   ADD_CHAR ('.');
    3841         1939 :   if (expr->rank == 0)
    3842              :     {
    3843          238 :       if (expr->ts.type == BT_CHARACTER)
    3844              :         {
    3845           84 :           ADD_CHAR ('w');  /* Address of character.  */
    3846           84 :           ADD_CHAR ('.');  /* Length of character.  */
    3847              :         }
    3848              :     }
    3849              :   else
    3850         1701 :     ADD_CHAR ('w');  /* Return value is a descriptor.  */
    3851              : 
    3852         1939 :   num_char_args = 0;
    3853        10224 :   for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
    3854              :     {
    3855         8285 :       if (a->expr == NULL)
    3856         2565 :         continue;
    3857              : 
    3858         5720 :       if (a->name && strcmp (a->name,"%VAL") == 0)
    3859         1300 :         ADD_CHAR ('.');
    3860              :       else
    3861              :         {
    3862         4420 :           if (a->expr->rank > 0)
    3863         2575 :             ADD_CHAR ('r');
    3864              :           else
    3865         1845 :             ADD_CHAR ('R');
    3866              :         }
    3867         5720 :       num_char_args += a->expr->ts.type == BT_CHARACTER;
    3868         5720 :       gcc_assert (fp - fnspec_buf + num_char_args <= MAX_SPEC_ARG*2);
    3869              :     }
    3870              : 
    3871         2743 :   for (i = 0; i < num_char_args; i++)
    3872          804 :     ADD_CHAR ('.');
    3873              : 
    3874         1939 :   *fp = '\0';
    3875         1939 :   return fnspec_buf;
    3876              : }
    3877              : 
    3878              : #undef MAX_SPEC_ARG
    3879              : #undef ADD_CHAR
    3880              : 
    3881              : /* Generate the right symbol for the specific intrinsic function and
    3882              :  modify the expr accordingly.  This assumes that absent optional
    3883              :  arguments should be removed.  */
    3884              : 
    3885              : gfc_symbol *
    3886         8277 : specific_intrinsic_symbol (gfc_expr *expr)
    3887              : {
    3888         8277 :   gfc_symbol *sym;
    3889              : 
    3890         8277 :   sym = gfc_find_intrinsic_symbol (expr);
    3891         8277 :   if (sym == NULL)
    3892              :     {
    3893         1939 :       sym = gfc_get_intrinsic_function_symbol (expr);
    3894         1939 :       sym->ts = expr->ts;
    3895         1939 :       if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl)
    3896          240 :         sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
    3897              : 
    3898         1939 :       gfc_copy_formal_args_intr (sym, expr->value.function.isym,
    3899              :                                  expr->value.function.actual, true);
    3900         1939 :       sym->backend_decl
    3901         1939 :         = gfc_get_extern_function_decl (sym, expr->value.function.actual,
    3902         1939 :                                         intrinsic_fnspec (expr));
    3903              :     }
    3904              : 
    3905         8277 :   remove_empty_actual_arguments (&(expr->value.function.actual));
    3906              : 
    3907         8277 :   return sym;
    3908              : }
    3909              : 
    3910              : /* Generate a call to an external intrinsic function.  FIXME: So far,
    3911              :    this only works for functions which are called with well-defined
    3912              :    types; CSHIFT and friends will come later.  */
    3913              : 
    3914              : static void
    3915        13716 : gfc_conv_intrinsic_funcall (gfc_se * se, gfc_expr * expr)
    3916              : {
    3917        13716 :   gfc_symbol *sym;
    3918        13716 :   vec<tree, va_gc> *append_args;
    3919        13716 :   bool specific_symbol;
    3920              : 
    3921        13716 :   gcc_assert (!se->ss || se->ss->info->expr == expr);
    3922              : 
    3923        13716 :   if (se->ss)
    3924        11762 :     gcc_assert (expr->rank > 0);
    3925              :   else
    3926         1954 :     gcc_assert (expr->rank == 0);
    3927              : 
    3928        13716 :   switch (expr->value.function.isym->id)
    3929              :     {
    3930              :     case GFC_ISYM_ANY:
    3931              :     case GFC_ISYM_ALL:
    3932              :     case GFC_ISYM_FINDLOC:
    3933              :     case GFC_ISYM_MAXLOC:
    3934              :     case GFC_ISYM_MINLOC:
    3935              :     case GFC_ISYM_MAXVAL:
    3936              :     case GFC_ISYM_MINVAL:
    3937              :     case GFC_ISYM_NORM2:
    3938              :     case GFC_ISYM_PRODUCT:
    3939              :     case GFC_ISYM_SUM:
    3940              :       specific_symbol = true;
    3941              :       break;
    3942         5439 :     default:
    3943         5439 :       specific_symbol = false;
    3944              :     }
    3945              : 
    3946        13716 :   if (specific_symbol)
    3947              :     {
    3948              :       /* Need to copy here because specific_intrinsic_symbol modifies
    3949              :          expr to omit the absent optional arguments.  */
    3950         8277 :       expr = gfc_copy_expr (expr);
    3951         8277 :       sym = specific_intrinsic_symbol (expr);
    3952              :     }
    3953              :   else
    3954         5439 :     sym = gfc_get_symbol_for_expr (expr, se->ignore_optional);
    3955              : 
    3956              :   /* Calls to libgfortran_matmul need to be appended special arguments,
    3957              :      to be able to call the BLAS ?gemm functions if required and possible.  */
    3958        13716 :   append_args = NULL;
    3959        13716 :   if (expr->value.function.isym->id == GFC_ISYM_MATMUL
    3960          865 :       && !expr->external_blas
    3961          827 :       && sym->ts.type != BT_LOGICAL)
    3962              :     {
    3963          811 :       tree cint = gfc_get_int_type (gfc_c_int_kind);
    3964              : 
    3965          811 :       if (flag_external_blas
    3966            0 :           && (sym->ts.type == BT_REAL || sym->ts.type == BT_COMPLEX)
    3967            0 :           && (sym->ts.kind == 4 || sym->ts.kind == 8))
    3968              :         {
    3969            0 :           tree gemm_fndecl;
    3970              : 
    3971            0 :           if (sym->ts.type == BT_REAL)
    3972              :             {
    3973            0 :               if (sym->ts.kind == 4)
    3974            0 :                 gemm_fndecl = gfor_fndecl_sgemm;
    3975              :               else
    3976            0 :                 gemm_fndecl = gfor_fndecl_dgemm;
    3977              :             }
    3978              :           else
    3979              :             {
    3980            0 :               if (sym->ts.kind == 4)
    3981            0 :                 gemm_fndecl = gfor_fndecl_cgemm;
    3982              :               else
    3983            0 :                 gemm_fndecl = gfor_fndecl_zgemm;
    3984              :             }
    3985              : 
    3986            0 :           vec_alloc (append_args, 3);
    3987            0 :           append_args->quick_push (build_int_cst (cint, 1));
    3988            0 :           append_args->quick_push (build_int_cst (cint,
    3989            0 :                                                   flag_blas_matmul_limit));
    3990            0 :           append_args->quick_push (gfc_build_addr_expr (NULL_TREE,
    3991              :                                                         gemm_fndecl));
    3992            0 :         }
    3993              :       else
    3994              :         {
    3995          811 :           vec_alloc (append_args, 3);
    3996          811 :           append_args->quick_push (build_int_cst (cint, 0));
    3997          811 :           append_args->quick_push (build_int_cst (cint, 0));
    3998          811 :           append_args->quick_push (null_pointer_node);
    3999              :         }
    4000              :     }
    4001              :   /* Non-character scalar reduce returns a pointer to a result of size set by
    4002              :      the element size of 'array'. Setting 'sym' allocatable ensures that the
    4003              :      result is deallocated at the appropriate time.  */
    4004        12905 :   else if (expr->value.function.isym->id == GFC_ISYM_REDUCE
    4005          102 :       && expr->rank == 0 && expr->ts.type != BT_CHARACTER)
    4006           96 :     sym->attr.allocatable = 1;
    4007              : 
    4008              : 
    4009        13716 :   gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
    4010              :                           append_args);
    4011              : 
    4012        13716 :   if (specific_symbol)
    4013         8277 :     gfc_free_expr (expr);
    4014              :   else
    4015         5439 :     gfc_free_symbol (sym);
    4016        13716 : }
    4017              : 
    4018              : /* ANY and ALL intrinsics. ANY->op == NE_EXPR, ALL->op == EQ_EXPR.
    4019              :    Implemented as
    4020              :     any(a)
    4021              :     {
    4022              :       forall (i=...)
    4023              :         if (a[i] != 0)
    4024              :           return 1
    4025              :       end forall
    4026              :       return 0
    4027              :     }
    4028              :     all(a)
    4029              :     {
    4030              :       forall (i=...)
    4031              :         if (a[i] == 0)
    4032              :           return 0
    4033              :       end forall
    4034              :       return 1
    4035              :     }
    4036              :  */
    4037              : static void
    4038        38155 : gfc_conv_intrinsic_anyall (gfc_se * se, gfc_expr * expr, enum tree_code op)
    4039              : {
    4040        38155 :   tree resvar;
    4041        38155 :   stmtblock_t block;
    4042        38155 :   stmtblock_t body;
    4043        38155 :   tree type;
    4044        38155 :   tree tmp;
    4045        38155 :   tree found;
    4046        38155 :   gfc_loopinfo loop;
    4047        38155 :   gfc_actual_arglist *actual;
    4048        38155 :   gfc_ss *arrayss;
    4049        38155 :   gfc_se arrayse;
    4050        38155 :   tree exit_label;
    4051              : 
    4052        38155 :   if (se->ss)
    4053              :     {
    4054            0 :       gfc_conv_intrinsic_funcall (se, expr);
    4055            0 :       return;
    4056              :     }
    4057              : 
    4058        38155 :   actual = expr->value.function.actual;
    4059        38155 :   type = gfc_typenode_for_spec (&expr->ts);
    4060              :   /* Initialize the result.  */
    4061        38155 :   resvar = gfc_create_var (type, "test");
    4062        38155 :   if (op == EQ_EXPR)
    4063          420 :     tmp = convert (type, boolean_true_node);
    4064              :   else
    4065        37735 :     tmp = convert (type, boolean_false_node);
    4066        38155 :   gfc_add_modify (&se->pre, resvar, tmp);
    4067              : 
    4068              :   /* Walk the arguments.  */
    4069        38155 :   arrayss = gfc_walk_expr (actual->expr);
    4070        38155 :   gcc_assert (arrayss != gfc_ss_terminator);
    4071              : 
    4072              :   /* Initialize the scalarizer.  */
    4073        38155 :   gfc_init_loopinfo (&loop);
    4074        38155 :   exit_label = gfc_build_label_decl (NULL_TREE);
    4075        38155 :   TREE_USED (exit_label) = 1;
    4076        38155 :   gfc_add_ss_to_loop (&loop, arrayss);
    4077              : 
    4078              :   /* Initialize the loop.  */
    4079        38155 :   gfc_conv_ss_startstride (&loop);
    4080        38155 :   gfc_conv_loop_setup (&loop, &expr->where);
    4081              : 
    4082        38155 :   gfc_mark_ss_chain_used (arrayss, 1);
    4083              :   /* Generate the loop body.  */
    4084        38155 :   gfc_start_scalarized_body (&loop, &body);
    4085              : 
    4086              :   /* If the condition matches then set the return value.  */
    4087        38155 :   gfc_start_block (&block);
    4088        38155 :   if (op == EQ_EXPR)
    4089          420 :     tmp = convert (type, boolean_false_node);
    4090              :   else
    4091        37735 :     tmp = convert (type, boolean_true_node);
    4092        38155 :   gfc_add_modify (&block, resvar, tmp);
    4093              : 
    4094              :   /* And break out of the loop.  */
    4095        38155 :   tmp = build1_v (GOTO_EXPR, exit_label);
    4096        38155 :   gfc_add_expr_to_block (&block, tmp);
    4097              : 
    4098        38155 :   found = gfc_finish_block (&block);
    4099              : 
    4100              :   /* Check this element.  */
    4101        38155 :   gfc_init_se (&arrayse, NULL);
    4102        38155 :   gfc_copy_loopinfo_to_se (&arrayse, &loop);
    4103        38155 :   arrayse.ss = arrayss;
    4104        38155 :   gfc_conv_expr_val (&arrayse, actual->expr);
    4105              : 
    4106        38155 :   gfc_add_block_to_block (&body, &arrayse.pre);
    4107        38155 :   tmp = fold_build2_loc (input_location, op, logical_type_node, arrayse.expr,
    4108        38155 :                          build_int_cst (TREE_TYPE (arrayse.expr), 0));
    4109        38155 :   tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
    4110        38155 :   gfc_add_expr_to_block (&body, tmp);
    4111        38155 :   gfc_add_block_to_block (&body, &arrayse.post);
    4112              : 
    4113        38155 :   gfc_trans_scalarizing_loops (&loop, &body);
    4114              : 
    4115              :   /* Add the exit label.  */
    4116        38155 :   tmp = build1_v (LABEL_EXPR, exit_label);
    4117        38155 :   gfc_add_expr_to_block (&loop.pre, tmp);
    4118              : 
    4119        38155 :   gfc_add_block_to_block (&se->pre, &loop.pre);
    4120        38155 :   gfc_add_block_to_block (&se->pre, &loop.post);
    4121        38155 :   gfc_cleanup_loop (&loop);
    4122              : 
    4123        38155 :   se->expr = resvar;
    4124              : }
    4125              : 
    4126              : 
    4127              : /* Generate the constant 180 / pi, which is used in the conversion
    4128              :    of acosd(), asind(), atand(), atan2d().  */
    4129              : 
    4130              : static tree
    4131          336 : rad2deg (int kind)
    4132              : {
    4133          336 :   tree retval;
    4134          336 :   mpfr_t pi, t0;
    4135              : 
    4136          336 :   gfc_set_model_kind (kind);
    4137          336 :   mpfr_init (pi);
    4138          336 :   mpfr_init (t0);
    4139          336 :   mpfr_set_si (t0, 180, GFC_RND_MODE);
    4140          336 :   mpfr_const_pi (pi, GFC_RND_MODE);
    4141          336 :   mpfr_div (t0, t0, pi, GFC_RND_MODE);
    4142          336 :   retval = gfc_conv_mpfr_to_tree (t0, kind, 0);
    4143          336 :   mpfr_clear (t0);
    4144          336 :   mpfr_clear (pi);
    4145          336 :   return retval;
    4146              : }
    4147              : 
    4148              : 
    4149              : static gfc_intrinsic_map_t *
    4150          546 : gfc_lookup_intrinsic (gfc_isym_id id)
    4151              : {
    4152          546 :   gfc_intrinsic_map_t *m = gfc_intrinsic_map;
    4153        11154 :   for (; m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
    4154        11154 :     if (id == m->id)
    4155              :       break;
    4156          546 :   gcc_assert (id == m->id);
    4157          546 :   return m;
    4158              : }
    4159              : 
    4160              : 
    4161              : /* ACOSD(x) is translated into ACOS(x) * 180 / pi.
    4162              :    ASIND(x) is translated into ASIN(x) * 180 / pi.
    4163              :    ATAND(x) is translated into ATAN(x) * 180 / pi.  */
    4164              : 
    4165              : static void
    4166          216 : gfc_conv_intrinsic_atrigd (gfc_se * se, gfc_expr * expr, gfc_isym_id id)
    4167              : {
    4168          216 :   tree arg;
    4169          216 :   tree atrigd;
    4170          216 :   tree type;
    4171          216 :   gfc_intrinsic_map_t *m;
    4172              : 
    4173          216 :   type = gfc_typenode_for_spec (&expr->ts);
    4174              : 
    4175          216 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    4176              : 
    4177          216 :   switch (id)
    4178              :     {
    4179           72 :     case GFC_ISYM_ACOSD:
    4180           72 :       m = gfc_lookup_intrinsic (GFC_ISYM_ACOS);
    4181           72 :       break;
    4182           72 :     case GFC_ISYM_ASIND:
    4183           72 :       m = gfc_lookup_intrinsic (GFC_ISYM_ASIN);
    4184           72 :       break;
    4185           72 :     case GFC_ISYM_ATAND:
    4186           72 :       m = gfc_lookup_intrinsic (GFC_ISYM_ATAN);
    4187           72 :       break;
    4188            0 :     default:
    4189            0 :       gcc_unreachable ();
    4190              :     }
    4191          216 :   atrigd = gfc_get_intrinsic_lib_fndecl (m, expr);
    4192          216 :   atrigd = build_call_expr_loc (input_location, atrigd, 1, arg);
    4193              : 
    4194          216 :   se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atrigd,
    4195              :                               fold_convert (type, rad2deg (expr->ts.kind)));
    4196          216 : }
    4197              : 
    4198              : 
    4199              : /* COTAN(X) is translated into -TAN(X+PI/2) for REAL argument and
    4200              :    COS(X) / SIN(X) for COMPLEX argument.  */
    4201              : 
    4202              : static void
    4203          102 : gfc_conv_intrinsic_cotan (gfc_se *se, gfc_expr *expr)
    4204              : {
    4205          102 :   gfc_intrinsic_map_t *m;
    4206          102 :   tree arg;
    4207          102 :   tree type;
    4208              : 
    4209          102 :   type = gfc_typenode_for_spec (&expr->ts);
    4210          102 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    4211              : 
    4212          102 :   if (expr->ts.type == BT_REAL)
    4213              :     {
    4214          102 :       tree tan;
    4215          102 :       tree tmp;
    4216          102 :       mpfr_t pio2;
    4217              : 
    4218              :       /* Create pi/2.  */
    4219          102 :       gfc_set_model_kind (expr->ts.kind);
    4220          102 :       mpfr_init (pio2);
    4221          102 :       mpfr_const_pi (pio2, GFC_RND_MODE);
    4222          102 :       mpfr_div_ui (pio2, pio2, 2, GFC_RND_MODE);
    4223          102 :       tmp = gfc_conv_mpfr_to_tree (pio2, expr->ts.kind, 0);
    4224          102 :       mpfr_clear (pio2);
    4225              : 
    4226              :       /* Find tan builtin function.  */
    4227          102 :       m = gfc_lookup_intrinsic (GFC_ISYM_TAN);
    4228          102 :       tan = gfc_get_intrinsic_lib_fndecl (m, expr);
    4229          102 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, type, arg, tmp);
    4230          102 :       tan = build_call_expr_loc (input_location, tan, 1, tmp);
    4231          102 :       se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tan);
    4232              :     }
    4233              :   else
    4234              :     {
    4235            0 :       tree sin;
    4236            0 :       tree cos;
    4237              : 
    4238              :       /* Find cos builtin function.  */
    4239            0 :       m = gfc_lookup_intrinsic (GFC_ISYM_COS);
    4240            0 :       cos = gfc_get_intrinsic_lib_fndecl (m, expr);
    4241            0 :       cos = build_call_expr_loc (input_location, cos, 1, arg);
    4242              : 
    4243              :       /* Find sin builtin function.  */
    4244            0 :       m = gfc_lookup_intrinsic (GFC_ISYM_SIN);
    4245            0 :       sin = gfc_get_intrinsic_lib_fndecl (m, expr);
    4246            0 :       sin = build_call_expr_loc (input_location, sin, 1, arg);
    4247              : 
    4248              :       /* Divide cos by sin. */
    4249            0 :       se->expr = fold_build2_loc (input_location, RDIV_EXPR, type, cos, sin);
    4250              :    }
    4251          102 : }
    4252              : 
    4253              : 
    4254              : /* COTAND(X) is translated into -TAND(X+90) for REAL argument.  */
    4255              : 
    4256              : static void
    4257          108 : gfc_conv_intrinsic_cotand (gfc_se *se, gfc_expr *expr)
    4258              : {
    4259          108 :   tree arg;
    4260          108 :   tree type;
    4261          108 :   tree ninety_tree;
    4262          108 :   mpfr_t ninety;
    4263              : 
    4264          108 :   type = gfc_typenode_for_spec (&expr->ts);
    4265          108 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    4266              : 
    4267          108 :   gfc_set_model_kind (expr->ts.kind);
    4268              : 
    4269              :   /* Build the tree for x + 90.  */
    4270          108 :   mpfr_init_set_ui (ninety, 90, GFC_RND_MODE);
    4271          108 :   ninety_tree = gfc_conv_mpfr_to_tree (ninety, expr->ts.kind, 0);
    4272          108 :   arg = fold_build2_loc (input_location, PLUS_EXPR, type, arg, ninety_tree);
    4273          108 :   mpfr_clear (ninety);
    4274              : 
    4275              :   /* Find tand.  */
    4276          108 :   gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_TAND);
    4277          108 :   tree tand = gfc_get_intrinsic_lib_fndecl (m, expr);
    4278          108 :   tand = build_call_expr_loc (input_location, tand, 1, arg);
    4279              : 
    4280          108 :   se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tand);
    4281          108 : }
    4282              : 
    4283              : 
    4284              : /* ATAN2D(Y,X) is translated into ATAN2(Y,X) * 180 / PI. */
    4285              : 
    4286              : static void
    4287          120 : gfc_conv_intrinsic_atan2d (gfc_se *se, gfc_expr *expr)
    4288              : {
    4289          120 :   tree args[2];
    4290          120 :   tree atan2d;
    4291          120 :   tree type;
    4292              : 
    4293          120 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    4294          120 :   type = TREE_TYPE (args[0]);
    4295              : 
    4296          120 :   gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_ATAN2);
    4297          120 :   atan2d = gfc_get_intrinsic_lib_fndecl (m, expr);
    4298          120 :   atan2d = build_call_expr_loc (input_location, atan2d, 2, args[0], args[1]);
    4299              : 
    4300          120 :   se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atan2d,
    4301              :                               rad2deg (expr->ts.kind));
    4302          120 : }
    4303              : 
    4304              : 
    4305              : /* COUNT(A) = Number of true elements in A.  */
    4306              : static void
    4307          143 : gfc_conv_intrinsic_count (gfc_se * se, gfc_expr * expr)
    4308              : {
    4309          143 :   tree resvar;
    4310          143 :   tree type;
    4311          143 :   stmtblock_t body;
    4312          143 :   tree tmp;
    4313          143 :   gfc_loopinfo loop;
    4314          143 :   gfc_actual_arglist *actual;
    4315          143 :   gfc_ss *arrayss;
    4316          143 :   gfc_se arrayse;
    4317              : 
    4318          143 :   if (se->ss)
    4319              :     {
    4320            0 :       gfc_conv_intrinsic_funcall (se, expr);
    4321            0 :       return;
    4322              :     }
    4323              : 
    4324          143 :   actual = expr->value.function.actual;
    4325              : 
    4326          143 :   type = gfc_typenode_for_spec (&expr->ts);
    4327              :   /* Initialize the result.  */
    4328          143 :   resvar = gfc_create_var (type, "count");
    4329          143 :   gfc_add_modify (&se->pre, resvar, build_int_cst (type, 0));
    4330              : 
    4331              :   /* Walk the arguments.  */
    4332          143 :   arrayss = gfc_walk_expr (actual->expr);
    4333          143 :   gcc_assert (arrayss != gfc_ss_terminator);
    4334              : 
    4335              :   /* Initialize the scalarizer.  */
    4336          143 :   gfc_init_loopinfo (&loop);
    4337          143 :   gfc_add_ss_to_loop (&loop, arrayss);
    4338              : 
    4339              :   /* Initialize the loop.  */
    4340          143 :   gfc_conv_ss_startstride (&loop);
    4341          143 :   gfc_conv_loop_setup (&loop, &expr->where);
    4342              : 
    4343          143 :   gfc_mark_ss_chain_used (arrayss, 1);
    4344              :   /* Generate the loop body.  */
    4345          143 :   gfc_start_scalarized_body (&loop, &body);
    4346              : 
    4347          143 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (resvar),
    4348          143 :                          resvar, build_int_cst (TREE_TYPE (resvar), 1));
    4349          143 :   tmp = build2_v (MODIFY_EXPR, resvar, tmp);
    4350              : 
    4351          143 :   gfc_init_se (&arrayse, NULL);
    4352          143 :   gfc_copy_loopinfo_to_se (&arrayse, &loop);
    4353          143 :   arrayse.ss = arrayss;
    4354          143 :   gfc_conv_expr_val (&arrayse, actual->expr);
    4355          143 :   tmp = build3_v (COND_EXPR, arrayse.expr, tmp,
    4356              :                   build_empty_stmt (input_location));
    4357              : 
    4358          143 :   gfc_add_block_to_block (&body, &arrayse.pre);
    4359          143 :   gfc_add_expr_to_block (&body, tmp);
    4360          143 :   gfc_add_block_to_block (&body, &arrayse.post);
    4361              : 
    4362          143 :   gfc_trans_scalarizing_loops (&loop, &body);
    4363              : 
    4364          143 :   gfc_add_block_to_block (&se->pre, &loop.pre);
    4365          143 :   gfc_add_block_to_block (&se->pre, &loop.post);
    4366          143 :   gfc_cleanup_loop (&loop);
    4367              : 
    4368          143 :   se->expr = resvar;
    4369              : }
    4370              : 
    4371              : 
    4372              : /* Update given gfc_se to have ss component pointing to the nested gfc_ss
    4373              :    struct and return the corresponding loopinfo.  */
    4374              : 
    4375              : static gfc_loopinfo *
    4376         3374 : enter_nested_loop (gfc_se *se)
    4377              : {
    4378         3374 :   se->ss = se->ss->nested_ss;
    4379         3374 :   gcc_assert (se->ss == se->ss->loop->ss);
    4380              : 
    4381         3374 :   return se->ss->loop;
    4382              : }
    4383              : 
    4384              : /* Build the condition for a mask, which may be optional.  */
    4385              : 
    4386              : static tree
    4387        12763 : conv_mask_condition (gfc_se *maskse, gfc_expr *maskexpr,
    4388              :                          bool optional_mask)
    4389              : {
    4390        12763 :   tree present;
    4391        12763 :   tree type;
    4392              : 
    4393        12763 :   if (optional_mask)
    4394              :     {
    4395          206 :       type = TREE_TYPE (maskse->expr);
    4396          206 :       present = gfc_conv_expr_present (maskexpr->symtree->n.sym);
    4397          206 :       present = convert (type, present);
    4398          206 :       present = fold_build1_loc (input_location, TRUTH_NOT_EXPR, type,
    4399              :                                  present);
    4400          206 :       return fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    4401          206 :                               type, present, maskse->expr);
    4402              :     }
    4403              :   else
    4404        12557 :     return maskse->expr;
    4405              : }
    4406              : 
    4407              : /* Inline implementation of the sum and product intrinsics.  */
    4408              : static void
    4409         2513 : gfc_conv_intrinsic_arith (gfc_se * se, gfc_expr * expr, enum tree_code op,
    4410              :                           bool norm2)
    4411              : {
    4412         2513 :   tree resvar;
    4413         2513 :   tree scale = NULL_TREE;
    4414         2513 :   tree type;
    4415         2513 :   stmtblock_t body;
    4416         2513 :   stmtblock_t block;
    4417         2513 :   tree tmp;
    4418         2513 :   gfc_loopinfo loop, *ploop;
    4419         2513 :   gfc_actual_arglist *arg_array, *arg_mask;
    4420         2513 :   gfc_ss *arrayss = NULL;
    4421         2513 :   gfc_ss *maskss = NULL;
    4422         2513 :   gfc_se arrayse;
    4423         2513 :   gfc_se maskse;
    4424         2513 :   gfc_se *parent_se;
    4425         2513 :   gfc_expr *arrayexpr;
    4426         2513 :   gfc_expr *maskexpr;
    4427         2513 :   bool optional_mask;
    4428              : 
    4429         2513 :   if (expr->rank > 0)
    4430              :     {
    4431          578 :       gcc_assert (gfc_inline_intrinsic_function_p (expr));
    4432              :       parent_se = se;
    4433              :     }
    4434              :   else
    4435              :     parent_se = NULL;
    4436              : 
    4437         2513 :   type = gfc_typenode_for_spec (&expr->ts);
    4438              :   /* Initialize the result.  */
    4439         2513 :   resvar = gfc_create_var (type, "val");
    4440         2513 :   if (norm2)
    4441              :     {
    4442              :       /* result = 0.0;
    4443              :          scale = 1.0.  */
    4444           68 :       scale = gfc_create_var (type, "scale");
    4445           68 :       gfc_add_modify (&se->pre, scale,
    4446              :                       gfc_build_const (type, integer_one_node));
    4447           68 :       tmp = gfc_build_const (type, integer_zero_node);
    4448              :     }
    4449         2445 :   else if (op == PLUS_EXPR || op == BIT_IOR_EXPR || op == BIT_XOR_EXPR)
    4450         2027 :     tmp = gfc_build_const (type, integer_zero_node);
    4451          418 :   else if (op == NE_EXPR)
    4452              :     /* PARITY.  */
    4453           36 :     tmp = convert (type, boolean_false_node);
    4454          382 :   else if (op == BIT_AND_EXPR)
    4455           24 :     tmp = gfc_build_const (type, fold_build1_loc (input_location, NEGATE_EXPR,
    4456              :                                                   type, integer_one_node));
    4457              :   else
    4458          358 :     tmp = gfc_build_const (type, integer_one_node);
    4459              : 
    4460         2513 :   gfc_add_modify (&se->pre, resvar, tmp);
    4461              : 
    4462         2513 :   arg_array = expr->value.function.actual;
    4463              : 
    4464         2513 :   arrayexpr = arg_array->expr;
    4465              : 
    4466         2513 :   if (op == NE_EXPR || norm2)
    4467              :     {
    4468              :       /* PARITY and NORM2.  */
    4469              :       maskexpr = NULL;
    4470              :       optional_mask = false;
    4471              :     }
    4472              :   else
    4473              :     {
    4474         2409 :       arg_mask  = arg_array->next->next;
    4475         2409 :       gcc_assert (arg_mask != NULL);
    4476         2409 :       maskexpr = arg_mask->expr;
    4477          371 :       optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
    4478          266 :         && maskexpr->symtree->n.sym->attr.dummy
    4479         2427 :         && maskexpr->symtree->n.sym->attr.optional;
    4480              :     }
    4481              : 
    4482         2513 :   if (expr->rank == 0)
    4483              :     {
    4484              :       /* Walk the arguments.  */
    4485         1935 :       arrayss = gfc_walk_expr (arrayexpr);
    4486         1935 :       gcc_assert (arrayss != gfc_ss_terminator);
    4487              : 
    4488         1935 :       if (maskexpr && maskexpr->rank > 0)
    4489              :         {
    4490          223 :           maskss = gfc_walk_expr (maskexpr);
    4491          223 :           gcc_assert (maskss != gfc_ss_terminator);
    4492              :         }
    4493              :       else
    4494              :         maskss = NULL;
    4495              : 
    4496              :       /* Initialize the scalarizer.  */
    4497         1935 :       gfc_init_loopinfo (&loop);
    4498              : 
    4499              :       /* We add the mask first because the number of iterations is
    4500              :          taken from the last ss, and this breaks if an absent
    4501              :          optional argument is used for mask.  */
    4502              : 
    4503         1935 :       if (maskexpr && maskexpr->rank > 0)
    4504          223 :         gfc_add_ss_to_loop (&loop, maskss);
    4505         1935 :       gfc_add_ss_to_loop (&loop, arrayss);
    4506              : 
    4507              :       /* Initialize the loop.  */
    4508         1935 :       gfc_conv_ss_startstride (&loop);
    4509         1935 :       gfc_conv_loop_setup (&loop, &expr->where);
    4510              : 
    4511         1935 :       if (maskexpr && maskexpr->rank > 0)
    4512          223 :         gfc_mark_ss_chain_used (maskss, 1);
    4513         1935 :       gfc_mark_ss_chain_used (arrayss, 1);
    4514              : 
    4515         1935 :       ploop = &loop;
    4516              :     }
    4517              :   else
    4518              :     /* All the work has been done in the parent loops.  */
    4519          578 :     ploop = enter_nested_loop (se);
    4520              : 
    4521         2513 :   gcc_assert (ploop);
    4522              : 
    4523              :   /* Generate the loop body.  */
    4524         2513 :   gfc_start_scalarized_body (ploop, &body);
    4525              : 
    4526              :   /* If we have a mask, only add this element if the mask is set.  */
    4527         2513 :   if (maskexpr && maskexpr->rank > 0)
    4528              :     {
    4529          307 :       gfc_init_se (&maskse, parent_se);
    4530          307 :       gfc_copy_loopinfo_to_se (&maskse, ploop);
    4531          307 :       if (expr->rank == 0)
    4532          223 :         maskse.ss = maskss;
    4533          307 :       gfc_conv_expr_val (&maskse, maskexpr);
    4534          307 :       gfc_add_block_to_block (&body, &maskse.pre);
    4535              : 
    4536          307 :       gfc_start_block (&block);
    4537              :     }
    4538              :   else
    4539         2206 :     gfc_init_block (&block);
    4540              : 
    4541              :   /* Do the actual summation/product.  */
    4542         2513 :   gfc_init_se (&arrayse, parent_se);
    4543         2513 :   gfc_copy_loopinfo_to_se (&arrayse, ploop);
    4544         2513 :   if (expr->rank == 0)
    4545         1935 :     arrayse.ss = arrayss;
    4546         2513 :   gfc_conv_expr_val (&arrayse, arrayexpr);
    4547         2513 :   gfc_add_block_to_block (&block, &arrayse.pre);
    4548              : 
    4549         2513 :   if (norm2)
    4550              :     {
    4551              :       /* if (x (i) != 0.0)
    4552              :            {
    4553              :              absX = abs(x(i))
    4554              :              if (absX > scale)
    4555              :                {
    4556              :                  val = scale/absX;
    4557              :                  result = 1.0 + result * val * val;
    4558              :                  scale = absX;
    4559              :                }
    4560              :              else
    4561              :                {
    4562              :                  val = absX/scale;
    4563              :                  result += val * val;
    4564              :                }
    4565              :            }  */
    4566           68 :       tree res1, res2, cond, absX, val;
    4567           68 :       stmtblock_t ifblock1, ifblock2, ifblock3;
    4568              : 
    4569           68 :       gfc_init_block (&ifblock1);
    4570              : 
    4571           68 :       absX = gfc_create_var (type, "absX");
    4572           68 :       gfc_add_modify (&ifblock1, absX,
    4573              :                       fold_build1_loc (input_location, ABS_EXPR, type,
    4574              :                                        arrayse.expr));
    4575           68 :       val = gfc_create_var (type, "val");
    4576           68 :       gfc_add_expr_to_block (&ifblock1, val);
    4577              : 
    4578           68 :       gfc_init_block (&ifblock2);
    4579           68 :       gfc_add_modify (&ifblock2, val,
    4580              :                       fold_build2_loc (input_location, RDIV_EXPR, type, scale,
    4581              :                                        absX));
    4582           68 :       res1 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
    4583           68 :       res1 = fold_build2_loc (input_location, MULT_EXPR, type, resvar, res1);
    4584           68 :       res1 = fold_build2_loc (input_location, PLUS_EXPR, type, res1,
    4585              :                               gfc_build_const (type, integer_one_node));
    4586           68 :       gfc_add_modify (&ifblock2, resvar, res1);
    4587           68 :       gfc_add_modify (&ifblock2, scale, absX);
    4588           68 :       res1 = gfc_finish_block (&ifblock2);
    4589              : 
    4590           68 :       gfc_init_block (&ifblock3);
    4591           68 :       gfc_add_modify (&ifblock3, val,
    4592              :                       fold_build2_loc (input_location, RDIV_EXPR, type, absX,
    4593              :                                        scale));
    4594           68 :       res2 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
    4595           68 :       res2 = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, res2);
    4596           68 :       gfc_add_modify (&ifblock3, resvar, res2);
    4597           68 :       res2 = gfc_finish_block (&ifblock3);
    4598              : 
    4599           68 :       cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    4600              :                               absX, scale);
    4601           68 :       tmp = build3_v (COND_EXPR, cond, res1, res2);
    4602           68 :       gfc_add_expr_to_block (&ifblock1, tmp);
    4603           68 :       tmp = gfc_finish_block (&ifblock1);
    4604              : 
    4605           68 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    4606              :                               arrayse.expr,
    4607              :                               gfc_build_const (type, integer_zero_node));
    4608              : 
    4609           68 :       tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    4610           68 :       gfc_add_expr_to_block (&block, tmp);
    4611              :     }
    4612              :   else
    4613              :     {
    4614         2445 :       tmp = fold_build2_loc (input_location, op, type, resvar, arrayse.expr);
    4615         2445 :       gfc_add_modify (&block, resvar, tmp);
    4616              :     }
    4617              : 
    4618         2513 :   gfc_add_block_to_block (&block, &arrayse.post);
    4619              : 
    4620         2513 :   if (maskexpr && maskexpr->rank > 0)
    4621              :     {
    4622              :       /* We enclose the above in if (mask) {...} .  If the mask is an
    4623              :          optional argument, generate
    4624              :          IF (.NOT. PRESENT(MASK) .OR. MASK(I)).  */
    4625          307 :       tree ifmask;
    4626          307 :       tmp = gfc_finish_block (&block);
    4627          307 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    4628          307 :       tmp = build3_v (COND_EXPR, ifmask, tmp,
    4629              :                       build_empty_stmt (input_location));
    4630          307 :     }
    4631              :   else
    4632         2206 :     tmp = gfc_finish_block (&block);
    4633         2513 :   gfc_add_expr_to_block (&body, tmp);
    4634              : 
    4635         2513 :   gfc_trans_scalarizing_loops (ploop, &body);
    4636              : 
    4637              :   /* For a scalar mask, enclose the loop in an if statement.  */
    4638         2513 :   if (maskexpr && maskexpr->rank == 0)
    4639              :     {
    4640           64 :       gfc_init_block (&block);
    4641           64 :       gfc_add_block_to_block (&block, &ploop->pre);
    4642           64 :       gfc_add_block_to_block (&block, &ploop->post);
    4643           64 :       tmp = gfc_finish_block (&block);
    4644              : 
    4645           64 :       if (expr->rank > 0)
    4646              :         {
    4647           34 :           tmp = build3_v (COND_EXPR, se->ss->info->data.scalar.value, tmp,
    4648              :                           build_empty_stmt (input_location));
    4649           34 :           gfc_advance_se_ss_chain (se);
    4650              :         }
    4651              :       else
    4652              :         {
    4653           30 :           tree ifmask;
    4654              : 
    4655           30 :           gcc_assert (expr->rank == 0);
    4656           30 :           gfc_init_se (&maskse, NULL);
    4657           30 :           gfc_conv_expr_val (&maskse, maskexpr);
    4658           30 :           ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    4659           30 :           tmp = build3_v (COND_EXPR, ifmask, tmp,
    4660              :                           build_empty_stmt (input_location));
    4661              :         }
    4662              : 
    4663           64 :       gfc_add_expr_to_block (&block, tmp);
    4664           64 :       gfc_add_block_to_block (&se->pre, &block);
    4665           64 :       gcc_assert (se->post.head == NULL);
    4666              :     }
    4667              :   else
    4668              :     {
    4669         2449 :       gfc_add_block_to_block (&se->pre, &ploop->pre);
    4670         2449 :       gfc_add_block_to_block (&se->pre, &ploop->post);
    4671              :     }
    4672              : 
    4673         2513 :   if (expr->rank == 0)
    4674         1935 :     gfc_cleanup_loop (ploop);
    4675              : 
    4676         2513 :   if (norm2)
    4677              :     {
    4678              :       /* result = scale * sqrt(result).  */
    4679           68 :       tree sqrt;
    4680           68 :       sqrt = gfc_builtin_decl_for_float_kind (BUILT_IN_SQRT, expr->ts.kind);
    4681           68 :       resvar = build_call_expr_loc (input_location,
    4682              :                                     sqrt, 1, resvar);
    4683           68 :       resvar = fold_build2_loc (input_location, MULT_EXPR, type, scale, resvar);
    4684              :     }
    4685              : 
    4686         2513 :   se->expr = resvar;
    4687         2513 : }
    4688              : 
    4689              : 
    4690              : /* Inline implementation of the dot_product intrinsic. This function
    4691              :    is based on gfc_conv_intrinsic_arith (the previous function).  */
    4692              : static void
    4693          113 : gfc_conv_intrinsic_dot_product (gfc_se * se, gfc_expr * expr)
    4694              : {
    4695          113 :   tree resvar;
    4696          113 :   tree type;
    4697          113 :   stmtblock_t body;
    4698          113 :   stmtblock_t block;
    4699          113 :   tree tmp;
    4700          113 :   gfc_loopinfo loop;
    4701          113 :   gfc_actual_arglist *actual;
    4702          113 :   gfc_ss *arrayss1, *arrayss2;
    4703          113 :   gfc_se arrayse1, arrayse2;
    4704          113 :   gfc_expr *arrayexpr1, *arrayexpr2;
    4705              : 
    4706          113 :   type = gfc_typenode_for_spec (&expr->ts);
    4707              : 
    4708              :   /* Initialize the result.  */
    4709          113 :   resvar = gfc_create_var (type, "val");
    4710          113 :   if (expr->ts.type == BT_LOGICAL)
    4711           30 :     tmp = build_int_cst (type, 0);
    4712              :   else
    4713           83 :     tmp = gfc_build_const (type, integer_zero_node);
    4714              : 
    4715          113 :   gfc_add_modify (&se->pre, resvar, tmp);
    4716              : 
    4717              :   /* Walk argument #1.  */
    4718          113 :   actual = expr->value.function.actual;
    4719          113 :   arrayexpr1 = actual->expr;
    4720          113 :   arrayss1 = gfc_walk_expr (arrayexpr1);
    4721          113 :   gcc_assert (arrayss1 != gfc_ss_terminator);
    4722              : 
    4723              :   /* Walk argument #2.  */
    4724          113 :   actual = actual->next;
    4725          113 :   arrayexpr2 = actual->expr;
    4726          113 :   arrayss2 = gfc_walk_expr (arrayexpr2);
    4727          113 :   gcc_assert (arrayss2 != gfc_ss_terminator);
    4728              : 
    4729              :   /* Initialize the scalarizer.  */
    4730          113 :   gfc_init_loopinfo (&loop);
    4731          113 :   gfc_add_ss_to_loop (&loop, arrayss1);
    4732          113 :   gfc_add_ss_to_loop (&loop, arrayss2);
    4733              : 
    4734              :   /* Initialize the loop.  */
    4735          113 :   gfc_conv_ss_startstride (&loop);
    4736          113 :   gfc_conv_loop_setup (&loop, &expr->where);
    4737              : 
    4738          113 :   gfc_mark_ss_chain_used (arrayss1, 1);
    4739          113 :   gfc_mark_ss_chain_used (arrayss2, 1);
    4740              : 
    4741              :   /* Generate the loop body.  */
    4742          113 :   gfc_start_scalarized_body (&loop, &body);
    4743          113 :   gfc_init_block (&block);
    4744              : 
    4745              :   /* Make the tree expression for [conjg(]array1[)].  */
    4746          113 :   gfc_init_se (&arrayse1, NULL);
    4747          113 :   gfc_copy_loopinfo_to_se (&arrayse1, &loop);
    4748          113 :   arrayse1.ss = arrayss1;
    4749          113 :   gfc_conv_expr_val (&arrayse1, arrayexpr1);
    4750          113 :   if (expr->ts.type == BT_COMPLEX)
    4751            9 :     arrayse1.expr = fold_build1_loc (input_location, CONJ_EXPR, type,
    4752              :                                      arrayse1.expr);
    4753          113 :   gfc_add_block_to_block (&block, &arrayse1.pre);
    4754              : 
    4755              :   /* Make the tree expression for array2.  */
    4756          113 :   gfc_init_se (&arrayse2, NULL);
    4757          113 :   gfc_copy_loopinfo_to_se (&arrayse2, &loop);
    4758          113 :   arrayse2.ss = arrayss2;
    4759          113 :   gfc_conv_expr_val (&arrayse2, arrayexpr2);
    4760          113 :   gfc_add_block_to_block (&block, &arrayse2.pre);
    4761              : 
    4762              :   /* Do the actual product and sum.  */
    4763          113 :   if (expr->ts.type == BT_LOGICAL)
    4764              :     {
    4765           30 :       tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, type,
    4766              :                              arrayse1.expr, arrayse2.expr);
    4767           30 :       tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, type, resvar, tmp);
    4768              :     }
    4769              :   else
    4770              :     {
    4771           83 :       tmp = fold_build2_loc (input_location, MULT_EXPR, type, arrayse1.expr,
    4772              :                              arrayse2.expr);
    4773           83 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, tmp);
    4774              :     }
    4775          113 :   gfc_add_modify (&block, resvar, tmp);
    4776              : 
    4777              :   /* Finish up the loop block and the loop.  */
    4778          113 :   tmp = gfc_finish_block (&block);
    4779          113 :   gfc_add_expr_to_block (&body, tmp);
    4780              : 
    4781          113 :   gfc_trans_scalarizing_loops (&loop, &body);
    4782          113 :   gfc_add_block_to_block (&se->pre, &loop.pre);
    4783          113 :   gfc_add_block_to_block (&se->pre, &loop.post);
    4784          113 :   gfc_cleanup_loop (&loop);
    4785              : 
    4786          113 :   se->expr = resvar;
    4787          113 : }
    4788              : 
    4789              : 
    4790              : /* Tells whether the expression E is a reference to an optional variable whose
    4791              :    presence is not known at compile time.  Those are variable references without
    4792              :    subreference; if there is a subreference, we can assume the variable is
    4793              :    present.  We have to special case full arrays, which we represent with a fake
    4794              :    "full" reference, and class descriptors for which a reference to data is not
    4795              :    really a subreference.  */
    4796              : 
    4797              : bool
    4798        14613 : maybe_absent_optional_variable (gfc_expr *e)
    4799              : {
    4800        14613 :   if (!(e && e->expr_type == EXPR_VARIABLE))
    4801              :     return false;
    4802              : 
    4803         1716 :   gfc_symbol *sym = e->symtree->n.sym;
    4804         1716 :   if (!sym->attr.optional)
    4805              :     return false;
    4806              : 
    4807          224 :   gfc_ref *ref = e->ref;
    4808          224 :   if (ref == nullptr)
    4809              :     return true;
    4810              : 
    4811           20 :   if (ref->type == REF_ARRAY
    4812           20 :       && ref->u.ar.type == AR_FULL
    4813           20 :       && ref->next == nullptr)
    4814              :     return true;
    4815              : 
    4816            0 :   if (!(sym->ts.type == BT_CLASS
    4817            0 :         && ref->type == REF_COMPONENT
    4818            0 :         && ref->u.c.component == CLASS_DATA (sym)))
    4819              :     return false;
    4820              : 
    4821            0 :   gfc_ref *next_ref = ref->next;
    4822            0 :   if (next_ref == nullptr)
    4823              :     return true;
    4824              : 
    4825            0 :   if (next_ref->type == REF_ARRAY
    4826            0 :       && next_ref->u.ar.type == AR_FULL
    4827            0 :       && next_ref->next == nullptr)
    4828            0 :     return true;
    4829              : 
    4830              :   return false;
    4831              : }
    4832              : 
    4833              : 
    4834              : /* Emit code for minloc or maxloc intrinsic.  There are many different cases
    4835              :    we need to handle.  For performance reasons we sometimes create two
    4836              :    loops instead of one, where the second one is much simpler.
    4837              :    Examples for minloc intrinsic:
    4838              :    A: Result is scalar.
    4839              :       1) Array mask is used and NaNs need to be supported:
    4840              :          limit = Infinity;
    4841              :          pos = 0;
    4842              :          S = from;
    4843              :          while (S <= to) {
    4844              :            if (mask[S]) {
    4845              :              if (pos == 0) pos = S + (1 - from);
    4846              :              if (a[S] <= limit) {
    4847              :                limit = a[S];
    4848              :                pos = S + (1 - from);
    4849              :                goto lab1;
    4850              :              }
    4851              :            }
    4852              :            S++;
    4853              :          }
    4854              :          goto lab2;
    4855              :          lab1:;
    4856              :          while (S <= to) {
    4857              :            if (mask[S])
    4858              :              if (a[S] < limit) {
    4859              :                limit = a[S];
    4860              :                pos = S + (1 - from);
    4861              :              }
    4862              :            S++;
    4863              :          }
    4864              :          lab2:;
    4865              :       2) NaNs need to be supported, but it is known at compile time or cheaply
    4866              :          at runtime whether array is nonempty or not:
    4867              :          limit = Infinity;
    4868              :          pos = 0;
    4869              :          S = from;
    4870              :          while (S <= to) {
    4871              :            if (a[S] <= limit) {
    4872              :              limit = a[S];
    4873              :              pos = S + (1 - from);
    4874              :              goto lab1;
    4875              :            }
    4876              :            S++;
    4877              :          }
    4878              :          if (from <= to) pos = 1;
    4879              :          goto lab2;
    4880              :          lab1:;
    4881              :          while (S <= to) {
    4882              :            if (a[S] < limit) {
    4883              :              limit = a[S];
    4884              :              pos = S + (1 - from);
    4885              :            }
    4886              :            S++;
    4887              :          }
    4888              :          lab2:;
    4889              :       3) NaNs aren't supported, array mask is used:
    4890              :          limit = infinities_supported ? Infinity : huge (limit);
    4891              :          pos = 0;
    4892              :          S = from;
    4893              :          while (S <= to) {
    4894              :            if (mask[S]) {
    4895              :              limit = a[S];
    4896              :              pos = S + (1 - from);
    4897              :              goto lab1;
    4898              :            }
    4899              :            S++;
    4900              :          }
    4901              :          goto lab2;
    4902              :          lab1:;
    4903              :          while (S <= to) {
    4904              :            if (mask[S])
    4905              :              if (a[S] < limit) {
    4906              :                limit = a[S];
    4907              :                pos = S + (1 - from);
    4908              :              }
    4909              :            S++;
    4910              :          }
    4911              :          lab2:;
    4912              :       4) Same without array mask:
    4913              :          limit = infinities_supported ? Infinity : huge (limit);
    4914              :          pos = (from <= to) ? 1 : 0;
    4915              :          S = from;
    4916              :          while (S <= to) {
    4917              :            if (a[S] < limit) {
    4918              :              limit = a[S];
    4919              :              pos = S + (1 - from);
    4920              :            }
    4921              :            S++;
    4922              :          }
    4923              :    B: Array result, non-CHARACTER type, DIM absent
    4924              :       Generate similar code as in the scalar case, using a collection of
    4925              :       variables (one per dimension) instead of a single variable as result.
    4926              :       Picking only cases 1) and 4) with ARRAY of rank 2, the generated code
    4927              :       becomes:
    4928              :       1) Array mask is used and NaNs need to be supported:
    4929              :          limit = Infinity;
    4930              :          pos0 = 0;
    4931              :          pos1 = 0;
    4932              :          S1 = from1;
    4933              :          second_loop_entry = false;
    4934              :          while (S1 <= to1) {
    4935              :            S0 = from0;
    4936              :            while (s0 <= to0 {
    4937              :              if (mask[S1][S0]) {
    4938              :                if (pos0 == 0) {
    4939              :                  pos0 = S0 + (1 - from0);
    4940              :                  pos1 = S1 + (1 - from1);
    4941              :                }
    4942              :                if (a[S1][S0] <= limit) {
    4943              :                  limit = a[S1][S0];
    4944              :                  pos0 = S0 + (1 - from0);
    4945              :                  pos1 = S1 + (1 - from1);
    4946              :                  second_loop_entry = true;
    4947              :                  goto lab1;
    4948              :                }
    4949              :              }
    4950              :              S0++;
    4951              :            }
    4952              :            S1++;
    4953              :          }
    4954              :          goto lab2;
    4955              :          lab1:;
    4956              :          S1 = second_loop_entry ? S1 : from1;
    4957              :          while (S1 <= to1) {
    4958              :            S0 = second_loop_entry ? S0 : from0;
    4959              :            while (S0 <= to0) {
    4960              :              if (mask[S1][S0])
    4961              :                if (a[S1][S0] < limit) {
    4962              :                  limit = a[S1][S0];
    4963              :                  pos0 = S + (1 - from0);
    4964              :                  pos1 = S + (1 - from1);
    4965              :                }
    4966              :              second_loop_entry = false;
    4967              :              S0++;
    4968              :            }
    4969              :            S1++;
    4970              :          }
    4971              :          lab2:;
    4972              :          result = { pos0, pos1 };
    4973              :       ...
    4974              :       4) NANs aren't supported, no array mask.
    4975              :          limit = infinities_supported ? Infinity : huge (limit);
    4976              :          pos0 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
    4977              :          pos1 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
    4978              :          S1 = from1;
    4979              :          while (S1 <= to1) {
    4980              :            S0 = from0;
    4981              :            while (S0 <= to0) {
    4982              :              if (a[S1][S0] < limit) {
    4983              :                limit = a[S1][S0];
    4984              :                pos0 = S + (1 - from0);
    4985              :                pos1 = S + (1 - from1);
    4986              :              }
    4987              :              S0++;
    4988              :            }
    4989              :            S1++;
    4990              :          }
    4991              :          result = { pos0, pos1 };
    4992              :    C: Otherwise, a call is generated.
    4993              :    For 2) and 4), if mask is scalar, this all goes into a conditional,
    4994              :    setting pos = 0; in the else branch.
    4995              : 
    4996              :    Since we now also support the BACK argument, instead of using
    4997              :    if (a[S] < limit), we now use
    4998              : 
    4999              :    if (back)
    5000              :      cond = a[S] <= limit;
    5001              :    else
    5002              :      cond = a[S] < limit;
    5003              :    if (cond) {
    5004              :      ....
    5005              : 
    5006              :    The optimizer is smart enough to move the condition out of the loop.
    5007              :    They are now marked as unlikely too for further speedup.  */
    5008              : 
    5009              : static void
    5010        18898 : gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op)
    5011              : {
    5012        18898 :   stmtblock_t body;
    5013        18898 :   stmtblock_t block;
    5014        18898 :   stmtblock_t ifblock;
    5015        18898 :   stmtblock_t elseblock;
    5016        18898 :   tree limit;
    5017        18898 :   tree type;
    5018        18898 :   tree tmp;
    5019        18898 :   tree cond;
    5020        18898 :   tree elsetmp;
    5021        18898 :   tree ifbody;
    5022        18898 :   tree offset[GFC_MAX_DIMENSIONS];
    5023        18898 :   tree nonempty;
    5024        18898 :   tree lab1, lab2;
    5025        18898 :   tree b_if, b_else;
    5026        18898 :   tree back;
    5027        18898 :   gfc_loopinfo loop, *ploop;
    5028        18898 :   gfc_actual_arglist *array_arg, *dim_arg, *mask_arg, *kind_arg;
    5029        18898 :   gfc_actual_arglist *back_arg;
    5030        18898 :   gfc_ss *arrayss = nullptr;
    5031        18898 :   gfc_ss *maskss = nullptr;
    5032        18898 :   gfc_ss *orig_ss = nullptr;
    5033        18898 :   gfc_se arrayse;
    5034        18898 :   gfc_se maskse;
    5035        18898 :   gfc_se nested_se;
    5036        18898 :   gfc_se *base_se;
    5037        18898 :   gfc_expr *arrayexpr;
    5038        18898 :   gfc_expr *maskexpr;
    5039        18898 :   gfc_expr *backexpr;
    5040        18898 :   gfc_se backse;
    5041        18898 :   tree pos[GFC_MAX_DIMENSIONS];
    5042        18898 :   tree idx[GFC_MAX_DIMENSIONS];
    5043        18898 :   tree result_var = NULL_TREE;
    5044        18898 :   int n;
    5045        18898 :   bool optional_mask;
    5046              : 
    5047        18898 :   array_arg = expr->value.function.actual;
    5048        18898 :   dim_arg = array_arg->next;
    5049        18898 :   mask_arg = dim_arg->next;
    5050        18898 :   kind_arg = mask_arg->next;
    5051        18898 :   back_arg = kind_arg->next;
    5052              : 
    5053        18898 :   bool dim_present = dim_arg->expr != nullptr;
    5054        18898 :   bool nested_loop = dim_present && expr->rank > 0;
    5055              : 
    5056              :   /* Remove kind.  */
    5057        18898 :   if (kind_arg->expr)
    5058              :     {
    5059         2240 :       gfc_free_expr (kind_arg->expr);
    5060         2240 :       kind_arg->expr = NULL;
    5061              :     }
    5062              : 
    5063              :   /* Pass BACK argument by value.  */
    5064        18898 :   back_arg->name = "%VAL";
    5065              : 
    5066        18898 :   if (se->ss)
    5067              :     {
    5068        14732 :       if (se->ss->info->useflags)
    5069              :         {
    5070         7671 :           if (!dim_present || !gfc_inline_intrinsic_function_p (expr))
    5071              :             {
    5072              :               /* The code generating and initializing the result array has been
    5073              :                  generated already before the scalarization loop, either with a
    5074              :                  library function call or with inline code; now we can just use
    5075              :                  the result.  */
    5076         4875 :               gfc_conv_tmp_array_ref (se);
    5077        13822 :               return;
    5078              :             }
    5079              :         }
    5080         7061 :       else if (!gfc_inline_intrinsic_function_p (expr))
    5081              :         {
    5082         3780 :           gfc_conv_intrinsic_funcall (se, expr);
    5083         3780 :           return;
    5084              :         }
    5085              :     }
    5086              : 
    5087        10243 :   arrayexpr = array_arg->expr;
    5088              : 
    5089              :   /* Special case for character maxloc.  Remove unneeded "dim" actual
    5090              :      argument, then call a library function.  */
    5091              : 
    5092        10243 :   if (arrayexpr->ts.type == BT_CHARACTER)
    5093              :     {
    5094          292 :       gcc_assert (expr->rank == 0);
    5095              : 
    5096          292 :       if (dim_arg->expr)
    5097              :         {
    5098          292 :           gfc_free_expr (dim_arg->expr);
    5099          292 :           dim_arg->expr = NULL;
    5100              :         }
    5101          292 :       gfc_conv_intrinsic_funcall (se, expr);
    5102          292 :       return;
    5103              :     }
    5104              : 
    5105         9951 :   type = gfc_typenode_for_spec (&expr->ts);
    5106              : 
    5107         9951 :   if (expr->rank > 0 && !dim_present)
    5108              :     {
    5109         3281 :       gfc_array_spec as;
    5110         3281 :       memset (&as, 0, sizeof (as));
    5111              : 
    5112         3281 :       as.rank = 1;
    5113         3281 :       as.lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
    5114              :                                       &arrayexpr->where,
    5115              :                                       HOST_WIDE_INT_1);
    5116         6562 :       as.upper[0] = gfc_get_int_expr (gfc_index_integer_kind,
    5117              :                                       &arrayexpr->where,
    5118         3281 :                                       arrayexpr->rank);
    5119              : 
    5120         3281 :       tree array = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
    5121              : 
    5122         3281 :       result_var = gfc_create_var (array, "loc_result");
    5123              :     }
    5124              : 
    5125         7155 :   const int reduction_dimensions = dim_present ? 1 : arrayexpr->rank;
    5126              : 
    5127              :   /* Initialize the result.  */
    5128        22177 :   for (int i = 0; i < reduction_dimensions; i++)
    5129              :     {
    5130        12226 :       pos[i] = gfc_create_var (gfc_array_index_type,
    5131              :                                gfc_get_string ("pos%d", i));
    5132        12226 :       offset[i] = gfc_create_var (gfc_array_index_type,
    5133              :                                   gfc_get_string ("offset%d", i));
    5134        12226 :       idx[i] = gfc_create_var (gfc_array_index_type,
    5135              :                                gfc_get_string ("idx%d", i));
    5136              :     }
    5137              : 
    5138         9951 :   maskexpr = mask_arg->expr;
    5139         6518 :   optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
    5140         5329 :     && maskexpr->symtree->n.sym->attr.dummy
    5141        10116 :     && maskexpr->symtree->n.sym->attr.optional;
    5142         9951 :   backexpr = back_arg->expr;
    5143              : 
    5144        17106 :   gfc_init_se (&backse, nested_loop ? se : nullptr);
    5145         9951 :   if (backexpr == nullptr)
    5146            0 :     back = logical_false_node;
    5147         9951 :   else if (maybe_absent_optional_variable (backexpr))
    5148              :     {
    5149              :       /* This should have been checked already by
    5150              :          maybe_absent_optional_variable.  */
    5151          184 :       gcc_checking_assert (backexpr->expr_type == EXPR_VARIABLE);
    5152              : 
    5153          184 :       gfc_conv_expr (&backse, backexpr);
    5154          184 :       tree present = gfc_conv_expr_present (backexpr->symtree->n.sym, false);
    5155          184 :       back = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    5156              :                               logical_type_node, present, backse.expr);
    5157              :     }
    5158              :   else
    5159              :     {
    5160         9767 :       gfc_conv_expr (&backse, backexpr);
    5161         9767 :       back = backse.expr;
    5162              :     }
    5163         9951 :   gfc_add_block_to_block (&se->pre, &backse.pre);
    5164         9951 :   back = gfc_evaluate_now_loc (input_location, back, &se->pre);
    5165         9951 :   gfc_add_block_to_block (&se->pre, &backse.post);
    5166              : 
    5167         9951 :   if (nested_loop)
    5168              :     {
    5169         2796 :       gfc_init_se (&nested_se, se);
    5170         2796 :       base_se = &nested_se;
    5171              :     }
    5172              :   else
    5173              :     {
    5174              :       /* Walk the arguments.  */
    5175         7155 :       arrayss = gfc_walk_expr (arrayexpr);
    5176         7155 :       gcc_assert (arrayss != gfc_ss_terminator);
    5177              : 
    5178         7155 :       if (maskexpr && maskexpr->rank != 0)
    5179              :         {
    5180         2700 :           maskss = gfc_walk_expr (maskexpr);
    5181         2700 :           gcc_assert (maskss != gfc_ss_terminator);
    5182              :         }
    5183              : 
    5184              :       base_se = nullptr;
    5185              :     }
    5186              : 
    5187        18091 :   nonempty = nullptr;
    5188         7448 :   if (!(maskexpr && maskexpr->rank > 0))
    5189              :     {
    5190         6077 :       mpz_t asize;
    5191         6077 :       bool reduction_size_known;
    5192              : 
    5193         6077 :       if (dim_present)
    5194              :         {
    5195         4032 :           int reduction_dim;
    5196         4032 :           if (dim_arg->expr->expr_type == EXPR_CONSTANT)
    5197         4030 :             reduction_dim = mpz_get_si (dim_arg->expr->value.integer) - 1;
    5198            2 :           else if (arrayexpr->rank == 1)
    5199              :             reduction_dim = 0;
    5200              :           else
    5201            0 :             gcc_unreachable ();
    5202         4032 :           reduction_size_known = gfc_array_dimen_size (arrayexpr, reduction_dim,
    5203              :                                                        &asize);
    5204              :         }
    5205              :       else
    5206         2045 :         reduction_size_known = gfc_array_size (arrayexpr, &asize);
    5207              : 
    5208         6077 :       if (reduction_size_known)
    5209              :         {
    5210         4482 :           nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
    5211         4482 :           mpz_clear (asize);
    5212         4482 :           nonempty = fold_build2_loc (input_location, GT_EXPR,
    5213              :                                       logical_type_node, nonempty,
    5214              :                                       gfc_index_zero_node);
    5215              :         }
    5216         6077 :       maskss = NULL;
    5217              :     }
    5218              : 
    5219         9951 :   limit = gfc_create_var (gfc_typenode_for_spec (&arrayexpr->ts), "limit");
    5220         9951 :   switch (arrayexpr->ts.type)
    5221              :     {
    5222         3898 :     case BT_REAL:
    5223         3898 :       tmp = gfc_build_inf_or_huge (TREE_TYPE (limit), arrayexpr->ts.kind);
    5224         3898 :       break;
    5225              : 
    5226         6029 :     case BT_INTEGER:
    5227         6029 :       n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
    5228         6029 :       tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
    5229              :                                   arrayexpr->ts.kind);
    5230         6029 :       break;
    5231              : 
    5232           24 :     case BT_UNSIGNED:
    5233              :       /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE().  */
    5234           24 :       if (op == GT_EXPR)
    5235              :         {
    5236           12 :           tmp = gfc_get_unsigned_type (arrayexpr->ts.kind);
    5237           12 :           tmp = build_int_cst (tmp, 0);
    5238              :         }
    5239              :       else
    5240              :         {
    5241           12 :           n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
    5242           12 :           tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
    5243              :                                                expr->ts.kind);
    5244              :         }
    5245              :       break;
    5246              : 
    5247            0 :     default:
    5248            0 :       gcc_unreachable ();
    5249              :     }
    5250              : 
    5251              :   /* We start with the most negative possible value for MAXLOC, and the most
    5252              :      positive possible value for MINLOC. The most negative possible value is
    5253              :      -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
    5254              :      possible value is HUGE in both cases.  BT_UNSIGNED has already been dealt
    5255              :      with above.  */
    5256         9951 :   if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
    5257         4724 :     tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
    5258         4724 :   if (op == GT_EXPR && arrayexpr->ts.type == BT_INTEGER)
    5259         2914 :     tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp), tmp,
    5260         2914 :                            build_int_cst (TREE_TYPE (tmp), 1));
    5261              : 
    5262         9951 :   gfc_add_modify (&se->pre, limit, tmp);
    5263              : 
    5264              :   /* If we are in a case where we generate two sets of loops, the second one
    5265              :      should continue where the first stopped instead of restarting from the
    5266              :      beginning.  So nested loops in the second set should have a partial range
    5267              :      on the first iteration, but they should start from the beginning and span
    5268              :      their full range on the following iterations.  So we use conditionals in
    5269              :      the loops lower bounds, and use the following variable in those
    5270              :      conditionals to decide whether to use the original loop bound or to use
    5271              :      the index at which the loop from the first set stopped.  */
    5272         9951 :   tree second_loop_entry = gfc_create_var (logical_type_node,
    5273              :                                            "second_loop_entry");
    5274         9951 :   gfc_add_modify (&se->pre, second_loop_entry, logical_false_node);
    5275              : 
    5276         9951 :   if (nested_loop)
    5277              :     {
    5278         2796 :       ploop = enter_nested_loop (&nested_se);
    5279         2796 :       orig_ss = nested_se.ss;
    5280         2796 :       ploop->temp_dim = 1;
    5281              :     }
    5282              :   else
    5283              :     {
    5284              :       /* Initialize the scalarizer.  */
    5285         7155 :       gfc_init_loopinfo (&loop);
    5286              : 
    5287              :       /* We add the mask first because the number of iterations is taken
    5288              :          from the last ss, and this breaks if an absent optional argument
    5289              :          is used for mask.  */
    5290              : 
    5291         7155 :       if (maskss)
    5292         2700 :         gfc_add_ss_to_loop (&loop, maskss);
    5293              : 
    5294         7155 :       gfc_add_ss_to_loop (&loop, arrayss);
    5295              : 
    5296              :       /* Initialize the loop.  */
    5297         7155 :       gfc_conv_ss_startstride (&loop);
    5298              : 
    5299              :       /* The code generated can have more than one loop in sequence (see the
    5300              :          comment at the function header).  This doesn't work well with the
    5301              :          scalarizer, which changes arrays' offset when the scalarization loops
    5302              :          are generated (see gfc_trans_preloop_setup).  Fortunately, we can use
    5303              :          the scalarizer temporary code to handle multiple loops.  Thus, we set
    5304              :          temp_dim here, we call gfc_mark_ss_chain_used with flag=3 later, and
    5305              :          we use gfc_trans_scalarized_loop_boundary even later to restore
    5306              :          offset.  */
    5307         7155 :       loop.temp_dim = loop.dimen;
    5308         7155 :       gfc_conv_loop_setup (&loop, &expr->where);
    5309              : 
    5310         7155 :       ploop = &loop;
    5311              :     }
    5312              : 
    5313         9951 :   gcc_assert (reduction_dimensions == ploop->dimen);
    5314              : 
    5315         9951 :   if (nonempty == NULL && !(maskexpr && maskexpr->rank > 0))
    5316              :     {
    5317         1595 :       nonempty = logical_true_node;
    5318              : 
    5319         3697 :       for (int i = 0; i < ploop->dimen; i++)
    5320              :         {
    5321         2102 :           if (!(ploop->from[i] && ploop->to[i]))
    5322              :             {
    5323              :               nonempty = NULL;
    5324              :               break;
    5325              :             }
    5326              : 
    5327         2102 :           tree tmp = fold_build2_loc (input_location, LE_EXPR,
    5328              :                                       logical_type_node, ploop->from[i],
    5329              :                                       ploop->to[i]);
    5330              : 
    5331         2102 :           nonempty = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    5332              :                                       logical_type_node, nonempty, tmp);
    5333              :         }
    5334              :     }
    5335              : 
    5336        11546 :   lab1 = NULL;
    5337        11546 :   lab2 = NULL;
    5338              :   /* Initialize the position to zero, following Fortran 2003.  We are free
    5339              :      to do this because Fortran 95 allows the result of an entirely false
    5340              :      mask to be processor dependent.  If we know at compile time the array
    5341              :      is non-empty and no MASK is used, we can initialize to 1 to simplify
    5342              :      the inner loop.  */
    5343         9951 :   if (nonempty != NULL && !HONOR_NANS (DECL_MODE (limit)))
    5344              :     {
    5345         3748 :       tree init = fold_build3_loc (input_location, COND_EXPR,
    5346              :                                    gfc_array_index_type, nonempty,
    5347              :                                    gfc_index_one_node,
    5348              :                                    gfc_index_zero_node);
    5349         8430 :       for (int i = 0; i < ploop->dimen; i++)
    5350         4682 :         gfc_add_modify (&ploop->pre, pos[i], init);
    5351              :     }
    5352              :   else
    5353              :     {
    5354        13747 :       for (int i = 0; i < ploop->dimen; i++)
    5355         7544 :         gfc_add_modify (&ploop->pre, pos[i], gfc_index_zero_node);
    5356         6203 :       lab1 = gfc_build_label_decl (NULL_TREE);
    5357         6203 :       TREE_USED (lab1) = 1;
    5358         6203 :       lab2 = gfc_build_label_decl (NULL_TREE);
    5359         6203 :       TREE_USED (lab2) = 1;
    5360              :     }
    5361              : 
    5362              :   /* An offset must be added to the loop
    5363              :      counter to obtain the required position.  */
    5364        22177 :   for (int i = 0; i < ploop->dimen; i++)
    5365              :     {
    5366        12226 :       gcc_assert (ploop->from[i]);
    5367              : 
    5368        12226 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    5369              :                              gfc_index_one_node, ploop->from[i]);
    5370        12226 :       gfc_add_modify (&ploop->pre, offset[i], tmp);
    5371              :     }
    5372              : 
    5373         9951 :   if (!nested_loop)
    5374              :     {
    5375         9965 :       gfc_mark_ss_chain_used (arrayss, lab1 ? 3 : 1);
    5376         7155 :       if (maskss)
    5377         2700 :         gfc_mark_ss_chain_used (maskss, lab1 ? 3 : 1);
    5378              :     }
    5379              : 
    5380              :   /* Generate the loop body.  */
    5381         9951 :   gfc_start_scalarized_body (ploop, &body);
    5382              : 
    5383              :   /* If we have a mask, only check this element if the mask is set.  */
    5384         9951 :   if (maskexpr && maskexpr->rank > 0)
    5385              :     {
    5386         3874 :       gfc_init_se (&maskse, base_se);
    5387         3874 :       gfc_copy_loopinfo_to_se (&maskse, ploop);
    5388         3874 :       if (!nested_loop)
    5389         2700 :         maskse.ss = maskss;
    5390         3874 :       gfc_conv_expr_val (&maskse, maskexpr);
    5391         3874 :       gfc_add_block_to_block (&body, &maskse.pre);
    5392              : 
    5393         3874 :       gfc_start_block (&block);
    5394              :     }
    5395              :   else
    5396         6077 :     gfc_init_block (&block);
    5397              : 
    5398              :   /* Compare with the current limit.  */
    5399         9951 :   gfc_init_se (&arrayse, base_se);
    5400         9951 :   gfc_copy_loopinfo_to_se (&arrayse, ploop);
    5401         9951 :   if (!nested_loop)
    5402         7155 :     arrayse.ss = arrayss;
    5403         9951 :   gfc_conv_expr_val (&arrayse, arrayexpr);
    5404         9951 :   gfc_add_block_to_block (&block, &arrayse.pre);
    5405              : 
    5406              :   /* We do the following if this is a more extreme value.  */
    5407         9951 :   gfc_start_block (&ifblock);
    5408              : 
    5409              :   /* Assign the value to the limit...  */
    5410         9951 :   gfc_add_modify (&ifblock, limit, arrayse.expr);
    5411              : 
    5412         9951 :   if (nonempty == NULL && HONOR_NANS (DECL_MODE (limit)))
    5413              :     {
    5414         1569 :       stmtblock_t ifblock2;
    5415         1569 :       tree ifbody2;
    5416              : 
    5417         1569 :       gfc_start_block (&ifblock2);
    5418         3439 :       for (int i = 0; i < ploop->dimen; i++)
    5419              :         {
    5420         1870 :           tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
    5421              :                                  ploop->loopvar[i], offset[i]);
    5422         1870 :           gfc_add_modify (&ifblock2, pos[i], tmp);
    5423              :         }
    5424         1569 :       ifbody2 = gfc_finish_block (&ifblock2);
    5425              : 
    5426         1569 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    5427              :                               pos[0], gfc_index_zero_node);
    5428         1569 :       tmp = build3_v (COND_EXPR, cond, ifbody2,
    5429              :                       build_empty_stmt (input_location));
    5430         1569 :       gfc_add_expr_to_block (&block, tmp);
    5431              :     }
    5432              : 
    5433        22177 :   for (int i = 0; i < ploop->dimen; i++)
    5434              :     {
    5435        12226 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
    5436              :                              ploop->loopvar[i], offset[i]);
    5437        12226 :       gfc_add_modify (&ifblock, pos[i], tmp);
    5438        12226 :       gfc_add_modify (&ifblock, idx[i], ploop->loopvar[i]);
    5439              :     }
    5440              : 
    5441         9951 :   gfc_add_modify (&ifblock, second_loop_entry, logical_true_node);
    5442              : 
    5443         9951 :   if (lab1)
    5444         6203 :     gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab1));
    5445              : 
    5446         9951 :   ifbody = gfc_finish_block (&ifblock);
    5447              : 
    5448         9951 :   if (!lab1 || HONOR_NANS (DECL_MODE (limit)))
    5449              :     {
    5450         7646 :       if (lab1)
    5451         5998 :         cond = fold_build2_loc (input_location,
    5452              :                                 op == GT_EXPR ? GE_EXPR : LE_EXPR,
    5453              :                                 logical_type_node, arrayse.expr, limit);
    5454              :       else
    5455              :         {
    5456         3748 :           tree ifbody2, elsebody2;
    5457              : 
    5458              :           /* We switch to > or >= depending on the value of the BACK argument. */
    5459         3748 :           cond = gfc_create_var (logical_type_node, "cond");
    5460              : 
    5461         3748 :           gfc_start_block (&ifblock);
    5462         5641 :           b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
    5463              :                                   logical_type_node, arrayse.expr, limit);
    5464              : 
    5465         3748 :           gfc_add_modify (&ifblock, cond, b_if);
    5466         3748 :           ifbody2 = gfc_finish_block (&ifblock);
    5467              : 
    5468         3748 :           gfc_start_block (&elseblock);
    5469         3748 :           b_else = fold_build2_loc (input_location, op, logical_type_node,
    5470              :                                     arrayse.expr, limit);
    5471              : 
    5472         3748 :           gfc_add_modify (&elseblock, cond, b_else);
    5473         3748 :           elsebody2 = gfc_finish_block (&elseblock);
    5474              : 
    5475         3748 :           tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
    5476              :                                  back, ifbody2, elsebody2);
    5477              : 
    5478         3748 :           gfc_add_expr_to_block (&block, tmp);
    5479              :         }
    5480              : 
    5481         7646 :       cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
    5482         7646 :       ifbody = build3_v (COND_EXPR, cond, ifbody,
    5483              :                          build_empty_stmt (input_location));
    5484              :     }
    5485         9951 :   gfc_add_expr_to_block (&block, ifbody);
    5486              : 
    5487         9951 :   if (maskexpr && maskexpr->rank > 0)
    5488              :     {
    5489              :       /* We enclose the above in if (mask) {...}.  If the mask is an
    5490              :          optional argument, generate IF (.NOT. PRESENT(MASK)
    5491              :          .OR. MASK(I)). */
    5492              : 
    5493         3874 :       tree ifmask;
    5494         3874 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5495         3874 :       tmp = gfc_finish_block (&block);
    5496         3874 :       tmp = build3_v (COND_EXPR, ifmask, tmp,
    5497              :                       build_empty_stmt (input_location));
    5498         3874 :     }
    5499              :   else
    5500         6077 :     tmp = gfc_finish_block (&block);
    5501         9951 :   gfc_add_expr_to_block (&body, tmp);
    5502              : 
    5503         9951 :   if (lab1)
    5504              :     {
    5505        13747 :       for (int i = 0; i < ploop->dimen; i++)
    5506         7544 :         ploop->from[i] = fold_build3_loc (input_location, COND_EXPR,
    5507         7544 :                                           TREE_TYPE (ploop->from[i]),
    5508              :                                           second_loop_entry, idx[i],
    5509              :                                           ploop->from[i]);
    5510              : 
    5511         6203 :       gfc_trans_scalarized_loop_boundary (ploop, &body);
    5512              : 
    5513         6203 :       if (nested_loop)
    5514              :         {
    5515              :           /* The first loop already advanced the parent se'ss chain, so clear
    5516              :              the parent now to avoid doing it a second time, making the chain
    5517              :              out of sync.  */
    5518         1858 :           nested_se.parent = nullptr;
    5519         1858 :           nested_se.ss = orig_ss;
    5520              :         }
    5521              : 
    5522         6203 :       stmtblock_t * const outer_block = &ploop->code[ploop->dimen - 1];
    5523              : 
    5524         6203 :       if (HONOR_NANS (DECL_MODE (limit)))
    5525              :         {
    5526         3898 :           if (nonempty != NULL)
    5527              :             {
    5528         2329 :               stmtblock_t init_block;
    5529         2329 :               gfc_init_block (&init_block);
    5530              : 
    5531         5229 :               for (int i = 0; i < ploop->dimen; i++)
    5532         2900 :                 gfc_add_modify (&init_block, pos[i], gfc_index_one_node);
    5533              : 
    5534         2329 :               tree ifbody = gfc_finish_block (&init_block);
    5535         2329 :               tmp = build3_v (COND_EXPR, nonempty, ifbody,
    5536              :                               build_empty_stmt (input_location));
    5537         2329 :               gfc_add_expr_to_block (outer_block, tmp);
    5538              :             }
    5539              :         }
    5540              : 
    5541         6203 :       gfc_add_expr_to_block (outer_block, build1_v (GOTO_EXPR, lab2));
    5542         6203 :       gfc_add_expr_to_block (outer_block, build1_v (LABEL_EXPR, lab1));
    5543              : 
    5544              :       /* If we have a mask, only check this element if the mask is set.  */
    5545         6203 :       if (maskexpr && maskexpr->rank > 0)
    5546              :         {
    5547         3874 :           gfc_init_se (&maskse, base_se);
    5548         3874 :           gfc_copy_loopinfo_to_se (&maskse, ploop);
    5549         3874 :           if (!nested_loop)
    5550         2700 :             maskse.ss = maskss;
    5551         3874 :           gfc_conv_expr_val (&maskse, maskexpr);
    5552         3874 :           gfc_add_block_to_block (&body, &maskse.pre);
    5553              : 
    5554         3874 :           gfc_start_block (&block);
    5555              :         }
    5556              :       else
    5557         2329 :         gfc_init_block (&block);
    5558              : 
    5559              :       /* Compare with the current limit.  */
    5560         6203 :       gfc_init_se (&arrayse, base_se);
    5561         6203 :       gfc_copy_loopinfo_to_se (&arrayse, ploop);
    5562         6203 :       if (!nested_loop)
    5563         4345 :         arrayse.ss = arrayss;
    5564         6203 :       gfc_conv_expr_val (&arrayse, arrayexpr);
    5565         6203 :       gfc_add_block_to_block (&block, &arrayse.pre);
    5566              : 
    5567              :       /* We do the following if this is a more extreme value.  */
    5568         6203 :       gfc_start_block (&ifblock);
    5569              : 
    5570              :       /* Assign the value to the limit...  */
    5571         6203 :       gfc_add_modify (&ifblock, limit, arrayse.expr);
    5572              : 
    5573        13747 :       for (int i = 0; i < ploop->dimen; i++)
    5574              :         {
    5575         7544 :           tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
    5576              :                                  ploop->loopvar[i], offset[i]);
    5577         7544 :           gfc_add_modify (&ifblock, pos[i], tmp);
    5578              :         }
    5579              : 
    5580         6203 :       ifbody = gfc_finish_block (&ifblock);
    5581              : 
    5582              :       /* We switch to > or >= depending on the value of the BACK argument. */
    5583         6203 :       {
    5584         6203 :         tree ifbody2, elsebody2;
    5585              : 
    5586         6203 :         cond = gfc_create_var (logical_type_node, "cond");
    5587              : 
    5588         6203 :         gfc_start_block (&ifblock);
    5589         9537 :         b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
    5590              :                                 logical_type_node, arrayse.expr, limit);
    5591              : 
    5592         6203 :         gfc_add_modify (&ifblock, cond, b_if);
    5593         6203 :         ifbody2 = gfc_finish_block (&ifblock);
    5594              : 
    5595         6203 :         gfc_start_block (&elseblock);
    5596         6203 :         b_else = fold_build2_loc (input_location, op, logical_type_node,
    5597              :                                   arrayse.expr, limit);
    5598              : 
    5599         6203 :         gfc_add_modify (&elseblock, cond, b_else);
    5600         6203 :         elsebody2 = gfc_finish_block (&elseblock);
    5601              : 
    5602         6203 :         tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
    5603              :                                back, ifbody2, elsebody2);
    5604              :       }
    5605              : 
    5606         6203 :       gfc_add_expr_to_block (&block, tmp);
    5607         6203 :       cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
    5608         6203 :       tmp = build3_v (COND_EXPR, cond, ifbody,
    5609              :                       build_empty_stmt (input_location));
    5610              : 
    5611         6203 :       gfc_add_expr_to_block (&block, tmp);
    5612              : 
    5613         6203 :       if (maskexpr && maskexpr->rank > 0)
    5614              :         {
    5615              :           /* We enclose the above in if (mask) {...}.  If the mask is
    5616              :          an optional argument, generate IF (.NOT. PRESENT(MASK)
    5617              :          .OR. MASK(I)).*/
    5618              : 
    5619         3874 :           tree ifmask;
    5620         3874 :           ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5621         3874 :           tmp = gfc_finish_block (&block);
    5622         3874 :           tmp = build3_v (COND_EXPR, ifmask, tmp,
    5623              :                           build_empty_stmt (input_location));
    5624         3874 :         }
    5625              :       else
    5626         2329 :         tmp = gfc_finish_block (&block);
    5627              : 
    5628         6203 :       gfc_add_expr_to_block (&body, tmp);
    5629         6203 :       gfc_add_modify (&body, second_loop_entry, logical_false_node);
    5630              :     }
    5631              : 
    5632         9951 :   gfc_trans_scalarizing_loops (ploop, &body);
    5633              : 
    5634         9951 :   if (lab2)
    5635         6203 :     gfc_add_expr_to_block (&ploop->pre, build1_v (LABEL_EXPR, lab2));
    5636              : 
    5637              :   /* For a scalar mask, enclose the loop in an if statement.  */
    5638         9951 :   if (maskexpr && maskexpr->rank == 0)
    5639              :     {
    5640         2644 :       tree ifmask;
    5641              : 
    5642         2644 :       gfc_init_se (&maskse, nested_loop ? se : nullptr);
    5643         2644 :       gfc_conv_expr_val (&maskse, maskexpr);
    5644         2644 :       gfc_add_block_to_block (&se->pre, &maskse.pre);
    5645         2644 :       gfc_init_block (&block);
    5646         2644 :       gfc_add_block_to_block (&block, &ploop->pre);
    5647         2644 :       gfc_add_block_to_block (&block, &ploop->post);
    5648         2644 :       tmp = gfc_finish_block (&block);
    5649              : 
    5650              :       /* For the else part of the scalar mask, just initialize
    5651              :          the pos variable the same way as above.  */
    5652              : 
    5653         2644 :       gfc_init_block (&elseblock);
    5654         5580 :       for (int i = 0; i < ploop->dimen; i++)
    5655         2936 :         gfc_add_modify (&elseblock, pos[i], gfc_index_zero_node);
    5656         2644 :       elsetmp = gfc_finish_block (&elseblock);
    5657         2644 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5658         2644 :       tmp = build3_v (COND_EXPR, ifmask, tmp, elsetmp);
    5659         2644 :       gfc_add_expr_to_block (&block, tmp);
    5660         2644 :       gfc_add_block_to_block (&se->pre, &block);
    5661         2644 :     }
    5662              :   else
    5663              :     {
    5664         7307 :       gfc_add_block_to_block (&se->pre, &ploop->pre);
    5665         7307 :       gfc_add_block_to_block (&se->pre, &ploop->post);
    5666              :     }
    5667              : 
    5668         9951 :   if (!nested_loop)
    5669         7155 :     gfc_cleanup_loop (&loop);
    5670              : 
    5671         9951 :   if (!dim_present)
    5672              :     {
    5673         8837 :       for (int i = 0; i < arrayexpr->rank; i++)
    5674              :         {
    5675         5556 :           tree res_idx = build_int_cst (gfc_array_index_type, i);
    5676         5556 :           tree res_arr_ref = gfc_build_array_ref (result_var, res_idx,
    5677              :                                                   NULL_TREE, true);
    5678              : 
    5679         5556 :           tree value = convert (type, pos[i]);
    5680         5556 :           gfc_add_modify (&se->pre, res_arr_ref, value);
    5681              :         }
    5682              : 
    5683         3281 :       se->expr = result_var;
    5684              :     }
    5685              :   else
    5686         6670 :     se->expr = convert (type, pos[0]);
    5687              : }
    5688              : 
    5689              : /* Emit code for findloc.  */
    5690              : 
    5691              : static void
    5692         1332 : gfc_conv_intrinsic_findloc (gfc_se *se, gfc_expr *expr)
    5693              : {
    5694         1332 :   gfc_actual_arglist *array_arg, *value_arg, *dim_arg, *mask_arg,
    5695              :     *kind_arg, *back_arg;
    5696         1332 :   gfc_expr *value_expr;
    5697         1332 :   int ikind;
    5698         1332 :   tree resvar;
    5699         1332 :   stmtblock_t block;
    5700         1332 :   stmtblock_t body;
    5701         1332 :   stmtblock_t loopblock;
    5702         1332 :   tree type;
    5703         1332 :   tree tmp;
    5704         1332 :   tree found;
    5705         1332 :   tree forward_branch = NULL_TREE;
    5706         1332 :   tree back_branch;
    5707         1332 :   gfc_loopinfo loop;
    5708         1332 :   gfc_ss *arrayss;
    5709         1332 :   gfc_ss *maskss;
    5710         1332 :   gfc_se arrayse;
    5711         1332 :   gfc_se valuese;
    5712         1332 :   gfc_se maskse;
    5713         1332 :   gfc_se backse;
    5714         1332 :   tree exit_label;
    5715         1332 :   gfc_expr *maskexpr;
    5716         1332 :   tree offset;
    5717         1332 :   int i;
    5718         1332 :   bool optional_mask;
    5719              : 
    5720         1332 :   array_arg = expr->value.function.actual;
    5721         1332 :   value_arg = array_arg->next;
    5722         1332 :   dim_arg   = value_arg->next;
    5723         1332 :   mask_arg  = dim_arg->next;
    5724         1332 :   kind_arg  = mask_arg->next;
    5725         1332 :   back_arg  = kind_arg->next;
    5726              : 
    5727              :   /* Remove kind and set ikind.  */
    5728         1332 :   if (kind_arg->expr)
    5729              :     {
    5730            0 :       ikind = mpz_get_si (kind_arg->expr->value.integer);
    5731            0 :       gfc_free_expr (kind_arg->expr);
    5732            0 :       kind_arg->expr = NULL;
    5733              :     }
    5734              :   else
    5735         1332 :     ikind = gfc_default_integer_kind;
    5736              : 
    5737         1332 :   value_expr = value_arg->expr;
    5738              : 
    5739              :   /* Unless it's a string, pass VALUE by value.  */
    5740         1332 :   if (value_expr->ts.type != BT_CHARACTER)
    5741          732 :     value_arg->name = "%VAL";
    5742              : 
    5743              :   /* Pass BACK argument by value.  */
    5744         1332 :   back_arg->name = "%VAL";
    5745              : 
    5746              :   /* Call the library if we have a character function or if
    5747              :      rank > 0.  */
    5748         1332 :   if (se->ss || array_arg->expr->ts.type == BT_CHARACTER)
    5749              :     {
    5750         1200 :       se->ignore_optional = 1;
    5751         1200 :       if (expr->rank == 0)
    5752              :         {
    5753              :           /* Remove dim argument.  */
    5754           84 :           gfc_free_expr (dim_arg->expr);
    5755           84 :           dim_arg->expr = NULL;
    5756              :         }
    5757         1200 :       gfc_conv_intrinsic_funcall (se, expr);
    5758         1200 :       return;
    5759              :     }
    5760              : 
    5761          132 :   type = gfc_get_int_type (ikind);
    5762              : 
    5763              :   /* Initialize the result.  */
    5764          132 :   resvar = gfc_create_var (gfc_array_index_type, "pos");
    5765          132 :   gfc_add_modify (&se->pre, resvar, build_int_cst (gfc_array_index_type, 0));
    5766          132 :   offset = gfc_create_var (gfc_array_index_type, "offset");
    5767              : 
    5768          132 :   maskexpr = mask_arg->expr;
    5769           72 :   optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
    5770           60 :     && maskexpr->symtree->n.sym->attr.dummy
    5771          144 :     && maskexpr->symtree->n.sym->attr.optional;
    5772              : 
    5773              :   /*  Generate two loops, one for BACK=.true. and one for BACK=.false.  */
    5774              : 
    5775          396 :   for (i = 0 ; i < 2; i++)
    5776              :     {
    5777              :       /* Walk the arguments.  */
    5778          264 :       arrayss = gfc_walk_expr (array_arg->expr);
    5779          264 :       gcc_assert (arrayss != gfc_ss_terminator);
    5780              : 
    5781          264 :       if (maskexpr && maskexpr->rank != 0)
    5782              :         {
    5783           84 :           maskss = gfc_walk_expr (maskexpr);
    5784           84 :           gcc_assert (maskss != gfc_ss_terminator);
    5785              :         }
    5786              :       else
    5787              :         maskss = NULL;
    5788              : 
    5789              :       /* Initialize the scalarizer.  */
    5790          264 :       gfc_init_loopinfo (&loop);
    5791          264 :       exit_label = gfc_build_label_decl (NULL_TREE);
    5792          264 :       TREE_USED (exit_label) = 1;
    5793              : 
    5794              :       /* We add the mask first because the number of iterations is
    5795              :          taken from the last ss, and this breaks if an absent
    5796              :          optional argument is used for mask.  */
    5797              : 
    5798          264 :       if (maskss)
    5799           84 :         gfc_add_ss_to_loop (&loop, maskss);
    5800          264 :       gfc_add_ss_to_loop (&loop, arrayss);
    5801              : 
    5802              :       /* Initialize the loop.  */
    5803          264 :       gfc_conv_ss_startstride (&loop);
    5804          264 :       gfc_conv_loop_setup (&loop, &expr->where);
    5805              : 
    5806              :       /* Calculate the offset.  */
    5807          264 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    5808              :                              gfc_index_one_node, loop.from[0]);
    5809          264 :       gfc_add_modify (&loop.pre, offset, tmp);
    5810              : 
    5811          264 :       gfc_mark_ss_chain_used (arrayss, 1);
    5812          264 :       if (maskss)
    5813           84 :         gfc_mark_ss_chain_used (maskss, 1);
    5814              : 
    5815              :       /* The first loop is for BACK=.true.  */
    5816          264 :       if (i == 0)
    5817          132 :         loop.reverse[0] = GFC_REVERSE_SET;
    5818              : 
    5819              :       /* Generate the loop body.  */
    5820          264 :       gfc_start_scalarized_body (&loop, &body);
    5821              : 
    5822              :       /* If we have an array mask, only add the element if it is
    5823              :          set.  */
    5824          264 :       if (maskss)
    5825              :         {
    5826           84 :           gfc_init_se (&maskse, NULL);
    5827           84 :           gfc_copy_loopinfo_to_se (&maskse, &loop);
    5828           84 :           maskse.ss = maskss;
    5829           84 :           gfc_conv_expr_val (&maskse, maskexpr);
    5830           84 :           gfc_add_block_to_block (&body, &maskse.pre);
    5831              :         }
    5832              : 
    5833              :       /* If the condition matches then set the return value.  */
    5834          264 :       gfc_start_block (&block);
    5835              : 
    5836              :       /* Add the offset.  */
    5837          264 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5838          264 :                              TREE_TYPE (resvar),
    5839              :                              loop.loopvar[0], offset);
    5840          264 :       gfc_add_modify (&block, resvar, tmp);
    5841              :       /* And break out of the loop.  */
    5842          264 :       tmp = build1_v (GOTO_EXPR, exit_label);
    5843          264 :       gfc_add_expr_to_block (&block, tmp);
    5844              : 
    5845          264 :       found = gfc_finish_block (&block);
    5846              : 
    5847              :       /* Check this element.  */
    5848          264 :       gfc_init_se (&arrayse, NULL);
    5849          264 :       gfc_copy_loopinfo_to_se (&arrayse, &loop);
    5850          264 :       arrayse.ss = arrayss;
    5851          264 :       gfc_conv_expr_val (&arrayse, array_arg->expr);
    5852          264 :       gfc_add_block_to_block (&body, &arrayse.pre);
    5853              : 
    5854          264 :       gfc_init_se (&valuese, NULL);
    5855          264 :       gfc_conv_expr_val (&valuese, value_arg->expr);
    5856          264 :       gfc_add_block_to_block (&body, &valuese.pre);
    5857              : 
    5858          264 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    5859              :                              arrayse.expr, valuese.expr);
    5860              : 
    5861          264 :       tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
    5862          264 :       if (maskss)
    5863              :         {
    5864              :           /* We enclose the above in if (mask) {...}.  If the mask is
    5865              :              an optional argument, generate IF (.NOT. PRESENT(MASK)
    5866              :              .OR. MASK(I)). */
    5867              : 
    5868           84 :           tree ifmask;
    5869           84 :           ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5870           84 :           tmp = build3_v (COND_EXPR, ifmask, tmp,
    5871              :                           build_empty_stmt (input_location));
    5872              :         }
    5873              : 
    5874          264 :       gfc_add_expr_to_block (&body, tmp);
    5875          264 :       gfc_add_block_to_block (&body, &arrayse.post);
    5876              : 
    5877          264 :       gfc_trans_scalarizing_loops (&loop, &body);
    5878              : 
    5879              :       /* Add the exit label.  */
    5880          264 :       tmp = build1_v (LABEL_EXPR, exit_label);
    5881          264 :       gfc_add_expr_to_block (&loop.pre, tmp);
    5882          264 :       gfc_start_block (&loopblock);
    5883          264 :       gfc_add_block_to_block (&loopblock, &loop.pre);
    5884          264 :       gfc_add_block_to_block (&loopblock, &loop.post);
    5885          264 :       if (i == 0)
    5886          132 :         forward_branch = gfc_finish_block (&loopblock);
    5887              :       else
    5888          132 :         back_branch = gfc_finish_block (&loopblock);
    5889              : 
    5890          264 :       gfc_cleanup_loop (&loop);
    5891              :     }
    5892              : 
    5893              :   /* Enclose the two loops in an IF statement.  */
    5894              : 
    5895          132 :   gfc_init_se (&backse, NULL);
    5896          132 :   gfc_conv_expr_val (&backse, back_arg->expr);
    5897          132 :   gfc_add_block_to_block (&se->pre, &backse.pre);
    5898          132 :   tmp = build3_v (COND_EXPR, backse.expr, forward_branch, back_branch);
    5899              : 
    5900              :   /* For a scalar mask, enclose the loop in an if statement.  */
    5901          132 :   if (maskexpr && maskss == NULL)
    5902              :     {
    5903           30 :       tree ifmask;
    5904           30 :       tree if_stmt;
    5905              : 
    5906           30 :       gfc_init_se (&maskse, NULL);
    5907           30 :       gfc_conv_expr_val (&maskse, maskexpr);
    5908           30 :       gfc_init_block (&block);
    5909           30 :       gfc_add_expr_to_block (&block, maskse.expr);
    5910           30 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5911           30 :       if_stmt = build3_v (COND_EXPR, ifmask, tmp,
    5912              :                           build_empty_stmt (input_location));
    5913           30 :       gfc_add_expr_to_block (&block, if_stmt);
    5914           30 :       tmp = gfc_finish_block (&block);
    5915              :     }
    5916              : 
    5917          132 :   gfc_add_expr_to_block (&se->pre, tmp);
    5918          132 :   se->expr = convert (type, resvar);
    5919              : 
    5920              : }
    5921              : 
    5922              : /* Emit code for fstat, lstat and stat intrinsic subroutines.  */
    5923              : 
    5924              : static tree
    5925           55 : conv_intrinsic_fstat_lstat_stat_sub (gfc_code *code)
    5926              : {
    5927           55 :   stmtblock_t block;
    5928           55 :   gfc_se se, se_stat;
    5929           55 :   tree unit = NULL_TREE;
    5930           55 :   tree name = NULL_TREE;
    5931           55 :   tree slen = NULL_TREE;
    5932           55 :   tree vals;
    5933           55 :   tree arg3 = NULL_TREE;
    5934           55 :   tree stat = NULL_TREE ;
    5935           55 :   tree present = NULL_TREE;
    5936           55 :   tree tmp;
    5937           55 :   int kind;
    5938              : 
    5939           55 :   gfc_init_block (&block);
    5940           55 :   gfc_init_se (&se, NULL);
    5941              : 
    5942           55 :   switch (code->resolved_isym->id)
    5943              :     {
    5944           21 :     case GFC_ISYM_FSTAT:
    5945              :       /* Deal with the UNIT argument.  */
    5946           21 :       gfc_conv_expr (&se, code->ext.actual->expr);
    5947           21 :       gfc_add_block_to_block (&block, &se.pre);
    5948           21 :       unit = gfc_evaluate_now (se.expr, &block);
    5949           21 :       unit = gfc_build_addr_expr (NULL_TREE, unit);
    5950           21 :       gfc_add_block_to_block (&block, &se.post);
    5951           21 :       break;
    5952              : 
    5953           34 :     case GFC_ISYM_LSTAT:
    5954           34 :     case GFC_ISYM_STAT:
    5955              :       /* Deal with the NAME argument.  */
    5956           34 :       gfc_conv_expr (&se, code->ext.actual->expr);
    5957           34 :       gfc_conv_string_parameter (&se);
    5958           34 :       gfc_add_block_to_block (&block, &se.pre);
    5959           34 :       name = se.expr;
    5960           34 :       slen = se.string_length;
    5961           34 :       gfc_add_block_to_block (&block, &se.post);
    5962           34 :       break;
    5963              : 
    5964            0 :     default:
    5965            0 :       gcc_unreachable ();
    5966              :     }
    5967              : 
    5968              :   /* Deal with the VALUES argument.  */
    5969           55 :   gfc_init_se (&se, NULL);
    5970           55 :   gfc_conv_expr_descriptor (&se, code->ext.actual->next->expr);
    5971           55 :   vals = gfc_build_addr_expr (NULL_TREE, se.expr);
    5972           55 :   gfc_add_block_to_block (&block, &se.pre);
    5973           55 :   gfc_add_block_to_block (&block, &se.post);
    5974           55 :   kind = code->ext.actual->next->expr->ts.kind;
    5975              : 
    5976              :   /* Deal with an optional STATUS.  */
    5977           55 :   if (code->ext.actual->next->next->expr)
    5978              :     {
    5979           45 :       gfc_init_se (&se_stat, NULL);
    5980           45 :       gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
    5981           45 :       stat = gfc_create_var (gfc_get_int_type (kind), "_stat");
    5982           45 :       arg3 = gfc_build_addr_expr (NULL_TREE, stat);
    5983              : 
    5984              :       /* Handle case of status being an optional dummy.  */
    5985           45 :       gfc_symbol *sym = code->ext.actual->next->next->expr->symtree->n.sym;
    5986           45 :       if (sym->attr.dummy && sym->attr.optional)
    5987              :         {
    5988            6 :           present = gfc_conv_expr_present (sym);
    5989           12 :           arg3 = fold_build3_loc (input_location, COND_EXPR,
    5990            6 :                                   TREE_TYPE (arg3), present, arg3,
    5991            6 :                                   fold_convert (TREE_TYPE (arg3),
    5992              :                                                 null_pointer_node));
    5993              :         }
    5994              :     }
    5995              : 
    5996              :   /* Call library function depending on KIND of VALUES argument.  */
    5997           55 :   switch (code->resolved_isym->id)
    5998              :     {
    5999           21 :     case GFC_ISYM_FSTAT:
    6000           21 :       tmp = (kind == 4 ? gfor_fndecl_fstat_i4_sub : gfor_fndecl_fstat_i8_sub);
    6001              :       break;
    6002           14 :     case GFC_ISYM_LSTAT:
    6003           14 :       tmp = (kind == 4 ? gfor_fndecl_lstat_i4_sub : gfor_fndecl_lstat_i8_sub);
    6004              :       break;
    6005           20 :     case GFC_ISYM_STAT:
    6006           20 :       tmp = (kind == 4 ? gfor_fndecl_stat_i4_sub : gfor_fndecl_stat_i8_sub);
    6007              :       break;
    6008            0 :     default:
    6009            0 :       gcc_unreachable ();
    6010              :     }
    6011              : 
    6012           55 :   if (code->resolved_isym->id == GFC_ISYM_FSTAT)
    6013           21 :     tmp = build_call_expr_loc (input_location, tmp, 3, unit, vals,
    6014              :                                stat ? arg3 : null_pointer_node);
    6015              :   else
    6016           34 :     tmp = build_call_expr_loc (input_location, tmp, 4, name, vals,
    6017              :                                stat ? arg3 : null_pointer_node, slen);
    6018           55 :   gfc_add_expr_to_block (&block, tmp);
    6019              : 
    6020              :   /* Handle kind conversion of status.  */
    6021           55 :   if (stat && stat != se_stat.expr)
    6022              :     {
    6023           45 :       stmtblock_t block2;
    6024              : 
    6025           45 :       gfc_init_block (&block2);
    6026           45 :       gfc_add_modify (&block2, se_stat.expr,
    6027           45 :                       fold_convert (TREE_TYPE (se_stat.expr), stat));
    6028              : 
    6029           45 :       if (present)
    6030              :         {
    6031            6 :           tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block2),
    6032              :                           build_empty_stmt (input_location));
    6033            6 :           gfc_add_expr_to_block (&block, tmp);
    6034              :         }
    6035              :       else
    6036           39 :         gfc_add_block_to_block (&block, &block2);
    6037              :     }
    6038              : 
    6039           55 :   return gfc_finish_block (&block);
    6040              : }
    6041              : 
    6042              : /* Emit code for minval or maxval intrinsic.  There are many different cases
    6043              :    we need to handle.  For performance reasons we sometimes create two
    6044              :    loops instead of one, where the second one is much simpler.
    6045              :    Examples for minval intrinsic:
    6046              :    1) Result is an array, a call is generated
    6047              :    2) Array mask is used and NaNs need to be supported, rank 1:
    6048              :       limit = Infinity;
    6049              :       nonempty = false;
    6050              :       S = from;
    6051              :       while (S <= to) {
    6052              :         if (mask[S]) {
    6053              :           nonempty = true;
    6054              :           if (a[S] <= limit) {
    6055              :             limit = a[S];
    6056              :             S++;
    6057              :             goto lab;
    6058              :           }
    6059              :         else
    6060              :           S++;
    6061              :         }
    6062              :       }
    6063              :       limit = nonempty ? NaN : huge (limit);
    6064              :       lab:
    6065              :       while (S <= to) { if(mask[S]) limit = min (a[S], limit); S++; }
    6066              :    3) NaNs need to be supported, but it is known at compile time or cheaply
    6067              :       at runtime whether array is nonempty or not, rank 1:
    6068              :       limit = Infinity;
    6069              :       S = from;
    6070              :       while (S <= to) {
    6071              :         if (a[S] <= limit) {
    6072              :           limit = a[S];
    6073              :           S++;
    6074              :           goto lab;
    6075              :           }
    6076              :         else
    6077              :           S++;
    6078              :       }
    6079              :       limit = (from <= to) ? NaN : huge (limit);
    6080              :       lab:
    6081              :       while (S <= to) { limit = min (a[S], limit); S++; }
    6082              :    4) Array mask is used and NaNs need to be supported, rank > 1:
    6083              :       limit = Infinity;
    6084              :       nonempty = false;
    6085              :       fast = false;
    6086              :       S1 = from1;
    6087              :       while (S1 <= to1) {
    6088              :         S2 = from2;
    6089              :         while (S2 <= to2) {
    6090              :           if (mask[S1][S2]) {
    6091              :             if (fast) limit = min (a[S1][S2], limit);
    6092              :             else {
    6093              :               nonempty = true;
    6094              :               if (a[S1][S2] <= limit) {
    6095              :                 limit = a[S1][S2];
    6096              :                 fast = true;
    6097              :               }
    6098              :             }
    6099              :           }
    6100              :           S2++;
    6101              :         }
    6102              :         S1++;
    6103              :       }
    6104              :       if (!fast)
    6105              :         limit = nonempty ? NaN : huge (limit);
    6106              :    5) NaNs need to be supported, but it is known at compile time or cheaply
    6107              :       at runtime whether array is nonempty or not, rank > 1:
    6108              :       limit = Infinity;
    6109              :       fast = false;
    6110              :       S1 = from1;
    6111              :       while (S1 <= to1) {
    6112              :         S2 = from2;
    6113              :         while (S2 <= to2) {
    6114              :           if (fast) limit = min (a[S1][S2], limit);
    6115              :           else {
    6116              :             if (a[S1][S2] <= limit) {
    6117              :               limit = a[S1][S2];
    6118              :               fast = true;
    6119              :             }
    6120              :           }
    6121              :           S2++;
    6122              :         }
    6123              :         S1++;
    6124              :       }
    6125              :       if (!fast)
    6126              :         limit = (nonempty_array) ? NaN : huge (limit);
    6127              :    6) NaNs aren't supported, but infinities are.  Array mask is used:
    6128              :       limit = Infinity;
    6129              :       nonempty = false;
    6130              :       S = from;
    6131              :       while (S <= to) {
    6132              :         if (mask[S]) { nonempty = true; limit = min (a[S], limit); }
    6133              :         S++;
    6134              :       }
    6135              :       limit = nonempty ? limit : huge (limit);
    6136              :    7) Same without array mask:
    6137              :       limit = Infinity;
    6138              :       S = from;
    6139              :       while (S <= to) { limit = min (a[S], limit); S++; }
    6140              :       limit = (from <= to) ? limit : huge (limit);
    6141              :    8) Neither NaNs nor infinities are supported (-ffast-math or BT_INTEGER):
    6142              :       limit = huge (limit);
    6143              :       S = from;
    6144              :       while (S <= to) { limit = min (a[S], limit); S++); }
    6145              :       (or
    6146              :       while (S <= to) { if (mask[S]) limit = min (a[S], limit); S++; }
    6147              :       with array mask instead).
    6148              :    For 3), 5), 7) and 8), if mask is scalar, this all goes into a conditional,
    6149              :    setting limit = huge (limit); in the else branch.  */
    6150              : 
    6151              : static void
    6152         2417 : gfc_conv_intrinsic_minmaxval (gfc_se * se, gfc_expr * expr, enum tree_code op)
    6153              : {
    6154         2417 :   tree limit;
    6155         2417 :   tree type;
    6156         2417 :   tree tmp;
    6157         2417 :   tree ifbody;
    6158         2417 :   tree nonempty;
    6159         2417 :   tree nonempty_var;
    6160         2417 :   tree lab;
    6161         2417 :   tree fast;
    6162         2417 :   tree huge_cst = NULL, nan_cst = NULL;
    6163         2417 :   stmtblock_t body;
    6164         2417 :   stmtblock_t block, block2;
    6165         2417 :   gfc_loopinfo loop;
    6166         2417 :   gfc_actual_arglist *actual;
    6167         2417 :   gfc_ss *arrayss;
    6168         2417 :   gfc_ss *maskss;
    6169         2417 :   gfc_se arrayse;
    6170         2417 :   gfc_se maskse;
    6171         2417 :   gfc_expr *arrayexpr;
    6172         2417 :   gfc_expr *maskexpr;
    6173         2417 :   int n;
    6174         2417 :   bool optional_mask;
    6175              : 
    6176         2417 :   if (se->ss)
    6177              :     {
    6178            0 :       gfc_conv_intrinsic_funcall (se, expr);
    6179          186 :       return;
    6180              :     }
    6181              : 
    6182         2417 :   actual = expr->value.function.actual;
    6183         2417 :   arrayexpr = actual->expr;
    6184              : 
    6185         2417 :   if (arrayexpr->ts.type == BT_CHARACTER)
    6186              :     {
    6187          186 :       gfc_actual_arglist *dim = actual->next;
    6188          186 :       if (expr->rank == 0 && dim->expr != 0)
    6189              :         {
    6190            6 :           gfc_free_expr (dim->expr);
    6191            6 :           dim->expr = NULL;
    6192              :         }
    6193          186 :       gfc_conv_intrinsic_funcall (se, expr);
    6194          186 :       return;
    6195              :     }
    6196              : 
    6197         2231 :   type = gfc_typenode_for_spec (&expr->ts);
    6198              :   /* Initialize the result.  */
    6199         2231 :   limit = gfc_create_var (type, "limit");
    6200         2231 :   n = gfc_validate_kind (expr->ts.type, expr->ts.kind, false);
    6201         2231 :   switch (expr->ts.type)
    6202              :     {
    6203         1245 :     case BT_REAL:
    6204         1245 :       huge_cst = gfc_conv_mpfr_to_tree (gfc_real_kinds[n].huge,
    6205              :                                         expr->ts.kind, 0);
    6206         1245 :       if (HONOR_INFINITIES (DECL_MODE (limit)))
    6207              :         {
    6208         1241 :           REAL_VALUE_TYPE real;
    6209         1241 :           real_inf (&real);
    6210         1241 :           tmp = build_real (type, real);
    6211              :         }
    6212              :       else
    6213              :         tmp = huge_cst;
    6214         1245 :       if (HONOR_NANS (DECL_MODE (limit)))
    6215         1241 :         nan_cst = gfc_build_nan (type, "");
    6216              :       break;
    6217              : 
    6218          956 :     case BT_INTEGER:
    6219          956 :       tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge, expr->ts.kind);
    6220          956 :       break;
    6221              : 
    6222           30 :     case BT_UNSIGNED:
    6223              :       /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE().  */
    6224           30 :       if (op == GT_EXPR)
    6225           18 :         tmp = build_int_cst (type, 0);
    6226              :       else
    6227           12 :         tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
    6228              :                                              expr->ts.kind);
    6229              :       break;
    6230              : 
    6231            0 :     default:
    6232            0 :       gcc_unreachable ();
    6233              :     }
    6234              : 
    6235              :   /* We start with the most negative possible value for MAXVAL, and the most
    6236              :      positive possible value for MINVAL. The most negative possible value is
    6237              :      -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
    6238              :      possible value is HUGE in both cases.   BT_UNSIGNED has already been dealt
    6239              :      with above.  */
    6240         2231 :   if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
    6241              :     {
    6242          987 :       tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
    6243          987 :       if (huge_cst)
    6244          560 :         huge_cst = fold_build1_loc (input_location, NEGATE_EXPR,
    6245          560 :                                     TREE_TYPE (huge_cst), huge_cst);
    6246              :     }
    6247              : 
    6248         1005 :   if (op == GT_EXPR && expr->ts.type == BT_INTEGER)
    6249          427 :     tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
    6250              :                            tmp, build_int_cst (type, 1));
    6251              : 
    6252         2231 :   gfc_add_modify (&se->pre, limit, tmp);
    6253              : 
    6254              :   /* Walk the arguments.  */
    6255         2231 :   arrayss = gfc_walk_expr (arrayexpr);
    6256         2231 :   gcc_assert (arrayss != gfc_ss_terminator);
    6257              : 
    6258         2231 :   actual = actual->next->next;
    6259         2231 :   gcc_assert (actual);
    6260         2231 :   maskexpr = actual->expr;
    6261         1572 :   optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
    6262         1560 :     && maskexpr->symtree->n.sym->attr.dummy
    6263         2243 :     && maskexpr->symtree->n.sym->attr.optional;
    6264         1560 :   nonempty = NULL;
    6265         1572 :   if (maskexpr && maskexpr->rank != 0)
    6266              :     {
    6267         1026 :       maskss = gfc_walk_expr (maskexpr);
    6268         1026 :       gcc_assert (maskss != gfc_ss_terminator);
    6269              :     }
    6270              :   else
    6271              :     {
    6272         1205 :       mpz_t asize;
    6273         1205 :       if (gfc_array_size (arrayexpr, &asize))
    6274              :         {
    6275          678 :           nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
    6276          678 :           mpz_clear (asize);
    6277          678 :           nonempty = fold_build2_loc (input_location, GT_EXPR,
    6278              :                                       logical_type_node, nonempty,
    6279              :                                       gfc_index_zero_node);
    6280              :         }
    6281         1205 :       maskss = NULL;
    6282              :     }
    6283              : 
    6284              :   /* Initialize the scalarizer.  */
    6285         2231 :   gfc_init_loopinfo (&loop);
    6286              : 
    6287              :   /* We add the mask first because the number of iterations is taken
    6288              :      from the last ss, and this breaks if an absent optional argument
    6289              :      is used for mask.  */
    6290              : 
    6291         2231 :   if (maskss)
    6292         1026 :     gfc_add_ss_to_loop (&loop, maskss);
    6293         2231 :   gfc_add_ss_to_loop (&loop, arrayss);
    6294              : 
    6295              :   /* Initialize the loop.  */
    6296         2231 :   gfc_conv_ss_startstride (&loop);
    6297              : 
    6298              :   /* The code generated can have more than one loop in sequence (see the
    6299              :      comment at the function header).  This doesn't work well with the
    6300              :      scalarizer, which changes arrays' offset when the scalarization loops
    6301              :      are generated (see gfc_trans_preloop_setup).  Fortunately, {min,max}val
    6302              :      are  currently inlined in the scalar case only.  As there is no dependency
    6303              :      to care about in that case, there is no temporary, so that we can use the
    6304              :      scalarizer temporary code to handle multiple loops.  Thus, we set temp_dim
    6305              :      here, we call gfc_mark_ss_chain_used with flag=3 later, and we use
    6306              :      gfc_trans_scalarized_loop_boundary even later to restore offset.
    6307              :      TODO: this prevents inlining of rank > 0 minmaxval calls, so this
    6308              :      should eventually go away.  We could either create two loops properly,
    6309              :      or find another way to save/restore the array offsets between the two
    6310              :      loops (without conflicting with temporary management), or use a single
    6311              :      loop minmaxval implementation.  See PR 31067.  */
    6312         2231 :   loop.temp_dim = loop.dimen;
    6313         2231 :   gfc_conv_loop_setup (&loop, &expr->where);
    6314              : 
    6315         2231 :   if (nonempty == NULL && maskss == NULL
    6316          527 :       && loop.dimen == 1 && loop.from[0] && loop.to[0])
    6317          491 :     nonempty = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    6318              :                                 loop.from[0], loop.to[0]);
    6319         2231 :   nonempty_var = NULL;
    6320         2231 :   if (nonempty == NULL
    6321         2231 :       && (HONOR_INFINITIES (DECL_MODE (limit))
    6322          480 :           || HONOR_NANS (DECL_MODE (limit))))
    6323              :     {
    6324          582 :       nonempty_var = gfc_create_var (logical_type_node, "nonempty");
    6325          582 :       gfc_add_modify (&se->pre, nonempty_var, logical_false_node);
    6326          582 :       nonempty = nonempty_var;
    6327              :     }
    6328         2231 :   lab = NULL;
    6329         2231 :   fast = NULL;
    6330         2231 :   if (HONOR_NANS (DECL_MODE (limit)))
    6331              :     {
    6332         1241 :       if (loop.dimen == 1)
    6333              :         {
    6334          821 :           lab = gfc_build_label_decl (NULL_TREE);
    6335          821 :           TREE_USED (lab) = 1;
    6336              :         }
    6337              :       else
    6338              :         {
    6339          420 :           fast = gfc_create_var (logical_type_node, "fast");
    6340          420 :           gfc_add_modify (&se->pre, fast, logical_false_node);
    6341              :         }
    6342              :     }
    6343              : 
    6344         2231 :   gfc_mark_ss_chain_used (arrayss, lab ? 3 : 1);
    6345         2231 :   if (maskss)
    6346         1704 :     gfc_mark_ss_chain_used (maskss, lab ? 3 : 1);
    6347              :   /* Generate the loop body.  */
    6348         2231 :   gfc_start_scalarized_body (&loop, &body);
    6349              : 
    6350              :   /* If we have a mask, only add this element if the mask is set.  */
    6351         2231 :   if (maskss)
    6352              :     {
    6353         1026 :       gfc_init_se (&maskse, NULL);
    6354         1026 :       gfc_copy_loopinfo_to_se (&maskse, &loop);
    6355         1026 :       maskse.ss = maskss;
    6356         1026 :       gfc_conv_expr_val (&maskse, maskexpr);
    6357         1026 :       gfc_add_block_to_block (&body, &maskse.pre);
    6358              : 
    6359         1026 :       gfc_start_block (&block);
    6360              :     }
    6361              :   else
    6362         1205 :     gfc_init_block (&block);
    6363              : 
    6364              :   /* Compare with the current limit.  */
    6365         2231 :   gfc_init_se (&arrayse, NULL);
    6366         2231 :   gfc_copy_loopinfo_to_se (&arrayse, &loop);
    6367         2231 :   arrayse.ss = arrayss;
    6368         2231 :   gfc_conv_expr_val (&arrayse, arrayexpr);
    6369         2231 :   arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
    6370         2231 :   gfc_add_block_to_block (&block, &arrayse.pre);
    6371              : 
    6372         2231 :   gfc_init_block (&block2);
    6373              : 
    6374         2231 :   if (nonempty_var)
    6375          582 :     gfc_add_modify (&block2, nonempty_var, logical_true_node);
    6376              : 
    6377         2231 :   if (HONOR_NANS (DECL_MODE (limit)))
    6378              :     {
    6379         1922 :       tmp = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
    6380              :                              logical_type_node, arrayse.expr, limit);
    6381         1241 :       if (lab)
    6382              :         {
    6383          821 :           stmtblock_t ifblock;
    6384          821 :           tree inc_loop;
    6385          821 :           inc_loop = fold_build2_loc (input_location, PLUS_EXPR,
    6386          821 :                                       TREE_TYPE (loop.loopvar[0]),
    6387              :                                       loop.loopvar[0], gfc_index_one_node);
    6388          821 :           gfc_init_block (&ifblock);
    6389          821 :           gfc_add_modify (&ifblock, limit, arrayse.expr);
    6390          821 :           gfc_add_modify (&ifblock, loop.loopvar[0], inc_loop);
    6391          821 :           gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab));
    6392          821 :           ifbody = gfc_finish_block (&ifblock);
    6393              :         }
    6394              :       else
    6395              :         {
    6396          420 :           stmtblock_t ifblock;
    6397              : 
    6398          420 :           gfc_init_block (&ifblock);
    6399          420 :           gfc_add_modify (&ifblock, limit, arrayse.expr);
    6400          420 :           gfc_add_modify (&ifblock, fast, logical_true_node);
    6401          420 :           ifbody = gfc_finish_block (&ifblock);
    6402              :         }
    6403         1241 :       tmp = build3_v (COND_EXPR, tmp, ifbody,
    6404              :                       build_empty_stmt (input_location));
    6405         1241 :       gfc_add_expr_to_block (&block2, tmp);
    6406              :     }
    6407              :   else
    6408              :     {
    6409              :       /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
    6410              :          signed zeros.  */
    6411         1535 :       tmp = fold_build2_loc (input_location,
    6412              :                              op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
    6413              :                              type, arrayse.expr, limit);
    6414          990 :       gfc_add_modify (&block2, limit, tmp);
    6415              :     }
    6416              : 
    6417         2231 :   if (fast)
    6418              :     {
    6419          420 :       tree elsebody = gfc_finish_block (&block2);
    6420              : 
    6421              :       /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
    6422              :          signed zeros.  */
    6423          420 :       if (HONOR_NANS (DECL_MODE (limit)))
    6424              :         {
    6425          420 :           tmp = fold_build2_loc (input_location, op, logical_type_node,
    6426              :                                  arrayse.expr, limit);
    6427          420 :           ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
    6428          420 :           ifbody = build3_v (COND_EXPR, tmp, ifbody,
    6429              :                              build_empty_stmt (input_location));
    6430              :         }
    6431              :       else
    6432              :         {
    6433            0 :           tmp = fold_build2_loc (input_location,
    6434              :                                  op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
    6435              :                                  type, arrayse.expr, limit);
    6436            0 :           ifbody = build2_v (MODIFY_EXPR, limit, tmp);
    6437              :         }
    6438          420 :       tmp = build3_v (COND_EXPR, fast, ifbody, elsebody);
    6439          420 :       gfc_add_expr_to_block (&block, tmp);
    6440              :     }
    6441              :   else
    6442         1811 :     gfc_add_block_to_block (&block, &block2);
    6443              : 
    6444         2231 :   gfc_add_block_to_block (&block, &arrayse.post);
    6445              : 
    6446         2231 :   tmp = gfc_finish_block (&block);
    6447         2231 :   if (maskss)
    6448              :     {
    6449              :       /* We enclose the above in if (mask) {...}.  If the mask is an
    6450              :          optional argument, generate IF (.NOT. PRESENT(MASK)
    6451              :          .OR. MASK(I)).  */
    6452         1026 :       tree ifmask;
    6453         1026 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    6454         1026 :       tmp = build3_v (COND_EXPR, ifmask, tmp,
    6455              :                       build_empty_stmt (input_location));
    6456              :     }
    6457         2231 :   gfc_add_expr_to_block (&body, tmp);
    6458              : 
    6459         2231 :   if (lab)
    6460              :     {
    6461          821 :       gfc_trans_scalarized_loop_boundary (&loop, &body);
    6462              : 
    6463          821 :       tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
    6464              :                              nan_cst, huge_cst);
    6465          821 :       gfc_add_modify (&loop.code[0], limit, tmp);
    6466          821 :       gfc_add_expr_to_block (&loop.code[0], build1_v (LABEL_EXPR, lab));
    6467              : 
    6468              :       /* If we have a mask, only add this element if the mask is set.  */
    6469          821 :       if (maskss)
    6470              :         {
    6471          348 :           gfc_init_se (&maskse, NULL);
    6472          348 :           gfc_copy_loopinfo_to_se (&maskse, &loop);
    6473          348 :           maskse.ss = maskss;
    6474          348 :           gfc_conv_expr_val (&maskse, maskexpr);
    6475          348 :           gfc_add_block_to_block (&body, &maskse.pre);
    6476              : 
    6477          348 :           gfc_start_block (&block);
    6478              :         }
    6479              :       else
    6480          473 :         gfc_init_block (&block);
    6481              : 
    6482              :       /* Compare with the current limit.  */
    6483          821 :       gfc_init_se (&arrayse, NULL);
    6484          821 :       gfc_copy_loopinfo_to_se (&arrayse, &loop);
    6485          821 :       arrayse.ss = arrayss;
    6486          821 :       gfc_conv_expr_val (&arrayse, arrayexpr);
    6487          821 :       arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
    6488          821 :       gfc_add_block_to_block (&block, &arrayse.pre);
    6489              : 
    6490              :       /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
    6491              :          signed zeros.  */
    6492          821 :       if (HONOR_NANS (DECL_MODE (limit)))
    6493              :         {
    6494          821 :           tmp = fold_build2_loc (input_location, op, logical_type_node,
    6495              :                                  arrayse.expr, limit);
    6496          821 :           ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
    6497          821 :           tmp = build3_v (COND_EXPR, tmp, ifbody,
    6498              :                           build_empty_stmt (input_location));
    6499          821 :           gfc_add_expr_to_block (&block, tmp);
    6500              :         }
    6501              :       else
    6502              :         {
    6503            0 :           tmp = fold_build2_loc (input_location,
    6504              :                                  op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
    6505              :                                  type, arrayse.expr, limit);
    6506            0 :           gfc_add_modify (&block, limit, tmp);
    6507              :         }
    6508              : 
    6509          821 :       gfc_add_block_to_block (&block, &arrayse.post);
    6510              : 
    6511          821 :       tmp = gfc_finish_block (&block);
    6512          821 :       if (maskss)
    6513              :         /* We enclose the above in if (mask) {...}.  */
    6514              :         {
    6515          348 :           tree ifmask;
    6516          348 :           ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    6517          348 :           tmp = build3_v (COND_EXPR, ifmask, tmp,
    6518              :                           build_empty_stmt (input_location));
    6519              :         }
    6520              : 
    6521          821 :       gfc_add_expr_to_block (&body, tmp);
    6522              :       /* Avoid initializing loopvar[0] again, it should be left where
    6523              :          it finished by the first loop.  */
    6524          821 :       loop.from[0] = loop.loopvar[0];
    6525              :     }
    6526         2231 :   gfc_trans_scalarizing_loops (&loop, &body);
    6527              : 
    6528         2231 :   if (fast)
    6529              :     {
    6530          420 :       tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
    6531              :                              nan_cst, huge_cst);
    6532          420 :       ifbody = build2_v (MODIFY_EXPR, limit, tmp);
    6533          420 :       tmp = build3_v (COND_EXPR, fast, build_empty_stmt (input_location),
    6534              :                       ifbody);
    6535          420 :       gfc_add_expr_to_block (&loop.pre, tmp);
    6536              :     }
    6537         1811 :   else if (HONOR_INFINITIES (DECL_MODE (limit)) && !lab)
    6538              :     {
    6539            0 :       tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty, limit,
    6540              :                              huge_cst);
    6541            0 :       gfc_add_modify (&loop.pre, limit, tmp);
    6542              :     }
    6543              : 
    6544              :   /* For a scalar mask, enclose the loop in an if statement.  */
    6545         2231 :   if (maskexpr && maskss == NULL)
    6546              :     {
    6547          546 :       tree else_stmt;
    6548          546 :       tree ifmask;
    6549              : 
    6550          546 :       gfc_init_se (&maskse, NULL);
    6551          546 :       gfc_conv_expr_val (&maskse, maskexpr);
    6552          546 :       gfc_init_block (&block);
    6553          546 :       gfc_add_block_to_block (&block, &loop.pre);
    6554          546 :       gfc_add_block_to_block (&block, &loop.post);
    6555          546 :       tmp = gfc_finish_block (&block);
    6556              : 
    6557          546 :       if (HONOR_INFINITIES (DECL_MODE (limit)))
    6558          354 :         else_stmt = build2_v (MODIFY_EXPR, limit, huge_cst);
    6559              :       else
    6560          192 :         else_stmt = build_empty_stmt (input_location);
    6561              : 
    6562          546 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    6563          546 :       tmp = build3_v (COND_EXPR, ifmask, tmp, else_stmt);
    6564          546 :       gfc_add_expr_to_block (&block, tmp);
    6565          546 :       gfc_add_block_to_block (&se->pre, &block);
    6566              :     }
    6567              :   else
    6568              :     {
    6569         1685 :       gfc_add_block_to_block (&se->pre, &loop.pre);
    6570         1685 :       gfc_add_block_to_block (&se->pre, &loop.post);
    6571              :     }
    6572              : 
    6573         2231 :   gfc_cleanup_loop (&loop);
    6574              : 
    6575         2231 :   se->expr = limit;
    6576              : }
    6577              : 
    6578              : /* BTEST (i, pos) = (i & (1 << pos)) != 0.  */
    6579              : static void
    6580          145 : gfc_conv_intrinsic_btest (gfc_se * se, gfc_expr * expr)
    6581              : {
    6582          145 :   tree args[2];
    6583          145 :   tree type;
    6584          145 :   tree tmp;
    6585              : 
    6586          145 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    6587          145 :   type = TREE_TYPE (args[0]);
    6588              : 
    6589              :   /* Optionally generate code for runtime argument check.  */
    6590          145 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    6591              :     {
    6592            6 :       tree below = fold_build2_loc (input_location, LT_EXPR,
    6593              :                                     logical_type_node, args[1],
    6594            6 :                                     build_int_cst (TREE_TYPE (args[1]), 0));
    6595            6 :       tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
    6596            6 :       tree above = fold_build2_loc (input_location, GE_EXPR,
    6597              :                                     logical_type_node, args[1], nbits);
    6598            6 :       tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    6599              :                                     logical_type_node, below, above);
    6600            6 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    6601              :                                "POS argument (%ld) out of range 0:%ld "
    6602              :                                "in intrinsic BTEST",
    6603              :                                fold_convert (long_integer_type_node, args[1]),
    6604              :                                fold_convert (long_integer_type_node, nbits));
    6605              :     }
    6606              : 
    6607          145 :   tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    6608              :                          build_int_cst (type, 1), args[1]);
    6609          145 :   tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], tmp);
    6610          145 :   tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
    6611              :                          build_int_cst (type, 0));
    6612          145 :   type = gfc_typenode_for_spec (&expr->ts);
    6613          145 :   se->expr = convert (type, tmp);
    6614          145 : }
    6615              : 
    6616              : 
    6617              : /* Generate code for BGE, BGT, BLE and BLT intrinsics.  */
    6618              : static void
    6619          216 : gfc_conv_intrinsic_bitcomp (gfc_se * se, gfc_expr * expr, enum tree_code op)
    6620              : {
    6621          216 :   tree args[2];
    6622              : 
    6623          216 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    6624              : 
    6625              :   /* Convert both arguments to the unsigned type of the same size.  */
    6626          216 :   args[0] = fold_convert (unsigned_type_for (TREE_TYPE (args[0])), args[0]);
    6627          216 :   args[1] = fold_convert (unsigned_type_for (TREE_TYPE (args[1])), args[1]);
    6628              : 
    6629              :   /* If they have unequal type size, convert to the larger one.  */
    6630          216 :   if (TYPE_PRECISION (TREE_TYPE (args[0]))
    6631          216 :       > TYPE_PRECISION (TREE_TYPE (args[1])))
    6632            0 :     args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
    6633          216 :   else if (TYPE_PRECISION (TREE_TYPE (args[1]))
    6634          216 :            > TYPE_PRECISION (TREE_TYPE (args[0])))
    6635            0 :     args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
    6636              : 
    6637              :   /* Now, we compare them.  */
    6638          216 :   se->expr = fold_build2_loc (input_location, op, logical_type_node,
    6639              :                               args[0], args[1]);
    6640          216 : }
    6641              : 
    6642              : 
    6643              : /* Generate code to perform the specified operation.  */
    6644              : static void
    6645         1915 : gfc_conv_intrinsic_bitop (gfc_se * se, gfc_expr * expr, enum tree_code op)
    6646              : {
    6647         1915 :   tree args[2];
    6648              : 
    6649         1915 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    6650         1915 :   se->expr = fold_build2_loc (input_location, op, TREE_TYPE (args[0]),
    6651              :                               args[0], args[1]);
    6652         1915 : }
    6653              : 
    6654              : /* Bitwise not.  */
    6655              : static void
    6656          230 : gfc_conv_intrinsic_not (gfc_se * se, gfc_expr * expr)
    6657              : {
    6658          230 :   tree arg;
    6659              : 
    6660          230 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    6661          230 :   se->expr = fold_build1_loc (input_location, BIT_NOT_EXPR,
    6662          230 :                               TREE_TYPE (arg), arg);
    6663          230 : }
    6664              : 
    6665              : 
    6666              : /* Generate code for OUT_OF_RANGE.  */
    6667              : static void
    6668          468 : gfc_conv_intrinsic_out_of_range (gfc_se * se, gfc_expr * expr)
    6669              : {
    6670          468 :   tree *args;
    6671          468 :   tree type;
    6672          468 :   tree tmp = NULL_TREE, tmp1, tmp2;
    6673          468 :   unsigned int num_args;
    6674          468 :   int k;
    6675          468 :   gfc_se rnd_se;
    6676          468 :   gfc_actual_arglist *arg = expr->value.function.actual;
    6677          468 :   gfc_expr *x = arg->expr;
    6678          468 :   gfc_expr *mold = arg->next->expr;
    6679              : 
    6680          468 :   num_args = gfc_intrinsic_argument_list_length (expr);
    6681          468 :   args = XALLOCAVEC (tree, num_args);
    6682              : 
    6683          468 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    6684              : 
    6685          468 :   gfc_init_se (&rnd_se, NULL);
    6686              : 
    6687          468 :   if (num_args == 3)
    6688              :     {
    6689              :       /* The ROUND argument is optional and shall appear only if X is
    6690              :          of type real and MOLD is of type integer (see edit F23/004).  */
    6691          270 :       gfc_expr *round = arg->next->next->expr;
    6692          270 :       gfc_conv_expr (&rnd_se, round);
    6693              : 
    6694          270 :       if (round->expr_type == EXPR_VARIABLE
    6695          198 :           && round->symtree->n.sym->attr.dummy
    6696           30 :           && round->symtree->n.sym->attr.optional)
    6697              :         {
    6698           30 :           tree present = gfc_conv_expr_present (round->symtree->n.sym);
    6699           30 :           rnd_se.expr = build3_loc (input_location, COND_EXPR,
    6700              :                                     logical_type_node, present,
    6701              :                                     rnd_se.expr, logical_false_node);
    6702           30 :           gfc_add_block_to_block (&se->pre, &rnd_se.pre);
    6703              :         }
    6704              :     }
    6705              :   else
    6706              :     {
    6707              :       /* If ROUND is absent, it is equivalent to having the value false.  */
    6708          198 :       rnd_se.expr = logical_false_node;
    6709              :     }
    6710              : 
    6711          468 :   type = TREE_TYPE (args[0]);
    6712          468 :   k = gfc_validate_kind (mold->ts.type, mold->ts.kind, false);
    6713              : 
    6714          468 :   switch (x->ts.type)
    6715              :     {
    6716          378 :     case BT_REAL:
    6717              :       /* X may be IEEE infinity or NaN, but the representation of MOLD may not
    6718              :          support infinity or NaN.  */
    6719          378 :       tree finite;
    6720          378 :       finite = build_call_expr_loc (input_location,
    6721              :                                     builtin_decl_explicit (BUILT_IN_ISFINITE),
    6722              :                                     1,  args[0]);
    6723          378 :       finite = convert (logical_type_node, finite);
    6724              : 
    6725          378 :       if (mold->ts.type == BT_REAL)
    6726              :         {
    6727           24 :           tmp1 = build1 (ABS_EXPR, type, args[0]);
    6728           24 :           tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
    6729              :                                         mold->ts.kind, 0);
    6730           24 :           tmp = build2 (GT_EXPR, logical_type_node, tmp1,
    6731              :                         convert (type, tmp2));
    6732              : 
    6733              :           /* Check if MOLD representation supports infinity or NaN.  */
    6734           24 :           bool infnan = (HONOR_INFINITIES (TREE_TYPE (args[1]))
    6735           24 :                          || HONOR_NANS (TREE_TYPE (args[1])));
    6736           24 :           tmp = build3 (COND_EXPR, logical_type_node, finite, tmp,
    6737              :                         infnan ? logical_false_node : logical_true_node);
    6738              :         }
    6739              :       else
    6740              :         {
    6741          354 :           tree rounded;
    6742          354 :           tree decl;
    6743              : 
    6744          354 :           decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, x->ts.kind);
    6745          354 :           gcc_assert (decl != NULL_TREE);
    6746              : 
    6747              :           /* Round or truncate argument X, depending on the optional argument
    6748              :              ROUND (default: .false.).  */
    6749          354 :           tmp1 = build_round_expr (args[0], type);
    6750          354 :           tmp2 = build_call_expr_loc (input_location, decl, 1, args[0]);
    6751          354 :           rounded = build3 (COND_EXPR, type, rnd_se.expr, tmp1, tmp2);
    6752              : 
    6753          354 :           if (mold->ts.type == BT_INTEGER)
    6754              :             {
    6755          180 :               tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
    6756              :                                            x->ts.kind);
    6757          180 :               tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
    6758              :                                            x->ts.kind);
    6759              :             }
    6760          174 :           else if (mold->ts.type == BT_UNSIGNED)
    6761              :             {
    6762          174 :               tmp1 = build_real_from_int_cst (type, integer_zero_node);
    6763          174 :               tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
    6764              :                                            x->ts.kind);
    6765              :             }
    6766              :           else
    6767            0 :             gcc_unreachable ();
    6768              : 
    6769          354 :           tmp1 = build2 (LT_EXPR, logical_type_node, rounded,
    6770              :                          convert (type, tmp1));
    6771          354 :           tmp2 = build2 (GT_EXPR, logical_type_node, rounded,
    6772              :                          convert (type, tmp2));
    6773          354 :           tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
    6774          354 :           tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node,
    6775              :                         build1 (TRUTH_NOT_EXPR, logical_type_node, finite),
    6776              :                         tmp);
    6777              :         }
    6778              :       break;
    6779              : 
    6780           48 :     case BT_INTEGER:
    6781           48 :       if (mold->ts.type == BT_INTEGER)
    6782              :         {
    6783           12 :           tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
    6784              :                                        x->ts.kind);
    6785           12 :           tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
    6786              :                                        x->ts.kind);
    6787           12 :           tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
    6788              :                          convert (type, tmp1));
    6789           12 :           tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
    6790              :                          convert (type, tmp2));
    6791           12 :           tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
    6792              :         }
    6793           36 :       else if (mold->ts.type == BT_UNSIGNED)
    6794              :         {
    6795           36 :           int i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
    6796           36 :           tmp = build_int_cst (type, 0);
    6797           36 :           tmp = build2 (LT_EXPR, logical_type_node, args[0], tmp);
    6798           36 :           if (mpz_cmp (gfc_integer_kinds[i].huge,
    6799           36 :                        gfc_unsigned_kinds[k].huge) > 0)
    6800              :             {
    6801            0 :               tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
    6802              :                                            x->ts.kind);
    6803            0 :               tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
    6804              :                              convert (type, tmp2));
    6805            0 :               tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp, tmp2);
    6806              :             }
    6807              :         }
    6808            0 :       else if (mold->ts.type == BT_REAL)
    6809              :         {
    6810            0 :           tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
    6811              :                                         mold->ts.kind, 0);
    6812            0 :           tmp1 = build1 (NEGATE_EXPR, TREE_TYPE (tmp2), tmp2);
    6813            0 :           tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
    6814              :                          convert (type, tmp1));
    6815            0 :           tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
    6816              :                          convert (type, tmp2));
    6817            0 :           tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
    6818              :         }
    6819              :       else
    6820            0 :         gcc_unreachable ();
    6821              :       break;
    6822              : 
    6823           42 :     case BT_UNSIGNED:
    6824           42 :       if (mold->ts.type == BT_UNSIGNED)
    6825              :         {
    6826           12 :           tmp = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
    6827              :                                       x->ts.kind);
    6828           12 :           tmp = build2 (GT_EXPR, logical_type_node, args[0],
    6829              :                         convert (type, tmp));
    6830              :         }
    6831           30 :       else if (mold->ts.type == BT_INTEGER)
    6832              :         {
    6833           18 :           tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
    6834              :                                       x->ts.kind);
    6835           18 :           tmp = build2 (GT_EXPR, logical_type_node, args[0],
    6836              :                         convert (type, tmp));
    6837              :         }
    6838           12 :       else if (mold->ts.type == BT_REAL)
    6839              :         {
    6840           12 :           tmp = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
    6841              :                                        mold->ts.kind, 0);
    6842           12 :           tmp = build2 (GT_EXPR, logical_type_node, args[0],
    6843              :                         convert (type, tmp));
    6844              :         }
    6845              :       else
    6846            0 :         gcc_unreachable ();
    6847              :       break;
    6848              : 
    6849            0 :     default:
    6850            0 :       gcc_unreachable ();
    6851              :     }
    6852              : 
    6853          468 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
    6854          468 : }
    6855              : 
    6856              : 
    6857              : /* Set or clear a single bit.  */
    6858              : static void
    6859          306 : gfc_conv_intrinsic_singlebitop (gfc_se * se, gfc_expr * expr, int set)
    6860              : {
    6861          306 :   tree args[2];
    6862          306 :   tree type;
    6863          306 :   tree tmp;
    6864          306 :   enum tree_code op;
    6865              : 
    6866          306 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    6867          306 :   type = TREE_TYPE (args[0]);
    6868              : 
    6869              :   /* Optionally generate code for runtime argument check.  */
    6870          306 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    6871              :     {
    6872           12 :       tree below = fold_build2_loc (input_location, LT_EXPR,
    6873              :                                     logical_type_node, args[1],
    6874           12 :                                     build_int_cst (TREE_TYPE (args[1]), 0));
    6875           12 :       tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
    6876           12 :       tree above = fold_build2_loc (input_location, GE_EXPR,
    6877              :                                     logical_type_node, args[1], nbits);
    6878           12 :       tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    6879              :                                     logical_type_node, below, above);
    6880           12 :       size_t len_name = strlen (expr->value.function.isym->name);
    6881           12 :       char *name = XALLOCAVEC (char, len_name + 1);
    6882           72 :       for (size_t i = 0; i < len_name; i++)
    6883           60 :         name[i] = TOUPPER (expr->value.function.isym->name[i]);
    6884           12 :       name[len_name] = '\0';
    6885           12 :       tree iname = gfc_build_addr_expr (pchar_type_node,
    6886              :                                         gfc_build_cstring_const (name));
    6887           12 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    6888              :                                "POS argument (%ld) out of range 0:%ld "
    6889              :                                "in intrinsic %s",
    6890              :                                fold_convert (long_integer_type_node, args[1]),
    6891              :                                fold_convert (long_integer_type_node, nbits),
    6892              :                                iname);
    6893              :     }
    6894              : 
    6895          306 :   tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    6896              :                          build_int_cst (type, 1), args[1]);
    6897          306 :   if (set)
    6898              :     op = BIT_IOR_EXPR;
    6899              :   else
    6900              :     {
    6901          168 :       op = BIT_AND_EXPR;
    6902          168 :       tmp = fold_build1_loc (input_location, BIT_NOT_EXPR, type, tmp);
    6903              :     }
    6904          306 :   se->expr = fold_build2_loc (input_location, op, type, args[0], tmp);
    6905          306 : }
    6906              : 
    6907              : /* Extract a sequence of bits.
    6908              :     IBITS(I, POS, LEN) = (I >> POS) & ~((~0) << LEN).  */
    6909              : static void
    6910           27 : gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr)
    6911              : {
    6912           27 :   tree args[3];
    6913           27 :   tree type;
    6914           27 :   tree tmp;
    6915           27 :   tree mask;
    6916           27 :   tree num_bits, cond;
    6917              : 
    6918           27 :   gfc_conv_intrinsic_function_args (se, expr, args, 3);
    6919           27 :   type = TREE_TYPE (args[0]);
    6920              : 
    6921              :   /* Optionally generate code for runtime argument check.  */
    6922           27 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    6923              :     {
    6924           12 :       tree tmp1 = fold_convert (long_integer_type_node, args[1]);
    6925           12 :       tree tmp2 = fold_convert (long_integer_type_node, args[2]);
    6926           12 :       tree nbits = build_int_cst (long_integer_type_node,
    6927           12 :                                   TYPE_PRECISION (type));
    6928           12 :       tree below = fold_build2_loc (input_location, LT_EXPR,
    6929              :                                     logical_type_node, args[1],
    6930           12 :                                     build_int_cst (TREE_TYPE (args[1]), 0));
    6931           12 :       tree above = fold_build2_loc (input_location, GT_EXPR,
    6932              :                                     logical_type_node, tmp1, nbits);
    6933           12 :       tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    6934              :                                     logical_type_node, below, above);
    6935           12 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    6936              :                                "POS argument (%ld) out of range 0:%ld "
    6937              :                                "in intrinsic IBITS", tmp1, nbits);
    6938           12 :       below = fold_build2_loc (input_location, LT_EXPR,
    6939              :                                logical_type_node, args[2],
    6940           12 :                                build_int_cst (TREE_TYPE (args[2]), 0));
    6941           12 :       above = fold_build2_loc (input_location, GT_EXPR,
    6942              :                                logical_type_node, tmp2, nbits);
    6943           12 :       scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    6944              :                                logical_type_node, below, above);
    6945           12 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    6946              :                                "LEN argument (%ld) out of range 0:%ld "
    6947              :                                "in intrinsic IBITS", tmp2, nbits);
    6948           12 :       above = fold_build2_loc (input_location, PLUS_EXPR,
    6949              :                                long_integer_type_node, tmp1, tmp2);
    6950           12 :       scond = fold_build2_loc (input_location, GT_EXPR,
    6951              :                                logical_type_node, above, nbits);
    6952           12 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    6953              :                                "POS(%ld)+LEN(%ld)>BIT_SIZE(%ld) "
    6954              :                                "in intrinsic IBITS", tmp1, tmp2, nbits);
    6955              :     }
    6956              : 
    6957              :   /* The Fortran standard allows (shift width) LEN <= BIT_SIZE(I), whereas
    6958              :      gcc requires a shift width < BIT_SIZE(I), so we have to catch this
    6959              :      special case.  See also gfc_conv_intrinsic_ishft ().  */
    6960           27 :   num_bits = build_int_cst (TREE_TYPE (args[2]), TYPE_PRECISION (type));
    6961              : 
    6962           27 :   mask = build_int_cst (type, -1);
    6963           27 :   mask = fold_build2_loc (input_location, LSHIFT_EXPR, type, mask, args[2]);
    6964           27 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[2],
    6965              :                           num_bits);
    6966           27 :   mask = fold_build3_loc (input_location, COND_EXPR, type, cond,
    6967              :                           build_int_cst (type, 0), mask);
    6968           27 :   mask = fold_build1_loc (input_location, BIT_NOT_EXPR, type, mask);
    6969              : 
    6970           27 :   tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, args[0], args[1]);
    6971              : 
    6972           27 :   se->expr = fold_build2_loc (input_location, BIT_AND_EXPR, type, tmp, mask);
    6973           27 : }
    6974              : 
    6975              : static void
    6976          492 : gfc_conv_intrinsic_shift (gfc_se * se, gfc_expr * expr, bool right_shift,
    6977              :                           bool arithmetic)
    6978              : {
    6979          492 :   tree args[2], type, num_bits, cond;
    6980          492 :   tree bigshift;
    6981          492 :   bool do_convert = false;
    6982              : 
    6983          492 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    6984              : 
    6985          492 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    6986          492 :   args[1] = gfc_evaluate_now (args[1], &se->pre);
    6987          492 :   type = TREE_TYPE (args[0]);
    6988              : 
    6989          492 :   if (!arithmetic)
    6990              :     {
    6991          390 :       args[0] = fold_convert (unsigned_type_for (type), args[0]);
    6992          390 :       do_convert = true;
    6993              :     }
    6994              :   else
    6995          102 :     gcc_assert (right_shift);
    6996              : 
    6997          492 :   if (flag_unsigned && arithmetic && expr->ts.type == BT_UNSIGNED)
    6998              :     {
    6999           30 :       do_convert = true;
    7000           30 :       args[0] = fold_convert (signed_type_for (type), args[0]);
    7001              :     }
    7002              : 
    7003          816 :   se->expr = fold_build2_loc (input_location,
    7004              :                               right_shift ? RSHIFT_EXPR : LSHIFT_EXPR,
    7005          492 :                               TREE_TYPE (args[0]), args[0], args[1]);
    7006              : 
    7007          492 :   if (do_convert)
    7008          420 :     se->expr = fold_convert (type, se->expr);
    7009              : 
    7010          492 :   if (!arithmetic)
    7011          390 :     bigshift = build_int_cst (type, 0);
    7012              :   else
    7013              :     {
    7014          102 :       tree nonneg = fold_build2_loc (input_location, GE_EXPR,
    7015              :                                      logical_type_node, args[0],
    7016          102 :                                      build_int_cst (TREE_TYPE (args[0]), 0));
    7017          102 :       bigshift = fold_build3_loc (input_location, COND_EXPR, type, nonneg,
    7018              :                                   build_int_cst (type, 0),
    7019              :                                   build_int_cst (type, -1));
    7020              :     }
    7021              : 
    7022              :   /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
    7023              :      gcc requires a shift width < BIT_SIZE(I), so we have to catch this
    7024              :      special case.  */
    7025          492 :   num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
    7026              : 
    7027              :   /* Optionally generate code for runtime argument check.  */
    7028          492 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    7029              :     {
    7030           30 :       tree below = fold_build2_loc (input_location, LT_EXPR,
    7031              :                                     logical_type_node, args[1],
    7032           30 :                                     build_int_cst (TREE_TYPE (args[1]), 0));
    7033           30 :       tree above = fold_build2_loc (input_location, GT_EXPR,
    7034              :                                     logical_type_node, args[1], num_bits);
    7035           30 :       tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    7036              :                                     logical_type_node, below, above);
    7037           30 :       size_t len_name = strlen (expr->value.function.isym->name);
    7038           30 :       char *name = XALLOCAVEC (char, len_name + 1);
    7039          210 :       for (size_t i = 0; i < len_name; i++)
    7040          180 :         name[i] = TOUPPER (expr->value.function.isym->name[i]);
    7041           30 :       name[len_name] = '\0';
    7042           30 :       tree iname = gfc_build_addr_expr (pchar_type_node,
    7043              :                                         gfc_build_cstring_const (name));
    7044           30 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    7045              :                                "SHIFT argument (%ld) out of range 0:%ld "
    7046              :                                "in intrinsic %s",
    7047              :                                fold_convert (long_integer_type_node, args[1]),
    7048              :                                fold_convert (long_integer_type_node, num_bits),
    7049              :                                iname);
    7050              :     }
    7051              : 
    7052          492 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    7053              :                           args[1], num_bits);
    7054              : 
    7055          492 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
    7056              :                               bigshift, se->expr);
    7057          492 : }
    7058              : 
    7059              : /* ISHFT (I, SHIFT) = (abs (shift) >= BIT_SIZE (i))
    7060              :                         ? 0
    7061              :                         : ((shift >= 0) ? i << shift : i >> -shift)
    7062              :    where all shifts are logical shifts.  */
    7063              : static void
    7064          318 : gfc_conv_intrinsic_ishft (gfc_se * se, gfc_expr * expr)
    7065              : {
    7066          318 :   tree args[2];
    7067          318 :   tree type;
    7068          318 :   tree utype;
    7069          318 :   tree tmp;
    7070          318 :   tree width;
    7071          318 :   tree num_bits;
    7072          318 :   tree cond;
    7073          318 :   tree lshift;
    7074          318 :   tree rshift;
    7075              : 
    7076          318 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    7077              : 
    7078          318 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    7079          318 :   args[1] = gfc_evaluate_now (args[1], &se->pre);
    7080              : 
    7081          318 :   type = TREE_TYPE (args[0]);
    7082          318 :   utype = unsigned_type_for (type);
    7083              : 
    7084          318 :   width = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (args[1]),
    7085              :                            args[1]);
    7086              : 
    7087              :   /* Left shift if positive.  */
    7088          318 :   lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type, args[0], width);
    7089              : 
    7090              :   /* Right shift if negative.
    7091              :      We convert to an unsigned type because we want a logical shift.
    7092              :      The standard doesn't define the case of shifting negative
    7093              :      numbers, and we try to be compatible with other compilers, most
    7094              :      notably g77, here.  */
    7095          318 :   rshift = fold_convert (type, fold_build2_loc (input_location, RSHIFT_EXPR,
    7096              :                                     utype, convert (utype, args[0]), width));
    7097              : 
    7098          318 :   tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[1],
    7099          318 :                          build_int_cst (TREE_TYPE (args[1]), 0));
    7100          318 :   tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp, lshift, rshift);
    7101              : 
    7102              :   /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
    7103              :      gcc requires a shift width < BIT_SIZE(I), so we have to catch this
    7104              :      special case.  */
    7105          318 :   num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
    7106              : 
    7107              :   /* Optionally generate code for runtime argument check.  */
    7108          318 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    7109              :     {
    7110           24 :       tree outside = fold_build2_loc (input_location, GT_EXPR,
    7111              :                                     logical_type_node, width, num_bits);
    7112           24 :       gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
    7113              :                                "SHIFT argument (%ld) out of range -%ld:%ld "
    7114              :                                "in intrinsic ISHFT",
    7115              :                                fold_convert (long_integer_type_node, args[1]),
    7116              :                                fold_convert (long_integer_type_node, num_bits),
    7117              :                                fold_convert (long_integer_type_node, num_bits));
    7118              :     }
    7119              : 
    7120          318 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, width,
    7121              :                           num_bits);
    7122          318 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
    7123              :                               build_int_cst (type, 0), tmp);
    7124          318 : }
    7125              : 
    7126              : 
    7127              : /* Circular shift.  AKA rotate or barrel shift.  */
    7128              : 
    7129              : static void
    7130          658 : gfc_conv_intrinsic_ishftc (gfc_se * se, gfc_expr * expr)
    7131              : {
    7132          658 :   tree *args;
    7133          658 :   tree type;
    7134          658 :   tree tmp;
    7135          658 :   tree lrot;
    7136          658 :   tree rrot;
    7137          658 :   tree zero;
    7138          658 :   tree nbits;
    7139          658 :   unsigned int num_args;
    7140              : 
    7141          658 :   num_args = gfc_intrinsic_argument_list_length (expr);
    7142          658 :   args = XALLOCAVEC (tree, num_args);
    7143              : 
    7144          658 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    7145              : 
    7146          658 :   type = TREE_TYPE (args[0]);
    7147          658 :   nbits = build_int_cst (long_integer_type_node, TYPE_PRECISION (type));
    7148              : 
    7149          658 :   if (num_args == 3)
    7150              :     {
    7151          550 :       gfc_expr *size = expr->value.function.actual->next->next->expr;
    7152              : 
    7153              :       /* Use a library function for the 3 parameter version.  */
    7154          550 :       tree int4type = gfc_get_int_type (4);
    7155              : 
    7156              :       /* Treat optional SIZE argument when it is passed as an optional
    7157              :          dummy.  If SIZE is absent, the default value is BIT_SIZE(I).  */
    7158          550 :       if (size->expr_type == EXPR_VARIABLE
    7159          438 :           && size->symtree->n.sym->attr.dummy
    7160           36 :           && size->symtree->n.sym->attr.optional)
    7161              :         {
    7162           36 :           tree type_of_size = TREE_TYPE (args[2]);
    7163           72 :           args[2] = build3_loc (input_location, COND_EXPR, type_of_size,
    7164           36 :                                 gfc_conv_expr_present (size->symtree->n.sym),
    7165              :                                 args[2], fold_convert (type_of_size, nbits));
    7166              :         }
    7167              : 
    7168              :       /* We convert the first argument to at least 4 bytes, and
    7169              :          convert back afterwards.  This removes the need for library
    7170              :          functions for all argument sizes, and function will be
    7171              :          aligned to at least 32 bits, so there's no loss.  */
    7172          550 :       if (expr->ts.kind < 4)
    7173          242 :         args[0] = convert (int4type, args[0]);
    7174              : 
    7175              :       /* Convert the SHIFT and SIZE args to INTEGER*4 otherwise we would
    7176              :          need loads of library  functions.  They cannot have values >
    7177              :          BIT_SIZE (I) so the conversion is safe.  */
    7178          550 :       args[1] = convert (int4type, args[1]);
    7179          550 :       args[2] = convert (int4type, args[2]);
    7180              : 
    7181              :       /* Optionally generate code for runtime argument check.  */
    7182          550 :       if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    7183              :         {
    7184           18 :           tree size = fold_convert (long_integer_type_node, args[2]);
    7185           18 :           tree below = fold_build2_loc (input_location, LE_EXPR,
    7186              :                                         logical_type_node, size,
    7187           18 :                                         build_int_cst (TREE_TYPE (args[1]), 0));
    7188           18 :           tree above = fold_build2_loc (input_location, GT_EXPR,
    7189              :                                         logical_type_node, size, nbits);
    7190           18 :           tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    7191              :                                         logical_type_node, below, above);
    7192           18 :           gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    7193              :                                    "SIZE argument (%ld) out of range 1:%ld "
    7194              :                                    "in intrinsic ISHFTC", size, nbits);
    7195           18 :           tree width = fold_convert (long_integer_type_node, args[1]);
    7196           18 :           width = fold_build1_loc (input_location, ABS_EXPR,
    7197              :                                    long_integer_type_node, width);
    7198           18 :           scond = fold_build2_loc (input_location, GT_EXPR,
    7199              :                                    logical_type_node, width, size);
    7200           18 :           gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    7201              :                                    "SHIFT argument (%ld) out of range -%ld:%ld "
    7202              :                                    "in intrinsic ISHFTC",
    7203              :                                    fold_convert (long_integer_type_node, args[1]),
    7204              :                                    size, size);
    7205              :         }
    7206              : 
    7207          550 :       switch (expr->ts.kind)
    7208              :         {
    7209          426 :         case 1:
    7210          426 :         case 2:
    7211          426 :         case 4:
    7212          426 :           tmp = gfor_fndecl_math_ishftc4;
    7213          426 :           break;
    7214          124 :         case 8:
    7215          124 :           tmp = gfor_fndecl_math_ishftc8;
    7216          124 :           break;
    7217            0 :         case 16:
    7218            0 :           tmp = gfor_fndecl_math_ishftc16;
    7219            0 :           break;
    7220            0 :         default:
    7221            0 :           gcc_unreachable ();
    7222              :         }
    7223          550 :       se->expr = build_call_expr_loc (input_location,
    7224              :                                       tmp, 3, args[0], args[1], args[2]);
    7225              :       /* Convert the result back to the original type, if we extended
    7226              :          the first argument's width above.  */
    7227          550 :       if (expr->ts.kind < 4)
    7228          242 :         se->expr = convert (type, se->expr);
    7229              : 
    7230          550 :       return;
    7231              :     }
    7232              : 
    7233              :   /* Evaluate arguments only once.  */
    7234          108 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    7235          108 :   args[1] = gfc_evaluate_now (args[1], &se->pre);
    7236              : 
    7237              :   /* Optionally generate code for runtime argument check.  */
    7238          108 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    7239              :     {
    7240           12 :       tree width = fold_convert (long_integer_type_node, args[1]);
    7241           12 :       width = fold_build1_loc (input_location, ABS_EXPR,
    7242              :                                long_integer_type_node, width);
    7243           12 :       tree outside = fold_build2_loc (input_location, GT_EXPR,
    7244              :                                       logical_type_node, width, nbits);
    7245           12 :       gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
    7246              :                                "SHIFT argument (%ld) out of range -%ld:%ld "
    7247              :                                "in intrinsic ISHFTC",
    7248              :                                fold_convert (long_integer_type_node, args[1]),
    7249              :                                nbits, nbits);
    7250              :     }
    7251              : 
    7252              :   /* Rotate left if positive.  */
    7253          108 :   lrot = fold_build2_loc (input_location, LROTATE_EXPR, type, args[0], args[1]);
    7254              : 
    7255              :   /* Rotate right if negative.  */
    7256          108 :   tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (args[1]),
    7257              :                          args[1]);
    7258          108 :   rrot = fold_build2_loc (input_location,RROTATE_EXPR, type, args[0], tmp);
    7259              : 
    7260          108 :   zero = build_int_cst (TREE_TYPE (args[1]), 0);
    7261          108 :   tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, args[1],
    7262              :                          zero);
    7263          108 :   rrot = fold_build3_loc (input_location, COND_EXPR, type, tmp, lrot, rrot);
    7264              : 
    7265              :   /* Do nothing if shift == 0.  */
    7266          108 :   tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, args[1],
    7267              :                          zero);
    7268          108 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, args[0],
    7269              :                               rrot);
    7270              : }
    7271              : 
    7272              : 
    7273              : /* LEADZ (i) = (i == 0) ? BIT_SIZE (i)
    7274              :                         : __builtin_clz(i) - (BIT_SIZE('int') - BIT_SIZE(i))
    7275              : 
    7276              :    The conditional expression is necessary because the result of LEADZ(0)
    7277              :    is defined, but the result of __builtin_clz(0) is undefined for most
    7278              :    targets.
    7279              : 
    7280              :    For INTEGER kinds smaller than the C 'int' type, we have to subtract the
    7281              :    difference in bit size between the argument of LEADZ and the C int.  */
    7282              : 
    7283              : static void
    7284          270 : gfc_conv_intrinsic_leadz (gfc_se * se, gfc_expr * expr)
    7285              : {
    7286          270 :   tree arg;
    7287          270 :   tree arg_type;
    7288          270 :   tree cond;
    7289          270 :   tree result_type;
    7290          270 :   tree leadz;
    7291          270 :   tree bit_size;
    7292          270 :   tree tmp;
    7293          270 :   tree func;
    7294          270 :   int s, argsize;
    7295              : 
    7296          270 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7297          270 :   argsize = TYPE_PRECISION (TREE_TYPE (arg));
    7298              : 
    7299              :   /* Which variant of __builtin_clz* should we call?  */
    7300          270 :   if (argsize <= INT_TYPE_SIZE)
    7301              :     {
    7302          183 :       arg_type = unsigned_type_node;
    7303          183 :       func = builtin_decl_explicit (BUILT_IN_CLZ);
    7304              :     }
    7305           87 :   else if (argsize <= LONG_TYPE_SIZE)
    7306              :     {
    7307           57 :       arg_type = long_unsigned_type_node;
    7308           57 :       func = builtin_decl_explicit (BUILT_IN_CLZL);
    7309              :     }
    7310           30 :   else if (argsize <= LONG_LONG_TYPE_SIZE)
    7311              :     {
    7312            0 :       arg_type = long_long_unsigned_type_node;
    7313            0 :       func = builtin_decl_explicit (BUILT_IN_CLZLL);
    7314              :     }
    7315              :   else
    7316              :     {
    7317           30 :       gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
    7318           30 :       arg_type = gfc_build_uint_type (argsize);
    7319           30 :       func = NULL_TREE;
    7320              :     }
    7321              : 
    7322              :   /* Convert the actual argument twice: first, to the unsigned type of the
    7323              :      same size; then, to the proper argument type for the built-in
    7324              :      function.  But the return type is of the default INTEGER kind.  */
    7325          270 :   arg = fold_convert (gfc_build_uint_type (argsize), arg);
    7326          270 :   arg = fold_convert (arg_type, arg);
    7327          270 :   arg = gfc_evaluate_now (arg, &se->pre);
    7328          270 :   result_type = gfc_get_int_type (gfc_default_integer_kind);
    7329              : 
    7330              :   /* Compute LEADZ for the case i .ne. 0.  */
    7331          270 :   if (func)
    7332              :     {
    7333          240 :       s = TYPE_PRECISION (arg_type) - argsize;
    7334          240 :       tmp = fold_convert (result_type,
    7335              :                           build_call_expr_loc (input_location, func,
    7336              :                                                1, arg));
    7337          240 :       leadz = fold_build2_loc (input_location, MINUS_EXPR, result_type,
    7338          240 :                                tmp, build_int_cst (result_type, s));
    7339              :     }
    7340              :   else
    7341              :     {
    7342              :       /* We end up here if the argument type is larger than 'long long'.
    7343              :          We generate this code:
    7344              : 
    7345              :             if (x & (ULL_MAX << ULL_SIZE) != 0)
    7346              :               return clzll ((unsigned long long) (x >> ULLSIZE));
    7347              :             else
    7348              :               return ULL_SIZE + clzll ((unsigned long long) x);
    7349              :          where ULL_MAX is the largest value that a ULL_MAX can hold
    7350              :          (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
    7351              :          is the bit-size of the long long type (64 in this example).  */
    7352           30 :       tree ullsize, ullmax, tmp1, tmp2, btmp;
    7353              : 
    7354           30 :       ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
    7355           30 :       ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
    7356              :                                 long_long_unsigned_type_node,
    7357              :                                 build_int_cst (long_long_unsigned_type_node,
    7358              :                                                0));
    7359              : 
    7360           30 :       cond = fold_build2_loc (input_location, LSHIFT_EXPR, arg_type,
    7361              :                               fold_convert (arg_type, ullmax), ullsize);
    7362           30 :       cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type,
    7363              :                               arg, cond);
    7364           30 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    7365              :                               cond, build_int_cst (arg_type, 0));
    7366              : 
    7367           30 :       tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
    7368              :                               arg, ullsize);
    7369           30 :       tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
    7370           30 :       btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
    7371           30 :       tmp1 = fold_convert (result_type,
    7372              :                            build_call_expr_loc (input_location, btmp, 1, tmp1));
    7373              : 
    7374           30 :       tmp2 = fold_convert (long_long_unsigned_type_node, arg);
    7375           30 :       btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
    7376           30 :       tmp2 = fold_convert (result_type,
    7377              :                            build_call_expr_loc (input_location, btmp, 1, tmp2));
    7378           30 :       tmp2 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
    7379              :                               tmp2, ullsize);
    7380              : 
    7381           30 :       leadz = fold_build3_loc (input_location, COND_EXPR, result_type,
    7382              :                                cond, tmp1, tmp2);
    7383              :     }
    7384              : 
    7385              :   /* Build BIT_SIZE.  */
    7386          270 :   bit_size = build_int_cst (result_type, argsize);
    7387              : 
    7388          270 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7389              :                           arg, build_int_cst (arg_type, 0));
    7390          270 :   se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
    7391              :                               bit_size, leadz);
    7392          270 : }
    7393              : 
    7394              : 
    7395              : /* TRAILZ(i) = (i == 0) ? BIT_SIZE (i) : __builtin_ctz(i)
    7396              : 
    7397              :    The conditional expression is necessary because the result of TRAILZ(0)
    7398              :    is defined, but the result of __builtin_ctz(0) is undefined for most
    7399              :    targets.  */
    7400              : 
    7401              : static void
    7402          282 : gfc_conv_intrinsic_trailz (gfc_se * se, gfc_expr *expr)
    7403              : {
    7404          282 :   tree arg;
    7405          282 :   tree arg_type;
    7406          282 :   tree cond;
    7407          282 :   tree result_type;
    7408          282 :   tree trailz;
    7409          282 :   tree bit_size;
    7410          282 :   tree func;
    7411          282 :   int argsize;
    7412              : 
    7413          282 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7414          282 :   argsize = TYPE_PRECISION (TREE_TYPE (arg));
    7415              : 
    7416              :   /* Which variant of __builtin_ctz* should we call?  */
    7417          282 :   if (argsize <= INT_TYPE_SIZE)
    7418              :     {
    7419          195 :       arg_type = unsigned_type_node;
    7420          195 :       func = builtin_decl_explicit (BUILT_IN_CTZ);
    7421              :     }
    7422           87 :   else if (argsize <= LONG_TYPE_SIZE)
    7423              :     {
    7424           57 :       arg_type = long_unsigned_type_node;
    7425           57 :       func = builtin_decl_explicit (BUILT_IN_CTZL);
    7426              :     }
    7427           30 :   else if (argsize <= LONG_LONG_TYPE_SIZE)
    7428              :     {
    7429            0 :       arg_type = long_long_unsigned_type_node;
    7430            0 :       func = builtin_decl_explicit (BUILT_IN_CTZLL);
    7431              :     }
    7432              :   else
    7433              :     {
    7434           30 :       gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
    7435           30 :       arg_type = gfc_build_uint_type (argsize);
    7436           30 :       func = NULL_TREE;
    7437              :     }
    7438              : 
    7439              :   /* Convert the actual argument twice: first, to the unsigned type of the
    7440              :      same size; then, to the proper argument type for the built-in
    7441              :      function.  But the return type is of the default INTEGER kind.  */
    7442          282 :   arg = fold_convert (gfc_build_uint_type (argsize), arg);
    7443          282 :   arg = fold_convert (arg_type, arg);
    7444          282 :   arg = gfc_evaluate_now (arg, &se->pre);
    7445          282 :   result_type = gfc_get_int_type (gfc_default_integer_kind);
    7446              : 
    7447              :   /* Compute TRAILZ for the case i .ne. 0.  */
    7448          282 :   if (func)
    7449          252 :     trailz = fold_convert (result_type, build_call_expr_loc (input_location,
    7450              :                                                              func, 1, arg));
    7451              :   else
    7452              :     {
    7453              :       /* We end up here if the argument type is larger than 'long long'.
    7454              :          We generate this code:
    7455              : 
    7456              :             if ((x & ULL_MAX) == 0)
    7457              :               return ULL_SIZE + ctzll ((unsigned long long) (x >> ULLSIZE));
    7458              :             else
    7459              :               return ctzll ((unsigned long long) x);
    7460              : 
    7461              :          where ULL_MAX is the largest value that a ULL_MAX can hold
    7462              :          (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
    7463              :          is the bit-size of the long long type (64 in this example).  */
    7464           30 :       tree ullsize, ullmax, tmp1, tmp2, btmp;
    7465              : 
    7466           30 :       ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
    7467           30 :       ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
    7468              :                                 long_long_unsigned_type_node,
    7469              :                                 build_int_cst (long_long_unsigned_type_node, 0));
    7470              : 
    7471           30 :       cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type, arg,
    7472              :                               fold_convert (arg_type, ullmax));
    7473           30 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, cond,
    7474              :                               build_int_cst (arg_type, 0));
    7475              : 
    7476           30 :       tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
    7477              :                               arg, ullsize);
    7478           30 :       tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
    7479           30 :       btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
    7480           30 :       tmp1 = fold_convert (result_type,
    7481              :                            build_call_expr_loc (input_location, btmp, 1, tmp1));
    7482           30 :       tmp1 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
    7483              :                               tmp1, ullsize);
    7484              : 
    7485           30 :       tmp2 = fold_convert (long_long_unsigned_type_node, arg);
    7486           30 :       btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
    7487           30 :       tmp2 = fold_convert (result_type,
    7488              :                            build_call_expr_loc (input_location, btmp, 1, tmp2));
    7489              : 
    7490           30 :       trailz = fold_build3_loc (input_location, COND_EXPR, result_type,
    7491              :                                 cond, tmp1, tmp2);
    7492              :     }
    7493              : 
    7494              :   /* Build BIT_SIZE.  */
    7495          282 :   bit_size = build_int_cst (result_type, argsize);
    7496              : 
    7497          282 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7498              :                           arg, build_int_cst (arg_type, 0));
    7499          282 :   se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
    7500              :                               bit_size, trailz);
    7501          282 : }
    7502              : 
    7503              : /* Using __builtin_popcount for POPCNT and __builtin_parity for POPPAR;
    7504              :    for types larger than "long long", we call the long long built-in for
    7505              :    the lower and higher bits and combine the result.  */
    7506              : 
    7507              : static void
    7508          134 : gfc_conv_intrinsic_popcnt_poppar (gfc_se * se, gfc_expr *expr, int parity)
    7509              : {
    7510          134 :   tree arg;
    7511          134 :   tree arg_type;
    7512          134 :   tree result_type;
    7513          134 :   tree func;
    7514          134 :   int argsize;
    7515              : 
    7516          134 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7517          134 :   argsize = TYPE_PRECISION (TREE_TYPE (arg));
    7518          134 :   result_type = gfc_get_int_type (gfc_default_integer_kind);
    7519              : 
    7520              :   /* Which variant of the builtin should we call?  */
    7521          134 :   if (argsize <= INT_TYPE_SIZE)
    7522              :     {
    7523          108 :       arg_type = unsigned_type_node;
    7524          198 :       func = builtin_decl_explicit (parity
    7525              :                                     ? BUILT_IN_PARITY
    7526              :                                     : BUILT_IN_POPCOUNT);
    7527              :     }
    7528           26 :   else if (argsize <= LONG_TYPE_SIZE)
    7529              :     {
    7530           12 :       arg_type = long_unsigned_type_node;
    7531           18 :       func = builtin_decl_explicit (parity
    7532              :                                     ? BUILT_IN_PARITYL
    7533              :                                     : BUILT_IN_POPCOUNTL);
    7534              :     }
    7535           14 :   else if (argsize <= LONG_LONG_TYPE_SIZE)
    7536              :     {
    7537            0 :       arg_type = long_long_unsigned_type_node;
    7538            0 :       func = builtin_decl_explicit (parity
    7539              :                                     ? BUILT_IN_PARITYLL
    7540              :                                     : BUILT_IN_POPCOUNTLL);
    7541              :     }
    7542              :   else
    7543              :     {
    7544              :       /* Our argument type is larger than 'long long', which mean none
    7545              :          of the POPCOUNT builtins covers it.  We thus call the 'long long'
    7546              :          variant multiple times, and add the results.  */
    7547           14 :       tree utype, arg2, call1, call2;
    7548              : 
    7549              :       /* For now, we only cover the case where argsize is twice as large
    7550              :          as 'long long'.  */
    7551           14 :       gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
    7552              : 
    7553           21 :       func = builtin_decl_explicit (parity
    7554              :                                     ? BUILT_IN_PARITYLL
    7555              :                                     : BUILT_IN_POPCOUNTLL);
    7556              : 
    7557              :       /* Convert it to an integer, and store into a variable.  */
    7558           14 :       utype = gfc_build_uint_type (argsize);
    7559           14 :       arg = fold_convert (utype, arg);
    7560           14 :       arg = gfc_evaluate_now (arg, &se->pre);
    7561              : 
    7562              :       /* Call the builtin twice.  */
    7563           14 :       call1 = build_call_expr_loc (input_location, func, 1,
    7564              :                                    fold_convert (long_long_unsigned_type_node,
    7565              :                                                  arg));
    7566              : 
    7567           14 :       arg2 = fold_build2_loc (input_location, RSHIFT_EXPR, utype, arg,
    7568              :                               build_int_cst (utype, LONG_LONG_TYPE_SIZE));
    7569           14 :       call2 = build_call_expr_loc (input_location, func, 1,
    7570              :                                    fold_convert (long_long_unsigned_type_node,
    7571              :                                                  arg2));
    7572              : 
    7573              :       /* Combine the results.  */
    7574           14 :       if (parity)
    7575            7 :         se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR,
    7576              :                                     integer_type_node, call1, call2);
    7577              :       else
    7578            7 :         se->expr = fold_build2_loc (input_location, PLUS_EXPR,
    7579              :                                     integer_type_node, call1, call2);
    7580              : 
    7581           14 :       se->expr = convert (result_type, se->expr);
    7582           14 :       return;
    7583              :     }
    7584              : 
    7585              :   /* Convert the actual argument twice: first, to the unsigned type of the
    7586              :      same size; then, to the proper argument type for the built-in
    7587              :      function.  */
    7588          120 :   arg = fold_convert (gfc_build_uint_type (argsize), arg);
    7589          120 :   arg = fold_convert (arg_type, arg);
    7590              : 
    7591          120 :   se->expr = fold_convert (result_type,
    7592              :                            build_call_expr_loc (input_location, func, 1, arg));
    7593              : }
    7594              : 
    7595              : 
    7596              : /* Process an intrinsic with unspecified argument-types that has an optional
    7597              :    argument (which could be of type character), e.g. EOSHIFT.  For those, we
    7598              :    need to append the string length of the optional argument if it is not
    7599              :    present and the type is really character.
    7600              :    primary specifies the position (starting at 1) of the non-optional argument
    7601              :    specifying the type and optional gives the position of the optional
    7602              :    argument in the arglist.  */
    7603              : 
    7604              : static void
    7605         5831 : conv_generic_with_optional_char_arg (gfc_se* se, gfc_expr* expr,
    7606              :                                      unsigned primary, unsigned optional)
    7607              : {
    7608         5831 :   gfc_actual_arglist* prim_arg;
    7609         5831 :   gfc_actual_arglist* opt_arg;
    7610         5831 :   unsigned cur_pos;
    7611         5831 :   gfc_actual_arglist* arg;
    7612         5831 :   gfc_symbol* sym;
    7613         5831 :   vec<tree, va_gc> *append_args;
    7614              : 
    7615              :   /* Find the two arguments given as position.  */
    7616         5831 :   cur_pos = 0;
    7617         5831 :   prim_arg = NULL;
    7618         5831 :   opt_arg = NULL;
    7619        17493 :   for (arg = expr->value.function.actual; arg; arg = arg->next)
    7620              :     {
    7621        17493 :       ++cur_pos;
    7622              : 
    7623        17493 :       if (cur_pos == primary)
    7624         5831 :         prim_arg = arg;
    7625        17493 :       if (cur_pos == optional)
    7626         5831 :         opt_arg = arg;
    7627              : 
    7628        17493 :       if (cur_pos >= primary && cur_pos >= optional)
    7629              :         break;
    7630              :     }
    7631         5831 :   gcc_assert (prim_arg);
    7632         5831 :   gcc_assert (prim_arg->expr);
    7633         5831 :   gcc_assert (opt_arg);
    7634              : 
    7635              :   /* If we do have type CHARACTER and the optional argument is really absent,
    7636              :      append a dummy 0 as string length.  */
    7637         5831 :   append_args = NULL;
    7638         5831 :   if (prim_arg->expr->ts.type == BT_CHARACTER && !opt_arg->expr)
    7639              :     {
    7640          608 :       tree dummy;
    7641              : 
    7642          608 :       dummy = build_int_cst (gfc_charlen_type_node, 0);
    7643          608 :       vec_alloc (append_args, 1);
    7644          608 :       append_args->quick_push (dummy);
    7645              :     }
    7646              : 
    7647              :   /* Build the call itself.  */
    7648         5831 :   gcc_assert (!se->ignore_optional);
    7649         5831 :   sym = gfc_get_symbol_for_expr (expr, false);
    7650         5831 :   gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
    7651              :                           append_args);
    7652         5831 :   gfc_free_symbol (sym);
    7653         5831 : }
    7654              : 
    7655              : /* The length of a character string.  */
    7656              : static void
    7657         5765 : gfc_conv_intrinsic_len (gfc_se * se, gfc_expr * expr)
    7658              : {
    7659         5765 :   tree len;
    7660         5765 :   tree type;
    7661         5765 :   tree decl;
    7662         5765 :   gfc_symbol *sym;
    7663         5765 :   gfc_se argse;
    7664         5765 :   gfc_expr *arg;
    7665              : 
    7666         5765 :   gcc_assert (!se->ss);
    7667              : 
    7668         5765 :   arg = expr->value.function.actual->expr;
    7669              : 
    7670         5765 :   type = gfc_typenode_for_spec (&expr->ts);
    7671         5765 :   switch (arg->expr_type)
    7672              :     {
    7673            0 :     case EXPR_CONSTANT:
    7674            0 :       len = build_int_cst (gfc_charlen_type_node, arg->value.character.length);
    7675            0 :       break;
    7676              : 
    7677            2 :     case EXPR_ARRAY:
    7678              :       /* If there is an explicit type-spec, use it.  */
    7679            2 :       if (arg->ts.u.cl->length && arg->ts.u.cl->length_from_typespec)
    7680              :         {
    7681            0 :           gfc_conv_string_length (arg->ts.u.cl, arg, &se->pre);
    7682            0 :           len = arg->ts.u.cl->backend_decl;
    7683            0 :           break;
    7684              :         }
    7685              : 
    7686              :       /* Obtain the string length from the function used by
    7687              :          trans-array.cc(gfc_trans_array_constructor).  */
    7688            2 :       len = NULL_TREE;
    7689            2 :       get_array_ctor_strlen (&se->pre, arg->value.constructor, &len);
    7690            2 :       break;
    7691              : 
    7692         5178 :     case EXPR_VARIABLE:
    7693         5178 :       if (arg->ref == NULL
    7694         2385 :             || (arg->ref->next == NULL && arg->ref->type == REF_ARRAY))
    7695              :         {
    7696              :           /* This doesn't catch all cases.
    7697              :              See http://gcc.gnu.org/ml/fortran/2004-06/msg00165.html
    7698              :              and the surrounding thread.  */
    7699         4646 :           sym = arg->symtree->n.sym;
    7700         4646 :           decl = gfc_get_symbol_decl (sym);
    7701         4646 :           if (decl == current_function_decl && sym->attr.function
    7702           55 :                 && (sym->result == sym))
    7703           55 :             decl = gfc_get_fake_result_decl (sym, 0);
    7704              : 
    7705         4646 :           len = sym->ts.u.cl->backend_decl;
    7706         4646 :           gcc_assert (len);
    7707              :           break;
    7708              :         }
    7709              : 
    7710              :       /* Fall through.  */
    7711              : 
    7712         1117 :     default:
    7713         1117 :       gfc_init_se (&argse, se);
    7714         1117 :       if (arg->rank == 0)
    7715          995 :         gfc_conv_expr (&argse, arg);
    7716              :       else
    7717          122 :         gfc_conv_expr_descriptor (&argse, arg);
    7718         1117 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    7719         1117 :       gfc_add_block_to_block (&se->post, &argse.post);
    7720         1117 :       len = argse.string_length;
    7721         1117 :       break;
    7722              :     }
    7723         5765 :   se->expr = convert (type, len);
    7724         5765 : }
    7725              : 
    7726              : /* The length of a character string not including trailing blanks.  */
    7727              : static void
    7728         2335 : gfc_conv_intrinsic_len_trim (gfc_se * se, gfc_expr * expr)
    7729              : {
    7730         2335 :   int kind = expr->value.function.actual->expr->ts.kind;
    7731         2335 :   tree args[2], type, fndecl;
    7732              : 
    7733         2335 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    7734         2335 :   type = gfc_typenode_for_spec (&expr->ts);
    7735              : 
    7736         2335 :   if (kind == 1)
    7737         1933 :     fndecl = gfor_fndecl_string_len_trim;
    7738          402 :   else if (kind == 4)
    7739          402 :     fndecl = gfor_fndecl_string_len_trim_char4;
    7740              :   else
    7741            0 :     gcc_unreachable ();
    7742              : 
    7743         2335 :   se->expr = build_call_expr_loc (input_location,
    7744              :                               fndecl, 2, args[0], args[1]);
    7745         2335 :   se->expr = convert (type, se->expr);
    7746         2335 : }
    7747              : 
    7748              : 
    7749              : /* Returns the starting position of a substring within a string.  */
    7750              : 
    7751              : static void
    7752          751 : gfc_conv_intrinsic_index_scan_verify (gfc_se * se, gfc_expr * expr,
    7753              :                                       tree function)
    7754              : {
    7755          751 :   tree logical4_type_node = gfc_get_logical_type (4);
    7756          751 :   tree type;
    7757          751 :   tree fndecl;
    7758          751 :   tree *args;
    7759          751 :   unsigned int num_args;
    7760              : 
    7761          751 :   args = XALLOCAVEC (tree, 5);
    7762              : 
    7763              :   /* Get number of arguments; characters count double due to the
    7764              :      string length argument. Kind= is not passed to the library
    7765              :      and thus ignored.  */
    7766          751 :   if (expr->value.function.actual->next->next->expr == NULL)
    7767              :     num_args = 4;
    7768              :   else
    7769          304 :     num_args = 5;
    7770              : 
    7771          751 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    7772          751 :   type = gfc_typenode_for_spec (&expr->ts);
    7773              : 
    7774          751 :   if (num_args == 4)
    7775          447 :     args[4] = build_int_cst (logical4_type_node, 0);
    7776              :   else
    7777          304 :     args[4] = convert (logical4_type_node, args[4]);
    7778              : 
    7779          751 :   fndecl = build_addr (function);
    7780          751 :   se->expr = build_call_array_loc (input_location,
    7781          751 :                                TREE_TYPE (TREE_TYPE (function)), fndecl,
    7782              :                                5, args);
    7783          751 :   se->expr = convert (type, se->expr);
    7784              : 
    7785          751 : }
    7786              : 
    7787              : /* The ascii value for a single character.  */
    7788              : static void
    7789         2033 : gfc_conv_intrinsic_ichar (gfc_se * se, gfc_expr * expr)
    7790              : {
    7791         2033 :   tree args[3], type, pchartype;
    7792         2033 :   int nargs;
    7793              : 
    7794         2033 :   nargs = gfc_intrinsic_argument_list_length (expr);
    7795         2033 :   gfc_conv_intrinsic_function_args (se, expr, args, nargs);
    7796         2033 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (args[1])));
    7797         2033 :   pchartype = gfc_get_pchar_type (expr->value.function.actual->expr->ts.kind);
    7798         2033 :   args[1] = fold_build1_loc (input_location, NOP_EXPR, pchartype, args[1]);
    7799         2033 :   type = gfc_typenode_for_spec (&expr->ts);
    7800              : 
    7801         2033 :   se->expr = build_fold_indirect_ref_loc (input_location,
    7802              :                                       args[1]);
    7803         2033 :   se->expr = convert (type, se->expr);
    7804         2033 : }
    7805              : 
    7806              : 
    7807              : /* Intrinsic ISNAN calls __builtin_isnan.  */
    7808              : 
    7809              : static void
    7810          432 : gfc_conv_intrinsic_isnan (gfc_se * se, gfc_expr * expr)
    7811              : {
    7812          432 :   tree arg;
    7813              : 
    7814          432 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7815          432 :   se->expr = build_call_expr_loc (input_location,
    7816              :                                   builtin_decl_explicit (BUILT_IN_ISNAN),
    7817              :                                   1, arg);
    7818          864 :   STRIP_TYPE_NOPS (se->expr);
    7819          432 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    7820          432 : }
    7821              : 
    7822              : 
    7823              : /* Intrinsics IS_IOSTAT_END and IS_IOSTAT_EOR just need to compare
    7824              :    their argument against a constant integer value.  */
    7825              : 
    7826              : static void
    7827           24 : gfc_conv_has_intvalue (gfc_se * se, gfc_expr * expr, const int value)
    7828              : {
    7829           24 :   tree arg;
    7830              : 
    7831           24 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7832           24 :   se->expr = fold_build2_loc (input_location, EQ_EXPR,
    7833              :                               gfc_typenode_for_spec (&expr->ts),
    7834           24 :                               arg, build_int_cst (TREE_TYPE (arg), value));
    7835           24 : }
    7836              : 
    7837              : 
    7838              : 
    7839              : /* MERGE (tsource, fsource, mask) = mask ? tsource : fsource.  */
    7840              : 
    7841              : static void
    7842          949 : gfc_conv_intrinsic_merge (gfc_se * se, gfc_expr * expr)
    7843              : {
    7844          949 :   tree tsource;
    7845          949 :   tree fsource;
    7846          949 :   tree mask;
    7847          949 :   tree type;
    7848          949 :   tree len, len2;
    7849          949 :   tree *args;
    7850          949 :   unsigned int num_args;
    7851              : 
    7852          949 :   num_args = gfc_intrinsic_argument_list_length (expr);
    7853          949 :   args = XALLOCAVEC (tree, num_args);
    7854              : 
    7855          949 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    7856          949 :   if (expr->ts.type != BT_CHARACTER)
    7857              :     {
    7858          422 :       tsource = args[0];
    7859          422 :       fsource = args[1];
    7860          422 :       mask = args[2];
    7861              :     }
    7862              :   else
    7863              :     {
    7864              :       /* We do the same as in the non-character case, but the argument
    7865              :          list is different because of the string length arguments. We
    7866              :          also have to set the string length for the result.  */
    7867          527 :       len = args[0];
    7868          527 :       tsource = args[1];
    7869          527 :       len2 = args[2];
    7870          527 :       fsource = args[3];
    7871          527 :       mask = args[4];
    7872              : 
    7873          527 :       gfc_trans_same_strlen_check ("MERGE intrinsic", &expr->where, len, len2,
    7874              :                                    &se->pre);
    7875          527 :       se->string_length = len;
    7876              :     }
    7877          949 :   tsource = gfc_evaluate_now (tsource, &se->pre);
    7878          949 :   fsource = gfc_evaluate_now (fsource, &se->pre);
    7879          949 :   mask = gfc_evaluate_now (mask, &se->pre);
    7880          949 :   type = TREE_TYPE (tsource);
    7881          949 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, mask, tsource,
    7882              :                               fold_convert (type, fsource));
    7883          949 : }
    7884              : 
    7885              : 
    7886              : /* MERGE_BITS (I, J, MASK) = (I & MASK) | (I & (~MASK)).  */
    7887              : 
    7888              : static void
    7889           42 : gfc_conv_intrinsic_merge_bits (gfc_se * se, gfc_expr * expr)
    7890              : {
    7891           42 :   tree args[3], mask, type;
    7892              : 
    7893           42 :   gfc_conv_intrinsic_function_args (se, expr, args, 3);
    7894           42 :   mask = gfc_evaluate_now (args[2], &se->pre);
    7895              : 
    7896           42 :   type = TREE_TYPE (args[0]);
    7897           42 :   gcc_assert (TREE_TYPE (args[1]) == type);
    7898           42 :   gcc_assert (TREE_TYPE (mask) == type);
    7899              : 
    7900           42 :   args[0] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], mask);
    7901           42 :   args[1] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[1],
    7902              :                              fold_build1_loc (input_location, BIT_NOT_EXPR,
    7903              :                                               type, mask));
    7904           42 :   se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
    7905              :                               args[0], args[1]);
    7906           42 : }
    7907              : 
    7908              : 
    7909              : /* MASKL(n)  =  n == 0 ? 0 : (~0) << (BIT_SIZE - n)
    7910              :    MASKR(n)  =  n == BIT_SIZE ? ~0 : ~((~0) << n)  */
    7911              : 
    7912              : static void
    7913           64 : gfc_conv_intrinsic_mask (gfc_se * se, gfc_expr * expr, int left)
    7914              : {
    7915           64 :   tree arg, allones, type, utype, res, cond, bitsize;
    7916           64 :   int i;
    7917              : 
    7918           64 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7919           64 :   arg = gfc_evaluate_now (arg, &se->pre);
    7920              : 
    7921           64 :   type = gfc_get_int_type (expr->ts.kind);
    7922           64 :   utype = unsigned_type_for (type);
    7923              : 
    7924           64 :   i = gfc_validate_kind (BT_INTEGER, expr->ts.kind, false);
    7925           64 :   bitsize = build_int_cst (TREE_TYPE (arg), gfc_integer_kinds[i].bit_size);
    7926              : 
    7927           64 :   allones = fold_build1_loc (input_location, BIT_NOT_EXPR, utype,
    7928              :                              build_int_cst (utype, 0));
    7929              : 
    7930           64 :   if (left)
    7931              :     {
    7932              :       /* Left-justified mask.  */
    7933           32 :       res = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (arg),
    7934              :                              bitsize, arg);
    7935           32 :       res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
    7936              :                              fold_convert (utype, res));
    7937              : 
    7938              :       /* Special case arg == 0, because SHIFT_EXPR wants a shift strictly
    7939              :          smaller than type width.  */
    7940           32 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
    7941           32 :                               build_int_cst (TREE_TYPE (arg), 0));
    7942           32 :       res = fold_build3_loc (input_location, COND_EXPR, utype, cond,
    7943              :                              build_int_cst (utype, 0), res);
    7944              :     }
    7945              :   else
    7946              :     {
    7947              :       /* Right-justified mask.  */
    7948           32 :       res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
    7949              :                              fold_convert (utype, arg));
    7950           32 :       res = fold_build1_loc (input_location, BIT_NOT_EXPR, utype, res);
    7951              : 
    7952              :       /* Special case agr == bit_size, because SHIFT_EXPR wants a shift
    7953              :          strictly smaller than type width.  */
    7954           32 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7955              :                               arg, bitsize);
    7956           32 :       res = fold_build3_loc (input_location, COND_EXPR, utype,
    7957              :                              cond, allones, res);
    7958              :     }
    7959              : 
    7960           64 :   se->expr = fold_convert (type, res);
    7961           64 : }
    7962              : 
    7963              : 
    7964              : /* FRACTION (s) is translated into:
    7965              :      isfinite (s) ? frexp (s, &dummy_int) : NaN  */
    7966              : static void
    7967           60 : gfc_conv_intrinsic_fraction (gfc_se * se, gfc_expr * expr)
    7968              : {
    7969           60 :   tree arg, type, tmp, res, frexp, cond;
    7970              : 
    7971           60 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
    7972              : 
    7973           60 :   type = gfc_typenode_for_spec (&expr->ts);
    7974           60 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7975           60 :   arg = gfc_evaluate_now (arg, &se->pre);
    7976              : 
    7977           60 :   cond = build_call_expr_loc (input_location,
    7978              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    7979              :                               1, arg);
    7980              : 
    7981           60 :   tmp = gfc_create_var (integer_type_node, NULL);
    7982           60 :   res = build_call_expr_loc (input_location, frexp, 2,
    7983              :                              fold_convert (type, arg),
    7984              :                              gfc_build_addr_expr (NULL_TREE, tmp));
    7985           60 :   res = fold_convert (type, res);
    7986              : 
    7987           60 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type,
    7988              :                               cond, res, gfc_build_nan (type, ""));
    7989           60 : }
    7990              : 
    7991              : 
    7992              : /* NEAREST (s, dir) is translated into
    7993              :      tmp = copysign (HUGE_VAL, dir);
    7994              :      return nextafter (s, tmp);
    7995              :  */
    7996              : static void
    7997         1595 : gfc_conv_intrinsic_nearest (gfc_se * se, gfc_expr * expr)
    7998              : {
    7999         1595 :   tree args[2], type, tmp, nextafter, copysign, huge_val;
    8000              : 
    8001         1595 :   nextafter = gfc_builtin_decl_for_float_kind (BUILT_IN_NEXTAFTER, expr->ts.kind);
    8002         1595 :   copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
    8003              : 
    8004         1595 :   type = gfc_typenode_for_spec (&expr->ts);
    8005         1595 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    8006              : 
    8007         1595 :   huge_val = gfc_build_inf_or_huge (type, expr->ts.kind);
    8008         1595 :   tmp = build_call_expr_loc (input_location, copysign, 2, huge_val,
    8009              :                              fold_convert (type, args[1]));
    8010         1595 :   se->expr = build_call_expr_loc (input_location, nextafter, 2,
    8011              :                                   fold_convert (type, args[0]), tmp);
    8012         1595 :   se->expr = fold_convert (type, se->expr);
    8013         1595 : }
    8014              : 
    8015              : 
    8016              : /* SPACING (s) is translated into
    8017              :     int e;
    8018              :     if (!isfinite (s))
    8019              :       res = NaN;
    8020              :     else if (s == 0)
    8021              :       res = tiny;
    8022              :     else
    8023              :     {
    8024              :       frexp (s, &e);
    8025              :       e = e - prec;
    8026              :       e = MAX_EXPR (e, emin);
    8027              :       res = scalbn (1., e);
    8028              :     }
    8029              :     return res;
    8030              : 
    8031              :  where prec is the precision of s, gfc_real_kinds[k].digits,
    8032              :        emin is min_exponent - 1, gfc_real_kinds[k].min_exponent - 1,
    8033              :    and tiny is tiny(s), gfc_real_kinds[k].tiny.  */
    8034              : 
    8035              : static void
    8036           70 : gfc_conv_intrinsic_spacing (gfc_se * se, gfc_expr * expr)
    8037              : {
    8038           70 :   tree arg, type, prec, emin, tiny, res, e;
    8039           70 :   tree cond, nan, tmp, frexp, scalbn;
    8040           70 :   int k;
    8041           70 :   stmtblock_t block;
    8042              : 
    8043           70 :   k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
    8044           70 :   prec = build_int_cst (integer_type_node, gfc_real_kinds[k].digits);
    8045           70 :   emin = build_int_cst (integer_type_node, gfc_real_kinds[k].min_exponent - 1);
    8046           70 :   tiny = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].tiny, expr->ts.kind, 0);
    8047              : 
    8048           70 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
    8049           70 :   scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
    8050              : 
    8051           70 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    8052           70 :   arg = gfc_evaluate_now (arg, &se->pre);
    8053              : 
    8054           70 :   type = gfc_typenode_for_spec (&expr->ts);
    8055           70 :   e = gfc_create_var (integer_type_node, NULL);
    8056           70 :   res = gfc_create_var (type, NULL);
    8057              : 
    8058              : 
    8059              :   /* Build the block for s /= 0.  */
    8060           70 :   gfc_start_block (&block);
    8061           70 :   tmp = build_call_expr_loc (input_location, frexp, 2, arg,
    8062              :                              gfc_build_addr_expr (NULL_TREE, e));
    8063           70 :   gfc_add_expr_to_block (&block, tmp);
    8064              : 
    8065           70 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node, e,
    8066              :                          prec);
    8067           70 :   gfc_add_modify (&block, e, fold_build2_loc (input_location, MAX_EXPR,
    8068              :                                               integer_type_node, tmp, emin));
    8069              : 
    8070           70 :   tmp = build_call_expr_loc (input_location, scalbn, 2,
    8071           70 :                          build_real_from_int_cst (type, integer_one_node), e);
    8072           70 :   gfc_add_modify (&block, res, tmp);
    8073              : 
    8074              :   /* Finish by building the IF statement for value zero.  */
    8075           70 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
    8076           70 :                           build_real_from_int_cst (type, integer_zero_node));
    8077           70 :   tmp = build3_v (COND_EXPR, cond, build2_v (MODIFY_EXPR, res, tiny),
    8078              :                   gfc_finish_block (&block));
    8079              : 
    8080              :   /* And deal with infinities and NaNs.  */
    8081           70 :   cond = build_call_expr_loc (input_location,
    8082              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    8083              :                               1, arg);
    8084           70 :   nan = gfc_build_nan (type, "");
    8085           70 :   tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, res, nan));
    8086              : 
    8087           70 :   gfc_add_expr_to_block (&se->pre, tmp);
    8088           70 :   se->expr = res;
    8089           70 : }
    8090              : 
    8091              : 
    8092              : /* RRSPACING (s) is translated into
    8093              :       int e;
    8094              :       real x;
    8095              :       x = fabs (s);
    8096              :       if (isfinite (x))
    8097              :       {
    8098              :         if (x != 0)
    8099              :         {
    8100              :           frexp (s, &e);
    8101              :           x = scalbn (x, precision - e);
    8102              :         }
    8103              :       }
    8104              :       else
    8105              :         x = NaN;
    8106              :       return x;
    8107              : 
    8108              :  where precision is gfc_real_kinds[k].digits.  */
    8109              : 
    8110              : static void
    8111           48 : gfc_conv_intrinsic_rrspacing (gfc_se * se, gfc_expr * expr)
    8112              : {
    8113           48 :   tree arg, type, e, x, cond, nan, stmt, tmp, frexp, scalbn, fabs;
    8114           48 :   int prec, k;
    8115           48 :   stmtblock_t block;
    8116              : 
    8117           48 :   k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
    8118           48 :   prec = gfc_real_kinds[k].digits;
    8119              : 
    8120           48 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
    8121           48 :   scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
    8122           48 :   fabs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
    8123              : 
    8124           48 :   type = gfc_typenode_for_spec (&expr->ts);
    8125           48 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    8126           48 :   arg = gfc_evaluate_now (arg, &se->pre);
    8127              : 
    8128           48 :   e = gfc_create_var (integer_type_node, NULL);
    8129           48 :   x = gfc_create_var (type, NULL);
    8130           48 :   gfc_add_modify (&se->pre, x,
    8131              :                   build_call_expr_loc (input_location, fabs, 1, arg));
    8132              : 
    8133              : 
    8134           48 :   gfc_start_block (&block);
    8135           48 :   tmp = build_call_expr_loc (input_location, frexp, 2, arg,
    8136              :                              gfc_build_addr_expr (NULL_TREE, e));
    8137           48 :   gfc_add_expr_to_block (&block, tmp);
    8138              : 
    8139           48 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
    8140           48 :                          build_int_cst (integer_type_node, prec), e);
    8141           48 :   tmp = build_call_expr_loc (input_location, scalbn, 2, x, tmp);
    8142           48 :   gfc_add_modify (&block, x, tmp);
    8143           48 :   stmt = gfc_finish_block (&block);
    8144              : 
    8145              :   /* if (x != 0) */
    8146           48 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, x,
    8147           48 :                           build_real_from_int_cst (type, integer_zero_node));
    8148           48 :   tmp = build3_v (COND_EXPR, cond, stmt, build_empty_stmt (input_location));
    8149              : 
    8150              :   /* And deal with infinities and NaNs.  */
    8151           48 :   cond = build_call_expr_loc (input_location,
    8152              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    8153              :                               1, x);
    8154           48 :   nan = gfc_build_nan (type, "");
    8155           48 :   tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, x, nan));
    8156              : 
    8157           48 :   gfc_add_expr_to_block (&se->pre, tmp);
    8158           48 :   se->expr = fold_convert (type, x);
    8159           48 : }
    8160              : 
    8161              : 
    8162              : /* SCALE (s, i) is translated into scalbn (s, i).  */
    8163              : static void
    8164           72 : gfc_conv_intrinsic_scale (gfc_se * se, gfc_expr * expr)
    8165              : {
    8166           72 :   tree args[2], type, scalbn;
    8167              : 
    8168           72 :   scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
    8169              : 
    8170           72 :   type = gfc_typenode_for_spec (&expr->ts);
    8171           72 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    8172           72 :   se->expr = build_call_expr_loc (input_location, scalbn, 2,
    8173              :                                   fold_convert (type, args[0]),
    8174              :                                   fold_convert (integer_type_node, args[1]));
    8175           72 :   se->expr = fold_convert (type, se->expr);
    8176           72 : }
    8177              : 
    8178              : 
    8179              : /* SET_EXPONENT (s, i) is translated into
    8180              :    isfinite(s) ? scalbn (frexp (s, &dummy_int), i) : NaN  */
    8181              : static void
    8182          262 : gfc_conv_intrinsic_set_exponent (gfc_se * se, gfc_expr * expr)
    8183              : {
    8184          262 :   tree args[2], type, tmp, frexp, scalbn, cond, nan, res;
    8185              : 
    8186          262 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
    8187          262 :   scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
    8188              : 
    8189          262 :   type = gfc_typenode_for_spec (&expr->ts);
    8190          262 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    8191          262 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    8192              : 
    8193          262 :   tmp = gfc_create_var (integer_type_node, NULL);
    8194          262 :   tmp = build_call_expr_loc (input_location, frexp, 2,
    8195              :                              fold_convert (type, args[0]),
    8196              :                              gfc_build_addr_expr (NULL_TREE, tmp));
    8197          262 :   res = build_call_expr_loc (input_location, scalbn, 2, tmp,
    8198              :                              fold_convert (integer_type_node, args[1]));
    8199          262 :   res = fold_convert (type, res);
    8200              : 
    8201              :   /* Call to isfinite */
    8202          262 :   cond = build_call_expr_loc (input_location,
    8203              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    8204              :                               1, args[0]);
    8205          262 :   nan = gfc_build_nan (type, "");
    8206              : 
    8207          262 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
    8208              :                               res, nan);
    8209          262 : }
    8210              : 
    8211              : 
    8212              : static void
    8213        15139 : gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
    8214              : {
    8215        15139 :   gfc_actual_arglist *actual;
    8216        15139 :   tree arg1;
    8217        15139 :   tree type;
    8218        15139 :   tree size;
    8219        15139 :   gfc_se argse;
    8220        15139 :   gfc_expr *e;
    8221        15139 :   gfc_symbol *sym = NULL;
    8222              : 
    8223        15139 :   gfc_init_se (&argse, NULL);
    8224        15139 :   actual = expr->value.function.actual;
    8225              : 
    8226        15139 :   if (actual->expr->ts.type == BT_CLASS)
    8227          609 :     gfc_add_class_array_ref (actual->expr);
    8228              : 
    8229        15139 :   e = actual->expr;
    8230              : 
    8231              :   /* These are emerging from the interface mapping, when a class valued
    8232              :      function appears as the rhs in a realloc on assign statement, where
    8233              :      the size of the result is that of one of the actual arguments.  */
    8234        15139 :   if (e->expr_type == EXPR_VARIABLE
    8235        14663 :       && e->symtree->n.sym->ns == NULL /* This is distinctive!  */
    8236          573 :       && e->symtree->n.sym->ts.type == BT_CLASS
    8237           62 :       && e->ref && e->ref->type == REF_COMPONENT
    8238           44 :       && strcmp (e->ref->u.c.component->name, "_data") == 0)
    8239        15139 :     sym = e->symtree->n.sym;
    8240              : 
    8241        15139 :   if ((gfc_option.rtcheck & GFC_RTCHECK_POINTER)
    8242              :       && e
    8243          854 :       && (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION))
    8244              :     {
    8245          854 :       symbol_attribute attr;
    8246          854 :       char *msg;
    8247          854 :       tree temp;
    8248          854 :       tree cond;
    8249              : 
    8250          854 :       if (e->symtree->n.sym && IS_CLASS_ARRAY (e->symtree->n.sym))
    8251              :         {
    8252           33 :           attr = CLASS_DATA (e->symtree->n.sym)->attr;
    8253           33 :           attr.pointer = attr.class_pointer;
    8254              :         }
    8255              :       else
    8256          821 :         attr = gfc_expr_attr (e);
    8257              : 
    8258          854 :       if (attr.allocatable)
    8259          100 :         msg = xasprintf ("Allocatable argument '%s' is not allocated",
    8260          100 :                          e->symtree->n.sym->name);
    8261          754 :       else if (attr.pointer)
    8262           46 :         msg = xasprintf ("Pointer argument '%s' is not associated",
    8263           46 :                          e->symtree->n.sym->name);
    8264              :       else
    8265          708 :         goto end_arg_check;
    8266              : 
    8267          146 :       if (sym)
    8268              :         {
    8269            0 :           temp = gfc_class_data_get (sym->backend_decl);
    8270            0 :           temp = gfc_conv_descriptor_data_get (temp);
    8271              :         }
    8272              :       else
    8273              :         {
    8274          146 :           argse.descriptor_only = 1;
    8275          146 :           gfc_conv_expr_descriptor (&argse, actual->expr);
    8276          146 :           temp = gfc_conv_descriptor_data_get (argse.expr);
    8277              :         }
    8278              : 
    8279          146 :       cond = fold_build2_loc (input_location, EQ_EXPR,
    8280              :                               logical_type_node, temp,
    8281          146 :                               fold_convert (TREE_TYPE (temp),
    8282              :                                             null_pointer_node));
    8283          146 :       gfc_trans_runtime_check (true, false, cond, &argse.pre, &e->where, msg);
    8284              : 
    8285          146 :       free (msg);
    8286              :     }
    8287        14285 :  end_arg_check:
    8288              : 
    8289        15139 :   argse.data_not_needed = 1;
    8290        15139 :   if (gfc_is_class_array_function (e))
    8291              :     {
    8292              :       /* For functions that return a class array conv_expr_descriptor is not
    8293              :          able to get the descriptor right.  Therefore this special case.  */
    8294            7 :       gfc_conv_expr_reference (&argse, e);
    8295            7 :       argse.expr = gfc_class_data_get (argse.expr);
    8296              :     }
    8297        15132 :   else if (sym && sym->backend_decl)
    8298              :     {
    8299           32 :       gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (sym->backend_decl)));
    8300           32 :       argse.expr = gfc_class_data_get (sym->backend_decl);
    8301              :     }
    8302              :   else
    8303        15100 :     gfc_conv_expr_descriptor (&argse, actual->expr);
    8304        15139 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8305        15139 :   gfc_add_block_to_block (&se->post, &argse.post);
    8306        15139 :   arg1 = argse.expr;
    8307              : 
    8308        15139 :   actual = actual->next;
    8309        15139 :   if (actual->expr)
    8310              :     {
    8311         9033 :       stmtblock_t block;
    8312         9033 :       gfc_init_block (&block);
    8313         9033 :       gfc_init_se (&argse, NULL);
    8314         9033 :       gfc_conv_expr_type (&argse, actual->expr,
    8315              :                           gfc_array_index_type);
    8316         9033 :       gfc_add_block_to_block (&block, &argse.pre);
    8317         9033 :       tree tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    8318              :                              argse.expr, gfc_index_one_node);
    8319         9033 :       size = gfc_tree_array_size (&block, arg1, e, tmp);
    8320              : 
    8321              :       /* Unusually, for an intrinsic, size does not exclude
    8322              :          an optional arg2, so we must test for it.  */
    8323         9033 :       if (actual->expr->expr_type == EXPR_VARIABLE
    8324         2398 :             && actual->expr->symtree->n.sym->attr.dummy
    8325           31 :             && actual->expr->symtree->n.sym->attr.optional)
    8326              :         {
    8327           31 :           tree cond;
    8328           31 :           stmtblock_t block2;
    8329           31 :           gfc_init_block (&block2);
    8330           31 :           gfc_init_se (&argse, NULL);
    8331           31 :           argse.want_pointer = 1;
    8332           31 :           argse.data_not_needed = 1;
    8333           31 :           gfc_conv_expr (&argse, actual->expr);
    8334           31 :           gfc_add_block_to_block (&se->pre, &argse.pre);
    8335              :           /* 'block2' contains the arg2 absent case, 'block' the arg2 present
    8336              :               case; size_var can be used in both blocks. */
    8337           31 :           tree size_var = gfc_create_var (TREE_TYPE (size), "size");
    8338           31 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    8339           31 :                                  TREE_TYPE (size_var), size_var, size);
    8340           31 :           gfc_add_expr_to_block (&block, tmp);
    8341           31 :           size = gfc_tree_array_size (&block2, arg1, e, NULL_TREE);
    8342           31 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    8343           31 :                                  TREE_TYPE (size_var), size_var, size);
    8344           31 :           gfc_add_expr_to_block (&block2, tmp);
    8345           31 :           cond = gfc_conv_expr_present (actual->expr->symtree->n.sym);
    8346           31 :           tmp = build3_v (COND_EXPR, cond, gfc_finish_block (&block),
    8347              :                           gfc_finish_block (&block2));
    8348           31 :           gfc_add_expr_to_block (&se->pre, tmp);
    8349           31 :           size = size_var;
    8350           31 :         }
    8351              :       else
    8352         9002 :         gfc_add_block_to_block (&se->pre, &block);
    8353              :     }
    8354              :   else
    8355         6106 :     size = gfc_tree_array_size (&se->pre, arg1, e, NULL_TREE);
    8356        15139 :   type = gfc_typenode_for_spec (&expr->ts);
    8357        15139 :   se->expr = convert (type, size);
    8358        15139 : }
    8359              : 
    8360              : 
    8361              : /* Helper function to compute the size of a character variable,
    8362              :    excluding the terminating null characters.  The result has
    8363              :    gfc_array_index_type type.  */
    8364              : 
    8365              : tree
    8366         1863 : size_of_string_in_bytes (int kind, tree string_length)
    8367              : {
    8368         1863 :   tree bytesize;
    8369         1863 :   int i = gfc_validate_kind (BT_CHARACTER, kind, false);
    8370              : 
    8371         3726 :   bytesize = build_int_cst (gfc_array_index_type,
    8372         1863 :                             gfc_character_kinds[i].bit_size / 8);
    8373              : 
    8374         1863 :   return fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    8375              :                           bytesize,
    8376         1863 :                           fold_convert (gfc_array_index_type, string_length));
    8377              : }
    8378              : 
    8379              : 
    8380              : static void
    8381         1309 : gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr)
    8382              : {
    8383         1309 :   gfc_expr *arg;
    8384         1309 :   gfc_se argse;
    8385         1309 :   tree source_bytes;
    8386         1309 :   tree tmp;
    8387         1309 :   tree lower;
    8388         1309 :   tree upper;
    8389         1309 :   tree byte_size;
    8390         1309 :   tree field;
    8391         1309 :   int n;
    8392              : 
    8393         1309 :   gfc_init_se (&argse, NULL);
    8394         1309 :   arg = expr->value.function.actual->expr;
    8395              : 
    8396         1309 :   if (arg->rank || arg->ts.type == BT_ASSUMED)
    8397         1012 :     gfc_conv_expr_descriptor (&argse, arg);
    8398              :   else
    8399          297 :     gfc_conv_expr_reference (&argse, arg);
    8400              : 
    8401         1309 :   if (arg->ts.type == BT_ASSUMED)
    8402              :     {
    8403              :       /* This only works if an array descriptor has been passed; thus, extract
    8404              :          the size from the descriptor.  */
    8405          172 :       gcc_assert (TYPE_PRECISION (gfc_array_index_type)
    8406              :                   == TYPE_PRECISION (size_type_node));
    8407          172 :       tmp = arg->symtree->n.sym->backend_decl;
    8408          172 :       tmp = DECL_LANG_SPECIFIC (tmp)
    8409           60 :             && GFC_DECL_SAVED_DESCRIPTOR (tmp) != NULL_TREE
    8410          226 :             ? GFC_DECL_SAVED_DESCRIPTOR (tmp) : tmp;
    8411          172 :       if (POINTER_TYPE_P (TREE_TYPE (tmp)))
    8412          172 :         tmp = build_fold_indirect_ref_loc (input_location, tmp);
    8413              : 
    8414          172 :       tmp = gfc_conv_descriptor_dtype (tmp);
    8415          172 :       field = gfc_advance_chain (TYPE_FIELDS (get_dtype_type_node ()),
    8416              :                                  GFC_DTYPE_ELEM_LEN);
    8417          172 :       tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
    8418              :                              tmp, field, NULL_TREE);
    8419              : 
    8420          172 :       byte_size = fold_convert (gfc_array_index_type, tmp);
    8421              :     }
    8422         1137 :   else if (arg->ts.type == BT_CLASS)
    8423              :     {
    8424              :       /* Conv_expr_descriptor returns a component_ref to _data component of the
    8425              :          class object.  The class object may be a non-pointer object, e.g.
    8426              :          located on the stack, or a memory location pointed to, e.g. a
    8427              :          parameter, i.e., an indirect_ref.  */
    8428          959 :       if (POINTER_TYPE_P (TREE_TYPE (argse.expr))
    8429          589 :           && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (argse.expr))))
    8430          198 :         byte_size
    8431          198 :           = gfc_class_vtab_size_get (build_fold_indirect_ref (argse.expr));
    8432          391 :       else if (GFC_CLASS_TYPE_P (TREE_TYPE (argse.expr)))
    8433            0 :         byte_size = gfc_class_vtab_size_get (argse.expr);
    8434          391 :       else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (argse.expr))
    8435          391 :                && TREE_CODE (argse.expr) == COMPONENT_REF)
    8436          328 :         byte_size = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
    8437           63 :       else if (arg->rank > 0
    8438           21 :                || (arg->rank == 0
    8439           21 :                    && arg->ref && arg->ref->type == REF_COMPONENT))
    8440              :         {
    8441              :           /* The scalarizer added an additional temp.  To get the class' vptr
    8442              :              one has to look at the original backend_decl.  */
    8443           63 :           if (argse.class_container)
    8444           21 :             byte_size = gfc_class_vtab_size_get (argse.class_container);
    8445           42 :           else if (DECL_LANG_SPECIFIC (arg->symtree->n.sym->backend_decl))
    8446           84 :             byte_size = gfc_class_vtab_size_get (
    8447           42 :               GFC_DECL_SAVED_DESCRIPTOR (arg->symtree->n.sym->backend_decl));
    8448              :           else
    8449            0 :             gcc_unreachable ();
    8450              :         }
    8451              :       else
    8452            0 :         gcc_unreachable ();
    8453              :     }
    8454              :   else
    8455              :     {
    8456          548 :       if (arg->ts.type == BT_CHARACTER)
    8457           84 :         byte_size = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
    8458              :       else
    8459              :         {
    8460          464 :           if (arg->rank == 0)
    8461            0 :             byte_size = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
    8462              :                                                                 argse.expr));
    8463              :           else
    8464          464 :             byte_size = gfc_get_element_type (TREE_TYPE (argse.expr));
    8465          464 :           byte_size = fold_convert (gfc_array_index_type,
    8466              :                                     size_in_bytes (byte_size));
    8467              :         }
    8468              :     }
    8469              : 
    8470         1309 :   if (arg->rank == 0)
    8471          297 :     se->expr = byte_size;
    8472              :   else
    8473              :     {
    8474         1012 :       source_bytes = gfc_create_var (gfc_array_index_type, "bytes");
    8475         1012 :       gfc_add_modify (&argse.pre, source_bytes, byte_size);
    8476              : 
    8477         1012 :       if (arg->rank == -1)
    8478              :         {
    8479          365 :           tree cond, loop_var, exit_label;
    8480          365 :           stmtblock_t body;
    8481              : 
    8482          365 :           tmp = fold_convert (gfc_array_index_type,
    8483              :                               gfc_conv_descriptor_rank (argse.expr));
    8484          365 :           loop_var = gfc_create_var (gfc_array_index_type, "i");
    8485          365 :           gfc_add_modify (&argse.pre, loop_var, gfc_index_zero_node);
    8486          365 :           exit_label = gfc_build_label_decl (NULL_TREE);
    8487              : 
    8488              :           /* Create loop:
    8489              :              for (;;)
    8490              :                 {
    8491              :                   if (i >= rank)
    8492              :                     goto exit;
    8493              :                   source_bytes = source_bytes * array.dim[i].extent;
    8494              :                   i = i + 1;
    8495              :                 }
    8496              :               exit:  */
    8497          365 :           gfc_start_block (&body);
    8498          365 :           cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    8499              :                                   loop_var, tmp);
    8500          365 :           tmp = build1_v (GOTO_EXPR, exit_label);
    8501          365 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    8502              :                                  cond, tmp, build_empty_stmt (input_location));
    8503          365 :           gfc_add_expr_to_block (&body, tmp);
    8504              : 
    8505          365 :           lower = gfc_conv_descriptor_lbound_get (argse.expr, loop_var);
    8506          365 :           upper = gfc_conv_descriptor_ubound_get (argse.expr, loop_var);
    8507          365 :           tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
    8508          365 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    8509              :                                  gfc_array_index_type, tmp, source_bytes);
    8510          365 :           gfc_add_modify (&body, source_bytes, tmp);
    8511              : 
    8512          365 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    8513              :                                  gfc_array_index_type, loop_var,
    8514              :                                  gfc_index_one_node);
    8515          365 :           gfc_add_modify_loc (input_location, &body, loop_var, tmp);
    8516              : 
    8517          365 :           tmp = gfc_finish_block (&body);
    8518              : 
    8519          365 :           tmp = fold_build1_loc (input_location, LOOP_EXPR, void_type_node,
    8520              :                                  tmp);
    8521          365 :           gfc_add_expr_to_block (&argse.pre, tmp);
    8522              : 
    8523          365 :           tmp = build1_v (LABEL_EXPR, exit_label);
    8524          365 :           gfc_add_expr_to_block (&argse.pre, tmp);
    8525              :         }
    8526              :       else
    8527              :         {
    8528              :           /* Obtain the size of the array in bytes.  */
    8529         1834 :           for (n = 0; n < arg->rank; n++)
    8530              :             {
    8531         1187 :               tree idx;
    8532         1187 :               idx = gfc_rank_cst[n];
    8533         1187 :               lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
    8534         1187 :               upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
    8535         1187 :               tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
    8536         1187 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
    8537              :                                      gfc_array_index_type, tmp, source_bytes);
    8538         1187 :               gfc_add_modify (&argse.pre, source_bytes, tmp);
    8539              :             }
    8540              :         }
    8541         1012 :       se->expr = source_bytes;
    8542              :     }
    8543              : 
    8544         1309 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8545         1309 : }
    8546              : 
    8547              : 
    8548              : static void
    8549          834 : gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
    8550              : {
    8551          834 :   gfc_expr *arg;
    8552          834 :   gfc_se argse;
    8553          834 :   tree type, result_type, tmp, class_decl = NULL;
    8554          834 :   gfc_symbol *sym;
    8555          834 :   bool unlimited = false;
    8556              : 
    8557          834 :   arg = expr->value.function.actual->expr;
    8558              : 
    8559          834 :   gfc_init_se (&argse, NULL);
    8560          834 :   result_type = gfc_get_int_type (expr->ts.kind);
    8561              : 
    8562          834 :   if (arg->rank == 0)
    8563              :     {
    8564          224 :       if (arg->ts.type == BT_CLASS)
    8565              :         {
    8566           86 :           unlimited = UNLIMITED_POLY (arg);
    8567           86 :           gfc_add_vptr_component (arg);
    8568           86 :           gfc_add_size_component (arg);
    8569           86 :           gfc_conv_expr (&argse, arg);
    8570           86 :           tmp = fold_convert (result_type, argse.expr);
    8571           86 :           class_decl = gfc_get_class_from_expr (argse.expr);
    8572           86 :           goto done;
    8573              :         }
    8574              : 
    8575          138 :       gfc_conv_expr_reference (&argse, arg);
    8576          138 :       type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
    8577              :                                                      argse.expr));
    8578              :     }
    8579              :   else
    8580              :     {
    8581          610 :       argse.want_pointer = 0;
    8582          610 :       gfc_conv_expr_descriptor (&argse, arg);
    8583          610 :       sym = arg->expr_type == EXPR_VARIABLE ? arg->symtree->n.sym : NULL;
    8584          610 :       if (arg->ts.type == BT_CLASS)
    8585              :         {
    8586           60 :           unlimited = UNLIMITED_POLY (arg);
    8587           60 :           if (TREE_CODE (argse.expr) == COMPONENT_REF)
    8588           54 :             tmp = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
    8589            6 :           else if (arg->rank > 0 && sym
    8590           12 :                    && DECL_LANG_SPECIFIC (sym->backend_decl))
    8591           12 :             tmp = gfc_class_vtab_size_get (
    8592            6 :                  GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl));
    8593              :           else
    8594            0 :             gcc_unreachable ();
    8595           60 :           tmp = fold_convert (result_type, tmp);
    8596           60 :           class_decl = gfc_get_class_from_expr (argse.expr);
    8597           60 :           goto done;
    8598              :         }
    8599          550 :       type = gfc_get_element_type (TREE_TYPE (argse.expr));
    8600              :     }
    8601              : 
    8602              :   /* Obtain the argument's word length.  */
    8603          688 :   if (arg->ts.type == BT_CHARACTER)
    8604          241 :     tmp = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
    8605              :   else
    8606          447 :     tmp = size_in_bytes (type);
    8607          688 :   tmp = fold_convert (result_type, tmp);
    8608              : 
    8609          834 : done:
    8610          834 :   if (unlimited && class_decl)
    8611           68 :     tmp = gfc_resize_class_size_with_len (NULL, class_decl, tmp);
    8612              : 
    8613          834 :   se->expr = fold_build2_loc (input_location, MULT_EXPR, result_type, tmp,
    8614              :                               build_int_cst (result_type, BITS_PER_UNIT));
    8615          834 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8616          834 : }
    8617              : 
    8618              : 
    8619              : /* Intrinsic string comparison functions.  */
    8620              : 
    8621              : static void
    8622           99 : gfc_conv_intrinsic_strcmp (gfc_se * se, gfc_expr * expr, enum tree_code op)
    8623              : {
    8624           99 :   tree args[4];
    8625              : 
    8626           99 :   gfc_conv_intrinsic_function_args (se, expr, args, 4);
    8627              : 
    8628           99 :   se->expr
    8629          198 :     = gfc_build_compare_string (args[0], args[1], args[2], args[3],
    8630           99 :                                 expr->value.function.actual->expr->ts.kind,
    8631              :                                 op);
    8632           99 :   se->expr = fold_build2_loc (input_location, op,
    8633              :                               gfc_typenode_for_spec (&expr->ts), se->expr,
    8634           99 :                               build_int_cst (TREE_TYPE (se->expr), 0));
    8635           99 : }
    8636              : 
    8637              : /* Generate a call to the adjustl/adjustr library function.  */
    8638              : static void
    8639          474 : gfc_conv_intrinsic_adjust (gfc_se * se, gfc_expr * expr, tree fndecl)
    8640              : {
    8641          474 :   tree args[3];
    8642          474 :   tree len;
    8643          474 :   tree type;
    8644          474 :   tree var;
    8645          474 :   tree tmp;
    8646              : 
    8647          474 :   gfc_conv_intrinsic_function_args (se, expr, &args[1], 2);
    8648          474 :   len = args[1];
    8649              : 
    8650          474 :   type = TREE_TYPE (args[2]);
    8651          474 :   var = gfc_conv_string_tmp (se, type, len);
    8652          474 :   args[0] = var;
    8653              : 
    8654          474 :   tmp = build_call_expr_loc (input_location,
    8655              :                          fndecl, 3, args[0], args[1], args[2]);
    8656          474 :   gfc_add_expr_to_block (&se->pre, tmp);
    8657          474 :   se->expr = var;
    8658          474 :   se->string_length = len;
    8659          474 : }
    8660              : 
    8661              : 
    8662              : /* Generate code for the TRANSFER intrinsic:
    8663              :         For scalar results:
    8664              :           DEST = TRANSFER (SOURCE, MOLD)
    8665              :         where:
    8666              :           typeof<DEST> = typeof<MOLD>
    8667              :         and:
    8668              :           MOLD is scalar.
    8669              : 
    8670              :         For array results:
    8671              :           DEST(1:N) = TRANSFER (SOURCE, MOLD[, SIZE])
    8672              :         where:
    8673              :           typeof<DEST> = typeof<MOLD>
    8674              :         and:
    8675              :           N = min (sizeof (SOURCE(:)), sizeof (DEST(:)),
    8676              :               sizeof (DEST(0) * SIZE).  */
    8677              : static void
    8678         3778 : gfc_conv_intrinsic_transfer (gfc_se * se, gfc_expr * expr)
    8679              : {
    8680         3778 :   tree tmp;
    8681         3778 :   tree tmpdecl;
    8682         3778 :   tree ptr;
    8683         3778 :   tree extent;
    8684         3778 :   tree source;
    8685         3778 :   tree source_type;
    8686         3778 :   tree source_bytes;
    8687         3778 :   tree mold_type;
    8688         3778 :   tree dest_word_len;
    8689         3778 :   tree size_words;
    8690         3778 :   tree size_bytes;
    8691         3778 :   tree upper;
    8692         3778 :   tree lower;
    8693         3778 :   tree stmt;
    8694         3778 :   tree class_ref = NULL_TREE;
    8695         3778 :   gfc_actual_arglist *arg;
    8696         3778 :   gfc_se argse;
    8697         3778 :   gfc_array_info *info;
    8698         3778 :   stmtblock_t block;
    8699         3778 :   int n;
    8700         3778 :   bool scalar_mold;
    8701         3778 :   gfc_expr *source_expr, *mold_expr, *class_expr;
    8702              : 
    8703         3778 :   info = NULL;
    8704         3778 :   if (se->loop)
    8705          472 :     info = &se->ss->info->data.array;
    8706              : 
    8707              :   /* Convert SOURCE.  The output from this stage is:-
    8708              :         source_bytes = length of the source in bytes
    8709              :         source = pointer to the source data.  */
    8710         3778 :   arg = expr->value.function.actual;
    8711         3778 :   source_expr = arg->expr;
    8712              : 
    8713              :   /* Ensure double transfer through LOGICAL preserves all
    8714              :      the needed bits.  */
    8715         3778 :   if (arg->expr->expr_type == EXPR_FUNCTION
    8716         2786 :         && arg->expr->value.function.esym == NULL
    8717         2762 :         && arg->expr->value.function.isym != NULL
    8718         2762 :         && arg->expr->value.function.isym->id == GFC_ISYM_TRANSFER
    8719           12 :         && arg->expr->ts.type == BT_LOGICAL
    8720           12 :         && expr->ts.type != arg->expr->ts.type)
    8721           12 :     arg->expr->value.function.name = "__transfer_in_transfer";
    8722              : 
    8723         3778 :   gfc_init_se (&argse, NULL);
    8724              : 
    8725         3778 :   source_bytes = gfc_create_var (gfc_array_index_type, NULL);
    8726              : 
    8727              :   /* Obtain the pointer to source and the length of source in bytes.  */
    8728         3778 :   if (arg->expr->rank == 0)
    8729              :     {
    8730         3422 :       gfc_conv_expr_reference (&argse, arg->expr);
    8731         3422 :       if (arg->expr->ts.type == BT_CLASS)
    8732              :         {
    8733           37 :           tmp = build_fold_indirect_ref_loc (input_location, argse.expr);
    8734           37 :           if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
    8735              :             {
    8736           19 :               source = gfc_class_data_get (tmp);
    8737           19 :               class_ref = tmp;
    8738              :             }
    8739              :           else
    8740              :             {
    8741              :               /* Array elements are evaluated as a reference to the data.
    8742              :                  To obtain the vptr for the element size, the argument
    8743              :                  expression must be stripped to the class reference and
    8744              :                  re-evaluated. The pre and post blocks are not needed.  */
    8745           18 :               gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
    8746           18 :               source = argse.expr;
    8747           18 :               class_expr = gfc_find_and_cut_at_last_class_ref (arg->expr);
    8748           18 :               gfc_init_se (&argse, NULL);
    8749           18 :               gfc_conv_expr (&argse, class_expr);
    8750           18 :               class_ref = argse.expr;
    8751              :             }
    8752              :         }
    8753              :       else
    8754         3385 :         source = argse.expr;
    8755              : 
    8756              :       /* Obtain the source word length.  */
    8757         3422 :       switch (arg->expr->ts.type)
    8758              :         {
    8759          294 :         case BT_CHARACTER:
    8760          294 :           tmp = size_of_string_in_bytes (arg->expr->ts.kind,
    8761              :                                          argse.string_length);
    8762          294 :           break;
    8763           37 :         case BT_CLASS:
    8764           37 :           if (class_ref != NULL_TREE)
    8765              :             {
    8766           37 :               tmp = gfc_class_vtab_size_get (class_ref);
    8767           37 :               if (UNLIMITED_POLY (source_expr))
    8768           30 :                 tmp = gfc_resize_class_size_with_len (NULL, class_ref, tmp);
    8769              :             }
    8770              :           else
    8771              :             {
    8772            0 :               tmp = gfc_class_vtab_size_get (argse.expr);
    8773            0 :               if (UNLIMITED_POLY (source_expr))
    8774            0 :                 tmp = gfc_resize_class_size_with_len (NULL, argse.expr, tmp);
    8775              :             }
    8776              :           break;
    8777         3091 :         default:
    8778         3091 :           source_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
    8779              :                                                                 source));
    8780         3091 :           tmp = fold_convert (gfc_array_index_type,
    8781              :                               size_in_bytes (source_type));
    8782         3091 :           break;
    8783              :         }
    8784              :     }
    8785              :   else
    8786              :     {
    8787          356 :       bool simply_contiguous = gfc_is_simply_contiguous (arg->expr,
    8788              :                                                          false, true);
    8789          356 :       argse.want_pointer = 0;
    8790              :       /* A non-contiguous SOURCE needs packing.  */
    8791          356 :       if (!simply_contiguous)
    8792           74 :         argse.force_tmp = 1;
    8793          356 :       gfc_conv_expr_descriptor (&argse, arg->expr);
    8794          356 :       source = gfc_conv_descriptor_data_get (argse.expr);
    8795          356 :       source_type = gfc_get_element_type (TREE_TYPE (argse.expr));
    8796              : 
    8797              :       /* Repack the source if not simply contiguous.  */
    8798          356 :       if (!simply_contiguous)
    8799              :         {
    8800           74 :           tmp = gfc_build_addr_expr (NULL_TREE, argse.expr);
    8801              : 
    8802           74 :           if (warn_array_temporaries)
    8803            0 :             gfc_warning (OPT_Warray_temporaries,
    8804              :                          "Creating array temporary at %L", &expr->where);
    8805              : 
    8806           74 :           source = build_call_expr_loc (input_location,
    8807              :                                     gfor_fndecl_in_pack, 1, tmp);
    8808           74 :           source = gfc_evaluate_now (source, &argse.pre);
    8809              : 
    8810              :           /* Free the temporary.  */
    8811           74 :           gfc_start_block (&block);
    8812           74 :           tmp = gfc_call_free (source);
    8813           74 :           gfc_add_expr_to_block (&block, tmp);
    8814           74 :           stmt = gfc_finish_block (&block);
    8815              : 
    8816              :           /* Clean up if it was repacked.  */
    8817           74 :           gfc_init_block (&block);
    8818           74 :           tmp = gfc_conv_array_data (argse.expr);
    8819           74 :           tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    8820              :                                  source, tmp);
    8821           74 :           tmp = build3_v (COND_EXPR, tmp, stmt,
    8822              :                           build_empty_stmt (input_location));
    8823           74 :           gfc_add_expr_to_block (&block, tmp);
    8824           74 :           gfc_add_block_to_block (&block, &se->post);
    8825           74 :           gfc_init_block (&se->post);
    8826           74 :           gfc_add_block_to_block (&se->post, &block);
    8827              :         }
    8828              : 
    8829              :       /* Obtain the source word length.  */
    8830          356 :       if (arg->expr->ts.type == BT_CHARACTER)
    8831          144 :         tmp = size_of_string_in_bytes (arg->expr->ts.kind,
    8832              :                                        argse.string_length);
    8833          212 :       else if (arg->expr->ts.type == BT_CLASS)
    8834              :         {
    8835           54 :           if (UNLIMITED_POLY (source_expr)
    8836           54 :               && DECL_LANG_SPECIFIC (source_expr->symtree->n.sym->backend_decl))
    8837           12 :             class_ref = GFC_DECL_SAVED_DESCRIPTOR
    8838              :               (source_expr->symtree->n.sym->backend_decl);
    8839              :           else
    8840           42 :             class_ref = TREE_OPERAND (argse.expr, 0);
    8841           54 :           tmp = gfc_class_vtab_size_get (class_ref);
    8842           54 :           if (UNLIMITED_POLY (arg->expr))
    8843           54 :             tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
    8844              :         }
    8845              :       else
    8846          158 :         tmp = fold_convert (gfc_array_index_type,
    8847              :                             size_in_bytes (source_type));
    8848              : 
    8849              :       /* Obtain the size of the array in bytes.  */
    8850          356 :       extent = gfc_create_var (gfc_array_index_type, NULL);
    8851          742 :       for (n = 0; n < arg->expr->rank; n++)
    8852              :         {
    8853          386 :           tree idx;
    8854          386 :           idx = gfc_rank_cst[n];
    8855          386 :           gfc_add_modify (&argse.pre, source_bytes, tmp);
    8856          386 :           lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
    8857          386 :           upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
    8858          386 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8859              :                                  gfc_array_index_type, upper, lower);
    8860          386 :           gfc_add_modify (&argse.pre, extent, tmp);
    8861          386 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    8862              :                                  gfc_array_index_type, extent,
    8863              :                                  gfc_index_one_node);
    8864          386 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    8865              :                                  gfc_array_index_type, tmp, source_bytes);
    8866              :         }
    8867              :     }
    8868              : 
    8869         3778 :   gfc_add_modify (&argse.pre, source_bytes, tmp);
    8870         3778 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8871         3778 :   gfc_add_block_to_block (&se->post, &argse.post);
    8872              : 
    8873              :   /* Now convert MOLD.  The outputs are:
    8874              :         mold_type = the TREE type of MOLD
    8875              :         dest_word_len = destination word length in bytes.  */
    8876         3778 :   arg = arg->next;
    8877         3778 :   mold_expr = arg->expr;
    8878              : 
    8879         3778 :   gfc_init_se (&argse, NULL);
    8880              : 
    8881         3778 :   scalar_mold = arg->expr->rank == 0;
    8882              : 
    8883         3778 :   if (arg->expr->rank == 0)
    8884              :     {
    8885         3455 :       gfc_conv_expr_reference (&argse, mold_expr);
    8886         3455 :       mold_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
    8887              :                                                           argse.expr));
    8888              :     }
    8889              :   else
    8890              :     {
    8891          323 :       argse.want_pointer = 0;
    8892          323 :       gfc_conv_expr_descriptor (&argse, mold_expr);
    8893          323 :       mold_type = gfc_get_element_type (TREE_TYPE (argse.expr));
    8894              :     }
    8895              : 
    8896         3778 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8897         3778 :   gfc_add_block_to_block (&se->post, &argse.post);
    8898              : 
    8899         3778 :   if (strcmp (expr->value.function.name, "__transfer_in_transfer") == 0)
    8900              :     {
    8901              :       /* If this TRANSFER is nested in another TRANSFER, use a type
    8902              :          that preserves all bits.  */
    8903           12 :       if (mold_expr->ts.type == BT_LOGICAL)
    8904           12 :         mold_type = gfc_get_int_type (mold_expr->ts.kind);
    8905              :     }
    8906              : 
    8907              :   /* Obtain the destination word length.  */
    8908         3778 :   switch (mold_expr->ts.type)
    8909              :     {
    8910          467 :     case BT_CHARACTER:
    8911          467 :       tmp = size_of_string_in_bytes (mold_expr->ts.kind, argse.string_length);
    8912          467 :       mold_type = gfc_get_character_type_len (mold_expr->ts.kind,
    8913              :                                               argse.string_length);
    8914          467 :       break;
    8915            6 :     case BT_CLASS:
    8916            6 :       if (scalar_mold)
    8917            6 :         class_ref = argse.expr;
    8918              :       else
    8919            0 :         class_ref = TREE_OPERAND (argse.expr, 0);
    8920            6 :       tmp = gfc_class_vtab_size_get (class_ref);
    8921            6 :       if (UNLIMITED_POLY (arg->expr))
    8922            0 :         tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
    8923              :       break;
    8924         3305 :     default:
    8925         3305 :       tmp = fold_convert (gfc_array_index_type, size_in_bytes (mold_type));
    8926         3305 :       break;
    8927              :     }
    8928              : 
    8929              :   /* Do not fix dest_word_len if it is a variable, since the temporary can wind
    8930              :      up being used before the assignment.  */
    8931         3778 :   if (mold_expr->ts.type == BT_CHARACTER && mold_expr->ts.deferred)
    8932              :     dest_word_len = tmp;
    8933              :   else
    8934              :     {
    8935         3724 :       dest_word_len = gfc_create_var (gfc_array_index_type, NULL);
    8936         3724 :       gfc_add_modify (&se->pre, dest_word_len, tmp);
    8937              :     }
    8938              : 
    8939              :   /* Finally convert SIZE, if it is present.  */
    8940         3778 :   arg = arg->next;
    8941         3778 :   size_words = gfc_create_var (gfc_array_index_type, NULL);
    8942              : 
    8943         3778 :   if (arg->expr)
    8944              :     {
    8945          222 :       gfc_init_se (&argse, NULL);
    8946          222 :       gfc_conv_expr_reference (&argse, arg->expr);
    8947          222 :       tmp = convert (gfc_array_index_type,
    8948              :                      build_fold_indirect_ref_loc (input_location,
    8949              :                                               argse.expr));
    8950          222 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    8951          222 :       gfc_add_block_to_block (&se->post, &argse.post);
    8952              :     }
    8953              :   else
    8954              :     tmp = NULL_TREE;
    8955              : 
    8956              :   /* Separate array and scalar results.  */
    8957         3778 :   if (scalar_mold && tmp == NULL_TREE)
    8958         3306 :     goto scalar_transfer;
    8959              : 
    8960          472 :   size_bytes = gfc_create_var (gfc_array_index_type, NULL);
    8961          472 :   if (tmp != NULL_TREE)
    8962          222 :     tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    8963              :                            tmp, dest_word_len);
    8964              :   else
    8965              :     tmp = source_bytes;
    8966              : 
    8967          472 :   gfc_add_modify (&se->pre, size_bytes, tmp);
    8968          472 :   gfc_add_modify (&se->pre, size_words,
    8969              :                        fold_build2_loc (input_location, CEIL_DIV_EXPR,
    8970              :                                         gfc_array_index_type,
    8971              :                                         size_bytes, dest_word_len));
    8972              : 
    8973              :   /* Evaluate the bounds of the result.  If the loop range exists, we have
    8974              :      to check if it is too large.  If so, we modify loop->to be consistent
    8975              :      with min(size, size(source)).  Otherwise, size is made consistent with
    8976              :      the loop range, so that the right number of bytes is transferred.*/
    8977          472 :   n = se->loop->order[0];
    8978          472 :   if (se->loop->to[n] != NULL_TREE)
    8979              :     {
    8980          205 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    8981              :                              se->loop->to[n], se->loop->from[n]);
    8982          205 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    8983              :                              tmp, gfc_index_one_node);
    8984          205 :       tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
    8985              :                          tmp, size_words);
    8986          205 :       gfc_add_modify (&se->pre, size_words, tmp);
    8987          205 :       gfc_add_modify (&se->pre, size_bytes,
    8988              :                            fold_build2_loc (input_location, MULT_EXPR,
    8989              :                                             gfc_array_index_type,
    8990              :                                             size_words, dest_word_len));
    8991          410 :       upper = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    8992          205 :                                size_words, se->loop->from[n]);
    8993          205 :       upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    8994              :                                upper, gfc_index_one_node);
    8995              :     }
    8996              :   else
    8997              :     {
    8998          267 :       upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    8999              :                                size_words, gfc_index_one_node);
    9000          267 :       se->loop->from[n] = gfc_index_zero_node;
    9001              :     }
    9002              : 
    9003          472 :   se->loop->to[n] = upper;
    9004              : 
    9005              :   /* Build a destination descriptor, using the pointer, source, as the
    9006              :      data field.  */
    9007          472 :   gfc_trans_create_temp_array (&se->pre, &se->post, se->ss, mold_type,
    9008              :                                NULL_TREE, false, true, false, &expr->where);
    9009              : 
    9010              :   /* Cast the pointer to the result.  */
    9011          472 :   tmp = gfc_conv_descriptor_data_get (info->descriptor);
    9012          472 :   tmp = fold_convert (pvoid_type_node, tmp);
    9013              : 
    9014              :   /* Use memcpy to do the transfer.  */
    9015          472 :   tmp
    9016          472 :     = build_call_expr_loc (input_location,
    9017              :                            builtin_decl_explicit (BUILT_IN_MEMCPY), 3, tmp,
    9018              :                            fold_convert (pvoid_type_node, source),
    9019              :                            fold_convert (size_type_node,
    9020              :                                          fold_build2_loc (input_location,
    9021              :                                                           MIN_EXPR,
    9022              :                                                           gfc_array_index_type,
    9023              :                                                           size_bytes,
    9024              :                                                           source_bytes)));
    9025          472 :   gfc_add_expr_to_block (&se->pre, tmp);
    9026              : 
    9027          472 :   se->expr = info->descriptor;
    9028          472 :   if (expr->ts.type == BT_CHARACTER)
    9029              :     {
    9030          275 :       tmp = fold_convert (gfc_charlen_type_node,
    9031              :                           TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
    9032          275 :       se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    9033              :                                            gfc_charlen_type_node,
    9034              :                                            dest_word_len, tmp);
    9035              :     }
    9036              : 
    9037          472 :   return;
    9038              : 
    9039              : /* Deal with scalar results.  */
    9040         3306 : scalar_transfer:
    9041         3306 :   extent = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
    9042              :                             dest_word_len, source_bytes);
    9043         3306 :   extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    9044              :                             extent, gfc_index_zero_node);
    9045              : 
    9046         3306 :   if (expr->ts.type == BT_CHARACTER)
    9047              :     {
    9048          192 :       tree direct, indirect, free;
    9049              : 
    9050          192 :       ptr = convert (gfc_get_pchar_type (expr->ts.kind), source);
    9051          192 :       tmpdecl = gfc_create_var (gfc_get_pchar_type (expr->ts.kind),
    9052              :                                 "transfer");
    9053              : 
    9054              :       /* If source is longer than the destination, use a pointer to
    9055              :          the source directly.  */
    9056          192 :       gfc_init_block (&block);
    9057          192 :       gfc_add_modify (&block, tmpdecl, ptr);
    9058          192 :       direct = gfc_finish_block (&block);
    9059              : 
    9060              :       /* Otherwise, allocate a string with the length of the destination
    9061              :          and copy the source into it.  */
    9062          192 :       gfc_init_block (&block);
    9063          192 :       tmp = gfc_get_pchar_type (expr->ts.kind);
    9064          192 :       tmp = gfc_call_malloc (&block, tmp, dest_word_len);
    9065          192 :       gfc_add_modify (&block, tmpdecl,
    9066          192 :                       fold_convert (TREE_TYPE (ptr), tmp));
    9067          192 :       tmp = build_call_expr_loc (input_location,
    9068              :                              builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
    9069              :                              fold_convert (pvoid_type_node, tmpdecl),
    9070              :                              fold_convert (pvoid_type_node, ptr),
    9071              :                              fold_convert (size_type_node, extent));
    9072          192 :       gfc_add_expr_to_block (&block, tmp);
    9073          192 :       indirect = gfc_finish_block (&block);
    9074              : 
    9075              :       /* Wrap it up with the condition.  */
    9076          192 :       tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    9077              :                              dest_word_len, source_bytes);
    9078          192 :       tmp = build3_v (COND_EXPR, tmp, direct, indirect);
    9079          192 :       gfc_add_expr_to_block (&se->pre, tmp);
    9080              : 
    9081              :       /* Free the temporary string, if necessary.  */
    9082          192 :       free = gfc_call_free (tmpdecl);
    9083          192 :       tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    9084              :                              dest_word_len, source_bytes);
    9085          192 :       tmp = build3_v (COND_EXPR, tmp, free, build_empty_stmt (input_location));
    9086          192 :       gfc_add_expr_to_block (&se->post, tmp);
    9087              : 
    9088          192 :       se->expr = tmpdecl;
    9089          192 :       tmp = fold_convert (gfc_charlen_type_node,
    9090              :                           TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
    9091          192 :       se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    9092              :                                            gfc_charlen_type_node,
    9093              :                                            dest_word_len, tmp);
    9094              :     }
    9095              :   else
    9096              :     {
    9097         3114 :       tmpdecl = gfc_create_var (mold_type, "transfer");
    9098              : 
    9099         3114 :       ptr = convert (build_pointer_type (mold_type), source);
    9100              : 
    9101              :       /* For CLASS results, allocate the needed memory first.  */
    9102         3114 :       if (mold_expr->ts.type == BT_CLASS)
    9103              :         {
    9104            6 :           tree cdata;
    9105            6 :           cdata = gfc_class_data_get (tmpdecl);
    9106            6 :           tmp = gfc_call_malloc (&se->pre, TREE_TYPE (cdata), dest_word_len);
    9107            6 :           gfc_add_modify (&se->pre, cdata, tmp);
    9108              :         }
    9109              : 
    9110              :       /* Use memcpy to do the transfer.  */
    9111         3114 :       if (mold_expr->ts.type == BT_CLASS)
    9112            6 :         tmp = gfc_class_data_get (tmpdecl);
    9113              :       else
    9114         3108 :         tmp = gfc_build_addr_expr (NULL_TREE, tmpdecl);
    9115              : 
    9116         3114 :       tmp = build_call_expr_loc (input_location,
    9117              :                              builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
    9118              :                              fold_convert (pvoid_type_node, tmp),
    9119              :                              fold_convert (pvoid_type_node, ptr),
    9120              :                              fold_convert (size_type_node, extent));
    9121         3114 :       gfc_add_expr_to_block (&se->pre, tmp);
    9122              : 
    9123              :       /* For CLASS results, set the _vptr.  */
    9124         3114 :       if (mold_expr->ts.type == BT_CLASS)
    9125            6 :         gfc_reset_vptr (&se->pre, nullptr, tmpdecl, source_expr->ts.u.derived);
    9126              : 
    9127         3114 :       se->expr = tmpdecl;
    9128              :     }
    9129              : }
    9130              : 
    9131              : 
    9132              : /* Generate code for the ALLOCATED intrinsic.
    9133              :    Generate inline code that directly check the address of the argument.  */
    9134              : 
    9135              : static void
    9136         7379 : gfc_conv_allocated (gfc_se *se, gfc_expr *expr)
    9137              : {
    9138         7379 :   gfc_se arg1se;
    9139         7379 :   tree tmp;
    9140         7379 :   gfc_expr *e = expr->value.function.actual->expr;
    9141              : 
    9142         7379 :   gfc_init_se (&arg1se, NULL);
    9143         7379 :   if (e->ts.type == BT_CLASS)
    9144              :     {
    9145              :       /* Make sure that class array expressions have both a _data
    9146              :          component reference and an array reference....  */
    9147          899 :       if (CLASS_DATA (e)->attr.dimension)
    9148          418 :         gfc_add_class_array_ref (e);
    9149              :       /* .... whilst scalars only need the _data component.  */
    9150              :       else
    9151          481 :         gfc_add_data_component (e);
    9152              :     }
    9153              : 
    9154         7379 :   gcc_assert (flag_coarray != GFC_FCOARRAY_LIB || !gfc_is_coindexed (e));
    9155              : 
    9156         7379 :   if (e->rank == 0)
    9157              :     {
    9158              :       /* Allocatable scalar.  */
    9159         2876 :       arg1se.want_pointer = 1;
    9160         2876 :       gfc_conv_expr (&arg1se, e);
    9161         2876 :       tmp = arg1se.expr;
    9162              :     }
    9163              :   else
    9164              :     {
    9165              :       /* Allocatable array.  */
    9166         4503 :       arg1se.descriptor_only = 1;
    9167         4503 :       gfc_conv_expr_descriptor (&arg1se, e);
    9168         4503 :       tmp = gfc_conv_descriptor_data_get (arg1se.expr);
    9169              :     }
    9170              : 
    9171         7379 :   tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
    9172         7379 :                          fold_convert (TREE_TYPE (tmp), null_pointer_node));
    9173              : 
    9174              :   /* Components of pointer array references sometimes come back with a pre block.  */
    9175         7379 :   if (arg1se.pre.head)
    9176          327 :     gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9177              : 
    9178         7379 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
    9179         7379 : }
    9180              : 
    9181              : 
    9182              : /* Generate code for the ASSOCIATED intrinsic.
    9183              :    If both POINTER and TARGET are arrays, generate a call to library function
    9184              :    _gfor_associated, and pass descriptors of POINTER and TARGET to it.
    9185              :    In other cases, generate inline code that directly compare the address of
    9186              :    POINTER with the address of TARGET.  */
    9187              : 
    9188              : static void
    9189         9478 : gfc_conv_associated (gfc_se *se, gfc_expr *expr)
    9190              : {
    9191         9478 :   gfc_actual_arglist *arg1;
    9192         9478 :   gfc_actual_arglist *arg2;
    9193         9478 :   gfc_se arg1se;
    9194         9478 :   gfc_se arg2se;
    9195         9478 :   tree tmp2;
    9196         9478 :   tree tmp;
    9197         9478 :   tree nonzero_arraylen = NULL_TREE;
    9198         9478 :   gfc_ss *ss;
    9199         9478 :   bool scalar;
    9200              : 
    9201         9478 :   gfc_init_se (&arg1se, NULL);
    9202         9478 :   gfc_init_se (&arg2se, NULL);
    9203         9478 :   arg1 = expr->value.function.actual;
    9204         9478 :   arg2 = arg1->next;
    9205              : 
    9206              :   /* Check whether the expression is a scalar or not; we cannot use
    9207              :      arg1->expr->rank as it can be nonzero for proc pointers.  */
    9208         9478 :   ss = gfc_walk_expr (arg1->expr);
    9209         9478 :   scalar = ss == gfc_ss_terminator;
    9210         9478 :   if (!scalar)
    9211         3913 :     gfc_free_ss_chain (ss);
    9212              : 
    9213         9478 :   if (!arg2->expr)
    9214              :     {
    9215              :       /* No optional target.  */
    9216         7101 :       if (scalar)
    9217              :         {
    9218              :           /* A pointer to a scalar.  */
    9219         4640 :           arg1se.want_pointer = 1;
    9220         4640 :           gfc_conv_expr (&arg1se, arg1->expr);
    9221         4640 :           if (arg1->expr->symtree->n.sym->attr.proc_pointer
    9222          185 :               && arg1->expr->symtree->n.sym->attr.dummy)
    9223           78 :             arg1se.expr = build_fold_indirect_ref_loc (input_location,
    9224              :                                                        arg1se.expr);
    9225         4640 :           if (arg1->expr->ts.type == BT_CLASS)
    9226              :             {
    9227          390 :               tmp2 = gfc_class_data_get (arg1se.expr);
    9228          390 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
    9229            0 :                 tmp2 = gfc_conv_descriptor_data_get (tmp2);
    9230              :             }
    9231              :           else
    9232         4250 :             tmp2 = arg1se.expr;
    9233              :         }
    9234              :       else
    9235              :         {
    9236              :           /* A pointer to an array.  */
    9237         2461 :           gfc_conv_expr_descriptor (&arg1se, arg1->expr);
    9238         2461 :           tmp2 = gfc_conv_descriptor_data_get (arg1se.expr);
    9239              :         }
    9240         7101 :       gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9241         7101 :       gfc_add_block_to_block (&se->post, &arg1se.post);
    9242         7101 :       tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp2,
    9243         7101 :                              fold_convert (TREE_TYPE (tmp2), null_pointer_node));
    9244         7101 :       se->expr = tmp;
    9245              :     }
    9246              :   else
    9247              :     {
    9248              :       /* An optional target.  */
    9249         2377 :       if (arg2->expr->ts.type == BT_CLASS
    9250           30 :           && arg2->expr->expr_type != EXPR_FUNCTION)
    9251           24 :         gfc_add_data_component (arg2->expr);
    9252              : 
    9253         2377 :       if (scalar)
    9254              :         {
    9255              :           /* A pointer to a scalar.  */
    9256          925 :           arg1se.want_pointer = 1;
    9257          925 :           gfc_conv_expr (&arg1se, arg1->expr);
    9258          925 :           if (arg1->expr->symtree->n.sym->attr.proc_pointer
    9259          128 :               && arg1->expr->symtree->n.sym->attr.dummy)
    9260           42 :             arg1se.expr = build_fold_indirect_ref_loc (input_location,
    9261              :                                                        arg1se.expr);
    9262          925 :           if (arg1->expr->ts.type == BT_CLASS)
    9263          252 :             arg1se.expr = gfc_class_data_get (arg1se.expr);
    9264              : 
    9265          925 :           arg2se.want_pointer = 1;
    9266          925 :           gfc_conv_expr (&arg2se, arg2->expr);
    9267          925 :           if (arg2->expr->symtree->n.sym->attr.proc_pointer
    9268           36 :               && arg2->expr->symtree->n.sym->attr.dummy)
    9269            0 :             arg2se.expr = build_fold_indirect_ref_loc (input_location,
    9270              :                                                        arg2se.expr);
    9271          925 :           if (arg2->expr->ts.type == BT_CLASS)
    9272              :             {
    9273            6 :               arg2se.expr = gfc_evaluate_now (arg2se.expr, &arg2se.pre);
    9274            6 :               arg2se.expr = gfc_class_data_get (arg2se.expr);
    9275              :             }
    9276          925 :           gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9277          925 :           gfc_add_block_to_block (&se->post, &arg1se.post);
    9278          925 :           gfc_add_block_to_block (&se->pre, &arg2se.pre);
    9279          925 :           gfc_add_block_to_block (&se->post, &arg2se.post);
    9280          925 :           tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    9281              :                                  arg1se.expr, arg2se.expr);
    9282          925 :           tmp2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9283              :                                   arg1se.expr, null_pointer_node);
    9284          925 :           se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    9285              :                                       logical_type_node, tmp, tmp2);
    9286              :         }
    9287              :       else
    9288              :         {
    9289              :           /* An array pointer of zero length is not associated if target is
    9290              :              present.  */
    9291         1452 :           arg1se.descriptor_only = 1;
    9292         1452 :           gfc_conv_expr_lhs (&arg1se, arg1->expr);
    9293         1452 :           if (arg1->expr->rank == -1)
    9294              :             {
    9295           84 :               tmp = gfc_conv_descriptor_rank (arg1se.expr);
    9296          168 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    9297           84 :                                      TREE_TYPE (tmp), tmp,
    9298           84 :                                      build_int_cst (TREE_TYPE (tmp), 1));
    9299              :             }
    9300              :           else
    9301         1368 :             tmp = gfc_rank_cst[arg1->expr->rank - 1];
    9302         1452 :           tmp = gfc_conv_descriptor_stride_get (arg1se.expr, tmp);
    9303         1452 :           if (arg2->expr->rank != 0)
    9304         1422 :             nonzero_arraylen = fold_build2_loc (input_location, NE_EXPR,
    9305              :                                                 logical_type_node, tmp,
    9306         1422 :                                                 build_int_cst (TREE_TYPE (tmp), 0));
    9307              : 
    9308              :           /* A pointer to an array, call library function _gfor_associated.  */
    9309         1452 :           arg1se.want_pointer = 1;
    9310         1452 :           gfc_conv_expr_descriptor (&arg1se, arg1->expr);
    9311         1452 :           gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9312         1452 :           gfc_add_block_to_block (&se->post, &arg1se.post);
    9313              : 
    9314         1452 :           arg2se.want_pointer = 1;
    9315         1452 :           arg2se.force_no_tmp = 1;
    9316         1452 :           if (arg2->expr->rank != 0)
    9317         1422 :             gfc_conv_expr_descriptor (&arg2se, arg2->expr);
    9318              :           else
    9319              :             {
    9320           30 :               gfc_conv_expr (&arg2se, arg2->expr);
    9321           30 :               arg2se.expr
    9322           30 :                 = gfc_conv_scalar_to_descriptor (&arg2se, arg2se.expr,
    9323           30 :                                                  gfc_expr_attr (arg2->expr));
    9324           30 :               arg2se.expr = gfc_build_addr_expr (NULL_TREE, arg2se.expr);
    9325              :             }
    9326         1452 :           gfc_add_block_to_block (&se->pre, &arg2se.pre);
    9327         1452 :           gfc_add_block_to_block (&se->post, &arg2se.post);
    9328         1452 :           se->expr = build_call_expr_loc (input_location,
    9329              :                                       gfor_fndecl_associated, 2,
    9330              :                                       arg1se.expr, arg2se.expr);
    9331         1452 :           se->expr = convert (logical_type_node, se->expr);
    9332         1452 :           if (arg2->expr->rank != 0)
    9333         1422 :             se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    9334              :                                         logical_type_node, se->expr,
    9335              :                                         nonzero_arraylen);
    9336              :         }
    9337              : 
    9338              :       /* If target is present zero character length pointers cannot
    9339              :          be associated.  */
    9340         2377 :       if (arg1->expr->ts.type == BT_CHARACTER)
    9341              :         {
    9342          631 :           tmp = arg1se.string_length;
    9343          631 :           tmp = fold_build2_loc (input_location, NE_EXPR,
    9344              :                                  logical_type_node, tmp,
    9345          631 :                                  build_zero_cst (TREE_TYPE (tmp)));
    9346          631 :           se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    9347              :                                       logical_type_node, se->expr, tmp);
    9348              :         }
    9349              :     }
    9350              : 
    9351         9478 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    9352         9478 : }
    9353              : 
    9354              : 
    9355              : /* Generate code for the SAME_TYPE_AS intrinsic.
    9356              :    Generate inline code that directly checks the vindices.  */
    9357              : 
    9358              : static void
    9359          409 : gfc_conv_same_type_as (gfc_se *se, gfc_expr *expr)
    9360              : {
    9361          409 :   gfc_expr *a, *b;
    9362          409 :   gfc_se se1, se2;
    9363          409 :   tree tmp;
    9364          409 :   tree conda = NULL_TREE, condb = NULL_TREE;
    9365              : 
    9366          409 :   gfc_init_se (&se1, NULL);
    9367          409 :   gfc_init_se (&se2, NULL);
    9368              : 
    9369          409 :   a = expr->value.function.actual->expr;
    9370          409 :   b = expr->value.function.actual->next->expr;
    9371              : 
    9372          409 :   bool unlimited_poly_a = UNLIMITED_POLY (a);
    9373          409 :   bool unlimited_poly_b = UNLIMITED_POLY (b);
    9374          409 :   if (unlimited_poly_a)
    9375              :     {
    9376          111 :       se1.want_pointer = 1;
    9377          111 :       gfc_add_vptr_component (a);
    9378              :     }
    9379          298 :   else if (a->ts.type == BT_CLASS)
    9380              :     {
    9381          256 :       gfc_add_vptr_component (a);
    9382          256 :       gfc_add_hash_component (a);
    9383              :     }
    9384           42 :   else if (a->ts.type == BT_DERIVED)
    9385           42 :     a = gfc_get_int_expr (gfc_default_integer_kind, NULL,
    9386           42 :                           a->ts.u.derived->hash_value);
    9387              : 
    9388          409 :   if (unlimited_poly_b)
    9389              :     {
    9390           72 :       se2.want_pointer = 1;
    9391           72 :       gfc_add_vptr_component (b);
    9392              :     }
    9393          337 :   else if (b->ts.type == BT_CLASS)
    9394              :     {
    9395          169 :       gfc_add_vptr_component (b);
    9396          169 :       gfc_add_hash_component (b);
    9397              :     }
    9398          168 :   else if (b->ts.type == BT_DERIVED)
    9399          168 :     b = gfc_get_int_expr (gfc_default_integer_kind, NULL,
    9400          168 :                           b->ts.u.derived->hash_value);
    9401              : 
    9402          409 :   gfc_conv_expr (&se1, a);
    9403          409 :   gfc_conv_expr (&se2, b);
    9404              : 
    9405          409 :   if (unlimited_poly_a)
    9406              :     {
    9407          111 :       conda = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9408              :                                se1.expr,
    9409          111 :                                build_int_cst (TREE_TYPE (se1.expr), 0));
    9410          111 :       se1.expr = gfc_vptr_hash_get (se1.expr);
    9411              :     }
    9412              : 
    9413          409 :   if (unlimited_poly_b)
    9414              :     {
    9415           72 :       condb = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9416              :                                se2.expr,
    9417           72 :                                build_int_cst (TREE_TYPE (se2.expr), 0));
    9418           72 :       se2.expr = gfc_vptr_hash_get (se2.expr);
    9419              :     }
    9420              : 
    9421          409 :   tmp = fold_build2_loc (input_location, EQ_EXPR,
    9422              :                          logical_type_node, se1.expr,
    9423          409 :                          fold_convert (TREE_TYPE (se1.expr), se2.expr));
    9424              : 
    9425          409 :   if (conda)
    9426          111 :     tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9427              :                            logical_type_node, conda, tmp);
    9428              : 
    9429          409 :   if (condb)
    9430           72 :     tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9431              :                            logical_type_node, condb, tmp);
    9432              : 
    9433          409 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
    9434          409 : }
    9435              : 
    9436              : 
    9437              : /* Generate code for SELECTED_CHAR_KIND (NAME) intrinsic function.  */
    9438              : 
    9439              : static void
    9440           42 : gfc_conv_intrinsic_sc_kind (gfc_se *se, gfc_expr *expr)
    9441              : {
    9442           42 :   tree args[2];
    9443              : 
    9444           42 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    9445           42 :   se->expr = build_call_expr_loc (input_location,
    9446              :                               gfor_fndecl_sc_kind, 2, args[0], args[1]);
    9447           42 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    9448           42 : }
    9449              : 
    9450              : 
    9451              : /* Generate code for SELECTED_INT_KIND (R) intrinsic function.  */
    9452              : 
    9453              : static void
    9454           45 : gfc_conv_intrinsic_si_kind (gfc_se *se, gfc_expr *expr)
    9455              : {
    9456           45 :   tree arg, type;
    9457              : 
    9458           45 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    9459              : 
    9460              :   /* The argument to SELECTED_INT_KIND is INTEGER(4).  */
    9461           45 :   type = gfc_get_int_type (4);
    9462           45 :   arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
    9463              : 
    9464              :   /* Convert it to the required type.  */
    9465           45 :   type = gfc_typenode_for_spec (&expr->ts);
    9466           45 :   se->expr = build_call_expr_loc (input_location,
    9467              :                               gfor_fndecl_si_kind, 1, arg);
    9468           45 :   se->expr = fold_convert (type, se->expr);
    9469           45 : }
    9470              : 
    9471              : 
    9472              : /* Generate code for SELECTED_LOGICAL_KIND (BITS) intrinsic function.  */
    9473              : 
    9474              : static void
    9475            6 : gfc_conv_intrinsic_sl_kind (gfc_se *se, gfc_expr *expr)
    9476              : {
    9477            6 :   tree arg, type;
    9478              : 
    9479            6 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    9480              : 
    9481              :   /* The argument to SELECTED_LOGICAL_KIND is INTEGER(4).  */
    9482            6 :   type = gfc_get_int_type (4);
    9483            6 :   arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
    9484              : 
    9485              :   /* Convert it to the required type.  */
    9486            6 :   type = gfc_typenode_for_spec (&expr->ts);
    9487            6 :   se->expr = build_call_expr_loc (input_location,
    9488              :                               gfor_fndecl_sl_kind, 1, arg);
    9489            6 :   se->expr = fold_convert (type, se->expr);
    9490            6 : }
    9491              : 
    9492              : 
    9493              : /* Generate code for SELECTED_REAL_KIND (P, R, RADIX) intrinsic function.  */
    9494              : 
    9495              : static void
    9496           82 : gfc_conv_intrinsic_sr_kind (gfc_se *se, gfc_expr *expr)
    9497              : {
    9498           82 :   gfc_actual_arglist *actual;
    9499           82 :   tree type;
    9500           82 :   gfc_se argse;
    9501           82 :   vec<tree, va_gc> *args = NULL;
    9502              : 
    9503          328 :   for (actual = expr->value.function.actual; actual; actual = actual->next)
    9504              :     {
    9505          246 :       gfc_init_se (&argse, se);
    9506              : 
    9507              :       /* Pass a NULL pointer for an absent arg.  */
    9508          246 :       if (actual->expr == NULL)
    9509           96 :         argse.expr = null_pointer_node;
    9510              :       else
    9511              :         {
    9512          150 :           gfc_typespec ts;
    9513          150 :           gfc_clear_ts (&ts);
    9514              : 
    9515          150 :           if (actual->expr->ts.kind != gfc_c_int_kind)
    9516              :             {
    9517              :               /* The arguments to SELECTED_REAL_KIND are INTEGER(4).  */
    9518            0 :               ts.type = BT_INTEGER;
    9519            0 :               ts.kind = gfc_c_int_kind;
    9520            0 :               gfc_convert_type (actual->expr, &ts, 2);
    9521              :             }
    9522          150 :           gfc_conv_expr_reference (&argse, actual->expr);
    9523              :         }
    9524              : 
    9525          246 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    9526          246 :       gfc_add_block_to_block (&se->post, &argse.post);
    9527          246 :       vec_safe_push (args, argse.expr);
    9528              :     }
    9529              : 
    9530              :   /* Convert it to the required type.  */
    9531           82 :   type = gfc_typenode_for_spec (&expr->ts);
    9532           82 :   se->expr = build_call_expr_loc_vec (input_location,
    9533              :                                       gfor_fndecl_sr_kind, args);
    9534           82 :   se->expr = fold_convert (type, se->expr);
    9535           82 : }
    9536              : 
    9537              : 
    9538              : /* Generate code for TRIM (A) intrinsic function.  */
    9539              : 
    9540              : static void
    9541          578 : gfc_conv_intrinsic_trim (gfc_se * se, gfc_expr * expr)
    9542              : {
    9543          578 :   tree var;
    9544          578 :   tree len;
    9545          578 :   tree addr;
    9546          578 :   tree tmp;
    9547          578 :   tree cond;
    9548          578 :   tree fndecl;
    9549          578 :   tree function;
    9550          578 :   tree *args;
    9551          578 :   unsigned int num_args;
    9552              : 
    9553          578 :   num_args = gfc_intrinsic_argument_list_length (expr) + 2;
    9554          578 :   args = XALLOCAVEC (tree, num_args);
    9555              : 
    9556          578 :   var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
    9557          578 :   addr = gfc_build_addr_expr (ppvoid_type_node, var);
    9558          578 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    9559              : 
    9560          578 :   gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
    9561          578 :   args[0] = gfc_build_addr_expr (NULL_TREE, len);
    9562          578 :   args[1] = addr;
    9563              : 
    9564          578 :   if (expr->ts.kind == 1)
    9565          546 :     function = gfor_fndecl_string_trim;
    9566           32 :   else if (expr->ts.kind == 4)
    9567           32 :     function = gfor_fndecl_string_trim_char4;
    9568              :   else
    9569            0 :     gcc_unreachable ();
    9570              : 
    9571          578 :   fndecl = build_addr (function);
    9572          578 :   tmp = build_call_array_loc (input_location,
    9573          578 :                           TREE_TYPE (TREE_TYPE (function)), fndecl,
    9574              :                           num_args, args);
    9575          578 :   gfc_add_expr_to_block (&se->pre, tmp);
    9576              : 
    9577              :   /* Free the temporary afterwards, if necessary.  */
    9578          578 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    9579          578 :                           len, build_int_cst (TREE_TYPE (len), 0));
    9580          578 :   tmp = gfc_call_free (var);
    9581          578 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    9582          578 :   gfc_add_expr_to_block (&se->post, tmp);
    9583              : 
    9584          578 :   se->expr = var;
    9585          578 :   se->string_length = len;
    9586          578 : }
    9587              : 
    9588              : 
    9589              : /* Generate code for REPEAT (STRING, NCOPIES) intrinsic function.  */
    9590              : 
    9591              : static void
    9592          529 : gfc_conv_intrinsic_repeat (gfc_se * se, gfc_expr * expr)
    9593              : {
    9594          529 :   tree args[3], ncopies, dest, dlen, src, slen, ncopies_type;
    9595          529 :   tree type, cond, tmp, count, exit_label, n, max, largest;
    9596          529 :   tree size;
    9597          529 :   stmtblock_t block, body;
    9598          529 :   int i;
    9599              : 
    9600              :   /* We store in charsize the size of a character.  */
    9601          529 :   i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
    9602          529 :   size = build_int_cst (sizetype, gfc_character_kinds[i].bit_size / 8);
    9603              : 
    9604              :   /* Get the arguments.  */
    9605          529 :   gfc_conv_intrinsic_function_args (se, expr, args, 3);
    9606          529 :   slen = fold_convert (sizetype, gfc_evaluate_now (args[0], &se->pre));
    9607          529 :   src = args[1];
    9608          529 :   ncopies = gfc_evaluate_now (args[2], &se->pre);
    9609          529 :   ncopies_type = TREE_TYPE (ncopies);
    9610              : 
    9611              :   /* Check that NCOPIES is not negative.  */
    9612          529 :   cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, ncopies,
    9613              :                           build_int_cst (ncopies_type, 0));
    9614          529 :   gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    9615              :                            "Argument NCOPIES of REPEAT intrinsic is negative "
    9616              :                            "(its value is %ld)",
    9617              :                            fold_convert (long_integer_type_node, ncopies));
    9618              : 
    9619              :   /* If the source length is zero, any non negative value of NCOPIES
    9620              :      is valid, and nothing happens.  */
    9621          529 :   n = gfc_create_var (ncopies_type, "ncopies");
    9622          529 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
    9623              :                           size_zero_node);
    9624          529 :   tmp = fold_build3_loc (input_location, COND_EXPR, ncopies_type, cond,
    9625              :                          build_int_cst (ncopies_type, 0), ncopies);
    9626          529 :   gfc_add_modify (&se->pre, n, tmp);
    9627          529 :   ncopies = n;
    9628              : 
    9629              :   /* Check that ncopies is not too large: ncopies should be less than
    9630              :      (or equal to) MAX / slen, where MAX is the maximal integer of
    9631              :      the gfc_charlen_type_node type.  If slen == 0, we need a special
    9632              :      case to avoid the division by zero.  */
    9633          529 :   max = fold_build2_loc (input_location, TRUNC_DIV_EXPR, sizetype,
    9634          529 :                          fold_convert (sizetype,
    9635              :                                        TYPE_MAX_VALUE (gfc_charlen_type_node)),
    9636              :                          slen);
    9637         1054 :   largest = TYPE_PRECISION (sizetype) > TYPE_PRECISION (ncopies_type)
    9638          529 :               ? sizetype : ncopies_type;
    9639          529 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    9640              :                           fold_convert (largest, ncopies),
    9641              :                           fold_convert (largest, max));
    9642          529 :   tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
    9643              :                          size_zero_node);
    9644          529 :   cond = fold_build3_loc (input_location, COND_EXPR, logical_type_node, tmp,
    9645              :                           logical_false_node, cond);
    9646          529 :   gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    9647              :                            "Argument NCOPIES of REPEAT intrinsic is too large");
    9648              : 
    9649              :   /* Compute the destination length.  */
    9650          529 :   dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
    9651              :                           fold_convert (gfc_charlen_type_node, slen),
    9652              :                           fold_convert (gfc_charlen_type_node, ncopies));
    9653          529 :   type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
    9654          529 :   dest = gfc_conv_string_tmp (se, build_pointer_type (type), dlen);
    9655              : 
    9656              :   /* Generate the code to do the repeat operation:
    9657              :        for (i = 0; i < ncopies; i++)
    9658              :          memmove (dest + (i * slen * size), src, slen*size);  */
    9659          529 :   gfc_start_block (&block);
    9660          529 :   count = gfc_create_var (sizetype, "count");
    9661          529 :   gfc_add_modify (&block, count, size_zero_node);
    9662          529 :   exit_label = gfc_build_label_decl (NULL_TREE);
    9663              : 
    9664              :   /* Start the loop body.  */
    9665          529 :   gfc_start_block (&body);
    9666              : 
    9667              :   /* Exit the loop if count >= ncopies.  */
    9668          529 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, count,
    9669              :                           fold_convert (sizetype, ncopies));
    9670          529 :   tmp = build1_v (GOTO_EXPR, exit_label);
    9671          529 :   TREE_USED (exit_label) = 1;
    9672          529 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    9673              :                          build_empty_stmt (input_location));
    9674          529 :   gfc_add_expr_to_block (&body, tmp);
    9675              : 
    9676              :   /* Call memmove (dest + (i*slen*size), src, slen*size).  */
    9677          529 :   tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, slen,
    9678              :                          count);
    9679          529 :   tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, tmp,
    9680              :                          size);
    9681          529 :   tmp = fold_build_pointer_plus_loc (input_location,
    9682              :                                      fold_convert (pvoid_type_node, dest), tmp);
    9683          529 :   tmp = build_call_expr_loc (input_location,
    9684              :                              builtin_decl_explicit (BUILT_IN_MEMMOVE),
    9685              :                              3, tmp, src,
    9686              :                              fold_build2_loc (input_location, MULT_EXPR,
    9687              :                                               size_type_node, slen, size));
    9688          529 :   gfc_add_expr_to_block (&body, tmp);
    9689              : 
    9690              :   /* Increment count.  */
    9691          529 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, sizetype,
    9692              :                          count, size_one_node);
    9693          529 :   gfc_add_modify (&body, count, tmp);
    9694              : 
    9695              :   /* Build the loop.  */
    9696          529 :   tmp = build1_v (LOOP_EXPR, gfc_finish_block (&body));
    9697          529 :   gfc_add_expr_to_block (&block, tmp);
    9698              : 
    9699              :   /* Add the exit label.  */
    9700          529 :   tmp = build1_v (LABEL_EXPR, exit_label);
    9701          529 :   gfc_add_expr_to_block (&block, tmp);
    9702              : 
    9703              :   /* Finish the block.  */
    9704          529 :   tmp = gfc_finish_block (&block);
    9705          529 :   gfc_add_expr_to_block (&se->pre, tmp);
    9706              : 
    9707              :   /* Set the result value.  */
    9708          529 :   se->expr = dest;
    9709          529 :   se->string_length = dlen;
    9710          529 : }
    9711              : 
    9712              : 
    9713              : /* Generate code for the IARGC intrinsic.  */
    9714              : 
    9715              : static void
    9716           12 : gfc_conv_intrinsic_iargc (gfc_se * se, gfc_expr * expr)
    9717              : {
    9718           12 :   tree tmp;
    9719           12 :   tree fndecl;
    9720           12 :   tree type;
    9721              : 
    9722              :   /* Call the library function.  This always returns an INTEGER(4).  */
    9723           12 :   fndecl = gfor_fndecl_iargc;
    9724           12 :   tmp = build_call_expr_loc (input_location,
    9725              :                          fndecl, 0);
    9726              : 
    9727              :   /* Convert it to the required type.  */
    9728           12 :   type = gfc_typenode_for_spec (&expr->ts);
    9729           12 :   tmp = fold_convert (type, tmp);
    9730              : 
    9731           12 :   se->expr = tmp;
    9732           12 : }
    9733              : 
    9734              : 
    9735              : /* Generate code for the KILL intrinsic.  */
    9736              : 
    9737              : static void
    9738            8 : conv_intrinsic_kill (gfc_se *se, gfc_expr *expr)
    9739              : {
    9740            8 :   tree *args;
    9741            8 :   tree int4_type_node = gfc_get_int_type (4);
    9742            8 :   tree pid;
    9743            8 :   tree sig;
    9744            8 :   tree tmp;
    9745            8 :   unsigned int num_args;
    9746              : 
    9747            8 :   num_args = gfc_intrinsic_argument_list_length (expr);
    9748            8 :   args = XALLOCAVEC (tree, num_args);
    9749            8 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    9750              : 
    9751              :   /* Convert PID to a INTEGER(4) entity.  */
    9752            8 :   pid = convert (int4_type_node, args[0]);
    9753              : 
    9754              :   /* Convert SIG to a INTEGER(4) entity.  */
    9755            8 :   sig = convert (int4_type_node, args[1]);
    9756              : 
    9757            8 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_kill, 2, pid, sig);
    9758              : 
    9759            8 :   se->expr = fold_convert (TREE_TYPE (args[0]), tmp);
    9760            8 : }
    9761              : 
    9762              : 
    9763              : static tree
    9764           15 : conv_intrinsic_kill_sub (gfc_code *code)
    9765              : {
    9766           15 :   stmtblock_t block;
    9767           15 :   gfc_se se, se_stat;
    9768           15 :   tree int4_type_node = gfc_get_int_type (4);
    9769           15 :   tree pid;
    9770           15 :   tree sig;
    9771           15 :   tree statp;
    9772           15 :   tree tmp;
    9773              : 
    9774              :   /* Make the function call.  */
    9775           15 :   gfc_init_block (&block);
    9776           15 :   gfc_init_se (&se, NULL);
    9777              : 
    9778              :   /* Convert PID to a INTEGER(4) entity.  */
    9779           15 :   gfc_conv_expr (&se, code->ext.actual->expr);
    9780           15 :   gfc_add_block_to_block (&block, &se.pre);
    9781           15 :   pid = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
    9782           15 :   gfc_add_block_to_block (&block, &se.post);
    9783              : 
    9784              :   /* Convert SIG to a INTEGER(4) entity.  */
    9785           15 :   gfc_conv_expr (&se, code->ext.actual->next->expr);
    9786           15 :   gfc_add_block_to_block (&block, &se.pre);
    9787           15 :   sig = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
    9788           15 :   gfc_add_block_to_block (&block, &se.post);
    9789              : 
    9790              :   /* Deal with an optional STATUS.  */
    9791           15 :   if (code->ext.actual->next->next->expr)
    9792              :     {
    9793           10 :       gfc_init_se (&se_stat, NULL);
    9794           10 :       gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
    9795           10 :       statp = gfc_create_var (gfc_get_int_type (4), "_statp");
    9796              :     }
    9797              :   else
    9798              :     statp = NULL_TREE;
    9799              : 
    9800           25 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_kill_sub, 3, pid, sig,
    9801           10 :         statp ? gfc_build_addr_expr (NULL_TREE, statp) : null_pointer_node);
    9802              : 
    9803           15 :   gfc_add_expr_to_block (&block, tmp);
    9804              : 
    9805           15 :   if (statp && statp != se_stat.expr)
    9806           10 :     gfc_add_modify (&block, se_stat.expr,
    9807           10 :                     fold_convert (TREE_TYPE (se_stat.expr), statp));
    9808              : 
    9809           15 :   return gfc_finish_block (&block);
    9810              : }
    9811              : 
    9812              : 
    9813              : 
    9814              : /* The loc intrinsic returns the address of its argument as
    9815              :    gfc_index_integer_kind integer.  */
    9816              : 
    9817              : static void
    9818         8816 : gfc_conv_intrinsic_loc (gfc_se * se, gfc_expr * expr)
    9819              : {
    9820         8816 :   tree temp_var;
    9821         8816 :   gfc_expr *arg_expr;
    9822              : 
    9823         8816 :   gcc_assert (!se->ss);
    9824              : 
    9825         8816 :   arg_expr = expr->value.function.actual->expr;
    9826         8816 :   if (arg_expr->rank == 0)
    9827              :     {
    9828         6401 :       if (arg_expr->ts.type == BT_CLASS)
    9829           18 :         gfc_add_data_component (arg_expr);
    9830         6401 :       gfc_conv_expr_reference (se, arg_expr);
    9831              :     }
    9832              :   else
    9833         2415 :     gfc_conv_array_parameter (se, arg_expr, true, NULL, NULL, NULL);
    9834         8816 :   se->expr = convert (gfc_get_int_type (gfc_index_integer_kind), se->expr);
    9835              : 
    9836              :   /* Create a temporary variable for loc return value.  Without this,
    9837              :      we get an error an ICE in gcc/expr.cc(expand_expr_addr_expr_1).  */
    9838         8816 :   temp_var = gfc_create_var (gfc_get_int_type (gfc_index_integer_kind), NULL);
    9839         8816 :   gfc_add_modify (&se->pre, temp_var, se->expr);
    9840         8816 :   se->expr = temp_var;
    9841         8816 : }
    9842              : 
    9843              : 
    9844              : /* Specialized trim for f_c_string.  */
    9845              : 
    9846              : static void
    9847           42 : conv_trim (gfc_se *tse, gfc_se *str)
    9848              : {
    9849           42 :   tree cond, plen, pvar, tlen, ttmp, tvar;
    9850              : 
    9851           42 :   tlen = gfc_create_var (gfc_charlen_type_node, "tlen");
    9852           42 :   plen = gfc_build_addr_expr (NULL_TREE, tlen);
    9853              : 
    9854           42 :   tvar = gfc_create_var (pchar_type_node, "tstr");
    9855           42 :   pvar = gfc_build_addr_expr (ppvoid_type_node, tvar);
    9856              : 
    9857           42 :   ttmp = build_call_expr_loc (input_location, gfor_fndecl_string_trim, 4,
    9858              :                               plen, pvar, str->string_length, str->expr);
    9859              : 
    9860           42 :   gfc_add_expr_to_block (&tse->pre, ttmp);
    9861              : 
    9862              :   /* Free the temporary afterwards, if necessary.  */
    9863           42 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    9864           42 :                           tlen, build_int_cst (TREE_TYPE (tlen), 0));
    9865           42 :   ttmp = gfc_call_free (tvar);
    9866           42 :   ttmp = build3_v (COND_EXPR, cond, ttmp, build_empty_stmt (input_location));
    9867           42 :   gfc_add_expr_to_block (&tse->post, ttmp);
    9868              : 
    9869           42 :   tse->expr = tvar;
    9870           42 :   tse->string_length = tlen;
    9871           42 : }
    9872              : 
    9873              : 
    9874              : /* The following routine generates code for the intrinsic functions from
    9875              :    the ISO_C_BINDING module: C_LOC, C_FUNLOC, C_ASSOCIATED, and
    9876              :    F_C_STRING.  */
    9877              : 
    9878              : static void
    9879         9697 : conv_isocbinding_function (gfc_se *se, gfc_expr *expr)
    9880              : {
    9881         9697 :   gfc_actual_arglist *arg = expr->value.function.actual;
    9882              : 
    9883         9697 :   if (expr->value.function.isym->id == GFC_ISYM_C_LOC)
    9884              :     {
    9885         7341 :       if (arg->expr->rank == 0)
    9886         1993 :         gfc_conv_expr_reference (se, arg->expr);
    9887         5348 :       else if (gfc_is_simply_contiguous (arg->expr, false, false))
    9888         4264 :         gfc_conv_array_parameter (se, arg->expr, true, NULL, NULL, NULL);
    9889              :       else
    9890              :         {
    9891         1084 :           gfc_conv_expr_descriptor (se, arg->expr);
    9892         1084 :           se->expr = gfc_conv_descriptor_data_get (se->expr);
    9893              :         }
    9894              : 
    9895              :       /* TODO -- the following two lines shouldn't be necessary, but if
    9896              :          they're removed, a bug is exposed later in the code path.
    9897              :          This workaround was thus introduced, but will have to be
    9898              :          removed; please see PR 35150 for details about the issue.  */
    9899         7341 :       se->expr = convert (pvoid_type_node, se->expr);
    9900         7341 :       se->expr = gfc_evaluate_now (se->expr, &se->pre);
    9901              :     }
    9902         2356 :   else if (expr->value.function.isym->id == GFC_ISYM_C_FUNLOC)
    9903              :     {
    9904          260 :       gfc_conv_expr_reference (se, arg->expr);
    9905          260 :       if (arg->expr->symtree->n.sym->attr.proc_pointer
    9906           29 :           && arg->expr->symtree->n.sym->attr.dummy)
    9907            7 :         se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    9908              :       /* The code below is necessary to create a reference from the calling
    9909              :          subprogram to the argument of C_FUNLOC() in the call graph.
    9910              :          Please see PR 117303 for more details. */
    9911          260 :       se->expr = convert (pvoid_type_node, se->expr);
    9912          260 :       se->expr = gfc_evaluate_now (se->expr, &se->pre);
    9913              :     }
    9914         2096 :   else if (expr->value.function.isym->id == GFC_ISYM_C_ASSOCIATED)
    9915              :     {
    9916         2054 :       gfc_se arg1se;
    9917         2054 :       gfc_se arg2se;
    9918              : 
    9919              :       /* Build the addr_expr for the first argument.  The argument is
    9920              :          already an *address* so we don't need to set want_pointer in
    9921              :          the gfc_se.  */
    9922         2054 :       gfc_init_se (&arg1se, NULL);
    9923         2054 :       gfc_conv_expr (&arg1se, arg->expr);
    9924         2054 :       gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9925         2054 :       gfc_add_block_to_block (&se->post, &arg1se.post);
    9926              : 
    9927              :       /* See if we were given two arguments.  */
    9928         2054 :       if (arg->next->expr == NULL)
    9929              :         /* Only given one arg so generate a null and do a
    9930              :            not-equal comparison against the first arg.  */
    9931         1675 :         se->expr = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9932              :                                     arg1se.expr,
    9933         1675 :                                     fold_convert (TREE_TYPE (arg1se.expr),
    9934              :                                                   null_pointer_node));
    9935              :       else
    9936              :         {
    9937          379 :           tree eq_expr;
    9938          379 :           tree not_null_expr;
    9939              : 
    9940              :           /* Given two arguments so build the arg2se from second arg.  */
    9941          379 :           gfc_init_se (&arg2se, NULL);
    9942          379 :           gfc_conv_expr (&arg2se, arg->next->expr);
    9943          379 :           gfc_add_block_to_block (&se->pre, &arg2se.pre);
    9944          379 :           gfc_add_block_to_block (&se->post, &arg2se.post);
    9945              : 
    9946              :           /* Generate test to compare that the two args are equal.  */
    9947          379 :           eq_expr = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    9948              :                                      arg1se.expr, arg2se.expr);
    9949              :           /* Generate test to ensure that the first arg is not null.  */
    9950          379 :           not_null_expr = fold_build2_loc (input_location, NE_EXPR,
    9951              :                                            logical_type_node,
    9952              :                                            arg1se.expr, null_pointer_node);
    9953              : 
    9954              :           /* Finally, the generated test must check that both arg1 is not
    9955              :              NULL and that it is equal to the second arg.  */
    9956          379 :           se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    9957              :                                       logical_type_node,
    9958              :                                       not_null_expr, eq_expr);
    9959              :         }
    9960              :     }
    9961           42 :   else if (expr->value.function.isym->id == GFC_ISYM_F_C_STRING)
    9962              :     {
    9963              :       /* There are three cases:
    9964              :          f_c_string(string)          -> trim(string) // c_null_char
    9965              :          f_c_string(string, .false.) -> trim(string) // c_null_char
    9966              :          f_c_string(string, .true.)  -> string       // c_null_char  */
    9967              : 
    9968           42 :       gfc_se lse, rse, tse;
    9969           42 :       tree len, tmp, var;
    9970           42 :       gfc_expr *string = arg->expr;
    9971           42 :       gfc_expr *asis = arg->next->expr;
    9972           42 :       gfc_expr *cnc;
    9973              : 
    9974              :       /* Convert string. */
    9975           42 :       gfc_init_se (&lse, se);
    9976           42 :       gfc_conv_expr (&lse, string);
    9977           42 :       gfc_conv_string_parameter (&lse);
    9978              : 
    9979              :       /* Create a string for C_NULL_CHAR and convert it.  */
    9980           42 :       cnc = gfc_get_character_expr (gfc_default_character_kind,
    9981              :                                     &string->where, "\0", 1);
    9982           42 :       gfc_init_se (&rse, se);
    9983           42 :       gfc_conv_expr (&rse, cnc);
    9984           42 :       gfc_conv_string_parameter (&rse);
    9985           42 :       gfc_free_expr (cnc);
    9986              : 
    9987              : #ifdef cnode
    9988              : #undef cnode
    9989              : #endif
    9990              : #define cnode gfc_charlen_type_node
    9991           42 :       if (asis)
    9992              :         {
    9993           30 :           stmtblock_t block;
    9994           30 :           gfc_se asis_se, vse;
    9995           30 :           tree elen, evar, tlen, tvar;
    9996           30 :           tree else_branch, then_branch;
    9997              : 
    9998           30 :           elen = evar = tlen = tvar = NULL_TREE;
    9999              : 
   10000              :           /* f_c_string(string, .true.) -> string // c_null_char  */
   10001              : 
   10002           30 :           gfc_init_block (&block);
   10003              : 
   10004           30 :           gfc_add_block_to_block (&block, &lse.pre);
   10005           30 :           gfc_add_block_to_block (&block, &rse.pre);
   10006              : 
   10007           30 :           tlen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
   10008              :                                   fold_convert (cnode, lse.string_length),
   10009              :                                   fold_convert (cnode, rse.string_length));
   10010              : 
   10011           30 :           gfc_init_se (&vse, se);
   10012           30 :           tvar = gfc_conv_string_tmp (&vse, pchar_type_node, tlen);
   10013           30 :           gfc_add_block_to_block (&block, &vse.pre);
   10014              : 
   10015           30 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
   10016              :                                      6, tlen, tvar,
   10017              :                                      lse.string_length, lse.expr,
   10018              :                                      rse.string_length, rse.expr);
   10019           30 :           gfc_add_expr_to_block (&block, tmp);
   10020              : 
   10021           30 :           then_branch = gfc_finish_block (&block);
   10022              : 
   10023              :           /* f_c_string(string, .false.) = trim(string) // c_null_char  */
   10024              : 
   10025           30 :           gfc_init_block (&block);
   10026              : 
   10027           30 :           gfc_init_se (&tse, se);
   10028           30 :           conv_trim (&tse, &lse);
   10029           30 :           gfc_add_block_to_block (&block, &tse.pre);
   10030           30 :           gfc_add_block_to_block (&block, &rse.pre);
   10031              : 
   10032           30 :           elen = fold_build2_loc (input_location, PLUS_EXPR, cnode,
   10033              :                                   fold_convert (cnode, tse.string_length),
   10034              :                                   fold_convert (cnode, rse.string_length));
   10035              : 
   10036           30 :           gfc_init_se (&vse, se);
   10037           30 :           evar = gfc_conv_string_tmp (&vse, pchar_type_node, elen);
   10038           30 :           gfc_add_block_to_block (&block, &vse.pre);
   10039              : 
   10040           30 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
   10041              :                                      6, elen, evar,
   10042              :                                      tse.string_length, tse.expr,
   10043              :                                      rse.string_length, rse.expr);
   10044           30 :           gfc_add_expr_to_block (&block, tmp);
   10045              : 
   10046           30 :           else_branch = gfc_finish_block (&block);
   10047              : 
   10048           30 :           gfc_init_se (&asis_se, se);
   10049           30 :           gfc_conv_expr (&asis_se, asis);
   10050           30 :           if (asis->expr_type == EXPR_VARIABLE
   10051           18 :             && asis->symtree->n.sym->attr.dummy
   10052            6 :             && asis->symtree->n.sym->attr.optional)
   10053              :             {
   10054            6 :               tree present = gfc_conv_expr_present (asis->symtree->n.sym);
   10055            6 :               asis_se.expr = build3_loc (input_location, COND_EXPR,
   10056              :                                          logical_type_node, present,
   10057              :                                          asis_se.expr,
   10058              :                                          build_int_cst (logical_type_node, 0));
   10059              :             }
   10060           30 :           gfc_add_block_to_block (&se->pre, &asis_se.pre);
   10061           30 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
   10062              :                                  asis_se.expr, then_branch, else_branch);
   10063              : 
   10064           30 :           gfc_add_expr_to_block (&se->pre, tmp);
   10065              : 
   10066           30 :           var = fold_build3_loc (input_location, COND_EXPR, pchar_type_node,
   10067              :                                  asis_se.expr, tvar, evar);
   10068           30 :           gfc_add_expr_to_block (&se->pre, var);
   10069              : 
   10070           30 :           len = fold_build3_loc (input_location, COND_EXPR, cnode,
   10071              :                                  asis_se.expr, tlen, elen);
   10072           30 :           gfc_add_expr_to_block (&se->pre, len);
   10073              :         }
   10074              :       else
   10075              :         {
   10076              :           /* f_c_string(string) = trim(string) // c_null_char  */
   10077              : 
   10078           12 :           gfc_add_block_to_block (&se->pre, &lse.pre);
   10079           12 :           gfc_add_block_to_block (&se->pre, &rse.pre);
   10080              : 
   10081           12 :           gfc_init_se (&tse, se);
   10082           12 :           conv_trim (&tse, &lse);
   10083           12 :           gfc_add_block_to_block (&se->pre, &tse.pre);
   10084           12 :           gfc_add_block_to_block (&se->post, &tse.post);
   10085              : 
   10086           12 :           len = fold_build2_loc (input_location, PLUS_EXPR, cnode,
   10087              :                                  fold_convert (cnode, tse.string_length),
   10088              :                                  fold_convert (cnode, rse.string_length));
   10089              : 
   10090           12 :           var = gfc_conv_string_tmp (se, pchar_type_node, len);
   10091              : 
   10092           12 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_concat_string,
   10093              :                                      6, len, var,
   10094              :                                      tse.string_length, tse.expr,
   10095              :                                      rse.string_length, rse.expr);
   10096           12 :           gfc_add_expr_to_block (&se->pre, tmp);
   10097              :         }
   10098              : 
   10099           42 :       se->expr = var;
   10100           42 :       se->string_length = len;
   10101              : 
   10102              : #undef cnode
   10103              :     }
   10104              :   else
   10105            0 :     gcc_unreachable ();
   10106         9697 : }
   10107              : 
   10108              : 
   10109              : /* The following routine generates code for the intrinsic
   10110              :    subroutines from the ISO_C_BINDING module:
   10111              :     * C_F_POINTER
   10112              :     * C_F_PROCPOINTER.  */
   10113              : 
   10114              : static tree
   10115         3171 : conv_isocbinding_subroutine (gfc_code *code)
   10116              : {
   10117         3171 :   gfc_expr *cptr, *fptr, *shape, *lower;
   10118         3171 :   gfc_se se, cptrse, fptrse, shapese, lowerse;
   10119         3171 :   gfc_ss *shape_ss, *lower_ss;
   10120         3171 :   tree desc, dim, tmp, stride, offset, lbound, ubound;
   10121         3171 :   stmtblock_t body, block;
   10122         3171 :   gfc_loopinfo loop;
   10123         3171 :   gfc_actual_arglist *arg;
   10124              : 
   10125         3171 :   arg = code->ext.actual;
   10126         3171 :   cptr = arg->expr;
   10127         3171 :   fptr = arg->next->expr;
   10128         3171 :   shape = arg->next->next ? arg->next->next->expr : NULL;
   10129         3089 :   lower = shape && arg->next->next->next ? arg->next->next->next->expr : NULL;
   10130              : 
   10131         3171 :   gfc_init_se (&se, NULL);
   10132         3171 :   gfc_init_se (&cptrse, NULL);
   10133         3171 :   gfc_conv_expr (&cptrse, cptr);
   10134         3171 :   gfc_add_block_to_block (&se.pre, &cptrse.pre);
   10135         3171 :   gfc_add_block_to_block (&se.post, &cptrse.post);
   10136              : 
   10137         3171 :   gfc_init_se (&fptrse, NULL);
   10138         3171 :   if (fptr->rank == 0)
   10139              :     {
   10140         2686 :       fptrse.want_pointer = 1;
   10141         2686 :       gfc_conv_expr (&fptrse, fptr);
   10142         2686 :       gfc_add_block_to_block (&se.pre, &fptrse.pre);
   10143         2686 :       gfc_add_block_to_block (&se.post, &fptrse.post);
   10144         2686 :       if (fptr->symtree->n.sym->attr.proc_pointer
   10145           81 :           && fptr->symtree->n.sym->attr.dummy)
   10146           19 :         fptrse.expr = build_fold_indirect_ref_loc (input_location, fptrse.expr);
   10147         2686 :       se.expr
   10148         2686 :         = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (fptrse.expr),
   10149              :                            fptrse.expr,
   10150         2686 :                            fold_convert (TREE_TYPE (fptrse.expr), cptrse.expr));
   10151         2686 :       gfc_add_expr_to_block (&se.pre, se.expr);
   10152         2686 :       gfc_add_block_to_block (&se.pre, &se.post);
   10153         2686 :       return gfc_finish_block (&se.pre);
   10154              :     }
   10155              : 
   10156          485 :   gfc_start_block (&block);
   10157              : 
   10158              :   /* Get the descriptor of the Fortran pointer.  */
   10159          485 :   fptrse.descriptor_only = 1;
   10160          485 :   gfc_conv_expr_descriptor (&fptrse, fptr);
   10161          485 :   gfc_add_block_to_block (&block, &fptrse.pre);
   10162          485 :   desc = fptrse.expr;
   10163              : 
   10164              :   /* Set the span field.  */
   10165          485 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
   10166          485 :   tmp = fold_convert (gfc_array_index_type, tmp);
   10167          485 :   gfc_conv_descriptor_span_set (&block, desc, tmp);
   10168              : 
   10169              :   /* Set data value, dtype, and offset.  */
   10170          485 :   tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc));
   10171          485 :   gfc_conv_descriptor_data_set (&block, desc, fold_convert (tmp, cptrse.expr));
   10172          485 :   gfc_add_modify (&block, gfc_conv_descriptor_dtype (desc),
   10173          485 :                   gfc_get_dtype (TREE_TYPE (desc)));
   10174              : 
   10175              :   /* Start scalarization of the bounds, using the shape argument.  */
   10176              : 
   10177          485 :   shape_ss = gfc_walk_expr (shape);
   10178          485 :   gcc_assert (shape_ss != gfc_ss_terminator);
   10179          485 :   gfc_init_se (&shapese, NULL);
   10180          485 :   if (lower)
   10181              :     {
   10182           12 :       lower_ss = gfc_walk_expr (lower);
   10183           12 :       gcc_assert (lower_ss != gfc_ss_terminator);
   10184           12 :       gfc_init_se (&lowerse, NULL);
   10185              :     }
   10186              : 
   10187          485 :   gfc_init_loopinfo (&loop);
   10188          485 :   gfc_add_ss_to_loop (&loop, shape_ss);
   10189          485 :   if (lower)
   10190           12 :     gfc_add_ss_to_loop (&loop, lower_ss);
   10191          485 :   gfc_conv_ss_startstride (&loop);
   10192          485 :   gfc_conv_loop_setup (&loop, &fptr->where);
   10193          485 :   gfc_mark_ss_chain_used (shape_ss, 1);
   10194          485 :   if (lower)
   10195           12 :     gfc_mark_ss_chain_used (lower_ss, 1);
   10196              : 
   10197          485 :   gfc_copy_loopinfo_to_se (&shapese, &loop);
   10198          485 :   shapese.ss = shape_ss;
   10199          485 :   if (lower)
   10200              :     {
   10201           12 :       gfc_copy_loopinfo_to_se (&lowerse, &loop);
   10202           12 :       lowerse.ss = lower_ss;
   10203              :     }
   10204              : 
   10205          485 :   stride = gfc_create_var (gfc_array_index_type, "stride");
   10206          485 :   offset = gfc_create_var (gfc_array_index_type, "offset");
   10207          485 :   gfc_add_modify (&block, stride, gfc_index_one_node);
   10208          485 :   gfc_add_modify (&block, offset, gfc_index_zero_node);
   10209              : 
   10210              :   /* Loop body.  */
   10211          485 :   gfc_start_scalarized_body (&loop, &body);
   10212              : 
   10213          485 :   dim = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
   10214              :                          loop.loopvar[0], loop.from[0]);
   10215              : 
   10216          485 :   if (lower)
   10217              :     {
   10218           12 :       gfc_conv_expr (&lowerse, lower);
   10219           12 :       gfc_add_block_to_block (&body, &lowerse.pre);
   10220           12 :       lbound = fold_convert (gfc_array_index_type, lowerse.expr);
   10221           12 :       gfc_add_block_to_block (&body, &lowerse.post);
   10222              :     }
   10223              :   else
   10224          473 :     lbound = gfc_index_one_node;
   10225              : 
   10226              :   /* Set bounds and stride.  */
   10227          485 :   gfc_conv_descriptor_lbound_set (&body, desc, dim, lbound);
   10228          485 :   gfc_conv_descriptor_stride_set (&body, desc, dim, stride);
   10229              : 
   10230          485 :   gfc_conv_expr (&shapese, shape);
   10231          485 :   gfc_add_block_to_block (&body, &shapese.pre);
   10232          485 :   ubound = fold_build2_loc (
   10233              :     input_location, MINUS_EXPR, gfc_array_index_type,
   10234              :     fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, lbound,
   10235              :                      fold_convert (gfc_array_index_type, shapese.expr)),
   10236              :     gfc_index_one_node);
   10237          485 :   gfc_conv_descriptor_ubound_set (&body, desc, dim, ubound);
   10238          485 :   gfc_add_block_to_block (&body, &shapese.post);
   10239              : 
   10240              :   /* Calculate offset.  */
   10241          485 :   tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   10242              :                          stride, lbound);
   10243          485 :   gfc_add_modify (&body, offset,
   10244              :                   fold_build2_loc (input_location, PLUS_EXPR,
   10245              :                                    gfc_array_index_type, offset, tmp));
   10246              : 
   10247              :   /* Update stride.  */
   10248          485 :   gfc_add_modify (
   10249              :     &body, stride,
   10250              :     fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, stride,
   10251              :                      fold_convert (gfc_array_index_type, shapese.expr)));
   10252              :   /* Finish scalarization loop.  */
   10253          485 :   gfc_trans_scalarizing_loops (&loop, &body);
   10254          485 :   gfc_add_block_to_block (&block, &loop.pre);
   10255          485 :   gfc_add_block_to_block (&block, &loop.post);
   10256          485 :   gfc_add_block_to_block (&block, &fptrse.post);
   10257          485 :   gfc_cleanup_loop (&loop);
   10258              : 
   10259          485 :   gfc_add_modify (&block, offset,
   10260              :                   fold_build1_loc (input_location, NEGATE_EXPR,
   10261              :                                    gfc_array_index_type, offset));
   10262          485 :   gfc_conv_descriptor_offset_set (&block, desc, offset);
   10263              : 
   10264          485 :   gfc_add_expr_to_block (&se.pre, gfc_finish_block (&block));
   10265          485 :   gfc_add_block_to_block (&se.pre, &se.post);
   10266          485 :   return gfc_finish_block (&se.pre);
   10267              : }
   10268              : 
   10269              : 
   10270              : /* Save and restore floating-point state.  */
   10271              : 
   10272              : tree
   10273          943 : gfc_save_fp_state (stmtblock_t *block)
   10274              : {
   10275          943 :   tree type, fpstate, tmp;
   10276              : 
   10277          943 :   type = build_array_type (char_type_node,
   10278              :                            build_range_type (size_type_node, size_zero_node,
   10279              :                                              size_int (GFC_FPE_STATE_BUFFER_SIZE)));
   10280          943 :   fpstate = gfc_create_var (type, "fpstate");
   10281          943 :   fpstate = gfc_build_addr_expr (pvoid_type_node, fpstate);
   10282              : 
   10283          943 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_entry,
   10284              :                              1, fpstate);
   10285          943 :   gfc_add_expr_to_block (block, tmp);
   10286              : 
   10287          943 :   return fpstate;
   10288              : }
   10289              : 
   10290              : 
   10291              : void
   10292          943 : gfc_restore_fp_state (stmtblock_t *block, tree fpstate)
   10293              : {
   10294          943 :   tree tmp;
   10295              : 
   10296          943 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_exit,
   10297              :                              1, fpstate);
   10298          943 :   gfc_add_expr_to_block (block, tmp);
   10299          943 : }
   10300              : 
   10301              : 
   10302              : /* Generate code for arguments of IEEE functions.  */
   10303              : 
   10304              : static void
   10305        12457 : conv_ieee_function_args (gfc_se *se, gfc_expr *expr, tree *argarray,
   10306              :                          int nargs)
   10307              : {
   10308        12457 :   gfc_actual_arglist *actual;
   10309        12457 :   gfc_expr *e;
   10310        12457 :   gfc_se argse;
   10311        12457 :   int arg;
   10312              : 
   10313        12457 :   actual = expr->value.function.actual;
   10314        34461 :   for (arg = 0; arg < nargs; arg++, actual = actual->next)
   10315              :     {
   10316        22004 :       gcc_assert (actual);
   10317        22004 :       e = actual->expr;
   10318              : 
   10319        22004 :       gfc_init_se (&argse, se);
   10320        22004 :       gfc_conv_expr_val (&argse, e);
   10321              : 
   10322        22004 :       gfc_add_block_to_block (&se->pre, &argse.pre);
   10323        22004 :       gfc_add_block_to_block (&se->post, &argse.post);
   10324        22004 :       argarray[arg] = argse.expr;
   10325              :     }
   10326        12457 : }
   10327              : 
   10328              : 
   10329              : /* Generate code for intrinsics IEEE_IS_NAN, IEEE_IS_FINITE
   10330              :    and IEEE_UNORDERED, which translate directly to GCC type-generic
   10331              :    built-ins.  */
   10332              : 
   10333              : static void
   10334         1062 : conv_intrinsic_ieee_builtin (gfc_se * se, gfc_expr * expr,
   10335              :                              enum built_in_function code, int nargs)
   10336              : {
   10337         1062 :   tree args[2];
   10338         1062 :   gcc_assert ((unsigned) nargs <= ARRAY_SIZE (args));
   10339              : 
   10340         1062 :   conv_ieee_function_args (se, expr, args, nargs);
   10341         1062 :   se->expr = build_call_expr_loc_array (input_location,
   10342              :                                         builtin_decl_explicit (code),
   10343              :                                         nargs, args);
   10344         2388 :   STRIP_TYPE_NOPS (se->expr);
   10345         1062 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
   10346         1062 : }
   10347              : 
   10348              : 
   10349              : /* Generate code for intrinsics IEEE_SIGNBIT.  */
   10350              : 
   10351              : static void
   10352          624 : conv_intrinsic_ieee_signbit (gfc_se * se, gfc_expr * expr)
   10353              : {
   10354          624 :   tree arg, signbit;
   10355              : 
   10356          624 :   conv_ieee_function_args (se, expr, &arg, 1);
   10357          624 :   signbit = build_call_expr_loc (input_location,
   10358              :                                  builtin_decl_explicit (BUILT_IN_SIGNBIT),
   10359              :                                  1, arg);
   10360          624 :   signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10361              :                              signbit, integer_zero_node);
   10362          624 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), signbit);
   10363          624 : }
   10364              : 
   10365              : 
   10366              : /* Generate code for IEEE_IS_NORMAL intrinsic:
   10367              :      IEEE_IS_NORMAL(x) --> (__builtin_isnormal(x) || x == 0)  */
   10368              : 
   10369              : static void
   10370          312 : conv_intrinsic_ieee_is_normal (gfc_se * se, gfc_expr * expr)
   10371              : {
   10372          312 :   tree arg, isnormal, iszero;
   10373              : 
   10374              :   /* Convert arg, evaluate it only once.  */
   10375          312 :   conv_ieee_function_args (se, expr, &arg, 1);
   10376          312 :   arg = gfc_evaluate_now (arg, &se->pre);
   10377              : 
   10378          312 :   isnormal = build_call_expr_loc (input_location,
   10379              :                                   builtin_decl_explicit (BUILT_IN_ISNORMAL),
   10380              :                                   1, arg);
   10381          312 :   iszero = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
   10382          312 :                             build_real_from_int_cst (TREE_TYPE (arg),
   10383          312 :                                                      integer_zero_node));
   10384          312 :   se->expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   10385              :                               logical_type_node, isnormal, iszero);
   10386          312 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
   10387          312 : }
   10388              : 
   10389              : 
   10390              : /* Generate code for IEEE_IS_NEGATIVE intrinsic:
   10391              :      IEEE_IS_NEGATIVE(x) --> (__builtin_signbit(x) && !__builtin_isnan(x))  */
   10392              : 
   10393              : static void
   10394          312 : conv_intrinsic_ieee_is_negative (gfc_se * se, gfc_expr * expr)
   10395              : {
   10396          312 :   tree arg, signbit, isnan;
   10397              : 
   10398              :   /* Convert arg, evaluate it only once.  */
   10399          312 :   conv_ieee_function_args (se, expr, &arg, 1);
   10400          312 :   arg = gfc_evaluate_now (arg, &se->pre);
   10401              : 
   10402          312 :   isnan = build_call_expr_loc (input_location,
   10403              :                                builtin_decl_explicit (BUILT_IN_ISNAN),
   10404              :                                1, arg);
   10405          936 :   STRIP_TYPE_NOPS (isnan);
   10406              : 
   10407          312 :   signbit = build_call_expr_loc (input_location,
   10408              :                                  builtin_decl_explicit (BUILT_IN_SIGNBIT),
   10409              :                                  1, arg);
   10410          312 :   signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10411              :                              signbit, integer_zero_node);
   10412              : 
   10413          312 :   se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   10414              :                               logical_type_node, signbit,
   10415              :                               fold_build1_loc (input_location, TRUTH_NOT_EXPR,
   10416          312 :                                                TREE_TYPE(isnan), isnan));
   10417              : 
   10418          312 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
   10419          312 : }
   10420              : 
   10421              : 
   10422              : /* Generate code for IEEE_LOGB and IEEE_RINT.  */
   10423              : 
   10424              : static void
   10425          240 : conv_intrinsic_ieee_logb_rint (gfc_se * se, gfc_expr * expr,
   10426              :                                enum built_in_function code)
   10427              : {
   10428          240 :   tree arg, decl, call, fpstate;
   10429          240 :   int argprec;
   10430              : 
   10431          240 :   conv_ieee_function_args (se, expr, &arg, 1);
   10432          240 :   argprec = TYPE_PRECISION (TREE_TYPE (arg));
   10433          240 :   decl = builtin_decl_for_precision (code, argprec);
   10434              : 
   10435              :   /* Save floating-point state.  */
   10436          240 :   fpstate = gfc_save_fp_state (&se->pre);
   10437              : 
   10438              :   /* Make the function call.  */
   10439          240 :   call = build_call_expr_loc (input_location, decl, 1, arg);
   10440          240 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), call);
   10441              : 
   10442              :   /* Restore floating-point state.  */
   10443          240 :   gfc_restore_fp_state (&se->post, fpstate);
   10444          240 : }
   10445              : 
   10446              : 
   10447              : /* Generate code for IEEE_REM.  */
   10448              : 
   10449              : static void
   10450           84 : conv_intrinsic_ieee_rem (gfc_se * se, gfc_expr * expr)
   10451              : {
   10452           84 :   tree args[2], decl, call, fpstate;
   10453           84 :   int argprec;
   10454              : 
   10455           84 :   conv_ieee_function_args (se, expr, args, 2);
   10456              : 
   10457              :   /* If arguments have unequal size, convert them to the larger.  */
   10458           84 :   if (TYPE_PRECISION (TREE_TYPE (args[0]))
   10459           84 :       > TYPE_PRECISION (TREE_TYPE (args[1])))
   10460            6 :     args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
   10461           78 :   else if (TYPE_PRECISION (TREE_TYPE (args[1]))
   10462           78 :            > TYPE_PRECISION (TREE_TYPE (args[0])))
   10463           24 :     args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
   10464              : 
   10465           84 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   10466           84 :   decl = builtin_decl_for_precision (BUILT_IN_REMAINDER, argprec);
   10467              : 
   10468              :   /* Save floating-point state.  */
   10469           84 :   fpstate = gfc_save_fp_state (&se->pre);
   10470              : 
   10471              :   /* Make the function call.  */
   10472           84 :   call = build_call_expr_loc_array (input_location, decl, 2, args);
   10473           84 :   se->expr = fold_convert (TREE_TYPE (args[0]), call);
   10474              : 
   10475              :   /* Restore floating-point state.  */
   10476           84 :   gfc_restore_fp_state (&se->post, fpstate);
   10477           84 : }
   10478              : 
   10479              : 
   10480              : /* Generate code for IEEE_NEXT_AFTER.  */
   10481              : 
   10482              : static void
   10483          180 : conv_intrinsic_ieee_next_after (gfc_se * se, gfc_expr * expr)
   10484              : {
   10485          180 :   tree args[2], decl, call, fpstate;
   10486          180 :   int argprec;
   10487              : 
   10488          180 :   conv_ieee_function_args (se, expr, args, 2);
   10489              : 
   10490              :   /* Result has the characteristics of first argument.  */
   10491          180 :   args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
   10492          180 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   10493          180 :   decl = builtin_decl_for_precision (BUILT_IN_NEXTAFTER, argprec);
   10494              : 
   10495              :   /* Save floating-point state.  */
   10496          180 :   fpstate = gfc_save_fp_state (&se->pre);
   10497              : 
   10498              :   /* Make the function call.  */
   10499          180 :   call = build_call_expr_loc_array (input_location, decl, 2, args);
   10500          180 :   se->expr = fold_convert (TREE_TYPE (args[0]), call);
   10501              : 
   10502              :   /* Restore floating-point state.  */
   10503          180 :   gfc_restore_fp_state (&se->post, fpstate);
   10504          180 : }
   10505              : 
   10506              : 
   10507              : /* Generate code for IEEE_SCALB.  */
   10508              : 
   10509              : static void
   10510          228 : conv_intrinsic_ieee_scalb (gfc_se * se, gfc_expr * expr)
   10511              : {
   10512          228 :   tree args[2], decl, call, huge, type;
   10513          228 :   int argprec, n;
   10514              : 
   10515          228 :   conv_ieee_function_args (se, expr, args, 2);
   10516              : 
   10517              :   /* Result has the characteristics of first argument.  */
   10518          228 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   10519          228 :   decl = builtin_decl_for_precision (BUILT_IN_SCALBN, argprec);
   10520              : 
   10521          228 :   if (TYPE_PRECISION (TREE_TYPE (args[1])) > TYPE_PRECISION (integer_type_node))
   10522              :     {
   10523              :       /* We need to fold the integer into the range of a C int.  */
   10524           18 :       args[1] = gfc_evaluate_now (args[1], &se->pre);
   10525           18 :       type = TREE_TYPE (args[1]);
   10526              : 
   10527           18 :       n = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
   10528           18 :       huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
   10529              :                                    gfc_c_int_kind);
   10530           18 :       huge = fold_convert (type, huge);
   10531           18 :       args[1] = fold_build2_loc (input_location, MIN_EXPR, type, args[1],
   10532              :                                  huge);
   10533           18 :       args[1] = fold_build2_loc (input_location, MAX_EXPR, type, args[1],
   10534              :                                  fold_build1_loc (input_location, NEGATE_EXPR,
   10535              :                                                   type, huge));
   10536              :     }
   10537              : 
   10538          228 :   args[1] = fold_convert (integer_type_node, args[1]);
   10539              : 
   10540              :   /* Make the function call.  */
   10541          228 :   call = build_call_expr_loc_array (input_location, decl, 2, args);
   10542          228 :   se->expr = fold_convert (TREE_TYPE (args[0]), call);
   10543          228 : }
   10544              : 
   10545              : 
   10546              : /* Generate code for IEEE_COPY_SIGN.  */
   10547              : 
   10548              : static void
   10549          576 : conv_intrinsic_ieee_copy_sign (gfc_se * se, gfc_expr * expr)
   10550              : {
   10551          576 :   tree args[2], decl, sign;
   10552          576 :   int argprec;
   10553              : 
   10554          576 :   conv_ieee_function_args (se, expr, args, 2);
   10555              : 
   10556              :   /* Get the sign of the second argument.  */
   10557          576 :   sign = build_call_expr_loc (input_location,
   10558              :                               builtin_decl_explicit (BUILT_IN_SIGNBIT),
   10559              :                               1, args[1]);
   10560          576 :   sign = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10561              :                           sign, integer_zero_node);
   10562              : 
   10563              :   /* Create a value of one, with the right sign.  */
   10564          576 :   sign = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
   10565              :                           sign,
   10566              :                           fold_build1_loc (input_location, NEGATE_EXPR,
   10567              :                                            integer_type_node,
   10568              :                                            integer_one_node),
   10569              :                           integer_one_node);
   10570          576 :   args[1] = fold_convert (TREE_TYPE (args[0]), sign);
   10571              : 
   10572          576 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   10573          576 :   decl = builtin_decl_for_precision (BUILT_IN_COPYSIGN, argprec);
   10574              : 
   10575          576 :   se->expr = build_call_expr_loc_array (input_location, decl, 2, args);
   10576          576 : }
   10577              : 
   10578              : 
   10579              : /* Generate code for IEEE_CLASS.  */
   10580              : 
   10581              : static void
   10582          648 : conv_intrinsic_ieee_class (gfc_se *se, gfc_expr *expr)
   10583              : {
   10584          648 :   tree arg, c, t1, t2, t3, t4;
   10585              : 
   10586              :   /* Convert arg, evaluate it only once.  */
   10587          648 :   conv_ieee_function_args (se, expr, &arg, 1);
   10588          648 :   arg = gfc_evaluate_now (arg, &se->pre);
   10589              : 
   10590          648 :   c = build_call_expr_loc (input_location,
   10591              :                            builtin_decl_explicit (BUILT_IN_FPCLASSIFY), 6,
   10592              :                            build_int_cst (integer_type_node, IEEE_QUIET_NAN),
   10593              :                            build_int_cst (integer_type_node,
   10594              :                                           IEEE_POSITIVE_INF),
   10595              :                            build_int_cst (integer_type_node,
   10596              :                                           IEEE_POSITIVE_NORMAL),
   10597              :                            build_int_cst (integer_type_node,
   10598              :                                           IEEE_POSITIVE_DENORMAL),
   10599              :                            build_int_cst (integer_type_node,
   10600              :                                           IEEE_POSITIVE_ZERO),
   10601              :                            arg);
   10602          648 :   c = gfc_evaluate_now (c, &se->pre);
   10603          648 :   t1 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   10604              :                         c, build_int_cst (integer_type_node,
   10605              :                                           IEEE_QUIET_NAN));
   10606          648 :   t2 = build_call_expr_loc (input_location,
   10607              :                             builtin_decl_explicit (BUILT_IN_ISSIGNALING), 1,
   10608              :                             arg);
   10609          648 :   t2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10610          648 :                         t2, build_zero_cst (TREE_TYPE (t2)));
   10611          648 :   t1 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   10612              :                         logical_type_node, t1, t2);
   10613          648 :   t3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   10614              :                         c, build_int_cst (integer_type_node,
   10615              :                                           IEEE_POSITIVE_ZERO));
   10616          648 :   t4 = build_call_expr_loc (input_location,
   10617              :                             builtin_decl_explicit (BUILT_IN_SIGNBIT), 1,
   10618              :                             arg);
   10619          648 :   t4 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10620          648 :                         t4, build_zero_cst (TREE_TYPE (t4)));
   10621          648 :   t3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   10622              :                         logical_type_node, t3, t4);
   10623          648 :   int s = IEEE_NEGATIVE_ZERO + IEEE_POSITIVE_ZERO;
   10624          648 :   gcc_assert (IEEE_NEGATIVE_INF == s - IEEE_POSITIVE_INF);
   10625          648 :   gcc_assert (IEEE_NEGATIVE_NORMAL == s - IEEE_POSITIVE_NORMAL);
   10626          648 :   gcc_assert (IEEE_NEGATIVE_DENORMAL == s - IEEE_POSITIVE_DENORMAL);
   10627          648 :   gcc_assert (IEEE_NEGATIVE_SUBNORMAL == s - IEEE_POSITIVE_SUBNORMAL);
   10628          648 :   gcc_assert (IEEE_NEGATIVE_ZERO == s - IEEE_POSITIVE_ZERO);
   10629          648 :   t4 = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (c),
   10630          648 :                         build_int_cst (TREE_TYPE (c), s), c);
   10631          648 :   t3 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c),
   10632              :                         t3, t4, c);
   10633          648 :   t1 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c), t1,
   10634          648 :                         build_int_cst (TREE_TYPE (c), IEEE_SIGNALING_NAN),
   10635              :                         t3);
   10636          648 :   tree type = gfc_typenode_for_spec (&expr->ts);
   10637              :   /* Perform a quick sanity check that the return type is
   10638              :      IEEE_CLASS_TYPE derived type defined in
   10639              :      libgfortran/ieee/ieee_arithmetic.F90
   10640              :      Primarily check that it is a derived type with a single
   10641              :      member in it.  */
   10642          648 :   gcc_assert (TREE_CODE (type) == RECORD_TYPE);
   10643          648 :   tree field = NULL_TREE;
   10644         1296 :   for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
   10645          648 :     if (TREE_CODE (f) == FIELD_DECL)
   10646              :       {
   10647          648 :         gcc_assert (field == NULL_TREE);
   10648              :         field = f;
   10649              :       }
   10650          648 :   gcc_assert (field);
   10651          648 :   t1 = fold_convert (TREE_TYPE (field), t1);
   10652          648 :   se->expr = build_constructor_single (type, field, t1);
   10653          648 : }
   10654              : 
   10655              : 
   10656              : /* Generate code for IEEE_VALUE.  */
   10657              : 
   10658              : static void
   10659         1111 : conv_intrinsic_ieee_value (gfc_se *se, gfc_expr *expr)
   10660              : {
   10661         1111 :   tree args[2], arg, ret, tmp;
   10662         1111 :   stmtblock_t body;
   10663              : 
   10664              :   /* Convert args, evaluate the second one only once.  */
   10665         1111 :   conv_ieee_function_args (se, expr, args, 2);
   10666         1111 :   arg = gfc_evaluate_now (args[1], &se->pre);
   10667              : 
   10668         1111 :   tree type = TREE_TYPE (arg);
   10669              :   /* Perform a quick sanity check that the second argument's type is
   10670              :      IEEE_CLASS_TYPE derived type defined in
   10671              :      libgfortran/ieee/ieee_arithmetic.F90
   10672              :      Primarily check that it is a derived type with a single
   10673              :      member in it.  */
   10674         1111 :   gcc_assert (TREE_CODE (type) == RECORD_TYPE);
   10675         1111 :   tree field = NULL_TREE;
   10676         2222 :   for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
   10677         1111 :     if (TREE_CODE (f) == FIELD_DECL)
   10678              :       {
   10679         1111 :         gcc_assert (field == NULL_TREE);
   10680              :         field = f;
   10681              :       }
   10682         1111 :   gcc_assert (field);
   10683         1111 :   arg = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
   10684              :                          arg, field, NULL_TREE);
   10685         1111 :   arg = gfc_evaluate_now (arg, &se->pre);
   10686              : 
   10687         1111 :   type = gfc_typenode_for_spec (&expr->ts);
   10688         1111 :   gcc_assert (SCALAR_FLOAT_TYPE_P (type));
   10689         1111 :   ret = gfc_create_var (type, NULL);
   10690              : 
   10691         1111 :   gfc_init_block (&body);
   10692              : 
   10693         1111 :   tree end_label = gfc_build_label_decl (NULL_TREE);
   10694        12221 :   for (int c = IEEE_SIGNALING_NAN; c <= IEEE_POSITIVE_INF; ++c)
   10695              :     {
   10696        11110 :       tree label = gfc_build_label_decl (NULL_TREE);
   10697        11110 :       tree low = build_int_cst (TREE_TYPE (arg), c);
   10698        11110 :       tmp = build_case_label (low, low, label);
   10699        11110 :       gfc_add_expr_to_block (&body, tmp);
   10700              : 
   10701        11110 :       REAL_VALUE_TYPE real;
   10702        11110 :       int k;
   10703        11110 :       switch (c)
   10704              :         {
   10705         1111 :         case IEEE_SIGNALING_NAN:
   10706         1111 :           real_nan (&real, "", 0, TYPE_MODE (type));
   10707         1111 :           break;
   10708         1111 :         case IEEE_QUIET_NAN:
   10709         1111 :           real_nan (&real, "", 1, TYPE_MODE (type));
   10710         1111 :           break;
   10711         1111 :         case IEEE_NEGATIVE_INF:
   10712         1111 :           real_inf (&real);
   10713         1111 :           real = real_value_negate (&real);
   10714         1111 :           break;
   10715         1111 :         case IEEE_NEGATIVE_NORMAL:
   10716         1111 :           real_from_integer (&real, TYPE_MODE (type), -42, SIGNED);
   10717         1111 :           break;
   10718         1111 :         case IEEE_NEGATIVE_DENORMAL:
   10719         1111 :           k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
   10720         1111 :           real_from_mpfr (&real, gfc_real_kinds[k].tiny,
   10721              :                           type, GFC_RND_MODE);
   10722         1111 :           real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
   10723         1111 :           real = real_value_negate (&real);
   10724         1111 :           break;
   10725         1111 :         case IEEE_NEGATIVE_ZERO:
   10726         1111 :           real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
   10727         1111 :           real = real_value_negate (&real);
   10728         1111 :           break;
   10729         1111 :         case IEEE_POSITIVE_ZERO:
   10730              :           /* Make this also the default: label.  The other possibility
   10731              :              would be to add a separate default: label followed by
   10732              :              __builtin_unreachable ().  */
   10733         1111 :           label = gfc_build_label_decl (NULL_TREE);
   10734         1111 :           tmp = build_case_label (NULL_TREE, NULL_TREE, label);
   10735         1111 :           gfc_add_expr_to_block (&body, tmp);
   10736         1111 :           real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
   10737         1111 :           break;
   10738         1111 :         case IEEE_POSITIVE_DENORMAL:
   10739         1111 :           k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
   10740         1111 :           real_from_mpfr (&real, gfc_real_kinds[k].tiny,
   10741              :                           type, GFC_RND_MODE);
   10742         1111 :           real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
   10743         1111 :           break;
   10744         1111 :         case IEEE_POSITIVE_NORMAL:
   10745         1111 :           real_from_integer (&real, TYPE_MODE (type), 42, SIGNED);
   10746         1111 :           break;
   10747         1111 :         case IEEE_POSITIVE_INF:
   10748         1111 :           real_inf (&real);
   10749         1111 :           break;
   10750              :         default:
   10751              :           gcc_unreachable ();
   10752              :         }
   10753              : 
   10754        11110 :       tree val = build_real (type, real);
   10755        11110 :       gfc_add_modify (&body, ret, val);
   10756              : 
   10757        11110 :       tmp = build1_v (GOTO_EXPR, end_label);
   10758        11110 :       gfc_add_expr_to_block (&body, tmp);
   10759              :     }
   10760              : 
   10761         1111 :   tmp = gfc_finish_block (&body);
   10762         1111 :   tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, arg, tmp);
   10763         1111 :   gfc_add_expr_to_block (&se->pre, tmp);
   10764              : 
   10765         1111 :   tmp = build1_v (LABEL_EXPR, end_label);
   10766         1111 :   gfc_add_expr_to_block (&se->pre, tmp);
   10767              : 
   10768         1111 :   se->expr = ret;
   10769         1111 : }
   10770              : 
   10771              : 
   10772              : /* Generate code for IEEE_FMA.  */
   10773              : 
   10774              : static void
   10775          120 : conv_intrinsic_ieee_fma (gfc_se * se, gfc_expr * expr)
   10776              : {
   10777          120 :   tree args[3], decl, call;
   10778          120 :   int argprec;
   10779              : 
   10780          120 :   conv_ieee_function_args (se, expr, args, 3);
   10781              : 
   10782              :   /* All three arguments should have the same type.  */
   10783          120 :   gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
   10784          120 :   gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[2])));
   10785              : 
   10786              :   /* Call the type-generic FMA built-in.  */
   10787          120 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   10788          120 :   decl = builtin_decl_for_precision (BUILT_IN_FMA, argprec);
   10789          120 :   call = build_call_expr_loc_array (input_location, decl, 3, args);
   10790              : 
   10791              :   /* Convert to the final type.  */
   10792          120 :   se->expr = fold_convert (TREE_TYPE (args[0]), call);
   10793          120 : }
   10794              : 
   10795              : 
   10796              : /* Generate code for IEEE_{MIN,MAX}_NUM{,_MAG}.  */
   10797              : 
   10798              : static void
   10799         3072 : conv_intrinsic_ieee_minmax (gfc_se * se, gfc_expr * expr, int max,
   10800              :                             const char *name)
   10801              : {
   10802         3072 :   tree args[2], func;
   10803         3072 :   built_in_function fn;
   10804              : 
   10805         3072 :   conv_ieee_function_args (se, expr, args, 2);
   10806         3072 :   gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
   10807         3072 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
   10808         3072 :   args[1] = gfc_evaluate_now (args[1], &se->pre);
   10809              : 
   10810         3072 :   if (startswith (name, "mag"))
   10811              :     {
   10812              :       /* IEEE_MIN_NUM_MAG and IEEE_MAX_NUM_MAG translate to C functions
   10813              :          fminmag() and fmaxmag(), which do not exist as built-ins.
   10814              : 
   10815              :          Following glibc, we emit this:
   10816              : 
   10817              :            fminmag (x, y) {
   10818              :              ax = ABS (x);
   10819              :              ay = ABS (y);
   10820              :              if (isless (ax, ay))
   10821              :                return x;
   10822              :              else if (isgreater (ax, ay))
   10823              :                return y;
   10824              :              else if (ax == ay)
   10825              :                return x < y ? x : y;
   10826              :              else if (issignaling (x) || issignaling (y))
   10827              :                return x + y;
   10828              :              else
   10829              :                return isnan (y) ? x : y;
   10830              :            }
   10831              : 
   10832              :            fmaxmag (x, y) {
   10833              :              ax = ABS (x);
   10834              :              ay = ABS (y);
   10835              :              if (isgreater (ax, ay))
   10836              :                return x;
   10837              :              else if (isless (ax, ay))
   10838              :                return y;
   10839              :              else if (ax == ay)
   10840              :                return x > y ? x : y;
   10841              :              else if (issignaling (x) || issignaling (y))
   10842              :                return x + y;
   10843              :              else
   10844              :                return isnan (y) ? x : y;
   10845              :            }
   10846              : 
   10847              :          */
   10848              : 
   10849         1536 :       tree abs0, abs1, sig0, sig1;
   10850         1536 :       tree cond1, cond2, cond3, cond4, cond5;
   10851         1536 :       tree res;
   10852         1536 :       tree type = TREE_TYPE (args[0]);
   10853              : 
   10854         1536 :       func = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
   10855         1536 :       abs0 = build_call_expr_loc (input_location, func, 1, args[0]);
   10856         1536 :       abs1 = build_call_expr_loc (input_location, func, 1, args[1]);
   10857         1536 :       abs0 = gfc_evaluate_now (abs0, &se->pre);
   10858         1536 :       abs1 = gfc_evaluate_now (abs1, &se->pre);
   10859              : 
   10860         1536 :       cond5 = build_call_expr_loc (input_location,
   10861              :                                    builtin_decl_explicit (BUILT_IN_ISNAN),
   10862              :                                    1, args[1]);
   10863         1536 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond5,
   10864              :                              args[0], args[1]);
   10865              : 
   10866         1536 :       sig0 = build_call_expr_loc (input_location,
   10867              :                                   builtin_decl_explicit (BUILT_IN_ISSIGNALING),
   10868              :                                   1, args[0]);
   10869         1536 :       sig1 = build_call_expr_loc (input_location,
   10870              :                                   builtin_decl_explicit (BUILT_IN_ISSIGNALING),
   10871              :                                   1, args[1]);
   10872         1536 :       cond4 = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
   10873              :                                logical_type_node, sig0, sig1);
   10874         1536 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond4,
   10875              :                              fold_build2_loc (input_location, PLUS_EXPR,
   10876              :                                               type, args[0], args[1]),
   10877              :                              res);
   10878              : 
   10879         1536 :       cond3 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   10880              :                                abs0, abs1);
   10881         2304 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond3,
   10882              :                              fold_build2_loc (input_location,
   10883              :                                               max ? MAX_EXPR : MIN_EXPR,
   10884              :                                               type, args[0], args[1]),
   10885              :                              res);
   10886              : 
   10887         2304 :       func = builtin_decl_explicit (max ? BUILT_IN_ISLESS : BUILT_IN_ISGREATER);
   10888         1536 :       cond2 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
   10889         1536 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond2,
   10890              :                              args[1], res);
   10891              : 
   10892         2304 :       func = builtin_decl_explicit (max ? BUILT_IN_ISGREATER : BUILT_IN_ISLESS);
   10893         1536 :       cond1 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
   10894         1536 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond1,
   10895              :                              args[0], res);
   10896              : 
   10897         1536 :       se->expr = res;
   10898              :     }
   10899              :   else
   10900              :     {
   10901              :       /* IEEE_MIN_NUM and IEEE_MAX_NUM translate to fmin() and fmax().  */
   10902         1536 :       fn = max ? BUILT_IN_FMAX : BUILT_IN_FMIN;
   10903         1536 :       func = gfc_builtin_decl_for_float_kind (fn, expr->ts.kind);
   10904         1536 :       se->expr = build_call_expr_loc_array (input_location, func, 2, args);
   10905              :     }
   10906         3072 : }
   10907              : 
   10908              : 
   10909              : /* Generate code for comparison functions IEEE_QUIET_* and
   10910              :    IEEE_SIGNALING_*.  */
   10911              : 
   10912              : static void
   10913         3888 : conv_intrinsic_ieee_comparison (gfc_se * se, gfc_expr * expr, int signaling,
   10914              :                                 const char *name)
   10915              : {
   10916         3888 :   tree args[2];
   10917         3888 :   tree arg1, arg2, res;
   10918              : 
   10919              :   /* Evaluate arguments only once.  */
   10920         3888 :   conv_ieee_function_args (se, expr, args, 2);
   10921         3888 :   arg1 = gfc_evaluate_now (args[0], &se->pre);
   10922         3888 :   arg2 = gfc_evaluate_now (args[1], &se->pre);
   10923              : 
   10924         3888 :   if (startswith (name, "eq"))
   10925              :     {
   10926          648 :       if (signaling)
   10927          324 :         res = build_call_expr_loc (input_location,
   10928              :                                    builtin_decl_explicit (BUILT_IN_ISEQSIG),
   10929              :                                    2, arg1, arg2);
   10930              :       else
   10931          324 :         res = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   10932              :                                arg1, arg2);
   10933              :     }
   10934         3240 :   else if (startswith (name, "ne"))
   10935              :     {
   10936          648 :       if (signaling)
   10937              :         {
   10938          324 :           res = build_call_expr_loc (input_location,
   10939              :                                      builtin_decl_explicit (BUILT_IN_ISEQSIG),
   10940              :                                      2, arg1, arg2);
   10941          324 :           res = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
   10942              :                                  logical_type_node, res);
   10943              :         }
   10944              :       else
   10945          324 :         res = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10946              :                                arg1, arg2);
   10947              :     }
   10948         2592 :   else if (startswith (name, "ge"))
   10949              :     {
   10950          648 :       if (signaling)
   10951          324 :         res = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   10952              :                                arg1, arg2);
   10953              :       else
   10954          324 :         res = build_call_expr_loc (input_location,
   10955              :                                    builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL),
   10956              :                                    2, arg1, arg2);
   10957              :     }
   10958         1944 :   else if (startswith (name, "gt"))
   10959              :     {
   10960          648 :       if (signaling)
   10961          324 :         res = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
   10962              :                                arg1, arg2);
   10963              :       else
   10964          324 :         res = build_call_expr_loc (input_location,
   10965              :                                    builtin_decl_explicit (BUILT_IN_ISGREATER),
   10966              :                                    2, arg1, arg2);
   10967              :     }
   10968         1296 :   else if (startswith (name, "le"))
   10969              :     {
   10970          648 :       if (signaling)
   10971          324 :         res = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
   10972              :                                arg1, arg2);
   10973              :       else
   10974          324 :         res = build_call_expr_loc (input_location,
   10975              :                                    builtin_decl_explicit (BUILT_IN_ISLESSEQUAL),
   10976              :                                    2, arg1, arg2);
   10977              :     }
   10978          648 :   else if (startswith (name, "lt"))
   10979              :     {
   10980          648 :       if (signaling)
   10981          324 :         res = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
   10982              :                                arg1, arg2);
   10983              :       else
   10984          324 :         res = build_call_expr_loc (input_location,
   10985              :                                    builtin_decl_explicit (BUILT_IN_ISLESS),
   10986              :                                    2, arg1, arg2);
   10987              :     }
   10988              :   else
   10989            0 :     gcc_unreachable ();
   10990              : 
   10991         3888 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), res);
   10992         3888 : }
   10993              : 
   10994              : 
   10995              : /* Generate code for an intrinsic function from the IEEE_ARITHMETIC
   10996              :    module.  */
   10997              : 
   10998              : bool
   10999        13939 : gfc_conv_ieee_arithmetic_function (gfc_se * se, gfc_expr * expr)
   11000              : {
   11001        13939 :   const char *name = expr->value.function.name;
   11002              : 
   11003        13939 :   if (startswith (name, "_gfortran_ieee_is_nan"))
   11004          522 :     conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISNAN, 1);
   11005        13417 :   else if (startswith (name, "_gfortran_ieee_is_finite"))
   11006          372 :     conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISFINITE, 1);
   11007        13045 :   else if (startswith (name, "_gfortran_ieee_unordered"))
   11008          168 :     conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISUNORDERED, 2);
   11009        12877 :   else if (startswith (name, "_gfortran_ieee_signbit"))
   11010          624 :     conv_intrinsic_ieee_signbit (se, expr);
   11011        12253 :   else if (startswith (name, "_gfortran_ieee_is_normal"))
   11012          312 :     conv_intrinsic_ieee_is_normal (se, expr);
   11013        11941 :   else if (startswith (name, "_gfortran_ieee_is_negative"))
   11014          312 :     conv_intrinsic_ieee_is_negative (se, expr);
   11015        11629 :   else if (startswith (name, "_gfortran_ieee_copy_sign"))
   11016          576 :     conv_intrinsic_ieee_copy_sign (se, expr);
   11017        11053 :   else if (startswith (name, "_gfortran_ieee_scalb"))
   11018          228 :     conv_intrinsic_ieee_scalb (se, expr);
   11019        10825 :   else if (startswith (name, "_gfortran_ieee_next_after"))
   11020          180 :     conv_intrinsic_ieee_next_after (se, expr);
   11021        10645 :   else if (startswith (name, "_gfortran_ieee_rem"))
   11022           84 :     conv_intrinsic_ieee_rem (se, expr);
   11023        10561 :   else if (startswith (name, "_gfortran_ieee_logb"))
   11024          144 :     conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_LOGB);
   11025        10417 :   else if (startswith (name, "_gfortran_ieee_rint"))
   11026           96 :     conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_RINT);
   11027        10321 :   else if (startswith (name, "ieee_class_") && ISDIGIT (name[11]))
   11028          648 :     conv_intrinsic_ieee_class (se, expr);
   11029         9673 :   else if (startswith (name, "ieee_value_") && ISDIGIT (name[11]))
   11030         1111 :     conv_intrinsic_ieee_value (se, expr);
   11031         8562 :   else if (startswith (name, "_gfortran_ieee_fma"))
   11032          120 :     conv_intrinsic_ieee_fma (se, expr);
   11033         8442 :   else if (startswith (name, "_gfortran_ieee_min_num_"))
   11034         1536 :     conv_intrinsic_ieee_minmax (se, expr, 0, name + 23);
   11035         6906 :   else if (startswith (name, "_gfortran_ieee_max_num_"))
   11036         1536 :     conv_intrinsic_ieee_minmax (se, expr, 1, name + 23);
   11037         5370 :   else if (startswith (name, "_gfortran_ieee_quiet_"))
   11038         1944 :     conv_intrinsic_ieee_comparison (se, expr, 0, name + 21);
   11039         3426 :   else if (startswith (name, "_gfortran_ieee_signaling_"))
   11040         1944 :     conv_intrinsic_ieee_comparison (se, expr, 1, name + 25);
   11041              :   else
   11042              :     /* It is not among the functions we translate directly.  We return
   11043              :        false, so a library function call is emitted.  */
   11044              :     return false;
   11045              : 
   11046              :   return true;
   11047              : }
   11048              : 
   11049              : 
   11050              : /* Generate a direct call to malloc() for the MALLOC intrinsic.  */
   11051              : 
   11052              : static void
   11053           16 : gfc_conv_intrinsic_malloc (gfc_se * se, gfc_expr * expr)
   11054              : {
   11055           16 :   tree arg, res, restype;
   11056              : 
   11057           16 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
   11058           16 :   arg = fold_convert (size_type_node, arg);
   11059           16 :   res = build_call_expr_loc (input_location,
   11060              :                              builtin_decl_explicit (BUILT_IN_MALLOC), 1, arg);
   11061           16 :   restype = gfc_typenode_for_spec (&expr->ts);
   11062           16 :   se->expr = fold_convert (restype, res);
   11063           16 : }
   11064              : 
   11065              : 
   11066              : /* Generate code for an intrinsic function.  Some map directly to library
   11067              :    calls, others get special handling.  In some cases the name of the function
   11068              :    used depends on the type specifiers.  */
   11069              : 
   11070              : void
   11071       263023 : gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
   11072              : {
   11073       263023 :   const char *name;
   11074       263023 :   int lib, kind;
   11075       263023 :   tree fndecl;
   11076              : 
   11077       263023 :   name = &expr->value.function.name[2];
   11078              : 
   11079       263023 :   if (expr->rank > 0)
   11080              :     {
   11081        50363 :       lib = gfc_is_intrinsic_libcall (expr);
   11082        50363 :       if (lib != 0)
   11083              :         {
   11084        19187 :           if (lib == 1)
   11085        11797 :             se->ignore_optional = 1;
   11086              : 
   11087        19187 :           switch (expr->value.function.isym->id)
   11088              :             {
   11089         5831 :             case GFC_ISYM_EOSHIFT:
   11090         5831 :             case GFC_ISYM_PACK:
   11091         5831 :             case GFC_ISYM_RESHAPE:
   11092         5831 :             case GFC_ISYM_REDUCE:
   11093              :               /* For all of those the first argument specifies the type and the
   11094              :                  third is optional.  */
   11095         5831 :               conv_generic_with_optional_char_arg (se, expr, 1, 3);
   11096         5831 :               break;
   11097              : 
   11098         1116 :             case GFC_ISYM_FINDLOC:
   11099         1116 :               gfc_conv_intrinsic_findloc (se, expr);
   11100         1116 :               break;
   11101              : 
   11102         2935 :             case GFC_ISYM_MINLOC:
   11103         2935 :               gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
   11104         2935 :               break;
   11105              : 
   11106         2439 :             case GFC_ISYM_MAXLOC:
   11107         2439 :               gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
   11108         2439 :               break;
   11109              : 
   11110         6866 :             default:
   11111         6866 :               gfc_conv_intrinsic_funcall (se, expr);
   11112         6866 :               break;
   11113              :             }
   11114              : 
   11115        19187 :           return;
   11116              :         }
   11117              :     }
   11118              : 
   11119       243836 :   switch (expr->value.function.isym->id)
   11120              :     {
   11121            0 :     case GFC_ISYM_NONE:
   11122            0 :       gcc_unreachable ();
   11123              : 
   11124          529 :     case GFC_ISYM_REPEAT:
   11125          529 :       gfc_conv_intrinsic_repeat (se, expr);
   11126          529 :       break;
   11127              : 
   11128          578 :     case GFC_ISYM_TRIM:
   11129          578 :       gfc_conv_intrinsic_trim (se, expr);
   11130          578 :       break;
   11131              : 
   11132           42 :     case GFC_ISYM_SC_KIND:
   11133           42 :       gfc_conv_intrinsic_sc_kind (se, expr);
   11134           42 :       break;
   11135              : 
   11136           45 :     case GFC_ISYM_SI_KIND:
   11137           45 :       gfc_conv_intrinsic_si_kind (se, expr);
   11138           45 :       break;
   11139              : 
   11140            6 :     case GFC_ISYM_SL_KIND:
   11141            6 :       gfc_conv_intrinsic_sl_kind (se, expr);
   11142            6 :       break;
   11143              : 
   11144           82 :     case GFC_ISYM_SR_KIND:
   11145           82 :       gfc_conv_intrinsic_sr_kind (se, expr);
   11146           82 :       break;
   11147              : 
   11148          228 :     case GFC_ISYM_EXPONENT:
   11149          228 :       gfc_conv_intrinsic_exponent (se, expr);
   11150          228 :       break;
   11151              : 
   11152          316 :     case GFC_ISYM_SCAN:
   11153          316 :       kind = expr->value.function.actual->expr->ts.kind;
   11154          316 :       if (kind == 1)
   11155          250 :        fndecl = gfor_fndecl_string_scan;
   11156           66 :       else if (kind == 4)
   11157           66 :        fndecl = gfor_fndecl_string_scan_char4;
   11158              :       else
   11159            0 :        gcc_unreachable ();
   11160              : 
   11161          316 :       gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
   11162          316 :       break;
   11163              : 
   11164           94 :     case GFC_ISYM_VERIFY:
   11165           94 :       kind = expr->value.function.actual->expr->ts.kind;
   11166           94 :       if (kind == 1)
   11167           70 :        fndecl = gfor_fndecl_string_verify;
   11168           24 :       else if (kind == 4)
   11169           24 :        fndecl = gfor_fndecl_string_verify_char4;
   11170              :       else
   11171            0 :        gcc_unreachable ();
   11172              : 
   11173           94 :       gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
   11174           94 :       break;
   11175              : 
   11176         7379 :     case GFC_ISYM_ALLOCATED:
   11177         7379 :       gfc_conv_allocated (se, expr);
   11178         7379 :       break;
   11179              : 
   11180         9478 :     case GFC_ISYM_ASSOCIATED:
   11181         9478 :       gfc_conv_associated(se, expr);
   11182         9478 :       break;
   11183              : 
   11184          409 :     case GFC_ISYM_SAME_TYPE_AS:
   11185          409 :       gfc_conv_same_type_as (se, expr);
   11186          409 :       break;
   11187              : 
   11188         7866 :     case GFC_ISYM_ABS:
   11189         7866 :       gfc_conv_intrinsic_abs (se, expr);
   11190         7866 :       break;
   11191              : 
   11192          351 :     case GFC_ISYM_ADJUSTL:
   11193          351 :       if (expr->ts.kind == 1)
   11194          297 :        fndecl = gfor_fndecl_adjustl;
   11195           54 :       else if (expr->ts.kind == 4)
   11196           54 :        fndecl = gfor_fndecl_adjustl_char4;
   11197              :       else
   11198            0 :        gcc_unreachable ();
   11199              : 
   11200          351 :       gfc_conv_intrinsic_adjust (se, expr, fndecl);
   11201          351 :       break;
   11202              : 
   11203          123 :     case GFC_ISYM_ADJUSTR:
   11204          123 :       if (expr->ts.kind == 1)
   11205           68 :        fndecl = gfor_fndecl_adjustr;
   11206           55 :       else if (expr->ts.kind == 4)
   11207           55 :        fndecl = gfor_fndecl_adjustr_char4;
   11208              :       else
   11209            0 :        gcc_unreachable ();
   11210              : 
   11211          123 :       gfc_conv_intrinsic_adjust (se, expr, fndecl);
   11212          123 :       break;
   11213              : 
   11214          440 :     case GFC_ISYM_AIMAG:
   11215          440 :       gfc_conv_intrinsic_imagpart (se, expr);
   11216          440 :       break;
   11217              : 
   11218          146 :     case GFC_ISYM_AINT:
   11219          146 :       gfc_conv_intrinsic_aint (se, expr, RND_TRUNC);
   11220          146 :       break;
   11221              : 
   11222          420 :     case GFC_ISYM_ALL:
   11223          420 :       gfc_conv_intrinsic_anyall (se, expr, EQ_EXPR);
   11224          420 :       break;
   11225              : 
   11226           74 :     case GFC_ISYM_ANINT:
   11227           74 :       gfc_conv_intrinsic_aint (se, expr, RND_ROUND);
   11228           74 :       break;
   11229              : 
   11230           90 :     case GFC_ISYM_AND:
   11231           90 :       gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
   11232           90 :       break;
   11233              : 
   11234        37735 :     case GFC_ISYM_ANY:
   11235        37735 :       gfc_conv_intrinsic_anyall (se, expr, NE_EXPR);
   11236        37735 :       break;
   11237              : 
   11238          216 :     case GFC_ISYM_ACOSD:
   11239          216 :     case GFC_ISYM_ASIND:
   11240          216 :     case GFC_ISYM_ATAND:
   11241          216 :       gfc_conv_intrinsic_atrigd (se, expr, expr->value.function.isym->id);
   11242          216 :       break;
   11243              : 
   11244          102 :     case GFC_ISYM_COTAN:
   11245          102 :       gfc_conv_intrinsic_cotan (se, expr);
   11246          102 :       break;
   11247              : 
   11248          108 :     case GFC_ISYM_COTAND:
   11249          108 :       gfc_conv_intrinsic_cotand (se, expr);
   11250          108 :       break;
   11251              : 
   11252          120 :     case GFC_ISYM_ATAN2D:
   11253          120 :       gfc_conv_intrinsic_atan2d (se, expr);
   11254          120 :       break;
   11255              : 
   11256          145 :     case GFC_ISYM_BTEST:
   11257          145 :       gfc_conv_intrinsic_btest (se, expr);
   11258          145 :       break;
   11259              : 
   11260           54 :     case GFC_ISYM_BGE:
   11261           54 :       gfc_conv_intrinsic_bitcomp (se, expr, GE_EXPR);
   11262           54 :       break;
   11263              : 
   11264           54 :     case GFC_ISYM_BGT:
   11265           54 :       gfc_conv_intrinsic_bitcomp (se, expr, GT_EXPR);
   11266           54 :       break;
   11267              : 
   11268           54 :     case GFC_ISYM_BLE:
   11269           54 :       gfc_conv_intrinsic_bitcomp (se, expr, LE_EXPR);
   11270           54 :       break;
   11271              : 
   11272           54 :     case GFC_ISYM_BLT:
   11273           54 :       gfc_conv_intrinsic_bitcomp (se, expr, LT_EXPR);
   11274           54 :       break;
   11275              : 
   11276         9697 :     case GFC_ISYM_C_ASSOCIATED:
   11277         9697 :     case GFC_ISYM_C_FUNLOC:
   11278         9697 :     case GFC_ISYM_C_LOC:
   11279         9697 :     case GFC_ISYM_F_C_STRING:
   11280         9697 :       conv_isocbinding_function (se, expr);
   11281         9697 :       break;
   11282              : 
   11283         2020 :     case GFC_ISYM_ACHAR:
   11284         2020 :     case GFC_ISYM_CHAR:
   11285         2020 :       gfc_conv_intrinsic_char (se, expr);
   11286         2020 :       break;
   11287              : 
   11288        39742 :     case GFC_ISYM_CONVERSION:
   11289        39742 :     case GFC_ISYM_DBLE:
   11290        39742 :     case GFC_ISYM_DFLOAT:
   11291        39742 :     case GFC_ISYM_FLOAT:
   11292        39742 :     case GFC_ISYM_LOGICAL:
   11293        39742 :     case GFC_ISYM_REAL:
   11294        39742 :     case GFC_ISYM_REALPART:
   11295        39742 :     case GFC_ISYM_SNGL:
   11296        39742 :       gfc_conv_intrinsic_conversion (se, expr);
   11297        39742 :       break;
   11298              : 
   11299              :       /* Integer conversions are handled separately to make sure we get the
   11300              :          correct rounding mode.  */
   11301         2836 :     case GFC_ISYM_INT:
   11302         2836 :     case GFC_ISYM_INT2:
   11303         2836 :     case GFC_ISYM_INT8:
   11304         2836 :     case GFC_ISYM_LONG:
   11305         2836 :     case GFC_ISYM_UINT:
   11306         2836 :       gfc_conv_intrinsic_int (se, expr, RND_TRUNC);
   11307         2836 :       break;
   11308              : 
   11309          162 :     case GFC_ISYM_NINT:
   11310          162 :       gfc_conv_intrinsic_int (se, expr, RND_ROUND);
   11311          162 :       break;
   11312              : 
   11313           16 :     case GFC_ISYM_CEILING:
   11314           16 :       gfc_conv_intrinsic_int (se, expr, RND_CEIL);
   11315           16 :       break;
   11316              : 
   11317          116 :     case GFC_ISYM_FLOOR:
   11318          116 :       gfc_conv_intrinsic_int (se, expr, RND_FLOOR);
   11319          116 :       break;
   11320              : 
   11321         3196 :     case GFC_ISYM_MOD:
   11322         3196 :       gfc_conv_intrinsic_mod (se, expr, 0);
   11323         3196 :       break;
   11324              : 
   11325          442 :     case GFC_ISYM_MODULO:
   11326          442 :       gfc_conv_intrinsic_mod (se, expr, 1);
   11327          442 :       break;
   11328              : 
   11329         1006 :     case GFC_ISYM_CAF_GET:
   11330         1006 :       gfc_conv_intrinsic_caf_get (se, expr, NULL_TREE, false, NULL);
   11331         1006 :       break;
   11332              : 
   11333          167 :     case GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE:
   11334          167 :       gfc_conv_intrinsic_caf_is_present_remote (se, expr);
   11335          167 :       break;
   11336              : 
   11337          485 :     case GFC_ISYM_CMPLX:
   11338          485 :       gfc_conv_intrinsic_cmplx (se, expr, name[5] == '1');
   11339          485 :       break;
   11340              : 
   11341           10 :     case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
   11342           10 :       gfc_conv_intrinsic_iargc (se, expr);
   11343           10 :       break;
   11344              : 
   11345            6 :     case GFC_ISYM_COMPLEX:
   11346            6 :       gfc_conv_intrinsic_cmplx (se, expr, 1);
   11347            6 :       break;
   11348              : 
   11349          257 :     case GFC_ISYM_CONJG:
   11350          257 :       gfc_conv_intrinsic_conjg (se, expr);
   11351          257 :       break;
   11352              : 
   11353            4 :     case GFC_ISYM_COSHAPE:
   11354            4 :       conv_intrinsic_cobound (se, expr);
   11355            4 :       break;
   11356              : 
   11357          143 :     case GFC_ISYM_COUNT:
   11358          143 :       gfc_conv_intrinsic_count (se, expr);
   11359          143 :       break;
   11360              : 
   11361            0 :     case GFC_ISYM_CTIME:
   11362            0 :       gfc_conv_intrinsic_ctime (se, expr);
   11363            0 :       break;
   11364              : 
   11365           96 :     case GFC_ISYM_DIM:
   11366           96 :       gfc_conv_intrinsic_dim (se, expr);
   11367           96 :       break;
   11368              : 
   11369          113 :     case GFC_ISYM_DOT_PRODUCT:
   11370          113 :       gfc_conv_intrinsic_dot_product (se, expr);
   11371          113 :       break;
   11372              : 
   11373           13 :     case GFC_ISYM_DPROD:
   11374           13 :       gfc_conv_intrinsic_dprod (se, expr);
   11375           13 :       break;
   11376              : 
   11377           66 :     case GFC_ISYM_DSHIFTL:
   11378           66 :       gfc_conv_intrinsic_dshift (se, expr, true);
   11379           66 :       break;
   11380              : 
   11381           66 :     case GFC_ISYM_DSHIFTR:
   11382           66 :       gfc_conv_intrinsic_dshift (se, expr, false);
   11383           66 :       break;
   11384              : 
   11385            0 :     case GFC_ISYM_FDATE:
   11386            0 :       gfc_conv_intrinsic_fdate (se, expr);
   11387            0 :       break;
   11388              : 
   11389           60 :     case GFC_ISYM_FRACTION:
   11390           60 :       gfc_conv_intrinsic_fraction (se, expr);
   11391           60 :       break;
   11392              : 
   11393           24 :     case GFC_ISYM_IALL:
   11394           24 :       gfc_conv_intrinsic_arith (se, expr, BIT_AND_EXPR, false);
   11395           24 :       break;
   11396              : 
   11397          606 :     case GFC_ISYM_IAND:
   11398          606 :       gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
   11399          606 :       break;
   11400              : 
   11401           12 :     case GFC_ISYM_IANY:
   11402           12 :       gfc_conv_intrinsic_arith (se, expr, BIT_IOR_EXPR, false);
   11403           12 :       break;
   11404              : 
   11405          168 :     case GFC_ISYM_IBCLR:
   11406          168 :       gfc_conv_intrinsic_singlebitop (se, expr, 0);
   11407          168 :       break;
   11408              : 
   11409           27 :     case GFC_ISYM_IBITS:
   11410           27 :       gfc_conv_intrinsic_ibits (se, expr);
   11411           27 :       break;
   11412              : 
   11413          138 :     case GFC_ISYM_IBSET:
   11414          138 :       gfc_conv_intrinsic_singlebitop (se, expr, 1);
   11415          138 :       break;
   11416              : 
   11417         2033 :     case GFC_ISYM_IACHAR:
   11418         2033 :     case GFC_ISYM_ICHAR:
   11419              :       /* We assume ASCII character sequence.  */
   11420         2033 :       gfc_conv_intrinsic_ichar (se, expr);
   11421         2033 :       break;
   11422              : 
   11423            2 :     case GFC_ISYM_IARGC:
   11424            2 :       gfc_conv_intrinsic_iargc (se, expr);
   11425            2 :       break;
   11426              : 
   11427          694 :     case GFC_ISYM_IEOR:
   11428          694 :       gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
   11429          694 :       break;
   11430              : 
   11431          341 :     case GFC_ISYM_INDEX:
   11432          341 :       kind = expr->value.function.actual->expr->ts.kind;
   11433          341 :       if (kind == 1)
   11434          275 :        fndecl = gfor_fndecl_string_index;
   11435           66 :       else if (kind == 4)
   11436           66 :        fndecl = gfor_fndecl_string_index_char4;
   11437              :       else
   11438            0 :        gcc_unreachable ();
   11439              : 
   11440          341 :       gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
   11441          341 :       break;
   11442              : 
   11443          495 :     case GFC_ISYM_IOR:
   11444          495 :       gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
   11445          495 :       break;
   11446              : 
   11447           12 :     case GFC_ISYM_IPARITY:
   11448           12 :       gfc_conv_intrinsic_arith (se, expr, BIT_XOR_EXPR, false);
   11449           12 :       break;
   11450              : 
   11451            6 :     case GFC_ISYM_IS_IOSTAT_END:
   11452            6 :       gfc_conv_has_intvalue (se, expr, LIBERROR_END);
   11453            6 :       break;
   11454              : 
   11455           18 :     case GFC_ISYM_IS_IOSTAT_EOR:
   11456           18 :       gfc_conv_has_intvalue (se, expr, LIBERROR_EOR);
   11457           18 :       break;
   11458              : 
   11459          735 :     case GFC_ISYM_IS_CONTIGUOUS:
   11460          735 :       gfc_conv_intrinsic_is_contiguous (se, expr);
   11461          735 :       break;
   11462              : 
   11463          432 :     case GFC_ISYM_ISNAN:
   11464          432 :       gfc_conv_intrinsic_isnan (se, expr);
   11465          432 :       break;
   11466              : 
   11467            8 :     case GFC_ISYM_KILL:
   11468            8 :       conv_intrinsic_kill (se, expr);
   11469            8 :       break;
   11470              : 
   11471           90 :     case GFC_ISYM_LSHIFT:
   11472           90 :       gfc_conv_intrinsic_shift (se, expr, false, false);
   11473           90 :       break;
   11474              : 
   11475           24 :     case GFC_ISYM_RSHIFT:
   11476           24 :       gfc_conv_intrinsic_shift (se, expr, true, true);
   11477           24 :       break;
   11478              : 
   11479           78 :     case GFC_ISYM_SHIFTA:
   11480           78 :       gfc_conv_intrinsic_shift (se, expr, true, true);
   11481           78 :       break;
   11482              : 
   11483          234 :     case GFC_ISYM_SHIFTL:
   11484          234 :       gfc_conv_intrinsic_shift (se, expr, false, false);
   11485          234 :       break;
   11486              : 
   11487           66 :     case GFC_ISYM_SHIFTR:
   11488           66 :       gfc_conv_intrinsic_shift (se, expr, true, false);
   11489           66 :       break;
   11490              : 
   11491          318 :     case GFC_ISYM_ISHFT:
   11492          318 :       gfc_conv_intrinsic_ishft (se, expr);
   11493          318 :       break;
   11494              : 
   11495          658 :     case GFC_ISYM_ISHFTC:
   11496          658 :       gfc_conv_intrinsic_ishftc (se, expr);
   11497          658 :       break;
   11498              : 
   11499          270 :     case GFC_ISYM_LEADZ:
   11500          270 :       gfc_conv_intrinsic_leadz (se, expr);
   11501          270 :       break;
   11502              : 
   11503          282 :     case GFC_ISYM_TRAILZ:
   11504          282 :       gfc_conv_intrinsic_trailz (se, expr);
   11505          282 :       break;
   11506              : 
   11507          103 :     case GFC_ISYM_POPCNT:
   11508          103 :       gfc_conv_intrinsic_popcnt_poppar (se, expr, 0);
   11509          103 :       break;
   11510              : 
   11511           31 :     case GFC_ISYM_POPPAR:
   11512           31 :       gfc_conv_intrinsic_popcnt_poppar (se, expr, 1);
   11513           31 :       break;
   11514              : 
   11515         5527 :     case GFC_ISYM_LBOUND:
   11516         5527 :       gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_LBOUND);
   11517         5527 :       break;
   11518              : 
   11519          210 :     case GFC_ISYM_LCOBOUND:
   11520          210 :       conv_intrinsic_cobound (se, expr);
   11521          210 :       break;
   11522              : 
   11523          744 :     case GFC_ISYM_TRANSPOSE:
   11524              :       /* The scalarizer has already been set up for reversed dimension access
   11525              :          order ; now we just get the argument value normally.  */
   11526          744 :       gfc_conv_expr (se, expr->value.function.actual->expr);
   11527          744 :       break;
   11528              : 
   11529         5765 :     case GFC_ISYM_LEN:
   11530         5765 :       gfc_conv_intrinsic_len (se, expr);
   11531         5765 :       break;
   11532              : 
   11533         2335 :     case GFC_ISYM_LEN_TRIM:
   11534         2335 :       gfc_conv_intrinsic_len_trim (se, expr);
   11535         2335 :       break;
   11536              : 
   11537           18 :     case GFC_ISYM_LGE:
   11538           18 :       gfc_conv_intrinsic_strcmp (se, expr, GE_EXPR);
   11539           18 :       break;
   11540              : 
   11541           36 :     case GFC_ISYM_LGT:
   11542           36 :       gfc_conv_intrinsic_strcmp (se, expr, GT_EXPR);
   11543           36 :       break;
   11544              : 
   11545           18 :     case GFC_ISYM_LLE:
   11546           18 :       gfc_conv_intrinsic_strcmp (se, expr, LE_EXPR);
   11547           18 :       break;
   11548              : 
   11549           27 :     case GFC_ISYM_LLT:
   11550           27 :       gfc_conv_intrinsic_strcmp (se, expr, LT_EXPR);
   11551           27 :       break;
   11552              : 
   11553           16 :     case GFC_ISYM_MALLOC:
   11554           16 :       gfc_conv_intrinsic_malloc (se, expr);
   11555           16 :       break;
   11556              : 
   11557           32 :     case GFC_ISYM_MASKL:
   11558           32 :       gfc_conv_intrinsic_mask (se, expr, 1);
   11559           32 :       break;
   11560              : 
   11561           32 :     case GFC_ISYM_MASKR:
   11562           32 :       gfc_conv_intrinsic_mask (se, expr, 0);
   11563           32 :       break;
   11564              : 
   11565         1049 :     case GFC_ISYM_MAX:
   11566         1049 :       if (expr->ts.type == BT_CHARACTER)
   11567          138 :         gfc_conv_intrinsic_minmax_char (se, expr, 1);
   11568              :       else
   11569          911 :         gfc_conv_intrinsic_minmax (se, expr, GT_EXPR);
   11570              :       break;
   11571              : 
   11572         6348 :     case GFC_ISYM_MAXLOC:
   11573         6348 :       gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
   11574         6348 :       break;
   11575              : 
   11576          216 :     case GFC_ISYM_FINDLOC:
   11577          216 :       gfc_conv_intrinsic_findloc (se, expr);
   11578          216 :       break;
   11579              : 
   11580         1101 :     case GFC_ISYM_MAXVAL:
   11581         1101 :       gfc_conv_intrinsic_minmaxval (se, expr, GT_EXPR);
   11582         1101 :       break;
   11583              : 
   11584          949 :     case GFC_ISYM_MERGE:
   11585          949 :       gfc_conv_intrinsic_merge (se, expr);
   11586          949 :       break;
   11587              : 
   11588           42 :     case GFC_ISYM_MERGE_BITS:
   11589           42 :       gfc_conv_intrinsic_merge_bits (se, expr);
   11590           42 :       break;
   11591              : 
   11592          598 :     case GFC_ISYM_MIN:
   11593          598 :       if (expr->ts.type == BT_CHARACTER)
   11594          144 :         gfc_conv_intrinsic_minmax_char (se, expr, -1);
   11595              :       else
   11596          454 :         gfc_conv_intrinsic_minmax (se, expr, LT_EXPR);
   11597              :       break;
   11598              : 
   11599         7176 :     case GFC_ISYM_MINLOC:
   11600         7176 :       gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
   11601         7176 :       break;
   11602              : 
   11603         1316 :     case GFC_ISYM_MINVAL:
   11604         1316 :       gfc_conv_intrinsic_minmaxval (se, expr, LT_EXPR);
   11605         1316 :       break;
   11606              : 
   11607         1595 :     case GFC_ISYM_NEAREST:
   11608         1595 :       gfc_conv_intrinsic_nearest (se, expr);
   11609         1595 :       break;
   11610              : 
   11611           68 :     case GFC_ISYM_NORM2:
   11612           68 :       gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, true);
   11613           68 :       break;
   11614              : 
   11615          230 :     case GFC_ISYM_NOT:
   11616          230 :       gfc_conv_intrinsic_not (se, expr);
   11617          230 :       break;
   11618              : 
   11619           12 :     case GFC_ISYM_OR:
   11620           12 :       gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
   11621           12 :       break;
   11622              : 
   11623          468 :     case GFC_ISYM_OUT_OF_RANGE:
   11624          468 :       gfc_conv_intrinsic_out_of_range (se, expr);
   11625          468 :       break;
   11626              : 
   11627           36 :     case GFC_ISYM_PARITY:
   11628           36 :       gfc_conv_intrinsic_arith (se, expr, NE_EXPR, false);
   11629           36 :       break;
   11630              : 
   11631         5070 :     case GFC_ISYM_PRESENT:
   11632         5070 :       gfc_conv_intrinsic_present (se, expr);
   11633         5070 :       break;
   11634              : 
   11635          358 :     case GFC_ISYM_PRODUCT:
   11636          358 :       gfc_conv_intrinsic_arith (se, expr, MULT_EXPR, false);
   11637          358 :       break;
   11638              : 
   11639        12457 :     case GFC_ISYM_RANK:
   11640        12457 :       gfc_conv_intrinsic_rank (se, expr);
   11641        12457 :       break;
   11642              : 
   11643           48 :     case GFC_ISYM_RRSPACING:
   11644           48 :       gfc_conv_intrinsic_rrspacing (se, expr);
   11645           48 :       break;
   11646              : 
   11647          262 :     case GFC_ISYM_SET_EXPONENT:
   11648          262 :       gfc_conv_intrinsic_set_exponent (se, expr);
   11649          262 :       break;
   11650              : 
   11651           72 :     case GFC_ISYM_SCALE:
   11652           72 :       gfc_conv_intrinsic_scale (se, expr);
   11653           72 :       break;
   11654              : 
   11655         4940 :     case GFC_ISYM_SHAPE:
   11656         4940 :       gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_SHAPE);
   11657         4940 :       break;
   11658              : 
   11659          423 :     case GFC_ISYM_SIGN:
   11660          423 :       gfc_conv_intrinsic_sign (se, expr);
   11661          423 :       break;
   11662              : 
   11663        15139 :     case GFC_ISYM_SIZE:
   11664        15139 :       gfc_conv_intrinsic_size (se, expr);
   11665        15139 :       break;
   11666              : 
   11667         1309 :     case GFC_ISYM_SIZEOF:
   11668         1309 :     case GFC_ISYM_C_SIZEOF:
   11669         1309 :       gfc_conv_intrinsic_sizeof (se, expr);
   11670         1309 :       break;
   11671              : 
   11672          834 :     case GFC_ISYM_STORAGE_SIZE:
   11673          834 :       gfc_conv_intrinsic_storage_size (se, expr);
   11674          834 :       break;
   11675              : 
   11676           70 :     case GFC_ISYM_SPACING:
   11677           70 :       gfc_conv_intrinsic_spacing (se, expr);
   11678           70 :       break;
   11679              : 
   11680         2256 :     case GFC_ISYM_STRIDE:
   11681         2256 :       conv_intrinsic_stride (se, expr);
   11682         2256 :       break;
   11683              : 
   11684         2003 :     case GFC_ISYM_SUM:
   11685         2003 :       gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, false);
   11686         2003 :       break;
   11687              : 
   11688           21 :     case GFC_ISYM_TEAM_NUMBER:
   11689           21 :       conv_intrinsic_team_number (se, expr);
   11690           21 :       break;
   11691              : 
   11692         4059 :     case GFC_ISYM_TRANSFER:
   11693         4059 :       if (se->ss && se->ss->info->useflags)
   11694              :         /* Access the previously obtained result.  */
   11695          281 :         gfc_conv_tmp_array_ref (se);
   11696              :       else
   11697         3778 :         gfc_conv_intrinsic_transfer (se, expr);
   11698              :       break;
   11699              : 
   11700            0 :     case GFC_ISYM_TTYNAM:
   11701            0 :       gfc_conv_intrinsic_ttynam (se, expr);
   11702            0 :       break;
   11703              : 
   11704         5711 :     case GFC_ISYM_UBOUND:
   11705         5711 :       gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_UBOUND);
   11706         5711 :       break;
   11707              : 
   11708          244 :     case GFC_ISYM_UCOBOUND:
   11709          244 :       conv_intrinsic_cobound (se, expr);
   11710          244 :       break;
   11711              : 
   11712           18 :     case GFC_ISYM_XOR:
   11713           18 :       gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
   11714           18 :       break;
   11715              : 
   11716         8816 :     case GFC_ISYM_LOC:
   11717         8816 :       gfc_conv_intrinsic_loc (se, expr);
   11718         8816 :       break;
   11719              : 
   11720         1505 :     case GFC_ISYM_THIS_IMAGE:
   11721              :       /* For num_images() == 1, handle as LCOBOUND.  */
   11722         1505 :       if (expr->value.function.actual->expr
   11723          526 :           && flag_coarray == GFC_FCOARRAY_SINGLE)
   11724          208 :         conv_intrinsic_cobound (se, expr);
   11725              :       else
   11726         1297 :         trans_this_image (se, expr);
   11727              :       break;
   11728              : 
   11729          193 :     case GFC_ISYM_IMAGE_INDEX:
   11730          193 :       trans_image_index (se, expr);
   11731          193 :       break;
   11732              : 
   11733           25 :     case GFC_ISYM_IMAGE_STATUS:
   11734           25 :       conv_intrinsic_image_status (se, expr);
   11735           25 :       break;
   11736              : 
   11737          810 :     case GFC_ISYM_NUM_IMAGES:
   11738          810 :       trans_num_images (se, expr);
   11739          810 :       break;
   11740              : 
   11741         1392 :     case GFC_ISYM_ACCESS:
   11742         1392 :     case GFC_ISYM_CHDIR:
   11743         1392 :     case GFC_ISYM_CHMOD:
   11744         1392 :     case GFC_ISYM_DTIME:
   11745         1392 :     case GFC_ISYM_ETIME:
   11746         1392 :     case GFC_ISYM_EXTENDS_TYPE_OF:
   11747         1392 :     case GFC_ISYM_FGET:
   11748         1392 :     case GFC_ISYM_FGETC:
   11749         1392 :     case GFC_ISYM_FNUM:
   11750         1392 :     case GFC_ISYM_FPUT:
   11751         1392 :     case GFC_ISYM_FPUTC:
   11752         1392 :     case GFC_ISYM_FSTAT:
   11753         1392 :     case GFC_ISYM_FTELL:
   11754         1392 :     case GFC_ISYM_GETCWD:
   11755         1392 :     case GFC_ISYM_GETGID:
   11756         1392 :     case GFC_ISYM_GETPID:
   11757         1392 :     case GFC_ISYM_GETUID:
   11758         1392 :     case GFC_ISYM_GET_TEAM:
   11759         1392 :     case GFC_ISYM_HOSTNM:
   11760         1392 :     case GFC_ISYM_IERRNO:
   11761         1392 :     case GFC_ISYM_IRAND:
   11762         1392 :     case GFC_ISYM_ISATTY:
   11763         1392 :     case GFC_ISYM_JN2:
   11764         1392 :     case GFC_ISYM_LINK:
   11765         1392 :     case GFC_ISYM_LSTAT:
   11766         1392 :     case GFC_ISYM_MATMUL:
   11767         1392 :     case GFC_ISYM_MCLOCK:
   11768         1392 :     case GFC_ISYM_MCLOCK8:
   11769         1392 :     case GFC_ISYM_RAND:
   11770         1392 :     case GFC_ISYM_REDUCE:
   11771         1392 :     case GFC_ISYM_RENAME:
   11772         1392 :     case GFC_ISYM_SECOND:
   11773         1392 :     case GFC_ISYM_SECNDS:
   11774         1392 :     case GFC_ISYM_SIGNAL:
   11775         1392 :     case GFC_ISYM_STAT:
   11776         1392 :     case GFC_ISYM_SYMLNK:
   11777         1392 :     case GFC_ISYM_SYSTEM:
   11778         1392 :     case GFC_ISYM_TIME:
   11779         1392 :     case GFC_ISYM_TIME8:
   11780         1392 :     case GFC_ISYM_UMASK:
   11781         1392 :     case GFC_ISYM_UNLINK:
   11782         1392 :     case GFC_ISYM_YN2:
   11783         1392 :       gfc_conv_intrinsic_funcall (se, expr);
   11784         1392 :       break;
   11785              : 
   11786            0 :     case GFC_ISYM_EOSHIFT:
   11787            0 :     case GFC_ISYM_PACK:
   11788            0 :     case GFC_ISYM_RESHAPE:
   11789              :       /* For those, expr->rank should always be >0 and thus the if above the
   11790              :          switch should have matched.  */
   11791            0 :       gcc_unreachable ();
   11792         3875 :       break;
   11793              : 
   11794         3875 :     default:
   11795         3875 :       gfc_conv_intrinsic_lib_function (se, expr);
   11796         3875 :       break;
   11797              :     }
   11798              : }
   11799              : 
   11800              : 
   11801              : static gfc_ss *
   11802         1560 : walk_inline_intrinsic_transpose (gfc_ss *ss, gfc_expr *expr)
   11803              : {
   11804         1560 :   gfc_ss *arg_ss, *tmp_ss;
   11805         1560 :   gfc_actual_arglist *arg;
   11806              : 
   11807         1560 :   arg = expr->value.function.actual;
   11808              : 
   11809         1560 :   gcc_assert (arg->expr);
   11810              : 
   11811         1560 :   arg_ss = gfc_walk_subexpr (gfc_ss_terminator, arg->expr);
   11812         1560 :   gcc_assert (arg_ss != gfc_ss_terminator);
   11813              : 
   11814              :   for (tmp_ss = arg_ss; ; tmp_ss = tmp_ss->next)
   11815              :     {
   11816         1665 :       if (tmp_ss->info->type != GFC_SS_SCALAR
   11817              :           && tmp_ss->info->type != GFC_SS_REFERENCE)
   11818              :         {
   11819         1628 :           gcc_assert (tmp_ss->dimen == 2);
   11820              : 
   11821              :           /* We just invert dimensions.  */
   11822         1628 :           std::swap (tmp_ss->dim[0], tmp_ss->dim[1]);
   11823              :         }
   11824              : 
   11825              :       /* Stop when tmp_ss points to the last valid element of the chain...  */
   11826         1665 :       if (tmp_ss->next == gfc_ss_terminator)
   11827              :         break;
   11828              :     }
   11829              : 
   11830              :   /* ... so that we can attach the rest of the chain to it.  */
   11831         1560 :   tmp_ss->next = ss;
   11832              : 
   11833         1560 :   return arg_ss;
   11834              : }
   11835              : 
   11836              : 
   11837              : /* Move the given dimension of the given gfc_ss list to a nested gfc_ss list.
   11838              :    This has the side effect of reversing the nested list, so there is no
   11839              :    need to call gfc_reverse_ss on it (the given list is assumed not to be
   11840              :    reversed yet).   */
   11841              : 
   11842              : static gfc_ss *
   11843         3371 : nest_loop_dimension (gfc_ss *ss, int dim)
   11844              : {
   11845         3371 :   int ss_dim, i;
   11846         3371 :   gfc_ss *new_ss, *prev_ss = gfc_ss_terminator;
   11847         3371 :   gfc_loopinfo *new_loop;
   11848              : 
   11849         3371 :   gcc_assert (ss != gfc_ss_terminator);
   11850              : 
   11851         8118 :   for (; ss != gfc_ss_terminator; ss = ss->next)
   11852              :     {
   11853         4747 :       new_ss = gfc_get_ss ();
   11854         4747 :       new_ss->next = prev_ss;
   11855         4747 :       new_ss->parent = ss;
   11856         4747 :       new_ss->info = ss->info;
   11857         4747 :       new_ss->info->refcount++;
   11858         4747 :       if (ss->dimen != 0)
   11859              :         {
   11860         4684 :           gcc_assert (ss->info->type != GFC_SS_SCALAR
   11861              :                       && ss->info->type != GFC_SS_REFERENCE);
   11862              : 
   11863         4684 :           new_ss->dimen = 1;
   11864         4684 :           new_ss->dim[0] = ss->dim[dim];
   11865              : 
   11866         4684 :           gcc_assert (dim < ss->dimen);
   11867              : 
   11868         4684 :           ss_dim = --ss->dimen;
   11869        10430 :           for (i = dim; i < ss_dim; i++)
   11870         5746 :             ss->dim[i] = ss->dim[i + 1];
   11871              : 
   11872         4684 :           ss->dim[ss_dim] = 0;
   11873              :         }
   11874         4747 :       prev_ss = new_ss;
   11875              : 
   11876         4747 :       if (ss->nested_ss)
   11877              :         {
   11878           81 :           ss->nested_ss->parent = new_ss;
   11879           81 :           new_ss->nested_ss = ss->nested_ss;
   11880              :         }
   11881         4747 :       ss->nested_ss = new_ss;
   11882              :     }
   11883              : 
   11884         3371 :   new_loop = gfc_get_loopinfo ();
   11885         3371 :   gfc_init_loopinfo (new_loop);
   11886              : 
   11887         3371 :   gcc_assert (prev_ss != NULL);
   11888         3371 :   gcc_assert (prev_ss != gfc_ss_terminator);
   11889         3371 :   gfc_add_ss_to_loop (new_loop, prev_ss);
   11890         3371 :   return new_ss->parent;
   11891              : }
   11892              : 
   11893              : 
   11894              : /* Create the gfc_ss list for the SUM/PRODUCT arguments when the function
   11895              :    is to be inlined.  */
   11896              : 
   11897              : static gfc_ss *
   11898          575 : walk_inline_intrinsic_arith (gfc_ss *ss, gfc_expr *expr)
   11899              : {
   11900          575 :   gfc_ss *tmp_ss, *tail, *array_ss;
   11901          575 :   gfc_actual_arglist *arg1, *arg2, *arg3;
   11902          575 :   int sum_dim;
   11903          575 :   bool scalar_mask = false;
   11904              : 
   11905              :   /* The rank of the result will be determined later.  */
   11906          575 :   arg1 = expr->value.function.actual;
   11907          575 :   arg2 = arg1->next;
   11908          575 :   arg3 = arg2->next;
   11909          575 :   gcc_assert (arg3 != NULL);
   11910              : 
   11911          575 :   if (expr->rank == 0)
   11912              :     return ss;
   11913              : 
   11914          575 :   tmp_ss = gfc_ss_terminator;
   11915              : 
   11916          575 :   if (arg3->expr)
   11917              :     {
   11918          118 :       gfc_ss *mask_ss;
   11919              : 
   11920          118 :       mask_ss = gfc_walk_subexpr (tmp_ss, arg3->expr);
   11921          118 :       if (mask_ss == tmp_ss)
   11922           34 :         scalar_mask = 1;
   11923              : 
   11924              :       tmp_ss = mask_ss;
   11925              :     }
   11926              : 
   11927          575 :   array_ss = gfc_walk_subexpr (tmp_ss, arg1->expr);
   11928          575 :   gcc_assert (array_ss != tmp_ss);
   11929              : 
   11930              :   /* Odd thing: If the mask is scalar, it is used by the frontend after
   11931              :      the array (to make an if around the nested loop). Thus it shall
   11932              :      be after array_ss once the gfc_ss list is reversed.  */
   11933          575 :   if (scalar_mask)
   11934           34 :     tmp_ss = gfc_get_scalar_ss (array_ss, arg3->expr);
   11935              :   else
   11936              :     tmp_ss = array_ss;
   11937              : 
   11938              :   /* "Hide" the dimension on which we will sum in the first arg's scalarization
   11939              :      chain.  */
   11940          575 :   sum_dim = mpz_get_si (arg2->expr->value.integer) - 1;
   11941          575 :   tail = nest_loop_dimension (tmp_ss, sum_dim);
   11942          575 :   tail->next = ss;
   11943              : 
   11944          575 :   return tmp_ss;
   11945              : }
   11946              : 
   11947              : 
   11948              : /* Create the gfc_ss list for the arguments to MINLOC or MAXLOC when the
   11949              :    function is to be inlined.  */
   11950              : 
   11951              : static gfc_ss *
   11952         6085 : walk_inline_intrinsic_minmaxloc (gfc_ss *ss, gfc_expr *expr ATTRIBUTE_UNUSED)
   11953              : {
   11954         6085 :   if (expr->rank == 0)
   11955              :     return ss;
   11956              : 
   11957         6085 :   gfc_actual_arglist *array_arg = expr->value.function.actual;
   11958         6085 :   gfc_actual_arglist *dim_arg = array_arg->next;
   11959         6085 :   gfc_actual_arglist *mask_arg = dim_arg->next;
   11960         6085 :   gfc_actual_arglist *kind_arg = mask_arg->next;
   11961         6085 :   gfc_actual_arglist *back_arg = kind_arg->next;
   11962              : 
   11963         6085 :   gfc_expr *array = array_arg->expr;
   11964         6085 :   gfc_expr *dim = dim_arg->expr;
   11965         6085 :   gfc_expr *mask = mask_arg->expr;
   11966         6085 :   gfc_expr *back = back_arg->expr;
   11967              : 
   11968         6085 :   if (dim == nullptr)
   11969         3289 :     return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
   11970              : 
   11971         2796 :   gfc_ss *tmp_ss = gfc_ss_terminator;
   11972              : 
   11973         2796 :   bool scalar_mask = false;
   11974         2796 :   if (mask)
   11975              :     {
   11976         1866 :       gfc_ss *mask_ss = gfc_walk_subexpr (tmp_ss, mask);
   11977         1866 :       if (mask_ss == tmp_ss)
   11978              :         scalar_mask = true;
   11979         1174 :       else if (maybe_absent_optional_variable (mask))
   11980           20 :         mask_ss->info->can_be_null_ref = true;
   11981              : 
   11982              :       tmp_ss = mask_ss;
   11983              :     }
   11984              : 
   11985         2796 :   gfc_ss *array_ss = gfc_walk_subexpr (tmp_ss, array);
   11986         2796 :   gcc_assert (array_ss != tmp_ss);
   11987              : 
   11988         2796 :   tmp_ss = array_ss;
   11989              : 
   11990              :   /* Move the dimension on which we will sum to a separate nested scalarization
   11991              :      chain, "hiding" that dimension from the outer scalarization.  */
   11992         2796 :   int dim_val = mpz_get_si (dim->value.integer);
   11993         2796 :   gfc_ss *tail = nest_loop_dimension (tmp_ss, dim_val - 1);
   11994              : 
   11995         2796 :   if (back && array->rank > 1)
   11996              :     {
   11997              :       /* If there are nested scalarization loops, include BACK in the
   11998              :          scalarization chains to avoid evaluating it multiple times in a loop.
   11999              :          Otherwise, prefer to handle it outside of scalarization.  */
   12000         2796 :       gfc_ss *back_ss = gfc_get_scalar_ss (ss, back);
   12001         2796 :       back_ss->info->type = GFC_SS_REFERENCE;
   12002         2796 :       if (maybe_absent_optional_variable (back))
   12003           16 :         back_ss->info->can_be_null_ref = true;
   12004              : 
   12005         2796 :       tail->next = back_ss;
   12006         2796 :     }
   12007              :   else
   12008            0 :     tail->next = ss;
   12009              : 
   12010         2796 :   if (scalar_mask)
   12011              :     {
   12012          692 :       tmp_ss = gfc_get_scalar_ss (tmp_ss, mask);
   12013              :       /* MASK can be a forwarded optional argument, so make the necessary setup
   12014              :          to avoid the scalarizer generating any unguarded pointer dereference in
   12015              :          that case.  */
   12016          692 :       tmp_ss->info->type = GFC_SS_REFERENCE;
   12017          692 :       if (maybe_absent_optional_variable (mask))
   12018            4 :         tmp_ss->info->can_be_null_ref = true;
   12019              :     }
   12020              : 
   12021              :   return tmp_ss;
   12022              : }
   12023              : 
   12024              : 
   12025              : static gfc_ss *
   12026         8220 : walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr)
   12027              : {
   12028              : 
   12029         8220 :   switch (expr->value.function.isym->id)
   12030              :     {
   12031          575 :       case GFC_ISYM_PRODUCT:
   12032          575 :       case GFC_ISYM_SUM:
   12033          575 :         return walk_inline_intrinsic_arith (ss, expr);
   12034              : 
   12035         1560 :       case GFC_ISYM_TRANSPOSE:
   12036         1560 :         return walk_inline_intrinsic_transpose (ss, expr);
   12037              : 
   12038         6085 :       case GFC_ISYM_MAXLOC:
   12039         6085 :       case GFC_ISYM_MINLOC:
   12040         6085 :         return walk_inline_intrinsic_minmaxloc (ss, expr);
   12041              : 
   12042            0 :       default:
   12043            0 :         gcc_unreachable ();
   12044              :     }
   12045              :   gcc_unreachable ();
   12046              : }
   12047              : 
   12048              : 
   12049              : /* This generates code to execute before entering the scalarization loop.
   12050              :    Currently does nothing.  */
   12051              : 
   12052              : void
   12053        11533 : gfc_add_intrinsic_ss_code (gfc_loopinfo * loop ATTRIBUTE_UNUSED, gfc_ss * ss)
   12054              : {
   12055        11533 :   switch (ss->info->expr->value.function.isym->id)
   12056              :     {
   12057        11533 :     case GFC_ISYM_UBOUND:
   12058        11533 :     case GFC_ISYM_LBOUND:
   12059        11533 :     case GFC_ISYM_COSHAPE:
   12060        11533 :     case GFC_ISYM_UCOBOUND:
   12061        11533 :     case GFC_ISYM_LCOBOUND:
   12062        11533 :     case GFC_ISYM_MAXLOC:
   12063        11533 :     case GFC_ISYM_MINLOC:
   12064        11533 :     case GFC_ISYM_THIS_IMAGE:
   12065        11533 :     case GFC_ISYM_SHAPE:
   12066        11533 :       break;
   12067              : 
   12068            0 :     default:
   12069            0 :       gcc_unreachable ();
   12070              :     }
   12071        11533 : }
   12072              : 
   12073              : 
   12074              : /* The LBOUND, LCOBOUND, UBOUND, UCOBOUND, and SHAPE intrinsics with
   12075              :    one parameter are expanded into code inside the scalarization loop.  */
   12076              : 
   12077              : static gfc_ss *
   12078        10089 : gfc_walk_intrinsic_bound (gfc_ss * ss, gfc_expr * expr)
   12079              : {
   12080        10089 :   if (expr->value.function.actual->expr->ts.type == BT_CLASS)
   12081          438 :     gfc_add_class_array_ref (expr->value.function.actual->expr);
   12082              : 
   12083              :   /* The two argument version returns a scalar.  */
   12084        10089 :   if (expr->value.function.isym->id != GFC_ISYM_SHAPE
   12085         3522 :       && expr->value.function.isym->id != GFC_ISYM_COSHAPE
   12086         3518 :       && expr->value.function.actual->next->expr)
   12087              :     return ss;
   12088              : 
   12089        10089 :   return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
   12090              : }
   12091              : 
   12092              : 
   12093              : /* Walk an intrinsic array libcall.  */
   12094              : 
   12095              : static gfc_ss *
   12096        14481 : gfc_walk_intrinsic_libfunc (gfc_ss * ss, gfc_expr * expr)
   12097              : {
   12098        14481 :   gcc_assert (expr->rank > 0);
   12099        14481 :   return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
   12100              : }
   12101              : 
   12102              : 
   12103              : /* Return whether the function call expression EXPR will be expanded
   12104              :    inline by gfc_conv_intrinsic_function.  */
   12105              : 
   12106              : bool
   12107       300511 : gfc_inline_intrinsic_function_p (gfc_expr *expr)
   12108              : {
   12109       300511 :   gfc_actual_arglist *args, *dim_arg, *mask_arg;
   12110       300511 :   gfc_expr *maskexpr;
   12111              : 
   12112       300511 :   gfc_intrinsic_sym *isym = expr->value.function.isym;
   12113       300511 :   if (!isym)
   12114              :     return false;
   12115              : 
   12116       300469 :   switch (isym->id)
   12117              :     {
   12118         5104 :     case GFC_ISYM_PRODUCT:
   12119         5104 :     case GFC_ISYM_SUM:
   12120              :       /* Disable inline expansion if code size matters.  */
   12121         5104 :       if (optimize_size)
   12122              :         return false;
   12123              : 
   12124         4249 :       args = expr->value.function.actual;
   12125         4249 :       dim_arg = args->next;
   12126              : 
   12127              :       /* We need to be able to subset the SUM argument at compile-time.  */
   12128         4249 :       if (dim_arg->expr && dim_arg->expr->expr_type != EXPR_CONSTANT)
   12129              :         return false;
   12130              : 
   12131              :       /* FIXME: If MASK is optional for a more than two-dimensional
   12132              :          argument, the scalarizer gets confused if the mask is
   12133              :          absent.  See PR 82995.  For now, fall back to the library
   12134              :          function.  */
   12135              : 
   12136         3637 :       mask_arg = dim_arg->next;
   12137         3637 :       maskexpr = mask_arg->expr;
   12138              : 
   12139         3637 :       if (expr->rank > 0 && maskexpr && maskexpr->expr_type == EXPR_VARIABLE
   12140          276 :           && maskexpr->symtree->n.sym->attr.dummy
   12141           48 :           && maskexpr->symtree->n.sym->attr.optional)
   12142              :         return false;
   12143              : 
   12144              :       return true;
   12145              : 
   12146              :     case GFC_ISYM_TRANSPOSE:
   12147              :       return true;
   12148              : 
   12149        57188 :     case GFC_ISYM_MINLOC:
   12150        57188 :     case GFC_ISYM_MAXLOC:
   12151        57188 :       {
   12152        57188 :         if ((isym->id == GFC_ISYM_MINLOC
   12153        30521 :              && (flag_inline_intrinsics
   12154        30521 :                  & GFC_FLAG_INLINE_INTRINSIC_MINLOC) == 0)
   12155        46611 :             || (isym->id == GFC_ISYM_MAXLOC
   12156        26667 :                 && (flag_inline_intrinsics
   12157        26667 :                     & GFC_FLAG_INLINE_INTRINSIC_MAXLOC) == 0))
   12158              :           return false;
   12159              : 
   12160        37638 :         gfc_actual_arglist *array_arg = expr->value.function.actual;
   12161        37638 :         gfc_actual_arglist *dim_arg = array_arg->next;
   12162              : 
   12163        37638 :         gfc_expr *array = array_arg->expr;
   12164        37638 :         gfc_expr *dim = dim_arg->expr;
   12165              : 
   12166        37638 :         if (!(array->ts.type == BT_INTEGER
   12167              :               || array->ts.type == BT_REAL))
   12168              :           return false;
   12169              : 
   12170        34658 :         if (array->rank == 1)
   12171              :           return true;
   12172              : 
   12173        20711 :         if (dim != nullptr
   12174        13372 :             && dim->expr_type != EXPR_CONSTANT)
   12175              :           return false;
   12176              : 
   12177              :         return true;
   12178              :       }
   12179              : 
   12180              :     default:
   12181              :       return false;
   12182              :     }
   12183              : }
   12184              : 
   12185              : 
   12186              : /* Returns nonzero if the specified intrinsic function call maps directly to
   12187              :    an external library call.  Should only be used for functions that return
   12188              :    arrays.  */
   12189              : 
   12190              : int
   12191        87716 : gfc_is_intrinsic_libcall (gfc_expr * expr)
   12192              : {
   12193        87716 :   gcc_assert (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym);
   12194        87716 :   gcc_assert (expr->rank > 0);
   12195              : 
   12196        87716 :   if (gfc_inline_intrinsic_function_p (expr))
   12197              :     return 0;
   12198              : 
   12199        73135 :   switch (expr->value.function.isym->id)
   12200              :     {
   12201              :     case GFC_ISYM_ALL:
   12202              :     case GFC_ISYM_ANY:
   12203              :     case GFC_ISYM_COUNT:
   12204              :     case GFC_ISYM_FINDLOC:
   12205              :     case GFC_ISYM_JN2:
   12206              :     case GFC_ISYM_IANY:
   12207              :     case GFC_ISYM_IALL:
   12208              :     case GFC_ISYM_IPARITY:
   12209              :     case GFC_ISYM_MATMUL:
   12210              :     case GFC_ISYM_MAXLOC:
   12211              :     case GFC_ISYM_MAXVAL:
   12212              :     case GFC_ISYM_MINLOC:
   12213              :     case GFC_ISYM_MINVAL:
   12214              :     case GFC_ISYM_NORM2:
   12215              :     case GFC_ISYM_PARITY:
   12216              :     case GFC_ISYM_PRODUCT:
   12217              :     case GFC_ISYM_SUM:
   12218              :     case GFC_ISYM_SPREAD:
   12219              :     case GFC_ISYM_YN2:
   12220              :       /* Ignore absent optional parameters.  */
   12221              :       return 1;
   12222              : 
   12223        15765 :     case GFC_ISYM_CSHIFT:
   12224        15765 :     case GFC_ISYM_EOSHIFT:
   12225        15765 :     case GFC_ISYM_GET_TEAM:
   12226        15765 :     case GFC_ISYM_FAILED_IMAGES:
   12227        15765 :     case GFC_ISYM_STOPPED_IMAGES:
   12228        15765 :     case GFC_ISYM_PACK:
   12229        15765 :     case GFC_ISYM_REDUCE:
   12230        15765 :     case GFC_ISYM_RESHAPE:
   12231        15765 :     case GFC_ISYM_UNPACK:
   12232              :       /* Pass absent optional parameters.  */
   12233        15765 :       return 2;
   12234              : 
   12235              :     default:
   12236              :       return 0;
   12237              :     }
   12238              : }
   12239              : 
   12240              : /* Walk an intrinsic function.  */
   12241              : gfc_ss *
   12242        55601 : gfc_walk_intrinsic_function (gfc_ss * ss, gfc_expr * expr,
   12243              :                              gfc_intrinsic_sym * isym)
   12244              : {
   12245        55601 :   gcc_assert (isym);
   12246              : 
   12247        55601 :   if (isym->elemental)
   12248        18345 :     return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
   12249              :                                              expr->value.function.isym,
   12250        18345 :                                              GFC_SS_SCALAR);
   12251              : 
   12252        37256 :   if (expr->rank == 0 && expr->corank == 0)
   12253              :     return ss;
   12254              : 
   12255        32790 :   if (gfc_inline_intrinsic_function_p (expr))
   12256         8220 :     return walk_inline_intrinsic_function (ss, expr);
   12257              : 
   12258        24570 :   if (expr->rank != 0 && gfc_is_intrinsic_libcall (expr))
   12259        13498 :     return gfc_walk_intrinsic_libfunc (ss, expr);
   12260              : 
   12261              :   /* Special cases.  */
   12262        11072 :   switch (isym->id)
   12263              :     {
   12264        10089 :     case GFC_ISYM_COSHAPE:
   12265        10089 :     case GFC_ISYM_LBOUND:
   12266        10089 :     case GFC_ISYM_LCOBOUND:
   12267        10089 :     case GFC_ISYM_UBOUND:
   12268        10089 :     case GFC_ISYM_UCOBOUND:
   12269        10089 :     case GFC_ISYM_THIS_IMAGE:
   12270        10089 :     case GFC_ISYM_SHAPE:
   12271        10089 :       return gfc_walk_intrinsic_bound (ss, expr);
   12272              : 
   12273          983 :     case GFC_ISYM_TRANSFER:
   12274          983 :     case GFC_ISYM_CAF_GET:
   12275          983 :       return gfc_walk_intrinsic_libfunc (ss, expr);
   12276              : 
   12277            0 :     default:
   12278              :       /* This probably meant someone forgot to add an intrinsic to the above
   12279              :          list(s) when they implemented it, or something's gone horribly
   12280              :          wrong.  */
   12281            0 :       gcc_unreachable ();
   12282              :     }
   12283              : }
   12284              : 
   12285              : static tree
   12286           88 : conv_co_collective (gfc_code *code)
   12287              : {
   12288           88 :   gfc_se argse;
   12289           88 :   stmtblock_t block, post_block;
   12290           88 :   tree fndecl, array = NULL_TREE, strlen, image_index, stat, errmsg, errmsg_len;
   12291           88 :   gfc_expr *image_idx_expr, *stat_expr, *errmsg_expr, *opr_expr;
   12292              : 
   12293           88 :   gfc_start_block (&block);
   12294           88 :   gfc_init_block (&post_block);
   12295              : 
   12296           88 :   if (code->resolved_isym->id == GFC_ISYM_CO_REDUCE)
   12297              :     {
   12298           17 :       opr_expr = code->ext.actual->next->expr;
   12299           17 :       image_idx_expr = code->ext.actual->next->next->expr;
   12300           17 :       stat_expr = code->ext.actual->next->next->next->expr;
   12301           17 :       errmsg_expr = code->ext.actual->next->next->next->next->expr;
   12302              :     }
   12303              :   else
   12304              :     {
   12305           71 :       opr_expr = NULL;
   12306           71 :       image_idx_expr = code->ext.actual->next->expr;
   12307           71 :       stat_expr = code->ext.actual->next->next->expr;
   12308           71 :       errmsg_expr = code->ext.actual->next->next->next->expr;
   12309              :     }
   12310              : 
   12311              :   /* stat.  */
   12312           88 :   if (stat_expr)
   12313              :     {
   12314           59 :       gfc_init_se (&argse, NULL);
   12315           59 :       gfc_conv_expr (&argse, stat_expr);
   12316           59 :       gfc_add_block_to_block (&block, &argse.pre);
   12317           59 :       gfc_add_block_to_block (&post_block, &argse.post);
   12318           59 :       stat = argse.expr;
   12319           59 :       if (flag_coarray != GFC_FCOARRAY_SINGLE)
   12320           32 :         stat = gfc_build_addr_expr (NULL_TREE, stat);
   12321              :     }
   12322           29 :   else if (flag_coarray == GFC_FCOARRAY_SINGLE)
   12323              :     stat = NULL_TREE;
   12324              :   else
   12325           20 :     stat = null_pointer_node;
   12326              : 
   12327              :   /* Early exit for GFC_FCOARRAY_SINGLE.  */
   12328           88 :   if (flag_coarray == GFC_FCOARRAY_SINGLE)
   12329              :     {
   12330           36 :       if (stat != NULL_TREE)
   12331              :         {
   12332              :           /* For optional stats, check the pointer is valid before zero'ing.  */
   12333           27 :           if (gfc_expr_attr (stat_expr).optional)
   12334              :             {
   12335           12 :               tree tmp;
   12336           12 :               stmtblock_t ass_block;
   12337           12 :               gfc_start_block (&ass_block);
   12338           12 :               gfc_add_modify (&ass_block, stat,
   12339           12 :                               fold_convert (TREE_TYPE (stat),
   12340              :                                             integer_zero_node));
   12341           12 :               tmp = fold_build2 (NE_EXPR, logical_type_node,
   12342              :                                  gfc_build_addr_expr (NULL_TREE, stat),
   12343              :                                  null_pointer_node);
   12344           12 :               tmp = fold_build3 (COND_EXPR, void_type_node, tmp,
   12345              :                                  gfc_finish_block (&ass_block),
   12346              :                                  build_empty_stmt (input_location));
   12347           12 :               gfc_add_expr_to_block (&block, tmp);
   12348              :             }
   12349              :           else
   12350           15 :             gfc_add_modify (&block, stat,
   12351           15 :                             fold_convert (TREE_TYPE (stat), integer_zero_node));
   12352              :         }
   12353           36 :       return gfc_finish_block (&block);
   12354              :     }
   12355              : 
   12356            5 :   gfc_symbol *derived = code->ext.actual->expr->ts.type == BT_DERIVED
   12357           52 :     ? code->ext.actual->expr->ts.u.derived : NULL;
   12358              : 
   12359              :   /* Handle the array.  */
   12360           52 :   gfc_init_se (&argse, NULL);
   12361           52 :   if (!derived || !derived->attr.alloc_comp
   12362            1 :       || code->resolved_isym->id != GFC_ISYM_CO_BROADCAST)
   12363              :     {
   12364           51 :       if (code->ext.actual->expr->rank == 0)
   12365              :         {
   12366           22 :           symbol_attribute attr;
   12367           22 :           gfc_clear_attr (&attr);
   12368           22 :           gfc_init_se (&argse, NULL);
   12369           22 :           gfc_conv_expr (&argse, code->ext.actual->expr);
   12370           22 :           gfc_add_block_to_block (&block, &argse.pre);
   12371           22 :           gfc_add_block_to_block (&post_block, &argse.post);
   12372           22 :           array = gfc_conv_scalar_to_descriptor (&argse, argse.expr, attr);
   12373           22 :           array = gfc_build_addr_expr (NULL_TREE, array);
   12374              :         }
   12375              :       else
   12376              :         {
   12377           29 :           argse.want_pointer = 1;
   12378           29 :           gfc_conv_expr_descriptor (&argse, code->ext.actual->expr);
   12379           29 :           array = argse.expr;
   12380              :         }
   12381              :     }
   12382              : 
   12383           52 :   gfc_add_block_to_block (&block, &argse.pre);
   12384           52 :   gfc_add_block_to_block (&post_block, &argse.post);
   12385              : 
   12386           52 :   if (code->ext.actual->expr->ts.type == BT_CHARACTER)
   12387           15 :     strlen = argse.string_length;
   12388              :   else
   12389           37 :     strlen = integer_zero_node;
   12390              : 
   12391              :   /* image_index.  */
   12392           52 :   if (image_idx_expr)
   12393              :     {
   12394           35 :       gfc_init_se (&argse, NULL);
   12395           35 :       gfc_conv_expr (&argse, image_idx_expr);
   12396           35 :       gfc_add_block_to_block (&block, &argse.pre);
   12397           35 :       gfc_add_block_to_block (&post_block, &argse.post);
   12398           35 :       image_index = fold_convert (integer_type_node, argse.expr);
   12399              :     }
   12400              :   else
   12401           17 :     image_index = integer_zero_node;
   12402              : 
   12403              :   /* errmsg.  */
   12404           52 :   if (errmsg_expr)
   12405              :     {
   12406           25 :       gfc_init_se (&argse, NULL);
   12407           25 :       gfc_conv_expr (&argse, errmsg_expr);
   12408           25 :       gfc_add_block_to_block (&block, &argse.pre);
   12409           25 :       gfc_add_block_to_block (&post_block, &argse.post);
   12410           25 :       errmsg = argse.expr;
   12411           25 :       errmsg_len = fold_convert (size_type_node, argse.string_length);
   12412              :     }
   12413              :   else
   12414              :     {
   12415           27 :       errmsg = null_pointer_node;
   12416           27 :       errmsg_len = build_zero_cst (size_type_node);
   12417              :     }
   12418              : 
   12419              :   /* Generate the function call.  */
   12420           52 :   switch (code->resolved_isym->id)
   12421              :     {
   12422           20 :     case GFC_ISYM_CO_BROADCAST:
   12423           20 :       fndecl = gfor_fndecl_co_broadcast;
   12424           20 :       break;
   12425            8 :     case GFC_ISYM_CO_MAX:
   12426            8 :       fndecl = gfor_fndecl_co_max;
   12427            8 :       break;
   12428            6 :     case GFC_ISYM_CO_MIN:
   12429            6 :       fndecl = gfor_fndecl_co_min;
   12430            6 :       break;
   12431           12 :     case GFC_ISYM_CO_REDUCE:
   12432           12 :       fndecl = gfor_fndecl_co_reduce;
   12433           12 :       break;
   12434            6 :     case GFC_ISYM_CO_SUM:
   12435            6 :       fndecl = gfor_fndecl_co_sum;
   12436            6 :       break;
   12437            0 :     default:
   12438            0 :       gcc_unreachable ();
   12439              :     }
   12440              : 
   12441           52 :   if (derived && derived->attr.alloc_comp
   12442            1 :       && code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
   12443              :     /* The derived type has the attribute 'alloc_comp'.  */
   12444              :     {
   12445            2 :       tree tmp = gfc_bcast_alloc_comp (derived, code->ext.actual->expr,
   12446            1 :                                        code->ext.actual->expr->rank,
   12447              :                                        image_index, stat, errmsg, errmsg_len);
   12448            1 :       gfc_add_expr_to_block (&block, tmp);
   12449            1 :     }
   12450              :   else
   12451              :     {
   12452           51 :       if (code->resolved_isym->id == GFC_ISYM_CO_SUM
   12453           45 :           || code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
   12454           25 :         fndecl = build_call_expr_loc (input_location, fndecl, 5, array,
   12455              :                                       image_index, stat, errmsg, errmsg_len);
   12456           26 :       else if (code->resolved_isym->id != GFC_ISYM_CO_REDUCE)
   12457           14 :         fndecl = build_call_expr_loc (input_location, fndecl, 6, array,
   12458              :                                       image_index, stat, errmsg,
   12459              :                                       strlen, errmsg_len);
   12460              :       else
   12461              :         {
   12462           12 :           tree opr, opr_flags;
   12463              : 
   12464              :           // FIXME: Handle TS29113's bind(C) strings with descriptor.
   12465           12 :           int opr_flag_int;
   12466           12 :           if (gfc_is_proc_ptr_comp (opr_expr))
   12467              :             {
   12468            0 :               gfc_symbol *sym = gfc_get_proc_ptr_comp (opr_expr)->ts.interface;
   12469            0 :               opr_flag_int = sym->attr.dimension
   12470            0 :                 || (sym->ts.type == BT_CHARACTER
   12471            0 :                     && !sym->attr.is_bind_c)
   12472            0 :                 ? GFC_CAF_BYREF : 0;
   12473            0 :               opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
   12474            0 :                 && !sym->attr.is_bind_c
   12475            0 :                 ? GFC_CAF_HIDDENLEN : 0;
   12476            0 :               opr_flag_int |= sym->formal->sym->attr.value
   12477            0 :                 ? GFC_CAF_ARG_VALUE : 0;
   12478              :             }
   12479              :           else
   12480              :             {
   12481           12 :               opr_flag_int = gfc_return_by_reference (opr_expr->symtree->n.sym)
   12482           12 :                 ? GFC_CAF_BYREF : 0;
   12483           24 :               opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
   12484            0 :                 && !opr_expr->symtree->n.sym->attr.is_bind_c
   12485           12 :                 ? GFC_CAF_HIDDENLEN : 0;
   12486           12 :               opr_flag_int |= opr_expr->symtree->n.sym->formal->sym->attr.value
   12487           12 :                 ? GFC_CAF_ARG_VALUE : 0;
   12488              :             }
   12489           12 :           opr_flags = build_int_cst (integer_type_node, opr_flag_int);
   12490           12 :           gfc_conv_expr (&argse, opr_expr);
   12491           12 :           opr = argse.expr;
   12492           12 :           fndecl = build_call_expr_loc (input_location, fndecl, 8, array, opr,
   12493              :                                         opr_flags, image_index, stat, errmsg,
   12494              :                                         strlen, errmsg_len);
   12495              :         }
   12496              :     }
   12497              : 
   12498           52 :   gfc_add_expr_to_block (&block, fndecl);
   12499           52 :   gfc_add_block_to_block (&block, &post_block);
   12500              : 
   12501           52 :   return gfc_finish_block (&block);
   12502              : }
   12503              : 
   12504              : 
   12505              : static tree
   12506           95 : conv_intrinsic_atomic_op (gfc_code *code)
   12507              : {
   12508           95 :   gfc_se argse;
   12509           95 :   tree tmp, atom, value, old = NULL_TREE, stat = NULL_TREE;
   12510           95 :   stmtblock_t block, post_block;
   12511           95 :   gfc_expr *atom_expr = code->ext.actual->expr;
   12512           95 :   gfc_expr *stat_expr;
   12513           95 :   built_in_function fn;
   12514              : 
   12515           95 :   if (atom_expr->expr_type == EXPR_FUNCTION
   12516            0 :       && atom_expr->value.function.isym
   12517            0 :       && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
   12518            0 :     atom_expr = atom_expr->value.function.actual->expr;
   12519              : 
   12520           95 :   gfc_start_block (&block);
   12521           95 :   gfc_init_block (&post_block);
   12522              : 
   12523           95 :   gfc_init_se (&argse, NULL);
   12524           95 :   argse.want_pointer = 1;
   12525           95 :   gfc_conv_expr (&argse, atom_expr);
   12526           95 :   gfc_add_block_to_block (&block, &argse.pre);
   12527           95 :   gfc_add_block_to_block (&post_block, &argse.post);
   12528           95 :   atom = argse.expr;
   12529              : 
   12530           95 :   gfc_init_se (&argse, NULL);
   12531           95 :   if (flag_coarray == GFC_FCOARRAY_LIB
   12532           56 :       && code->ext.actual->next->expr->ts.kind == atom_expr->ts.kind)
   12533           54 :     argse.want_pointer = 1;
   12534           95 :   gfc_conv_expr (&argse, code->ext.actual->next->expr);
   12535           95 :   gfc_add_block_to_block (&block, &argse.pre);
   12536           95 :   gfc_add_block_to_block (&post_block, &argse.post);
   12537           95 :   value = argse.expr;
   12538              : 
   12539           95 :   switch (code->resolved_isym->id)
   12540              :     {
   12541           58 :     case GFC_ISYM_ATOMIC_ADD:
   12542           58 :     case GFC_ISYM_ATOMIC_AND:
   12543           58 :     case GFC_ISYM_ATOMIC_DEF:
   12544           58 :     case GFC_ISYM_ATOMIC_OR:
   12545           58 :     case GFC_ISYM_ATOMIC_XOR:
   12546           58 :       stat_expr = code->ext.actual->next->next->expr;
   12547           58 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   12548           34 :         old = null_pointer_node;
   12549              :       break;
   12550           37 :     default:
   12551           37 :       gfc_init_se (&argse, NULL);
   12552           37 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   12553           22 :         argse.want_pointer = 1;
   12554           37 :       gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
   12555           37 :       gfc_add_block_to_block (&block, &argse.pre);
   12556           37 :       gfc_add_block_to_block (&post_block, &argse.post);
   12557           37 :       old = argse.expr;
   12558           37 :       stat_expr = code->ext.actual->next->next->next->expr;
   12559              :     }
   12560              : 
   12561              :   /* STAT=  */
   12562           95 :   if (stat_expr != NULL)
   12563              :     {
   12564           82 :       gcc_assert (stat_expr->expr_type == EXPR_VARIABLE);
   12565           82 :       gfc_init_se (&argse, NULL);
   12566           82 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   12567           48 :         argse.want_pointer = 1;
   12568           82 :       gfc_conv_expr_val (&argse, stat_expr);
   12569           82 :       gfc_add_block_to_block (&block, &argse.pre);
   12570           82 :       gfc_add_block_to_block (&post_block, &argse.post);
   12571           82 :       stat = argse.expr;
   12572              :     }
   12573           13 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
   12574            8 :     stat = null_pointer_node;
   12575              : 
   12576           95 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   12577              :     {
   12578           56 :       tree image_index, caf_decl, offset, token;
   12579           56 :       int op;
   12580              : 
   12581           56 :       switch (code->resolved_isym->id)
   12582              :         {
   12583              :         case GFC_ISYM_ATOMIC_ADD:
   12584              :         case GFC_ISYM_ATOMIC_FETCH_ADD:
   12585              :           op = (int) GFC_CAF_ATOMIC_ADD;
   12586              :           break;
   12587           12 :         case GFC_ISYM_ATOMIC_AND:
   12588           12 :         case GFC_ISYM_ATOMIC_FETCH_AND:
   12589           12 :           op = (int) GFC_CAF_ATOMIC_AND;
   12590           12 :           break;
   12591           12 :         case GFC_ISYM_ATOMIC_OR:
   12592           12 :         case GFC_ISYM_ATOMIC_FETCH_OR:
   12593           12 :           op = (int) GFC_CAF_ATOMIC_OR;
   12594           12 :           break;
   12595           12 :         case GFC_ISYM_ATOMIC_XOR:
   12596           12 :         case GFC_ISYM_ATOMIC_FETCH_XOR:
   12597           12 :           op = (int) GFC_CAF_ATOMIC_XOR;
   12598           12 :           break;
   12599           11 :         case GFC_ISYM_ATOMIC_DEF:
   12600           11 :           op = 0;  /* Unused.  */
   12601           11 :           break;
   12602            0 :         default:
   12603            0 :           gcc_unreachable ();
   12604              :         }
   12605              : 
   12606           56 :       caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
   12607           56 :       if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
   12608            0 :         caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
   12609              : 
   12610           56 :       if (gfc_is_coindexed (atom_expr))
   12611           48 :         image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
   12612              :       else
   12613            8 :         image_index = integer_zero_node;
   12614              : 
   12615              :       /* Ensure VALUE names addressable storage: taking the address of a
   12616              :          constant is invalid in C, and scalars need a temporary as well.  */
   12617           56 :       if (!POINTER_TYPE_P (TREE_TYPE (value)))
   12618              :         {
   12619           42 :           tree elem
   12620           42 :             = fold_convert (TREE_TYPE (TREE_TYPE (atom)), value);
   12621           42 :           elem = gfc_trans_force_lval (&block, elem);
   12622           42 :           value = gfc_build_addr_expr (NULL_TREE, elem);
   12623              :         }
   12624           14 :       else if (TREE_CODE (value) == ADDR_EXPR
   12625           14 :                && TREE_CONSTANT (TREE_OPERAND (value, 0)))
   12626              :         {
   12627            0 :           tree elem
   12628            0 :             = fold_convert (TREE_TYPE (TREE_TYPE (atom)),
   12629              :                             build_fold_indirect_ref (value));
   12630            0 :           elem = gfc_trans_force_lval (&block, elem);
   12631            0 :           value = gfc_build_addr_expr (NULL_TREE, elem);
   12632              :         }
   12633              : 
   12634           56 :       gfc_init_se (&argse, NULL);
   12635           56 :       gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
   12636              :                                 atom_expr);
   12637              : 
   12638           56 :       gfc_add_block_to_block (&block, &argse.pre);
   12639           56 :       if (code->resolved_isym->id == GFC_ISYM_ATOMIC_DEF)
   12640           11 :         tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_def, 7,
   12641              :                                    token, offset, image_index, value, stat,
   12642              :                                    build_int_cst (integer_type_node,
   12643           11 :                                                   (int) atom_expr->ts.type),
   12644              :                                    build_int_cst (integer_type_node,
   12645           11 :                                                   (int) atom_expr->ts.kind));
   12646              :       else
   12647           45 :         tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_op, 9,
   12648           45 :                                    build_int_cst (integer_type_node, op),
   12649              :                                    token, offset, image_index, value, old, stat,
   12650              :                                    build_int_cst (integer_type_node,
   12651           45 :                                                   (int) atom_expr->ts.type),
   12652              :                                    build_int_cst (integer_type_node,
   12653           45 :                                                   (int) atom_expr->ts.kind));
   12654              : 
   12655           56 :       gfc_add_expr_to_block (&block, tmp);
   12656           56 :       gfc_add_block_to_block (&block, &argse.post);
   12657           56 :       gfc_add_block_to_block (&block, &post_block);
   12658           56 :       return gfc_finish_block (&block);
   12659              :     }
   12660              : 
   12661              : 
   12662           39 :   switch (code->resolved_isym->id)
   12663              :     {
   12664              :     case GFC_ISYM_ATOMIC_ADD:
   12665              :     case GFC_ISYM_ATOMIC_FETCH_ADD:
   12666              :       fn = BUILT_IN_ATOMIC_FETCH_ADD_N;
   12667              :       break;
   12668            8 :     case GFC_ISYM_ATOMIC_AND:
   12669            8 :     case GFC_ISYM_ATOMIC_FETCH_AND:
   12670            8 :       fn = BUILT_IN_ATOMIC_FETCH_AND_N;
   12671            8 :       break;
   12672            9 :     case GFC_ISYM_ATOMIC_DEF:
   12673            9 :       fn = BUILT_IN_ATOMIC_STORE_N;
   12674            9 :       break;
   12675            8 :     case GFC_ISYM_ATOMIC_OR:
   12676            8 :     case GFC_ISYM_ATOMIC_FETCH_OR:
   12677            8 :       fn = BUILT_IN_ATOMIC_FETCH_OR_N;
   12678            8 :       break;
   12679            8 :     case GFC_ISYM_ATOMIC_XOR:
   12680            8 :     case GFC_ISYM_ATOMIC_FETCH_XOR:
   12681            8 :       fn = BUILT_IN_ATOMIC_FETCH_XOR_N;
   12682            8 :       break;
   12683            0 :     default:
   12684            0 :       gcc_unreachable ();
   12685              :     }
   12686              : 
   12687           39 :   tmp = TREE_TYPE (TREE_TYPE (atom));
   12688           78 :   fn = (built_in_function) ((int) fn
   12689           39 :                             + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
   12690           39 :                             + 1);
   12691           39 :   tree itype = TREE_TYPE (TREE_TYPE (atom));
   12692           39 :   tmp = builtin_decl_explicit (fn);
   12693              : 
   12694           39 :   switch (code->resolved_isym->id)
   12695              :     {
   12696           24 :     case GFC_ISYM_ATOMIC_ADD:
   12697           24 :     case GFC_ISYM_ATOMIC_AND:
   12698           24 :     case GFC_ISYM_ATOMIC_DEF:
   12699           24 :     case GFC_ISYM_ATOMIC_OR:
   12700           24 :     case GFC_ISYM_ATOMIC_XOR:
   12701           24 :       tmp = build_call_expr_loc (input_location, tmp, 3, atom,
   12702              :                                  fold_convert (itype, value),
   12703              :                                  build_int_cst (NULL, MEMMODEL_RELAXED));
   12704           24 :       gfc_add_expr_to_block (&block, tmp);
   12705           24 :       break;
   12706           15 :     default:
   12707           15 :       tmp = build_call_expr_loc (input_location, tmp, 3, atom,
   12708              :                                  fold_convert (itype, value),
   12709              :                                  build_int_cst (NULL, MEMMODEL_RELAXED));
   12710           15 :       gfc_add_modify (&block, old, fold_convert (TREE_TYPE (old), tmp));
   12711           15 :       break;
   12712              :     }
   12713              : 
   12714           39 :   if (stat != NULL_TREE)
   12715           34 :     gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
   12716           39 :   gfc_add_block_to_block (&block, &post_block);
   12717           39 :   return gfc_finish_block (&block);
   12718              : }
   12719              : 
   12720              : 
   12721              : static tree
   12722          176 : conv_intrinsic_atomic_ref (gfc_code *code)
   12723              : {
   12724          176 :   gfc_se argse;
   12725          176 :   tree tmp, atom, value, stat = NULL_TREE;
   12726          176 :   stmtblock_t block, post_block;
   12727          176 :   built_in_function fn;
   12728          176 :   gfc_expr *atom_expr = code->ext.actual->next->expr;
   12729              : 
   12730          176 :   if (atom_expr->expr_type == EXPR_FUNCTION
   12731            0 :       && atom_expr->value.function.isym
   12732            0 :       && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
   12733            0 :     atom_expr = atom_expr->value.function.actual->expr;
   12734              : 
   12735          176 :   gfc_start_block (&block);
   12736          176 :   gfc_init_block (&post_block);
   12737          176 :   gfc_init_se (&argse, NULL);
   12738          176 :   argse.want_pointer = 1;
   12739          176 :   gfc_conv_expr (&argse, atom_expr);
   12740          176 :   gfc_add_block_to_block (&block, &argse.pre);
   12741          176 :   gfc_add_block_to_block (&post_block, &argse.post);
   12742          176 :   atom = argse.expr;
   12743              : 
   12744          176 :   gfc_init_se (&argse, NULL);
   12745          176 :   if (flag_coarray == GFC_FCOARRAY_LIB
   12746          115 :       && code->ext.actual->expr->ts.kind == atom_expr->ts.kind)
   12747          109 :     argse.want_pointer = 1;
   12748          176 :   gfc_conv_expr (&argse, code->ext.actual->expr);
   12749          176 :   gfc_add_block_to_block (&block, &argse.pre);
   12750          176 :   gfc_add_block_to_block (&post_block, &argse.post);
   12751          176 :   value = argse.expr;
   12752              : 
   12753              :   /* STAT=  */
   12754          176 :   if (code->ext.actual->next->next->expr != NULL)
   12755              :     {
   12756          164 :       gcc_assert (code->ext.actual->next->next->expr->expr_type
   12757              :                   == EXPR_VARIABLE);
   12758          164 :       gfc_init_se (&argse, NULL);
   12759          164 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   12760          108 :         argse.want_pointer = 1;
   12761          164 :       gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
   12762          164 :       gfc_add_block_to_block (&block, &argse.pre);
   12763          164 :       gfc_add_block_to_block (&post_block, &argse.post);
   12764          164 :       stat = argse.expr;
   12765              :     }
   12766           12 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
   12767            7 :     stat = null_pointer_node;
   12768              : 
   12769          176 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   12770              :     {
   12771          115 :       tree image_index, caf_decl, offset, token;
   12772          115 :       tree orig_value = NULL_TREE, vardecl = NULL_TREE;
   12773              : 
   12774          115 :       caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
   12775          115 :       if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
   12776            0 :         caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
   12777              : 
   12778          115 :       if (gfc_is_coindexed (atom_expr))
   12779          103 :         image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
   12780              :       else
   12781           12 :         image_index = integer_zero_node;
   12782              : 
   12783          115 :       gfc_init_se (&argse, NULL);
   12784          115 :       gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
   12785              :                                 atom_expr);
   12786          115 :       gfc_add_block_to_block (&block, &argse.pre);
   12787              : 
   12788              :       /* Different type, need type conversion.  */
   12789          115 :       if (!POINTER_TYPE_P (TREE_TYPE (value)))
   12790              :         {
   12791            6 :           vardecl = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
   12792            6 :           orig_value = value;
   12793            6 :           value = gfc_build_addr_expr (NULL_TREE, vardecl);
   12794              :         }
   12795              : 
   12796          115 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_ref, 7,
   12797              :                                  token, offset, image_index, value, stat,
   12798              :                                  build_int_cst (integer_type_node,
   12799          115 :                                                 (int) atom_expr->ts.type),
   12800              :                                  build_int_cst (integer_type_node,
   12801          115 :                                                 (int) atom_expr->ts.kind));
   12802          115 :       gfc_add_expr_to_block (&block, tmp);
   12803          115 :       if (vardecl != NULL_TREE)
   12804            6 :         gfc_add_modify (&block, orig_value,
   12805            6 :                         fold_convert (TREE_TYPE (orig_value), vardecl));
   12806          115 :       gfc_add_block_to_block (&block, &argse.post);
   12807          115 :       gfc_add_block_to_block (&block, &post_block);
   12808          115 :       return gfc_finish_block (&block);
   12809              :     }
   12810              : 
   12811           61 :   tmp = TREE_TYPE (TREE_TYPE (atom));
   12812          122 :   fn = (built_in_function) ((int) BUILT_IN_ATOMIC_LOAD_N
   12813           61 :                             + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
   12814           61 :                             + 1);
   12815           61 :   tmp = builtin_decl_explicit (fn);
   12816           61 :   tmp = build_call_expr_loc (input_location, tmp, 2, atom,
   12817              :                              build_int_cst (integer_type_node,
   12818              :                                             MEMMODEL_RELAXED));
   12819           61 :   gfc_add_modify (&block, value, fold_convert (TREE_TYPE (value), tmp));
   12820              : 
   12821           61 :   if (stat != NULL_TREE)
   12822           56 :     gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
   12823           61 :   gfc_add_block_to_block (&block, &post_block);
   12824           61 :   return gfc_finish_block (&block);
   12825              : }
   12826              : 
   12827              : 
   12828              : static tree
   12829           14 : conv_intrinsic_atomic_cas (gfc_code *code)
   12830              : {
   12831           14 :   gfc_se argse;
   12832           14 :   tree tmp, atom, old, new_val, comp, stat = NULL_TREE;
   12833           14 :   stmtblock_t block, post_block;
   12834           14 :   built_in_function fn;
   12835           14 :   gfc_expr *atom_expr = code->ext.actual->expr;
   12836              : 
   12837           14 :   if (atom_expr->expr_type == EXPR_FUNCTION
   12838            0 :       && atom_expr->value.function.isym
   12839            0 :       && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
   12840            0 :     atom_expr = atom_expr->value.function.actual->expr;
   12841              : 
   12842           14 :   gfc_init_block (&block);
   12843           14 :   gfc_init_block (&post_block);
   12844           14 :   gfc_init_se (&argse, NULL);
   12845           14 :   argse.want_pointer = 1;
   12846           14 :   gfc_conv_expr (&argse, atom_expr);
   12847           14 :   atom = argse.expr;
   12848              : 
   12849           14 :   gfc_init_se (&argse, NULL);
   12850           14 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   12851            8 :     argse.want_pointer = 1;
   12852           14 :   gfc_conv_expr (&argse, code->ext.actual->next->expr);
   12853           14 :   gfc_add_block_to_block (&block, &argse.pre);
   12854           14 :   gfc_add_block_to_block (&post_block, &argse.post);
   12855           14 :   old = argse.expr;
   12856              : 
   12857           14 :   gfc_init_se (&argse, NULL);
   12858           14 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   12859            8 :     argse.want_pointer = 1;
   12860           14 :   gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
   12861           14 :   gfc_add_block_to_block (&block, &argse.pre);
   12862           14 :   gfc_add_block_to_block (&post_block, &argse.post);
   12863           14 :   comp = argse.expr;
   12864              : 
   12865           14 :   gfc_init_se (&argse, NULL);
   12866           14 :   if (flag_coarray == GFC_FCOARRAY_LIB
   12867            8 :       && code->ext.actual->next->next->next->expr->ts.kind
   12868            8 :          == atom_expr->ts.kind)
   12869            8 :     argse.want_pointer = 1;
   12870           14 :   gfc_conv_expr (&argse, code->ext.actual->next->next->next->expr);
   12871           14 :   gfc_add_block_to_block (&block, &argse.pre);
   12872           14 :   gfc_add_block_to_block (&post_block, &argse.post);
   12873           14 :   new_val = argse.expr;
   12874              : 
   12875              :   /* STAT=  */
   12876           14 :   if (code->ext.actual->next->next->next->next->expr != NULL)
   12877              :     {
   12878           14 :       gcc_assert (code->ext.actual->next->next->next->next->expr->expr_type
   12879              :                   == EXPR_VARIABLE);
   12880           14 :       gfc_init_se (&argse, NULL);
   12881           14 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   12882            8 :         argse.want_pointer = 1;
   12883           14 :       gfc_conv_expr_val (&argse,
   12884           14 :                          code->ext.actual->next->next->next->next->expr);
   12885           14 :       gfc_add_block_to_block (&block, &argse.pre);
   12886           14 :       gfc_add_block_to_block (&post_block, &argse.post);
   12887           14 :       stat = argse.expr;
   12888              :     }
   12889            0 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
   12890            0 :     stat = null_pointer_node;
   12891              : 
   12892           14 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   12893              :     {
   12894            8 :       tree image_index, caf_decl, offset, token;
   12895              : 
   12896            8 :       caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
   12897            8 :       if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
   12898            0 :         caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
   12899              : 
   12900            8 :       if (gfc_is_coindexed (atom_expr))
   12901            8 :         image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
   12902              :       else
   12903            0 :         image_index = integer_zero_node;
   12904              : 
   12905            8 :       if (TREE_TYPE (TREE_TYPE (new_val)) != TREE_TYPE (TREE_TYPE (old)))
   12906              :         {
   12907            0 :           tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "new");
   12908            0 :           gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), new_val));
   12909            0 :           new_val = gfc_build_addr_expr (NULL_TREE, tmp);
   12910              :         }
   12911              : 
   12912            8 :       gfc_init_se (&argse, NULL);
   12913            8 :       gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
   12914              :                                 atom_expr);
   12915            8 :       gfc_add_block_to_block (&block, &argse.pre);
   12916              : 
   12917            8 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_cas, 9,
   12918              :                                  token, offset, image_index, old, comp, new_val,
   12919              :                                  stat, build_int_cst (integer_type_node,
   12920            8 :                                                       (int) atom_expr->ts.type),
   12921              :                                  build_int_cst (integer_type_node,
   12922            8 :                                                 (int) atom_expr->ts.kind));
   12923            8 :       gfc_add_expr_to_block (&block, tmp);
   12924            8 :       gfc_add_block_to_block (&block, &argse.post);
   12925            8 :       gfc_add_block_to_block (&block, &post_block);
   12926            8 :       return gfc_finish_block (&block);
   12927              :     }
   12928              : 
   12929            6 :   tmp = TREE_TYPE (TREE_TYPE (atom));
   12930           12 :   fn = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
   12931            6 :                             + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
   12932            6 :                             + 1);
   12933            6 :   tmp = builtin_decl_explicit (fn);
   12934              : 
   12935            6 :   gfc_add_modify (&block, old, comp);
   12936           12 :   tmp = build_call_expr_loc (input_location, tmp, 6, atom,
   12937              :                              gfc_build_addr_expr (NULL, old),
   12938            6 :                              fold_convert (TREE_TYPE (old), new_val),
   12939              :                              boolean_false_node,
   12940              :                              build_int_cst (NULL, MEMMODEL_RELAXED),
   12941              :                              build_int_cst (NULL, MEMMODEL_RELAXED));
   12942            6 :   gfc_add_expr_to_block (&block, tmp);
   12943              : 
   12944            6 :   if (stat != NULL_TREE)
   12945            6 :     gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
   12946            6 :   gfc_add_block_to_block (&block, &post_block);
   12947            6 :   return gfc_finish_block (&block);
   12948              : }
   12949              : 
   12950              : static tree
   12951          105 : conv_intrinsic_event_query (gfc_code *code)
   12952              : {
   12953          105 :   gfc_se se, argse;
   12954          105 :   tree stat = NULL_TREE, stat2 = NULL_TREE;
   12955          105 :   tree count = NULL_TREE, count2 = NULL_TREE;
   12956              : 
   12957          105 :   gfc_expr *event_expr = code->ext.actual->expr;
   12958              : 
   12959          105 :   if (code->ext.actual->next->next->expr)
   12960              :     {
   12961           18 :       gcc_assert (code->ext.actual->next->next->expr->expr_type
   12962              :                   == EXPR_VARIABLE);
   12963           18 :       gfc_init_se (&argse, NULL);
   12964           18 :       gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
   12965           18 :       stat = argse.expr;
   12966              :     }
   12967           87 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
   12968           58 :     stat = null_pointer_node;
   12969              : 
   12970          105 :   if (code->ext.actual->next->expr)
   12971              :     {
   12972          105 :       gcc_assert (code->ext.actual->next->expr->expr_type == EXPR_VARIABLE);
   12973          105 :       gfc_init_se (&argse, NULL);
   12974          105 :       gfc_conv_expr_val (&argse, code->ext.actual->next->expr);
   12975          105 :       count = argse.expr;
   12976              :     }
   12977              : 
   12978          105 :   gfc_start_block (&se.pre);
   12979          105 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   12980              :     {
   12981           70 :       tree tmp, token, image_index;
   12982           70 :       tree index = build_zero_cst (gfc_array_index_type);
   12983              : 
   12984           70 :       if (event_expr->expr_type == EXPR_FUNCTION
   12985            0 :           && event_expr->value.function.isym
   12986            0 :           && event_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
   12987            0 :         event_expr = event_expr->value.function.actual->expr;
   12988              : 
   12989           70 :       tree caf_decl = gfc_get_tree_for_caf_expr (event_expr);
   12990              : 
   12991           70 :       if (event_expr->symtree->n.sym->ts.type != BT_DERIVED
   12992           70 :           || event_expr->symtree->n.sym->ts.u.derived->from_intmod
   12993              :              != INTMOD_ISO_FORTRAN_ENV
   12994           70 :           || event_expr->symtree->n.sym->ts.u.derived->intmod_sym_id
   12995              :              != ISOFORTRAN_EVENT_TYPE)
   12996              :         {
   12997            0 :           gfc_error ("Sorry, the event component of derived type at %L is not "
   12998              :                      "yet supported", &event_expr->where);
   12999            0 :           return NULL_TREE;
   13000              :         }
   13001              : 
   13002           70 :       if (gfc_is_coindexed (event_expr))
   13003              :         {
   13004            0 :           gfc_error ("The event variable at %L shall not be coindexed",
   13005              :                      &event_expr->where);
   13006            0 :           return NULL_TREE;
   13007              :         }
   13008              : 
   13009           70 :       image_index = integer_zero_node;
   13010              : 
   13011           70 :       gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
   13012              :                                 event_expr);
   13013              : 
   13014              :       /* For arrays, obtain the array index.  */
   13015           70 :       if (gfc_expr_attr (event_expr).dimension)
   13016              :         {
   13017           52 :           tree desc, tmp, extent, lbound, ubound;
   13018           52 :           gfc_array_ref *ar, ar2;
   13019           52 :           int i;
   13020              : 
   13021              :           /* TODO: Extend this, once DT components are supported.  */
   13022           52 :           ar = &event_expr->ref->u.ar;
   13023           52 :           ar2 = *ar;
   13024           52 :           memset (ar, '\0', sizeof (*ar));
   13025           52 :           ar->as = ar2.as;
   13026           52 :           ar->type = AR_FULL;
   13027              : 
   13028           52 :           gfc_init_se (&argse, NULL);
   13029           52 :           argse.descriptor_only = 1;
   13030           52 :           gfc_conv_expr_descriptor (&argse, event_expr);
   13031           52 :           gfc_add_block_to_block (&se.pre, &argse.pre);
   13032           52 :           desc = argse.expr;
   13033           52 :           *ar = ar2;
   13034              : 
   13035           52 :           extent = build_one_cst (gfc_array_index_type);
   13036          156 :           for (i = 0; i < ar->dimen; i++)
   13037              :             {
   13038           52 :               gfc_init_se (&argse, NULL);
   13039           52 :               gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
   13040           52 :               gfc_add_block_to_block (&argse.pre, &argse.pre);
   13041           52 :               lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
   13042           52 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
   13043           52 :                                      TREE_TYPE (lbound), argse.expr, lbound);
   13044           52 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
   13045           52 :                                      TREE_TYPE (tmp), extent, tmp);
   13046           52 :               index = fold_build2_loc (input_location, PLUS_EXPR,
   13047           52 :                                        TREE_TYPE (tmp), index, tmp);
   13048           52 :               if (i < ar->dimen - 1)
   13049              :                 {
   13050            0 :                   ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
   13051            0 :                   tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
   13052            0 :                   extent = fold_build2_loc (input_location, MULT_EXPR,
   13053            0 :                                             TREE_TYPE (tmp), extent, tmp);
   13054              :                 }
   13055              :             }
   13056              :         }
   13057              : 
   13058           70 :       if (count != null_pointer_node && TREE_TYPE (count) != integer_type_node)
   13059              :         {
   13060            0 :           count2 = count;
   13061            0 :           count = gfc_create_var (integer_type_node, "count");
   13062              :         }
   13063              : 
   13064           70 :       if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
   13065              :         {
   13066            0 :           stat2 = stat;
   13067            0 :           stat = gfc_create_var (integer_type_node, "stat");
   13068              :         }
   13069              : 
   13070           70 :       index = fold_convert (size_type_node, index);
   13071          140 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_query, 5,
   13072              :                                    token, index, image_index, count
   13073           70 :                                    ? gfc_build_addr_expr (NULL, count) : count,
   13074           70 :                                    stat != null_pointer_node
   13075           12 :                                    ? gfc_build_addr_expr (NULL, stat) : stat);
   13076           70 :       gfc_add_expr_to_block (&se.pre, tmp);
   13077              : 
   13078           70 :       if (count2 != NULL_TREE)
   13079            0 :         gfc_add_modify (&se.pre, count2,
   13080            0 :                         fold_convert (TREE_TYPE (count2), count));
   13081              : 
   13082           70 :       if (stat2 != NULL_TREE)
   13083            0 :         gfc_add_modify (&se.pre, stat2,
   13084            0 :                         fold_convert (TREE_TYPE (stat2), stat));
   13085              : 
   13086           70 :       return gfc_finish_block (&se.pre);
   13087              :     }
   13088              : 
   13089           35 :   gfc_init_se (&argse, NULL);
   13090           35 :   gfc_conv_expr_val (&argse, code->ext.actual->expr);
   13091           35 :   gfc_add_modify (&se.pre, count, fold_convert (TREE_TYPE (count), argse.expr));
   13092              : 
   13093           35 :   if (stat != NULL_TREE)
   13094            6 :     gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
   13095              : 
   13096           35 :   return gfc_finish_block (&se.pre);
   13097              : }
   13098              : 
   13099              : 
   13100              : /* This is a peculiar case because of the need to do dependency checking.
   13101              :    It is called via trans-stmt.cc(gfc_trans_call), where it is picked out as
   13102              :    a special case and this function called instead of
   13103              :    gfc_conv_procedure_call.  */
   13104              : void
   13105          197 : gfc_conv_intrinsic_mvbits (gfc_se *se, gfc_actual_arglist *actual_args,
   13106              :                            gfc_loopinfo *loop)
   13107              : {
   13108          197 :   gfc_actual_arglist *actual;
   13109          197 :   gfc_se argse[5];
   13110          197 :   gfc_expr *arg[5];
   13111          197 :   gfc_ss *lss;
   13112          197 :   int n;
   13113              : 
   13114          197 :   tree from, frompos, len, to, topos;
   13115          197 :   tree lenmask, oldbits, newbits, bitsize;
   13116          197 :   tree type, utype, above, mask1, mask2;
   13117              : 
   13118          197 :   if (loop)
   13119           67 :     lss = loop->ss;
   13120              :   else
   13121          130 :     lss = gfc_ss_terminator;
   13122              : 
   13123              :   actual = actual_args;
   13124         1182 :   for (n = 0; n < 5; n++, actual = actual->next)
   13125              :     {
   13126          985 :       arg[n] = actual->expr;
   13127          985 :       gfc_init_se (&argse[n], NULL);
   13128              : 
   13129          985 :       if (lss != gfc_ss_terminator)
   13130              :         {
   13131          335 :           gfc_copy_loopinfo_to_se (&argse[n], loop);
   13132              :           /* Find the ss for the expression if it is there.  */
   13133          335 :           argse[n].ss = lss;
   13134          335 :           gfc_mark_ss_chain_used (lss, 1);
   13135              :         }
   13136              : 
   13137          985 :       gfc_conv_expr (&argse[n], arg[n]);
   13138              : 
   13139          985 :       if (loop)
   13140          335 :         lss = argse[n].ss;
   13141              :     }
   13142              : 
   13143          197 :   from    = argse[0].expr;
   13144          197 :   frompos = argse[1].expr;
   13145          197 :   len     = argse[2].expr;
   13146          197 :   to      = argse[3].expr;
   13147          197 :   topos   = argse[4].expr;
   13148              : 
   13149              :   /* The type of the result (TO).  */
   13150          197 :   type    = TREE_TYPE (to);
   13151          197 :   bitsize = build_int_cst (integer_type_node, TYPE_PRECISION (type));
   13152              : 
   13153              :   /* Optionally generate code for runtime argument check.  */
   13154          197 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
   13155              :     {
   13156           18 :       tree nbits, below, ccond;
   13157           18 :       tree fp = fold_convert (long_integer_type_node, frompos);
   13158           18 :       tree ln = fold_convert (long_integer_type_node, len);
   13159           18 :       tree tp = fold_convert (long_integer_type_node, topos);
   13160           18 :       below = fold_build2_loc (input_location, LT_EXPR,
   13161              :                                logical_type_node, frompos,
   13162           18 :                                build_int_cst (TREE_TYPE (frompos), 0));
   13163           18 :       above = fold_build2_loc (input_location, GT_EXPR,
   13164              :                                logical_type_node, frompos,
   13165           18 :                                fold_convert (TREE_TYPE (frompos), bitsize));
   13166           18 :       ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
   13167              :                                logical_type_node, below, above);
   13168           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
   13169           18 :                                &arg[1]->where,
   13170              :                                "FROMPOS argument (%ld) out of range 0:%d "
   13171              :                                "in intrinsic MVBITS", fp, bitsize);
   13172           18 :       below = fold_build2_loc (input_location, LT_EXPR,
   13173              :                                logical_type_node, len,
   13174           18 :                                build_int_cst (TREE_TYPE (len), 0));
   13175           18 :       above = fold_build2_loc (input_location, GT_EXPR,
   13176              :                                logical_type_node, len,
   13177           18 :                                fold_convert (TREE_TYPE (len), bitsize));
   13178           18 :       ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
   13179              :                                logical_type_node, below, above);
   13180           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[2].pre,
   13181           18 :                                &arg[2]->where,
   13182              :                                "LEN argument (%ld) out of range 0:%d "
   13183              :                                "in intrinsic MVBITS", ln, bitsize);
   13184           18 :       below = fold_build2_loc (input_location, LT_EXPR,
   13185              :                                logical_type_node, topos,
   13186           18 :                                build_int_cst (TREE_TYPE (topos), 0));
   13187           18 :       above = fold_build2_loc (input_location, GT_EXPR,
   13188              :                                logical_type_node, topos,
   13189           18 :                                fold_convert (TREE_TYPE (topos), bitsize));
   13190           18 :       ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
   13191              :                                logical_type_node, below, above);
   13192           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
   13193           18 :                                &arg[4]->where,
   13194              :                                "TOPOS argument (%ld) out of range 0:%d "
   13195              :                                "in intrinsic MVBITS", tp, bitsize);
   13196              : 
   13197              :       /* The tests above ensure that FROMPOS, LEN and TOPOS fit into short
   13198              :          integers.  Additions below cannot overflow.  */
   13199           18 :       nbits = fold_convert (long_integer_type_node, bitsize);
   13200           18 :       above = fold_build2_loc (input_location, PLUS_EXPR,
   13201              :                                long_integer_type_node, fp, ln);
   13202           18 :       ccond = fold_build2_loc (input_location, GT_EXPR,
   13203              :                                logical_type_node, above, nbits);
   13204           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
   13205              :                                &arg[1]->where,
   13206              :                                "FROMPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
   13207              :                                "in intrinsic MVBITS", fp, ln, bitsize);
   13208           18 :       above = fold_build2_loc (input_location, PLUS_EXPR,
   13209              :                                long_integer_type_node, tp, ln);
   13210           18 :       ccond = fold_build2_loc (input_location, GT_EXPR,
   13211              :                                logical_type_node, above, nbits);
   13212           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
   13213              :                                &arg[4]->where,
   13214              :                                "TOPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
   13215              :                                "in intrinsic MVBITS", tp, ln, bitsize);
   13216              :     }
   13217              : 
   13218         1182 :   for (n = 0; n < 5; n++)
   13219              :     {
   13220          985 :       gfc_add_block_to_block (&se->pre, &argse[n].pre);
   13221          985 :       gfc_add_block_to_block (&se->post, &argse[n].post);
   13222              :     }
   13223              : 
   13224              :   /* lenmask = (LEN >= bit_size (TYPE)) ? ~(TYPE)0 : ((TYPE)1 << LEN) - 1  */
   13225          197 :   above = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   13226          197 :                            len, fold_convert (TREE_TYPE (len), bitsize));
   13227          197 :   mask1 = build_int_cst (type, -1);
   13228          197 :   mask2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
   13229              :                            build_int_cst (type, 1), len);
   13230          197 :   mask2 = fold_build2_loc (input_location, MINUS_EXPR, type,
   13231              :                            mask2, build_int_cst (type, 1));
   13232          197 :   lenmask = fold_build3_loc (input_location, COND_EXPR, type,
   13233              :                              above, mask1, mask2);
   13234              : 
   13235              :   /* newbits = (((UTYPE)(FROM) >> FROMPOS) & lenmask) << TOPOS.
   13236              :    * For valid frompos+len <= bit_size(FROM) the conversion to unsigned is
   13237              :    * not strictly necessary; artificial bits from rshift will be masked.  */
   13238          197 :   utype = unsigned_type_for (type);
   13239          197 :   newbits = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
   13240              :                              fold_convert (utype, from), frompos);
   13241          197 :   newbits = fold_build2_loc (input_location, BIT_AND_EXPR, type,
   13242              :                              fold_convert (type, newbits), lenmask);
   13243          197 :   newbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
   13244              :                              newbits, topos);
   13245              : 
   13246              :   /* oldbits = TO & (~(lenmask << TOPOS)).  */
   13247          197 :   oldbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
   13248              :                              lenmask, topos);
   13249          197 :   oldbits = fold_build1_loc (input_location, BIT_NOT_EXPR, type, oldbits);
   13250          197 :   oldbits = fold_build2_loc (input_location, BIT_AND_EXPR, type, oldbits, to);
   13251              : 
   13252              :   /* TO = newbits | oldbits.  */
   13253          197 :   se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
   13254              :                               oldbits, newbits);
   13255              : 
   13256              :   /* Return the assignment.  */
   13257          197 :   se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
   13258              :                               void_type_node, to, se->expr);
   13259          197 : }
   13260              : 
   13261              : /* Comes from trans-stmt.cc, but we don't want the whole header included.  */
   13262              : extern void gfc_trans_sync_stat (struct sync_stat *sync_stat, gfc_se *se,
   13263              :                                  tree *stat, tree *errmsg, tree *errmsg_len);
   13264              : 
   13265              : static tree
   13266          263 : conv_intrinsic_move_alloc (gfc_code *code)
   13267              : {
   13268          263 :   stmtblock_t block;
   13269          263 :   gfc_expr *from_expr, *to_expr;
   13270          263 :   gfc_se from_se, to_se;
   13271          263 :   tree tmp, to_tree, from_tree, stat, errmsg, errmsg_len, fin_label = NULL_TREE;
   13272          263 :   bool coarray, from_is_class, from_is_scalar;
   13273          263 :   gfc_actual_arglist *arg = code->ext.actual;
   13274          263 :   sync_stat tmp_sync_stat = {nullptr, nullptr};
   13275              : 
   13276          263 :   gfc_start_block (&block);
   13277              : 
   13278          263 :   from_expr = arg->expr;
   13279          263 :   arg = arg->next;
   13280          263 :   to_expr = arg->expr;
   13281          263 :   arg = arg->next;
   13282              : 
   13283          789 :   while (arg)
   13284              :     {
   13285          526 :       if (arg->expr)
   13286              :         {
   13287            0 :           if (!strcmp ("stat", arg->name))
   13288            0 :             tmp_sync_stat.stat = arg->expr;
   13289            0 :           else if (!strcmp ("errmsg", arg->name))
   13290            0 :             tmp_sync_stat.errmsg = arg->expr;
   13291              :         }
   13292          526 :       arg = arg->next;
   13293              :     }
   13294              : 
   13295          263 :   gfc_init_se (&from_se, NULL);
   13296          263 :   gfc_init_se (&to_se, NULL);
   13297              : 
   13298          263 :   gfc_trans_sync_stat (&tmp_sync_stat, &from_se, &stat, &errmsg, &errmsg_len);
   13299          263 :   if (stat != null_pointer_node)
   13300            0 :     fin_label = gfc_build_label_decl (NULL_TREE);
   13301              : 
   13302          263 :   gcc_assert (from_expr->ts.type != BT_CLASS || to_expr->ts.type == BT_CLASS);
   13303          263 :   coarray = from_expr->corank != 0;
   13304              : 
   13305          263 :   from_is_class = from_expr->ts.type == BT_CLASS;
   13306          263 :   from_is_scalar = from_expr->rank == 0 && !coarray;
   13307          263 :   if (to_expr->ts.type == BT_CLASS || from_is_scalar)
   13308              :     {
   13309          163 :       from_se.want_pointer = 1;
   13310          163 :       if (from_is_scalar)
   13311          115 :         gfc_conv_expr (&from_se, from_expr);
   13312              :       else
   13313           48 :         gfc_conv_expr_descriptor (&from_se, from_expr);
   13314          163 :       if (from_is_class)
   13315           64 :         from_tree = gfc_class_data_get (from_se.expr);
   13316              :       else
   13317              :         {
   13318           99 :           gfc_symbol *vtab;
   13319           99 :           from_tree = from_se.expr;
   13320              : 
   13321           99 :           if (to_expr->ts.type == BT_CLASS)
   13322              :             {
   13323           36 :               vtab = gfc_find_vtab (&from_expr->ts);
   13324           36 :               gcc_assert (vtab);
   13325           36 :               from_se.expr = gfc_get_symbol_decl (vtab);
   13326              :             }
   13327              :         }
   13328          163 :       gfc_add_block_to_block (&block, &from_se.pre);
   13329              : 
   13330          163 :       to_se.want_pointer = 1;
   13331          163 :       if (to_expr->rank == 0)
   13332          115 :         gfc_conv_expr (&to_se, to_expr);
   13333              :       else
   13334           48 :         gfc_conv_expr_descriptor (&to_se, to_expr);
   13335          163 :       if (to_expr->ts.type == BT_CLASS)
   13336          100 :         to_tree = gfc_class_data_get (to_se.expr);
   13337              :       else
   13338           63 :         to_tree = to_se.expr;
   13339          163 :       gfc_add_block_to_block (&block, &to_se.pre);
   13340              : 
   13341              :       /* Deallocate "to".  */
   13342          163 :       if (to_expr->rank == 0)
   13343              :         {
   13344          115 :           tmp = gfc_deallocate_scalar_with_status (to_tree, stat, fin_label,
   13345              :                                                    true, to_expr, to_expr->ts,
   13346              :                                                    NULL_TREE, false, true,
   13347              :                                                    errmsg, errmsg_len);
   13348          115 :           gfc_add_expr_to_block (&block, tmp);
   13349              :         }
   13350              : 
   13351          163 :       if (from_is_scalar)
   13352              :         {
   13353              :           /* Assign (_data) pointers.  */
   13354          115 :           gfc_add_modify_loc (input_location, &block, to_tree,
   13355          115 :                               fold_convert (TREE_TYPE (to_tree), from_tree));
   13356              : 
   13357              :           /* Set "from" to NULL.  */
   13358          115 :           gfc_add_modify_loc (input_location, &block, from_tree,
   13359          115 :                               fold_convert (TREE_TYPE (from_tree),
   13360              :                                             null_pointer_node));
   13361              : 
   13362          115 :           gfc_add_block_to_block (&block, &from_se.post);
   13363              :         }
   13364          163 :       gfc_add_block_to_block (&block, &to_se.post);
   13365              : 
   13366              :       /* Set _vptr.  */
   13367          163 :       if (to_expr->ts.type == BT_CLASS)
   13368              :         {
   13369          100 :           gfc_class_set_vptr (&block, to_se.expr, from_se.expr);
   13370          100 :           if (from_is_class)
   13371           64 :             gfc_reset_vptr (&block, from_expr);
   13372          100 :           if (UNLIMITED_POLY (to_expr))
   13373              :             {
   13374           20 :               tree to_len = gfc_class_len_get (to_se.class_container);
   13375           20 :               tmp = from_expr->ts.type == BT_CHARACTER && from_se.string_length
   13376           20 :                       ? from_se.string_length
   13377              :                       : size_zero_node;
   13378           20 :               gfc_add_modify_loc (input_location, &block, to_len,
   13379           20 :                                   fold_convert (TREE_TYPE (to_len), tmp));
   13380              :             }
   13381              :         }
   13382              : 
   13383          163 :       if (from_is_scalar)
   13384              :         {
   13385          115 :           if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
   13386              :             {
   13387            6 :               gfc_add_modify_loc (input_location, &block, to_se.string_length,
   13388            6 :                                   fold_convert (TREE_TYPE (to_se.string_length),
   13389              :                                                 from_se.string_length));
   13390            6 :               if (from_expr->ts.deferred)
   13391            6 :                 gfc_add_modify_loc (
   13392              :                   input_location, &block, from_se.string_length,
   13393            6 :                   build_int_cst (TREE_TYPE (from_se.string_length), 0));
   13394              :             }
   13395          115 :           if (UNLIMITED_POLY (from_expr))
   13396            2 :             gfc_reset_len (&block, from_expr);
   13397              : 
   13398          115 :           return gfc_finish_block (&block);
   13399              :         }
   13400              : 
   13401           48 :       gfc_init_se (&to_se, NULL);
   13402           48 :       gfc_init_se (&from_se, NULL);
   13403              :     }
   13404              : 
   13405              :   /* Deallocate "to".  */
   13406          148 :   if (from_expr->rank == 0)
   13407              :     {
   13408            4 :       to_se.want_coarray = 1;
   13409            4 :       from_se.want_coarray = 1;
   13410              :     }
   13411          148 :   gfc_conv_expr_descriptor (&to_se, to_expr);
   13412          148 :   gfc_conv_expr_descriptor (&from_se, from_expr);
   13413          148 :   gfc_add_block_to_block (&block, &to_se.pre);
   13414          148 :   gfc_add_block_to_block (&block, &from_se.pre);
   13415              : 
   13416              :   /* For coarrays, call SYNC ALL if TO is already deallocated as MOVE_ALLOC
   13417              :      is an image control "statement", cf. IR F08/0040 in 12-006A.  */
   13418          148 :   if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
   13419              :     {
   13420            6 :       tree cond;
   13421              : 
   13422            6 :       tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
   13423              :                                         fin_label, true, to_expr,
   13424              :                                         GFC_CAF_COARRAY_DEALLOCATE_ONLY,
   13425              :                                         NULL_TREE, NULL_TREE,
   13426              :                                         gfc_conv_descriptor_token (to_se.expr),
   13427              :                                         true);
   13428            6 :       gfc_add_expr_to_block (&block, tmp);
   13429              : 
   13430            6 :       tmp = gfc_conv_descriptor_data_get (to_se.expr);
   13431            6 :       cond = fold_build2_loc (input_location, EQ_EXPR,
   13432              :                               logical_type_node, tmp,
   13433            6 :                               fold_convert (TREE_TYPE (tmp),
   13434              :                                             null_pointer_node));
   13435            6 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
   13436              :                                  3, null_pointer_node, null_pointer_node,
   13437              :                                  integer_zero_node);
   13438              : 
   13439            6 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
   13440              :                              tmp, build_empty_stmt (input_location));
   13441            6 :       gfc_add_expr_to_block (&block, tmp);
   13442            6 :     }
   13443              :   else
   13444              :     {
   13445          142 :       if (to_expr->ts.type == BT_DERIVED
   13446           25 :           && to_expr->ts.u.derived->attr.alloc_comp)
   13447              :         {
   13448           19 :           tmp = gfc_deallocate_alloc_comp (to_expr->ts.u.derived,
   13449              :                                            to_se.expr, to_expr->rank);
   13450           19 :           gfc_add_expr_to_block (&block, tmp);
   13451              :         }
   13452              : 
   13453          142 :       tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
   13454              :                                         fin_label, true, to_expr,
   13455              :                                         GFC_CAF_COARRAY_NOCOARRAY, NULL_TREE,
   13456              :                                         NULL_TREE, NULL_TREE, true);
   13457          142 :       gfc_add_expr_to_block (&block, tmp);
   13458              :     }
   13459              : 
   13460              :   /* Copy the array descriptor data.  */
   13461          148 :   gfc_add_modify_loc (input_location, &block, to_se.expr, from_se.expr);
   13462              : 
   13463              :   /* Set "from" to NULL.  */
   13464          148 :   tmp = gfc_conv_descriptor_data_get (from_se.expr);
   13465          148 :   gfc_add_modify_loc (input_location, &block, tmp,
   13466          148 :                       fold_convert (TREE_TYPE (tmp), null_pointer_node));
   13467              : 
   13468          148 :   if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
   13469              :     {
   13470              :       /* Copy the array descriptor data has overwritten the to-token and cleared
   13471              :          from.data.  Now also clear the from.token.  */
   13472            6 :       gfc_add_modify (&block, gfc_conv_descriptor_token (from_se.expr),
   13473              :                       null_pointer_node);
   13474              :     }
   13475              : 
   13476          148 :   if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
   13477              :     {
   13478            7 :       gfc_add_modify_loc (input_location, &block, to_se.string_length,
   13479            7 :                           fold_convert (TREE_TYPE (to_se.string_length),
   13480              :                                         from_se.string_length));
   13481            7 :       if (from_expr->ts.deferred)
   13482            6 :         gfc_add_modify_loc (input_location, &block, from_se.string_length,
   13483            6 :                         build_int_cst (TREE_TYPE (from_se.string_length), 0));
   13484              :     }
   13485          148 :   if (fin_label)
   13486            0 :     gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, fin_label));
   13487              : 
   13488          148 :   gfc_add_block_to_block (&block, &to_se.post);
   13489          148 :   gfc_add_block_to_block (&block, &from_se.post);
   13490              : 
   13491          148 :   return gfc_finish_block (&block);
   13492              : }
   13493              : 
   13494              : 
   13495              : tree
   13496         6718 : gfc_conv_intrinsic_subroutine (gfc_code *code)
   13497              : {
   13498         6718 :   tree res;
   13499              : 
   13500         6718 :   gcc_assert (code->resolved_isym);
   13501              : 
   13502         6718 :   switch (code->resolved_isym->id)
   13503              :     {
   13504          263 :     case GFC_ISYM_MOVE_ALLOC:
   13505          263 :       res = conv_intrinsic_move_alloc (code);
   13506          263 :       break;
   13507              : 
   13508           14 :     case GFC_ISYM_ATOMIC_CAS:
   13509           14 :       res = conv_intrinsic_atomic_cas (code);
   13510           14 :       break;
   13511              : 
   13512           95 :     case GFC_ISYM_ATOMIC_ADD:
   13513           95 :     case GFC_ISYM_ATOMIC_AND:
   13514           95 :     case GFC_ISYM_ATOMIC_DEF:
   13515           95 :     case GFC_ISYM_ATOMIC_OR:
   13516           95 :     case GFC_ISYM_ATOMIC_XOR:
   13517           95 :     case GFC_ISYM_ATOMIC_FETCH_ADD:
   13518           95 :     case GFC_ISYM_ATOMIC_FETCH_AND:
   13519           95 :     case GFC_ISYM_ATOMIC_FETCH_OR:
   13520           95 :     case GFC_ISYM_ATOMIC_FETCH_XOR:
   13521           95 :       res = conv_intrinsic_atomic_op (code);
   13522           95 :       break;
   13523              : 
   13524          176 :     case GFC_ISYM_ATOMIC_REF:
   13525          176 :       res = conv_intrinsic_atomic_ref (code);
   13526          176 :       break;
   13527              : 
   13528          105 :     case GFC_ISYM_EVENT_QUERY:
   13529          105 :       res = conv_intrinsic_event_query (code);
   13530          105 :       break;
   13531              : 
   13532         3171 :     case GFC_ISYM_C_F_POINTER:
   13533         3171 :     case GFC_ISYM_C_F_PROCPOINTER:
   13534         3171 :       res = conv_isocbinding_subroutine (code);
   13535         3171 :       break;
   13536              : 
   13537          360 :     case GFC_ISYM_CAF_SEND:
   13538          360 :       res = conv_caf_send_to_remote (code);
   13539          360 :       break;
   13540              : 
   13541          140 :     case GFC_ISYM_CAF_SENDGET:
   13542          140 :       res = conv_caf_sendget (code);
   13543          140 :       break;
   13544              : 
   13545           88 :     case GFC_ISYM_CO_BROADCAST:
   13546           88 :     case GFC_ISYM_CO_MIN:
   13547           88 :     case GFC_ISYM_CO_MAX:
   13548           88 :     case GFC_ISYM_CO_REDUCE:
   13549           88 :     case GFC_ISYM_CO_SUM:
   13550           88 :       res = conv_co_collective (code);
   13551           88 :       break;
   13552              : 
   13553           10 :     case GFC_ISYM_FREE:
   13554           10 :       res = conv_intrinsic_free (code);
   13555           10 :       break;
   13556              : 
   13557           55 :     case GFC_ISYM_FSTAT:
   13558           55 :     case GFC_ISYM_LSTAT:
   13559           55 :     case GFC_ISYM_STAT:
   13560           55 :       res = conv_intrinsic_fstat_lstat_stat_sub (code);
   13561           55 :       break;
   13562              : 
   13563           90 :     case GFC_ISYM_RANDOM_INIT:
   13564           90 :       res = conv_intrinsic_random_init (code);
   13565           90 :       break;
   13566              : 
   13567           15 :     case GFC_ISYM_KILL:
   13568           15 :       res = conv_intrinsic_kill_sub (code);
   13569           15 :       break;
   13570              : 
   13571              :     case GFC_ISYM_MVBITS:
   13572              :       res = NULL_TREE;
   13573              :       break;
   13574              : 
   13575          194 :     case GFC_ISYM_SYSTEM_CLOCK:
   13576          194 :       res = conv_intrinsic_system_clock (code);
   13577          194 :       break;
   13578              : 
   13579          102 :     case GFC_ISYM_SPLIT:
   13580          102 :       res = conv_intrinsic_split (code);
   13581          102 :       break;
   13582              : 
   13583              :     default:
   13584              :       res = NULL_TREE;
   13585              :       break;
   13586              :     }
   13587              : 
   13588         6718 :   return res;
   13589              : }
   13590              : 
   13591              : #include "gt-fortran-trans-intrinsic.h"
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.