LCOV - code coverage report
Current view: top level - gcc/fortran - trans-intrinsic.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.9 % 7123 6759
Test Date: 2026-11-14 16:39:50 Functions: 98.3 % 172 169
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 "trans-descriptor.h"
      43              : #include "dependency.h"       /* For CAF array alias analysis.  */
      44              : #include "attribs.h"
      45              : #include "realmpfr.h"
      46              : #include "constructor.h"
      47              : 
      48              : /* This maps Fortran intrinsic math functions to external library or GCC
      49              :    builtin functions.  */
      50              : typedef struct GTY(()) gfc_intrinsic_map_t {
      51              :   /* The explicit enum is required to work around inadequacies in the
      52              :      garbage collection/gengtype parsing mechanism.  */
      53              :   enum gfc_isym_id id;
      54              : 
      55              :   /* Enum value from the "language-independent", aka C-centric, part
      56              :      of gcc, or END_BUILTINS of no such value set.  */
      57              :   enum built_in_function float_built_in;
      58              :   enum built_in_function double_built_in;
      59              :   enum built_in_function long_double_built_in;
      60              :   enum built_in_function complex_float_built_in;
      61              :   enum built_in_function complex_double_built_in;
      62              :   enum built_in_function complex_long_double_built_in;
      63              : 
      64              :   /* True if the naming pattern is to prepend "c" for complex and
      65              :      append "f" for kind=4.  False if the naming pattern is to
      66              :      prepend "_gfortran_" and append "[rc](4|8|10|16)".  */
      67              :   bool libm_name;
      68              : 
      69              :   /* True if a complex version of the function exists.  */
      70              :   bool complex_available;
      71              : 
      72              :   /* True if the function should be marked const.  */
      73              :   bool is_constant;
      74              : 
      75              :   /* The base library name of this function.  */
      76              :   const char *name;
      77              : 
      78              :   /* Cache decls created for the various operand types.  */
      79              :   tree real4_decl;
      80              :   tree real8_decl;
      81              :   tree real10_decl;
      82              :   tree real16_decl;
      83              :   tree complex4_decl;
      84              :   tree complex8_decl;
      85              :   tree complex10_decl;
      86              :   tree complex16_decl;
      87              : }
      88              : gfc_intrinsic_map_t;
      89              : 
      90              : /* ??? The NARGS==1 hack here is based on the fact that (c99 at least)
      91              :    defines complex variants of all of the entries in mathbuiltins.def
      92              :    except for atan2.  */
      93              : #define DEFINE_MATH_BUILTIN(ID, NAME, ARGTYPE) \
      94              :   { GFC_ISYM_ ## ID, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
      95              :     BUILT_IN_ ## ID ## L, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
      96              :     true, false, true, NAME, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, \
      97              :     NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
      98              : 
      99              : #define DEFINE_MATH_BUILTIN_C(ID, NAME, ARGTYPE) \
     100              :   { GFC_ISYM_ ## ID, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
     101              :     BUILT_IN_ ## ID ## L, BUILT_IN_C ## ID ## F, BUILT_IN_C ## ID, \
     102              :     BUILT_IN_C ## ID ## L, true, true, true, NAME, NULL_TREE, NULL_TREE, \
     103              :     NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
     104              : 
     105              : #define LIB_FUNCTION(ID, NAME, HAVE_COMPLEX) \
     106              :   { GFC_ISYM_ ## ID, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
     107              :     END_BUILTINS, END_BUILTINS, END_BUILTINS, \
     108              :     false, HAVE_COMPLEX, true, NAME, NULL_TREE, NULL_TREE, NULL_TREE, \
     109              :     NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE }
     110              : 
     111              : #define OTHER_BUILTIN(ID, NAME, TYPE, CONST) \
     112              :   { GFC_ISYM_NONE, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
     113              :     BUILT_IN_ ## ID ## L, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
     114              :     true, false, CONST, NAME, NULL_TREE, NULL_TREE, \
     115              :     NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
     116              : 
     117              : static GTY(()) gfc_intrinsic_map_t gfc_intrinsic_map[] =
     118              : {
     119              :   /* Functions built into gcc itself (DEFINE_MATH_BUILTIN and
     120              :      DEFINE_MATH_BUILTIN_C), then the built-ins that don't correspond
     121              :      to any GFC_ISYM id directly, which use the OTHER_BUILTIN macro.  */
     122              : #include "mathbuiltins.def"
     123              : 
     124              :   /* Functions in libgfortran.  */
     125              :   LIB_FUNCTION (ERFC_SCALED, "erfc_scaled", false),
     126              :   LIB_FUNCTION (SIND, "sind", false),
     127              :   LIB_FUNCTION (COSD, "cosd", false),
     128              :   LIB_FUNCTION (TAND, "tand", false),
     129              : 
     130              :   /* End the list.  */
     131              :   LIB_FUNCTION (NONE, NULL, false)
     132              : 
     133              : };
     134              : #undef OTHER_BUILTIN
     135              : #undef LIB_FUNCTION
     136              : #undef DEFINE_MATH_BUILTIN
     137              : #undef DEFINE_MATH_BUILTIN_C
     138              : 
     139              : 
     140              : enum rounding_mode { RND_ROUND, RND_TRUNC, RND_CEIL, RND_FLOOR };
     141              : 
     142              : 
     143              : /* Find the correct variant of a given builtin from its argument.  */
     144              : static tree
     145        11472 : builtin_decl_for_precision (enum built_in_function base_built_in,
     146              :                             int precision)
     147              : {
     148        11472 :   enum built_in_function i = END_BUILTINS;
     149              : 
     150        11472 :   gfc_intrinsic_map_t *m;
     151       491199 :   for (m = gfc_intrinsic_map; m->double_built_in != base_built_in ; m++)
     152              :     ;
     153              : 
     154        11472 :   if (precision == TYPE_PRECISION (float_type_node))
     155         5832 :     i = m->float_built_in;
     156         5640 :   else if (precision == TYPE_PRECISION (double_type_node))
     157              :     i = m->double_built_in;
     158         1695 :   else if (precision == TYPE_PRECISION (long_double_type_node)
     159         1695 :            && (!gfc_real16_is_float128
     160         1571 :                || long_double_type_node != gfc_float128_type_node))
     161         1571 :     i = m->long_double_built_in;
     162          124 :   else if (precision == TYPE_PRECISION (gfc_float128_type_node))
     163              :     {
     164              :       /* Special treatment, because it is not exactly a built-in, but
     165              :          a library function.  */
     166          124 :       return m->real16_decl;
     167              :     }
     168              : 
     169        11348 :   return (i == END_BUILTINS ? NULL_TREE : builtin_decl_explicit (i));
     170              : }
     171              : 
     172              : 
     173              : tree
     174        10433 : gfc_builtin_decl_for_float_kind (enum built_in_function double_built_in,
     175              :                                  int kind)
     176              : {
     177        10433 :   int i = gfc_validate_kind (BT_REAL, kind, false);
     178              : 
     179        10433 :   if (gfc_real_kinds[i].c_float128)
     180              :     {
     181              :       /* For _Float128, the story is a bit different, because we return
     182              :          a decl to a library function rather than a built-in.  */
     183              :       gfc_intrinsic_map_t *m;
     184        36328 :       for (m = gfc_intrinsic_map; m->double_built_in != double_built_in ; m++)
     185              :         ;
     186              : 
     187          905 :       return m->real16_decl;
     188              :     }
     189              : 
     190         9528 :   return builtin_decl_for_precision (double_built_in,
     191         9528 :                                      gfc_real_kinds[i].mode_precision);
     192              : }
     193              : 
     194              : 
     195              : /* Evaluate the arguments to an intrinsic function.  The value
     196              :    of NARGS may be less than the actual number of arguments in EXPR
     197              :    to allow optional "KIND" arguments that are not included in the
     198              :    generated code to be ignored.  */
     199              : 
     200              : static void
     201        84793 : gfc_conv_intrinsic_function_args (gfc_se *se, gfc_expr *expr,
     202              :                                   tree *argarray, int nargs)
     203              : {
     204        84793 :   gfc_actual_arglist *actual;
     205        84793 :   gfc_expr *e;
     206        84793 :   gfc_intrinsic_arg  *formal;
     207        84793 :   gfc_se argse;
     208        84793 :   int curr_arg;
     209              : 
     210        84793 :   formal = expr->value.function.isym->formal;
     211        84793 :   actual = expr->value.function.actual;
     212              : 
     213       190713 :    for (curr_arg = 0; curr_arg < nargs; curr_arg++,
     214        65416 :         actual = actual->next,
     215       105920 :         formal = formal ? formal->next : NULL)
     216              :     {
     217       105920 :       gcc_assert (actual);
     218       105920 :       e = actual->expr;
     219              :       /* Skip omitted optional arguments.  */
     220       105920 :       if (!e)
     221              :         {
     222           31 :           --curr_arg;
     223           31 :           continue;
     224              :         }
     225              : 
     226              :       /* Evaluate the parameter.  This will substitute scalarized
     227              :          references automatically.  */
     228       105889 :       gfc_init_se (&argse, se);
     229              : 
     230       105889 :       if (e->ts.type == BT_CHARACTER)
     231              :         {
     232         9681 :           gfc_conv_expr (&argse, e);
     233         9681 :           gfc_conv_string_parameter (&argse);
     234         9681 :           argarray[curr_arg++] = argse.string_length;
     235         9681 :           gcc_assert (curr_arg < nargs);
     236              :         }
     237              :       else
     238        96208 :         gfc_conv_expr_val (&argse, e);
     239              : 
     240              :       /* If an optional argument is itself an optional dummy argument,
     241              :          check its presence and substitute a null if absent.  */
     242       105889 :       if (e->expr_type == EXPR_VARIABLE
     243        53875 :             && e->symtree->n.sym->attr.optional
     244          203 :             && formal
     245          153 :             && formal->optional)
     246           80 :         gfc_conv_missing_dummy (&argse, e, formal->ts, 0);
     247              : 
     248       105889 :       gfc_add_block_to_block (&se->pre, &argse.pre);
     249       105889 :       gfc_add_block_to_block (&se->post, &argse.post);
     250       105889 :       argarray[curr_arg] = argse.expr;
     251              :     }
     252        84793 : }
     253              : 
     254              : /* Count the number of actual arguments to the intrinsic function EXPR
     255              :    including any "hidden" string length arguments.  */
     256              : 
     257              : static unsigned int
     258        58496 : gfc_intrinsic_argument_list_length (gfc_expr *expr)
     259              : {
     260        58496 :   int n = 0;
     261        58496 :   gfc_actual_arglist *actual;
     262              : 
     263       132391 :   for (actual = expr->value.function.actual; actual; actual = actual->next)
     264              :     {
     265        73895 :       if (!actual->expr)
     266         6433 :         continue;
     267              : 
     268        67462 :       if (actual->expr->ts.type == BT_CHARACTER)
     269         4553 :         n += 2;
     270              :       else
     271        62909 :         n++;
     272              :     }
     273              : 
     274        58496 :   return n;
     275              : }
     276              : 
     277              : 
     278              : /* Conversions between different types are output by the frontend as
     279              :    intrinsic functions.  We implement these directly with inline code.  */
     280              : 
     281              : static void
     282        42019 : gfc_conv_intrinsic_conversion (gfc_se * se, gfc_expr * expr)
     283              : {
     284        42019 :   tree type;
     285        42019 :   tree *args;
     286        42019 :   int nargs;
     287              : 
     288        42019 :   nargs = gfc_intrinsic_argument_list_length (expr);
     289        42019 :   args = XALLOCAVEC (tree, nargs);
     290              : 
     291              :   /* Evaluate all the arguments passed. Whilst we're only interested in the
     292              :      first one here, there are other parts of the front-end that assume this
     293              :      and will trigger an ICE if it's not the case.  */
     294        42019 :   type = gfc_typenode_for_spec (&expr->ts);
     295        42019 :   gcc_assert (expr->value.function.actual->expr);
     296        42019 :   gfc_conv_intrinsic_function_args (se, expr, args, nargs);
     297              : 
     298              :   /* Conversion between character kinds involves a call to a library
     299              :      function.  */
     300        42019 :   if (expr->ts.type == BT_CHARACTER)
     301              :     {
     302          248 :       tree fndecl, var, addr, tmp;
     303              : 
     304          248 :       if (expr->ts.kind == 1
     305           97 :           && expr->value.function.actual->expr->ts.kind == 4)
     306           97 :         fndecl = gfor_fndecl_convert_char4_to_char1;
     307          151 :       else if (expr->ts.kind == 4
     308          151 :                && expr->value.function.actual->expr->ts.kind == 1)
     309          151 :         fndecl = gfor_fndecl_convert_char1_to_char4;
     310              :       else
     311            0 :         gcc_unreachable ();
     312              : 
     313              :       /* Create the variable storing the converted value.  */
     314          248 :       type = gfc_get_pchar_type (expr->ts.kind);
     315          248 :       var = gfc_create_var (type, "str");
     316          248 :       addr = gfc_build_addr_expr (build_pointer_type (type), var);
     317              : 
     318              :       /* Call the library function that will perform the conversion.  */
     319          248 :       gcc_assert (nargs >= 2);
     320          248 :       tmp = build_call_expr_loc (input_location,
     321              :                              fndecl, 3, addr, args[0], args[1]);
     322          248 :       gfc_add_expr_to_block (&se->pre, tmp);
     323              : 
     324              :       /* Free the temporary afterwards.  */
     325          248 :       tmp = gfc_call_free (var);
     326          248 :       gfc_add_expr_to_block (&se->post, tmp);
     327              : 
     328          248 :       se->expr = var;
     329          248 :       se->string_length = args[0];
     330              : 
     331          248 :       return;
     332              :     }
     333              : 
     334              :   /* Conversion from complex to non-complex involves taking the real
     335              :      component of the value.  */
     336        41771 :   if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
     337        41771 :       && expr->ts.type != BT_COMPLEX)
     338              :     {
     339          583 :       tree artype;
     340              : 
     341          583 :       artype = TREE_TYPE (TREE_TYPE (args[0]));
     342          583 :       args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
     343              :                                  args[0]);
     344              :     }
     345              : 
     346        41771 :   se->expr = convert (type, args[0]);
     347              : }
     348              : 
     349              : /* This is needed because the gcc backend only implements
     350              :    FIX_TRUNC_EXPR, which is the same as INT() in Fortran.
     351              :    FLOOR(x) = INT(x) <= x ? INT(x) : INT(x) - 1
     352              :    Similarly for CEILING.  */
     353              : 
     354              : static tree
     355          132 : build_fixbound_expr (stmtblock_t * pblock, tree arg, tree type, int up)
     356              : {
     357          132 :   tree tmp;
     358          132 :   tree cond;
     359          132 :   tree argtype;
     360          132 :   tree intval;
     361              : 
     362          132 :   argtype = TREE_TYPE (arg);
     363          132 :   arg = gfc_evaluate_now (arg, pblock);
     364              : 
     365          132 :   intval = convert (type, arg);
     366          132 :   intval = gfc_evaluate_now (intval, pblock);
     367              : 
     368          132 :   tmp = convert (argtype, intval);
     369          248 :   cond = fold_build2_loc (input_location, up ? GE_EXPR : LE_EXPR,
     370              :                           logical_type_node, tmp, arg);
     371              : 
     372          248 :   tmp = fold_build2_loc (input_location, up ? PLUS_EXPR : MINUS_EXPR, type,
     373              :                          intval, build_int_cst (type, 1));
     374          132 :   tmp = fold_build3_loc (input_location, COND_EXPR, type, cond, intval, tmp);
     375          132 :   return tmp;
     376              : }
     377              : 
     378              : 
     379              : /* Round to nearest integer, away from zero.  */
     380              : 
     381              : static tree
     382          516 : build_round_expr (tree arg, tree restype)
     383              : {
     384          516 :   tree argtype;
     385          516 :   tree fn;
     386          516 :   int argprec, resprec;
     387              : 
     388          516 :   argtype = TREE_TYPE (arg);
     389          516 :   argprec = TYPE_PRECISION (argtype);
     390          516 :   resprec = TYPE_PRECISION (restype);
     391              : 
     392              :   /* Depending on the type of the result, choose the int intrinsic (iround,
     393              :      available only as a builtin, therefore cannot use it for _Float128), long
     394              :      int intrinsic (lround family) or long long intrinsic (llround).  If we
     395              :      don't have an appropriate function that converts directly to the integer
     396              :      type (such as kind == 16), just use ROUND, and then convert the result to
     397              :      an integer.  We might also need to convert the result afterwards.  */
     398          516 :   if (resprec <= INT_TYPE_SIZE
     399          516 :       && argprec <= TYPE_PRECISION (long_double_type_node))
     400          458 :     fn = builtin_decl_for_precision (BUILT_IN_IROUND, argprec);
     401           62 :   else if (resprec <= LONG_TYPE_SIZE)
     402           46 :     fn = builtin_decl_for_precision (BUILT_IN_LROUND, argprec);
     403           12 :   else if (resprec <= LONG_LONG_TYPE_SIZE)
     404            0 :     fn = builtin_decl_for_precision (BUILT_IN_LLROUND, argprec);
     405           12 :   else if (resprec >= argprec)
     406           12 :     fn = builtin_decl_for_precision (BUILT_IN_ROUND, argprec);
     407              :   else
     408            0 :     gcc_unreachable ();
     409              : 
     410          516 :   return convert (restype, build_call_expr_loc (input_location,
     411          516 :                                                 fn, 1, arg));
     412              : }
     413              : 
     414              : 
     415              : /* Convert a real to an integer using a specific rounding mode.
     416              :    Ideally we would just build the corresponding GENERIC node,
     417              :    however the RTL expander only actually supports FIX_TRUNC_EXPR.  */
     418              : 
     419              : static tree
     420         1801 : build_fix_expr (stmtblock_t * pblock, tree arg, tree type,
     421              :                enum rounding_mode op)
     422              : {
     423         1801 :   switch (op)
     424              :     {
     425          116 :     case RND_FLOOR:
     426          116 :       return build_fixbound_expr (pblock, arg, type, 0);
     427              : 
     428           16 :     case RND_CEIL:
     429           16 :       return build_fixbound_expr (pblock, arg, type, 1);
     430              : 
     431          162 :     case RND_ROUND:
     432          162 :       return build_round_expr (arg, type);
     433              : 
     434         1507 :     case RND_TRUNC:
     435         1507 :       return fold_build1_loc (input_location, FIX_TRUNC_EXPR, type, arg);
     436              : 
     437            0 :     default:
     438            0 :       gcc_unreachable ();
     439              :     }
     440              : }
     441              : 
     442              : 
     443              : /* Copy the PDT components of the result for MERGE and TRAQNSFER.  */
     444              : 
     445              : static void
     446           12 : copy_pdt_components (gfc_se *se, gfc_expr *expr)
     447              : {
     448           12 :   tree tmp, pdt_tmp;
     449           12 :   pdt_tmp = gfc_evaluate_now (se->expr, &se->pre);
     450           12 :   tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
     451              :                              pdt_tmp, se->expr, expr->rank, 0);
     452           12 :   gfc_add_expr_to_block (&se->pre, tmp);
     453           12 : }
     454              : 
     455              : 
     456              : /* Round a real value using the specified rounding mode.
     457              :    We use a temporary integer of that same kind size as the result.
     458              :    Values larger than those that can be represented by this kind are
     459              :    unchanged, as they will not be accurate enough to represent the
     460              :    rounding.
     461              :     huge = HUGE (KIND (a))
     462              :     aint (a) = ((a > huge) || (a < -huge)) ? a : (real)(int)a
     463              :    */
     464              : 
     465              : static void
     466          220 : gfc_conv_intrinsic_aint (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
     467              : {
     468          220 :   tree type;
     469          220 :   tree itype;
     470          220 :   tree arg[2];
     471          220 :   tree tmp;
     472          220 :   tree cond;
     473          220 :   tree decl;
     474          220 :   mpfr_t huge;
     475          220 :   int n, nargs;
     476          220 :   int kind;
     477              : 
     478          220 :   kind = expr->ts.kind;
     479          220 :   nargs = gfc_intrinsic_argument_list_length (expr);
     480              : 
     481          220 :   decl = NULL_TREE;
     482              :   /* We have builtin functions for some cases.  */
     483          220 :   switch (op)
     484              :     {
     485           74 :     case RND_ROUND:
     486           74 :       decl = gfc_builtin_decl_for_float_kind (BUILT_IN_ROUND, kind);
     487           74 :       break;
     488              : 
     489          146 :     case RND_TRUNC:
     490          146 :       decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, kind);
     491          146 :       break;
     492              : 
     493            0 :     default:
     494            0 :       gcc_unreachable ();
     495              :     }
     496              : 
     497              :   /* Evaluate the argument.  */
     498          220 :   gcc_assert (expr->value.function.actual->expr);
     499          220 :   gfc_conv_intrinsic_function_args (se, expr, arg, nargs);
     500              : 
     501              :   /* Use a builtin function if one exists.  */
     502          220 :   if (decl != NULL_TREE)
     503              :     {
     504          220 :       se->expr = build_call_expr_loc (input_location, decl, 1, arg[0]);
     505          220 :       return;
     506              :     }
     507              : 
     508              :   /* This code is probably redundant, but we'll keep it lying around just
     509              :      in case.  */
     510            0 :   type = gfc_typenode_for_spec (&expr->ts);
     511            0 :   arg[0] = gfc_evaluate_now (arg[0], &se->pre);
     512              : 
     513              :   /* Test if the value is too large to handle sensibly.  */
     514            0 :   gfc_set_model_kind (kind);
     515            0 :   mpfr_init (huge);
     516            0 :   n = gfc_validate_kind (BT_INTEGER, kind, false);
     517            0 :   mpfr_set_z (huge, gfc_integer_kinds[n].huge, GFC_RND_MODE);
     518            0 :   tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
     519            0 :   cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, arg[0],
     520              :                           tmp);
     521              : 
     522            0 :   mpfr_neg (huge, huge, GFC_RND_MODE);
     523            0 :   tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
     524            0 :   tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, arg[0],
     525              :                          tmp);
     526            0 :   cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
     527              :                           cond, tmp);
     528            0 :   itype = gfc_get_int_type (kind);
     529              : 
     530            0 :   tmp = build_fix_expr (&se->pre, arg[0], itype, op);
     531            0 :   tmp = convert (type, tmp);
     532            0 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
     533              :                               arg[0]);
     534            0 :   mpfr_clear (huge);
     535              : }
     536              : 
     537              : 
     538              : /* Convert to an integer using the specified rounding mode.  */
     539              : 
     540              : static void
     541         3346 : gfc_conv_intrinsic_int (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
     542              : {
     543         3346 :   tree type;
     544         3346 :   tree *args;
     545         3346 :   int nargs;
     546              : 
     547         3346 :   nargs = gfc_intrinsic_argument_list_length (expr);
     548         3346 :   args = XALLOCAVEC (tree, nargs);
     549              : 
     550              :   /* Evaluate the argument, we process all arguments even though we only
     551              :      use the first one for code generation purposes.  */
     552         3346 :   type = gfc_typenode_for_spec (&expr->ts);
     553         3346 :   gcc_assert (expr->value.function.actual->expr);
     554         3346 :   gfc_conv_intrinsic_function_args (se, expr, args, nargs);
     555              : 
     556         3346 :   if (TREE_CODE (TREE_TYPE (args[0])) == INTEGER_TYPE)
     557              :     {
     558              :       /* Conversion to a different integer kind.  */
     559         1545 :       se->expr = convert (type, args[0]);
     560              :     }
     561              :   else
     562              :     {
     563              :       /* Conversion from complex to non-complex involves taking the real
     564              :          component of the value.  */
     565         1801 :       if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
     566         1801 :           && expr->ts.type != BT_COMPLEX)
     567              :         {
     568          192 :           tree artype;
     569              : 
     570          192 :           artype = TREE_TYPE (TREE_TYPE (args[0]));
     571          192 :           args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
     572              :                                      args[0]);
     573              :         }
     574              : 
     575         1801 :       se->expr = build_fix_expr (&se->pre, args[0], type, op);
     576              :     }
     577         3346 : }
     578              : 
     579              : 
     580              : /* Get the imaginary component of a value.  */
     581              : 
     582              : static void
     583          440 : gfc_conv_intrinsic_imagpart (gfc_se * se, gfc_expr * expr)
     584              : {
     585          440 :   tree arg;
     586              : 
     587          440 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
     588          440 :   se->expr = fold_build1_loc (input_location, IMAGPART_EXPR,
     589          440 :                               TREE_TYPE (TREE_TYPE (arg)), arg);
     590          440 : }
     591              : 
     592              : 
     593              : /* Get the complex conjugate of a value.  */
     594              : 
     595              : static void
     596          257 : gfc_conv_intrinsic_conjg (gfc_se * se, gfc_expr * expr)
     597              : {
     598          257 :   tree arg;
     599              : 
     600          257 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
     601          257 :   se->expr = fold_build1_loc (input_location, CONJ_EXPR, TREE_TYPE (arg), arg);
     602          257 : }
     603              : 
     604              : 
     605              : 
     606              : static tree
     607       686133 : define_quad_builtin (const char *name, tree type, bool is_const)
     608              : {
     609       686133 :   tree fndecl;
     610       686133 :   fndecl = build_decl (input_location, FUNCTION_DECL, get_identifier (name),
     611              :                        type);
     612              : 
     613              :   /* Mark the decl as external.  */
     614       686133 :   DECL_EXTERNAL (fndecl) = 1;
     615       686133 :   TREE_PUBLIC (fndecl) = 1;
     616              : 
     617              :   /* Mark it __attribute__((const)).  */
     618       686133 :   TREE_READONLY (fndecl) = is_const;
     619              : 
     620       686133 :   rest_of_decl_compilation (fndecl, 1, 0);
     621              : 
     622       686133 :   return fndecl;
     623              : }
     624              : 
     625              : /* Add SIMD attribute for FNDECL built-in if the built-in
     626              :    name is in VECTORIZED_BUILTINS.  */
     627              : 
     628              : static void
     629     47281420 : add_simd_flag_for_built_in (tree fndecl)
     630              : {
     631     47281420 :   if (gfc_vectorized_builtins == NULL
     632     18855220 :       || fndecl == NULL_TREE)
     633     39132130 :     return;
     634              : 
     635      8149290 :   const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
     636      8149290 :   int *clauses = gfc_vectorized_builtins->get (name);
     637      8149290 :   if (clauses)
     638              :     {
     639      5112668 :       for (unsigned i = 0; i < 3; i++)
     640      3834501 :         if (*clauses & (1 << i))
     641              :           {
     642      1278172 :             gfc_simd_clause simd_type = (gfc_simd_clause)*clauses;
     643      1278172 :             tree omp_clause = NULL_TREE;
     644      1278172 :             if (simd_type == SIMD_NONE)
     645              :               ; /* No SIMD clause.  */
     646              :             else
     647              :               {
     648      1278172 :                 omp_clause_code code
     649              :                   = (simd_type == SIMD_INBRANCH
     650      1278172 :                      ? OMP_CLAUSE_INBRANCH : OMP_CLAUSE_NOTINBRANCH);
     651      1278172 :                 omp_clause = build_omp_clause (UNKNOWN_LOCATION, code);
     652      1278172 :                 omp_clause = build_tree_list (NULL_TREE, omp_clause);
     653              :               }
     654              : 
     655      1278172 :             DECL_ATTRIBUTES (fndecl)
     656      2556344 :               = tree_cons (get_identifier ("omp declare simd"), omp_clause,
     657      1278172 :                            DECL_ATTRIBUTES (fndecl));
     658              :           }
     659              :     }
     660              : }
     661              : 
     662              :   /* Set SIMD attribute to all built-in functions that are mentioned
     663              :      in gfc_vectorized_builtins vector.  */
     664              : 
     665              : void
     666        80138 : gfc_adjust_builtins (void)
     667              : {
     668        80138 :   gfc_intrinsic_map_t *m;
     669      4808280 :   for (m = gfc_intrinsic_map;
     670      4808280 :        m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
     671              :     {
     672      4728142 :       add_simd_flag_for_built_in (m->real4_decl);
     673      4728142 :       add_simd_flag_for_built_in (m->complex4_decl);
     674      4728142 :       add_simd_flag_for_built_in (m->real8_decl);
     675      4728142 :       add_simd_flag_for_built_in (m->complex8_decl);
     676      4728142 :       add_simd_flag_for_built_in (m->real10_decl);
     677      4728142 :       add_simd_flag_for_built_in (m->complex10_decl);
     678      4728142 :       add_simd_flag_for_built_in (m->real16_decl);
     679      4728142 :       add_simd_flag_for_built_in (m->complex16_decl);
     680      4728142 :       add_simd_flag_for_built_in (m->real16_decl);
     681      4728142 :       add_simd_flag_for_built_in (m->complex16_decl);
     682              :     }
     683              : 
     684              :   /* Release all strings.  */
     685        80138 :   if (gfc_vectorized_builtins != NULL)
     686              :     {
     687      1725523 :       for (hash_map<nofree_string_hash, int>::iterator it
     688        31958 :            = gfc_vectorized_builtins->begin ();
     689      1757481 :            it != gfc_vectorized_builtins->end (); ++it)
     690      1725523 :         free (const_cast<char *> ((*it).first));
     691              : 
     692        63916 :       delete gfc_vectorized_builtins;
     693        31958 :       gfc_vectorized_builtins = NULL;
     694              :     }
     695        80138 : }
     696              : 
     697              : /* Initialize function decls for library functions.  The external functions
     698              :    are created as required.  Builtin functions are added here.  */
     699              : 
     700              : void
     701        32673 : gfc_build_intrinsic_lib_fndecls (void)
     702              : {
     703        32673 :   gfc_intrinsic_map_t *m;
     704        32673 :   tree quad_decls[END_BUILTINS + 1];
     705              : 
     706        32673 :   if (gfc_real16_is_float128)
     707              :   {
     708              :     /* If we have soft-float types, we create the decls for their
     709              :        C99-like library functions.  For now, we only handle _Float128
     710              :        q-suffixed or IEC 60559 f128-suffixed functions.  */
     711              : 
     712        32673 :     tree type, complex_type, func_1, func_2, func_3, func_cabs, func_frexp;
     713        32673 :     tree func_iround, func_lround, func_llround, func_scalbn, func_cpow;
     714              : 
     715        32673 :     memset (quad_decls, 0, sizeof(tree) * (END_BUILTINS + 1));
     716              : 
     717        32673 :     type = gfc_float128_type_node;
     718        32673 :     complex_type = gfc_complex_float128_type_node;
     719              :     /* type (*) (type) */
     720        32673 :     func_1 = build_function_type_list (type, type, NULL_TREE);
     721              :     /* int (*) (type) */
     722        32673 :     func_iround = build_function_type_list (integer_type_node,
     723              :                                             type, NULL_TREE);
     724              :     /* long (*) (type) */
     725        32673 :     func_lround = build_function_type_list (long_integer_type_node,
     726              :                                             type, NULL_TREE);
     727              :     /* long long (*) (type) */
     728        32673 :     func_llround = build_function_type_list (long_long_integer_type_node,
     729              :                                              type, NULL_TREE);
     730              :     /* type (*) (type, type) */
     731        32673 :     func_2 = build_function_type_list (type, type, type, NULL_TREE);
     732              :     /* type (*) (type, type, type) */
     733        32673 :     func_3 = build_function_type_list (type, type, type, type, NULL_TREE);
     734              :     /* type (*) (type, &int) */
     735        32673 :     func_frexp
     736        32673 :       = build_function_type_list (type,
     737              :                                   type,
     738              :                                   build_pointer_type (integer_type_node),
     739              :                                   NULL_TREE);
     740              :     /* type (*) (type, int) */
     741        32673 :     func_scalbn = build_function_type_list (type,
     742              :                                             type, integer_type_node, NULL_TREE);
     743              :     /* type (*) (complex type) */
     744        32673 :     func_cabs = build_function_type_list (type, complex_type, NULL_TREE);
     745              :     /* complex type (*) (complex type, complex type) */
     746        32673 :     func_cpow
     747        32673 :       = build_function_type_list (complex_type,
     748              :                                   complex_type, complex_type, NULL_TREE);
     749              : 
     750              : #define DEFINE_MATH_BUILTIN(ID, NAME, ARGTYPE)
     751              : #define DEFINE_MATH_BUILTIN_C(ID, NAME, ARGTYPE)
     752              : #define LIB_FUNCTION(ID, NAME, HAVE_COMPLEX)
     753              : 
     754              :     /* Only these built-ins are actually needed here. These are used directly
     755              :        from the code, when calling builtin_decl_for_precision() or
     756              :        builtin_decl_for_float_type(). The others are all constructed by
     757              :        gfc_get_intrinsic_lib_fndecl().  */
     758              : #define OTHER_BUILTIN(ID, NAME, TYPE, CONST) \
     759              :     quad_decls[BUILT_IN_ ## ID]                                         \
     760              :       = define_quad_builtin (gfc_real16_use_iec_60559                   \
     761              :                              ? NAME "f128" : NAME "q", func_ ## TYPE,       \
     762              :                              CONST);
     763              : 
     764              : #include "mathbuiltins.def"
     765              : 
     766              : #undef OTHER_BUILTIN
     767              : #undef LIB_FUNCTION
     768              : #undef DEFINE_MATH_BUILTIN
     769              : #undef DEFINE_MATH_BUILTIN_C
     770              : 
     771              :     /* There is one built-in we defined manually, because it gets called
     772              :        with builtin_decl_for_precision() or builtin_decl_for_float_type()
     773              :        even though it is not an OTHER_BUILTIN: it is SQRT.  */
     774        32673 :     quad_decls[BUILT_IN_SQRT]
     775        32673 :       = define_quad_builtin (gfc_real16_use_iec_60559
     776              :                              ? "sqrtf128" : "sqrtq", func_1, true);
     777              :   }
     778              : 
     779              :   /* Add GCC builtin functions.  */
     780      1960380 :   for (m = gfc_intrinsic_map;
     781      1960380 :        m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
     782              :     {
     783      1927707 :       if (m->float_built_in != END_BUILTINS)
     784      1797015 :         m->real4_decl = builtin_decl_explicit (m->float_built_in);
     785      1927707 :       if (m->complex_float_built_in != END_BUILTINS)
     786       522768 :         m->complex4_decl = builtin_decl_explicit (m->complex_float_built_in);
     787      1927707 :       if (m->double_built_in != END_BUILTINS)
     788      1797015 :         m->real8_decl = builtin_decl_explicit (m->double_built_in);
     789      1927707 :       if (m->complex_double_built_in != END_BUILTINS)
     790       522768 :         m->complex8_decl = builtin_decl_explicit (m->complex_double_built_in);
     791              : 
     792              :       /* If real(kind=10) exists, it is always long double.  */
     793      1927707 :       if (m->long_double_built_in != END_BUILTINS)
     794      1797015 :         m->real10_decl = builtin_decl_explicit (m->long_double_built_in);
     795      1927707 :       if (m->complex_long_double_built_in != END_BUILTINS)
     796       522768 :         m->complex10_decl
     797       522768 :           = builtin_decl_explicit (m->complex_long_double_built_in);
     798              : 
     799      1927707 :       if (!gfc_real16_is_float128)
     800              :         {
     801            0 :           if (m->long_double_built_in != END_BUILTINS)
     802            0 :             m->real16_decl = builtin_decl_explicit (m->long_double_built_in);
     803            0 :           if (m->complex_long_double_built_in != END_BUILTINS)
     804            0 :             m->complex16_decl
     805            0 :               = builtin_decl_explicit (m->complex_long_double_built_in);
     806              :         }
     807      1927707 :       else if (quad_decls[m->double_built_in] != NULL_TREE)
     808              :         {
     809              :           /* Quad-precision function calls are constructed when first
     810              :              needed by builtin_decl_for_precision(), except for those
     811              :              that will be used directly (define by OTHER_BUILTIN).  */
     812       686133 :           m->real16_decl = quad_decls[m->double_built_in];
     813              :         }
     814      1241574 :       else if (quad_decls[m->complex_double_built_in] != NULL_TREE)
     815              :         {
     816              :           /* Same thing for the complex ones.  */
     817            0 :           m->complex16_decl = quad_decls[m->double_built_in];
     818              :         }
     819              :     }
     820        32673 : }
     821              : 
     822              : 
     823              : /* Create a fndecl for a simple intrinsic library function.  */
     824              : 
     825              : static tree
     826         4546 : gfc_get_intrinsic_lib_fndecl (gfc_intrinsic_map_t * m, gfc_expr * expr)
     827              : {
     828         4546 :   tree type;
     829         4546 :   vec<tree, va_gc> *argtypes;
     830         4546 :   tree fndecl;
     831         4546 :   gfc_actual_arglist *actual;
     832         4546 :   tree *pdecl;
     833         4546 :   gfc_typespec *ts;
     834         4546 :   char name[GFC_MAX_SYMBOL_LEN + 3];
     835              : 
     836         4546 :   ts = &expr->ts;
     837         4546 :   if (ts->type == BT_REAL)
     838              :     {
     839         3684 :       switch (ts->kind)
     840              :         {
     841         1308 :         case 4:
     842         1308 :           pdecl = &m->real4_decl;
     843         1308 :           break;
     844         1307 :         case 8:
     845         1307 :           pdecl = &m->real8_decl;
     846         1307 :           break;
     847          599 :         case 10:
     848          599 :           pdecl = &m->real10_decl;
     849          599 :           break;
     850          470 :         case 16:
     851          470 :           pdecl = &m->real16_decl;
     852          470 :           break;
     853            0 :         default:
     854            0 :           gcc_unreachable ();
     855              :         }
     856              :     }
     857          862 :   else if (ts->type == BT_COMPLEX)
     858              :     {
     859          862 :       gcc_assert (m->complex_available);
     860              : 
     861          862 :       switch (ts->kind)
     862              :         {
     863          386 :         case 4:
     864          386 :           pdecl = &m->complex4_decl;
     865          386 :           break;
     866          405 :         case 8:
     867          405 :           pdecl = &m->complex8_decl;
     868          405 :           break;
     869           51 :         case 10:
     870           51 :           pdecl = &m->complex10_decl;
     871           51 :           break;
     872           20 :         case 16:
     873           20 :           pdecl = &m->complex16_decl;
     874           20 :           break;
     875            0 :         default:
     876            0 :           gcc_unreachable ();
     877              :         }
     878              :     }
     879              :   else
     880            0 :     gcc_unreachable ();
     881              : 
     882         4546 :   if (*pdecl)
     883              :     return *pdecl;
     884              : 
     885          409 :   if (m->libm_name)
     886              :     {
     887          178 :       int n = gfc_validate_kind (BT_REAL, ts->kind, false);
     888          178 :       if (gfc_real_kinds[n].c_float)
     889            0 :         snprintf (name, sizeof (name), "%s%s%s",
     890            0 :                   ts->type == BT_COMPLEX ? "c" : "", m->name, "f");
     891          178 :       else if (gfc_real_kinds[n].c_double)
     892            0 :         snprintf (name, sizeof (name), "%s%s",
     893            0 :                   ts->type == BT_COMPLEX ? "c" : "", m->name);
     894          178 :       else if (gfc_real_kinds[n].c_long_double)
     895            0 :         snprintf (name, sizeof (name), "%s%s%s",
     896            0 :                   ts->type == BT_COMPLEX ? "c" : "", m->name, "l");
     897          178 :       else if (gfc_real_kinds[n].c_float128)
     898          178 :         snprintf (name, sizeof (name), "%s%s%s",
     899          178 :                   ts->type == BT_COMPLEX ? "c" : "", m->name,
     900          178 :                   gfc_real_kinds[n].use_iec_60559 ? "f128" : "q");
     901              :       else
     902            0 :         gcc_unreachable ();
     903              :     }
     904              :   else
     905              :     {
     906          462 :       snprintf (name, sizeof (name), PREFIX ("%s_%c%d"), m->name,
     907          231 :                 ts->type == BT_COMPLEX ? 'c' : 'r',
     908              :                 gfc_type_abi_kind (ts));
     909              :     }
     910              : 
     911          409 :   argtypes = NULL;
     912          838 :   for (actual = expr->value.function.actual; actual; actual = actual->next)
     913              :     {
     914          429 :       type = gfc_typenode_for_spec (&actual->expr->ts);
     915          429 :       vec_safe_push (argtypes, type);
     916              :     }
     917         1227 :   type = build_function_type_vec (gfc_typenode_for_spec (ts), argtypes);
     918          409 :   fndecl = build_decl (input_location,
     919              :                        FUNCTION_DECL, get_identifier (name), type);
     920              : 
     921              :   /* Mark the decl as external.  */
     922          409 :   DECL_EXTERNAL (fndecl) = 1;
     923          409 :   TREE_PUBLIC (fndecl) = 1;
     924              : 
     925              :   /* Mark it __attribute__((const)), if possible.  */
     926          409 :   TREE_READONLY (fndecl) = m->is_constant;
     927              : 
     928          409 :   rest_of_decl_compilation (fndecl, 1, 0);
     929              : 
     930          409 :   (*pdecl) = fndecl;
     931          409 :   return fndecl;
     932              : }
     933              : 
     934              : 
     935              : /* Convert an intrinsic function into an external or builtin call.  */
     936              : 
     937              : static void
     938         3928 : gfc_conv_intrinsic_lib_function (gfc_se * se, gfc_expr * expr)
     939              : {
     940         3928 :   gfc_intrinsic_map_t *m;
     941         3928 :   tree fndecl;
     942         3928 :   tree rettype;
     943         3928 :   tree *args;
     944         3928 :   unsigned int num_args;
     945         3928 :   gfc_isym_id id;
     946              : 
     947         3928 :   id = expr->value.function.isym->id;
     948              :   /* Find the entry for this function.  */
     949        82787 :   for (m = gfc_intrinsic_map;
     950        82787 :        m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
     951              :     {
     952        82787 :       if (id == m->id)
     953              :         break;
     954              :     }
     955              : 
     956         3928 :   if (m->id == GFC_ISYM_NONE)
     957              :     {
     958            0 :       gfc_internal_error ("Intrinsic function %qs (%d) not recognized",
     959              :                           expr->value.function.name, id);
     960              :     }
     961              : 
     962              :   /* Get the decl and generate the call.  */
     963         3928 :   num_args = gfc_intrinsic_argument_list_length (expr);
     964         3928 :   args = XALLOCAVEC (tree, num_args);
     965              : 
     966         3928 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
     967         3928 :   fndecl = gfc_get_intrinsic_lib_fndecl (m, expr);
     968         3928 :   rettype = TREE_TYPE (TREE_TYPE (fndecl));
     969              : 
     970         3928 :   fndecl = build_addr (fndecl);
     971         3928 :   se->expr = build_call_array_loc (input_location, rettype, fndecl, num_args, args);
     972         3928 : }
     973              : 
     974              : 
     975              : /* If bounds-checking is enabled, create code to verify at runtime that the
     976              :    string lengths for both expressions are the same (needed for e.g. MERGE).
     977              :    If bounds-checking is not enabled, does nothing.  */
     978              : 
     979              : void
     980         1580 : gfc_trans_same_strlen_check (const char* intr_name, locus* where,
     981              :                              tree a, tree b, stmtblock_t* target)
     982              : {
     983         1580 :   tree cond;
     984         1580 :   tree name;
     985              : 
     986              :   /* If bounds-checking is disabled, do nothing.  */
     987         1580 :   if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
     988              :     return;
     989              : 
     990              :   /* Compare the two string lengths.  */
     991           94 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, a, b);
     992              : 
     993              :   /* Output the runtime-check.  */
     994           94 :   name = gfc_build_cstring_const (intr_name);
     995           94 :   name = gfc_build_addr_expr (pchar_type_node, name);
     996           94 :   gfc_trans_runtime_check (true, false, cond, target, where,
     997              :                            "Unequal character lengths (%ld/%ld) in %s",
     998              :                            fold_convert (long_integer_type_node, a),
     999              :                            fold_convert (long_integer_type_node, b), name);
    1000              : }
    1001              : 
    1002              : 
    1003              : /* The EXPONENT(X) intrinsic function is translated into
    1004              :        int ret;
    1005              :        return isfinite(X) ? (frexp (X, &ret) , ret) : huge
    1006              :    so that if X is a NaN or infinity, the result is HUGE(0).
    1007              :  */
    1008              : 
    1009              : static void
    1010          228 : gfc_conv_intrinsic_exponent (gfc_se *se, gfc_expr *expr)
    1011              : {
    1012          228 :   tree arg, type, res, tmp, frexp, cond, huge;
    1013          228 :   int i;
    1014              : 
    1015          456 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP,
    1016          228 :                                        expr->value.function.actual->expr->ts.kind);
    1017              : 
    1018          228 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    1019          228 :   arg = gfc_evaluate_now (arg, &se->pre);
    1020              : 
    1021          228 :   i = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
    1022          228 :   huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[i].huge, gfc_c_int_kind);
    1023          228 :   cond = build_call_expr_loc (input_location,
    1024              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    1025              :                               1, arg);
    1026              : 
    1027          228 :   res = gfc_create_var (integer_type_node, NULL);
    1028          228 :   tmp = build_call_expr_loc (input_location, frexp, 2, arg,
    1029              :                              gfc_build_addr_expr (NULL_TREE, res));
    1030          228 :   tmp = fold_build2_loc (input_location, COMPOUND_EXPR, integer_type_node,
    1031              :                          tmp, res);
    1032          228 :   se->expr = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
    1033              :                               cond, tmp, huge);
    1034              : 
    1035          228 :   type = gfc_typenode_for_spec (&expr->ts);
    1036          228 :   se->expr = fold_convert (type, se->expr);
    1037          228 : }
    1038              : 
    1039              : 
    1040              : static int caf_call_cnt = 0;
    1041              : 
    1042              : static tree
    1043         1544 : conv_caf_func_index (stmtblock_t *block, gfc_namespace *ns, const char *pat,
    1044              :                      gfc_expr *hash)
    1045              : {
    1046         1544 :   char *name;
    1047         1544 :   gfc_se argse;
    1048         1544 :   gfc_expr func_index;
    1049         1544 :   gfc_symtree *index_st;
    1050         1544 :   tree func_index_tree;
    1051         1544 :   stmtblock_t blk;
    1052              : 
    1053              :   /* Need to get namespace where static variables are possible.  */
    1054         1544 :   while (ns && ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
    1055            0 :     ns = ns->parent;
    1056         1544 :   gcc_assert (ns);
    1057              : 
    1058         1544 :   name = xasprintf (pat, caf_call_cnt);
    1059         1544 :   gcc_assert (!gfc_get_sym_tree (name, ns, &index_st, false));
    1060         1544 :   free (name);
    1061              : 
    1062         1544 :   index_st->n.sym->attr.flavor = FL_VARIABLE;
    1063         1544 :   index_st->n.sym->attr.save = SAVE_EXPLICIT;
    1064         1544 :   index_st->n.sym->value
    1065         1544 :     = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
    1066              :                              &gfc_current_locus);
    1067         1544 :   mpz_set_si (index_st->n.sym->value->value.integer, -1);
    1068         1544 :   index_st->n.sym->ts.type = BT_INTEGER;
    1069         1544 :   index_st->n.sym->ts.kind = gfc_default_integer_kind;
    1070         1544 :   gfc_set_sym_referenced (index_st->n.sym);
    1071         1544 :   memset (&func_index, 0, sizeof (gfc_expr));
    1072         1544 :   gfc_clear_ts (&func_index.ts);
    1073         1544 :   func_index.expr_type = EXPR_VARIABLE;
    1074         1544 :   func_index.symtree = index_st;
    1075         1544 :   func_index.ts = index_st->n.sym->ts;
    1076         1544 :   gfc_commit_symbol (index_st->n.sym);
    1077              : 
    1078         1544 :   gfc_init_se (&argse, NULL);
    1079         1544 :   gfc_conv_expr (&argse, &func_index);
    1080         1544 :   gfc_add_block_to_block (block, &argse.pre);
    1081         1544 :   func_index_tree = argse.expr;
    1082              : 
    1083         1544 :   gfc_init_se (&argse, NULL);
    1084         1544 :   gfc_conv_expr (&argse, hash);
    1085              : 
    1086         1544 :   gfc_init_block (&blk);
    1087         1544 :   gfc_add_modify (&blk, func_index_tree,
    1088              :                   build_call_expr (gfor_fndecl_caf_get_remote_function_index, 1,
    1089              :                                    argse.expr));
    1090         1544 :   gfc_add_expr_to_block (
    1091              :     block,
    1092              :     build3 (COND_EXPR, void_type_node,
    1093              :             gfc_likely (build2 (EQ_EXPR, logical_type_node, func_index_tree,
    1094              :                                 build_int_cst (integer_type_node, -1)),
    1095              :                         PRED_FIRST_MATCH),
    1096              :             gfc_finish_block (&blk), NULL_TREE));
    1097              : 
    1098         1544 :   return func_index_tree;
    1099              : }
    1100              : 
    1101              : static tree
    1102         1544 : conv_caf_add_call_data (stmtblock_t *blk, gfc_namespace *ns, const char *pat,
    1103              :                         gfc_symbol *data_sym, tree *data_size)
    1104              : {
    1105         1544 :   char *name;
    1106         1544 :   gfc_symtree *data_st;
    1107         1544 :   gfc_constructor *con;
    1108         1544 :   gfc_expr data, data_init;
    1109         1544 :   gfc_se argse;
    1110         1544 :   tree data_tree;
    1111              : 
    1112         1544 :   memset (&data, 0, sizeof (gfc_expr));
    1113         1544 :   gfc_clear_ts (&data.ts);
    1114         1544 :   data.expr_type = EXPR_VARIABLE;
    1115         1544 :   name = xasprintf (pat, caf_call_cnt);
    1116         1544 :   gcc_assert (!gfc_get_sym_tree (name, ns, &data_st, false));
    1117         1544 :   free (name);
    1118         1544 :   data_st->n.sym->attr.flavor = FL_VARIABLE;
    1119         1544 :   data_st->n.sym->ts = data_sym->ts;
    1120         1544 :   data.symtree = data_st;
    1121         1544 :   gfc_set_sym_referenced (data.symtree->n.sym);
    1122         1544 :   data.ts = data_st->n.sym->ts;
    1123         1544 :   gfc_commit_symbol (data_st->n.sym);
    1124              : 
    1125         1544 :   memset (&data_init, 0, sizeof (gfc_expr));
    1126         1544 :   gfc_clear_ts (&data_init.ts);
    1127         1544 :   data_init.expr_type = EXPR_STRUCTURE;
    1128         1544 :   data_init.ts = data.ts;
    1129         1860 :   for (gfc_component *comp = data.ts.u.derived->components; comp;
    1130          316 :        comp = comp->next)
    1131              :     {
    1132          316 :       con = gfc_constructor_get ();
    1133          316 :       con->expr = comp->initializer;
    1134          316 :       comp->initializer = NULL;
    1135          316 :       gfc_constructor_append (&data_init.value.constructor, con);
    1136              :     }
    1137              : 
    1138         1544 :   if (data.ts.u.derived->components)
    1139              :     {
    1140          110 :       gfc_init_se (&argse, NULL);
    1141          110 :       gfc_conv_expr (&argse, &data);
    1142          110 :       data_tree = argse.expr;
    1143          110 :       gfc_add_expr_to_block (blk,
    1144              :                              gfc_trans_structure_assign (data_tree, &data_init,
    1145              :                                                          true, true));
    1146          110 :       gfc_constructor_free (data_init.value.constructor);
    1147          110 :       *data_size = TREE_TYPE (data_tree)->type_common.size_unit;
    1148          110 :       data_tree = gfc_build_addr_expr (pvoid_type_node, data_tree);
    1149              :     }
    1150              :   else
    1151              :     {
    1152         1434 :       data_tree = build_zero_cst (pvoid_type_node);
    1153         1434 :       *data_size = build_zero_cst (size_type_node);
    1154              :     }
    1155              : 
    1156         1544 :   return data_tree;
    1157              : }
    1158              : 
    1159              : static tree
    1160          251 : conv_shape_to_cst (gfc_expr *e)
    1161              : {
    1162          251 :   tree tmp = NULL;
    1163          690 :   for (int d = 0; d < e->rank; ++d)
    1164              :     {
    1165          439 :       if (!tmp)
    1166          251 :         tmp = gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind);
    1167              :       else
    1168          188 :         tmp = fold_build2 (MULT_EXPR, TREE_TYPE (tmp), tmp,
    1169              :                            gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind));
    1170              :     }
    1171          251 :   return fold_convert (size_type_node, tmp);
    1172              : }
    1173              : 
    1174              : static void
    1175         1279 : conv_stat_and_team (stmtblock_t *block, gfc_expr *expr, tree *stat, tree *team,
    1176              :                     tree *team_no)
    1177              : {
    1178         1279 :   gfc_expr *stat_e, *team_e;
    1179              : 
    1180         1279 :   stat_e = gfc_find_stat_co (expr);
    1181         1279 :   if (stat_e)
    1182              :     {
    1183           33 :       gfc_se stat_se;
    1184           33 :       gfc_init_se (&stat_se, NULL);
    1185           33 :       gfc_conv_expr_reference (&stat_se, stat_e);
    1186           33 :       *stat = stat_se.expr;
    1187           33 :       gfc_add_block_to_block (block, &stat_se.pre);
    1188           33 :       gfc_add_block_to_block (block, &stat_se.post);
    1189              :     }
    1190              :   else
    1191         1246 :     *stat = null_pointer_node;
    1192              : 
    1193         1279 :   team_e = gfc_find_team_co (expr, TEAM_TEAM);
    1194         1279 :   if (team_e)
    1195              :     {
    1196           18 :       gfc_se team_se;
    1197           18 :       gfc_init_se (&team_se, NULL);
    1198           18 :       gfc_conv_expr (&team_se, team_e);
    1199           18 :       *team
    1200           18 :         = gfc_build_addr_expr (NULL_TREE, gfc_trans_force_lval (&team_se.pre,
    1201              :                                                                 team_se.expr));
    1202           18 :       gfc_add_block_to_block (block, &team_se.pre);
    1203           18 :       gfc_add_block_to_block (block, &team_se.post);
    1204              :     }
    1205              :   else
    1206         1261 :     *team = null_pointer_node;
    1207              : 
    1208         1279 :   team_e = gfc_find_team_co (expr, TEAM_NUMBER);
    1209         1279 :   if (team_e)
    1210              :     {
    1211           30 :       gfc_se team_se;
    1212           30 :       gfc_init_se (&team_se, NULL);
    1213           30 :       gfc_conv_expr (&team_se, team_e);
    1214           30 :       *team_no = gfc_build_addr_expr (
    1215              :         NULL_TREE,
    1216              :         gfc_trans_force_lval (&team_se.pre,
    1217              :                               fold_convert (integer_type_node, team_se.expr)));
    1218           30 :       gfc_add_block_to_block (block, &team_se.pre);
    1219           30 :       gfc_add_block_to_block (block, &team_se.post);
    1220              :     }
    1221              :   else
    1222         1249 :     *team_no = null_pointer_node;
    1223         1279 : }
    1224              : 
    1225              : /* Get data from a remote coarray.  */
    1226              : 
    1227              : static void
    1228         1018 : gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs,
    1229              :                             bool may_realloc, symbol_attribute *caf_attr)
    1230              : {
    1231         1018 :   gfc_expr *array_expr;
    1232         1018 :   tree caf_decl, token, image_index, tmp, res_var, type, stat, dest_size,
    1233              :     dest_data, opt_dest_desc, get_fn_index_tree, add_data_tree, add_data_size,
    1234              :     opt_src_desc, opt_src_charlen, opt_dest_charlen, team, team_no;
    1235         1018 :   symbol_attribute caf_attr_store;
    1236         1018 :   gfc_namespace *ns;
    1237         1018 :   gfc_expr *get_fn_hash = expr->value.function.actual->next->expr,
    1238         1018 :            *get_fn_expr = expr->value.function.actual->next->next->expr;
    1239         1018 :   gfc_symbol *add_data_sym = get_fn_expr->symtree->n.sym->formal->sym;
    1240              : 
    1241         1018 :   gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
    1242              : 
    1243         1018 :   if (se->ss && se->ss->info->useflags)
    1244              :     {
    1245              :       /* Access the previously obtained result.  */
    1246          379 :       gfc_conv_tmp_array_ref (se);
    1247          379 :       return;
    1248              :     }
    1249              : 
    1250          639 :   array_expr = expr->value.function.actual->expr;
    1251          639 :   ns = array_expr->expr_type == EXPR_VARIABLE
    1252          639 :            && !array_expr->symtree->n.sym->attr.associate_var
    1253          583 :            && !array_expr->symtree->n.sym->module
    1254          639 :          ? array_expr->symtree->n.sym->ns
    1255              :          : gfc_current_ns;
    1256          639 :   type = gfc_typenode_for_spec (&array_expr->ts);
    1257              : 
    1258          639 :   if (caf_attr == NULL)
    1259              :     {
    1260          639 :       caf_attr_store = gfc_caf_attr (array_expr);
    1261          639 :       caf_attr = &caf_attr_store;
    1262              :     }
    1263              : 
    1264          639 :   res_var = lhs;
    1265              : 
    1266          639 :   conv_stat_and_team (&se->pre, expr, &stat, &team, &team_no);
    1267              : 
    1268          639 :   get_fn_index_tree
    1269          639 :     = conv_caf_func_index (&se->pre, ns, "__caf_get_from_remote_fn_index_%d",
    1270              :                            get_fn_hash);
    1271          639 :   add_data_tree
    1272          639 :     = conv_caf_add_call_data (&se->pre, ns, "__caf_get_from_remote_add_data_%d",
    1273              :                               add_data_sym, &add_data_size);
    1274          639 :   ++caf_call_cnt;
    1275              : 
    1276          639 :   if (array_expr->rank == 0)
    1277              :     {
    1278          258 :       res_var = gfc_create_var (type, "caf_res");
    1279          258 :       if (array_expr->ts.type == BT_CHARACTER)
    1280              :         {
    1281           33 :           gfc_conv_string_length (array_expr->ts.u.cl, array_expr, &se->pre);
    1282           33 :           se->string_length = array_expr->ts.u.cl->backend_decl;
    1283           33 :           opt_src_charlen = gfc_build_addr_expr (
    1284              :             NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
    1285           33 :           dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
    1286              :         }
    1287              :       else
    1288              :         {
    1289          225 :           dest_size = res_var->typed.type->type_common.size_unit;
    1290          225 :           opt_src_charlen
    1291          225 :             = build_zero_cst (build_pointer_type (size_type_node));
    1292              :         }
    1293          258 :       dest_data
    1294          258 :         = gfc_evaluate_now (gfc_build_addr_expr (NULL_TREE, res_var), &se->pre);
    1295          258 :       res_var = build_fold_indirect_ref (dest_data);
    1296          258 :       dest_data = gfc_build_addr_expr (pvoid_type_node, dest_data);
    1297          258 :       opt_dest_desc = build_zero_cst (pvoid_type_node);
    1298              :     }
    1299              :   else
    1300              :     {
    1301              :       /* Create temporary.  */
    1302          381 :       may_realloc = gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
    1303              :                                                  type, NULL_TREE, false, false,
    1304              :                                                  false, &array_expr->where)
    1305              :                     == NULL_TREE;
    1306          381 :       res_var = se->ss->info->data.array.descriptor;
    1307          381 :       if (array_expr->ts.type == BT_CHARACTER)
    1308              :         {
    1309           16 :           se->string_length = array_expr->ts.u.cl->backend_decl;
    1310           16 :           opt_src_charlen = gfc_build_addr_expr (
    1311              :             NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
    1312           16 :           dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
    1313              :         }
    1314              :       else
    1315              :         {
    1316          365 :           opt_src_charlen
    1317          365 :             = build_zero_cst (build_pointer_type (size_type_node));
    1318          365 :           dest_size = fold_build2 (
    1319              :             MULT_EXPR, size_type_node,
    1320              :             fold_convert (size_type_node,
    1321              :                           array_expr->shape
    1322              :                             ? conv_shape_to_cst (array_expr)
    1323              :                             : gfc_conv_descriptor_size (res_var,
    1324              :                                                         array_expr->rank)),
    1325              :             fold_convert (size_type_node,
    1326              :                           gfc_conv_descriptor_span_get (res_var)));
    1327              :         }
    1328          381 :       opt_dest_desc = res_var;
    1329          381 :       dest_data = gfc_conv_descriptor_data_get (res_var);
    1330          381 :       opt_dest_desc = gfc_build_addr_expr (NULL_TREE, opt_dest_desc);
    1331          381 :       if (may_realloc)
    1332              :         {
    1333           62 :           tmp = gfc_conv_descriptor_data_get (res_var);
    1334           62 :           tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
    1335              :                                             NULL_TREE, NULL_TREE, true, NULL,
    1336              :                                             GFC_CAF_COARRAY_NOCOARRAY);
    1337           62 :           gfc_add_expr_to_block (&se->post, tmp);
    1338              :         }
    1339          381 :       dest_data
    1340          381 :         = gfc_build_addr_expr (NULL_TREE,
    1341              :                                gfc_trans_force_lval (&se->pre, dest_data));
    1342              :     }
    1343              : 
    1344          639 :   opt_dest_charlen = opt_src_charlen;
    1345          639 :   caf_decl = gfc_get_tree_for_caf_expr (array_expr);
    1346          639 :   if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
    1347            2 :     caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
    1348              : 
    1349          639 :   if (!TYPE_LANG_SPECIFIC (TREE_TYPE (caf_decl))->rank
    1350          639 :       || GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl)))
    1351          554 :     opt_src_desc = build_zero_cst (pvoid_type_node);
    1352              :   else
    1353           85 :     opt_src_desc = gfc_build_addr_expr (pvoid_type_node, caf_decl);
    1354              : 
    1355          639 :   image_index = gfc_caf_get_image_index (&se->pre, array_expr, caf_decl);
    1356          639 :   gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, array_expr);
    1357              : 
    1358              :   /* It guarantees memory consistency within the same segment.  */
    1359          639 :   tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
    1360          639 :   tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1361              :                     gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    1362              :                     tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    1363          639 :   ASM_VOLATILE_P (tmp) = 1;
    1364          639 :   gfc_add_expr_to_block (&se->pre, tmp);
    1365              : 
    1366          639 :   tmp = build_call_expr_loc (
    1367              :     input_location, gfor_fndecl_caf_get_from_remote, 15, token, opt_src_desc,
    1368              :     opt_src_charlen, image_index, dest_size, dest_data, opt_dest_charlen,
    1369              :     opt_dest_desc, constant_boolean_node (may_realloc, boolean_type_node),
    1370              :     get_fn_index_tree, add_data_tree, add_data_size, stat, team, team_no);
    1371              : 
    1372          639 :   gfc_add_expr_to_block (&se->pre, tmp);
    1373              : 
    1374          639 :   if (se->ss)
    1375          381 :     gfc_advance_se_ss_chain (se);
    1376              : 
    1377          639 :   se->expr = res_var;
    1378              : 
    1379          639 :   return;
    1380              : }
    1381              : 
    1382              : /* Generate call to caf_is_present_on_remote for allocated (coarrary[...])
    1383              :    calls.  */
    1384              : 
    1385              : static void
    1386          265 : gfc_conv_intrinsic_caf_is_present_remote (gfc_se *se, gfc_expr *e)
    1387              : {
    1388          265 :   gfc_expr *caf_expr, *hash, *present_fn;
    1389          265 :   gfc_symbol *add_data_sym;
    1390          265 :   tree fn_index, add_data_tree, add_data_size, caf_decl, image_index, token;
    1391              : 
    1392          265 :   gcc_assert (e->expr_type == EXPR_FUNCTION
    1393              :               && e->value.function.isym->id
    1394              :                    == GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE);
    1395          265 :   caf_expr = e->value.function.actual->expr;
    1396          265 :   hash = e->value.function.actual->next->expr;
    1397          265 :   present_fn = e->value.function.actual->next->next->expr;
    1398          265 :   add_data_sym = present_fn->symtree->n.sym->formal->sym;
    1399              : 
    1400          265 :   fn_index = conv_caf_func_index (&se->pre, e->symtree->n.sym->ns,
    1401              :                                   "__caf_present_on_remote_fn_index_%d", hash);
    1402          265 :   add_data_tree = conv_caf_add_call_data (&se->pre, e->symtree->n.sym->ns,
    1403              :                                           "__caf_present_on_remote_add_data_%d",
    1404              :                                           add_data_sym, &add_data_size);
    1405          265 :   ++caf_call_cnt;
    1406              : 
    1407          265 :   caf_decl = gfc_get_tree_for_caf_expr (caf_expr);
    1408          265 :   if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
    1409            6 :     caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
    1410              : 
    1411          265 :   image_index = gfc_caf_get_image_index (&se->pre, caf_expr, caf_decl);
    1412          265 :   gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, caf_expr);
    1413              : 
    1414          265 :   se->expr
    1415          265 :     = fold_convert (logical_type_node,
    1416              :                     build_call_expr_loc (input_location,
    1417              :                                          gfor_fndecl_caf_is_present_on_remote,
    1418              :                                          5, token, image_index, fn_index,
    1419              :                                          add_data_tree, add_data_size));
    1420          265 : }
    1421              : 
    1422              : static tree
    1423          360 : conv_caf_send_to_remote (gfc_code *code)
    1424              : {
    1425          360 :   gfc_expr *lhs_expr, *rhs_expr, *lhs_hash, *receiver_fn_expr;
    1426          360 :   gfc_symbol *add_data_sym;
    1427          360 :   gfc_se lhs_se, rhs_se;
    1428          360 :   stmtblock_t block;
    1429          360 :   gfc_namespace *ns;
    1430          360 :   tree caf_decl, token, rhs_size, image_index, tmp, rhs_data;
    1431          360 :   tree lhs_stat, lhs_team, lhs_team_no, opt_lhs_charlen, opt_rhs_charlen;
    1432          360 :   tree opt_lhs_desc = NULL_TREE, opt_rhs_desc = NULL_TREE;
    1433          360 :   tree receiver_fn_index_tree, add_data_tree, add_data_size;
    1434              : 
    1435          360 :   gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
    1436          360 :   gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SEND);
    1437              : 
    1438          360 :   lhs_expr = code->ext.actual->expr;
    1439          360 :   rhs_expr = code->ext.actual->next->expr;
    1440          360 :   lhs_hash = code->ext.actual->next->next->expr;
    1441          360 :   receiver_fn_expr = code->ext.actual->next->next->next->expr;
    1442          360 :   add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
    1443              : 
    1444          360 :   ns = lhs_expr->expr_type == EXPR_VARIABLE
    1445          360 :            && !lhs_expr->symtree->n.sym->attr.associate_var
    1446          360 :          ? lhs_expr->symtree->n.sym->ns
    1447              :          : gfc_current_ns;
    1448              : 
    1449          360 :   gfc_init_block (&block);
    1450              : 
    1451              :   /* LHS.  */
    1452          360 :   gfc_init_se (&lhs_se, NULL);
    1453          360 :   caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
    1454          360 :   if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
    1455            0 :     caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
    1456          360 :   if (lhs_expr->rank == 0)
    1457              :     {
    1458          266 :       if (lhs_expr->ts.type == BT_CHARACTER)
    1459              :         {
    1460           24 :           gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
    1461           24 :           lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
    1462           24 :           opt_lhs_charlen = gfc_build_addr_expr (
    1463              :             NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
    1464              :         }
    1465              :       else
    1466          242 :         opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
    1467          266 :       opt_lhs_desc = null_pointer_node;
    1468              :     }
    1469              :   else
    1470              :     {
    1471           94 :       gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
    1472           94 :       gfc_add_block_to_block (&block, &lhs_se.pre);
    1473           94 :       opt_lhs_desc = lhs_se.expr;
    1474           94 :       if (lhs_expr->ts.type == BT_CHARACTER)
    1475           44 :         opt_lhs_charlen = gfc_build_addr_expr (
    1476              :           NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
    1477              :       else
    1478           50 :         opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
    1479              :       /* Get the third formal argument of the receiver function.  (This is the
    1480              :          location where to put the data on the remote image.)  Need to look at
    1481              :          the argument in the function decl, because in the gfc_symbol's formal
    1482              :          argument an array may have no descriptor while in the generated
    1483              :          function decl it has.  */
    1484           94 :       tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
    1485              :         TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
    1486           94 :       if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    1487           56 :         opt_lhs_desc = null_pointer_node;
    1488              :       else
    1489           38 :         opt_lhs_desc
    1490           38 :           = gfc_build_addr_expr (NULL_TREE,
    1491              :                                  gfc_trans_force_lval (&block, opt_lhs_desc));
    1492              :     }
    1493              : 
    1494              :   /* Obtain token, offset and image index for the LHS.  */
    1495          360 :   image_index = gfc_caf_get_image_index (&block, lhs_expr, caf_decl);
    1496          360 :   gfc_get_caf_token_offset (&lhs_se, &token, NULL, caf_decl, NULL, lhs_expr);
    1497              : 
    1498              :   /* RHS.  */
    1499          360 :   gfc_init_se (&rhs_se, NULL);
    1500          360 :   if (rhs_expr->rank == 0)
    1501              :     {
    1502          436 :       rhs_se.want_pointer = rhs_expr->ts.type == BT_CHARACTER
    1503          218 :                             && rhs_expr->expr_type != EXPR_CONSTANT;
    1504          218 :       gfc_conv_expr (&rhs_se, rhs_expr);
    1505          218 :       gfc_add_block_to_block (&block, &rhs_se.pre);
    1506          218 :       opt_rhs_desc = null_pointer_node;
    1507          218 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1508              :         {
    1509           40 :           rhs_data
    1510           40 :             = rhs_expr->expr_type == EXPR_CONSTANT
    1511           40 :                 ? gfc_build_addr_expr (NULL_TREE,
    1512              :                                        gfc_trans_force_lval (&block,
    1513              :                                                              rhs_se.expr))
    1514              :                 : rhs_se.expr;
    1515           40 :           opt_rhs_charlen = gfc_build_addr_expr (
    1516              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1517           40 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1518              :         }
    1519              :       else
    1520              :         {
    1521          178 :           rhs_data
    1522          178 :             = gfc_build_addr_expr (NULL_TREE,
    1523              :                                    gfc_trans_force_lval (&block, rhs_se.expr));
    1524          178 :           opt_rhs_charlen
    1525          178 :             = build_zero_cst (build_pointer_type (size_type_node));
    1526          178 :           rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
    1527              :         }
    1528              :     }
    1529              :   else
    1530              :     {
    1531          284 :       rhs_se.force_tmp = rhs_expr->shape == NULL
    1532          142 :                          || !gfc_is_simply_contiguous (rhs_expr, false, false);
    1533          142 :       gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
    1534          142 :       gfc_add_block_to_block (&block, &rhs_se.pre);
    1535          142 :       opt_rhs_desc = rhs_se.expr;
    1536          142 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1537              :         {
    1538           28 :           opt_rhs_charlen = gfc_build_addr_expr (
    1539              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1540           28 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1541              :         }
    1542              :       else
    1543              :         {
    1544          114 :           opt_rhs_charlen
    1545          114 :             = build_zero_cst (build_pointer_type (size_type_node));
    1546          114 :           rhs_size = fold_build2 (
    1547              :             MULT_EXPR, size_type_node,
    1548              :             fold_convert (size_type_node,
    1549              :                           rhs_expr->shape
    1550              :                             ? conv_shape_to_cst (rhs_expr)
    1551              :                             : gfc_conv_descriptor_size (rhs_se.expr,
    1552              :                                                         rhs_expr->rank)),
    1553              :             fold_convert (size_type_node,
    1554              :                           gfc_conv_descriptor_span_get (rhs_se.expr)));
    1555              :         }
    1556              : 
    1557          142 :       rhs_data = gfc_build_addr_expr (
    1558              :         NULL_TREE, gfc_trans_force_lval (&block, gfc_conv_descriptor_data_get (
    1559              :                                                    opt_rhs_desc)));
    1560          142 :       opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
    1561              :     }
    1562          360 :   gfc_add_block_to_block (&block, &rhs_se.pre);
    1563              : 
    1564          360 :   conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
    1565              : 
    1566          360 :   receiver_fn_index_tree
    1567          360 :     = conv_caf_func_index (&block, ns, "__caf_send_to_remote_fn_index_%d",
    1568              :                            lhs_hash);
    1569          360 :   add_data_tree
    1570          360 :     = conv_caf_add_call_data (&block, ns, "__caf_send_to_remote_add_data_%d",
    1571              :                               add_data_sym, &add_data_size);
    1572          360 :   ++caf_call_cnt;
    1573              : 
    1574          360 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_send_to_remote, 14,
    1575              :                              token, opt_lhs_desc, opt_lhs_charlen, image_index,
    1576              :                              rhs_size, rhs_data, opt_rhs_charlen, opt_rhs_desc,
    1577              :                              receiver_fn_index_tree, add_data_tree,
    1578              :                              add_data_size, lhs_stat, lhs_team, lhs_team_no);
    1579              : 
    1580          360 :   gfc_add_expr_to_block (&block, tmp);
    1581          360 :   gfc_add_block_to_block (&block, &lhs_se.post);
    1582          360 :   gfc_add_block_to_block (&block, &rhs_se.post);
    1583              : 
    1584              :   /* It guarantees memory consistency within the same segment.  */
    1585          360 :   tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
    1586          360 :   tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1587              :                     gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    1588              :                     tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    1589          360 :   ASM_VOLATILE_P (tmp) = 1;
    1590          360 :   gfc_add_expr_to_block (&block, tmp);
    1591              : 
    1592          360 :   return gfc_finish_block (&block);
    1593              : }
    1594              : 
    1595              : /* Send-get data to a remote coarray.  */
    1596              : 
    1597              : static tree
    1598          140 : conv_caf_sendget (gfc_code *code)
    1599              : {
    1600              :   /* lhs stuff  */
    1601          140 :   gfc_expr *lhs_expr, *lhs_hash, *receiver_fn_expr;
    1602          140 :   gfc_symbol *lhs_add_data_sym;
    1603          140 :   gfc_se lhs_se;
    1604          140 :   tree lhs_caf_decl, lhs_token, opt_lhs_charlen,
    1605          140 :     opt_lhs_desc = NULL_TREE, receiver_fn_index_tree, lhs_image_index,
    1606              :     lhs_add_data_tree, lhs_add_data_size, lhs_stat, lhs_team, lhs_team_no;
    1607          140 :   int transfer_rank;
    1608              : 
    1609              :   /* rhs stuff  */
    1610          140 :   gfc_expr *rhs_expr, *rhs_hash, *sender_fn_expr;
    1611          140 :   gfc_symbol *rhs_add_data_sym;
    1612          140 :   gfc_se rhs_se;
    1613          140 :   tree rhs_caf_decl, rhs_token, opt_rhs_charlen,
    1614          140 :     opt_rhs_desc = NULL_TREE, sender_fn_index_tree, rhs_image_index,
    1615              :     rhs_add_data_tree, rhs_add_data_size, rhs_stat, rhs_team, rhs_team_no;
    1616              : 
    1617              :   /* shared  */
    1618          140 :   stmtblock_t block;
    1619          140 :   gfc_namespace *ns;
    1620          140 :   tree tmp, rhs_size;
    1621              : 
    1622          140 :   gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
    1623          140 :   gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SENDGET);
    1624              : 
    1625          140 :   lhs_expr = code->ext.actual->expr;
    1626          140 :   rhs_expr = code->ext.actual->next->expr;
    1627          140 :   lhs_hash = code->ext.actual->next->next->expr;
    1628          140 :   receiver_fn_expr = code->ext.actual->next->next->next->expr;
    1629          140 :   rhs_hash = code->ext.actual->next->next->next->next->expr;
    1630          140 :   sender_fn_expr = code->ext.actual->next->next->next->next->next->expr;
    1631              : 
    1632          140 :   lhs_add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
    1633          140 :   rhs_add_data_sym = sender_fn_expr->symtree->n.sym->formal->sym;
    1634              : 
    1635          140 :   ns = lhs_expr->expr_type == EXPR_VARIABLE
    1636          140 :            && !lhs_expr->symtree->n.sym->attr.associate_var
    1637          140 :          ? lhs_expr->symtree->n.sym->ns
    1638              :          : gfc_current_ns;
    1639              : 
    1640          140 :   gfc_init_block (&block);
    1641              : 
    1642          140 :   lhs_stat = null_pointer_node;
    1643          140 :   lhs_team = null_pointer_node;
    1644          140 :   rhs_stat = null_pointer_node;
    1645          140 :   rhs_team = null_pointer_node;
    1646              : 
    1647              :   /* LHS.  */
    1648          140 :   gfc_init_se (&lhs_se, NULL);
    1649          140 :   lhs_caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
    1650          140 :   if (TREE_CODE (TREE_TYPE (lhs_caf_decl)) == REFERENCE_TYPE)
    1651            0 :     lhs_caf_decl = build_fold_indirect_ref_loc (input_location, lhs_caf_decl);
    1652          140 :   if (lhs_expr->rank == 0)
    1653              :     {
    1654           78 :       if (lhs_expr->ts.type == BT_CHARACTER)
    1655              :         {
    1656           16 :           gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
    1657           16 :           lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
    1658           16 :           opt_lhs_charlen = gfc_build_addr_expr (
    1659              :             NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
    1660              :         }
    1661              :       else
    1662           62 :         opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
    1663           78 :       opt_lhs_desc = null_pointer_node;
    1664              :     }
    1665              :   else
    1666              :     {
    1667           62 :       gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
    1668           62 :       gfc_add_block_to_block (&block, &lhs_se.pre);
    1669           62 :       opt_lhs_desc = lhs_se.expr;
    1670           62 :       if (lhs_expr->ts.type == BT_CHARACTER)
    1671           32 :         opt_lhs_charlen = gfc_build_addr_expr (
    1672              :           NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
    1673              :       else
    1674           30 :         opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
    1675              :       /* Get the third formal argument of the receiver function.  (This is the
    1676              :          location where to put the data on the remote image.)  Need to look at
    1677              :          the argument in the function decl, because in the gfc_symbol's formal
    1678              :          argument an array may have no descriptor while in the generated
    1679              :          function decl it has.  */
    1680           62 :       tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
    1681              :         TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
    1682           62 :       if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    1683           54 :         opt_lhs_desc = null_pointer_node;
    1684              :       else
    1685            8 :         opt_lhs_desc
    1686            8 :           = gfc_build_addr_expr (NULL_TREE,
    1687              :                                  gfc_trans_force_lval (&block, opt_lhs_desc));
    1688              :     }
    1689              : 
    1690              :   /* Obtain token, offset and image index for the LHS.  */
    1691          140 :   lhs_image_index = gfc_caf_get_image_index (&block, lhs_expr, lhs_caf_decl);
    1692          140 :   gfc_get_caf_token_offset (&lhs_se, &lhs_token, NULL, lhs_caf_decl, NULL,
    1693              :                             lhs_expr);
    1694              : 
    1695              :   /* RHS.  */
    1696          140 :   rhs_caf_decl = gfc_get_tree_for_caf_expr (rhs_expr);
    1697          140 :   if (TREE_CODE (TREE_TYPE (rhs_caf_decl)) == REFERENCE_TYPE)
    1698            0 :     rhs_caf_decl = build_fold_indirect_ref_loc (input_location, rhs_caf_decl);
    1699          140 :   transfer_rank = rhs_expr->rank;
    1700          140 :   gfc_expression_rank (rhs_expr);
    1701          140 :   gfc_init_se (&rhs_se, NULL);
    1702          140 :   if (rhs_expr->rank == 0)
    1703              :     {
    1704           80 :       opt_rhs_desc = null_pointer_node;
    1705           80 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1706              :         {
    1707           32 :           gfc_conv_expr (&rhs_se, rhs_expr);
    1708           32 :           gfc_add_block_to_block (&block, &rhs_se.pre);
    1709           32 :           opt_rhs_charlen = gfc_build_addr_expr (
    1710              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1711           32 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1712              :         }
    1713              :       else
    1714              :         {
    1715           48 :           gfc_typespec *ts
    1716           48 :             = &sender_fn_expr->symtree->n.sym->formal->next->next->sym->ts;
    1717              : 
    1718           48 :           opt_rhs_charlen
    1719           48 :             = build_zero_cst (build_pointer_type (size_type_node));
    1720           48 :           rhs_size = gfc_typenode_for_spec (ts)->type_common.size_unit;
    1721              :         }
    1722              :     }
    1723              :   /* Get the fifth formal argument of the getter function.  This is the argument
    1724              :      pointing to the data to get on the remote image.  Need to look at the
    1725              :      argument in the function decl, because in the gfc_symbol's formal argument
    1726              :      an array may have no descriptor while in the generated function decl it
    1727              :      has.  */
    1728           60 :   else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_VALUE (
    1729              :              TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
    1730              :                TREE_TYPE (sender_fn_expr->symtree->n.sym->backend_decl))))))))))
    1731              :     {
    1732           52 :       rhs_se.data_not_needed = 1;
    1733           52 :       gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
    1734           52 :       gfc_add_block_to_block (&block, &rhs_se.pre);
    1735           52 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1736              :         {
    1737           16 :           opt_rhs_charlen = gfc_build_addr_expr (
    1738              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1739           16 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1740              :         }
    1741              :       else
    1742              :         {
    1743           36 :           opt_rhs_charlen
    1744           36 :             = build_zero_cst (build_pointer_type (size_type_node));
    1745           36 :           rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
    1746              :         }
    1747           52 :       opt_rhs_desc = null_pointer_node;
    1748              :     }
    1749              :   else
    1750              :     {
    1751            8 :       gfc_ref *arr_ref = rhs_expr->ref;
    1752            8 :       while (arr_ref && arr_ref->type != REF_ARRAY)
    1753            0 :         arr_ref = arr_ref->next;
    1754            8 :       rhs_se.force_tmp
    1755           16 :         = (rhs_expr->shape == NULL
    1756            8 :            && (!arr_ref || !gfc_full_array_ref_p (arr_ref, nullptr)))
    1757           16 :           || !gfc_is_simply_contiguous (rhs_expr, false, false);
    1758            8 :       gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
    1759            8 :       gfc_add_block_to_block (&block, &rhs_se.pre);
    1760            8 :       opt_rhs_desc = rhs_se.expr;
    1761            8 :       if (rhs_expr->ts.type == BT_CHARACTER)
    1762              :         {
    1763            0 :           opt_rhs_charlen = gfc_build_addr_expr (
    1764              :             NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
    1765            0 :           rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
    1766              :         }
    1767              :       else
    1768              :         {
    1769            8 :           opt_rhs_charlen
    1770            8 :             = build_zero_cst (build_pointer_type (size_type_node));
    1771            8 :           rhs_size = fold_build2 (
    1772              :             MULT_EXPR, size_type_node,
    1773              :             fold_convert (size_type_node,
    1774              :                           rhs_expr->shape
    1775              :                             ? conv_shape_to_cst (rhs_expr)
    1776              :                             : gfc_conv_descriptor_size (rhs_se.expr,
    1777              :                                                         rhs_expr->rank)),
    1778              :             fold_convert (size_type_node,
    1779              :                           gfc_conv_descriptor_span_get (rhs_se.expr)));
    1780              :         }
    1781              : 
    1782            8 :       opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
    1783              :     }
    1784          140 :   gfc_add_block_to_block (&block, &rhs_se.pre);
    1785              : 
    1786              :   /* Obtain token, offset and image index for the RHS.  */
    1787          140 :   rhs_image_index = gfc_caf_get_image_index (&block, rhs_expr, rhs_caf_decl);
    1788          140 :   gfc_get_caf_token_offset (&rhs_se, &rhs_token, NULL, rhs_caf_decl, NULL,
    1789              :                             rhs_expr);
    1790              : 
    1791              :   /* stat and team.  */
    1792          140 :   conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
    1793          140 :   conv_stat_and_team (&block, rhs_expr, &rhs_stat, &rhs_team, &rhs_team_no);
    1794              : 
    1795          140 :   sender_fn_index_tree
    1796          140 :     = conv_caf_func_index (&block, ns, "__caf_transfer_from_fn_index_%d",
    1797              :                            rhs_hash);
    1798          140 :   rhs_add_data_tree
    1799          140 :     = conv_caf_add_call_data (&block, ns,
    1800              :                               "__caf_transfer_from_remote_add_data_%d",
    1801              :                               rhs_add_data_sym, &rhs_add_data_size);
    1802          140 :   receiver_fn_index_tree
    1803          140 :     = conv_caf_func_index (&block, ns, "__caf_transfer_to_remote_fn_index_%d",
    1804              :                            lhs_hash);
    1805          140 :   lhs_add_data_tree
    1806          140 :     = conv_caf_add_call_data (&block, ns,
    1807              :                               "__caf_transfer_to_remote_add_data_%d",
    1808              :                               lhs_add_data_sym, &lhs_add_data_size);
    1809          140 :   ++caf_call_cnt;
    1810              : 
    1811          140 :   tmp = build_call_expr_loc (
    1812              :     input_location, gfor_fndecl_caf_transfer_between_remotes, 22, lhs_token,
    1813              :     opt_lhs_desc, opt_lhs_charlen, lhs_image_index, receiver_fn_index_tree,
    1814              :     lhs_add_data_tree, lhs_add_data_size, rhs_token, opt_rhs_desc,
    1815              :     opt_rhs_charlen, rhs_image_index, sender_fn_index_tree, rhs_add_data_tree,
    1816              :     rhs_add_data_size, rhs_size,
    1817              :     transfer_rank == 0 ? boolean_true_node : boolean_false_node, lhs_stat,
    1818              :     rhs_stat, lhs_team, lhs_team_no, rhs_team, rhs_team_no);
    1819              : 
    1820          140 :   gfc_add_expr_to_block (&block, tmp);
    1821          140 :   gfc_add_block_to_block (&block, &lhs_se.post);
    1822          140 :   gfc_add_block_to_block (&block, &rhs_se.post);
    1823              : 
    1824              :   /* It guarantees memory consistency within the same segment.  */
    1825          140 :   tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
    1826          140 :   tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1827              :                     gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    1828              :                     tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    1829          140 :   ASM_VOLATILE_P (tmp) = 1;
    1830          140 :   gfc_add_expr_to_block (&block, tmp);
    1831              : 
    1832          140 :   return gfc_finish_block (&block);
    1833              : }
    1834              : 
    1835              : 
    1836              : /* F2018:5.4.7(5): a subobject of a coarray is a coarray with the codimensions
    1837              :    of that coarray.  Return a copy of E cut back to the reference carrying the
    1838              :    codimensions, so that the descriptor built for it holds the cobounds.  */
    1839              : 
    1840              : static gfc_expr *
    1841         1324 : strip_subobject_of_coarray (gfc_expr *e)
    1842              : {
    1843         1324 :   gfc_expr *coarray;
    1844         1324 :   gfc_ref *ref;
    1845         1324 :   gfc_typespec ts;
    1846              : 
    1847         1324 :   ts = e->symtree->n.sym->ts;
    1848         1481 :   for (ref = e->ref; ref; ref = ref->next)
    1849              :     {
    1850         1481 :       if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    1851              :         break;
    1852          157 :       if (ref->type == REF_COMPONENT)
    1853          157 :         ts = ref->u.c.component->ts;
    1854              :     }
    1855              : 
    1856         1324 :   coarray = gfc_copy_expr (e);
    1857         1324 :   if (!ref || !ref->next)
    1858              :     return coarray;
    1859              : 
    1860           82 :   for (ref = coarray->ref; ref; ref = ref->next)
    1861           82 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    1862              :       break;
    1863              : 
    1864           64 :   gfc_free_ref_list (ref->next);
    1865           64 :   ref->next = NULL;
    1866           64 :   coarray->ts = ts;
    1867           64 :   gfc_expression_rank (coarray);
    1868              : 
    1869           64 :   return coarray;
    1870              : }
    1871              : 
    1872              : 
    1873              : static void
    1874         1406 : trans_this_image (gfc_se * se, gfc_expr *expr)
    1875              : {
    1876         1406 :   stmtblock_t loop;
    1877         1406 :   tree type, desc, dim_arg, cond, tmp, m, loop_var, exit_label, min_var, lbound,
    1878              :     ubound, extent, ml, team;
    1879         1406 :   gfc_expr *coarray;
    1880         1406 :   gfc_se argse;
    1881         1406 :   int rank, corank;
    1882              : 
    1883              :   /* The case -fcoarray=single is handled elsewhere.  */
    1884         1406 :   gcc_assert (flag_coarray != GFC_FCOARRAY_SINGLE);
    1885              : 
    1886              :   /* Translate team, if present.  */
    1887         1406 :   if (expr->value.function.actual->next->next->expr)
    1888              :     {
    1889           18 :       gfc_init_se (&argse, NULL);
    1890           18 :       gfc_conv_expr_val (&argse, expr->value.function.actual->next->next->expr);
    1891           18 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    1892           18 :       gfc_add_block_to_block (&se->post, &argse.post);
    1893           18 :       team = fold_convert (pvoid_type_node, argse.expr);
    1894              :     }
    1895              :   else
    1896         1388 :     team = null_pointer_node;
    1897              : 
    1898              :   /* Argument-free version: THIS_IMAGE().  */
    1899         1406 :   if (expr->value.function.actual->expr == NULL)
    1900              :     {
    1901         1076 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
    1902              :                                  team);
    1903         1076 :       se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
    1904              :                                tmp);
    1905         1088 :       return;
    1906              :     }
    1907              : 
    1908              :   /* Coarray-argument version: THIS_IMAGE(coarray [, dim]).  */
    1909              : 
    1910          330 :   type = gfc_get_int_type (gfc_default_integer_kind);
    1911              : 
    1912          330 :   coarray = strip_subobject_of_coarray (expr->value.function.actual->expr);
    1913          330 :   corank = coarray->corank;
    1914          330 :   rank = coarray->rank;
    1915              : 
    1916              :   /* Obtain the descriptor of the COARRAY.  */
    1917          330 :   gfc_init_se (&argse, NULL);
    1918          330 :   argse.want_coarray = 1;
    1919          330 :   gfc_conv_expr_descriptor (&argse, coarray);
    1920          330 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    1921          330 :   gfc_add_block_to_block (&se->post, &argse.post);
    1922          330 :   desc = argse.expr;
    1923          330 :   gfc_free_expr (coarray);
    1924              : 
    1925          330 :   if (se->ss)
    1926              :     {
    1927              :       /* Create an implicit second parameter from the loop variable.  */
    1928           82 :       gcc_assert (!expr->value.function.actual->next->expr);
    1929           82 :       gcc_assert (corank > 0);
    1930           82 :       gcc_assert (se->loop->dimen == 1);
    1931           82 :       gcc_assert (se->ss->info->expr == expr);
    1932              : 
    1933           82 :       dim_arg = fold_convert_loc (input_location, gfc_array_dim_rank_type,
    1934              :                                   se->loop->loopvar[0]);
    1935           82 :       dim_arg = fold_build2_loc (input_location, PLUS_EXPR,
    1936              :                                  gfc_array_dim_rank_type, dim_arg,
    1937              :                                  gfc_rank_cst[1]);
    1938           82 :       gfc_advance_se_ss_chain (se);
    1939              :     }
    1940              :   else
    1941              :     {
    1942              :       /* Use the passed DIM= argument.  */
    1943          248 :       gcc_assert (expr->value.function.actual->next->expr);
    1944          248 :       gfc_init_se (&argse, NULL);
    1945          248 :       gfc_conv_expr_type (&argse, expr->value.function.actual->next->expr,
    1946              :                           gfc_array_dim_rank_type);
    1947          248 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    1948          248 :       dim_arg = argse.expr;
    1949              : 
    1950          248 :       if (INTEGER_CST_P (dim_arg))
    1951              :         {
    1952          132 :           if (wi::ltu_p (wi::to_wide (dim_arg), 1)
    1953          264 :               || wi::gtu_p (wi::to_wide (dim_arg),
    1954          132 :                             GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
    1955            0 :             gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
    1956            0 :                        "dimension index", expr->value.function.isym->name,
    1957              :                        &expr->where);
    1958              :         }
    1959          116 :      else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    1960              :         {
    1961            0 :           dim_arg = gfc_evaluate_now (dim_arg, &se->pre);
    1962            0 :           cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    1963              :                                   dim_arg, gfc_rank_cst[1]);
    1964            0 :           tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
    1965            0 :           tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    1966              :                                  dim_arg, tmp);
    1967            0 :           cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    1968              :                                   logical_type_node, cond, tmp);
    1969            0 :           gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    1970              :                                    gfc_msg_fault);
    1971              :         }
    1972              :     }
    1973              : 
    1974              :   /* Used algorithm; cf. Fortran 2008, C.10. Note, due to the scalarizer,
    1975              :      one always has a dim_arg argument.
    1976              : 
    1977              :      m = this_image() - 1
    1978              :      if (corank == 1)
    1979              :        {
    1980              :          sub(1) = m + lcobound(corank)
    1981              :          return;
    1982              :        }
    1983              :      i = rank
    1984              :      min_var = min (rank + corank - 2, rank + dim_arg - 1)
    1985              :      for (;;)
    1986              :        {
    1987              :          extent = gfc_extent(i)
    1988              :          ml = m
    1989              :          m  = m/extent
    1990              :          if (i >= min_var)
    1991              :            goto exit_label
    1992              :          i++
    1993              :        }
    1994              :      exit_label:
    1995              :      sub(dim_arg) = (dim_arg < corank) ? ml - m*extent + lcobound(dim_arg)
    1996              :                                        : m + lcobound(corank)
    1997              :   */
    1998              : 
    1999              :   /* this_image () - 1.  */
    2000          330 :   tmp
    2001          330 :     = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1, team);
    2002          330 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
    2003              :                          fold_convert (type, tmp), build_int_cst (type, 1));
    2004          330 :   if (corank == 1)
    2005              :     {
    2006              :       /* sub(1) = m + lcobound(corank).  */
    2007           12 :       lbound = gfc_conv_descriptor_lbound_get (desc,
    2008           12 :                         build_int_cst (TREE_TYPE (gfc_array_index_type),
    2009           12 :                                        corank+rank-1));
    2010           12 :       lbound = fold_convert (type, lbound);
    2011           12 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
    2012              : 
    2013           12 :       se->expr = tmp;
    2014           12 :       return;
    2015              :     }
    2016              : 
    2017          318 :   m = gfc_create_var (type, NULL);
    2018          318 :   ml = gfc_create_var (type, NULL);
    2019          318 :   loop_var = gfc_create_var (gfc_array_dim_rank_type, NULL);
    2020          318 :   min_var = gfc_create_var (gfc_array_dim_rank_type, NULL);
    2021              : 
    2022              :   /* m = this_image () - 1.  */
    2023          318 :   gfc_add_modify (&se->pre, m, tmp);
    2024              : 
    2025              :   /* min_var = min (rank + corank-2, rank + dim_arg - 1).  */
    2026          318 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, signed_char_type_node,
    2027              :                          fold_convert_loc (input_location,
    2028              :                                            signed_char_type_node, dim_arg),
    2029          318 :                          build_int_cst (signed_char_type_node, rank - 1));
    2030          318 :   tmp = fold_convert_loc (input_location, gfc_array_dim_rank_type, tmp);
    2031          636 :   tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_dim_rank_type,
    2032          318 :                          gfc_rank_cst[rank + corank - 2], tmp);
    2033          318 :   gfc_add_modify (&se->pre, min_var, tmp);
    2034              : 
    2035              :   /* i = rank.  */
    2036          318 :   tmp = gfc_rank_cst[rank];
    2037          318 :   gfc_add_modify (&se->pre, loop_var, tmp);
    2038              : 
    2039          318 :   exit_label = gfc_build_label_decl (NULL_TREE);
    2040          318 :   TREE_USED (exit_label) = 1;
    2041              : 
    2042              :   /* Loop body.  */
    2043          318 :   gfc_init_block (&loop);
    2044              : 
    2045              :   /* ml = m.  */
    2046          318 :   gfc_add_modify (&loop, ml, m);
    2047              : 
    2048              :   /* extent = ...  */
    2049          318 :   lbound = gfc_conv_descriptor_lbound_get (desc, loop_var);
    2050          318 :   ubound = gfc_conv_descriptor_ubound_get (desc, loop_var);
    2051          318 :   extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
    2052          318 :   extent = fold_convert (type, extent);
    2053              : 
    2054              :   /* m = m/extent.  */
    2055          318 :   gfc_add_modify (&loop, m,
    2056              :                   fold_build2_loc (input_location, TRUNC_DIV_EXPR, type,
    2057              :                           m, extent));
    2058              : 
    2059              :   /* Exit condition:  if (i >= min_var) goto exit_label.  */
    2060          318 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, loop_var,
    2061              :                   min_var);
    2062          318 :   tmp = build1_v (GOTO_EXPR, exit_label);
    2063          318 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    2064              :                          build_empty_stmt (input_location));
    2065          318 :   gfc_add_expr_to_block (&loop, tmp);
    2066              : 
    2067              :   /* Increment loop variable: i++.  */
    2068          318 :   gfc_add_modify (&loop, loop_var,
    2069              :                   fold_build2_loc (input_location, PLUS_EXPR,
    2070          318 :                                    TREE_TYPE (loop_var), loop_var,
    2071              :                                    gfc_rank_cst[1]));
    2072              : 
    2073              :   /* Making the loop... actually loop!  */
    2074          318 :   tmp = gfc_finish_block (&loop);
    2075          318 :   tmp = build1_v (LOOP_EXPR, tmp);
    2076          318 :   gfc_add_expr_to_block (&se->pre, tmp);
    2077              : 
    2078              :   /* The exit label.  */
    2079          318 :   tmp = build1_v (LABEL_EXPR, exit_label);
    2080          318 :   gfc_add_expr_to_block (&se->pre, tmp);
    2081              : 
    2082              :   /*  sub(co_dim) = (co_dim < corank) ? ml - m*extent + lcobound(dim_arg)
    2083              :                                       : m + lcobound(corank) */
    2084              : 
    2085          318 :   cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, dim_arg,
    2086          318 :                           build_int_cst (TREE_TYPE (dim_arg), corank));
    2087              : 
    2088          636 :   lbound = gfc_conv_descriptor_lbound_get (desc,
    2089              :                 fold_build2_loc (input_location, PLUS_EXPR,
    2090          318 :                                  TREE_TYPE (dim_arg), dim_arg,
    2091          318 :                                  build_int_cst (TREE_TYPE (dim_arg), rank-1)));
    2092          318 :   lbound = fold_convert (type, lbound);
    2093              : 
    2094          318 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, type, ml,
    2095              :                          fold_build2_loc (input_location, MULT_EXPR, type,
    2096              :                                           m, extent));
    2097          318 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
    2098              : 
    2099          318 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
    2100              :                               fold_build2_loc (input_location, PLUS_EXPR, type,
    2101              :                                                m, lbound));
    2102              : }
    2103              : 
    2104              : 
    2105              : /* Convert a call to image_status.  */
    2106              : 
    2107              : static void
    2108           38 : conv_intrinsic_image_status (gfc_se *se, gfc_expr *expr)
    2109              : {
    2110           38 :   unsigned int num_args;
    2111           38 :   tree *args, tmp;
    2112              : 
    2113           38 :   num_args = gfc_intrinsic_argument_list_length (expr);
    2114           38 :   args = XALLOCAVEC (tree, num_args);
    2115           38 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    2116              :   /* In args[0] the number of the image the status is desired for has to be
    2117              :      given.  */
    2118              : 
    2119           38 :   if (flag_coarray == GFC_FCOARRAY_SINGLE)
    2120              :     {
    2121            1 :       tree arg;
    2122            1 :       arg = gfc_evaluate_now (args[0], &se->pre);
    2123            1 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    2124              :                              fold_convert (integer_type_node, arg),
    2125              :                              integer_one_node);
    2126            1 :       tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
    2127              :                              tmp, integer_zero_node,
    2128              :                              build_int_cst (integer_type_node,
    2129              :                                             GFC_STAT_STOPPED_IMAGE));
    2130              :     }
    2131           37 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
    2132              :     /* The team is optional and therefore needs to be a pointer to the opaque
    2133              :        pointer.  */
    2134           41 :     tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_image_status, 2,
    2135              :                                args[0],
    2136              :                                num_args < 2
    2137              :                                  ? null_pointer_node
    2138            4 :                                  : gfc_build_addr_expr (NULL_TREE, args[1]));
    2139              :   else
    2140            0 :     gcc_unreachable ();
    2141              : 
    2142           38 :   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
    2143           38 : }
    2144              : 
    2145              : static void
    2146           53 : conv_intrinsic_team_number (gfc_se *se, gfc_expr *expr)
    2147              : {
    2148           53 :   unsigned int num_args;
    2149              : 
    2150           53 :   tree *args, tmp;
    2151              : 
    2152           53 :   num_args = gfc_intrinsic_argument_list_length (expr);
    2153           53 :   args = XALLOCAVEC (tree, num_args);
    2154           53 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    2155              : 
    2156           53 :   if (flag_coarray == GFC_FCOARRAY_SINGLE)
    2157              :     /* Only the initial team exists, and its team number is -1.  */
    2158           26 :     tmp = build_int_cst (integer_type_node, -1);
    2159           27 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
    2160           27 :     tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
    2161           27 :                                expr->value.function.actual->expr
    2162              :                                ? args[0] : null_pointer_node);
    2163              :   else
    2164            0 :     gcc_unreachable ();
    2165              : 
    2166           53 :   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
    2167           53 : }
    2168              : 
    2169              : 
    2170              : static void
    2171          257 : trans_image_index (gfc_se * se, gfc_expr *expr)
    2172              : {
    2173          257 :   tree num_images, cond, coindex, type, lbound, ubound, desc, subdesc, tmp,
    2174          257 :     invalid_bound, team = null_pointer_node, team_number = null_pointer_node;
    2175          257 :   gfc_expr *coarray;
    2176          257 :   gfc_se argse, subse;
    2177          257 :   int rank, corank, codim;
    2178              : 
    2179          257 :   type = gfc_get_int_type (gfc_default_integer_kind);
    2180              : 
    2181          257 :   coarray = strip_subobject_of_coarray (expr->value.function.actual->expr);
    2182          257 :   corank = coarray->corank;
    2183          257 :   rank = coarray->rank;
    2184              : 
    2185              :   /* Obtain the descriptor of the COARRAY.  */
    2186          257 :   gfc_init_se (&argse, NULL);
    2187          257 :   argse.want_coarray = 1;
    2188          257 :   gfc_conv_expr_descriptor (&argse, coarray);
    2189          257 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2190          257 :   gfc_add_block_to_block (&se->post, &argse.post);
    2191          257 :   desc = argse.expr;
    2192          257 :   gfc_free_expr (coarray);
    2193              : 
    2194              :   /* Obtain a handle to the SUB argument.  */
    2195          257 :   gfc_init_se (&subse, NULL);
    2196          257 :   gfc_conv_expr_descriptor (&subse, expr->value.function.actual->next->expr);
    2197          257 :   gfc_add_block_to_block (&se->pre, &subse.pre);
    2198          257 :   gfc_add_block_to_block (&se->post, &subse.post);
    2199          257 :   subdesc = build_fold_indirect_ref_loc (input_location,
    2200              :                         gfc_conv_descriptor_data_get (subse.expr));
    2201              : 
    2202          257 :   if (expr->value.function.actual->next->next->expr)
    2203              :     {
    2204           27 :       gfc_init_se (&argse, NULL);
    2205           27 :       gfc_conv_expr_val (&argse, expr->value.function.actual->next->next->expr);
    2206           27 :       team = argse.expr;
    2207           27 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2208           27 :       gfc_add_block_to_block (&se->post, &argse.post);
    2209              :     }
    2210          230 :   else if (expr->value.function.actual->next->next->next->expr)
    2211              :     {
    2212           25 :       gfc_init_se (&argse, NULL);
    2213           25 :       gfc_conv_expr_val (&argse,
    2214           25 :                          expr->value.function.actual->next->next->next->expr);
    2215           25 :       team_number = gfc_build_addr_expr (
    2216              :         NULL_TREE,
    2217              :         gfc_trans_force_lval (&argse.pre,
    2218              :                               fold_convert (integer_type_node, argse.expr)));
    2219           25 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2220           25 :       gfc_add_block_to_block (&se->post, &argse.post);
    2221              :     }
    2222              : 
    2223              :   /* Fortran 2008 does not require that the values remain in the cobounds,
    2224              :      thus we need explicitly check this - and return 0 if they are exceeded.  */
    2225              : 
    2226          257 :   lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
    2227          257 :   tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1], NULL);
    2228          257 :   invalid_bound = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2229              :                                  fold_convert (gfc_array_index_type, tmp),
    2230              :                                  lbound);
    2231              : 
    2232          513 :   for (codim = corank + rank - 2; codim >= rank; codim--)
    2233              :     {
    2234          256 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
    2235          256 :       ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
    2236          256 :       tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
    2237          256 :       cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2238              :                               fold_convert (gfc_array_index_type, tmp),
    2239              :                               lbound);
    2240          256 :       invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    2241              :                                        logical_type_node, invalid_bound, cond);
    2242          256 :       cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2243              :                               fold_convert (gfc_array_index_type, tmp),
    2244              :                               ubound);
    2245          256 :       invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    2246              :                                        logical_type_node, invalid_bound, cond);
    2247              :     }
    2248              : 
    2249          257 :   invalid_bound = gfc_unlikely (invalid_bound, PRED_FORTRAN_INVALID_BOUND);
    2250              : 
    2251              :   /* See Fortran 2008, C.10 for the following algorithm.  */
    2252              : 
    2253              :   /* coindex = sub(corank) - lcobound(n).  */
    2254          257 :   coindex = fold_convert (gfc_array_index_type,
    2255              :                           gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1],
    2256              :                                                NULL));
    2257          257 :   lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
    2258          257 :   coindex = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    2259              :                              fold_convert (gfc_array_index_type, coindex),
    2260              :                              lbound);
    2261              : 
    2262          770 :   for (codim = corank + rank - 2; codim >= rank; codim--)
    2263              :     {
    2264          256 :       tree extent, ubound;
    2265              : 
    2266              :       /* coindex = coindex*extent(codim) + sub(codim) - lcobound(codim).  */
    2267          256 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
    2268          256 :       ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
    2269          256 :       extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
    2270              : 
    2271              :       /* coindex *= extent.  */
    2272          256 :       coindex = fold_build2_loc (input_location, MULT_EXPR,
    2273              :                                  gfc_array_index_type, coindex, extent);
    2274              : 
    2275              :       /* coindex += sub(codim).  */
    2276          256 :       tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
    2277          256 :       coindex = fold_build2_loc (input_location, PLUS_EXPR,
    2278              :                                  gfc_array_index_type, coindex,
    2279              :                                  fold_convert (gfc_array_index_type, tmp));
    2280              : 
    2281              :       /* coindex -= lbound(codim).  */
    2282          256 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
    2283          256 :       coindex = fold_build2_loc (input_location, MINUS_EXPR,
    2284              :                                  gfc_array_index_type, coindex, lbound);
    2285              :     }
    2286              : 
    2287          257 :   coindex = fold_build2_loc (input_location, PLUS_EXPR, type,
    2288              :                              fold_convert(type, coindex),
    2289              :                              build_int_cst (type, 1));
    2290              : 
    2291              :   /* Return 0 if "coindex" exceeds num_images().  */
    2292              : 
    2293          257 :   if (flag_coarray == GFC_FCOARRAY_SINGLE)
    2294          126 :     num_images = build_int_cst (type, 1);
    2295              :   else
    2296              :     {
    2297          131 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
    2298              :                                  team, team_number);
    2299          131 :       num_images = fold_convert (type, tmp);
    2300              :     }
    2301              : 
    2302          257 :   tmp = gfc_create_var (type, NULL);
    2303          257 :   gfc_add_modify (&se->pre, tmp, coindex);
    2304              : 
    2305          257 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, tmp,
    2306              :                           num_images);
    2307          257 :   cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
    2308              :                           cond,
    2309              :                           fold_convert (logical_type_node, invalid_bound));
    2310          257 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
    2311              :                               build_int_cst (type, 0), tmp);
    2312          257 : }
    2313              : 
    2314              : static void
    2315          928 : trans_num_images (gfc_se * se, gfc_expr *expr)
    2316              : {
    2317          928 :   tree tmp, team = null_pointer_node, team_number = null_pointer_node;
    2318          928 :   gfc_se argse;
    2319              : 
    2320          928 :   if (expr->value.function.actual->expr)
    2321              :     {
    2322           18 :       gfc_init_se (&argse, NULL);
    2323           18 :       gfc_conv_expr_val (&argse, expr->value.function.actual->expr);
    2324           18 :       team = argse.expr;
    2325           18 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2326           18 :       gfc_add_block_to_block (&se->post, &argse.post);
    2327              :     }
    2328          910 :   else if (expr->value.function.actual->next->expr)
    2329              :     {
    2330           24 :       gfc_init_se (&argse, NULL);
    2331           24 :       gfc_conv_expr_val (&argse, expr->value.function.actual->next->expr);
    2332           24 :       team_number = gfc_build_addr_expr (
    2333              :         NULL_TREE,
    2334              :         gfc_trans_force_lval (&argse.pre,
    2335              :                               fold_convert (integer_type_node, argse.expr)));
    2336           24 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2337           24 :       gfc_add_block_to_block (&se->post, &argse.post);
    2338              :     }
    2339              : 
    2340          928 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
    2341              :                              team, team_number);
    2342          928 :   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
    2343          928 : }
    2344              : 
    2345              : 
    2346              : static void
    2347        13983 : gfc_conv_intrinsic_rank (gfc_se *se, gfc_expr *expr)
    2348              : {
    2349        13983 :   gfc_se argse;
    2350              : 
    2351        13983 :   gfc_init_se (&argse, NULL);
    2352        13983 :   argse.data_not_needed = 1;
    2353        13983 :   argse.descriptor_only = 1;
    2354              : 
    2355        13983 :   gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
    2356        13983 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2357        13983 :   gfc_add_block_to_block (&se->post, &argse.post);
    2358              : 
    2359        13983 :   se->expr = gfc_conv_descriptor_rank_get (argse.expr);
    2360        13983 :   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
    2361              :                            se->expr);
    2362        13983 : }
    2363              : 
    2364              : 
    2365              : static void
    2366          748 : gfc_conv_intrinsic_is_contiguous (gfc_se * se, gfc_expr * expr)
    2367              : {
    2368          748 :   gfc_expr *arg;
    2369          748 :   arg = expr->value.function.actual->expr;
    2370          748 :   gfc_conv_is_contiguous_expr (se, arg);
    2371          748 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    2372          748 : }
    2373              : 
    2374              : /* This function does the work for gfc_conv_intrinsic_is_contiguous,
    2375              :    plus it can be called directly.  */
    2376              : 
    2377              : void
    2378         2165 : gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
    2379              : {
    2380         2165 :   gfc_ss *ss;
    2381         2165 :   gfc_se argse;
    2382         2165 :   tree desc, tmp, stride, extent, cond;
    2383         2165 :   int i;
    2384         2165 :   tree fncall0;
    2385         2165 :   gfc_array_spec *as;
    2386         2165 :   gfc_symbol *sym = NULL;
    2387              : 
    2388         2165 :   if (arg->ts.type == BT_CLASS)
    2389           96 :     gfc_add_class_array_ref (arg);
    2390              : 
    2391         2165 :   if (arg->expr_type == EXPR_VARIABLE)
    2392         2129 :     sym = arg->symtree->n.sym;
    2393              : 
    2394         2165 :   ss = gfc_walk_expr (arg);
    2395         2165 :   gcc_assert (ss != gfc_ss_terminator);
    2396         2165 :   gfc_init_se (&argse, NULL);
    2397         2165 :   argse.data_not_needed = 1;
    2398         2165 :   gfc_conv_expr_descriptor (&argse, arg);
    2399              : 
    2400         2165 :   as = gfc_get_full_arrayspec_from_expr (arg);
    2401              : 
    2402              :   /* Create:  stride[0] == 1 && stride[1] == extend[0]*stride[0] && ...
    2403              :      Note in addition that zero-sized arrays don't count as contiguous.  */
    2404              : 
    2405         2165 :   if (as && as->type == AS_ASSUMED_RANK)
    2406              :     {
    2407              :       /* Build the call to is_contiguous0.  */
    2408          250 :       argse.want_pointer = 1;
    2409          250 :       gfc_conv_expr_descriptor (&argse, arg);
    2410          250 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2411          250 :       gfc_add_block_to_block (&se->post, &argse.post);
    2412          250 :       tree ptr = gfc_evaluate_now (argse.expr, &se->pre);
    2413          250 :       fncall0 = build_call_expr_loc (input_location,
    2414              :                                      gfor_fndecl_is_contiguous0, 1, ptr);
    2415          250 :       desc = build_fold_indirect_ref_loc (input_location, ptr);
    2416          250 :       se->expr = fncall0;
    2417          250 :       se->expr = convert (boolean_type_node, se->expr);
    2418          250 :     }
    2419              :   else
    2420              :     {
    2421         1915 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2422         1915 :       gfc_add_block_to_block (&se->post, &argse.post);
    2423         1915 :       desc = gfc_evaluate_now (argse.expr, &se->pre);
    2424              : 
    2425         1915 :       stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[0]);
    2426         1915 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    2427         1915 :                               stride, build_int_cst (TREE_TYPE (stride), 1));
    2428              : 
    2429         2238 :       for (i = 0; i < arg->rank - 1; i++)
    2430              :         {
    2431          323 :           tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
    2432          323 :           extent = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
    2433          323 :           extent = fold_build2_loc (input_location, MINUS_EXPR,
    2434              :                                     gfc_array_index_type, extent, tmp);
    2435          323 :           extent = fold_build2_loc (input_location, PLUS_EXPR,
    2436              :                                     gfc_array_index_type, extent,
    2437              :                                     gfc_index_one_node);
    2438          323 :           tmp = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i]);
    2439          323 :           tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    2440              :                                  tmp, extent);
    2441          323 :           stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i+1]);
    2442          323 :           tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    2443              :                                  stride, tmp);
    2444          323 :           cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    2445              :                                   boolean_type_node, cond, tmp);
    2446              :         }
    2447         1915 :       se->expr = cond;
    2448              :     }
    2449              : 
    2450              :   /* An array that is addressed by the span of its descriptor needs to be
    2451              :      checked if that span differs from the element size.  */
    2452          894 :   if (as && sym && !sym->attr.contiguous
    2453         3059 :       && (IS_POINTER (sym) || gfc_is_span_addressed_dummy (sym)))
    2454              :     {
    2455          254 :       tree span = gfc_conv_descriptor_span_get (desc);
    2456          254 :       tmp = fold_convert (TREE_TYPE (span),
    2457              :                           gfc_conv_descriptor_elem_len_get (desc));
    2458          254 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    2459              :                               span, tmp);
    2460          254 :       se->expr = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2461              :                                   boolean_type_node, cond,
    2462              :                                   convert (boolean_type_node, se->expr));
    2463              :     }
    2464              : 
    2465         2165 :   if (as && as->type == AS_ASSUMED_RANK)
    2466              :     {
    2467          250 :       tree rank = gfc_conv_descriptor_rank_get (desc);
    2468          250 :       tree scalar = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    2469              :                                      rank, gfc_rank_cst[0]);
    2470          250 :       se->expr = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    2471          250 :                                   TREE_TYPE (se->expr), scalar, se->expr);
    2472              :     }
    2473              : 
    2474         2165 :   gfc_free_ss_chain (ss);
    2475         2165 : }
    2476              : 
    2477              : 
    2478              : /* Evaluate a single upper or lower bound.  */
    2479              : /* TODO: bound intrinsic generates way too much unnecessary code.  */
    2480              : 
    2481              : static void
    2482        16361 : gfc_conv_intrinsic_bound (gfc_se * se, gfc_expr * expr, enum gfc_isym_id op)
    2483              : {
    2484        16361 :   gfc_actual_arglist *arg;
    2485        16361 :   gfc_actual_arglist *arg2;
    2486        16361 :   tree desc;
    2487        16361 :   tree type;
    2488        16361 :   tree bound;
    2489        16361 :   tree tmp;
    2490        16361 :   tree cond, cond1;
    2491        16361 :   tree ubound;
    2492        16361 :   tree lbound;
    2493        16361 :   tree size;
    2494        16361 :   gfc_se argse;
    2495        16361 :   gfc_array_spec * as;
    2496        16361 :   bool assumed_rank_lb_one;
    2497              : 
    2498        16361 :   arg = expr->value.function.actual;
    2499        16361 :   arg2 = arg->next;
    2500              : 
    2501        16361 :   if (se->ss)
    2502              :     {
    2503              :       /* Create an implicit second parameter from the loop variable.  */
    2504         8028 :       gcc_assert (!arg2->expr || op == GFC_ISYM_SHAPE);
    2505         8028 :       gcc_assert (se->loop->dimen == 1);
    2506         8028 :       gcc_assert (se->ss->info->expr == expr);
    2507         8028 :       gfc_advance_se_ss_chain (se);
    2508         8028 :       bound = se->loop->loopvar[0];
    2509         8028 :       bound = fold_build2_loc (input_location, MINUS_EXPR,
    2510              :                                gfc_array_index_type, bound,
    2511              :                                se->loop->from[0]);
    2512         8028 :       bound = fold_convert_loc (input_location, gfc_array_dim_rank_type,
    2513              :                                 bound);
    2514              :     }
    2515              :   else
    2516              :     {
    2517              :       /* use the passed argument.  */
    2518         8333 :       gcc_assert (arg2->expr);
    2519         8333 :       gfc_init_se (&argse, NULL);
    2520         8333 :       gfc_conv_expr_type (&argse, arg2->expr, gfc_array_dim_rank_type);
    2521         8333 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2522         8333 :       bound = argse.expr;
    2523              :       /* Convert from one based to zero based.  */
    2524         8333 :       bound = fold_build2_loc (input_location, MINUS_EXPR,
    2525              :                                gfc_array_dim_rank_type, bound,
    2526              :                                gfc_rank_cst[1]);
    2527              :     }
    2528              : 
    2529              :   /* TODO: don't re-evaluate the descriptor on each iteration.  */
    2530              :   /* Get a descriptor for the first parameter.  */
    2531        16361 :   gfc_init_se (&argse, NULL);
    2532        16361 :   gfc_conv_expr_descriptor (&argse, arg->expr);
    2533        16361 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2534        16361 :   gfc_add_block_to_block (&se->post, &argse.post);
    2535              : 
    2536        16361 :   desc = argse.expr;
    2537              : 
    2538        16361 :   as = gfc_get_full_arrayspec_from_expr (arg->expr);
    2539              : 
    2540        16361 :   if (INTEGER_CST_P (bound))
    2541              :     {
    2542         8213 :       gcc_assert (op != GFC_ISYM_SHAPE);
    2543         7976 :       if (((!as || as->type != AS_ASSUMED_RANK)
    2544         7305 :            && wi::geu_p (wi::to_wide (bound),
    2545         7305 :                          GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))))
    2546        16426 :           || wi::gtu_p (wi::to_wide (bound), GFC_MAX_DIMENSIONS))
    2547            0 :         gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
    2548              :                    "dimension index",
    2549              :                    (op == GFC_ISYM_UBOUND) ? "UBOUND" : "LBOUND",
    2550              :                    &expr->where);
    2551              :     }
    2552              : 
    2553        16361 :   if (!INTEGER_CST_P (bound) || (as && as->type == AS_ASSUMED_RANK))
    2554              :     {
    2555         9056 :       if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2556              :         {
    2557          651 :           bound = gfc_evaluate_now (bound, &se->pre);
    2558          651 :           cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2559              :                                   bound, gfc_rank_cst[0]);
    2560          651 :           if (as && as->type == AS_ASSUMED_RANK)
    2561          546 :             tmp = gfc_conv_descriptor_rank_get (desc);
    2562              :           else
    2563          105 :             tmp = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))];
    2564          651 :           tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    2565              :                                  bound, tmp);
    2566          651 :           cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    2567              :                                   logical_type_node, cond, tmp);
    2568          651 :           gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    2569              :                                    gfc_msg_fault);
    2570              :         }
    2571              :     }
    2572              : 
    2573              :   /* Take care of the lbound shift for assumed-rank arrays that are
    2574              :      nonallocatable and nonpointers. Those have a lbound of 1.  */
    2575        15777 :   assumed_rank_lb_one = as && as->type == AS_ASSUMED_RANK
    2576        11241 :                         && ((arg->expr->ts.type != BT_CLASS
    2577         1987 :                              && !arg->expr->symtree->n.sym->attr.allocatable
    2578         1644 :                              && !arg->expr->symtree->n.sym->attr.pointer)
    2579          920 :                             || (arg->expr->ts.type == BT_CLASS
    2580          198 :                              && !CLASS_DATA (arg->expr)->attr.allocatable
    2581          162 :                              && !CLASS_DATA (arg->expr)->attr.class_pointer));
    2582              : 
    2583        16361 :   ubound = gfc_conv_descriptor_ubound_get (desc, bound);
    2584        16361 :   lbound = gfc_conv_descriptor_lbound_get (desc, bound);
    2585        16361 :   size = fold_build2_loc (input_location, MINUS_EXPR,
    2586              :                           gfc_array_index_type, ubound, lbound);
    2587        16361 :   size = fold_build2_loc (input_location, PLUS_EXPR,
    2588              :                           gfc_array_index_type, size, gfc_index_one_node);
    2589              : 
    2590              :   /* 13.14.53: Result value for LBOUND
    2591              : 
    2592              :      Case (i): For an array section or for an array expression other than a
    2593              :                whole array or array structure component, LBOUND(ARRAY, DIM)
    2594              :                has the value 1.  For a whole array or array structure
    2595              :                component, LBOUND(ARRAY, DIM) has the value:
    2596              :                  (a) equal to the lower bound for subscript DIM of ARRAY if
    2597              :                      dimension DIM of ARRAY does not have extent zero
    2598              :                      or if ARRAY is an assumed-size array of rank DIM,
    2599              :               or (b) 1 otherwise.
    2600              : 
    2601              :      13.14.113: Result value for UBOUND
    2602              : 
    2603              :      Case (i): For an array section or for an array expression other than a
    2604              :                whole array or array structure component, UBOUND(ARRAY, DIM)
    2605              :                has the value equal to the number of elements in the given
    2606              :                dimension; otherwise, it has a value equal to the upper bound
    2607              :                for subscript DIM of ARRAY if dimension DIM of ARRAY does
    2608              :                not have size zero and has value zero if dimension DIM has
    2609              :                size zero.  */
    2610              : 
    2611        16361 :   if (op == GFC_ISYM_LBOUND && assumed_rank_lb_one)
    2612          556 :     se->expr = gfc_index_one_node;
    2613        15805 :   else if (as)
    2614              :     {
    2615        15221 :       if (op == GFC_ISYM_UBOUND)
    2616              :         {
    2617         5407 :           cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2618              :                                   size, gfc_index_zero_node);
    2619        10186 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    2620              :                                       gfc_array_index_type, cond,
    2621              :                                       (assumed_rank_lb_one ? size : ubound),
    2622              :                                       gfc_index_zero_node);
    2623              :         }
    2624         9814 :       else if (op == GFC_ISYM_LBOUND)
    2625              :         {
    2626         4931 :           cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2627              :                                   size, gfc_index_zero_node);
    2628         4931 :           if (as->type == AS_ASSUMED_SIZE)
    2629              :             {
    2630           98 :               cond1 = fold_build2_loc (input_location, EQ_EXPR,
    2631              :                                        logical_type_node, bound,
    2632           98 :                                        build_int_cst (TREE_TYPE (bound),
    2633           98 :                                                       arg->expr->rank - 1));
    2634           98 :               cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    2635              :                                       logical_type_node, cond, cond1);
    2636              :             }
    2637         4931 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    2638              :                                       gfc_array_index_type, cond,
    2639              :                                       lbound, gfc_index_one_node);
    2640              :         }
    2641         4883 :       else if (op == GFC_ISYM_SHAPE)
    2642         4883 :         se->expr = fold_build2_loc (input_location, MAX_EXPR,
    2643              :                                     gfc_array_index_type, size,
    2644              :                                     gfc_index_zero_node);
    2645              :       else
    2646            0 :         gcc_unreachable ();
    2647              : 
    2648              :       /* According to F2018 16.9.172, para 5, an assumed rank object,
    2649              :          argument associated with and assumed size array, has the ubound
    2650              :          of the final dimension set to -1 and UBOUND must return this.
    2651              :          Similarly for the SHAPE intrinsic.  */
    2652        15221 :       if (op != GFC_ISYM_LBOUND && assumed_rank_lb_one)
    2653              :         {
    2654          835 :           tree minus_one = build_int_cst (gfc_array_index_type, -1);
    2655          835 :           tree rank = gfc_conv_descriptor_rank_get (desc);
    2656          835 :           rank = fold_build2_loc (input_location, MINUS_EXPR,
    2657              :                                   gfc_array_dim_rank_type, rank,
    2658              :                                   gfc_rank_cst[1]);
    2659              : 
    2660              :           /* Fix the expression to stop it from becoming even more
    2661              :              complicated.  */
    2662          835 :           se->expr = gfc_evaluate_now (se->expr, &se->pre);
    2663              : 
    2664              :           /* Descriptors for assumed-size arrays have ubound = -1
    2665              :              in the last dimension.  */
    2666          835 :           cond1 = fold_build2_loc (input_location, EQ_EXPR,
    2667              :                                    logical_type_node, ubound, minus_one);
    2668          835 :           cond = fold_build2_loc (input_location, EQ_EXPR,
    2669              :                                   logical_type_node, bound, rank);
    2670          835 :           cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    2671              :                                   logical_type_node, cond, cond1);
    2672          835 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    2673              :                                       gfc_array_index_type, cond,
    2674              :                                       minus_one, se->expr);
    2675              :         }
    2676              :     }
    2677              :   else   /* as is null; this is an old-fashioned 1-based array.  */
    2678              :     {
    2679          584 :       if (op != GFC_ISYM_LBOUND)
    2680              :         {
    2681          482 :           se->expr = fold_build2_loc (input_location, MAX_EXPR,
    2682              :                                       gfc_array_index_type, size,
    2683              :                                       gfc_index_zero_node);
    2684              :         }
    2685              :       else
    2686          102 :         se->expr = gfc_index_one_node;
    2687              :     }
    2688              : 
    2689              : 
    2690        16361 :   type = gfc_typenode_for_spec (&expr->ts);
    2691        16361 :   se->expr = convert (type, se->expr);
    2692        16361 : }
    2693              : 
    2694              : 
    2695              : static void
    2696          737 : conv_intrinsic_cobound (gfc_se * se, gfc_expr * expr)
    2697              : {
    2698          737 :   gfc_actual_arglist *arg;
    2699          737 :   gfc_actual_arglist *arg2;
    2700          737 :   gfc_expr *coarray;
    2701          737 :   gfc_se argse;
    2702          737 :   tree bound, lbound, resbound, resbound2, desc, cond, tmp;
    2703          737 :   tree type;
    2704          737 :   int corank;
    2705              : 
    2706          737 :   gcc_assert (expr->value.function.isym->id == GFC_ISYM_LCOBOUND
    2707              :               || expr->value.function.isym->id == GFC_ISYM_UCOBOUND
    2708              :               || expr->value.function.isym->id == GFC_ISYM_COSHAPE
    2709              :               || expr->value.function.isym->id == GFC_ISYM_THIS_IMAGE);
    2710              : 
    2711          737 :   arg = expr->value.function.actual;
    2712          737 :   arg2 = arg->next;
    2713              : 
    2714          737 :   gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
    2715              : 
    2716          737 :   coarray = strip_subobject_of_coarray (arg->expr);
    2717          737 :   corank = coarray->corank;
    2718              : 
    2719          737 :   gfc_init_se (&argse, NULL);
    2720          737 :   argse.want_coarray = 1;
    2721              : 
    2722          737 :   gfc_conv_expr_descriptor (&argse, coarray);
    2723          737 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2724          737 :   gfc_add_block_to_block (&se->post, &argse.post);
    2725          737 :   desc = argse.expr;
    2726              : 
    2727          737 :   if (se->ss)
    2728              :     {
    2729              :       /* Create an implicit second parameter from the loop variable.  */
    2730          297 :       gcc_assert (!arg2->expr
    2731              :                   || expr->value.function.isym->id == GFC_ISYM_COSHAPE);
    2732          297 :       gcc_assert (corank > 0);
    2733          297 :       gcc_assert (se->loop->dimen == 1);
    2734          297 :       gcc_assert (se->ss->info->expr == expr);
    2735              : 
    2736          297 :       bound = fold_convert_loc (input_location, gfc_array_dim_rank_type,
    2737              :                                 se->loop->loopvar[0]);
    2738          297 :       tree rank = gfc_rank_cst[coarray->rank];
    2739          297 :       bound = fold_build2_loc (input_location, PLUS_EXPR,
    2740              :                                gfc_array_dim_rank_type, bound, rank);
    2741          297 :       gfc_advance_se_ss_chain (se);
    2742              :     }
    2743          440 :   else if (expr->value.function.isym->id == GFC_ISYM_COSHAPE)
    2744            0 :     bound = gfc_rank_cst[1];
    2745              :   else
    2746              :     {
    2747          440 :       gcc_assert (arg2->expr);
    2748          440 :       gfc_init_se (&argse, NULL);
    2749          440 :       gfc_conv_expr_type (&argse, arg2->expr, gfc_array_dim_rank_type);
    2750          440 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    2751          440 :       bound = argse.expr;
    2752              : 
    2753          440 :       if (INTEGER_CST_P (bound))
    2754              :         {
    2755          346 :           if (wi::ltu_p (wi::to_wide (bound), 1)
    2756          692 :               || wi::gtu_p (wi::to_wide (bound),
    2757          346 :                             GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
    2758            0 :             gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
    2759            0 :                        "dimension index", expr->value.function.isym->name,
    2760              :                        &expr->where);
    2761              :         }
    2762           94 :       else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2763              :         {
    2764           36 :           bound = gfc_evaluate_now (bound, &se->pre);
    2765           36 :           cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2766              :                                   bound, gfc_rank_cst[1]);
    2767           36 :           tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
    2768           36 :           tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2769              :                                  bound, rank);
    2770           36 :           cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    2771              :                                   logical_type_node, cond, tmp);
    2772           36 :           gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    2773              :                                    gfc_msg_fault);
    2774              :         }
    2775              : 
    2776              : 
    2777              :       /* Subtract 1 to get to zero based and add dimensions.  */
    2778          440 :       switch (coarray->rank)
    2779              :         {
    2780           82 :         case 0:
    2781           82 :           bound = fold_build2_loc (input_location, MINUS_EXPR,
    2782              :                                    gfc_array_dim_rank_type, bound,
    2783              :                                    gfc_rank_cst[1]);
    2784              :         case 1:
    2785              :           break;
    2786           38 :         default:
    2787           38 :           {
    2788           38 :             tree rank = gfc_rank_cst[coarray->rank - 1];
    2789           38 :             bound = fold_build2_loc (input_location, PLUS_EXPR,
    2790              :                                      gfc_array_dim_rank_type, bound, rank);
    2791              :           }
    2792              :         }
    2793              :     }
    2794              : 
    2795          737 :   resbound = gfc_conv_descriptor_lbound_get (desc, bound);
    2796              : 
    2797              :   /* COSHAPE needs the lower cobound and so it is stashed here before resbound
    2798              :      is overwritten.  */
    2799          737 :   lbound = NULL_TREE;
    2800          737 :   if (expr->value.function.isym->id == GFC_ISYM_COSHAPE)
    2801           16 :     lbound = resbound;
    2802              : 
    2803              :   /* Handle UCOBOUND with special handling of the last codimension.  */
    2804          737 :   if (expr->value.function.isym->id == GFC_ISYM_UCOBOUND
    2805          465 :       || expr->value.function.isym->id == GFC_ISYM_COSHAPE)
    2806              :     {
    2807              :       /* Last codimension: For -fcoarray=single just return
    2808              :          the lcobound - otherwise add
    2809              :            ceiling (real (num_images ()) / real (size)) - 1
    2810              :          = (num_images () + size - 1) / size - 1
    2811              :          = (num_images - 1) / size(),
    2812              :          where size is the product of the extent of all but the last
    2813              :          codimension.  */
    2814              : 
    2815          288 :       if (flag_coarray != GFC_FCOARRAY_SINGLE && corank > 1)
    2816              :         {
    2817           80 :           tree cosize;
    2818              : 
    2819           80 :           cosize = gfc_conv_descriptor_cosize (desc, coarray->rank, corank);
    2820           80 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
    2821              :                                      2, null_pointer_node, null_pointer_node);
    2822           80 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2823              :                                  gfc_array_index_type,
    2824              :                                  fold_convert (gfc_array_index_type, tmp),
    2825              :                                  build_int_cst (gfc_array_index_type, 1));
    2826           80 :           tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    2827              :                                  gfc_array_index_type, tmp,
    2828              :                                  fold_convert (gfc_array_index_type, cosize));
    2829           80 :           resbound = fold_build2_loc (input_location, PLUS_EXPR,
    2830              :                                       gfc_array_index_type, resbound, tmp);
    2831           80 :         }
    2832          208 :       else if (flag_coarray != GFC_FCOARRAY_SINGLE)
    2833              :         {
    2834              :           /* ubound = lbound + num_images() - 1.  */
    2835           56 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
    2836              :                                      2, null_pointer_node, null_pointer_node);
    2837           56 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2838              :                                  gfc_array_index_type,
    2839              :                                  fold_convert (gfc_array_index_type, tmp),
    2840              :                                  build_int_cst (gfc_array_index_type, 1));
    2841           56 :           resbound = fold_build2_loc (input_location, PLUS_EXPR,
    2842              :                                       gfc_array_index_type, resbound, tmp);
    2843              :         }
    2844              : 
    2845          288 :       if (corank > 1)
    2846              :         {
    2847          195 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    2848              :                                   bound,
    2849          195 :                                   build_int_cst (TREE_TYPE (bound),
    2850          195 :                                                  coarray->rank + corank - 1));
    2851              : 
    2852          195 :           resbound2 = gfc_conv_descriptor_ubound_get (desc, bound);
    2853          195 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    2854              :                                       gfc_array_index_type, cond,
    2855              :                                       resbound, resbound2);
    2856              :         }
    2857              :       else
    2858              :         se->expr = resbound;
    2859              : 
    2860              :       /* Get the coshape for this dimension.  */
    2861          288 :       if (expr->value.function.isym->id == GFC_ISYM_COSHAPE)
    2862              :         {
    2863           16 :           gcc_assert (lbound != NULL_TREE);
    2864           16 :           se->expr = fold_build2_loc (input_location, MINUS_EXPR,
    2865              :                                       gfc_array_index_type,
    2866              :                                       se->expr, lbound);
    2867           16 :           se->expr = fold_build2_loc (input_location, PLUS_EXPR,
    2868              :                                       gfc_array_index_type,
    2869              :                                       se->expr, gfc_index_one_node);
    2870              :         }
    2871              :     }
    2872              :   else
    2873          449 :     se->expr = resbound;
    2874              : 
    2875          737 :   type = gfc_typenode_for_spec (&expr->ts);
    2876          737 :   se->expr = convert (type, se->expr);
    2877              : 
    2878          737 :   gfc_free_expr (coarray);
    2879          737 : }
    2880              : 
    2881              : 
    2882              : static void
    2883         2548 : conv_intrinsic_stride (gfc_se * se, gfc_expr * expr)
    2884              : {
    2885         2548 :   gfc_actual_arglist *array_arg;
    2886         2548 :   gfc_actual_arglist *dim_arg;
    2887         2548 :   gfc_se argse;
    2888         2548 :   tree desc, tmp;
    2889              : 
    2890         2548 :   array_arg = expr->value.function.actual;
    2891         2548 :   dim_arg = array_arg->next;
    2892              : 
    2893         2548 :   gcc_assert (array_arg->expr->expr_type == EXPR_VARIABLE);
    2894              : 
    2895         2548 :   gfc_init_se (&argse, NULL);
    2896         2548 :   gfc_conv_expr_descriptor (&argse, array_arg->expr);
    2897         2548 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2898         2548 :   gfc_add_block_to_block (&se->post, &argse.post);
    2899         2548 :   desc = argse.expr;
    2900              : 
    2901         2548 :   gcc_assert (dim_arg->expr);
    2902         2548 :   gfc_init_se (&argse, NULL);
    2903         2548 :   gfc_conv_expr_type (&argse, dim_arg->expr, gfc_array_index_type);
    2904         2548 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    2905         2548 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    2906              :                          argse.expr, gfc_index_one_node);
    2907         2548 :   se->expr = gfc_conv_descriptor_stride_get (desc, tmp);
    2908         2548 : }
    2909              : 
    2910              : static void
    2911         8069 : gfc_conv_intrinsic_abs (gfc_se * se, gfc_expr * expr)
    2912              : {
    2913         8069 :   tree arg, cabs;
    2914              : 
    2915         8069 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    2916              : 
    2917         8069 :   switch (expr->value.function.actual->expr->ts.type)
    2918              :     {
    2919         7045 :     case BT_INTEGER:
    2920         7045 :     case BT_REAL:
    2921         7045 :       se->expr = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (arg),
    2922              :                                   arg);
    2923         7045 :       break;
    2924              : 
    2925         1024 :     case BT_COMPLEX:
    2926         1024 :       cabs = gfc_builtin_decl_for_float_kind (BUILT_IN_CABS, expr->ts.kind);
    2927         1024 :       se->expr = build_call_expr_loc (input_location, cabs, 1, arg);
    2928         1024 :       break;
    2929              : 
    2930            0 :     default:
    2931            0 :       gcc_unreachable ();
    2932              :     }
    2933         8069 : }
    2934              : 
    2935              : 
    2936              : /* Create a complex value from one or two real components.  */
    2937              : 
    2938              : static void
    2939          497 : gfc_conv_intrinsic_cmplx (gfc_se * se, gfc_expr * expr, int both)
    2940              : {
    2941          497 :   tree real;
    2942          497 :   tree imag;
    2943          497 :   tree type;
    2944          497 :   tree *args;
    2945          497 :   unsigned int num_args;
    2946              : 
    2947          497 :   num_args = gfc_intrinsic_argument_list_length (expr);
    2948          497 :   args = XALLOCAVEC (tree, num_args);
    2949              : 
    2950          497 :   type = gfc_typenode_for_spec (&expr->ts);
    2951          497 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    2952          497 :   real = convert (TREE_TYPE (type), args[0]);
    2953          497 :   if (both)
    2954          453 :     imag = convert (TREE_TYPE (type), args[1]);
    2955           44 :   else if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE)
    2956              :     {
    2957           30 :       imag = fold_build1_loc (input_location, IMAGPART_EXPR,
    2958           30 :                               TREE_TYPE (TREE_TYPE (args[0])), args[0]);
    2959           30 :       imag = convert (TREE_TYPE (type), imag);
    2960              :     }
    2961              :   else
    2962           14 :     imag = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
    2963              : 
    2964          497 :   se->expr = fold_build2_loc (input_location, COMPLEX_EXPR, type, real, imag);
    2965          497 : }
    2966              : 
    2967              : 
    2968              : /* Remainder function MOD(A, P) = A - INT(A / P) * P
    2969              :                       MODULO(A, P) = A - FLOOR (A / P) * P
    2970              : 
    2971              :    The obvious algorithms above are numerically instable for large
    2972              :    arguments, hence these intrinsics are instead implemented via calls
    2973              :    to the fmod family of functions.  It is the responsibility of the
    2974              :    user to ensure that the second argument is non-zero.  */
    2975              : 
    2976              : static void
    2977         4030 : gfc_conv_intrinsic_mod (gfc_se * se, gfc_expr * expr, int modulo)
    2978              : {
    2979         4030 :   tree type;
    2980         4030 :   tree tmp;
    2981         4030 :   tree test;
    2982         4030 :   tree test2;
    2983         4030 :   tree fmod;
    2984         4030 :   tree zero;
    2985         4030 :   tree args[2];
    2986              : 
    2987         4030 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    2988              : 
    2989         4030 :   switch (expr->ts.type)
    2990              :     {
    2991         3877 :     case BT_INTEGER:
    2992              :       /* Integer case is easy, we've got a builtin op.  */
    2993         3877 :       type = TREE_TYPE (args[0]);
    2994              : 
    2995         3877 :       if (modulo)
    2996          411 :        se->expr = fold_build2_loc (input_location, FLOOR_MOD_EXPR, type,
    2997              :                                    args[0], args[1]);
    2998              :       else
    2999         3466 :        se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
    3000              :                                    args[0], args[1]);
    3001              :       break;
    3002              : 
    3003           30 :     case BT_UNSIGNED:
    3004              :       /* Even easier, we only need one.  */
    3005           30 :       type = TREE_TYPE (args[0]);
    3006           30 :       se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
    3007              :                                   args[0], args[1]);
    3008           30 :       break;
    3009              : 
    3010          123 :     case BT_REAL:
    3011          123 :       fmod = NULL_TREE;
    3012              :       /* Check if we have a builtin fmod.  */
    3013          123 :       fmod = gfc_builtin_decl_for_float_kind (BUILT_IN_FMOD, expr->ts.kind);
    3014              : 
    3015              :       /* The builtin should always be available.  */
    3016          123 :       gcc_assert (fmod != NULL_TREE);
    3017              : 
    3018          123 :       tmp = build_addr (fmod);
    3019          123 :       se->expr = build_call_array_loc (input_location,
    3020          123 :                                        TREE_TYPE (TREE_TYPE (fmod)),
    3021              :                                        tmp, 2, args);
    3022          123 :       if (modulo == 0)
    3023          123 :         return;
    3024              : 
    3025           25 :       type = TREE_TYPE (args[0]);
    3026              : 
    3027           25 :       args[0] = gfc_evaluate_now (args[0], &se->pre);
    3028           25 :       args[1] = gfc_evaluate_now (args[1], &se->pre);
    3029              : 
    3030              :       /* Definition:
    3031              :          modulo = arg - floor (arg/arg2) * arg2
    3032              : 
    3033              :          In order to calculate the result accurately, we use the fmod
    3034              :          function as follows.
    3035              : 
    3036              :          res = fmod (arg, arg2);
    3037              :          if (res)
    3038              :            {
    3039              :              if ((arg < 0) xor (arg2 < 0))
    3040              :                res += arg2;
    3041              :            }
    3042              :          else
    3043              :            res = copysign (0., arg2);
    3044              : 
    3045              :          => As two nested ternary exprs:
    3046              : 
    3047              :          res = res ? (((arg < 0) xor (arg2 < 0)) ? res + arg2 : res)
    3048              :                : copysign (0., arg2);
    3049              : 
    3050              :       */
    3051              : 
    3052           25 :       zero = gfc_build_const (type, integer_zero_node);
    3053           25 :       tmp = gfc_evaluate_now (se->expr, &se->pre);
    3054           25 :       if (!flag_signed_zeros)
    3055              :         {
    3056            1 :           test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    3057              :                                   args[0], zero);
    3058            1 :           test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    3059              :                                    args[1], zero);
    3060            1 :           test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
    3061              :                                    logical_type_node, test, test2);
    3062            1 :           test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    3063              :                                   tmp, zero);
    3064            1 :           test = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    3065              :                                   logical_type_node, test, test2);
    3066            1 :           test = gfc_evaluate_now (test, &se->pre);
    3067            1 :           se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
    3068              :                                       fold_build2_loc (input_location,
    3069              :                                                        PLUS_EXPR,
    3070              :                                                        type, tmp, args[1]),
    3071              :                                       tmp);
    3072              :         }
    3073              :       else
    3074              :         {
    3075           24 :           tree expr1, copysign, cscall;
    3076           24 :           copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN,
    3077              :                                                       expr->ts.kind);
    3078           24 :           test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    3079              :                                   args[0], zero);
    3080           24 :           test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    3081              :                                    args[1], zero);
    3082           24 :           test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
    3083              :                                    logical_type_node, test, test2);
    3084           24 :           expr1 = fold_build3_loc (input_location, COND_EXPR, type, test2,
    3085              :                                    fold_build2_loc (input_location,
    3086              :                                                     PLUS_EXPR,
    3087              :                                                     type, tmp, args[1]),
    3088              :                                    tmp);
    3089           24 :           test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    3090              :                                   tmp, zero);
    3091           24 :           cscall = build_call_expr_loc (input_location, copysign, 2, zero,
    3092              :                                         args[1]);
    3093           24 :           se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
    3094              :                                       expr1, cscall);
    3095              :         }
    3096              :       return;
    3097              : 
    3098            0 :     default:
    3099            0 :       gcc_unreachable ();
    3100              :     }
    3101              : }
    3102              : 
    3103              : /* DSHIFTL(I,J,S) = (I << S) | (J >> (BITSIZE(J) - S))
    3104              :    DSHIFTR(I,J,S) = (I << (BITSIZE(I) - S)) | (J >> S)
    3105              :    where the right shifts are logical (i.e. 0's are shifted in).
    3106              :    Because SHIFT_EXPR's want shifts strictly smaller than the integral
    3107              :    type width, we have to special-case both S == 0 and S == BITSIZE(J):
    3108              :      DSHIFTL(I,J,0) = I
    3109              :      DSHIFTL(I,J,BITSIZE) = J
    3110              :      DSHIFTR(I,J,0) = J
    3111              :      DSHIFTR(I,J,BITSIZE) = I.  */
    3112              : 
    3113              : static void
    3114          132 : gfc_conv_intrinsic_dshift (gfc_se * se, gfc_expr * expr, bool dshiftl)
    3115              : {
    3116          132 :   tree type, utype, stype, arg1, arg2, shift, res, left, right;
    3117          132 :   tree args[3], cond, tmp;
    3118          132 :   int bitsize;
    3119              : 
    3120          132 :   gfc_conv_intrinsic_function_args (se, expr, args, 3);
    3121              : 
    3122          132 :   gcc_assert (TREE_TYPE (args[0]) == TREE_TYPE (args[1]));
    3123          132 :   type = TREE_TYPE (args[0]);
    3124          132 :   bitsize = TYPE_PRECISION (type);
    3125          132 :   utype = unsigned_type_for (type);
    3126          132 :   stype = TREE_TYPE (args[2]);
    3127              : 
    3128          132 :   arg1 = gfc_evaluate_now (args[0], &se->pre);
    3129          132 :   arg2 = gfc_evaluate_now (args[1], &se->pre);
    3130          132 :   shift = gfc_evaluate_now (args[2], &se->pre);
    3131              : 
    3132              :   /* The generic case.  */
    3133          132 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, stype,
    3134          132 :                          build_int_cst (stype, bitsize), shift);
    3135          198 :   left = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3136              :                           arg1, dshiftl ? shift : tmp);
    3137              : 
    3138          198 :   right = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
    3139              :                            fold_convert (utype, arg2), dshiftl ? tmp : shift);
    3140          132 :   right = fold_convert (type, right);
    3141              : 
    3142          132 :   res = fold_build2_loc (input_location, BIT_IOR_EXPR, type, left, right);
    3143              : 
    3144              :   /* Special cases.  */
    3145          132 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
    3146              :                           build_int_cst (stype, 0));
    3147          198 :   res = fold_build3_loc (input_location, COND_EXPR, type, cond,
    3148              :                          dshiftl ? arg1 : arg2, res);
    3149              : 
    3150          132 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
    3151          132 :                           build_int_cst (stype, bitsize));
    3152          198 :   res = fold_build3_loc (input_location, COND_EXPR, type, cond,
    3153              :                          dshiftl ? arg2 : arg1, res);
    3154              : 
    3155          132 :   se->expr = res;
    3156          132 : }
    3157              : 
    3158              : 
    3159              : /* Positive difference DIM (x, y) = ((x - y) < 0) ? 0 : x - y.  */
    3160              : 
    3161              : static void
    3162           96 : gfc_conv_intrinsic_dim (gfc_se * se, gfc_expr * expr)
    3163              : {
    3164           96 :   tree val;
    3165           96 :   tree tmp;
    3166           96 :   tree type;
    3167           96 :   tree zero;
    3168           96 :   tree args[2];
    3169              : 
    3170           96 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    3171           96 :   type = TREE_TYPE (args[0]);
    3172              : 
    3173           96 :   val = fold_build2_loc (input_location, MINUS_EXPR, type, args[0], args[1]);
    3174           96 :   val = gfc_evaluate_now (val, &se->pre);
    3175              : 
    3176           96 :   zero = gfc_build_const (type, integer_zero_node);
    3177           96 :   tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node, val, zero);
    3178           96 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, zero, val);
    3179           96 : }
    3180              : 
    3181              : 
    3182              : /* SIGN(A, B) is absolute value of A times sign of B.
    3183              :    The real value versions use library functions to ensure the correct
    3184              :    handling of negative zero.  Integer case implemented as:
    3185              :    SIGN(A, B) = { tmp = (A ^ B) >> C; (A + tmp) ^ tmp }
    3186              :   */
    3187              : 
    3188              : static void
    3189          423 : gfc_conv_intrinsic_sign (gfc_se * se, gfc_expr * expr)
    3190              : {
    3191          423 :   tree tmp;
    3192          423 :   tree type;
    3193          423 :   tree args[2];
    3194              : 
    3195          423 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    3196          423 :   if (expr->ts.type == BT_REAL)
    3197              :     {
    3198          161 :       tree abs;
    3199              : 
    3200          161 :       tmp = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
    3201          161 :       abs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
    3202              : 
    3203              :       /* We explicitly have to ignore the minus sign. We do so by using
    3204              :          result = (arg1 == 0) ? abs(arg0) : copysign(arg0, arg1).  */
    3205          161 :       if (!flag_sign_zero
    3206          197 :           && MODE_HAS_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (args[1]))))
    3207              :         {
    3208           12 :           tree cond, zero;
    3209           12 :           zero = build_real_from_int_cst (TREE_TYPE (args[1]), integer_zero_node);
    3210           12 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3211              :                                   args[1], zero);
    3212           24 :           se->expr = fold_build3_loc (input_location, COND_EXPR,
    3213           12 :                                   TREE_TYPE (args[0]), cond,
    3214              :                                   build_call_expr_loc (input_location, abs, 1,
    3215              :                                                        args[0]),
    3216              :                                   build_call_expr_loc (input_location, tmp, 2,
    3217              :                                                        args[0], args[1]));
    3218              :         }
    3219              :       else
    3220          149 :         se->expr = build_call_expr_loc (input_location, tmp, 2,
    3221              :                                         args[0], args[1]);
    3222          161 :       return;
    3223              :     }
    3224              : 
    3225              :   /* Having excluded floating point types, we know we are now dealing
    3226              :      with signed integer types.  */
    3227          262 :   type = TREE_TYPE (args[0]);
    3228              : 
    3229              :   /* Args[0] is used multiple times below.  */
    3230          262 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    3231              : 
    3232              :   /* Construct (A ^ B) >> 31, which generates a bit mask of all zeros if
    3233              :      the signs of A and B are the same, and of all ones if they differ.  */
    3234          262 :   tmp = fold_build2_loc (input_location, BIT_XOR_EXPR, type, args[0], args[1]);
    3235          262 :   tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, tmp,
    3236          262 :                          build_int_cst (type, TYPE_PRECISION (type) - 1));
    3237          262 :   tmp = gfc_evaluate_now (tmp, &se->pre);
    3238              : 
    3239              :   /* Construct (A + tmp) ^ tmp, which is A if tmp is zero, and -A if tmp]
    3240              :      is all ones (i.e. -1).  */
    3241          262 :   se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR, type,
    3242              :                               fold_build2_loc (input_location, PLUS_EXPR,
    3243              :                                                type, args[0], tmp), tmp);
    3244              : }
    3245              : 
    3246              : 
    3247              : /* Test for the presence of an optional argument.  */
    3248              : 
    3249              : static void
    3250         5232 : gfc_conv_intrinsic_present (gfc_se * se, gfc_expr * expr)
    3251              : {
    3252         5232 :   gfc_expr *arg;
    3253              : 
    3254         5232 :   arg = expr->value.function.actual->expr;
    3255         5232 :   gcc_assert (arg->expr_type == EXPR_VARIABLE);
    3256         5232 :   se->expr = gfc_conv_expr_present (arg->symtree->n.sym);
    3257         5232 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    3258         5232 : }
    3259              : 
    3260              : 
    3261              : /* Calculate the double precision product of two single precision values.  */
    3262              : 
    3263              : static void
    3264           13 : gfc_conv_intrinsic_dprod (gfc_se * se, gfc_expr * expr)
    3265              : {
    3266           13 :   tree type;
    3267           13 :   tree args[2];
    3268              : 
    3269           13 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    3270              : 
    3271              :   /* Convert the args to double precision before multiplying.  */
    3272           13 :   type = gfc_typenode_for_spec (&expr->ts);
    3273           13 :   args[0] = convert (type, args[0]);
    3274           13 :   args[1] = convert (type, args[1]);
    3275           13 :   se->expr = fold_build2_loc (input_location, MULT_EXPR, type, args[0],
    3276              :                               args[1]);
    3277           13 : }
    3278              : 
    3279              : 
    3280              : /* Return a length one character string containing an ascii character.  */
    3281              : 
    3282              : static void
    3283         2038 : gfc_conv_intrinsic_char (gfc_se * se, gfc_expr * expr)
    3284              : {
    3285         2038 :   tree arg[2];
    3286         2038 :   tree var;
    3287         2038 :   tree type;
    3288         2038 :   unsigned int num_args;
    3289              : 
    3290         2038 :   num_args = gfc_intrinsic_argument_list_length (expr);
    3291         2038 :   gfc_conv_intrinsic_function_args (se, expr, arg, num_args);
    3292              : 
    3293         2038 :   type = gfc_get_char_type (expr->ts.kind);
    3294         2038 :   var = gfc_create_var (type, "char");
    3295              : 
    3296         2038 :   arg[0] = fold_build1_loc (input_location, NOP_EXPR, type, arg[0]);
    3297         2038 :   gfc_add_modify (&se->pre, var, arg[0]);
    3298         2038 :   se->expr = gfc_build_addr_expr (build_pointer_type (type), var);
    3299         2038 :   se->string_length = build_int_cst (gfc_charlen_type_node, 1);
    3300         2038 : }
    3301              : 
    3302              : 
    3303              : static void
    3304            0 : gfc_conv_intrinsic_ctime (gfc_se * se, gfc_expr * expr)
    3305              : {
    3306            0 :   tree var;
    3307            0 :   tree len;
    3308            0 :   tree tmp;
    3309            0 :   tree cond;
    3310            0 :   tree fndecl;
    3311            0 :   tree *args;
    3312            0 :   unsigned int num_args;
    3313              : 
    3314            0 :   num_args = gfc_intrinsic_argument_list_length (expr) + 2;
    3315            0 :   args = XALLOCAVEC (tree, num_args);
    3316              : 
    3317            0 :   var = gfc_create_var (pchar_type_node, "pstr");
    3318            0 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    3319              : 
    3320            0 :   gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
    3321            0 :   args[0] = gfc_build_addr_expr (NULL_TREE, var);
    3322            0 :   args[1] = gfc_build_addr_expr (NULL_TREE, len);
    3323              : 
    3324            0 :   fndecl = build_addr (gfor_fndecl_ctime);
    3325            0 :   tmp = build_call_array_loc (input_location,
    3326            0 :                           TREE_TYPE (TREE_TYPE (gfor_fndecl_ctime)),
    3327              :                           fndecl, num_args, args);
    3328            0 :   gfc_add_expr_to_block (&se->pre, tmp);
    3329              : 
    3330              :   /* Free the temporary afterwards, if necessary.  */
    3331            0 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3332            0 :                           len, build_int_cst (TREE_TYPE (len), 0));
    3333            0 :   tmp = gfc_call_free (var);
    3334            0 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    3335            0 :   gfc_add_expr_to_block (&se->post, tmp);
    3336              : 
    3337            0 :   se->expr = var;
    3338            0 :   se->string_length = len;
    3339            0 : }
    3340              : 
    3341              : 
    3342              : static void
    3343            0 : gfc_conv_intrinsic_fdate (gfc_se * se, gfc_expr * expr)
    3344              : {
    3345            0 :   tree var;
    3346            0 :   tree len;
    3347            0 :   tree tmp;
    3348            0 :   tree cond;
    3349            0 :   tree fndecl;
    3350            0 :   tree *args;
    3351            0 :   unsigned int num_args;
    3352              : 
    3353            0 :   num_args = gfc_intrinsic_argument_list_length (expr) + 2;
    3354            0 :   args = XALLOCAVEC (tree, num_args);
    3355              : 
    3356            0 :   var = gfc_create_var (pchar_type_node, "pstr");
    3357            0 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    3358              : 
    3359            0 :   gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
    3360            0 :   args[0] = gfc_build_addr_expr (NULL_TREE, var);
    3361            0 :   args[1] = gfc_build_addr_expr (NULL_TREE, len);
    3362              : 
    3363            0 :   fndecl = build_addr (gfor_fndecl_fdate);
    3364            0 :   tmp = build_call_array_loc (input_location,
    3365            0 :                           TREE_TYPE (TREE_TYPE (gfor_fndecl_fdate)),
    3366              :                           fndecl, num_args, args);
    3367            0 :   gfc_add_expr_to_block (&se->pre, tmp);
    3368              : 
    3369              :   /* Free the temporary afterwards, if necessary.  */
    3370            0 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3371            0 :                           len, build_int_cst (TREE_TYPE (len), 0));
    3372            0 :   tmp = gfc_call_free (var);
    3373            0 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    3374            0 :   gfc_add_expr_to_block (&se->post, tmp);
    3375              : 
    3376            0 :   se->expr = var;
    3377            0 :   se->string_length = len;
    3378            0 : }
    3379              : 
    3380              : 
    3381              : /* Generate a direct call to free() for the FREE subroutine.  */
    3382              : 
    3383              : static tree
    3384           10 : conv_intrinsic_free (gfc_code *code)
    3385              : {
    3386           10 :   stmtblock_t block;
    3387           10 :   gfc_se argse;
    3388           10 :   tree arg, call;
    3389              : 
    3390           10 :   gfc_init_se (&argse, NULL);
    3391           10 :   gfc_conv_expr (&argse, code->ext.actual->expr);
    3392           10 :   arg = fold_convert (ptr_type_node, argse.expr);
    3393              : 
    3394           10 :   gfc_init_block (&block);
    3395           10 :   call = build_call_expr_loc (input_location,
    3396              :                               builtin_decl_explicit (BUILT_IN_FREE), 1, arg);
    3397           10 :   gfc_add_expr_to_block (&block, call);
    3398           10 :   return gfc_finish_block (&block);
    3399              : }
    3400              : 
    3401              : 
    3402              : /* Call the RANDOM_INIT library subroutine with a hidden argument for
    3403              :    handling seeding on coarray images.  */
    3404              : 
    3405              : static tree
    3406           90 : conv_intrinsic_random_init (gfc_code *code)
    3407              : {
    3408           90 :   stmtblock_t block;
    3409           90 :   gfc_se se;
    3410           90 :   tree arg1, arg2, tmp;
    3411              :   /* On none coarray == lib compiles use LOGICAL(4) else regular LOGICAL.  */
    3412           90 :   tree used_bool_type_node = flag_coarray == GFC_FCOARRAY_LIB
    3413           90 :                              ? logical_type_node
    3414           90 :                              : gfc_get_logical_type (4);
    3415              : 
    3416              :   /* Make the function call.  */
    3417           90 :   gfc_init_block (&block);
    3418           90 :   gfc_init_se (&se, NULL);
    3419              : 
    3420              :   /* Convert REPEATABLE to the desired LOGICAL entity.  */
    3421           90 :   gfc_conv_expr (&se, code->ext.actual->expr);
    3422           90 :   gfc_add_block_to_block (&block, &se.pre);
    3423           90 :   arg1 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
    3424           90 :   gfc_add_block_to_block (&block, &se.post);
    3425              : 
    3426              :   /* Convert IMAGE_DISTINCT to the desired LOGICAL entity.  */
    3427           90 :   gfc_conv_expr (&se, code->ext.actual->next->expr);
    3428           90 :   gfc_add_block_to_block (&block, &se.pre);
    3429           90 :   arg2 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
    3430           90 :   gfc_add_block_to_block (&block, &se.post);
    3431              : 
    3432           90 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    3433              :     {
    3434            0 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
    3435              :                                  2, arg1, arg2);
    3436              :     }
    3437              :   else
    3438              :     {
    3439              :       /* The ABI for libgfortran needs to be maintained, so a hidden
    3440              :          argument must be include if code is compiled with -fcoarray=single
    3441              :          or without the option.  Set to 0.  */
    3442           90 :       tree arg3 = build_int_cst (gfc_get_int_type (4), 0);
    3443           90 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_random_init,
    3444              :                                  3, arg1, arg2, arg3);
    3445              :     }
    3446              : 
    3447           90 :   gfc_add_expr_to_block (&block, tmp);
    3448              : 
    3449           90 :   return gfc_finish_block (&block);
    3450              : }
    3451              : 
    3452              : 
    3453              : /* Call the SYSTEM_CLOCK library functions, handling the type and kind
    3454              :    conversions.  */
    3455              : 
    3456              : static tree
    3457          196 : conv_intrinsic_system_clock (gfc_code *code)
    3458              : {
    3459          196 :   stmtblock_t block;
    3460          196 :   gfc_se count_se, count_rate_se, count_max_se;
    3461          196 :   tree arg1 = NULL_TREE, arg2 = NULL_TREE, arg3 = NULL_TREE;
    3462          196 :   tree tmp;
    3463          196 :   int least;
    3464              : 
    3465          196 :   gfc_expr *count = code->ext.actual->expr;
    3466          196 :   gfc_expr *count_rate = code->ext.actual->next->expr;
    3467          196 :   gfc_expr *count_max = code->ext.actual->next->next->expr;
    3468              : 
    3469              :   /* Evaluate our arguments.  */
    3470          196 :   if (count)
    3471              :     {
    3472          196 :       gfc_init_se (&count_se, NULL);
    3473          196 :       gfc_conv_expr (&count_se, count);
    3474              :     }
    3475              : 
    3476          196 :   if (count_rate)
    3477              :     {
    3478          181 :       gfc_init_se (&count_rate_se, NULL);
    3479          181 :       gfc_conv_expr (&count_rate_se, count_rate);
    3480              :     }
    3481              : 
    3482          196 :   if (count_max)
    3483              :     {
    3484          180 :       gfc_init_se (&count_max_se, NULL);
    3485          180 :       gfc_conv_expr (&count_max_se, count_max);
    3486              :     }
    3487              : 
    3488              :   /* Find the smallest kind found of the arguments.  */
    3489          196 :   least = 16;
    3490          196 :   least = (count && count->ts.kind < least) ? count->ts.kind : least;
    3491          196 :   least = (count_rate && count_rate->ts.kind < least) ? count_rate->ts.kind
    3492              :                                                       : least;
    3493          196 :   least = (count_max && count_max->ts.kind < least) ? count_max->ts.kind
    3494              :                                                     : least;
    3495              : 
    3496              :   /* Prepare temporary variables.  */
    3497              : 
    3498          196 :   if (count)
    3499              :     {
    3500          196 :       if (least >= 8)
    3501           18 :         arg1 = gfc_create_var (gfc_get_int_type (8), "count");
    3502          178 :       else if (least == 4)
    3503          154 :         arg1 = gfc_create_var (gfc_get_int_type (4), "count");
    3504           24 :       else if (count->ts.kind == 1)
    3505           12 :         arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[0].pedantic_min_int,
    3506              :                                      count->ts.kind);
    3507              :       else
    3508           12 :         arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[1].pedantic_min_int,
    3509              :                                      count->ts.kind);
    3510              :     }
    3511              : 
    3512          196 :   if (count_rate)
    3513              :     {
    3514          181 :       if (least >= 8)
    3515           18 :         arg2 = gfc_create_var (gfc_get_int_type (8), "count_rate");
    3516          163 :       else if (least == 4)
    3517          139 :         arg2 = gfc_create_var (gfc_get_int_type (4), "count_rate");
    3518              :       else
    3519           24 :         arg2 = integer_zero_node;
    3520              :     }
    3521              : 
    3522          196 :   if (count_max)
    3523              :     {
    3524          180 :       if (least >= 8)
    3525           18 :         arg3 = gfc_create_var (gfc_get_int_type (8), "count_max");
    3526          162 :       else if (least == 4)
    3527          138 :         arg3 = gfc_create_var (gfc_get_int_type (4), "count_max");
    3528              :       else
    3529           24 :         arg3 = integer_zero_node;
    3530              :     }
    3531              : 
    3532              :   /* Make the function call.  */
    3533          196 :   gfc_init_block (&block);
    3534              : 
    3535          196 : if (least <= 2)
    3536              :   {
    3537           24 :     if (least == 1)
    3538              :       {
    3539           12 :         arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
    3540              :                : null_pointer_node;
    3541           12 :         arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
    3542              :                : null_pointer_node;
    3543           12 :         arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
    3544              :                : null_pointer_node;
    3545              :       }
    3546              : 
    3547           24 :     if (least == 2)
    3548              :       {
    3549           12 :         arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
    3550              :                : null_pointer_node;
    3551           12 :         arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
    3552              :                : null_pointer_node;
    3553           12 :         arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
    3554              :                : null_pointer_node;
    3555              :       }
    3556              :   }
    3557              : else
    3558              :   {
    3559          172 :     if (least == 4)
    3560              :       {
    3561          585 :         tmp = build_call_expr_loc (input_location,
    3562              :                 gfor_fndecl_system_clock4, 3,
    3563          154 :                 arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
    3564              :                        : null_pointer_node,
    3565          139 :                 arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
    3566              :                        : null_pointer_node,
    3567          138 :                 arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
    3568              :                        : null_pointer_node);
    3569          154 :         gfc_add_expr_to_block (&block, tmp);
    3570              :       }
    3571              :     /* Handle kind>=8, 10, or 16 arguments */
    3572          172 :     if (least >= 8)
    3573              :       {
    3574           72 :         tmp = build_call_expr_loc (input_location,
    3575              :                 gfor_fndecl_system_clock8, 3,
    3576           18 :                 arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
    3577              :                        : null_pointer_node,
    3578           18 :                 arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
    3579              :                        : null_pointer_node,
    3580           18 :                 arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
    3581              :                        : null_pointer_node);
    3582           18 :         gfc_add_expr_to_block (&block, tmp);
    3583              :       }
    3584              :   }
    3585              : 
    3586              :   /* And store values back if needed.  */
    3587          196 :   if (arg1 && arg1 != count_se.expr)
    3588          196 :     gfc_add_modify (&block, count_se.expr,
    3589          196 :                     fold_convert (TREE_TYPE (count_se.expr), arg1));
    3590          196 :   if (arg2 && arg2 != count_rate_se.expr)
    3591          181 :     gfc_add_modify (&block, count_rate_se.expr,
    3592          181 :                     fold_convert (TREE_TYPE (count_rate_se.expr), arg2));
    3593          196 :   if (arg3 && arg3 != count_max_se.expr)
    3594          180 :     gfc_add_modify (&block, count_max_se.expr,
    3595          180 :                     fold_convert (TREE_TYPE (count_max_se.expr), arg3));
    3596              : 
    3597          196 :   return gfc_finish_block (&block);
    3598              : }
    3599              : 
    3600              : static tree
    3601          102 : conv_intrinsic_split (gfc_code *code)
    3602              : {
    3603          102 :   stmtblock_t block, post_block;
    3604          102 :   gfc_se se;
    3605          102 :   gfc_expr *string_expr, *set_expr, *pos_expr, *back_expr;
    3606          102 :   tree string, string_len;
    3607          102 :   tree set, set_len;
    3608          102 :   tree pos, pos_for_call;
    3609          102 :   tree back;
    3610          102 :   tree fndecl, call;
    3611              : 
    3612          102 :   string_expr = code->ext.actual->expr;
    3613          102 :   set_expr = code->ext.actual->next->expr;
    3614          102 :   pos_expr = code->ext.actual->next->next->expr;
    3615          102 :   back_expr = code->ext.actual->next->next->next->expr;
    3616              : 
    3617          102 :   gfc_start_block (&block);
    3618          102 :   gfc_init_block (&post_block);
    3619              : 
    3620          102 :   gfc_init_se (&se, NULL);
    3621          102 :   gfc_conv_expr (&se, string_expr);
    3622          102 :   gfc_conv_string_parameter (&se);
    3623          102 :   gfc_add_block_to_block (&block, &se.pre);
    3624          102 :   gfc_add_block_to_block (&post_block, &se.post);
    3625          102 :   string = se.expr;
    3626          102 :   string_len = se.string_length;
    3627              : 
    3628          102 :   gfc_init_se (&se, NULL);
    3629          102 :   gfc_conv_expr (&se, set_expr);
    3630          102 :   gfc_conv_string_parameter (&se);
    3631          102 :   gfc_add_block_to_block (&block, &se.pre);
    3632          102 :   gfc_add_block_to_block (&post_block, &se.post);
    3633          102 :   set = se.expr;
    3634          102 :   set_len = se.string_length;
    3635              : 
    3636          102 :   gfc_init_se (&se, NULL);
    3637          102 :   gfc_conv_expr (&se, pos_expr);
    3638          102 :   gfc_add_block_to_block (&block, &se.pre);
    3639          102 :   gfc_add_block_to_block (&post_block, &se.post);
    3640          102 :   pos = se.expr;
    3641          102 :   pos_for_call = fold_convert (gfc_charlen_type_node, pos);
    3642              : 
    3643          102 :   if (back_expr)
    3644              :     {
    3645           48 :       gfc_init_se (&se, NULL);
    3646           48 :       gfc_conv_expr (&se, back_expr);
    3647           48 :       gfc_add_block_to_block (&block, &se.pre);
    3648           48 :       gfc_add_block_to_block (&post_block, &se.post);
    3649           48 :       back = se.expr;
    3650              :     }
    3651              :   else
    3652           54 :     back = logical_false_node;
    3653              : 
    3654          102 :   if (string_expr->ts.kind == 1)
    3655           66 :     fndecl = gfor_fndecl_string_split;
    3656           36 :   else if (string_expr->ts.kind == 4)
    3657           36 :     fndecl = gfor_fndecl_string_split_char4;
    3658              :   else
    3659            0 :     gcc_unreachable ();
    3660              : 
    3661          102 :   call = build_call_expr_loc (input_location, fndecl, 6, string_len, string,
    3662              :                               set_len, set, pos_for_call, back);
    3663          102 :   gfc_add_modify (&block, pos, fold_convert (TREE_TYPE (pos), call));
    3664              : 
    3665          102 :   gfc_add_block_to_block (&block, &post_block);
    3666          102 :   return gfc_finish_block (&block);
    3667              : }
    3668              : 
    3669              : /* Return a character string containing the tty name.  */
    3670              : 
    3671              : static void
    3672            0 : gfc_conv_intrinsic_ttynam (gfc_se * se, gfc_expr * expr)
    3673              : {
    3674            0 :   tree var;
    3675            0 :   tree len;
    3676            0 :   tree tmp;
    3677            0 :   tree cond;
    3678            0 :   tree fndecl;
    3679            0 :   tree *args;
    3680            0 :   unsigned int num_args;
    3681              : 
    3682            0 :   num_args = gfc_intrinsic_argument_list_length (expr) + 2;
    3683            0 :   args = XALLOCAVEC (tree, num_args);
    3684              : 
    3685            0 :   var = gfc_create_var (pchar_type_node, "pstr");
    3686            0 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    3687              : 
    3688            0 :   gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
    3689            0 :   args[0] = gfc_build_addr_expr (NULL_TREE, var);
    3690            0 :   args[1] = gfc_build_addr_expr (NULL_TREE, len);
    3691              : 
    3692            0 :   fndecl = build_addr (gfor_fndecl_ttynam);
    3693            0 :   tmp = build_call_array_loc (input_location,
    3694            0 :                           TREE_TYPE (TREE_TYPE (gfor_fndecl_ttynam)),
    3695              :                           fndecl, num_args, args);
    3696            0 :   gfc_add_expr_to_block (&se->pre, tmp);
    3697              : 
    3698              :   /* Free the temporary afterwards, if necessary.  */
    3699            0 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3700            0 :                           len, build_int_cst (TREE_TYPE (len), 0));
    3701            0 :   tmp = gfc_call_free (var);
    3702            0 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    3703            0 :   gfc_add_expr_to_block (&se->post, tmp);
    3704              : 
    3705            0 :   se->expr = var;
    3706            0 :   se->string_length = len;
    3707            0 : }
    3708              : 
    3709              : 
    3710              : /* Get the minimum/maximum value of all the parameters.
    3711              :     minmax (a1, a2, a3, ...)
    3712              :     {
    3713              :       mvar = a1;
    3714              :       mvar = COMP (mvar, a2)
    3715              :       mvar = COMP (mvar, a3)
    3716              :       ...
    3717              :       return mvar;
    3718              :     }
    3719              :     Where COMP is MIN/MAX_EXPR for integral types or when we don't
    3720              :     care about NaNs, or IFN_FMIN/MAX when the target has support for
    3721              :     fast NaN-honouring min/max.  When neither holds expand a sequence
    3722              :     of explicit comparisons.  */
    3723              : 
    3724              : /* TODO: Mismatching types can occur when specific names are used.
    3725              :    These should be handled during resolution.  */
    3726              : static void
    3727         1367 : gfc_conv_intrinsic_minmax (gfc_se * se, gfc_expr * expr, enum tree_code op)
    3728              : {
    3729         1367 :   tree tmp;
    3730         1367 :   tree mvar;
    3731         1367 :   tree val;
    3732         1367 :   tree *args;
    3733         1367 :   tree type;
    3734         1367 :   tree argtype;
    3735         1367 :   gfc_actual_arglist *argexpr;
    3736         1367 :   unsigned int i, nargs;
    3737              : 
    3738         1367 :   nargs = gfc_intrinsic_argument_list_length (expr);
    3739         1367 :   args = XALLOCAVEC (tree, nargs);
    3740              : 
    3741         1367 :   gfc_conv_intrinsic_function_args (se, expr, args, nargs);
    3742         1367 :   type = gfc_typenode_for_spec (&expr->ts);
    3743              : 
    3744              :   /* Only evaluate the argument once.  */
    3745         1367 :   if (!VAR_P (args[0]) && !TREE_CONSTANT (args[0]))
    3746          370 :     args[0] = gfc_evaluate_now (args[0], &se->pre);
    3747              : 
    3748              :   /* Determine suitable type of temporary, as a GNU extension allows
    3749              :      different argument kinds.  */
    3750         1367 :   argtype = TREE_TYPE (args[0]);
    3751         1367 :   argexpr = expr->value.function.actual;
    3752         2953 :   for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
    3753              :     {
    3754         1586 :       tree tmptype = TREE_TYPE (args[i]);
    3755         1586 :       if (TYPE_PRECISION (tmptype) > TYPE_PRECISION (argtype))
    3756            1 :         argtype = tmptype;
    3757              :     }
    3758         1367 :   mvar = gfc_create_var (argtype, "M");
    3759         1367 :   gfc_add_modify (&se->pre, mvar, convert (argtype, args[0]));
    3760              : 
    3761         1367 :   argexpr = expr->value.function.actual;
    3762         2953 :   for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
    3763              :     {
    3764         1586 :       tree cond = NULL_TREE;
    3765         1586 :       val = args[i];
    3766              : 
    3767              :       /* Handle absent optional arguments by ignoring the comparison.  */
    3768         1586 :       if (argexpr->expr->expr_type == EXPR_VARIABLE
    3769          920 :           && argexpr->expr->symtree->n.sym->attr.optional
    3770           45 :           && INDIRECT_REF_P (val))
    3771              :         {
    3772           84 :           cond = fold_build2_loc (input_location,
    3773              :                                 NE_EXPR, logical_type_node,
    3774           42 :                                 TREE_OPERAND (val, 0),
    3775           42 :                         build_int_cst (TREE_TYPE (TREE_OPERAND (val, 0)), 0));
    3776              :         }
    3777         1544 :       else if (!VAR_P (val) && !TREE_CONSTANT (val))
    3778              :         /* Only evaluate the argument once.  */
    3779          599 :         val = gfc_evaluate_now (val, &se->pre);
    3780              : 
    3781         1586 :       tree calc;
    3782              :       /* For floating point types, the question is what MAX(a, NaN) or
    3783              :          MIN(a, NaN) should return (where "a" is a normal number).
    3784              :          There are valid use case for returning either one, but the
    3785              :          Fortran standard doesn't specify which one should be chosen.
    3786              :          Also, there is no consensus among other tested compilers.  In
    3787              :          short, it's a mess.  So lets just do whatever is fastest.  */
    3788         1586 :       tree_code code = op == GT_EXPR ? MAX_EXPR : MIN_EXPR;
    3789         1586 :       calc = fold_build2_loc (input_location, code, argtype,
    3790              :                               convert (argtype, val), mvar);
    3791         1586 :       tmp = build2_v (MODIFY_EXPR, mvar, calc);
    3792              : 
    3793         1586 :       if (cond != NULL_TREE)
    3794           42 :         tmp = build3_v (COND_EXPR, cond, tmp,
    3795              :                         build_empty_stmt (input_location));
    3796         1586 :       gfc_add_expr_to_block (&se->pre, tmp);
    3797              :     }
    3798         1367 :   se->expr = convert (type, mvar);
    3799         1367 : }
    3800              : 
    3801              : 
    3802              : /* Generate library calls for MIN and MAX intrinsics for character
    3803              :    variables.  */
    3804              : static void
    3805          282 : gfc_conv_intrinsic_minmax_char (gfc_se * se, gfc_expr * expr, int op)
    3806              : {
    3807          282 :   tree *args;
    3808          282 :   tree var, len, fndecl, tmp, cond, function;
    3809          282 :   unsigned int nargs;
    3810              : 
    3811          282 :   nargs = gfc_intrinsic_argument_list_length (expr);
    3812          282 :   args = XALLOCAVEC (tree, nargs + 4);
    3813          282 :   gfc_conv_intrinsic_function_args (se, expr, &args[4], nargs);
    3814              : 
    3815              :   /* Create the result variables.  */
    3816          282 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    3817          282 :   args[0] = gfc_build_addr_expr (NULL_TREE, len);
    3818          282 :   var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
    3819          282 :   args[1] = gfc_build_addr_expr (ppvoid_type_node, var);
    3820          282 :   args[2] = build_int_cst (integer_type_node, op);
    3821          282 :   args[3] = build_int_cst (integer_type_node, nargs / 2);
    3822              : 
    3823          282 :   if (expr->ts.kind == 1)
    3824          210 :     function = gfor_fndecl_string_minmax;
    3825           72 :   else if (expr->ts.kind == 4)
    3826           72 :     function = gfor_fndecl_string_minmax_char4;
    3827              :   else
    3828            0 :     gcc_unreachable ();
    3829              : 
    3830              :   /* Make the function call.  */
    3831          282 :   fndecl = build_addr (function);
    3832          282 :   tmp = build_call_array_loc (input_location,
    3833          282 :                           TREE_TYPE (TREE_TYPE (function)), fndecl,
    3834              :                           nargs + 4, args);
    3835          282 :   gfc_add_expr_to_block (&se->pre, tmp);
    3836              : 
    3837              :   /* Free the temporary afterwards, if necessary.  */
    3838          282 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3839          282 :                           len, build_int_cst (TREE_TYPE (len), 0));
    3840          282 :   tmp = gfc_call_free (var);
    3841          282 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    3842          282 :   gfc_add_expr_to_block (&se->post, tmp);
    3843              : 
    3844          282 :   se->expr = var;
    3845          282 :   se->string_length = len;
    3846          282 : }
    3847              : 
    3848              : 
    3849              : /* Create a symbol node for this intrinsic.  The symbol from the frontend
    3850              :    has the generic name.  */
    3851              : 
    3852              : static gfc_symbol *
    3853        11423 : gfc_get_symbol_for_expr (gfc_expr * expr, bool ignore_optional)
    3854              : {
    3855        11423 :   gfc_symbol *sym;
    3856              : 
    3857              :   /* TODO: Add symbols for intrinsic function to the global namespace.  */
    3858        11423 :   gcc_assert (strlen (expr->value.function.name) <= GFC_MAX_SYMBOL_LEN - 5);
    3859        11423 :   sym = gfc_new_symbol (expr->value.function.name, NULL);
    3860              : 
    3861        11423 :   sym->ts = expr->ts;
    3862        11423 :   if (sym->ts.type == BT_CHARACTER)
    3863         1784 :     sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    3864        11423 :   sym->attr.external = 1;
    3865        11423 :   sym->attr.function = 1;
    3866        11423 :   sym->attr.always_explicit = 1;
    3867        11423 :   sym->attr.proc = PROC_INTRINSIC;
    3868        11423 :   sym->attr.flavor = FL_PROCEDURE;
    3869        11423 :   sym->result = sym;
    3870        11423 :   if (expr->rank > 0)
    3871              :     {
    3872         9975 :       sym->attr.dimension = 1;
    3873         9975 :       sym->as = gfc_get_array_spec ();
    3874         9975 :       sym->as->type = AS_ASSUMED_SHAPE;
    3875         9975 :       sym->as->rank = expr->rank;
    3876              :     }
    3877              : 
    3878        11423 :   gfc_copy_formal_args_intr (sym, expr->value.function.isym,
    3879              :                              ignore_optional ? expr->value.function.actual
    3880              :                                              : NULL);
    3881              : 
    3882        11423 :   return sym;
    3883              : }
    3884              : 
    3885              : /* Remove empty actual arguments.  */
    3886              : 
    3887              : static void
    3888         8297 : remove_empty_actual_arguments (gfc_actual_arglist **ap)
    3889              : {
    3890        44588 :   while (*ap)
    3891              :     {
    3892        36291 :       if ((*ap)->expr == NULL)
    3893              :         {
    3894        11104 :           gfc_actual_arglist *r = *ap;
    3895        11104 :           *ap = r->next;
    3896        11104 :           r->next = NULL;
    3897        11104 :           gfc_free_actual_arglist (r);
    3898              :         }
    3899              :       else
    3900        25187 :         ap = &((*ap)->next);
    3901              :     }
    3902         8297 : }
    3903              : 
    3904              : #define MAX_SPEC_ARG 12
    3905              : 
    3906              : /* Make up an fn spec that's right for intrinsic functions that we
    3907              :    want to call.  */
    3908              : 
    3909              : static char *
    3910         1949 : intrinsic_fnspec (gfc_expr *expr)
    3911              : {
    3912         1949 :   static char fnspec_buf[MAX_SPEC_ARG*2+1];
    3913         1949 :   char *fp;
    3914         1949 :   int i;
    3915         1949 :   int num_char_args;
    3916              : 
    3917              : #define ADD_CHAR(c) do { *fp++ = c; *fp++ = ' '; } while(0)
    3918              : 
    3919              :   /* Set the fndecl.  */
    3920         1949 :   fp = fnspec_buf;
    3921              :   /* Function return value.  FIXME: Check if the second letter could
    3922              :      be something other than a space, for further optimization.  */
    3923         1949 :   ADD_CHAR ('.');
    3924         1949 :   if (expr->rank == 0)
    3925              :     {
    3926          238 :       if (expr->ts.type == BT_CHARACTER)
    3927              :         {
    3928           84 :           ADD_CHAR ('w');  /* Address of character.  */
    3929           84 :           ADD_CHAR ('.');  /* Length of character.  */
    3930              :         }
    3931              :     }
    3932              :   else
    3933         1711 :     ADD_CHAR ('w');  /* Return value is a descriptor.  */
    3934              : 
    3935         1949 :   num_char_args = 0;
    3936        10290 :   for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
    3937              :     {
    3938         8341 :       if (a->expr == NULL)
    3939         2579 :         continue;
    3940              : 
    3941         5762 :       if (a->name && strcmp (a->name,"%VAL") == 0)
    3942         1316 :         ADD_CHAR ('.');
    3943              :       else
    3944              :         {
    3945         4446 :           if (a->expr->rank > 0)
    3946         2595 :             ADD_CHAR ('r');
    3947              :           else
    3948         1851 :             ADD_CHAR ('R');
    3949              :         }
    3950         5762 :       num_char_args += a->expr->ts.type == BT_CHARACTER;
    3951         5762 :       gcc_assert (fp - fnspec_buf + num_char_args <= MAX_SPEC_ARG*2);
    3952              :     }
    3953              : 
    3954         2753 :   for (i = 0; i < num_char_args; i++)
    3955          804 :     ADD_CHAR ('.');
    3956              : 
    3957         1949 :   *fp = '\0';
    3958         1949 :   return fnspec_buf;
    3959              : }
    3960              : 
    3961              : #undef MAX_SPEC_ARG
    3962              : #undef ADD_CHAR
    3963              : 
    3964              : /* Generate the right symbol for the specific intrinsic function and
    3965              :  modify the expr accordingly.  This assumes that absent optional
    3966              :  arguments should be removed.  */
    3967              : 
    3968              : gfc_symbol *
    3969         8297 : specific_intrinsic_symbol (gfc_expr *expr)
    3970              : {
    3971         8297 :   gfc_symbol *sym;
    3972              : 
    3973         8297 :   sym = gfc_find_intrinsic_symbol (expr);
    3974         8297 :   if (sym == NULL)
    3975              :     {
    3976         1949 :       sym = gfc_get_intrinsic_function_symbol (expr);
    3977         1949 :       sym->ts = expr->ts;
    3978         1949 :       if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl)
    3979          240 :         sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
    3980              : 
    3981         1949 :       gfc_copy_formal_args_intr (sym, expr->value.function.isym,
    3982              :                                  expr->value.function.actual, true);
    3983         1949 :       sym->backend_decl
    3984         1949 :         = gfc_get_extern_function_decl (sym, expr->value.function.actual,
    3985         1949 :                                         intrinsic_fnspec (expr));
    3986              :     }
    3987              : 
    3988         8297 :   remove_empty_actual_arguments (&(expr->value.function.actual));
    3989              : 
    3990         8297 :   return sym;
    3991              : }
    3992              : 
    3993              : /* Generate a call to an external intrinsic function.  FIXME: So far,
    3994              :    this only works for functions which are called with well-defined
    3995              :    types; CSHIFT and friends will come later.  */
    3996              : 
    3997              : static void
    3998        13811 : gfc_conv_intrinsic_funcall (gfc_se * se, gfc_expr * expr)
    3999              : {
    4000        13811 :   gfc_symbol *sym;
    4001        13811 :   vec<tree, va_gc> *append_args;
    4002        13811 :   bool specific_symbol;
    4003              : 
    4004        13811 :   gcc_assert (!se->ss || se->ss->info->expr == expr);
    4005              : 
    4006        13811 :   if (se->ss)
    4007        11801 :     gcc_assert (expr->rank > 0);
    4008              :   else
    4009         2010 :     gcc_assert (expr->rank == 0);
    4010              : 
    4011        13811 :   switch (expr->value.function.isym->id)
    4012              :     {
    4013              :     case GFC_ISYM_ANY:
    4014              :     case GFC_ISYM_ALL:
    4015              :     case GFC_ISYM_FINDLOC:
    4016              :     case GFC_ISYM_MAXLOC:
    4017              :     case GFC_ISYM_MINLOC:
    4018              :     case GFC_ISYM_MAXVAL:
    4019              :     case GFC_ISYM_MINVAL:
    4020              :     case GFC_ISYM_NORM2:
    4021              :     case GFC_ISYM_PRODUCT:
    4022              :     case GFC_ISYM_SUM:
    4023              :       specific_symbol = true;
    4024              :       break;
    4025         5514 :     default:
    4026         5514 :       specific_symbol = false;
    4027              :     }
    4028              : 
    4029        13811 :   if (specific_symbol)
    4030              :     {
    4031              :       /* Need to copy here because specific_intrinsic_symbol modifies
    4032              :          expr to omit the absent optional arguments.  */
    4033         8297 :       expr = gfc_copy_expr (expr);
    4034         8297 :       sym = specific_intrinsic_symbol (expr);
    4035              :     }
    4036              :   else
    4037         5514 :     sym = gfc_get_symbol_for_expr (expr, se->ignore_optional);
    4038              : 
    4039              :   /* Calls to libgfortran_matmul need to be appended special arguments,
    4040              :      to be able to call the BLAS ?gemm functions if required and possible.  */
    4041        13811 :   append_args = NULL;
    4042        13811 :   if (expr->value.function.isym->id == GFC_ISYM_MATMUL
    4043          860 :       && !expr->external_blas
    4044          822 :       && sym->ts.type != BT_LOGICAL)
    4045              :     {
    4046          806 :       tree cint = gfc_get_int_type (gfc_c_int_kind);
    4047              : 
    4048          806 :       if (flag_external_blas
    4049            0 :           && (sym->ts.type == BT_REAL || sym->ts.type == BT_COMPLEX)
    4050            0 :           && (sym->ts.kind == 4 || sym->ts.kind == 8))
    4051              :         {
    4052            0 :           tree gemm_fndecl;
    4053              : 
    4054            0 :           if (sym->ts.type == BT_REAL)
    4055              :             {
    4056            0 :               if (sym->ts.kind == 4)
    4057            0 :                 gemm_fndecl = gfor_fndecl_sgemm;
    4058              :               else
    4059            0 :                 gemm_fndecl = gfor_fndecl_dgemm;
    4060              :             }
    4061              :           else
    4062              :             {
    4063            0 :               if (sym->ts.kind == 4)
    4064            0 :                 gemm_fndecl = gfor_fndecl_cgemm;
    4065              :               else
    4066            0 :                 gemm_fndecl = gfor_fndecl_zgemm;
    4067              :             }
    4068              : 
    4069            0 :           vec_alloc (append_args, 3);
    4070            0 :           append_args->quick_push (build_int_cst (cint, 1));
    4071            0 :           append_args->quick_push (build_int_cst (cint,
    4072            0 :                                                   flag_blas_matmul_limit));
    4073            0 :           append_args->quick_push (gfc_build_addr_expr (NULL_TREE,
    4074              :                                                         gemm_fndecl));
    4075            0 :         }
    4076              :       else
    4077              :         {
    4078          806 :           vec_alloc (append_args, 3);
    4079          806 :           append_args->quick_push (build_int_cst (cint, 0));
    4080          806 :           append_args->quick_push (build_int_cst (cint, 0));
    4081          806 :           append_args->quick_push (null_pointer_node);
    4082              :         }
    4083              :     }
    4084              :   /* Non-character scalar reduce returns a pointer to a result of size set by
    4085              :      the element size of 'array'. Setting 'sym' allocatable ensures that the
    4086              :      result is deallocated at the appropriate time.  */
    4087        13005 :   else if (expr->value.function.isym->id == GFC_ISYM_REDUCE
    4088          108 :       && expr->rank == 0 && expr->ts.type != BT_CHARACTER)
    4089          102 :     sym->attr.allocatable = 1;
    4090              : 
    4091              : 
    4092        13811 :   gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
    4093              :                           append_args);
    4094              : 
    4095        13811 :   if (specific_symbol)
    4096         8297 :     gfc_free_expr (expr);
    4097              :   else
    4098         5514 :     gfc_free_symbol (sym);
    4099        13811 : }
    4100              : 
    4101              : /* ANY and ALL intrinsics. ANY->op == NE_EXPR, ALL->op == EQ_EXPR.
    4102              :    Implemented as
    4103              :     any(a)
    4104              :     {
    4105              :       forall (i=...)
    4106              :         if (a[i] != 0)
    4107              :           return 1
    4108              :       end forall
    4109              :       return 0
    4110              :     }
    4111              :     all(a)
    4112              :     {
    4113              :       forall (i=...)
    4114              :         if (a[i] == 0)
    4115              :           return 0
    4116              :       end forall
    4117              :       return 1
    4118              :     }
    4119              :  */
    4120              : static void
    4121        39621 : gfc_conv_intrinsic_anyall (gfc_se * se, gfc_expr * expr, enum tree_code op)
    4122              : {
    4123        39621 :   tree resvar;
    4124        39621 :   stmtblock_t block;
    4125        39621 :   stmtblock_t body;
    4126        39621 :   tree type;
    4127        39621 :   tree tmp;
    4128        39621 :   tree found;
    4129        39621 :   gfc_loopinfo loop;
    4130        39621 :   gfc_actual_arglist *actual;
    4131        39621 :   gfc_ss *arrayss;
    4132        39621 :   gfc_se arrayse;
    4133        39621 :   tree exit_label;
    4134              : 
    4135        39621 :   if (se->ss)
    4136              :     {
    4137            0 :       gfc_conv_intrinsic_funcall (se, expr);
    4138            0 :       return;
    4139              :     }
    4140              : 
    4141        39621 :   actual = expr->value.function.actual;
    4142        39621 :   type = gfc_typenode_for_spec (&expr->ts);
    4143              :   /* Initialize the result.  */
    4144        39621 :   resvar = gfc_create_var (type, "test");
    4145        39621 :   if (op == EQ_EXPR)
    4146          432 :     tmp = convert (type, boolean_true_node);
    4147              :   else
    4148        39189 :     tmp = convert (type, boolean_false_node);
    4149        39621 :   gfc_add_modify (&se->pre, resvar, tmp);
    4150              : 
    4151              :   /* Walk the arguments.  */
    4152        39621 :   arrayss = gfc_walk_expr (actual->expr);
    4153        39621 :   gcc_assert (arrayss != gfc_ss_terminator);
    4154              : 
    4155              :   /* Initialize the scalarizer.  */
    4156        39621 :   gfc_init_loopinfo (&loop);
    4157        39621 :   exit_label = gfc_build_label_decl (NULL_TREE);
    4158        39621 :   TREE_USED (exit_label) = 1;
    4159        39621 :   gfc_add_ss_to_loop (&loop, arrayss);
    4160              : 
    4161              :   /* Initialize the loop.  */
    4162        39621 :   gfc_conv_ss_startstride (&loop);
    4163        39621 :   gfc_conv_loop_setup (&loop, &expr->where);
    4164              : 
    4165        39621 :   gfc_mark_ss_chain_used (arrayss, 1);
    4166              :   /* Generate the loop body.  */
    4167        39621 :   gfc_start_scalarized_body (&loop, &body);
    4168              : 
    4169              :   /* If the condition matches then set the return value.  */
    4170        39621 :   gfc_start_block (&block);
    4171        39621 :   if (op == EQ_EXPR)
    4172          432 :     tmp = convert (type, boolean_false_node);
    4173              :   else
    4174        39189 :     tmp = convert (type, boolean_true_node);
    4175        39621 :   gfc_add_modify (&block, resvar, tmp);
    4176              : 
    4177              :   /* And break out of the loop.  */
    4178        39621 :   tmp = build1_v (GOTO_EXPR, exit_label);
    4179        39621 :   gfc_add_expr_to_block (&block, tmp);
    4180              : 
    4181        39621 :   found = gfc_finish_block (&block);
    4182              : 
    4183              :   /* Check this element.  */
    4184        39621 :   gfc_init_se (&arrayse, NULL);
    4185        39621 :   gfc_copy_loopinfo_to_se (&arrayse, &loop);
    4186        39621 :   arrayse.ss = arrayss;
    4187        39621 :   gfc_conv_expr_val (&arrayse, actual->expr);
    4188              : 
    4189        39621 :   gfc_add_block_to_block (&body, &arrayse.pre);
    4190        39621 :   tmp = fold_build2_loc (input_location, op, logical_type_node, arrayse.expr,
    4191        39621 :                          build_int_cst (TREE_TYPE (arrayse.expr), 0));
    4192        39621 :   tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
    4193        39621 :   gfc_add_expr_to_block (&body, tmp);
    4194        39621 :   gfc_add_block_to_block (&body, &arrayse.post);
    4195              : 
    4196        39621 :   gfc_trans_scalarizing_loops (&loop, &body);
    4197              : 
    4198              :   /* Add the exit label.  */
    4199        39621 :   tmp = build1_v (LABEL_EXPR, exit_label);
    4200        39621 :   gfc_add_expr_to_block (&loop.pre, tmp);
    4201              : 
    4202        39621 :   gfc_add_block_to_block (&se->pre, &loop.pre);
    4203        39621 :   gfc_add_block_to_block (&se->pre, &loop.post);
    4204        39621 :   gfc_cleanup_loop (&loop);
    4205              : 
    4206        39621 :   se->expr = resvar;
    4207              : }
    4208              : 
    4209              : 
    4210              : /* Generate the constant 180 / pi, which is used in the conversion
    4211              :    of acosd(), asind(), atand(), atan2d().  */
    4212              : 
    4213              : static tree
    4214          408 : rad2deg (int kind)
    4215              : {
    4216          408 :   tree retval;
    4217          408 :   mpfr_t pi, t0;
    4218              : 
    4219          408 :   gfc_set_model_kind (kind);
    4220          408 :   mpfr_init (pi);
    4221          408 :   mpfr_init (t0);
    4222          408 :   mpfr_set_si (t0, 180, GFC_RND_MODE);
    4223          408 :   mpfr_const_pi (pi, GFC_RND_MODE);
    4224          408 :   mpfr_div (t0, t0, pi, GFC_RND_MODE);
    4225          408 :   retval = gfc_conv_mpfr_to_tree (t0, kind, 0);
    4226          408 :   mpfr_clear (t0);
    4227          408 :   mpfr_clear (pi);
    4228          408 :   return retval;
    4229              : }
    4230              : 
    4231              : 
    4232              : static gfc_intrinsic_map_t *
    4233          618 : gfc_lookup_intrinsic (gfc_isym_id id)
    4234              : {
    4235          618 :   gfc_intrinsic_map_t *m = gfc_intrinsic_map;
    4236        11514 :   for (; m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
    4237        11514 :     if (id == m->id)
    4238              :       break;
    4239          618 :   gcc_assert (id == m->id);
    4240          618 :   return m;
    4241              : }
    4242              : 
    4243              : 
    4244              : /* ACOSD(x) is translated into ACOS(x) * 180 / pi.
    4245              :    ASIND(x) is translated into ASIN(x) * 180 / pi.
    4246              :    ATAND(x) is translated into ATAN(x) * 180 / pi.  */
    4247              : 
    4248              : static void
    4249          270 : gfc_conv_intrinsic_atrigd (gfc_se * se, gfc_expr * expr, gfc_isym_id id)
    4250              : {
    4251          270 :   tree arg;
    4252          270 :   tree atrigd;
    4253          270 :   tree type;
    4254          270 :   gfc_intrinsic_map_t *m;
    4255              : 
    4256          270 :   type = gfc_typenode_for_spec (&expr->ts);
    4257              : 
    4258          270 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    4259              : 
    4260          270 :   switch (id)
    4261              :     {
    4262           90 :     case GFC_ISYM_ACOSD:
    4263           90 :       m = gfc_lookup_intrinsic (GFC_ISYM_ACOS);
    4264           90 :       break;
    4265           90 :     case GFC_ISYM_ASIND:
    4266           90 :       m = gfc_lookup_intrinsic (GFC_ISYM_ASIN);
    4267           90 :       break;
    4268           90 :     case GFC_ISYM_ATAND:
    4269           90 :       m = gfc_lookup_intrinsic (GFC_ISYM_ATAN);
    4270           90 :       break;
    4271            0 :     default:
    4272            0 :       gcc_unreachable ();
    4273              :     }
    4274          270 :   atrigd = gfc_get_intrinsic_lib_fndecl (m, expr);
    4275          270 :   atrigd = build_call_expr_loc (input_location, atrigd, 1, arg);
    4276              : 
    4277          270 :   se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atrigd,
    4278              :                               fold_convert (type, rad2deg (expr->ts.kind)));
    4279          270 : }
    4280              : 
    4281              : 
    4282              : /* COTAN(X) is translated into -TAN(X+PI/2) for REAL argument and
    4283              :    COS(X) / SIN(X) for COMPLEX argument.  */
    4284              : 
    4285              : static void
    4286          102 : gfc_conv_intrinsic_cotan (gfc_se *se, gfc_expr *expr)
    4287              : {
    4288          102 :   gfc_intrinsic_map_t *m;
    4289          102 :   tree arg;
    4290          102 :   tree type;
    4291              : 
    4292          102 :   type = gfc_typenode_for_spec (&expr->ts);
    4293          102 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    4294              : 
    4295          102 :   if (expr->ts.type == BT_REAL)
    4296              :     {
    4297          102 :       tree tan;
    4298          102 :       tree tmp;
    4299          102 :       mpfr_t pio2;
    4300              : 
    4301              :       /* Create pi/2.  */
    4302          102 :       gfc_set_model_kind (expr->ts.kind);
    4303          102 :       mpfr_init (pio2);
    4304          102 :       mpfr_const_pi (pio2, GFC_RND_MODE);
    4305          102 :       mpfr_div_ui (pio2, pio2, 2, GFC_RND_MODE);
    4306          102 :       tmp = gfc_conv_mpfr_to_tree (pio2, expr->ts.kind, 0);
    4307          102 :       mpfr_clear (pio2);
    4308              : 
    4309              :       /* Find tan builtin function.  */
    4310          102 :       m = gfc_lookup_intrinsic (GFC_ISYM_TAN);
    4311          102 :       tan = gfc_get_intrinsic_lib_fndecl (m, expr);
    4312          102 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, type, arg, tmp);
    4313          102 :       tan = build_call_expr_loc (input_location, tan, 1, tmp);
    4314          102 :       se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tan);
    4315              :     }
    4316              :   else
    4317              :     {
    4318            0 :       tree sin;
    4319            0 :       tree cos;
    4320              : 
    4321              :       /* Find cos builtin function.  */
    4322            0 :       m = gfc_lookup_intrinsic (GFC_ISYM_COS);
    4323            0 :       cos = gfc_get_intrinsic_lib_fndecl (m, expr);
    4324            0 :       cos = build_call_expr_loc (input_location, cos, 1, arg);
    4325              : 
    4326              :       /* Find sin builtin function.  */
    4327            0 :       m = gfc_lookup_intrinsic (GFC_ISYM_SIN);
    4328            0 :       sin = gfc_get_intrinsic_lib_fndecl (m, expr);
    4329            0 :       sin = build_call_expr_loc (input_location, sin, 1, arg);
    4330              : 
    4331              :       /* Divide cos by sin. */
    4332            0 :       se->expr = fold_build2_loc (input_location, RDIV_EXPR, type, cos, sin);
    4333              :    }
    4334          102 : }
    4335              : 
    4336              : 
    4337              : /* COTAND(X) is translated into -TAND(X+90) for REAL argument.  */
    4338              : 
    4339              : static void
    4340          108 : gfc_conv_intrinsic_cotand (gfc_se *se, gfc_expr *expr)
    4341              : {
    4342          108 :   tree arg;
    4343          108 :   tree type;
    4344          108 :   tree ninety_tree;
    4345          108 :   mpfr_t ninety;
    4346              : 
    4347          108 :   type = gfc_typenode_for_spec (&expr->ts);
    4348          108 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    4349              : 
    4350          108 :   gfc_set_model_kind (expr->ts.kind);
    4351              : 
    4352              :   /* Build the tree for x + 90.  */
    4353          108 :   mpfr_init_set_ui (ninety, 90, GFC_RND_MODE);
    4354          108 :   ninety_tree = gfc_conv_mpfr_to_tree (ninety, expr->ts.kind, 0);
    4355          108 :   arg = fold_build2_loc (input_location, PLUS_EXPR, type, arg, ninety_tree);
    4356          108 :   mpfr_clear (ninety);
    4357              : 
    4358              :   /* Find tand.  */
    4359          108 :   gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_TAND);
    4360          108 :   tree tand = gfc_get_intrinsic_lib_fndecl (m, expr);
    4361          108 :   tand = build_call_expr_loc (input_location, tand, 1, arg);
    4362              : 
    4363          108 :   se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tand);
    4364          108 : }
    4365              : 
    4366              : 
    4367              : /* ATAN2D(Y,X) is translated into ATAN2(Y,X) * 180 / PI. */
    4368              : 
    4369              : static void
    4370          138 : gfc_conv_intrinsic_atan2d (gfc_se *se, gfc_expr *expr)
    4371              : {
    4372          138 :   tree args[2];
    4373          138 :   tree atan2d;
    4374          138 :   tree type;
    4375              : 
    4376          138 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    4377          138 :   type = TREE_TYPE (args[0]);
    4378              : 
    4379          138 :   gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_ATAN2);
    4380          138 :   atan2d = gfc_get_intrinsic_lib_fndecl (m, expr);
    4381          138 :   atan2d = build_call_expr_loc (input_location, atan2d, 2, args[0], args[1]);
    4382              : 
    4383          138 :   se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atan2d,
    4384              :                               rad2deg (expr->ts.kind));
    4385          138 : }
    4386              : 
    4387              : 
    4388              : /* COUNT(A) = Number of true elements in A.  */
    4389              : static void
    4390          155 : gfc_conv_intrinsic_count (gfc_se * se, gfc_expr * expr)
    4391              : {
    4392          155 :   tree resvar;
    4393          155 :   tree type;
    4394          155 :   stmtblock_t body;
    4395          155 :   tree tmp;
    4396          155 :   gfc_loopinfo loop;
    4397          155 :   gfc_actual_arglist *actual;
    4398          155 :   gfc_ss *arrayss;
    4399          155 :   gfc_se arrayse;
    4400              : 
    4401          155 :   if (se->ss)
    4402              :     {
    4403            0 :       gfc_conv_intrinsic_funcall (se, expr);
    4404            0 :       return;
    4405              :     }
    4406              : 
    4407          155 :   actual = expr->value.function.actual;
    4408              : 
    4409          155 :   type = gfc_typenode_for_spec (&expr->ts);
    4410              :   /* Initialize the result.  */
    4411          155 :   resvar = gfc_create_var (type, "count");
    4412          155 :   gfc_add_modify (&se->pre, resvar, build_int_cst (type, 0));
    4413              : 
    4414              :   /* Walk the arguments.  */
    4415          155 :   arrayss = gfc_walk_expr (actual->expr);
    4416          155 :   gcc_assert (arrayss != gfc_ss_terminator);
    4417              : 
    4418              :   /* Initialize the scalarizer.  */
    4419          155 :   gfc_init_loopinfo (&loop);
    4420          155 :   gfc_add_ss_to_loop (&loop, arrayss);
    4421              : 
    4422              :   /* Initialize the loop.  */
    4423          155 :   gfc_conv_ss_startstride (&loop);
    4424          155 :   gfc_conv_loop_setup (&loop, &expr->where);
    4425              : 
    4426          155 :   gfc_mark_ss_chain_used (arrayss, 1);
    4427              :   /* Generate the loop body.  */
    4428          155 :   gfc_start_scalarized_body (&loop, &body);
    4429              : 
    4430          155 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (resvar),
    4431          155 :                          resvar, build_int_cst (TREE_TYPE (resvar), 1));
    4432          155 :   tmp = build2_v (MODIFY_EXPR, resvar, tmp);
    4433              : 
    4434          155 :   gfc_init_se (&arrayse, NULL);
    4435          155 :   gfc_copy_loopinfo_to_se (&arrayse, &loop);
    4436          155 :   arrayse.ss = arrayss;
    4437          155 :   gfc_conv_expr_val (&arrayse, actual->expr);
    4438          155 :   tmp = build3_v (COND_EXPR, arrayse.expr, tmp,
    4439              :                   build_empty_stmt (input_location));
    4440              : 
    4441          155 :   gfc_add_block_to_block (&body, &arrayse.pre);
    4442          155 :   gfc_add_expr_to_block (&body, tmp);
    4443          155 :   gfc_add_block_to_block (&body, &arrayse.post);
    4444              : 
    4445          155 :   gfc_trans_scalarizing_loops (&loop, &body);
    4446              : 
    4447          155 :   gfc_add_block_to_block (&se->pre, &loop.pre);
    4448          155 :   gfc_add_block_to_block (&se->pre, &loop.post);
    4449          155 :   gfc_cleanup_loop (&loop);
    4450              : 
    4451          155 :   se->expr = resvar;
    4452              : }
    4453              : 
    4454              : 
    4455              : /* Update given gfc_se to have ss component pointing to the nested gfc_ss
    4456              :    struct and return the corresponding loopinfo.  */
    4457              : 
    4458              : static gfc_loopinfo *
    4459         3374 : enter_nested_loop (gfc_se *se)
    4460              : {
    4461         3374 :   se->ss = se->ss->nested_ss;
    4462         3374 :   gcc_assert (se->ss == se->ss->loop->ss);
    4463              : 
    4464         3374 :   return se->ss->loop;
    4465              : }
    4466              : 
    4467              : /* Build the condition for a mask, which may be optional.  */
    4468              : 
    4469              : static tree
    4470        12795 : conv_mask_condition (gfc_se *maskse, gfc_expr *maskexpr,
    4471              :                          bool optional_mask)
    4472              : {
    4473        12795 :   tree present;
    4474        12795 :   tree type;
    4475              : 
    4476        12795 :   if (optional_mask)
    4477              :     {
    4478          206 :       type = TREE_TYPE (maskse->expr);
    4479          206 :       present = gfc_conv_expr_present (maskexpr->symtree->n.sym);
    4480          206 :       present = convert (type, present);
    4481          206 :       present = fold_build1_loc (input_location, TRUTH_NOT_EXPR, type,
    4482              :                                  present);
    4483          206 :       return fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    4484          206 :                               type, present, maskse->expr);
    4485              :     }
    4486              :   else
    4487        12589 :     return maskse->expr;
    4488              : }
    4489              : 
    4490              : /* Inline implementation of the sum and product intrinsics.  */
    4491              : static void
    4492         2791 : gfc_conv_intrinsic_arith (gfc_se * se, gfc_expr * expr, enum tree_code op,
    4493              :                           bool norm2)
    4494              : {
    4495         2791 :   tree resvar;
    4496         2791 :   tree scale = NULL_TREE;
    4497         2791 :   tree type;
    4498         2791 :   stmtblock_t body;
    4499         2791 :   stmtblock_t block;
    4500         2791 :   tree tmp;
    4501         2791 :   gfc_loopinfo loop, *ploop;
    4502         2791 :   gfc_actual_arglist *arg_array, *arg_mask;
    4503         2791 :   gfc_ss *arrayss = NULL;
    4504         2791 :   gfc_ss *maskss = NULL;
    4505         2791 :   gfc_se arrayse;
    4506         2791 :   gfc_se maskse;
    4507         2791 :   gfc_se *parent_se;
    4508         2791 :   gfc_expr *arrayexpr;
    4509         2791 :   gfc_expr *maskexpr;
    4510         2791 :   bool optional_mask;
    4511              : 
    4512         2791 :   if (expr->rank > 0)
    4513              :     {
    4514          578 :       gcc_assert (gfc_inline_intrinsic_function_p (expr));
    4515              :       parent_se = se;
    4516              :     }
    4517              :   else
    4518              :     parent_se = NULL;
    4519              : 
    4520         2791 :   type = gfc_typenode_for_spec (&expr->ts);
    4521              :   /* Initialize the result.  */
    4522         2791 :   resvar = gfc_create_var (type, "val");
    4523         2791 :   if (norm2)
    4524              :     {
    4525              :       /* result = 0.0;
    4526              :          scale = 1.0.  */
    4527           68 :       scale = gfc_create_var (type, "scale");
    4528           68 :       gfc_add_modify (&se->pre, scale,
    4529              :                       gfc_build_const (type, integer_one_node));
    4530           68 :       tmp = gfc_build_const (type, integer_zero_node);
    4531              :     }
    4532         2723 :   else if (op == PLUS_EXPR || op == BIT_IOR_EXPR || op == BIT_XOR_EXPR)
    4533         2305 :     tmp = gfc_build_const (type, integer_zero_node);
    4534          418 :   else if (op == NE_EXPR)
    4535              :     /* PARITY.  */
    4536           36 :     tmp = convert (type, boolean_false_node);
    4537          382 :   else if (op == BIT_AND_EXPR)
    4538           24 :     tmp = gfc_build_const (type, fold_build1_loc (input_location, NEGATE_EXPR,
    4539              :                                                   type, integer_one_node));
    4540              :   else
    4541          358 :     tmp = gfc_build_const (type, integer_one_node);
    4542              : 
    4543         2791 :   gfc_add_modify (&se->pre, resvar, tmp);
    4544              : 
    4545         2791 :   arg_array = expr->value.function.actual;
    4546              : 
    4547         2791 :   arrayexpr = arg_array->expr;
    4548              : 
    4549         2791 :   if (op == NE_EXPR || norm2)
    4550              :     {
    4551              :       /* PARITY and NORM2.  */
    4552              :       maskexpr = NULL;
    4553              :       optional_mask = false;
    4554              :     }
    4555              :   else
    4556              :     {
    4557         2687 :       arg_mask  = arg_array->next->next;
    4558         2687 :       gcc_assert (arg_mask != NULL);
    4559         2687 :       maskexpr = arg_mask->expr;
    4560          371 :       optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
    4561          266 :         && maskexpr->symtree->n.sym->attr.dummy
    4562         2705 :         && maskexpr->symtree->n.sym->attr.optional;
    4563              :     }
    4564              : 
    4565         2791 :   if (expr->rank == 0)
    4566              :     {
    4567              :       /* Walk the arguments.  */
    4568         2213 :       arrayss = gfc_walk_expr (arrayexpr);
    4569         2213 :       gcc_assert (arrayss != gfc_ss_terminator);
    4570              : 
    4571         2213 :       if (maskexpr && maskexpr->rank > 0)
    4572              :         {
    4573          223 :           maskss = gfc_walk_expr (maskexpr);
    4574          223 :           gcc_assert (maskss != gfc_ss_terminator);
    4575              :         }
    4576              :       else
    4577              :         maskss = NULL;
    4578              : 
    4579              :       /* Initialize the scalarizer.  */
    4580         2213 :       gfc_init_loopinfo (&loop);
    4581              : 
    4582              :       /* We add the mask first because the number of iterations is
    4583              :          taken from the last ss, and this breaks if an absent
    4584              :          optional argument is used for mask.  */
    4585              : 
    4586         2213 :       if (maskexpr && maskexpr->rank > 0)
    4587          223 :         gfc_add_ss_to_loop (&loop, maskss);
    4588         2213 :       gfc_add_ss_to_loop (&loop, arrayss);
    4589              : 
    4590              :       /* Initialize the loop.  */
    4591         2213 :       gfc_conv_ss_startstride (&loop);
    4592         2213 :       gfc_conv_loop_setup (&loop, &expr->where);
    4593              : 
    4594         2213 :       if (maskexpr && maskexpr->rank > 0)
    4595          223 :         gfc_mark_ss_chain_used (maskss, 1);
    4596         2213 :       gfc_mark_ss_chain_used (arrayss, 1);
    4597              : 
    4598         2213 :       ploop = &loop;
    4599              :     }
    4600              :   else
    4601              :     /* All the work has been done in the parent loops.  */
    4602          578 :     ploop = enter_nested_loop (se);
    4603              : 
    4604         2791 :   gcc_assert (ploop);
    4605              : 
    4606              :   /* Generate the loop body.  */
    4607         2791 :   gfc_start_scalarized_body (ploop, &body);
    4608              : 
    4609              :   /* If we have a mask, only add this element if the mask is set.  */
    4610         2791 :   if (maskexpr && maskexpr->rank > 0)
    4611              :     {
    4612          307 :       gfc_init_se (&maskse, parent_se);
    4613          307 :       gfc_copy_loopinfo_to_se (&maskse, ploop);
    4614          307 :       if (expr->rank == 0)
    4615          223 :         maskse.ss = maskss;
    4616          307 :       gfc_conv_expr_val (&maskse, maskexpr);
    4617          307 :       gfc_add_block_to_block (&body, &maskse.pre);
    4618              : 
    4619          307 :       gfc_start_block (&block);
    4620              :     }
    4621              :   else
    4622         2484 :     gfc_init_block (&block);
    4623              : 
    4624              :   /* Do the actual summation/product.  */
    4625         2791 :   gfc_init_se (&arrayse, parent_se);
    4626         2791 :   gfc_copy_loopinfo_to_se (&arrayse, ploop);
    4627         2791 :   if (expr->rank == 0)
    4628         2213 :     arrayse.ss = arrayss;
    4629         2791 :   gfc_conv_expr_val (&arrayse, arrayexpr);
    4630         2791 :   gfc_add_block_to_block (&block, &arrayse.pre);
    4631              : 
    4632         2791 :   if (norm2)
    4633              :     {
    4634              :       /* if (x (i) != 0.0)
    4635              :            {
    4636              :              absX = abs(x(i))
    4637              :              if (absX > scale)
    4638              :                {
    4639              :                  val = scale/absX;
    4640              :                  result = 1.0 + result * val * val;
    4641              :                  scale = absX;
    4642              :                }
    4643              :              else
    4644              :                {
    4645              :                  val = absX/scale;
    4646              :                  result += val * val;
    4647              :                }
    4648              :            }  */
    4649           68 :       tree res1, res2, cond, absX, val;
    4650           68 :       stmtblock_t ifblock1, ifblock2, ifblock3;
    4651              : 
    4652           68 :       gfc_init_block (&ifblock1);
    4653              : 
    4654           68 :       absX = gfc_create_var (type, "absX");
    4655           68 :       gfc_add_modify (&ifblock1, absX,
    4656              :                       fold_build1_loc (input_location, ABS_EXPR, type,
    4657              :                                        arrayse.expr));
    4658           68 :       val = gfc_create_var (type, "val");
    4659           68 :       gfc_add_expr_to_block (&ifblock1, val);
    4660              : 
    4661           68 :       gfc_init_block (&ifblock2);
    4662           68 :       gfc_add_modify (&ifblock2, val,
    4663              :                       fold_build2_loc (input_location, RDIV_EXPR, type, scale,
    4664              :                                        absX));
    4665           68 :       res1 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
    4666           68 :       res1 = fold_build2_loc (input_location, MULT_EXPR, type, resvar, res1);
    4667           68 :       res1 = fold_build2_loc (input_location, PLUS_EXPR, type, res1,
    4668              :                               gfc_build_const (type, integer_one_node));
    4669           68 :       gfc_add_modify (&ifblock2, resvar, res1);
    4670           68 :       gfc_add_modify (&ifblock2, scale, absX);
    4671           68 :       res1 = gfc_finish_block (&ifblock2);
    4672              : 
    4673           68 :       gfc_init_block (&ifblock3);
    4674           68 :       gfc_add_modify (&ifblock3, val,
    4675              :                       fold_build2_loc (input_location, RDIV_EXPR, type, absX,
    4676              :                                        scale));
    4677           68 :       res2 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
    4678           68 :       res2 = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, res2);
    4679           68 :       gfc_add_modify (&ifblock3, resvar, res2);
    4680           68 :       res2 = gfc_finish_block (&ifblock3);
    4681              : 
    4682           68 :       cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    4683              :                               absX, scale);
    4684           68 :       tmp = build3_v (COND_EXPR, cond, res1, res2);
    4685           68 :       gfc_add_expr_to_block (&ifblock1, tmp);
    4686           68 :       tmp = gfc_finish_block (&ifblock1);
    4687              : 
    4688           68 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    4689              :                               arrayse.expr,
    4690              :                               gfc_build_const (type, integer_zero_node));
    4691              : 
    4692           68 :       tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    4693           68 :       gfc_add_expr_to_block (&block, tmp);
    4694              :     }
    4695              :   else
    4696              :     {
    4697         2723 :       tmp = fold_build2_loc (input_location, op, type, resvar, arrayse.expr);
    4698         2723 :       gfc_add_modify (&block, resvar, tmp);
    4699              :     }
    4700              : 
    4701         2791 :   gfc_add_block_to_block (&block, &arrayse.post);
    4702              : 
    4703         2791 :   if (maskexpr && maskexpr->rank > 0)
    4704              :     {
    4705              :       /* We enclose the above in if (mask) {...} .  If the mask is an
    4706              :          optional argument, generate
    4707              :          IF (.NOT. PRESENT(MASK) .OR. MASK(I)).  */
    4708          307 :       tree ifmask;
    4709          307 :       tmp = gfc_finish_block (&block);
    4710          307 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    4711          307 :       tmp = build3_v (COND_EXPR, ifmask, tmp,
    4712              :                       build_empty_stmt (input_location));
    4713          307 :     }
    4714              :   else
    4715         2484 :     tmp = gfc_finish_block (&block);
    4716         2791 :   gfc_add_expr_to_block (&body, tmp);
    4717              : 
    4718         2791 :   gfc_trans_scalarizing_loops (ploop, &body);
    4719              : 
    4720              :   /* For a scalar mask, enclose the loop in an if statement.  */
    4721         2791 :   if (maskexpr && maskexpr->rank == 0)
    4722              :     {
    4723           64 :       gfc_init_block (&block);
    4724           64 :       gfc_add_block_to_block (&block, &ploop->pre);
    4725           64 :       gfc_add_block_to_block (&block, &ploop->post);
    4726           64 :       tmp = gfc_finish_block (&block);
    4727              : 
    4728           64 :       if (expr->rank > 0)
    4729              :         {
    4730           34 :           tmp = build3_v (COND_EXPR, se->ss->info->data.scalar.value, tmp,
    4731              :                           build_empty_stmt (input_location));
    4732           34 :           gfc_advance_se_ss_chain (se);
    4733              :         }
    4734              :       else
    4735              :         {
    4736           30 :           tree ifmask;
    4737              : 
    4738           30 :           gcc_assert (expr->rank == 0);
    4739           30 :           gfc_init_se (&maskse, NULL);
    4740           30 :           gfc_conv_expr_val (&maskse, maskexpr);
    4741           30 :           ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    4742           30 :           tmp = build3_v (COND_EXPR, ifmask, tmp,
    4743              :                           build_empty_stmt (input_location));
    4744              :         }
    4745              : 
    4746           64 :       gfc_add_expr_to_block (&block, tmp);
    4747           64 :       gfc_add_block_to_block (&se->pre, &block);
    4748           64 :       gcc_assert (se->post.head == NULL);
    4749              :     }
    4750              :   else
    4751              :     {
    4752         2727 :       gfc_add_block_to_block (&se->pre, &ploop->pre);
    4753         2727 :       gfc_add_block_to_block (&se->pre, &ploop->post);
    4754              :     }
    4755              : 
    4756         2791 :   if (expr->rank == 0)
    4757         2213 :     gfc_cleanup_loop (ploop);
    4758              : 
    4759         2791 :   if (norm2)
    4760              :     {
    4761              :       /* result = scale * sqrt(result).  */
    4762           68 :       tree sqrt;
    4763           68 :       sqrt = gfc_builtin_decl_for_float_kind (BUILT_IN_SQRT, expr->ts.kind);
    4764           68 :       resvar = build_call_expr_loc (input_location,
    4765              :                                     sqrt, 1, resvar);
    4766           68 :       resvar = fold_build2_loc (input_location, MULT_EXPR, type, scale, resvar);
    4767              :     }
    4768              : 
    4769         2791 :   se->expr = resvar;
    4770         2791 : }
    4771              : 
    4772              : 
    4773              : /* Inline implementation of the dot_product intrinsic. This function
    4774              :    is based on gfc_conv_intrinsic_arith (the previous function).  */
    4775              : static void
    4776          113 : gfc_conv_intrinsic_dot_product (gfc_se * se, gfc_expr * expr)
    4777              : {
    4778          113 :   tree resvar;
    4779          113 :   tree type;
    4780          113 :   stmtblock_t body;
    4781          113 :   stmtblock_t block;
    4782          113 :   tree tmp;
    4783          113 :   gfc_loopinfo loop;
    4784          113 :   gfc_actual_arglist *actual;
    4785          113 :   gfc_ss *arrayss1, *arrayss2;
    4786          113 :   gfc_se arrayse1, arrayse2;
    4787          113 :   gfc_expr *arrayexpr1, *arrayexpr2;
    4788              : 
    4789          113 :   type = gfc_typenode_for_spec (&expr->ts);
    4790              : 
    4791              :   /* Initialize the result.  */
    4792          113 :   resvar = gfc_create_var (type, "val");
    4793          113 :   if (expr->ts.type == BT_LOGICAL)
    4794           30 :     tmp = build_int_cst (type, 0);
    4795              :   else
    4796           83 :     tmp = gfc_build_const (type, integer_zero_node);
    4797              : 
    4798          113 :   gfc_add_modify (&se->pre, resvar, tmp);
    4799              : 
    4800              :   /* Walk argument #1.  */
    4801          113 :   actual = expr->value.function.actual;
    4802          113 :   arrayexpr1 = actual->expr;
    4803          113 :   arrayss1 = gfc_walk_expr (arrayexpr1);
    4804          113 :   gcc_assert (arrayss1 != gfc_ss_terminator);
    4805              : 
    4806              :   /* Walk argument #2.  */
    4807          113 :   actual = actual->next;
    4808          113 :   arrayexpr2 = actual->expr;
    4809          113 :   arrayss2 = gfc_walk_expr (arrayexpr2);
    4810          113 :   gcc_assert (arrayss2 != gfc_ss_terminator);
    4811              : 
    4812              :   /* Initialize the scalarizer.  */
    4813          113 :   gfc_init_loopinfo (&loop);
    4814          113 :   gfc_add_ss_to_loop (&loop, arrayss1);
    4815          113 :   gfc_add_ss_to_loop (&loop, arrayss2);
    4816              : 
    4817              :   /* Initialize the loop.  */
    4818          113 :   gfc_conv_ss_startstride (&loop);
    4819          113 :   gfc_conv_loop_setup (&loop, &expr->where);
    4820              : 
    4821          113 :   gfc_mark_ss_chain_used (arrayss1, 1);
    4822          113 :   gfc_mark_ss_chain_used (arrayss2, 1);
    4823              : 
    4824              :   /* Generate the loop body.  */
    4825          113 :   gfc_start_scalarized_body (&loop, &body);
    4826          113 :   gfc_init_block (&block);
    4827              : 
    4828              :   /* Make the tree expression for [conjg(]array1[)].  */
    4829          113 :   gfc_init_se (&arrayse1, NULL);
    4830          113 :   gfc_copy_loopinfo_to_se (&arrayse1, &loop);
    4831          113 :   arrayse1.ss = arrayss1;
    4832          113 :   gfc_conv_expr_val (&arrayse1, arrayexpr1);
    4833          113 :   if (expr->ts.type == BT_COMPLEX)
    4834            9 :     arrayse1.expr = fold_build1_loc (input_location, CONJ_EXPR, type,
    4835              :                                      arrayse1.expr);
    4836          113 :   gfc_add_block_to_block (&block, &arrayse1.pre);
    4837              : 
    4838              :   /* Make the tree expression for array2.  */
    4839          113 :   gfc_init_se (&arrayse2, NULL);
    4840          113 :   gfc_copy_loopinfo_to_se (&arrayse2, &loop);
    4841          113 :   arrayse2.ss = arrayss2;
    4842          113 :   gfc_conv_expr_val (&arrayse2, arrayexpr2);
    4843          113 :   gfc_add_block_to_block (&block, &arrayse2.pre);
    4844              : 
    4845              :   /* Do the actual product and sum.  */
    4846          113 :   if (expr->ts.type == BT_LOGICAL)
    4847              :     {
    4848           30 :       tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, type,
    4849              :                              arrayse1.expr, arrayse2.expr);
    4850           30 :       tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, type, resvar, tmp);
    4851              :     }
    4852              :   else
    4853              :     {
    4854           83 :       tmp = fold_build2_loc (input_location, MULT_EXPR, type, arrayse1.expr,
    4855              :                              arrayse2.expr);
    4856           83 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, tmp);
    4857              :     }
    4858          113 :   gfc_add_modify (&block, resvar, tmp);
    4859              : 
    4860              :   /* Finish up the loop block and the loop.  */
    4861          113 :   tmp = gfc_finish_block (&block);
    4862          113 :   gfc_add_expr_to_block (&body, tmp);
    4863              : 
    4864          113 :   gfc_trans_scalarizing_loops (&loop, &body);
    4865          113 :   gfc_add_block_to_block (&se->pre, &loop.pre);
    4866          113 :   gfc_add_block_to_block (&se->pre, &loop.post);
    4867          113 :   gfc_cleanup_loop (&loop);
    4868              : 
    4869          113 :   se->expr = resvar;
    4870          113 : }
    4871              : 
    4872              : 
    4873              : /* Tells whether the expression E is a reference to an optional variable whose
    4874              :    presence is not known at compile time.  Those are variable references without
    4875              :    subreference; if there is a subreference, we can assume the variable is
    4876              :    present.  We have to special case full arrays, which we represent with a fake
    4877              :    "full" reference, and class descriptors for which a reference to data is not
    4878              :    really a subreference.  */
    4879              : 
    4880              : bool
    4881        14629 : maybe_absent_optional_variable (gfc_expr *e)
    4882              : {
    4883        14629 :   if (!(e && e->expr_type == EXPR_VARIABLE))
    4884              :     return false;
    4885              : 
    4886         1716 :   gfc_symbol *sym = e->symtree->n.sym;
    4887         1716 :   if (!sym->attr.optional)
    4888              :     return false;
    4889              : 
    4890          224 :   gfc_ref *ref = e->ref;
    4891          224 :   if (ref == nullptr)
    4892              :     return true;
    4893              : 
    4894           20 :   if (ref->type == REF_ARRAY
    4895           20 :       && ref->u.ar.type == AR_FULL
    4896           20 :       && ref->next == nullptr)
    4897              :     return true;
    4898              : 
    4899            0 :   if (!(sym->ts.type == BT_CLASS
    4900            0 :         && ref->type == REF_COMPONENT
    4901            0 :         && ref->u.c.component == CLASS_DATA (sym)))
    4902              :     return false;
    4903              : 
    4904            0 :   gfc_ref *next_ref = ref->next;
    4905            0 :   if (next_ref == nullptr)
    4906              :     return true;
    4907              : 
    4908            0 :   if (next_ref->type == REF_ARRAY
    4909            0 :       && next_ref->u.ar.type == AR_FULL
    4910            0 :       && next_ref->next == nullptr)
    4911            0 :     return true;
    4912              : 
    4913              :   return false;
    4914              : }
    4915              : 
    4916              : 
    4917              : /* Emit code for minloc or maxloc intrinsic.  There are many different cases
    4918              :    we need to handle.  For performance reasons we sometimes create two
    4919              :    loops instead of one, where the second one is much simpler.
    4920              :    Examples for minloc intrinsic:
    4921              :    A: Result is scalar.
    4922              :       1) Array mask is used and NaNs need to be supported:
    4923              :          limit = Infinity;
    4924              :          pos = 0;
    4925              :          S = from;
    4926              :          while (S <= to) {
    4927              :            if (mask[S]) {
    4928              :              if (pos == 0) pos = S + (1 - from);
    4929              :              if (a[S] <= limit) {
    4930              :                limit = a[S];
    4931              :                pos = S + (1 - from);
    4932              :                goto lab1;
    4933              :              }
    4934              :            }
    4935              :            S++;
    4936              :          }
    4937              :          goto lab2;
    4938              :          lab1:;
    4939              :          while (S <= to) {
    4940              :            if (mask[S])
    4941              :              if (a[S] < limit) {
    4942              :                limit = a[S];
    4943              :                pos = S + (1 - from);
    4944              :              }
    4945              :            S++;
    4946              :          }
    4947              :          lab2:;
    4948              :       2) NaNs need to be supported, but it is known at compile time or cheaply
    4949              :          at runtime whether array is nonempty or not:
    4950              :          limit = Infinity;
    4951              :          pos = 0;
    4952              :          S = from;
    4953              :          while (S <= to) {
    4954              :            if (a[S] <= limit) {
    4955              :              limit = a[S];
    4956              :              pos = S + (1 - from);
    4957              :              goto lab1;
    4958              :            }
    4959              :            S++;
    4960              :          }
    4961              :          if (from <= to) pos = 1;
    4962              :          goto lab2;
    4963              :          lab1:;
    4964              :          while (S <= to) {
    4965              :            if (a[S] < limit) {
    4966              :              limit = a[S];
    4967              :              pos = S + (1 - from);
    4968              :            }
    4969              :            S++;
    4970              :          }
    4971              :          lab2:;
    4972              :       3) NaNs aren't supported, array mask is used:
    4973              :          limit = infinities_supported ? Infinity : huge (limit);
    4974              :          pos = 0;
    4975              :          S = from;
    4976              :          while (S <= to) {
    4977              :            if (mask[S]) {
    4978              :              limit = a[S];
    4979              :              pos = S + (1 - from);
    4980              :              goto lab1;
    4981              :            }
    4982              :            S++;
    4983              :          }
    4984              :          goto lab2;
    4985              :          lab1:;
    4986              :          while (S <= to) {
    4987              :            if (mask[S])
    4988              :              if (a[S] < limit) {
    4989              :                limit = a[S];
    4990              :                pos = S + (1 - from);
    4991              :              }
    4992              :            S++;
    4993              :          }
    4994              :          lab2:;
    4995              :       4) Same without array mask:
    4996              :          limit = infinities_supported ? Infinity : huge (limit);
    4997              :          pos = (from <= to) ? 1 : 0;
    4998              :          S = from;
    4999              :          while (S <= to) {
    5000              :            if (a[S] < limit) {
    5001              :              limit = a[S];
    5002              :              pos = S + (1 - from);
    5003              :            }
    5004              :            S++;
    5005              :          }
    5006              :    B: Array result, non-CHARACTER type, DIM absent
    5007              :       Generate similar code as in the scalar case, using a collection of
    5008              :       variables (one per dimension) instead of a single variable as result.
    5009              :       Picking only cases 1) and 4) with ARRAY of rank 2, the generated code
    5010              :       becomes:
    5011              :       1) Array mask is used and NaNs need to be supported:
    5012              :          limit = Infinity;
    5013              :          pos0 = 0;
    5014              :          pos1 = 0;
    5015              :          S1 = from1;
    5016              :          second_loop_entry = false;
    5017              :          while (S1 <= to1) {
    5018              :            S0 = from0;
    5019              :            while (s0 <= to0 {
    5020              :              if (mask[S1][S0]) {
    5021              :                if (pos0 == 0) {
    5022              :                  pos0 = S0 + (1 - from0);
    5023              :                  pos1 = S1 + (1 - from1);
    5024              :                }
    5025              :                if (a[S1][S0] <= limit) {
    5026              :                  limit = a[S1][S0];
    5027              :                  pos0 = S0 + (1 - from0);
    5028              :                  pos1 = S1 + (1 - from1);
    5029              :                  second_loop_entry = true;
    5030              :                  goto lab1;
    5031              :                }
    5032              :              }
    5033              :              S0++;
    5034              :            }
    5035              :            S1++;
    5036              :          }
    5037              :          goto lab2;
    5038              :          lab1:;
    5039              :          S1 = second_loop_entry ? S1 : from1;
    5040              :          while (S1 <= to1) {
    5041              :            S0 = second_loop_entry ? S0 : from0;
    5042              :            while (S0 <= to0) {
    5043              :              if (mask[S1][S0])
    5044              :                if (a[S1][S0] < limit) {
    5045              :                  limit = a[S1][S0];
    5046              :                  pos0 = S + (1 - from0);
    5047              :                  pos1 = S + (1 - from1);
    5048              :                }
    5049              :              second_loop_entry = false;
    5050              :              S0++;
    5051              :            }
    5052              :            S1++;
    5053              :          }
    5054              :          lab2:;
    5055              :          result = { pos0, pos1 };
    5056              :       ...
    5057              :       4) NANs aren't supported, no array mask.
    5058              :          limit = infinities_supported ? Infinity : huge (limit);
    5059              :          pos0 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
    5060              :          pos1 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
    5061              :          S1 = from1;
    5062              :          while (S1 <= to1) {
    5063              :            S0 = from0;
    5064              :            while (S0 <= to0) {
    5065              :              if (a[S1][S0] < limit) {
    5066              :                limit = a[S1][S0];
    5067              :                pos0 = S + (1 - from0);
    5068              :                pos1 = S + (1 - from1);
    5069              :              }
    5070              :              S0++;
    5071              :            }
    5072              :            S1++;
    5073              :          }
    5074              :          result = { pos0, pos1 };
    5075              :    C: Otherwise, a call is generated.
    5076              :    For 2) and 4), if mask is scalar, this all goes into a conditional,
    5077              :    setting pos = 0; in the else branch.
    5078              : 
    5079              :    Since we now also support the BACK argument, instead of using
    5080              :    if (a[S] < limit), we now use
    5081              : 
    5082              :    if (back)
    5083              :      cond = a[S] <= limit;
    5084              :    else
    5085              :      cond = a[S] < limit;
    5086              :    if (cond) {
    5087              :      ....
    5088              : 
    5089              :    The optimizer is smart enough to move the condition out of the loop.
    5090              :    They are now marked as unlikely too for further speedup.  */
    5091              : 
    5092              : static void
    5093        18946 : gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op)
    5094              : {
    5095        18946 :   stmtblock_t body;
    5096        18946 :   stmtblock_t block;
    5097        18946 :   stmtblock_t ifblock;
    5098        18946 :   stmtblock_t elseblock;
    5099        18946 :   tree limit;
    5100        18946 :   tree type;
    5101        18946 :   tree tmp;
    5102        18946 :   tree cond;
    5103        18946 :   tree elsetmp;
    5104        18946 :   tree ifbody;
    5105        18946 :   tree offset[GFC_MAX_DIMENSIONS];
    5106        18946 :   tree nonempty;
    5107        18946 :   tree lab1, lab2;
    5108        18946 :   tree b_if, b_else;
    5109        18946 :   tree back;
    5110        18946 :   gfc_loopinfo loop, *ploop;
    5111        18946 :   gfc_actual_arglist *array_arg, *dim_arg, *mask_arg, *kind_arg;
    5112        18946 :   gfc_actual_arglist *back_arg;
    5113        18946 :   gfc_ss *arrayss = nullptr;
    5114        18946 :   gfc_ss *maskss = nullptr;
    5115        18946 :   gfc_ss *orig_ss = nullptr;
    5116        18946 :   gfc_se arrayse;
    5117        18946 :   gfc_se maskse;
    5118        18946 :   gfc_se nested_se;
    5119        18946 :   gfc_se *base_se;
    5120        18946 :   gfc_expr *arrayexpr;
    5121        18946 :   gfc_expr *maskexpr;
    5122        18946 :   gfc_expr *backexpr;
    5123        18946 :   gfc_se backse;
    5124        18946 :   tree pos[GFC_MAX_DIMENSIONS];
    5125        18946 :   tree idx[GFC_MAX_DIMENSIONS];
    5126        18946 :   tree result_var = NULL_TREE;
    5127        18946 :   int n;
    5128        18946 :   bool optional_mask;
    5129              : 
    5130        18946 :   array_arg = expr->value.function.actual;
    5131        18946 :   dim_arg = array_arg->next;
    5132        18946 :   mask_arg = dim_arg->next;
    5133        18946 :   kind_arg = mask_arg->next;
    5134        18946 :   back_arg = kind_arg->next;
    5135              : 
    5136        18946 :   bool dim_present = dim_arg->expr != nullptr;
    5137        18946 :   bool nested_loop = dim_present && expr->rank > 0;
    5138              : 
    5139              :   /* Remove kind.  */
    5140        18946 :   if (kind_arg->expr)
    5141              :     {
    5142         2240 :       gfc_free_expr (kind_arg->expr);
    5143         2240 :       kind_arg->expr = NULL;
    5144              :     }
    5145              : 
    5146              :   /* Pass BACK argument by value.  */
    5147        18946 :   back_arg->name = "%VAL";
    5148              : 
    5149        18946 :   if (se->ss)
    5150              :     {
    5151        14780 :       if (se->ss->info->useflags)
    5152              :         {
    5153         7695 :           if (!dim_present || !gfc_inline_intrinsic_function_p (expr))
    5154              :             {
    5155              :               /* The code generating and initializing the result array has been
    5156              :                  generated already before the scalarization loop, either with a
    5157              :                  library function call or with inline code; now we can just use
    5158              :                  the result.  */
    5159         4899 :               gfc_conv_tmp_array_ref (se);
    5160        13878 :               return;
    5161              :             }
    5162              :         }
    5163         7085 :       else if (!gfc_inline_intrinsic_function_p (expr))
    5164              :         {
    5165         3788 :           gfc_conv_intrinsic_funcall (se, expr);
    5166         3788 :           return;
    5167              :         }
    5168              :     }
    5169              : 
    5170        10259 :   arrayexpr = array_arg->expr;
    5171              : 
    5172              :   /* Special case for character maxloc.  Remove unneeded "dim" actual
    5173              :      argument, then call a library function.  */
    5174              : 
    5175        10259 :   if (arrayexpr->ts.type == BT_CHARACTER)
    5176              :     {
    5177          292 :       gcc_assert (expr->rank == 0);
    5178              : 
    5179          292 :       if (dim_arg->expr)
    5180              :         {
    5181          292 :           gfc_free_expr (dim_arg->expr);
    5182          292 :           dim_arg->expr = NULL;
    5183              :         }
    5184          292 :       gfc_conv_intrinsic_funcall (se, expr);
    5185          292 :       return;
    5186              :     }
    5187              : 
    5188         9967 :   type = gfc_typenode_for_spec (&expr->ts);
    5189              : 
    5190         9967 :   if (expr->rank > 0 && !dim_present)
    5191              :     {
    5192         3297 :       gfc_array_spec as;
    5193         3297 :       memset (&as, 0, sizeof (as));
    5194              : 
    5195         3297 :       as.rank = 1;
    5196         3297 :       as.lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
    5197              :                                       &arrayexpr->where,
    5198              :                                       HOST_WIDE_INT_1);
    5199         6594 :       as.upper[0] = gfc_get_int_expr (gfc_index_integer_kind,
    5200              :                                       &arrayexpr->where,
    5201         3297 :                                       arrayexpr->rank);
    5202              : 
    5203         3297 :       tree array = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
    5204              : 
    5205         3297 :       result_var = gfc_create_var (array, "loc_result");
    5206              :     }
    5207              : 
    5208         7171 :   const int reduction_dimensions = dim_present ? 1 : arrayexpr->rank;
    5209              : 
    5210              :   /* Initialize the result.  */
    5211        22225 :   for (int i = 0; i < reduction_dimensions; i++)
    5212              :     {
    5213        12258 :       pos[i] = gfc_create_var (gfc_array_index_type,
    5214              :                                gfc_get_string ("pos%d", i));
    5215        12258 :       offset[i] = gfc_create_var (gfc_array_index_type,
    5216              :                                   gfc_get_string ("offset%d", i));
    5217        12258 :       idx[i] = gfc_create_var (gfc_array_index_type,
    5218              :                                gfc_get_string ("idx%d", i));
    5219              :     }
    5220              : 
    5221         9967 :   maskexpr = mask_arg->expr;
    5222         6534 :   optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
    5223         5345 :     && maskexpr->symtree->n.sym->attr.dummy
    5224        10132 :     && maskexpr->symtree->n.sym->attr.optional;
    5225         9967 :   backexpr = back_arg->expr;
    5226              : 
    5227        17138 :   gfc_init_se (&backse, nested_loop ? se : nullptr);
    5228         9967 :   if (backexpr == nullptr)
    5229            0 :     back = logical_false_node;
    5230         9967 :   else if (maybe_absent_optional_variable (backexpr))
    5231              :     {
    5232              :       /* This should have been checked already by
    5233              :          maybe_absent_optional_variable.  */
    5234          184 :       gcc_checking_assert (backexpr->expr_type == EXPR_VARIABLE);
    5235              : 
    5236          184 :       gfc_conv_expr (&backse, backexpr);
    5237          184 :       tree present = gfc_conv_expr_present (backexpr->symtree->n.sym, false);
    5238          184 :       back = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    5239              :                               logical_type_node, present, backse.expr);
    5240              :     }
    5241              :   else
    5242              :     {
    5243         9783 :       gfc_conv_expr (&backse, backexpr);
    5244         9783 :       back = backse.expr;
    5245              :     }
    5246         9967 :   gfc_add_block_to_block (&se->pre, &backse.pre);
    5247         9967 :   back = gfc_evaluate_now_loc (input_location, back, &se->pre);
    5248         9967 :   gfc_add_block_to_block (&se->pre, &backse.post);
    5249              : 
    5250         9967 :   if (nested_loop)
    5251              :     {
    5252         2796 :       gfc_init_se (&nested_se, se);
    5253         2796 :       base_se = &nested_se;
    5254              :     }
    5255              :   else
    5256              :     {
    5257              :       /* Walk the arguments.  */
    5258         7171 :       arrayss = gfc_walk_expr (arrayexpr);
    5259         7171 :       gcc_assert (arrayss != gfc_ss_terminator);
    5260              : 
    5261         7171 :       if (maskexpr && maskexpr->rank != 0)
    5262              :         {
    5263         2716 :           maskss = gfc_walk_expr (maskexpr);
    5264         2716 :           gcc_assert (maskss != gfc_ss_terminator);
    5265              :         }
    5266              : 
    5267              :       base_se = nullptr;
    5268              :     }
    5269              : 
    5270        18123 :   nonempty = nullptr;
    5271         7464 :   if (!(maskexpr && maskexpr->rank > 0))
    5272              :     {
    5273         6077 :       mpz_t asize;
    5274         6077 :       bool reduction_size_known;
    5275              : 
    5276         6077 :       if (dim_present)
    5277              :         {
    5278         4032 :           int reduction_dim;
    5279         4032 :           if (dim_arg->expr->expr_type == EXPR_CONSTANT)
    5280         4030 :             reduction_dim = mpz_get_si (dim_arg->expr->value.integer) - 1;
    5281            2 :           else if (arrayexpr->rank == 1)
    5282              :             reduction_dim = 0;
    5283              :           else
    5284            0 :             gcc_unreachable ();
    5285         4032 :           reduction_size_known = gfc_array_dimen_size (arrayexpr, reduction_dim,
    5286              :                                                        &asize);
    5287              :         }
    5288              :       else
    5289         2045 :         reduction_size_known = gfc_array_size (arrayexpr, &asize);
    5290              : 
    5291         6077 :       if (reduction_size_known)
    5292              :         {
    5293         4482 :           nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
    5294         4482 :           mpz_clear (asize);
    5295         4482 :           nonempty = fold_build2_loc (input_location, GT_EXPR,
    5296              :                                       logical_type_node, nonempty,
    5297              :                                       gfc_index_zero_node);
    5298              :         }
    5299         6077 :       maskss = NULL;
    5300              :     }
    5301              : 
    5302         9967 :   limit = gfc_create_var (gfc_typenode_for_spec (&arrayexpr->ts), "limit");
    5303         9967 :   switch (arrayexpr->ts.type)
    5304              :     {
    5305         3914 :     case BT_REAL:
    5306         3914 :       tmp = gfc_build_inf_or_huge (TREE_TYPE (limit), arrayexpr->ts.kind);
    5307         3914 :       break;
    5308              : 
    5309         6029 :     case BT_INTEGER:
    5310         6029 :       n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
    5311         6029 :       tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
    5312              :                                   arrayexpr->ts.kind);
    5313         6029 :       break;
    5314              : 
    5315           24 :     case BT_UNSIGNED:
    5316              :       /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE().  */
    5317           24 :       if (op == GT_EXPR)
    5318              :         {
    5319           12 :           tmp = gfc_get_unsigned_type (arrayexpr->ts.kind);
    5320           12 :           tmp = build_int_cst (tmp, 0);
    5321              :         }
    5322              :       else
    5323              :         {
    5324           12 :           n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
    5325           12 :           tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
    5326              :                                                expr->ts.kind);
    5327              :         }
    5328              :       break;
    5329              : 
    5330            0 :     default:
    5331            0 :       gcc_unreachable ();
    5332              :     }
    5333              : 
    5334              :   /* We start with the most negative possible value for MAXLOC, and the most
    5335              :      positive possible value for MINLOC. The most negative possible value is
    5336              :      -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
    5337              :      possible value is HUGE in both cases.  BT_UNSIGNED has already been dealt
    5338              :      with above.  */
    5339         9967 :   if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
    5340         4732 :     tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
    5341         4732 :   if (op == GT_EXPR && arrayexpr->ts.type == BT_INTEGER)
    5342         2914 :     tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp), tmp,
    5343         2914 :                            build_int_cst (TREE_TYPE (tmp), 1));
    5344              : 
    5345         9967 :   gfc_add_modify (&se->pre, limit, tmp);
    5346              : 
    5347              :   /* If we are in a case where we generate two sets of loops, the second one
    5348              :      should continue where the first stopped instead of restarting from the
    5349              :      beginning.  So nested loops in the second set should have a partial range
    5350              :      on the first iteration, but they should start from the beginning and span
    5351              :      their full range on the following iterations.  So we use conditionals in
    5352              :      the loops lower bounds, and use the following variable in those
    5353              :      conditionals to decide whether to use the original loop bound or to use
    5354              :      the index at which the loop from the first set stopped.  */
    5355         9967 :   tree second_loop_entry = gfc_create_var (logical_type_node,
    5356              :                                            "second_loop_entry");
    5357         9967 :   gfc_add_modify (&se->pre, second_loop_entry, logical_false_node);
    5358              : 
    5359         9967 :   if (nested_loop)
    5360              :     {
    5361         2796 :       ploop = enter_nested_loop (&nested_se);
    5362         2796 :       orig_ss = nested_se.ss;
    5363         2796 :       ploop->temp_dim = 1;
    5364              :     }
    5365              :   else
    5366              :     {
    5367              :       /* Initialize the scalarizer.  */
    5368         7171 :       gfc_init_loopinfo (&loop);
    5369              : 
    5370              :       /* We add the mask first because the number of iterations is taken
    5371              :          from the last ss, and this breaks if an absent optional argument
    5372              :          is used for mask.  */
    5373              : 
    5374         7171 :       if (maskss)
    5375         2716 :         gfc_add_ss_to_loop (&loop, maskss);
    5376              : 
    5377         7171 :       gfc_add_ss_to_loop (&loop, arrayss);
    5378              : 
    5379              :       /* Initialize the loop.  */
    5380         7171 :       gfc_conv_ss_startstride (&loop);
    5381              : 
    5382              :       /* The code generated can have more than one loop in sequence (see the
    5383              :          comment at the function header).  This doesn't work well with the
    5384              :          scalarizer, which changes arrays' offset when the scalarization loops
    5385              :          are generated (see gfc_trans_preloop_setup).  Fortunately, we can use
    5386              :          the scalarizer temporary code to handle multiple loops.  Thus, we set
    5387              :          temp_dim here, we call gfc_mark_ss_chain_used with flag=3 later, and
    5388              :          we use gfc_trans_scalarized_loop_boundary even later to restore
    5389              :          offset.  */
    5390         7171 :       loop.temp_dim = loop.dimen;
    5391         7171 :       gfc_conv_loop_setup (&loop, &expr->where);
    5392              : 
    5393         7171 :       ploop = &loop;
    5394              :     }
    5395              : 
    5396         9967 :   gcc_assert (reduction_dimensions == ploop->dimen);
    5397              : 
    5398         9967 :   if (nonempty == NULL && !(maskexpr && maskexpr->rank > 0))
    5399              :     {
    5400         1595 :       nonempty = logical_true_node;
    5401              : 
    5402         3697 :       for (int i = 0; i < ploop->dimen; i++)
    5403              :         {
    5404         2102 :           if (!(ploop->from[i] && ploop->to[i]))
    5405              :             {
    5406              :               nonempty = NULL;
    5407              :               break;
    5408              :             }
    5409              : 
    5410         2102 :           tree tmp = fold_build2_loc (input_location, LE_EXPR,
    5411              :                                       logical_type_node, ploop->from[i],
    5412              :                                       ploop->to[i]);
    5413              : 
    5414         2102 :           nonempty = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    5415              :                                       logical_type_node, nonempty, tmp);
    5416              :         }
    5417              :     }
    5418              : 
    5419        11562 :   lab1 = NULL;
    5420        11562 :   lab2 = NULL;
    5421              :   /* Initialize the position to zero, following Fortran 2003.  We are free
    5422              :      to do this because Fortran 95 allows the result of an entirely false
    5423              :      mask to be processor dependent.  If we know at compile time the array
    5424              :      is non-empty and no MASK is used, we can initialize to 1 to simplify
    5425              :      the inner loop.  */
    5426         9967 :   if (nonempty != NULL && !HONOR_NANS (DECL_MODE (limit)))
    5427              :     {
    5428         3748 :       tree init = fold_build3_loc (input_location, COND_EXPR,
    5429              :                                    gfc_array_index_type, nonempty,
    5430              :                                    gfc_index_one_node,
    5431              :                                    gfc_index_zero_node);
    5432        12178 :       for (int i = 0; i < ploop->dimen; i++)
    5433         4682 :         gfc_add_modify (&ploop->pre, pos[i], init);
    5434              :     }
    5435              :   else
    5436              :     {
    5437        13795 :       for (int i = 0; i < ploop->dimen; i++)
    5438         7576 :         gfc_add_modify (&ploop->pre, pos[i], gfc_index_zero_node);
    5439         6219 :       lab1 = gfc_build_label_decl (NULL_TREE);
    5440         6219 :       TREE_USED (lab1) = 1;
    5441         6219 :       lab2 = gfc_build_label_decl (NULL_TREE);
    5442         6219 :       TREE_USED (lab2) = 1;
    5443              :     }
    5444              : 
    5445              :   /* An offset must be added to the loop
    5446              :      counter to obtain the required position.  */
    5447        22225 :   for (int i = 0; i < ploop->dimen; i++)
    5448              :     {
    5449        12258 :       gcc_assert (ploop->from[i]);
    5450              : 
    5451        12258 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    5452              :                              gfc_index_one_node, ploop->from[i]);
    5453        12258 :       gfc_add_modify (&ploop->pre, offset[i], tmp);
    5454              :     }
    5455              : 
    5456         9967 :   if (!nested_loop)
    5457              :     {
    5458         9981 :       gfc_mark_ss_chain_used (arrayss, lab1 ? 3 : 1);
    5459         7171 :       if (maskss)
    5460         2716 :         gfc_mark_ss_chain_used (maskss, lab1 ? 3 : 1);
    5461              :     }
    5462              : 
    5463              :   /* Generate the loop body.  */
    5464         9967 :   gfc_start_scalarized_body (ploop, &body);
    5465              : 
    5466              :   /* If we have a mask, only check this element if the mask is set.  */
    5467         9967 :   if (maskexpr && maskexpr->rank > 0)
    5468              :     {
    5469         3890 :       gfc_init_se (&maskse, base_se);
    5470         3890 :       gfc_copy_loopinfo_to_se (&maskse, ploop);
    5471         3890 :       if (!nested_loop)
    5472         2716 :         maskse.ss = maskss;
    5473         3890 :       gfc_conv_expr_val (&maskse, maskexpr);
    5474         3890 :       gfc_add_block_to_block (&body, &maskse.pre);
    5475              : 
    5476         3890 :       gfc_start_block (&block);
    5477              :     }
    5478              :   else
    5479         6077 :     gfc_init_block (&block);
    5480              : 
    5481              :   /* Compare with the current limit.  */
    5482         9967 :   gfc_init_se (&arrayse, base_se);
    5483         9967 :   gfc_copy_loopinfo_to_se (&arrayse, ploop);
    5484         9967 :   if (!nested_loop)
    5485         7171 :     arrayse.ss = arrayss;
    5486         9967 :   gfc_conv_expr_val (&arrayse, arrayexpr);
    5487         9967 :   gfc_add_block_to_block (&block, &arrayse.pre);
    5488              : 
    5489              :   /* We do the following if this is a more extreme value.  */
    5490         9967 :   gfc_start_block (&ifblock);
    5491              : 
    5492              :   /* Assign the value to the limit...  */
    5493         9967 :   gfc_add_modify (&ifblock, limit, arrayse.expr);
    5494              : 
    5495         9967 :   if (nonempty == NULL && HONOR_NANS (DECL_MODE (limit)))
    5496              :     {
    5497         1585 :       stmtblock_t ifblock2;
    5498         1585 :       tree ifbody2;
    5499              : 
    5500         1585 :       gfc_start_block (&ifblock2);
    5501         5072 :       for (int i = 0; i < ploop->dimen; i++)
    5502              :         {
    5503         1902 :           tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
    5504              :                                  ploop->loopvar[i], offset[i]);
    5505         1902 :           gfc_add_modify (&ifblock2, pos[i], tmp);
    5506              :         }
    5507         1585 :       ifbody2 = gfc_finish_block (&ifblock2);
    5508              : 
    5509         1585 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    5510              :                               pos[0], gfc_index_zero_node);
    5511         1585 :       tmp = build3_v (COND_EXPR, cond, ifbody2,
    5512              :                       build_empty_stmt (input_location));
    5513         1585 :       gfc_add_expr_to_block (&block, tmp);
    5514              :     }
    5515              : 
    5516        22225 :   for (int i = 0; i < ploop->dimen; i++)
    5517              :     {
    5518        12258 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
    5519              :                              ploop->loopvar[i], offset[i]);
    5520        12258 :       gfc_add_modify (&ifblock, pos[i], tmp);
    5521        12258 :       gfc_add_modify (&ifblock, idx[i], ploop->loopvar[i]);
    5522              :     }
    5523              : 
    5524         9967 :   gfc_add_modify (&ifblock, second_loop_entry, logical_true_node);
    5525              : 
    5526         9967 :   if (lab1)
    5527         6219 :     gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab1));
    5528              : 
    5529         9967 :   ifbody = gfc_finish_block (&ifblock);
    5530              : 
    5531         9967 :   if (!lab1 || HONOR_NANS (DECL_MODE (limit)))
    5532              :     {
    5533         7662 :       if (lab1)
    5534         6022 :         cond = fold_build2_loc (input_location,
    5535              :                                 op == GT_EXPR ? GE_EXPR : LE_EXPR,
    5536              :                                 logical_type_node, arrayse.expr, limit);
    5537              :       else
    5538              :         {
    5539         3748 :           tree ifbody2, elsebody2;
    5540              : 
    5541              :           /* We switch to > or >= depending on the value of the BACK argument. */
    5542         3748 :           cond = gfc_create_var (logical_type_node, "cond");
    5543              : 
    5544         3748 :           gfc_start_block (&ifblock);
    5545         5641 :           b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
    5546              :                                   logical_type_node, arrayse.expr, limit);
    5547              : 
    5548         3748 :           gfc_add_modify (&ifblock, cond, b_if);
    5549         3748 :           ifbody2 = gfc_finish_block (&ifblock);
    5550              : 
    5551         3748 :           gfc_start_block (&elseblock);
    5552         3748 :           b_else = fold_build2_loc (input_location, op, logical_type_node,
    5553              :                                     arrayse.expr, limit);
    5554              : 
    5555         3748 :           gfc_add_modify (&elseblock, cond, b_else);
    5556         3748 :           elsebody2 = gfc_finish_block (&elseblock);
    5557              : 
    5558         3748 :           tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
    5559              :                                  back, ifbody2, elsebody2);
    5560              : 
    5561         3748 :           gfc_add_expr_to_block (&block, tmp);
    5562              :         }
    5563              : 
    5564         7662 :       cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
    5565         7662 :       ifbody = build3_v (COND_EXPR, cond, ifbody,
    5566              :                          build_empty_stmt (input_location));
    5567              :     }
    5568         9967 :   gfc_add_expr_to_block (&block, ifbody);
    5569              : 
    5570         9967 :   if (maskexpr && maskexpr->rank > 0)
    5571              :     {
    5572              :       /* We enclose the above in if (mask) {...}.  If the mask is an
    5573              :          optional argument, generate IF (.NOT. PRESENT(MASK)
    5574              :          .OR. MASK(I)). */
    5575              : 
    5576         3890 :       tree ifmask;
    5577         3890 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5578         3890 :       tmp = gfc_finish_block (&block);
    5579         3890 :       tmp = build3_v (COND_EXPR, ifmask, tmp,
    5580              :                       build_empty_stmt (input_location));
    5581         3890 :     }
    5582              :   else
    5583         6077 :     tmp = gfc_finish_block (&block);
    5584         9967 :   gfc_add_expr_to_block (&body, tmp);
    5585              : 
    5586         9967 :   if (lab1)
    5587              :     {
    5588        13795 :       for (int i = 0; i < ploop->dimen; i++)
    5589         7576 :         ploop->from[i] = fold_build3_loc (input_location, COND_EXPR,
    5590         7576 :                                           TREE_TYPE (ploop->from[i]),
    5591              :                                           second_loop_entry, idx[i],
    5592              :                                           ploop->from[i]);
    5593              : 
    5594         6219 :       gfc_trans_scalarized_loop_boundary (ploop, &body);
    5595              : 
    5596         6219 :       if (nested_loop)
    5597              :         {
    5598              :           /* The first loop already advanced the parent se'ss chain, so clear
    5599              :              the parent now to avoid doing it a second time, making the chain
    5600              :              out of sync.  */
    5601         1858 :           nested_se.parent = nullptr;
    5602         1858 :           nested_se.ss = orig_ss;
    5603              :         }
    5604              : 
    5605         6219 :       stmtblock_t * const outer_block = &ploop->code[ploop->dimen - 1];
    5606              : 
    5607         6219 :       if (HONOR_NANS (DECL_MODE (limit)))
    5608              :         {
    5609         3914 :           if (nonempty != NULL)
    5610              :             {
    5611         2329 :               stmtblock_t init_block;
    5612         2329 :               gfc_init_block (&init_block);
    5613              : 
    5614         7558 :               for (int i = 0; i < ploop->dimen; i++)
    5615         2900 :                 gfc_add_modify (&init_block, pos[i], gfc_index_one_node);
    5616              : 
    5617         2329 :               tree ifbody = gfc_finish_block (&init_block);
    5618         2329 :               tmp = build3_v (COND_EXPR, nonempty, ifbody,
    5619              :                               build_empty_stmt (input_location));
    5620         2329 :               gfc_add_expr_to_block (outer_block, tmp);
    5621              :             }
    5622              :         }
    5623              : 
    5624         6219 :       gfc_add_expr_to_block (outer_block, build1_v (GOTO_EXPR, lab2));
    5625         6219 :       gfc_add_expr_to_block (outer_block, build1_v (LABEL_EXPR, lab1));
    5626              : 
    5627              :       /* If we have a mask, only check this element if the mask is set.  */
    5628         6219 :       if (maskexpr && maskexpr->rank > 0)
    5629              :         {
    5630         3890 :           gfc_init_se (&maskse, base_se);
    5631         3890 :           gfc_copy_loopinfo_to_se (&maskse, ploop);
    5632         3890 :           if (!nested_loop)
    5633         2716 :             maskse.ss = maskss;
    5634         3890 :           gfc_conv_expr_val (&maskse, maskexpr);
    5635         3890 :           gfc_add_block_to_block (&body, &maskse.pre);
    5636              : 
    5637         3890 :           gfc_start_block (&block);
    5638              :         }
    5639              :       else
    5640         2329 :         gfc_init_block (&block);
    5641              : 
    5642              :       /* Compare with the current limit.  */
    5643         6219 :       gfc_init_se (&arrayse, base_se);
    5644         6219 :       gfc_copy_loopinfo_to_se (&arrayse, ploop);
    5645         6219 :       if (!nested_loop)
    5646         4361 :         arrayse.ss = arrayss;
    5647         6219 :       gfc_conv_expr_val (&arrayse, arrayexpr);
    5648         6219 :       gfc_add_block_to_block (&block, &arrayse.pre);
    5649              : 
    5650              :       /* We do the following if this is a more extreme value.  */
    5651         6219 :       gfc_start_block (&ifblock);
    5652              : 
    5653              :       /* Assign the value to the limit...  */
    5654         6219 :       gfc_add_modify (&ifblock, limit, arrayse.expr);
    5655              : 
    5656        20014 :       for (int i = 0; i < ploop->dimen; i++)
    5657              :         {
    5658         7576 :           tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
    5659              :                                  ploop->loopvar[i], offset[i]);
    5660         7576 :           gfc_add_modify (&ifblock, pos[i], tmp);
    5661              :         }
    5662              : 
    5663         6219 :       ifbody = gfc_finish_block (&ifblock);
    5664              : 
    5665              :       /* We switch to > or >= depending on the value of the BACK argument. */
    5666         6219 :       {
    5667         6219 :         tree ifbody2, elsebody2;
    5668              : 
    5669         6219 :         cond = gfc_create_var (logical_type_node, "cond");
    5670              : 
    5671         6219 :         gfc_start_block (&ifblock);
    5672         9561 :         b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
    5673              :                                 logical_type_node, arrayse.expr, limit);
    5674              : 
    5675         6219 :         gfc_add_modify (&ifblock, cond, b_if);
    5676         6219 :         ifbody2 = gfc_finish_block (&ifblock);
    5677              : 
    5678         6219 :         gfc_start_block (&elseblock);
    5679         6219 :         b_else = fold_build2_loc (input_location, op, logical_type_node,
    5680              :                                   arrayse.expr, limit);
    5681              : 
    5682         6219 :         gfc_add_modify (&elseblock, cond, b_else);
    5683         6219 :         elsebody2 = gfc_finish_block (&elseblock);
    5684              : 
    5685         6219 :         tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
    5686              :                                back, ifbody2, elsebody2);
    5687              :       }
    5688              : 
    5689         6219 :       gfc_add_expr_to_block (&block, tmp);
    5690         6219 :       cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
    5691         6219 :       tmp = build3_v (COND_EXPR, cond, ifbody,
    5692              :                       build_empty_stmt (input_location));
    5693              : 
    5694         6219 :       gfc_add_expr_to_block (&block, tmp);
    5695              : 
    5696         6219 :       if (maskexpr && maskexpr->rank > 0)
    5697              :         {
    5698              :           /* We enclose the above in if (mask) {...}.  If the mask is
    5699              :          an optional argument, generate IF (.NOT. PRESENT(MASK)
    5700              :          .OR. MASK(I)).*/
    5701              : 
    5702         3890 :           tree ifmask;
    5703         3890 :           ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5704         3890 :           tmp = gfc_finish_block (&block);
    5705         3890 :           tmp = build3_v (COND_EXPR, ifmask, tmp,
    5706              :                           build_empty_stmt (input_location));
    5707         3890 :         }
    5708              :       else
    5709         2329 :         tmp = gfc_finish_block (&block);
    5710              : 
    5711         6219 :       gfc_add_expr_to_block (&body, tmp);
    5712         6219 :       gfc_add_modify (&body, second_loop_entry, logical_false_node);
    5713              :     }
    5714              : 
    5715         9967 :   gfc_trans_scalarizing_loops (ploop, &body);
    5716              : 
    5717         9967 :   if (lab2)
    5718         6219 :     gfc_add_expr_to_block (&ploop->pre, build1_v (LABEL_EXPR, lab2));
    5719              : 
    5720              :   /* For a scalar mask, enclose the loop in an if statement.  */
    5721         9967 :   if (maskexpr && maskexpr->rank == 0)
    5722              :     {
    5723         2644 :       tree ifmask;
    5724              : 
    5725         2644 :       gfc_init_se (&maskse, nested_loop ? se : nullptr);
    5726         2644 :       gfc_conv_expr_val (&maskse, maskexpr);
    5727         2644 :       gfc_add_block_to_block (&se->pre, &maskse.pre);
    5728         2644 :       gfc_init_block (&block);
    5729         2644 :       gfc_add_block_to_block (&block, &ploop->pre);
    5730         2644 :       gfc_add_block_to_block (&block, &ploop->post);
    5731         2644 :       tmp = gfc_finish_block (&block);
    5732              : 
    5733              :       /* For the else part of the scalar mask, just initialize
    5734              :          the pos variable the same way as above.  */
    5735              : 
    5736         2644 :       gfc_init_block (&elseblock);
    5737         8224 :       for (int i = 0; i < ploop->dimen; i++)
    5738         2936 :         gfc_add_modify (&elseblock, pos[i], gfc_index_zero_node);
    5739         2644 :       elsetmp = gfc_finish_block (&elseblock);
    5740         2644 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5741         2644 :       tmp = build3_v (COND_EXPR, ifmask, tmp, elsetmp);
    5742         2644 :       gfc_add_expr_to_block (&block, tmp);
    5743         2644 :       gfc_add_block_to_block (&se->pre, &block);
    5744         2644 :     }
    5745              :   else
    5746              :     {
    5747         7323 :       gfc_add_block_to_block (&se->pre, &ploop->pre);
    5748         7323 :       gfc_add_block_to_block (&se->pre, &ploop->post);
    5749              :     }
    5750              : 
    5751         9967 :   if (!nested_loop)
    5752         7171 :     gfc_cleanup_loop (&loop);
    5753              : 
    5754         9967 :   if (!dim_present)
    5755              :     {
    5756         8885 :       for (int i = 0; i < arrayexpr->rank; i++)
    5757              :         {
    5758         5588 :           tree res_idx = build_int_cst (gfc_array_index_type, i);
    5759         5588 :           tree res_arr_ref = gfc_build_array_ref (result_var, res_idx,
    5760              :                                                   NULL_TREE, true);
    5761              : 
    5762         5588 :           tree value = convert (type, pos[i]);
    5763         5588 :           gfc_add_modify (&se->pre, res_arr_ref, value);
    5764              :         }
    5765              : 
    5766         3297 :       se->expr = result_var;
    5767              :     }
    5768              :   else
    5769         6670 :     se->expr = convert (type, pos[0]);
    5770              : }
    5771              : 
    5772              : /* Emit code for findloc.  */
    5773              : 
    5774              : static void
    5775         1344 : gfc_conv_intrinsic_findloc (gfc_se *se, gfc_expr *expr)
    5776              : {
    5777         1344 :   gfc_actual_arglist *array_arg, *value_arg, *dim_arg, *mask_arg,
    5778              :     *kind_arg, *back_arg;
    5779         1344 :   gfc_expr *value_expr;
    5780         1344 :   int ikind;
    5781         1344 :   tree resvar;
    5782         1344 :   stmtblock_t block;
    5783         1344 :   stmtblock_t body;
    5784         1344 :   stmtblock_t loopblock;
    5785         1344 :   tree type;
    5786         1344 :   tree tmp;
    5787         1344 :   tree found;
    5788         1344 :   tree forward_branch = NULL_TREE;
    5789         1344 :   tree back_branch;
    5790         1344 :   gfc_loopinfo loop;
    5791         1344 :   gfc_ss *arrayss;
    5792         1344 :   gfc_ss *maskss;
    5793         1344 :   gfc_se arrayse;
    5794         1344 :   gfc_se valuese;
    5795         1344 :   gfc_se maskse;
    5796         1344 :   gfc_se backse;
    5797         1344 :   tree exit_label;
    5798         1344 :   gfc_expr *maskexpr;
    5799         1344 :   tree offset;
    5800         1344 :   int i;
    5801         1344 :   bool optional_mask;
    5802              : 
    5803         1344 :   array_arg = expr->value.function.actual;
    5804         1344 :   value_arg = array_arg->next;
    5805         1344 :   dim_arg   = value_arg->next;
    5806         1344 :   mask_arg  = dim_arg->next;
    5807         1344 :   kind_arg  = mask_arg->next;
    5808         1344 :   back_arg  = kind_arg->next;
    5809              : 
    5810              :   /* Remove kind and set ikind.  */
    5811         1344 :   if (kind_arg->expr)
    5812              :     {
    5813            0 :       ikind = mpz_get_si (kind_arg->expr->value.integer);
    5814            0 :       gfc_free_expr (kind_arg->expr);
    5815            0 :       kind_arg->expr = NULL;
    5816              :     }
    5817              :   else
    5818         1344 :     ikind = gfc_default_integer_kind;
    5819              : 
    5820         1344 :   value_expr = value_arg->expr;
    5821              : 
    5822              :   /* Unless it's a string, pass VALUE by value.  */
    5823         1344 :   if (value_expr->ts.type != BT_CHARACTER)
    5824          744 :     value_arg->name = "%VAL";
    5825              : 
    5826              :   /* Pass BACK argument by value.  */
    5827         1344 :   back_arg->name = "%VAL";
    5828              : 
    5829              :   /* Call the library if we have a character function or if
    5830              :      rank > 0.  */
    5831         1344 :   if (se->ss || array_arg->expr->ts.type == BT_CHARACTER)
    5832              :     {
    5833         1212 :       se->ignore_optional = 1;
    5834         1212 :       if (expr->rank == 0)
    5835              :         {
    5836              :           /* Remove dim argument.  */
    5837           84 :           gfc_free_expr (dim_arg->expr);
    5838           84 :           dim_arg->expr = NULL;
    5839              :         }
    5840         1212 :       gfc_conv_intrinsic_funcall (se, expr);
    5841         1212 :       return;
    5842              :     }
    5843              : 
    5844          132 :   type = gfc_get_int_type (ikind);
    5845              : 
    5846              :   /* Initialize the result.  */
    5847          132 :   resvar = gfc_create_var (gfc_array_index_type, "pos");
    5848          132 :   gfc_add_modify (&se->pre, resvar, build_int_cst (gfc_array_index_type, 0));
    5849          132 :   offset = gfc_create_var (gfc_array_index_type, "offset");
    5850              : 
    5851          132 :   maskexpr = mask_arg->expr;
    5852           72 :   optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
    5853           60 :     && maskexpr->symtree->n.sym->attr.dummy
    5854          144 :     && maskexpr->symtree->n.sym->attr.optional;
    5855              : 
    5856              :   /*  Generate two loops, one for BACK=.true. and one for BACK=.false.  */
    5857              : 
    5858          396 :   for (i = 0 ; i < 2; i++)
    5859              :     {
    5860              :       /* Walk the arguments.  */
    5861          264 :       arrayss = gfc_walk_expr (array_arg->expr);
    5862          264 :       gcc_assert (arrayss != gfc_ss_terminator);
    5863              : 
    5864          264 :       if (maskexpr && maskexpr->rank != 0)
    5865              :         {
    5866           84 :           maskss = gfc_walk_expr (maskexpr);
    5867           84 :           gcc_assert (maskss != gfc_ss_terminator);
    5868              :         }
    5869              :       else
    5870              :         maskss = NULL;
    5871              : 
    5872              :       /* Initialize the scalarizer.  */
    5873          264 :       gfc_init_loopinfo (&loop);
    5874          264 :       exit_label = gfc_build_label_decl (NULL_TREE);
    5875          264 :       TREE_USED (exit_label) = 1;
    5876              : 
    5877              :       /* We add the mask first because the number of iterations is
    5878              :          taken from the last ss, and this breaks if an absent
    5879              :          optional argument is used for mask.  */
    5880              : 
    5881          264 :       if (maskss)
    5882           84 :         gfc_add_ss_to_loop (&loop, maskss);
    5883          264 :       gfc_add_ss_to_loop (&loop, arrayss);
    5884              : 
    5885              :       /* Initialize the loop.  */
    5886          264 :       gfc_conv_ss_startstride (&loop);
    5887          264 :       gfc_conv_loop_setup (&loop, &expr->where);
    5888              : 
    5889              :       /* Calculate the offset.  */
    5890          264 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    5891              :                              gfc_index_one_node, loop.from[0]);
    5892          264 :       gfc_add_modify (&loop.pre, offset, tmp);
    5893              : 
    5894          264 :       gfc_mark_ss_chain_used (arrayss, 1);
    5895          264 :       if (maskss)
    5896           84 :         gfc_mark_ss_chain_used (maskss, 1);
    5897              : 
    5898              :       /* The first loop is for BACK=.true.  */
    5899          264 :       if (i == 0)
    5900          132 :         loop.reverse[0] = GFC_REVERSE_SET;
    5901              : 
    5902              :       /* Generate the loop body.  */
    5903          264 :       gfc_start_scalarized_body (&loop, &body);
    5904              : 
    5905              :       /* If we have an array mask, only add the element if it is
    5906              :          set.  */
    5907          264 :       if (maskss)
    5908              :         {
    5909           84 :           gfc_init_se (&maskse, NULL);
    5910           84 :           gfc_copy_loopinfo_to_se (&maskse, &loop);
    5911           84 :           maskse.ss = maskss;
    5912           84 :           gfc_conv_expr_val (&maskse, maskexpr);
    5913           84 :           gfc_add_block_to_block (&body, &maskse.pre);
    5914              :         }
    5915              : 
    5916              :       /* If the condition matches then set the return value.  */
    5917          264 :       gfc_start_block (&block);
    5918              : 
    5919              :       /* Add the offset.  */
    5920          264 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5921          264 :                              TREE_TYPE (resvar),
    5922              :                              loop.loopvar[0], offset);
    5923          264 :       gfc_add_modify (&block, resvar, tmp);
    5924              :       /* And break out of the loop.  */
    5925          264 :       tmp = build1_v (GOTO_EXPR, exit_label);
    5926          264 :       gfc_add_expr_to_block (&block, tmp);
    5927              : 
    5928          264 :       found = gfc_finish_block (&block);
    5929              : 
    5930              :       /* Check this element.  */
    5931          264 :       gfc_init_se (&arrayse, NULL);
    5932          264 :       gfc_copy_loopinfo_to_se (&arrayse, &loop);
    5933          264 :       arrayse.ss = arrayss;
    5934          264 :       gfc_conv_expr_val (&arrayse, array_arg->expr);
    5935          264 :       gfc_add_block_to_block (&body, &arrayse.pre);
    5936              : 
    5937          264 :       gfc_init_se (&valuese, NULL);
    5938          264 :       gfc_conv_expr_val (&valuese, value_arg->expr);
    5939          264 :       gfc_add_block_to_block (&body, &valuese.pre);
    5940              : 
    5941          264 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    5942              :                              arrayse.expr, valuese.expr);
    5943              : 
    5944          264 :       tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
    5945          264 :       if (maskss)
    5946              :         {
    5947              :           /* We enclose the above in if (mask) {...}.  If the mask is
    5948              :              an optional argument, generate IF (.NOT. PRESENT(MASK)
    5949              :              .OR. MASK(I)). */
    5950              : 
    5951           84 :           tree ifmask;
    5952           84 :           ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5953           84 :           tmp = build3_v (COND_EXPR, ifmask, tmp,
    5954              :                           build_empty_stmt (input_location));
    5955              :         }
    5956              : 
    5957          264 :       gfc_add_expr_to_block (&body, tmp);
    5958          264 :       gfc_add_block_to_block (&body, &arrayse.post);
    5959              : 
    5960          264 :       gfc_trans_scalarizing_loops (&loop, &body);
    5961              : 
    5962              :       /* Add the exit label.  */
    5963          264 :       tmp = build1_v (LABEL_EXPR, exit_label);
    5964          264 :       gfc_add_expr_to_block (&loop.pre, tmp);
    5965          264 :       gfc_start_block (&loopblock);
    5966          264 :       gfc_add_block_to_block (&loopblock, &loop.pre);
    5967          264 :       gfc_add_block_to_block (&loopblock, &loop.post);
    5968          264 :       if (i == 0)
    5969          132 :         forward_branch = gfc_finish_block (&loopblock);
    5970              :       else
    5971          132 :         back_branch = gfc_finish_block (&loopblock);
    5972              : 
    5973          264 :       gfc_cleanup_loop (&loop);
    5974              :     }
    5975              : 
    5976              :   /* Enclose the two loops in an IF statement.  */
    5977              : 
    5978          132 :   gfc_init_se (&backse, NULL);
    5979          132 :   gfc_conv_expr_val (&backse, back_arg->expr);
    5980          132 :   gfc_add_block_to_block (&se->pre, &backse.pre);
    5981          132 :   tmp = build3_v (COND_EXPR, backse.expr, forward_branch, back_branch);
    5982              : 
    5983              :   /* For a scalar mask, enclose the loop in an if statement.  */
    5984          132 :   if (maskexpr && maskss == NULL)
    5985              :     {
    5986           30 :       tree ifmask;
    5987           30 :       tree if_stmt;
    5988              : 
    5989           30 :       gfc_init_se (&maskse, NULL);
    5990           30 :       gfc_conv_expr_val (&maskse, maskexpr);
    5991           30 :       gfc_init_block (&block);
    5992           30 :       gfc_add_expr_to_block (&block, maskse.expr);
    5993           30 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    5994           30 :       if_stmt = build3_v (COND_EXPR, ifmask, tmp,
    5995              :                           build_empty_stmt (input_location));
    5996           30 :       gfc_add_expr_to_block (&block, if_stmt);
    5997           30 :       tmp = gfc_finish_block (&block);
    5998              :     }
    5999              : 
    6000          132 :   gfc_add_expr_to_block (&se->pre, tmp);
    6001          132 :   se->expr = convert (type, resvar);
    6002              : 
    6003              : }
    6004              : 
    6005              : /* Emit code for fstat, lstat and stat intrinsic subroutines.  */
    6006              : 
    6007              : static tree
    6008           55 : conv_intrinsic_fstat_lstat_stat_sub (gfc_code *code)
    6009              : {
    6010           55 :   stmtblock_t block;
    6011           55 :   gfc_se se, se_stat;
    6012           55 :   tree unit = NULL_TREE;
    6013           55 :   tree name = NULL_TREE;
    6014           55 :   tree slen = NULL_TREE;
    6015           55 :   tree vals;
    6016           55 :   tree arg3 = NULL_TREE;
    6017           55 :   tree stat = NULL_TREE ;
    6018           55 :   tree present = NULL_TREE;
    6019           55 :   tree tmp;
    6020           55 :   int kind;
    6021              : 
    6022           55 :   gfc_init_block (&block);
    6023           55 :   gfc_init_se (&se, NULL);
    6024              : 
    6025           55 :   switch (code->resolved_isym->id)
    6026              :     {
    6027           21 :     case GFC_ISYM_FSTAT:
    6028              :       /* Deal with the UNIT argument.  */
    6029           21 :       gfc_conv_expr (&se, code->ext.actual->expr);
    6030           21 :       gfc_add_block_to_block (&block, &se.pre);
    6031           21 :       unit = gfc_evaluate_now (se.expr, &block);
    6032           21 :       unit = gfc_build_addr_expr (NULL_TREE, unit);
    6033           21 :       gfc_add_block_to_block (&block, &se.post);
    6034           21 :       break;
    6035              : 
    6036           34 :     case GFC_ISYM_LSTAT:
    6037           34 :     case GFC_ISYM_STAT:
    6038              :       /* Deal with the NAME argument.  */
    6039           34 :       gfc_conv_expr (&se, code->ext.actual->expr);
    6040           34 :       gfc_conv_string_parameter (&se);
    6041           34 :       gfc_add_block_to_block (&block, &se.pre);
    6042           34 :       name = se.expr;
    6043           34 :       slen = se.string_length;
    6044           34 :       gfc_add_block_to_block (&block, &se.post);
    6045           34 :       break;
    6046              : 
    6047            0 :     default:
    6048            0 :       gcc_unreachable ();
    6049              :     }
    6050              : 
    6051              :   /* Deal with the VALUES argument.  */
    6052           55 :   gfc_init_se (&se, NULL);
    6053           55 :   gfc_conv_expr_descriptor (&se, code->ext.actual->next->expr);
    6054           55 :   vals = gfc_build_addr_expr (NULL_TREE, se.expr);
    6055           55 :   gfc_add_block_to_block (&block, &se.pre);
    6056           55 :   gfc_add_block_to_block (&block, &se.post);
    6057           55 :   kind = code->ext.actual->next->expr->ts.kind;
    6058              : 
    6059              :   /* Deal with an optional STATUS.  */
    6060           55 :   if (code->ext.actual->next->next->expr)
    6061              :     {
    6062           45 :       gfc_init_se (&se_stat, NULL);
    6063           45 :       gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
    6064           45 :       stat = gfc_create_var (gfc_get_int_type (kind), "_stat");
    6065           45 :       arg3 = gfc_build_addr_expr (NULL_TREE, stat);
    6066              : 
    6067              :       /* Handle case of status being an optional dummy.  */
    6068           45 :       gfc_symbol *sym = code->ext.actual->next->next->expr->symtree->n.sym;
    6069           45 :       if (sym->attr.dummy && sym->attr.optional)
    6070              :         {
    6071            6 :           present = gfc_conv_expr_present (sym);
    6072           12 :           arg3 = fold_build3_loc (input_location, COND_EXPR,
    6073            6 :                                   TREE_TYPE (arg3), present, arg3,
    6074            6 :                                   fold_convert (TREE_TYPE (arg3),
    6075              :                                                 null_pointer_node));
    6076              :         }
    6077              :     }
    6078              : 
    6079              :   /* Call library function depending on KIND of VALUES argument.  */
    6080           55 :   switch (code->resolved_isym->id)
    6081              :     {
    6082           21 :     case GFC_ISYM_FSTAT:
    6083           21 :       tmp = (kind == 4 ? gfor_fndecl_fstat_i4_sub : gfor_fndecl_fstat_i8_sub);
    6084              :       break;
    6085           14 :     case GFC_ISYM_LSTAT:
    6086           14 :       tmp = (kind == 4 ? gfor_fndecl_lstat_i4_sub : gfor_fndecl_lstat_i8_sub);
    6087              :       break;
    6088           20 :     case GFC_ISYM_STAT:
    6089           20 :       tmp = (kind == 4 ? gfor_fndecl_stat_i4_sub : gfor_fndecl_stat_i8_sub);
    6090              :       break;
    6091            0 :     default:
    6092            0 :       gcc_unreachable ();
    6093              :     }
    6094              : 
    6095           55 :   if (code->resolved_isym->id == GFC_ISYM_FSTAT)
    6096           21 :     tmp = build_call_expr_loc (input_location, tmp, 3, unit, vals,
    6097              :                                stat ? arg3 : null_pointer_node);
    6098              :   else
    6099           34 :     tmp = build_call_expr_loc (input_location, tmp, 4, name, vals,
    6100              :                                stat ? arg3 : null_pointer_node, slen);
    6101           55 :   gfc_add_expr_to_block (&block, tmp);
    6102              : 
    6103              :   /* Handle kind conversion of status.  */
    6104           55 :   if (stat && stat != se_stat.expr)
    6105              :     {
    6106           45 :       stmtblock_t block2;
    6107              : 
    6108           45 :       gfc_init_block (&block2);
    6109           45 :       gfc_add_modify (&block2, se_stat.expr,
    6110           45 :                       fold_convert (TREE_TYPE (se_stat.expr), stat));
    6111              : 
    6112           45 :       if (present)
    6113              :         {
    6114            6 :           tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block2),
    6115              :                           build_empty_stmt (input_location));
    6116            6 :           gfc_add_expr_to_block (&block, tmp);
    6117              :         }
    6118              :       else
    6119           39 :         gfc_add_block_to_block (&block, &block2);
    6120              :     }
    6121              : 
    6122           55 :   return gfc_finish_block (&block);
    6123              : }
    6124              : 
    6125              : /* Emit code for minval or maxval intrinsic.  There are many different cases
    6126              :    we need to handle.  For performance reasons we sometimes create two
    6127              :    loops instead of one, where the second one is much simpler.
    6128              :    Examples for minval intrinsic:
    6129              :    1) Result is an array, a call is generated
    6130              :    2) Array mask is used and NaNs need to be supported, rank 1:
    6131              :       limit = Infinity;
    6132              :       nonempty = false;
    6133              :       S = from;
    6134              :       while (S <= to) {
    6135              :         if (mask[S]) {
    6136              :           nonempty = true;
    6137              :           if (a[S] <= limit) {
    6138              :             limit = a[S];
    6139              :             S++;
    6140              :             goto lab;
    6141              :           }
    6142              :         else
    6143              :           S++;
    6144              :         }
    6145              :       }
    6146              :       limit = nonempty ? NaN : huge (limit);
    6147              :       lab:
    6148              :       while (S <= to) { if(mask[S]) limit = min (a[S], limit); S++; }
    6149              :    3) NaNs need to be supported, but it is known at compile time or cheaply
    6150              :       at runtime whether array is nonempty or not, rank 1:
    6151              :       limit = Infinity;
    6152              :       S = from;
    6153              :       while (S <= to) {
    6154              :         if (a[S] <= limit) {
    6155              :           limit = a[S];
    6156              :           S++;
    6157              :           goto lab;
    6158              :           }
    6159              :         else
    6160              :           S++;
    6161              :       }
    6162              :       limit = (from <= to) ? NaN : huge (limit);
    6163              :       lab:
    6164              :       while (S <= to) { limit = min (a[S], limit); S++; }
    6165              :    4) Array mask is used and NaNs need to be supported, rank > 1:
    6166              :       limit = Infinity;
    6167              :       nonempty = false;
    6168              :       fast = false;
    6169              :       S1 = from1;
    6170              :       while (S1 <= to1) {
    6171              :         S2 = from2;
    6172              :         while (S2 <= to2) {
    6173              :           if (mask[S1][S2]) {
    6174              :             if (fast) limit = min (a[S1][S2], limit);
    6175              :             else {
    6176              :               nonempty = true;
    6177              :               if (a[S1][S2] <= limit) {
    6178              :                 limit = a[S1][S2];
    6179              :                 fast = true;
    6180              :               }
    6181              :             }
    6182              :           }
    6183              :           S2++;
    6184              :         }
    6185              :         S1++;
    6186              :       }
    6187              :       if (!fast)
    6188              :         limit = nonempty ? NaN : huge (limit);
    6189              :    5) NaNs need to be supported, but it is known at compile time or cheaply
    6190              :       at runtime whether array is nonempty or not, rank > 1:
    6191              :       limit = Infinity;
    6192              :       fast = false;
    6193              :       S1 = from1;
    6194              :       while (S1 <= to1) {
    6195              :         S2 = from2;
    6196              :         while (S2 <= to2) {
    6197              :           if (fast) limit = min (a[S1][S2], limit);
    6198              :           else {
    6199              :             if (a[S1][S2] <= limit) {
    6200              :               limit = a[S1][S2];
    6201              :               fast = true;
    6202              :             }
    6203              :           }
    6204              :           S2++;
    6205              :         }
    6206              :         S1++;
    6207              :       }
    6208              :       if (!fast)
    6209              :         limit = (nonempty_array) ? NaN : huge (limit);
    6210              :    6) NaNs aren't supported, but infinities are.  Array mask is used:
    6211              :       limit = Infinity;
    6212              :       nonempty = false;
    6213              :       S = from;
    6214              :       while (S <= to) {
    6215              :         if (mask[S]) { nonempty = true; limit = min (a[S], limit); }
    6216              :         S++;
    6217              :       }
    6218              :       limit = nonempty ? limit : huge (limit);
    6219              :    7) Same without array mask:
    6220              :       limit = Infinity;
    6221              :       S = from;
    6222              :       while (S <= to) { limit = min (a[S], limit); S++; }
    6223              :       limit = (from <= to) ? limit : huge (limit);
    6224              :    8) Neither NaNs nor infinities are supported (-ffast-math or BT_INTEGER):
    6225              :       limit = huge (limit);
    6226              :       S = from;
    6227              :       while (S <= to) { limit = min (a[S], limit); S++); }
    6228              :       (or
    6229              :       while (S <= to) { if (mask[S]) limit = min (a[S], limit); S++; }
    6230              :       with array mask instead).
    6231              :    For 3), 5), 7) and 8), if mask is scalar, this all goes into a conditional,
    6232              :    setting limit = huge (limit); in the else branch.  */
    6233              : 
    6234              : static void
    6235         2417 : gfc_conv_intrinsic_minmaxval (gfc_se * se, gfc_expr * expr, enum tree_code op)
    6236              : {
    6237         2417 :   tree limit;
    6238         2417 :   tree type;
    6239         2417 :   tree tmp;
    6240         2417 :   tree ifbody;
    6241         2417 :   tree nonempty;
    6242         2417 :   tree nonempty_var;
    6243         2417 :   tree lab;
    6244         2417 :   tree fast;
    6245         2417 :   tree huge_cst = NULL, nan_cst = NULL;
    6246         2417 :   stmtblock_t body;
    6247         2417 :   stmtblock_t block, block2;
    6248         2417 :   gfc_loopinfo loop;
    6249         2417 :   gfc_actual_arglist *actual;
    6250         2417 :   gfc_ss *arrayss;
    6251         2417 :   gfc_ss *maskss;
    6252         2417 :   gfc_se arrayse;
    6253         2417 :   gfc_se maskse;
    6254         2417 :   gfc_expr *arrayexpr;
    6255         2417 :   gfc_expr *maskexpr;
    6256         2417 :   int n;
    6257         2417 :   bool optional_mask;
    6258              : 
    6259         2417 :   if (se->ss)
    6260              :     {
    6261            0 :       gfc_conv_intrinsic_funcall (se, expr);
    6262          186 :       return;
    6263              :     }
    6264              : 
    6265         2417 :   actual = expr->value.function.actual;
    6266         2417 :   arrayexpr = actual->expr;
    6267              : 
    6268         2417 :   if (arrayexpr->ts.type == BT_CHARACTER)
    6269              :     {
    6270          186 :       gfc_actual_arglist *dim = actual->next;
    6271          186 :       if (expr->rank == 0 && dim->expr != 0)
    6272              :         {
    6273            6 :           gfc_free_expr (dim->expr);
    6274            6 :           dim->expr = NULL;
    6275              :         }
    6276          186 :       gfc_conv_intrinsic_funcall (se, expr);
    6277          186 :       return;
    6278              :     }
    6279              : 
    6280         2231 :   type = gfc_typenode_for_spec (&expr->ts);
    6281              :   /* Initialize the result.  */
    6282         2231 :   limit = gfc_create_var (type, "limit");
    6283         2231 :   n = gfc_validate_kind (expr->ts.type, expr->ts.kind, false);
    6284         2231 :   switch (expr->ts.type)
    6285              :     {
    6286         1245 :     case BT_REAL:
    6287         1245 :       huge_cst = gfc_conv_mpfr_to_tree (gfc_real_kinds[n].huge,
    6288              :                                         expr->ts.kind, 0);
    6289         1245 :       if (HONOR_INFINITIES (DECL_MODE (limit)))
    6290              :         {
    6291         1241 :           REAL_VALUE_TYPE real;
    6292         1241 :           real_inf (&real);
    6293         1241 :           tmp = build_real (type, real);
    6294              :         }
    6295              :       else
    6296              :         tmp = huge_cst;
    6297         1245 :       if (HONOR_NANS (DECL_MODE (limit)))
    6298         1241 :         nan_cst = gfc_build_nan (type, "");
    6299              :       break;
    6300              : 
    6301          956 :     case BT_INTEGER:
    6302          956 :       tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge, expr->ts.kind);
    6303          956 :       break;
    6304              : 
    6305           30 :     case BT_UNSIGNED:
    6306              :       /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE().  */
    6307           30 :       if (op == GT_EXPR)
    6308           18 :         tmp = build_int_cst (type, 0);
    6309              :       else
    6310           12 :         tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
    6311              :                                              expr->ts.kind);
    6312              :       break;
    6313              : 
    6314            0 :     default:
    6315            0 :       gcc_unreachable ();
    6316              :     }
    6317              : 
    6318              :   /* We start with the most negative possible value for MAXVAL, and the most
    6319              :      positive possible value for MINVAL. The most negative possible value is
    6320              :      -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
    6321              :      possible value is HUGE in both cases.   BT_UNSIGNED has already been dealt
    6322              :      with above.  */
    6323         2231 :   if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
    6324              :     {
    6325          987 :       tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
    6326          987 :       if (huge_cst)
    6327          560 :         huge_cst = fold_build1_loc (input_location, NEGATE_EXPR,
    6328          560 :                                     TREE_TYPE (huge_cst), huge_cst);
    6329              :     }
    6330              : 
    6331         1005 :   if (op == GT_EXPR && expr->ts.type == BT_INTEGER)
    6332          427 :     tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
    6333              :                            tmp, build_int_cst (type, 1));
    6334              : 
    6335         2231 :   gfc_add_modify (&se->pre, limit, tmp);
    6336              : 
    6337              :   /* Walk the arguments.  */
    6338         2231 :   arrayss = gfc_walk_expr (arrayexpr);
    6339         2231 :   gcc_assert (arrayss != gfc_ss_terminator);
    6340              : 
    6341         2231 :   actual = actual->next->next;
    6342         2231 :   gcc_assert (actual);
    6343         2231 :   maskexpr = actual->expr;
    6344         1572 :   optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
    6345         1560 :     && maskexpr->symtree->n.sym->attr.dummy
    6346         2243 :     && maskexpr->symtree->n.sym->attr.optional;
    6347         2777 :   nonempty = NULL;
    6348         1572 :   if (maskexpr && maskexpr->rank != 0)
    6349              :     {
    6350         1026 :       maskss = gfc_walk_expr (maskexpr);
    6351         1026 :       gcc_assert (maskss != gfc_ss_terminator);
    6352              :     }
    6353              :   else
    6354              :     {
    6355         1205 :       mpz_t asize;
    6356         1205 :       if (gfc_array_size (arrayexpr, &asize))
    6357              :         {
    6358          678 :           nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
    6359          678 :           mpz_clear (asize);
    6360          678 :           nonempty = fold_build2_loc (input_location, GT_EXPR,
    6361              :                                       logical_type_node, nonempty,
    6362              :                                       gfc_index_zero_node);
    6363              :         }
    6364         1205 :       maskss = NULL;
    6365              :     }
    6366              : 
    6367              :   /* Initialize the scalarizer.  */
    6368         2231 :   gfc_init_loopinfo (&loop);
    6369              : 
    6370              :   /* We add the mask first because the number of iterations is taken
    6371              :      from the last ss, and this breaks if an absent optional argument
    6372              :      is used for mask.  */
    6373              : 
    6374         2231 :   if (maskss)
    6375         1026 :     gfc_add_ss_to_loop (&loop, maskss);
    6376         2231 :   gfc_add_ss_to_loop (&loop, arrayss);
    6377              : 
    6378              :   /* Initialize the loop.  */
    6379         2231 :   gfc_conv_ss_startstride (&loop);
    6380              : 
    6381              :   /* The code generated can have more than one loop in sequence (see the
    6382              :      comment at the function header).  This doesn't work well with the
    6383              :      scalarizer, which changes arrays' offset when the scalarization loops
    6384              :      are generated (see gfc_trans_preloop_setup).  Fortunately, {min,max}val
    6385              :      are  currently inlined in the scalar case only.  As there is no dependency
    6386              :      to care about in that case, there is no temporary, so that we can use the
    6387              :      scalarizer temporary code to handle multiple loops.  Thus, we set temp_dim
    6388              :      here, we call gfc_mark_ss_chain_used with flag=3 later, and we use
    6389              :      gfc_trans_scalarized_loop_boundary even later to restore offset.
    6390              :      TODO: this prevents inlining of rank > 0 minmaxval calls, so this
    6391              :      should eventually go away.  We could either create two loops properly,
    6392              :      or find another way to save/restore the array offsets between the two
    6393              :      loops (without conflicting with temporary management), or use a single
    6394              :      loop minmaxval implementation.  See PR 31067.  */
    6395         2231 :   loop.temp_dim = loop.dimen;
    6396         2231 :   gfc_conv_loop_setup (&loop, &expr->where);
    6397              : 
    6398         2231 :   if (nonempty == NULL && maskss == NULL
    6399          527 :       && loop.dimen == 1 && loop.from[0] && loop.to[0])
    6400          491 :     nonempty = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    6401              :                                 loop.from[0], loop.to[0]);
    6402         2231 :   nonempty_var = NULL;
    6403         2231 :   if (nonempty == NULL
    6404         2231 :       && (HONOR_INFINITIES (DECL_MODE (limit))
    6405          480 :           || HONOR_NANS (DECL_MODE (limit))))
    6406              :     {
    6407          582 :       nonempty_var = gfc_create_var (logical_type_node, "nonempty");
    6408          582 :       gfc_add_modify (&se->pre, nonempty_var, logical_false_node);
    6409          582 :       nonempty = nonempty_var;
    6410              :     }
    6411         2231 :   lab = NULL;
    6412         2231 :   fast = NULL;
    6413         2231 :   if (HONOR_NANS (DECL_MODE (limit)))
    6414              :     {
    6415         1241 :       if (loop.dimen == 1)
    6416              :         {
    6417          821 :           lab = gfc_build_label_decl (NULL_TREE);
    6418          821 :           TREE_USED (lab) = 1;
    6419              :         }
    6420              :       else
    6421              :         {
    6422          420 :           fast = gfc_create_var (logical_type_node, "fast");
    6423          420 :           gfc_add_modify (&se->pre, fast, logical_false_node);
    6424              :         }
    6425              :     }
    6426              : 
    6427         2231 :   gfc_mark_ss_chain_used (arrayss, lab ? 3 : 1);
    6428         2231 :   if (maskss)
    6429         1704 :     gfc_mark_ss_chain_used (maskss, lab ? 3 : 1);
    6430              :   /* Generate the loop body.  */
    6431         2231 :   gfc_start_scalarized_body (&loop, &body);
    6432              : 
    6433              :   /* If we have a mask, only add this element if the mask is set.  */
    6434         2231 :   if (maskss)
    6435              :     {
    6436         1026 :       gfc_init_se (&maskse, NULL);
    6437         1026 :       gfc_copy_loopinfo_to_se (&maskse, &loop);
    6438         1026 :       maskse.ss = maskss;
    6439         1026 :       gfc_conv_expr_val (&maskse, maskexpr);
    6440         1026 :       gfc_add_block_to_block (&body, &maskse.pre);
    6441              : 
    6442         1026 :       gfc_start_block (&block);
    6443              :     }
    6444              :   else
    6445         1205 :     gfc_init_block (&block);
    6446              : 
    6447              :   /* Compare with the current limit.  */
    6448         2231 :   gfc_init_se (&arrayse, NULL);
    6449         2231 :   gfc_copy_loopinfo_to_se (&arrayse, &loop);
    6450         2231 :   arrayse.ss = arrayss;
    6451         2231 :   gfc_conv_expr_val (&arrayse, arrayexpr);
    6452         2231 :   arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
    6453         2231 :   gfc_add_block_to_block (&block, &arrayse.pre);
    6454              : 
    6455         2231 :   gfc_init_block (&block2);
    6456              : 
    6457         2231 :   if (nonempty_var)
    6458          582 :     gfc_add_modify (&block2, nonempty_var, logical_true_node);
    6459              : 
    6460         2231 :   if (HONOR_NANS (DECL_MODE (limit)))
    6461              :     {
    6462         1922 :       tmp = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
    6463              :                              logical_type_node, arrayse.expr, limit);
    6464         1241 :       if (lab)
    6465              :         {
    6466          821 :           stmtblock_t ifblock;
    6467          821 :           tree inc_loop;
    6468          821 :           inc_loop = fold_build2_loc (input_location, PLUS_EXPR,
    6469          821 :                                       TREE_TYPE (loop.loopvar[0]),
    6470              :                                       loop.loopvar[0], gfc_index_one_node);
    6471          821 :           gfc_init_block (&ifblock);
    6472          821 :           gfc_add_modify (&ifblock, limit, arrayse.expr);
    6473          821 :           gfc_add_modify (&ifblock, loop.loopvar[0], inc_loop);
    6474          821 :           gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab));
    6475          821 :           ifbody = gfc_finish_block (&ifblock);
    6476              :         }
    6477              :       else
    6478              :         {
    6479          420 :           stmtblock_t ifblock;
    6480              : 
    6481          420 :           gfc_init_block (&ifblock);
    6482          420 :           gfc_add_modify (&ifblock, limit, arrayse.expr);
    6483          420 :           gfc_add_modify (&ifblock, fast, logical_true_node);
    6484          420 :           ifbody = gfc_finish_block (&ifblock);
    6485              :         }
    6486         1241 :       tmp = build3_v (COND_EXPR, tmp, ifbody,
    6487              :                       build_empty_stmt (input_location));
    6488         1241 :       gfc_add_expr_to_block (&block2, tmp);
    6489              :     }
    6490              :   else
    6491              :     {
    6492              :       /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
    6493              :          signed zeros.  */
    6494         1535 :       tmp = fold_build2_loc (input_location,
    6495              :                              op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
    6496              :                              type, arrayse.expr, limit);
    6497          990 :       gfc_add_modify (&block2, limit, tmp);
    6498              :     }
    6499              : 
    6500         2231 :   if (fast)
    6501              :     {
    6502          420 :       tree elsebody = gfc_finish_block (&block2);
    6503              : 
    6504              :       /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
    6505              :          signed zeros.  */
    6506          420 :       if (HONOR_NANS (DECL_MODE (limit)))
    6507              :         {
    6508          420 :           tmp = fold_build2_loc (input_location, op, logical_type_node,
    6509              :                                  arrayse.expr, limit);
    6510          420 :           ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
    6511          420 :           ifbody = build3_v (COND_EXPR, tmp, ifbody,
    6512              :                              build_empty_stmt (input_location));
    6513              :         }
    6514              :       else
    6515              :         {
    6516            0 :           tmp = fold_build2_loc (input_location,
    6517              :                                  op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
    6518              :                                  type, arrayse.expr, limit);
    6519            0 :           ifbody = build2_v (MODIFY_EXPR, limit, tmp);
    6520              :         }
    6521          420 :       tmp = build3_v (COND_EXPR, fast, ifbody, elsebody);
    6522          420 :       gfc_add_expr_to_block (&block, tmp);
    6523              :     }
    6524              :   else
    6525         1811 :     gfc_add_block_to_block (&block, &block2);
    6526              : 
    6527         2231 :   gfc_add_block_to_block (&block, &arrayse.post);
    6528              : 
    6529         2231 :   tmp = gfc_finish_block (&block);
    6530         2231 :   if (maskss)
    6531              :     {
    6532              :       /* We enclose the above in if (mask) {...}.  If the mask is an
    6533              :          optional argument, generate IF (.NOT. PRESENT(MASK)
    6534              :          .OR. MASK(I)).  */
    6535         1026 :       tree ifmask;
    6536         1026 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    6537         1026 :       tmp = build3_v (COND_EXPR, ifmask, tmp,
    6538              :                       build_empty_stmt (input_location));
    6539              :     }
    6540         2231 :   gfc_add_expr_to_block (&body, tmp);
    6541              : 
    6542         2231 :   if (lab)
    6543              :     {
    6544          821 :       gfc_trans_scalarized_loop_boundary (&loop, &body);
    6545              : 
    6546          821 :       tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
    6547              :                              nan_cst, huge_cst);
    6548          821 :       gfc_add_modify (&loop.code[0], limit, tmp);
    6549          821 :       gfc_add_expr_to_block (&loop.code[0], build1_v (LABEL_EXPR, lab));
    6550              : 
    6551              :       /* If we have a mask, only add this element if the mask is set.  */
    6552          821 :       if (maskss)
    6553              :         {
    6554          348 :           gfc_init_se (&maskse, NULL);
    6555          348 :           gfc_copy_loopinfo_to_se (&maskse, &loop);
    6556          348 :           maskse.ss = maskss;
    6557          348 :           gfc_conv_expr_val (&maskse, maskexpr);
    6558          348 :           gfc_add_block_to_block (&body, &maskse.pre);
    6559              : 
    6560          348 :           gfc_start_block (&block);
    6561              :         }
    6562              :       else
    6563          473 :         gfc_init_block (&block);
    6564              : 
    6565              :       /* Compare with the current limit.  */
    6566          821 :       gfc_init_se (&arrayse, NULL);
    6567          821 :       gfc_copy_loopinfo_to_se (&arrayse, &loop);
    6568          821 :       arrayse.ss = arrayss;
    6569          821 :       gfc_conv_expr_val (&arrayse, arrayexpr);
    6570          821 :       arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
    6571          821 :       gfc_add_block_to_block (&block, &arrayse.pre);
    6572              : 
    6573              :       /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
    6574              :          signed zeros.  */
    6575          821 :       if (HONOR_NANS (DECL_MODE (limit)))
    6576              :         {
    6577          821 :           tmp = fold_build2_loc (input_location, op, logical_type_node,
    6578              :                                  arrayse.expr, limit);
    6579          821 :           ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
    6580          821 :           tmp = build3_v (COND_EXPR, tmp, ifbody,
    6581              :                           build_empty_stmt (input_location));
    6582          821 :           gfc_add_expr_to_block (&block, tmp);
    6583              :         }
    6584              :       else
    6585              :         {
    6586            0 :           tmp = fold_build2_loc (input_location,
    6587              :                                  op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
    6588              :                                  type, arrayse.expr, limit);
    6589            0 :           gfc_add_modify (&block, limit, tmp);
    6590              :         }
    6591              : 
    6592          821 :       gfc_add_block_to_block (&block, &arrayse.post);
    6593              : 
    6594          821 :       tmp = gfc_finish_block (&block);
    6595          821 :       if (maskss)
    6596              :         /* We enclose the above in if (mask) {...}.  */
    6597              :         {
    6598          348 :           tree ifmask;
    6599          348 :           ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    6600          348 :           tmp = build3_v (COND_EXPR, ifmask, tmp,
    6601              :                           build_empty_stmt (input_location));
    6602              :         }
    6603              : 
    6604          821 :       gfc_add_expr_to_block (&body, tmp);
    6605              :       /* Avoid initializing loopvar[0] again, it should be left where
    6606              :          it finished by the first loop.  */
    6607          821 :       loop.from[0] = loop.loopvar[0];
    6608              :     }
    6609         2231 :   gfc_trans_scalarizing_loops (&loop, &body);
    6610              : 
    6611         2231 :   if (fast)
    6612              :     {
    6613          420 :       tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
    6614              :                              nan_cst, huge_cst);
    6615          420 :       ifbody = build2_v (MODIFY_EXPR, limit, tmp);
    6616          420 :       tmp = build3_v (COND_EXPR, fast, build_empty_stmt (input_location),
    6617              :                       ifbody);
    6618          420 :       gfc_add_expr_to_block (&loop.pre, tmp);
    6619              :     }
    6620         1811 :   else if (HONOR_INFINITIES (DECL_MODE (limit)) && !lab)
    6621              :     {
    6622            0 :       tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty, limit,
    6623              :                              huge_cst);
    6624            0 :       gfc_add_modify (&loop.pre, limit, tmp);
    6625              :     }
    6626              : 
    6627              :   /* For a scalar mask, enclose the loop in an if statement.  */
    6628         2231 :   if (maskexpr && maskss == NULL)
    6629              :     {
    6630          546 :       tree else_stmt;
    6631          546 :       tree ifmask;
    6632              : 
    6633          546 :       gfc_init_se (&maskse, NULL);
    6634          546 :       gfc_conv_expr_val (&maskse, maskexpr);
    6635          546 :       gfc_init_block (&block);
    6636          546 :       gfc_add_block_to_block (&block, &loop.pre);
    6637          546 :       gfc_add_block_to_block (&block, &loop.post);
    6638          546 :       tmp = gfc_finish_block (&block);
    6639              : 
    6640          546 :       if (HONOR_INFINITIES (DECL_MODE (limit)))
    6641          354 :         else_stmt = build2_v (MODIFY_EXPR, limit, huge_cst);
    6642              :       else
    6643          192 :         else_stmt = build_empty_stmt (input_location);
    6644              : 
    6645          546 :       ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
    6646          546 :       tmp = build3_v (COND_EXPR, ifmask, tmp, else_stmt);
    6647          546 :       gfc_add_expr_to_block (&block, tmp);
    6648          546 :       gfc_add_block_to_block (&se->pre, &block);
    6649              :     }
    6650              :   else
    6651              :     {
    6652         1685 :       gfc_add_block_to_block (&se->pre, &loop.pre);
    6653         1685 :       gfc_add_block_to_block (&se->pre, &loop.post);
    6654              :     }
    6655              : 
    6656         2231 :   gfc_cleanup_loop (&loop);
    6657              : 
    6658         2231 :   se->expr = limit;
    6659              : }
    6660              : 
    6661              : /* BTEST (i, pos) = (i & (1 << pos)) != 0.  */
    6662              : static void
    6663          145 : gfc_conv_intrinsic_btest (gfc_se * se, gfc_expr * expr)
    6664              : {
    6665          145 :   tree args[2];
    6666          145 :   tree type;
    6667          145 :   tree tmp;
    6668              : 
    6669          145 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    6670          145 :   type = TREE_TYPE (args[0]);
    6671              : 
    6672              :   /* Optionally generate code for runtime argument check.  */
    6673          145 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    6674              :     {
    6675            6 :       tree below = fold_build2_loc (input_location, LT_EXPR,
    6676              :                                     logical_type_node, args[1],
    6677            6 :                                     build_int_cst (TREE_TYPE (args[1]), 0));
    6678            6 :       tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
    6679            6 :       tree above = fold_build2_loc (input_location, GE_EXPR,
    6680              :                                     logical_type_node, args[1], nbits);
    6681            6 :       tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    6682              :                                     logical_type_node, below, above);
    6683            6 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    6684              :                                "POS argument (%ld) out of range 0:%ld "
    6685              :                                "in intrinsic BTEST",
    6686              :                                fold_convert (long_integer_type_node, args[1]),
    6687              :                                fold_convert (long_integer_type_node, nbits));
    6688              :     }
    6689              : 
    6690          145 :   tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    6691              :                          build_int_cst (type, 1), args[1]);
    6692          145 :   tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], tmp);
    6693          145 :   tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
    6694              :                          build_int_cst (type, 0));
    6695          145 :   type = gfc_typenode_for_spec (&expr->ts);
    6696          145 :   se->expr = convert (type, tmp);
    6697          145 : }
    6698              : 
    6699              : 
    6700              : /* Generate code for BGE, BGT, BLE and BLT intrinsics.  */
    6701              : static void
    6702          216 : gfc_conv_intrinsic_bitcomp (gfc_se * se, gfc_expr * expr, enum tree_code op)
    6703              : {
    6704          216 :   tree args[2];
    6705              : 
    6706          216 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    6707              : 
    6708              :   /* Convert both arguments to the unsigned type of the same size.  */
    6709          216 :   args[0] = fold_convert (unsigned_type_for (TREE_TYPE (args[0])), args[0]);
    6710          216 :   args[1] = fold_convert (unsigned_type_for (TREE_TYPE (args[1])), args[1]);
    6711              : 
    6712              :   /* If they have unequal type size, convert to the larger one.  */
    6713          216 :   if (TYPE_PRECISION (TREE_TYPE (args[0]))
    6714          216 :       > TYPE_PRECISION (TREE_TYPE (args[1])))
    6715            0 :     args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
    6716          216 :   else if (TYPE_PRECISION (TREE_TYPE (args[1]))
    6717          216 :            > TYPE_PRECISION (TREE_TYPE (args[0])))
    6718            0 :     args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
    6719              : 
    6720              :   /* Now, we compare them.  */
    6721          216 :   se->expr = fold_build2_loc (input_location, op, logical_type_node,
    6722              :                               args[0], args[1]);
    6723          216 : }
    6724              : 
    6725              : 
    6726              : /* Generate code to perform the specified operation.  */
    6727              : static void
    6728         1915 : gfc_conv_intrinsic_bitop (gfc_se * se, gfc_expr * expr, enum tree_code op)
    6729              : {
    6730         1915 :   tree args[2];
    6731              : 
    6732         1915 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    6733         1915 :   se->expr = fold_build2_loc (input_location, op, TREE_TYPE (args[0]),
    6734              :                               args[0], args[1]);
    6735         1915 : }
    6736              : 
    6737              : /* Bitwise not.  */
    6738              : static void
    6739          230 : gfc_conv_intrinsic_not (gfc_se * se, gfc_expr * expr)
    6740              : {
    6741          230 :   tree arg;
    6742              : 
    6743          230 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    6744          230 :   se->expr = fold_build1_loc (input_location, BIT_NOT_EXPR,
    6745          230 :                               TREE_TYPE (arg), arg);
    6746          230 : }
    6747              : 
    6748              : 
    6749              : /* Generate code for OUT_OF_RANGE.  */
    6750              : static void
    6751          468 : gfc_conv_intrinsic_out_of_range (gfc_se * se, gfc_expr * expr)
    6752              : {
    6753          468 :   tree *args;
    6754          468 :   tree type;
    6755          468 :   tree tmp = NULL_TREE, tmp1, tmp2;
    6756          468 :   unsigned int num_args;
    6757          468 :   int k;
    6758          468 :   gfc_se rnd_se;
    6759          468 :   gfc_actual_arglist *arg = expr->value.function.actual;
    6760          468 :   gfc_expr *x = arg->expr;
    6761          468 :   gfc_expr *mold = arg->next->expr;
    6762              : 
    6763          468 :   num_args = gfc_intrinsic_argument_list_length (expr);
    6764          468 :   args = XALLOCAVEC (tree, num_args);
    6765              : 
    6766          468 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    6767              : 
    6768          468 :   gfc_init_se (&rnd_se, NULL);
    6769              : 
    6770          468 :   if (num_args == 3)
    6771              :     {
    6772              :       /* The ROUND argument is optional and shall appear only if X is
    6773              :          of type real and MOLD is of type integer (see edit F23/004).  */
    6774          270 :       gfc_expr *round = arg->next->next->expr;
    6775          270 :       gfc_conv_expr (&rnd_se, round);
    6776              : 
    6777          270 :       if (round->expr_type == EXPR_VARIABLE
    6778          198 :           && round->symtree->n.sym->attr.dummy
    6779           30 :           && round->symtree->n.sym->attr.optional)
    6780              :         {
    6781           30 :           tree present = gfc_conv_expr_present (round->symtree->n.sym);
    6782           30 :           rnd_se.expr = build3_loc (input_location, COND_EXPR,
    6783              :                                     logical_type_node, present,
    6784              :                                     rnd_se.expr, logical_false_node);
    6785           30 :           gfc_add_block_to_block (&se->pre, &rnd_se.pre);
    6786              :         }
    6787              :     }
    6788              :   else
    6789              :     {
    6790              :       /* If ROUND is absent, it is equivalent to having the value false.  */
    6791          198 :       rnd_se.expr = logical_false_node;
    6792              :     }
    6793              : 
    6794          468 :   type = TREE_TYPE (args[0]);
    6795          468 :   k = gfc_validate_kind (mold->ts.type, mold->ts.kind, false);
    6796              : 
    6797          468 :   switch (x->ts.type)
    6798              :     {
    6799          378 :     case BT_REAL:
    6800              :       /* X may be IEEE infinity or NaN, but the representation of MOLD may not
    6801              :          support infinity or NaN.  */
    6802          378 :       tree finite;
    6803          378 :       finite = build_call_expr_loc (input_location,
    6804              :                                     builtin_decl_explicit (BUILT_IN_ISFINITE),
    6805              :                                     1,  args[0]);
    6806          378 :       finite = convert (logical_type_node, finite);
    6807              : 
    6808          378 :       if (mold->ts.type == BT_REAL)
    6809              :         {
    6810           24 :           tmp1 = build1 (ABS_EXPR, type, args[0]);
    6811           24 :           tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
    6812              :                                         mold->ts.kind, 0);
    6813           24 :           tmp = build2 (GT_EXPR, logical_type_node, tmp1,
    6814              :                         convert (type, tmp2));
    6815              : 
    6816              :           /* Check if MOLD representation supports infinity or NaN.  */
    6817           24 :           bool infnan = (HONOR_INFINITIES (TREE_TYPE (args[1]))
    6818           24 :                          || HONOR_NANS (TREE_TYPE (args[1])));
    6819           24 :           tmp = build3 (COND_EXPR, logical_type_node, finite, tmp,
    6820              :                         infnan ? logical_false_node : logical_true_node);
    6821              :         }
    6822              :       else
    6823              :         {
    6824          354 :           tree rounded;
    6825          354 :           tree decl;
    6826              : 
    6827          354 :           decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, x->ts.kind);
    6828          354 :           gcc_assert (decl != NULL_TREE);
    6829              : 
    6830              :           /* Round or truncate argument X, depending on the optional argument
    6831              :              ROUND (default: .false.).  */
    6832          354 :           tmp1 = build_round_expr (args[0], type);
    6833          354 :           tmp2 = build_call_expr_loc (input_location, decl, 1, args[0]);
    6834          354 :           rounded = build3 (COND_EXPR, type, rnd_se.expr, tmp1, tmp2);
    6835              : 
    6836          354 :           if (mold->ts.type == BT_INTEGER)
    6837              :             {
    6838          180 :               tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
    6839              :                                            x->ts.kind);
    6840          180 :               tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
    6841              :                                            x->ts.kind);
    6842              :             }
    6843          174 :           else if (mold->ts.type == BT_UNSIGNED)
    6844              :             {
    6845          174 :               tmp1 = build_real_from_int_cst (type, integer_zero_node);
    6846          174 :               tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
    6847              :                                            x->ts.kind);
    6848              :             }
    6849              :           else
    6850            0 :             gcc_unreachable ();
    6851              : 
    6852          354 :           tmp1 = build2 (LT_EXPR, logical_type_node, rounded,
    6853              :                          convert (type, tmp1));
    6854          354 :           tmp2 = build2 (GT_EXPR, logical_type_node, rounded,
    6855              :                          convert (type, tmp2));
    6856          354 :           tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
    6857          354 :           tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node,
    6858              :                         build1 (TRUTH_NOT_EXPR, logical_type_node, finite),
    6859              :                         tmp);
    6860              :         }
    6861              :       break;
    6862              : 
    6863           48 :     case BT_INTEGER:
    6864           48 :       if (mold->ts.type == BT_INTEGER)
    6865              :         {
    6866           12 :           tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
    6867              :                                        x->ts.kind);
    6868           12 :           tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
    6869              :                                        x->ts.kind);
    6870           12 :           tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
    6871              :                          convert (type, tmp1));
    6872           12 :           tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
    6873              :                          convert (type, tmp2));
    6874           12 :           tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
    6875              :         }
    6876           36 :       else if (mold->ts.type == BT_UNSIGNED)
    6877              :         {
    6878           36 :           int i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
    6879           36 :           tmp = build_int_cst (type, 0);
    6880           36 :           tmp = build2 (LT_EXPR, logical_type_node, args[0], tmp);
    6881           36 :           if (mpz_cmp (gfc_integer_kinds[i].huge,
    6882           36 :                        gfc_unsigned_kinds[k].huge) > 0)
    6883              :             {
    6884            0 :               tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
    6885              :                                            x->ts.kind);
    6886            0 :               tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
    6887              :                              convert (type, tmp2));
    6888            0 :               tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp, tmp2);
    6889              :             }
    6890              :         }
    6891            0 :       else if (mold->ts.type == BT_REAL)
    6892              :         {
    6893            0 :           tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
    6894              :                                         mold->ts.kind, 0);
    6895            0 :           tmp1 = build1 (NEGATE_EXPR, TREE_TYPE (tmp2), tmp2);
    6896            0 :           tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
    6897              :                          convert (type, tmp1));
    6898            0 :           tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
    6899              :                          convert (type, tmp2));
    6900            0 :           tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
    6901              :         }
    6902              :       else
    6903            0 :         gcc_unreachable ();
    6904              :       break;
    6905              : 
    6906           42 :     case BT_UNSIGNED:
    6907           42 :       if (mold->ts.type == BT_UNSIGNED)
    6908              :         {
    6909           12 :           tmp = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
    6910              :                                       x->ts.kind);
    6911           12 :           tmp = build2 (GT_EXPR, logical_type_node, args[0],
    6912              :                         convert (type, tmp));
    6913              :         }
    6914           30 :       else if (mold->ts.type == BT_INTEGER)
    6915              :         {
    6916           18 :           tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
    6917              :                                       x->ts.kind);
    6918           18 :           tmp = build2 (GT_EXPR, logical_type_node, args[0],
    6919              :                         convert (type, tmp));
    6920              :         }
    6921           12 :       else if (mold->ts.type == BT_REAL)
    6922              :         {
    6923           12 :           tmp = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
    6924              :                                        mold->ts.kind, 0);
    6925           12 :           tmp = build2 (GT_EXPR, logical_type_node, args[0],
    6926              :                         convert (type, tmp));
    6927              :         }
    6928              :       else
    6929            0 :         gcc_unreachable ();
    6930              :       break;
    6931              : 
    6932            0 :     default:
    6933            0 :       gcc_unreachable ();
    6934              :     }
    6935              : 
    6936          468 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
    6937          468 : }
    6938              : 
    6939              : 
    6940              : /* Set or clear a single bit.  */
    6941              : static void
    6942          306 : gfc_conv_intrinsic_singlebitop (gfc_se * se, gfc_expr * expr, int set)
    6943              : {
    6944          306 :   tree args[2];
    6945          306 :   tree type;
    6946          306 :   tree tmp;
    6947          306 :   enum tree_code op;
    6948              : 
    6949          306 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    6950          306 :   type = TREE_TYPE (args[0]);
    6951              : 
    6952              :   /* Optionally generate code for runtime argument check.  */
    6953          306 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    6954              :     {
    6955           12 :       tree below = fold_build2_loc (input_location, LT_EXPR,
    6956              :                                     logical_type_node, args[1],
    6957           12 :                                     build_int_cst (TREE_TYPE (args[1]), 0));
    6958           12 :       tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
    6959           12 :       tree above = fold_build2_loc (input_location, GE_EXPR,
    6960              :                                     logical_type_node, args[1], nbits);
    6961           12 :       tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    6962              :                                     logical_type_node, below, above);
    6963           12 :       size_t len_name = strlen (expr->value.function.isym->name);
    6964           12 :       char *name = XALLOCAVEC (char, len_name + 1);
    6965           72 :       for (size_t i = 0; i < len_name; i++)
    6966           60 :         name[i] = TOUPPER (expr->value.function.isym->name[i]);
    6967           12 :       name[len_name] = '\0';
    6968           12 :       tree iname = gfc_build_addr_expr (pchar_type_node,
    6969              :                                         gfc_build_cstring_const (name));
    6970           12 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    6971              :                                "POS argument (%ld) out of range 0:%ld "
    6972              :                                "in intrinsic %s",
    6973              :                                fold_convert (long_integer_type_node, args[1]),
    6974              :                                fold_convert (long_integer_type_node, nbits),
    6975              :                                iname);
    6976              :     }
    6977              : 
    6978          306 :   tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    6979              :                          build_int_cst (type, 1), args[1]);
    6980          306 :   if (set)
    6981              :     op = BIT_IOR_EXPR;
    6982              :   else
    6983              :     {
    6984          168 :       op = BIT_AND_EXPR;
    6985          168 :       tmp = fold_build1_loc (input_location, BIT_NOT_EXPR, type, tmp);
    6986              :     }
    6987          306 :   se->expr = fold_build2_loc (input_location, op, type, args[0], tmp);
    6988          306 : }
    6989              : 
    6990              : /* Extract a sequence of bits.
    6991              :     IBITS(I, POS, LEN) = (I >> POS) & ~((~0) << LEN).  */
    6992              : static void
    6993           27 : gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr)
    6994              : {
    6995           27 :   tree args[3];
    6996           27 :   tree type;
    6997           27 :   tree tmp;
    6998           27 :   tree mask;
    6999           27 :   tree num_bits, cond;
    7000              : 
    7001           27 :   gfc_conv_intrinsic_function_args (se, expr, args, 3);
    7002           27 :   type = TREE_TYPE (args[0]);
    7003              : 
    7004              :   /* Optionally generate code for runtime argument check.  */
    7005           27 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    7006              :     {
    7007           12 :       tree tmp1 = fold_convert (long_integer_type_node, args[1]);
    7008           12 :       tree tmp2 = fold_convert (long_integer_type_node, args[2]);
    7009           12 :       tree nbits = build_int_cst (long_integer_type_node,
    7010           12 :                                   TYPE_PRECISION (type));
    7011           12 :       tree below = fold_build2_loc (input_location, LT_EXPR,
    7012              :                                     logical_type_node, args[1],
    7013           12 :                                     build_int_cst (TREE_TYPE (args[1]), 0));
    7014           12 :       tree above = fold_build2_loc (input_location, GT_EXPR,
    7015              :                                     logical_type_node, tmp1, nbits);
    7016           12 :       tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    7017              :                                     logical_type_node, below, above);
    7018           12 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    7019              :                                "POS argument (%ld) out of range 0:%ld "
    7020              :                                "in intrinsic IBITS", tmp1, nbits);
    7021           12 :       below = fold_build2_loc (input_location, LT_EXPR,
    7022              :                                logical_type_node, args[2],
    7023           12 :                                build_int_cst (TREE_TYPE (args[2]), 0));
    7024           12 :       above = fold_build2_loc (input_location, GT_EXPR,
    7025              :                                logical_type_node, tmp2, nbits);
    7026           12 :       scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    7027              :                                logical_type_node, below, above);
    7028           12 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    7029              :                                "LEN argument (%ld) out of range 0:%ld "
    7030              :                                "in intrinsic IBITS", tmp2, nbits);
    7031           12 :       above = fold_build2_loc (input_location, PLUS_EXPR,
    7032              :                                long_integer_type_node, tmp1, tmp2);
    7033           12 :       scond = fold_build2_loc (input_location, GT_EXPR,
    7034              :                                logical_type_node, above, nbits);
    7035           12 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    7036              :                                "POS(%ld)+LEN(%ld)>BIT_SIZE(%ld) "
    7037              :                                "in intrinsic IBITS", tmp1, tmp2, nbits);
    7038              :     }
    7039              : 
    7040              :   /* The Fortran standard allows (shift width) LEN <= BIT_SIZE(I), whereas
    7041              :      gcc requires a shift width < BIT_SIZE(I), so we have to catch this
    7042              :      special case.  See also gfc_conv_intrinsic_ishft ().  */
    7043           27 :   num_bits = build_int_cst (TREE_TYPE (args[2]), TYPE_PRECISION (type));
    7044              : 
    7045           27 :   mask = build_int_cst (type, -1);
    7046           27 :   mask = fold_build2_loc (input_location, LSHIFT_EXPR, type, mask, args[2]);
    7047           27 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[2],
    7048              :                           num_bits);
    7049           27 :   mask = fold_build3_loc (input_location, COND_EXPR, type, cond,
    7050              :                           build_int_cst (type, 0), mask);
    7051           27 :   mask = fold_build1_loc (input_location, BIT_NOT_EXPR, type, mask);
    7052              : 
    7053           27 :   tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, args[0], args[1]);
    7054              : 
    7055           27 :   se->expr = fold_build2_loc (input_location, BIT_AND_EXPR, type, tmp, mask);
    7056           27 : }
    7057              : 
    7058              : static void
    7059          492 : gfc_conv_intrinsic_shift (gfc_se * se, gfc_expr * expr, bool right_shift,
    7060              :                           bool arithmetic)
    7061              : {
    7062          492 :   tree args[2], type, num_bits, cond;
    7063          492 :   tree bigshift;
    7064          492 :   bool do_convert = false;
    7065              : 
    7066          492 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    7067              : 
    7068          492 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    7069          492 :   args[1] = gfc_evaluate_now (args[1], &se->pre);
    7070          492 :   type = TREE_TYPE (args[0]);
    7071              : 
    7072          492 :   if (!arithmetic)
    7073              :     {
    7074          390 :       args[0] = fold_convert (unsigned_type_for (type), args[0]);
    7075          390 :       do_convert = true;
    7076              :     }
    7077              :   else
    7078          102 :     gcc_assert (right_shift);
    7079              : 
    7080          492 :   if (flag_unsigned && arithmetic && expr->ts.type == BT_UNSIGNED)
    7081              :     {
    7082           30 :       do_convert = true;
    7083           30 :       args[0] = fold_convert (signed_type_for (type), args[0]);
    7084              :     }
    7085              : 
    7086          816 :   se->expr = fold_build2_loc (input_location,
    7087              :                               right_shift ? RSHIFT_EXPR : LSHIFT_EXPR,
    7088          492 :                               TREE_TYPE (args[0]), args[0], args[1]);
    7089              : 
    7090          492 :   if (do_convert)
    7091          420 :     se->expr = fold_convert (type, se->expr);
    7092              : 
    7093          492 :   if (!arithmetic)
    7094          390 :     bigshift = build_int_cst (type, 0);
    7095              :   else
    7096              :     {
    7097          102 :       tree nonneg = fold_build2_loc (input_location, GE_EXPR,
    7098              :                                      logical_type_node, args[0],
    7099          102 :                                      build_int_cst (TREE_TYPE (args[0]), 0));
    7100          102 :       bigshift = fold_build3_loc (input_location, COND_EXPR, type, nonneg,
    7101              :                                   build_int_cst (type, 0),
    7102              :                                   build_int_cst (type, -1));
    7103              :     }
    7104              : 
    7105              :   /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
    7106              :      gcc requires a shift width < BIT_SIZE(I), so we have to catch this
    7107              :      special case.  */
    7108          492 :   num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
    7109              : 
    7110              :   /* Optionally generate code for runtime argument check.  */
    7111          492 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    7112              :     {
    7113           30 :       tree below = fold_build2_loc (input_location, LT_EXPR,
    7114              :                                     logical_type_node, args[1],
    7115           30 :                                     build_int_cst (TREE_TYPE (args[1]), 0));
    7116           30 :       tree above = fold_build2_loc (input_location, GT_EXPR,
    7117              :                                     logical_type_node, args[1], num_bits);
    7118           30 :       tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    7119              :                                     logical_type_node, below, above);
    7120           30 :       size_t len_name = strlen (expr->value.function.isym->name);
    7121           30 :       char *name = XALLOCAVEC (char, len_name + 1);
    7122          210 :       for (size_t i = 0; i < len_name; i++)
    7123          180 :         name[i] = TOUPPER (expr->value.function.isym->name[i]);
    7124           30 :       name[len_name] = '\0';
    7125           30 :       tree iname = gfc_build_addr_expr (pchar_type_node,
    7126              :                                         gfc_build_cstring_const (name));
    7127           30 :       gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    7128              :                                "SHIFT argument (%ld) out of range 0:%ld "
    7129              :                                "in intrinsic %s",
    7130              :                                fold_convert (long_integer_type_node, args[1]),
    7131              :                                fold_convert (long_integer_type_node, num_bits),
    7132              :                                iname);
    7133              :     }
    7134              : 
    7135          492 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    7136              :                           args[1], num_bits);
    7137              : 
    7138          492 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
    7139              :                               bigshift, se->expr);
    7140          492 : }
    7141              : 
    7142              : /* ISHFT (I, SHIFT) = (abs (shift) >= BIT_SIZE (i))
    7143              :                         ? 0
    7144              :                         : ((shift >= 0) ? i << shift : i >> -shift)
    7145              :    where all shifts are logical shifts.  */
    7146              : static void
    7147          318 : gfc_conv_intrinsic_ishft (gfc_se * se, gfc_expr * expr)
    7148              : {
    7149          318 :   tree args[2];
    7150          318 :   tree type;
    7151          318 :   tree utype;
    7152          318 :   tree tmp;
    7153          318 :   tree width;
    7154          318 :   tree num_bits;
    7155          318 :   tree cond;
    7156          318 :   tree lshift;
    7157          318 :   tree rshift;
    7158              : 
    7159          318 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    7160              : 
    7161          318 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    7162          318 :   args[1] = gfc_evaluate_now (args[1], &se->pre);
    7163              : 
    7164          318 :   type = TREE_TYPE (args[0]);
    7165          318 :   utype = unsigned_type_for (type);
    7166              : 
    7167          318 :   width = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (args[1]),
    7168              :                            args[1]);
    7169              : 
    7170              :   /* Left shift if positive.  */
    7171          318 :   lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type, args[0], width);
    7172              : 
    7173              :   /* Right shift if negative.
    7174              :      We convert to an unsigned type because we want a logical shift.
    7175              :      The standard doesn't define the case of shifting negative
    7176              :      numbers, and we try to be compatible with other compilers, most
    7177              :      notably g77, here.  */
    7178          318 :   rshift = fold_convert (type, fold_build2_loc (input_location, RSHIFT_EXPR,
    7179              :                                     utype, convert (utype, args[0]), width));
    7180              : 
    7181          318 :   tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[1],
    7182          318 :                          build_int_cst (TREE_TYPE (args[1]), 0));
    7183          318 :   tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp, lshift, rshift);
    7184              : 
    7185              :   /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
    7186              :      gcc requires a shift width < BIT_SIZE(I), so we have to catch this
    7187              :      special case.  */
    7188          318 :   num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
    7189              : 
    7190              :   /* Optionally generate code for runtime argument check.  */
    7191          318 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    7192              :     {
    7193           24 :       tree outside = fold_build2_loc (input_location, GT_EXPR,
    7194              :                                     logical_type_node, width, num_bits);
    7195           24 :       gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
    7196              :                                "SHIFT argument (%ld) out of range -%ld:%ld "
    7197              :                                "in intrinsic ISHFT",
    7198              :                                fold_convert (long_integer_type_node, args[1]),
    7199              :                                fold_convert (long_integer_type_node, num_bits),
    7200              :                                fold_convert (long_integer_type_node, num_bits));
    7201              :     }
    7202              : 
    7203          318 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, width,
    7204              :                           num_bits);
    7205          318 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
    7206              :                               build_int_cst (type, 0), tmp);
    7207          318 : }
    7208              : 
    7209              : 
    7210              : /* Circular shift.  AKA rotate or barrel shift.  */
    7211              : 
    7212              : static void
    7213          658 : gfc_conv_intrinsic_ishftc (gfc_se * se, gfc_expr * expr)
    7214              : {
    7215          658 :   tree *args;
    7216          658 :   tree type;
    7217          658 :   tree tmp;
    7218          658 :   tree lrot;
    7219          658 :   tree rrot;
    7220          658 :   tree zero;
    7221          658 :   tree nbits;
    7222          658 :   unsigned int num_args;
    7223              : 
    7224          658 :   num_args = gfc_intrinsic_argument_list_length (expr);
    7225          658 :   args = XALLOCAVEC (tree, num_args);
    7226              : 
    7227          658 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    7228              : 
    7229          658 :   type = TREE_TYPE (args[0]);
    7230          658 :   nbits = build_int_cst (long_integer_type_node, TYPE_PRECISION (type));
    7231              : 
    7232          658 :   if (num_args == 3)
    7233              :     {
    7234          550 :       gfc_expr *size = expr->value.function.actual->next->next->expr;
    7235              : 
    7236              :       /* Use a library function for the 3 parameter version.  */
    7237          550 :       tree int4type = gfc_get_int_type (4);
    7238              : 
    7239              :       /* Treat optional SIZE argument when it is passed as an optional
    7240              :          dummy.  If SIZE is absent, the default value is BIT_SIZE(I).  */
    7241          550 :       if (size->expr_type == EXPR_VARIABLE
    7242          438 :           && size->symtree->n.sym->attr.dummy
    7243           36 :           && size->symtree->n.sym->attr.optional)
    7244              :         {
    7245           36 :           tree type_of_size = TREE_TYPE (args[2]);
    7246           72 :           args[2] = build3_loc (input_location, COND_EXPR, type_of_size,
    7247           36 :                                 gfc_conv_expr_present (size->symtree->n.sym),
    7248              :                                 args[2], fold_convert (type_of_size, nbits));
    7249              :         }
    7250              : 
    7251              :       /* We convert the first argument to at least 4 bytes, and
    7252              :          convert back afterwards.  This removes the need for library
    7253              :          functions for all argument sizes, and function will be
    7254              :          aligned to at least 32 bits, so there's no loss.  */
    7255          550 :       if (expr->ts.kind < 4)
    7256          242 :         args[0] = convert (int4type, args[0]);
    7257              : 
    7258              :       /* Convert the SHIFT and SIZE args to INTEGER*4 otherwise we would
    7259              :          need loads of library  functions.  They cannot have values >
    7260              :          BIT_SIZE (I) so the conversion is safe.  */
    7261          550 :       args[1] = convert (int4type, args[1]);
    7262          550 :       args[2] = convert (int4type, args[2]);
    7263              : 
    7264              :       /* Optionally generate code for runtime argument check.  */
    7265          550 :       if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    7266              :         {
    7267           18 :           tree size = fold_convert (long_integer_type_node, args[2]);
    7268           18 :           tree below = fold_build2_loc (input_location, LE_EXPR,
    7269              :                                         logical_type_node, size,
    7270           18 :                                         build_int_cst (TREE_TYPE (args[1]), 0));
    7271           18 :           tree above = fold_build2_loc (input_location, GT_EXPR,
    7272              :                                         logical_type_node, size, nbits);
    7273           18 :           tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    7274              :                                         logical_type_node, below, above);
    7275           18 :           gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    7276              :                                    "SIZE argument (%ld) out of range 1:%ld "
    7277              :                                    "in intrinsic ISHFTC", size, nbits);
    7278           18 :           tree width = fold_convert (long_integer_type_node, args[1]);
    7279           18 :           width = fold_build1_loc (input_location, ABS_EXPR,
    7280              :                                    long_integer_type_node, width);
    7281           18 :           scond = fold_build2_loc (input_location, GT_EXPR,
    7282              :                                    logical_type_node, width, size);
    7283           18 :           gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
    7284              :                                    "SHIFT argument (%ld) out of range -%ld:%ld "
    7285              :                                    "in intrinsic ISHFTC",
    7286              :                                    fold_convert (long_integer_type_node, args[1]),
    7287              :                                    size, size);
    7288              :         }
    7289              : 
    7290          550 :       switch (expr->ts.kind)
    7291              :         {
    7292          426 :         case 1:
    7293          426 :         case 2:
    7294          426 :         case 4:
    7295          426 :           tmp = gfor_fndecl_math_ishftc4;
    7296          426 :           break;
    7297          124 :         case 8:
    7298          124 :           tmp = gfor_fndecl_math_ishftc8;
    7299          124 :           break;
    7300            0 :         case 16:
    7301            0 :           tmp = gfor_fndecl_math_ishftc16;
    7302            0 :           break;
    7303            0 :         default:
    7304            0 :           gcc_unreachable ();
    7305              :         }
    7306          550 :       se->expr = build_call_expr_loc (input_location,
    7307              :                                       tmp, 3, args[0], args[1], args[2]);
    7308              :       /* Convert the result back to the original type, if we extended
    7309              :          the first argument's width above.  */
    7310          550 :       if (expr->ts.kind < 4)
    7311          242 :         se->expr = convert (type, se->expr);
    7312              : 
    7313              :       return;
    7314              :     }
    7315              : 
    7316              :   /* Evaluate arguments only once.  */
    7317          108 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    7318          108 :   args[1] = gfc_evaluate_now (args[1], &se->pre);
    7319              : 
    7320              :   /* Optionally generate code for runtime argument check.  */
    7321          108 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
    7322              :     {
    7323           12 :       tree width = fold_convert (long_integer_type_node, args[1]);
    7324           12 :       width = fold_build1_loc (input_location, ABS_EXPR,
    7325              :                                long_integer_type_node, width);
    7326           12 :       tree outside = fold_build2_loc (input_location, GT_EXPR,
    7327              :                                       logical_type_node, width, nbits);
    7328           12 :       gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
    7329              :                                "SHIFT argument (%ld) out of range -%ld:%ld "
    7330              :                                "in intrinsic ISHFTC",
    7331              :                                fold_convert (long_integer_type_node, args[1]),
    7332              :                                nbits, nbits);
    7333              :     }
    7334              : 
    7335              :   /* Rotate left if positive.  */
    7336          108 :   lrot = fold_build2_loc (input_location, LROTATE_EXPR, type, args[0], args[1]);
    7337              : 
    7338              :   /* Rotate right if negative.  */
    7339          108 :   tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (args[1]),
    7340              :                          args[1]);
    7341          108 :   rrot = fold_build2_loc (input_location,RROTATE_EXPR, type, args[0], tmp);
    7342              : 
    7343          108 :   zero = build_int_cst (TREE_TYPE (args[1]), 0);
    7344          108 :   tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, args[1],
    7345              :                          zero);
    7346          108 :   rrot = fold_build3_loc (input_location, COND_EXPR, type, tmp, lrot, rrot);
    7347              : 
    7348              :   /* Do nothing if shift == 0.  */
    7349          108 :   tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, args[1],
    7350              :                          zero);
    7351          108 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, args[0],
    7352              :                               rrot);
    7353              : }
    7354              : 
    7355              : 
    7356              : /* LEADZ (i) = (i == 0) ? BIT_SIZE (i)
    7357              :                         : __builtin_clz(i) - (BIT_SIZE('int') - BIT_SIZE(i))
    7358              : 
    7359              :    The conditional expression is necessary because the result of LEADZ(0)
    7360              :    is defined, but the result of __builtin_clz(0) is undefined for most
    7361              :    targets.
    7362              : 
    7363              :    For INTEGER kinds smaller than the C 'int' type, we have to subtract the
    7364              :    difference in bit size between the argument of LEADZ and the C int.  */
    7365              : 
    7366              : static void
    7367          432 : gfc_conv_intrinsic_leadz (gfc_se * se, gfc_expr * expr)
    7368              : {
    7369          432 :   tree arg;
    7370          432 :   tree arg_type;
    7371          432 :   tree cond;
    7372          432 :   tree result_type;
    7373          432 :   tree leadz;
    7374          432 :   tree bit_size;
    7375          432 :   tree tmp;
    7376          432 :   tree func;
    7377          432 :   int s, argsize;
    7378              : 
    7379          432 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7380          432 :   argsize = TYPE_PRECISION (TREE_TYPE (arg));
    7381              : 
    7382              :   /* Which variant of __builtin_clz* should we call?  */
    7383          432 :   if (argsize <= INT_TYPE_SIZE)
    7384              :     {
    7385          291 :       arg_type = unsigned_type_node;
    7386          291 :       func = builtin_decl_explicit (BUILT_IN_CLZ);
    7387              :     }
    7388          141 :   else if (argsize <= LONG_TYPE_SIZE)
    7389              :     {
    7390           93 :       arg_type = long_unsigned_type_node;
    7391           93 :       func = builtin_decl_explicit (BUILT_IN_CLZL);
    7392              :     }
    7393           48 :   else if (argsize <= LONG_LONG_TYPE_SIZE)
    7394              :     {
    7395            0 :       arg_type = long_long_unsigned_type_node;
    7396            0 :       func = builtin_decl_explicit (BUILT_IN_CLZLL);
    7397              :     }
    7398              :   else
    7399              :     {
    7400           48 :       gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
    7401           48 :       arg_type = gfc_build_uint_type (argsize);
    7402           48 :       func = NULL_TREE;
    7403              :     }
    7404              : 
    7405              :   /* Convert the actual argument twice: first, to the unsigned type of the
    7406              :      same size; then, to the proper argument type for the built-in
    7407              :      function.  But the return type is of the default INTEGER kind.  */
    7408          432 :   arg = fold_convert (gfc_build_uint_type (argsize), arg);
    7409          432 :   arg = fold_convert (arg_type, arg);
    7410          432 :   arg = gfc_evaluate_now (arg, &se->pre);
    7411          432 :   result_type = gfc_get_int_type (gfc_default_integer_kind);
    7412              : 
    7413              :   /* Compute LEADZ for the case i .ne. 0.  */
    7414          432 :   if (func)
    7415              :     {
    7416          384 :       s = TYPE_PRECISION (arg_type) - argsize;
    7417          384 :       tmp = fold_convert (result_type,
    7418              :                           build_call_expr_loc (input_location, func,
    7419              :                                                1, arg));
    7420          384 :       leadz = fold_build2_loc (input_location, MINUS_EXPR, result_type,
    7421          384 :                                tmp, build_int_cst (result_type, s));
    7422              :     }
    7423              :   else
    7424              :     {
    7425              :       /* We end up here if the argument type is larger than 'long long'.
    7426              :          We generate this code:
    7427              : 
    7428              :             if (x & (ULL_MAX << ULL_SIZE) != 0)
    7429              :               return clzll ((unsigned long long) (x >> ULLSIZE));
    7430              :             else
    7431              :               return ULL_SIZE + clzll ((unsigned long long) x);
    7432              :          where ULL_MAX is the largest value that a ULL_MAX can hold
    7433              :          (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
    7434              :          is the bit-size of the long long type (64 in this example).  */
    7435           48 :       tree ullsize, ullmax, tmp1, tmp2, btmp;
    7436              : 
    7437           48 :       ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
    7438           48 :       ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
    7439              :                                 long_long_unsigned_type_node,
    7440              :                                 build_int_cst (long_long_unsigned_type_node,
    7441              :                                                0));
    7442              : 
    7443           48 :       cond = fold_build2_loc (input_location, LSHIFT_EXPR, arg_type,
    7444              :                               fold_convert (arg_type, ullmax), ullsize);
    7445           48 :       cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type,
    7446              :                               arg, cond);
    7447           48 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    7448              :                               cond, build_int_cst (arg_type, 0));
    7449              : 
    7450           48 :       tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
    7451              :                               arg, ullsize);
    7452           48 :       tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
    7453           48 :       btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
    7454           48 :       tmp1 = fold_convert (result_type,
    7455              :                            build_call_expr_loc (input_location, btmp, 1, tmp1));
    7456              : 
    7457           48 :       tmp2 = fold_convert (long_long_unsigned_type_node, arg);
    7458           48 :       btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
    7459           48 :       tmp2 = fold_convert (result_type,
    7460              :                            build_call_expr_loc (input_location, btmp, 1, tmp2));
    7461           48 :       tmp2 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
    7462              :                               tmp2, ullsize);
    7463              : 
    7464           48 :       leadz = fold_build3_loc (input_location, COND_EXPR, result_type,
    7465              :                                cond, tmp1, tmp2);
    7466              :     }
    7467              : 
    7468              :   /* Build BIT_SIZE.  */
    7469          432 :   bit_size = build_int_cst (result_type, argsize);
    7470              : 
    7471          432 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7472              :                           arg, build_int_cst (arg_type, 0));
    7473          432 :   se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
    7474              :                               bit_size, leadz);
    7475          432 : }
    7476              : 
    7477              : 
    7478              : /* TRAILZ(i) = (i == 0) ? BIT_SIZE (i) : __builtin_ctz(i)
    7479              : 
    7480              :    The conditional expression is necessary because the result of TRAILZ(0)
    7481              :    is defined, but the result of __builtin_ctz(0) is undefined for most
    7482              :    targets.  */
    7483              : 
    7484              : static void
    7485          444 : gfc_conv_intrinsic_trailz (gfc_se * se, gfc_expr *expr)
    7486              : {
    7487          444 :   tree arg;
    7488          444 :   tree arg_type;
    7489          444 :   tree cond;
    7490          444 :   tree result_type;
    7491          444 :   tree trailz;
    7492          444 :   tree bit_size;
    7493          444 :   tree func;
    7494          444 :   int argsize;
    7495              : 
    7496          444 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7497          444 :   argsize = TYPE_PRECISION (TREE_TYPE (arg));
    7498              : 
    7499              :   /* Which variant of __builtin_ctz* should we call?  */
    7500          444 :   if (argsize <= INT_TYPE_SIZE)
    7501              :     {
    7502          303 :       arg_type = unsigned_type_node;
    7503          303 :       func = builtin_decl_explicit (BUILT_IN_CTZ);
    7504              :     }
    7505          141 :   else if (argsize <= LONG_TYPE_SIZE)
    7506              :     {
    7507           93 :       arg_type = long_unsigned_type_node;
    7508           93 :       func = builtin_decl_explicit (BUILT_IN_CTZL);
    7509              :     }
    7510           48 :   else if (argsize <= LONG_LONG_TYPE_SIZE)
    7511              :     {
    7512            0 :       arg_type = long_long_unsigned_type_node;
    7513            0 :       func = builtin_decl_explicit (BUILT_IN_CTZLL);
    7514              :     }
    7515              :   else
    7516              :     {
    7517           48 :       gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
    7518           48 :       arg_type = gfc_build_uint_type (argsize);
    7519           48 :       func = NULL_TREE;
    7520              :     }
    7521              : 
    7522              :   /* Convert the actual argument twice: first, to the unsigned type of the
    7523              :      same size; then, to the proper argument type for the built-in
    7524              :      function.  But the return type is of the default INTEGER kind.  */
    7525          444 :   arg = fold_convert (gfc_build_uint_type (argsize), arg);
    7526          444 :   arg = fold_convert (arg_type, arg);
    7527          444 :   arg = gfc_evaluate_now (arg, &se->pre);
    7528          444 :   result_type = gfc_get_int_type (gfc_default_integer_kind);
    7529              : 
    7530              :   /* Compute TRAILZ for the case i .ne. 0.  */
    7531          444 :   if (func)
    7532          396 :     trailz = fold_convert (result_type, build_call_expr_loc (input_location,
    7533              :                                                              func, 1, arg));
    7534              :   else
    7535              :     {
    7536              :       /* We end up here if the argument type is larger than 'long long'.
    7537              :          We generate this code:
    7538              : 
    7539              :             if ((x & ULL_MAX) == 0)
    7540              :               return ULL_SIZE + ctzll ((unsigned long long) (x >> ULLSIZE));
    7541              :             else
    7542              :               return ctzll ((unsigned long long) x);
    7543              : 
    7544              :          where ULL_MAX is the largest value that a ULL_MAX can hold
    7545              :          (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
    7546              :          is the bit-size of the long long type (64 in this example).  */
    7547           48 :       tree ullsize, ullmax, tmp1, tmp2, btmp;
    7548              : 
    7549           48 :       ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
    7550           48 :       ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
    7551              :                                 long_long_unsigned_type_node,
    7552              :                                 build_int_cst (long_long_unsigned_type_node, 0));
    7553              : 
    7554           48 :       cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type, arg,
    7555              :                               fold_convert (arg_type, ullmax));
    7556           48 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, cond,
    7557              :                               build_int_cst (arg_type, 0));
    7558              : 
    7559           48 :       tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
    7560              :                               arg, ullsize);
    7561           48 :       tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
    7562           48 :       btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
    7563           48 :       tmp1 = fold_convert (result_type,
    7564              :                            build_call_expr_loc (input_location, btmp, 1, tmp1));
    7565           48 :       tmp1 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
    7566              :                               tmp1, ullsize);
    7567              : 
    7568           48 :       tmp2 = fold_convert (long_long_unsigned_type_node, arg);
    7569           48 :       btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
    7570           48 :       tmp2 = fold_convert (result_type,
    7571              :                            build_call_expr_loc (input_location, btmp, 1, tmp2));
    7572              : 
    7573           48 :       trailz = fold_build3_loc (input_location, COND_EXPR, result_type,
    7574              :                                 cond, tmp1, tmp2);
    7575              :     }
    7576              : 
    7577              :   /* Build BIT_SIZE.  */
    7578          444 :   bit_size = build_int_cst (result_type, argsize);
    7579              : 
    7580          444 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7581              :                           arg, build_int_cst (arg_type, 0));
    7582          444 :   se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
    7583              :                               bit_size, trailz);
    7584          444 : }
    7585              : 
    7586              : /* Using __builtin_popcount for POPCNT and __builtin_parity for POPPAR;
    7587              :    for types larger than "long long", we call the long long built-in for
    7588              :    the lower and higher bits and combine the result.  */
    7589              : 
    7590              : static void
    7591          458 : gfc_conv_intrinsic_popcnt_poppar (gfc_se * se, gfc_expr *expr, int parity)
    7592              : {
    7593          458 :   tree arg;
    7594          458 :   tree arg_type;
    7595          458 :   tree result_type;
    7596          458 :   tree func;
    7597          458 :   int argsize;
    7598              : 
    7599          458 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7600          458 :   argsize = TYPE_PRECISION (TREE_TYPE (arg));
    7601          458 :   result_type = gfc_get_int_type (gfc_default_integer_kind);
    7602              : 
    7603              :   /* Which variant of the builtin should we call?  */
    7604          458 :   if (argsize <= INT_TYPE_SIZE)
    7605              :     {
    7606          324 :       arg_type = unsigned_type_node;
    7607          522 :       func = builtin_decl_explicit (parity
    7608              :                                     ? BUILT_IN_PARITY
    7609              :                                     : BUILT_IN_POPCOUNT);
    7610              :     }
    7611          134 :   else if (argsize <= LONG_TYPE_SIZE)
    7612              :     {
    7613           84 :       arg_type = long_unsigned_type_node;
    7614          126 :       func = builtin_decl_explicit (parity
    7615              :                                     ? BUILT_IN_PARITYL
    7616              :                                     : BUILT_IN_POPCOUNTL);
    7617              :     }
    7618           50 :   else if (argsize <= LONG_LONG_TYPE_SIZE)
    7619              :     {
    7620            0 :       arg_type = long_long_unsigned_type_node;
    7621            0 :       func = builtin_decl_explicit (parity
    7622              :                                     ? BUILT_IN_PARITYLL
    7623              :                                     : BUILT_IN_POPCOUNTLL);
    7624              :     }
    7625              :   else
    7626              :     {
    7627              :       /* Our argument type is larger than 'long long', which mean none
    7628              :          of the POPCOUNT builtins covers it.  We thus call the 'long long'
    7629              :          variant multiple times, and add the results.  */
    7630           50 :       tree utype, arg2, call1, call2;
    7631              : 
    7632              :       /* For now, we only cover the case where argsize is twice as large
    7633              :          as 'long long'.  */
    7634           50 :       gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
    7635              : 
    7636           75 :       func = builtin_decl_explicit (parity
    7637              :                                     ? BUILT_IN_PARITYLL
    7638              :                                     : BUILT_IN_POPCOUNTLL);
    7639              : 
    7640              :       /* Convert it to an integer, and store into a variable.  */
    7641           50 :       utype = gfc_build_uint_type (argsize);
    7642           50 :       arg = fold_convert (utype, arg);
    7643           50 :       arg = gfc_evaluate_now (arg, &se->pre);
    7644              : 
    7645              :       /* Call the builtin twice.  */
    7646           50 :       call1 = build_call_expr_loc (input_location, func, 1,
    7647              :                                    fold_convert (long_long_unsigned_type_node,
    7648              :                                                  arg));
    7649              : 
    7650           50 :       arg2 = fold_build2_loc (input_location, RSHIFT_EXPR, utype, arg,
    7651              :                               build_int_cst (utype, LONG_LONG_TYPE_SIZE));
    7652           50 :       call2 = build_call_expr_loc (input_location, func, 1,
    7653              :                                    fold_convert (long_long_unsigned_type_node,
    7654              :                                                  arg2));
    7655              : 
    7656              :       /* Combine the results.  */
    7657           50 :       if (parity)
    7658           25 :         se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR,
    7659              :                                     integer_type_node, call1, call2);
    7660              :       else
    7661           25 :         se->expr = fold_build2_loc (input_location, PLUS_EXPR,
    7662              :                                     integer_type_node, call1, call2);
    7663              : 
    7664           50 :       se->expr = convert (result_type, se->expr);
    7665           50 :       return;
    7666              :     }
    7667              : 
    7668              :   /* Convert the actual argument twice: first, to the unsigned type of the
    7669              :      same size; then, to the proper argument type for the built-in
    7670              :      function.  */
    7671          408 :   arg = fold_convert (gfc_build_uint_type (argsize), arg);
    7672          408 :   arg = fold_convert (arg_type, arg);
    7673              : 
    7674          408 :   se->expr = fold_convert (result_type,
    7675              :                            build_call_expr_loc (input_location, func, 1, arg));
    7676              : }
    7677              : 
    7678              : 
    7679              : /* Process an intrinsic with unspecified argument-types that has an optional
    7680              :    argument (which could be of type character), e.g. EOSHIFT.  For those, we
    7681              :    need to append the string length of the optional argument if it is not
    7682              :    present and the type is really character.
    7683              :    primary specifies the position (starting at 1) of the non-optional argument
    7684              :    specifying the type and optional gives the position of the optional
    7685              :    argument in the arglist.  */
    7686              : 
    7687              : static void
    7688         5909 : conv_generic_with_optional_char_arg (gfc_se* se, gfc_expr* expr,
    7689              :                                      unsigned primary, unsigned optional)
    7690              : {
    7691         5909 :   gfc_actual_arglist* prim_arg;
    7692         5909 :   gfc_actual_arglist* opt_arg;
    7693         5909 :   unsigned cur_pos;
    7694         5909 :   gfc_actual_arglist* arg;
    7695         5909 :   gfc_symbol* sym;
    7696         5909 :   vec<tree, va_gc> *append_args;
    7697              : 
    7698              :   /* Find the two arguments given as position.  */
    7699         5909 :   cur_pos = 0;
    7700         5909 :   prim_arg = NULL;
    7701         5909 :   opt_arg = NULL;
    7702        17727 :   for (arg = expr->value.function.actual; arg; arg = arg->next)
    7703              :     {
    7704        17727 :       ++cur_pos;
    7705              : 
    7706        17727 :       if (cur_pos == primary)
    7707         5909 :         prim_arg = arg;
    7708        17727 :       if (cur_pos == optional)
    7709         5909 :         opt_arg = arg;
    7710              : 
    7711        17727 :       if (cur_pos >= primary && cur_pos >= optional)
    7712              :         break;
    7713              :     }
    7714         5909 :   gcc_assert (prim_arg);
    7715         5909 :   gcc_assert (prim_arg->expr);
    7716         5909 :   gcc_assert (opt_arg);
    7717              : 
    7718              :   /* If we do have type CHARACTER and the optional argument is really absent,
    7719              :      append a dummy 0 as string length.  */
    7720         5909 :   append_args = NULL;
    7721         5909 :   if (prim_arg->expr->ts.type == BT_CHARACTER && !opt_arg->expr)
    7722              :     {
    7723          608 :       tree dummy;
    7724              : 
    7725          608 :       dummy = build_int_cst (gfc_charlen_type_node, 0);
    7726          608 :       vec_alloc (append_args, 1);
    7727          608 :       append_args->quick_push (dummy);
    7728              :     }
    7729              : 
    7730              :   /* Build the call itself.  */
    7731         5909 :   gcc_assert (!se->ignore_optional);
    7732         5909 :   sym = gfc_get_symbol_for_expr (expr, false);
    7733         5909 :   gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
    7734              :                           append_args);
    7735         5909 :   gfc_free_symbol (sym);
    7736         5909 : }
    7737              : 
    7738              : /* The length of a character string.  */
    7739              : static void
    7740         5906 : gfc_conv_intrinsic_len (gfc_se * se, gfc_expr * expr)
    7741              : {
    7742         5906 :   tree len;
    7743         5906 :   tree type;
    7744         5906 :   tree decl;
    7745         5906 :   gfc_symbol *sym;
    7746         5906 :   gfc_se argse;
    7747         5906 :   gfc_expr *arg;
    7748              : 
    7749         5906 :   gcc_assert (!se->ss);
    7750              : 
    7751         5906 :   arg = expr->value.function.actual->expr;
    7752              : 
    7753         5906 :   type = gfc_typenode_for_spec (&expr->ts);
    7754         5906 :   switch (arg->expr_type)
    7755              :     {
    7756            0 :     case EXPR_CONSTANT:
    7757            0 :       len = build_int_cst (gfc_charlen_type_node, arg->value.character.length);
    7758            0 :       break;
    7759              : 
    7760            2 :     case EXPR_ARRAY:
    7761              :       /* If there is an explicit type-spec, use it.  */
    7762            2 :       if (arg->ts.u.cl->length && arg->ts.u.cl->length_from_typespec)
    7763              :         {
    7764            0 :           gfc_conv_string_length (arg->ts.u.cl, arg, &se->pre);
    7765            0 :           len = arg->ts.u.cl->backend_decl;
    7766            0 :           break;
    7767              :         }
    7768              : 
    7769              :       /* Obtain the string length from the function used by
    7770              :          trans-array.cc(gfc_trans_array_constructor).  */
    7771            2 :       len = NULL_TREE;
    7772            2 :       get_array_ctor_strlen (&se->pre, arg->value.constructor, &len);
    7773            2 :       break;
    7774              : 
    7775         5317 :     case EXPR_VARIABLE:
    7776         5317 :       if (arg->ref == NULL
    7777         2338 :             || (arg->ref->next == NULL && arg->ref->type == REF_ARRAY))
    7778              :         {
    7779              :           /* This doesn't catch all cases.
    7780              :              See http://gcc.gnu.org/ml/fortran/2004-06/msg00165.html
    7781              :              and the surrounding thread.  */
    7782         4868 :           sym = arg->symtree->n.sym;
    7783         4868 :           decl = gfc_get_symbol_decl (sym);
    7784         4868 :           if (decl == current_function_decl && sym->attr.function
    7785           55 :                 && (sym->result == sym))
    7786           55 :             decl = gfc_get_fake_result_decl (sym, 0);
    7787              : 
    7788         4868 :           len = sym->ts.u.cl->backend_decl;
    7789         4868 :           gcc_assert (len);
    7790              :           break;
    7791              :         }
    7792              : 
    7793              :       /* Fall through.  */
    7794              : 
    7795         1036 :     default:
    7796         1036 :       gfc_init_se (&argse, se);
    7797         1036 :       if (arg->rank == 0)
    7798          914 :         gfc_conv_expr (&argse, arg);
    7799              :       else
    7800          122 :         gfc_conv_expr_descriptor (&argse, arg);
    7801         1036 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    7802         1036 :       gfc_add_block_to_block (&se->post, &argse.post);
    7803         1036 :       len = argse.string_length;
    7804         1036 :       break;
    7805              :     }
    7806         5906 :   se->expr = convert (type, len);
    7807         5906 : }
    7808              : 
    7809              : /* The length of a character string not including trailing blanks.  */
    7810              : static void
    7811         2359 : gfc_conv_intrinsic_len_trim (gfc_se * se, gfc_expr * expr)
    7812              : {
    7813         2359 :   int kind = expr->value.function.actual->expr->ts.kind;
    7814         2359 :   tree args[2], type, fndecl;
    7815              : 
    7816         2359 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    7817         2359 :   type = gfc_typenode_for_spec (&expr->ts);
    7818              : 
    7819         2359 :   if (kind == 1)
    7820         1957 :     fndecl = gfor_fndecl_string_len_trim;
    7821          402 :   else if (kind == 4)
    7822          402 :     fndecl = gfor_fndecl_string_len_trim_char4;
    7823              :   else
    7824            0 :     gcc_unreachable ();
    7825              : 
    7826         2359 :   se->expr = build_call_expr_loc (input_location,
    7827              :                               fndecl, 2, args[0], args[1]);
    7828         2359 :   se->expr = convert (type, se->expr);
    7829         2359 : }
    7830              : 
    7831              : 
    7832              : /* Returns the starting position of a substring within a string.  */
    7833              : 
    7834              : static void
    7835          757 : gfc_conv_intrinsic_index_scan_verify (gfc_se * se, gfc_expr * expr,
    7836              :                                       tree function)
    7837              : {
    7838          757 :   tree logical4_type_node = gfc_get_logical_type (4);
    7839          757 :   tree type;
    7840          757 :   tree fndecl;
    7841          757 :   tree *args;
    7842          757 :   unsigned int num_args;
    7843              : 
    7844          757 :   args = XALLOCAVEC (tree, 5);
    7845              : 
    7846              :   /* Get number of arguments; characters count double due to the
    7847              :      string length argument. Kind= is not passed to the library
    7848              :      and thus ignored.  */
    7849          757 :   if (expr->value.function.actual->next->next->expr == NULL)
    7850              :     num_args = 4;
    7851              :   else
    7852          304 :     num_args = 5;
    7853              : 
    7854          757 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    7855          757 :   type = gfc_typenode_for_spec (&expr->ts);
    7856              : 
    7857          757 :   if (num_args == 4)
    7858          453 :     args[4] = build_int_cst (logical4_type_node, 0);
    7859              :   else
    7860          304 :     args[4] = convert (logical4_type_node, args[4]);
    7861              : 
    7862          757 :   fndecl = build_addr (function);
    7863          757 :   se->expr = build_call_array_loc (input_location,
    7864          757 :                                TREE_TYPE (TREE_TYPE (function)), fndecl,
    7865              :                                5, args);
    7866          757 :   se->expr = convert (type, se->expr);
    7867              : 
    7868          757 : }
    7869              : 
    7870              : /* The ascii value for a single character.  */
    7871              : static void
    7872         2033 : gfc_conv_intrinsic_ichar (gfc_se * se, gfc_expr * expr)
    7873              : {
    7874         2033 :   tree args[3], type, pchartype;
    7875         2033 :   int nargs;
    7876              : 
    7877         2033 :   nargs = gfc_intrinsic_argument_list_length (expr);
    7878         2033 :   gfc_conv_intrinsic_function_args (se, expr, args, nargs);
    7879         2033 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (args[1])));
    7880         2033 :   pchartype = gfc_get_pchar_type (expr->value.function.actual->expr->ts.kind);
    7881         2033 :   args[1] = fold_build1_loc (input_location, NOP_EXPR, pchartype, args[1]);
    7882         2033 :   type = gfc_typenode_for_spec (&expr->ts);
    7883              : 
    7884         2033 :   se->expr = build_fold_indirect_ref_loc (input_location,
    7885              :                                       args[1]);
    7886         2033 :   se->expr = convert (type, se->expr);
    7887         2033 : }
    7888              : 
    7889              : 
    7890              : /* Intrinsic ISNAN calls __builtin_isnan.  */
    7891              : 
    7892              : static void
    7893          432 : gfc_conv_intrinsic_isnan (gfc_se * se, gfc_expr * expr)
    7894              : {
    7895          432 :   tree arg;
    7896              : 
    7897          432 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7898          432 :   se->expr = build_call_expr_loc (input_location,
    7899              :                                   builtin_decl_explicit (BUILT_IN_ISNAN),
    7900              :                                   1, arg);
    7901          864 :   STRIP_TYPE_NOPS (se->expr);
    7902          432 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    7903          432 : }
    7904              : 
    7905              : 
    7906              : /* Intrinsics IS_IOSTAT_END and IS_IOSTAT_EOR just need to compare
    7907              :    their argument against a constant integer value.  */
    7908              : 
    7909              : static void
    7910           24 : gfc_conv_has_intvalue (gfc_se * se, gfc_expr * expr, const int value)
    7911              : {
    7912           24 :   tree arg;
    7913              : 
    7914           24 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    7915           24 :   se->expr = fold_build2_loc (input_location, EQ_EXPR,
    7916              :                               gfc_typenode_for_spec (&expr->ts),
    7917           24 :                               arg, build_int_cst (TREE_TYPE (arg), value));
    7918           24 : }
    7919              : 
    7920              : 
    7921              : 
    7922              : /* MERGE (tsource, fsource, mask) = mask ? tsource : fsource.  */
    7923              : 
    7924              : static void
    7925          959 : gfc_conv_intrinsic_merge (gfc_se * se, gfc_expr * expr)
    7926              : {
    7927          959 :   tree tsource;
    7928          959 :   tree fsource;
    7929          959 :   tree mask;
    7930          959 :   tree type;
    7931          959 :   tree len, len2;
    7932          959 :   tree *args;
    7933          959 :   unsigned int num_args;
    7934              : 
    7935          959 :   num_args = gfc_intrinsic_argument_list_length (expr);
    7936          959 :   args = XALLOCAVEC (tree, num_args);
    7937              : 
    7938          959 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    7939          959 :   if (expr->ts.type != BT_CHARACTER)
    7940              :     {
    7941          432 :       tsource = args[0];
    7942          432 :       fsource = args[1];
    7943          432 :       mask = args[2];
    7944              :     }
    7945              :   else
    7946              :     {
    7947              :       /* We do the same as in the non-character case, but the argument
    7948              :          list is different because of the string length arguments. We
    7949              :          also have to set the string length for the result.  */
    7950          527 :       len = args[0];
    7951          527 :       tsource = args[1];
    7952          527 :       len2 = args[2];
    7953          527 :       fsource = args[3];
    7954          527 :       mask = args[4];
    7955              : 
    7956          527 :       gfc_trans_same_strlen_check ("MERGE intrinsic", &expr->where, len, len2,
    7957              :                                    &se->pre);
    7958          527 :       se->string_length = len;
    7959              :     }
    7960          959 :   tsource = gfc_evaluate_now (tsource, &se->pre);
    7961          959 :   fsource = gfc_evaluate_now (fsource, &se->pre);
    7962          959 :   mask = gfc_evaluate_now (mask, &se->pre);
    7963          959 :   type = TREE_TYPE (tsource);
    7964          959 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, mask, tsource,
    7965              :                               fold_convert (type, fsource));
    7966          959 :   se->expr = gfc_evaluate_now (se->expr, &se->pre);
    7967          959 :   if (IS_PDT (expr))
    7968            6 :     copy_pdt_components (se, expr);
    7969          959 : }
    7970              : 
    7971              : 
    7972              : /* MERGE_BITS (I, J, MASK) = (I & MASK) | (I & (~MASK)).  */
    7973              : 
    7974              : static void
    7975           42 : gfc_conv_intrinsic_merge_bits (gfc_se * se, gfc_expr * expr)
    7976              : {
    7977           42 :   tree args[3], mask, type;
    7978              : 
    7979           42 :   gfc_conv_intrinsic_function_args (se, expr, args, 3);
    7980           42 :   mask = gfc_evaluate_now (args[2], &se->pre);
    7981              : 
    7982           42 :   type = TREE_TYPE (args[0]);
    7983           42 :   gcc_assert (TREE_TYPE (args[1]) == type);
    7984           42 :   gcc_assert (TREE_TYPE (mask) == type);
    7985              : 
    7986           42 :   args[0] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], mask);
    7987           42 :   args[1] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[1],
    7988              :                              fold_build1_loc (input_location, BIT_NOT_EXPR,
    7989              :                                               type, mask));
    7990           42 :   se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
    7991              :                               args[0], args[1]);
    7992           42 : }
    7993              : 
    7994              : 
    7995              : /* MASKL(n)  =  n == 0 ? 0 : (~0) << (BIT_SIZE - n)
    7996              :    MASKR(n)  =  n == BIT_SIZE ? ~0 : ~((~0) << n)  */
    7997              : 
    7998              : static void
    7999           64 : gfc_conv_intrinsic_mask (gfc_se * se, gfc_expr * expr, int left)
    8000              : {
    8001           64 :   tree arg, allones, type, utype, res, cond, bitsize;
    8002           64 :   int i;
    8003              : 
    8004           64 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    8005           64 :   arg = gfc_evaluate_now (arg, &se->pre);
    8006              : 
    8007           64 :   type = gfc_get_int_type (expr->ts.kind);
    8008           64 :   utype = unsigned_type_for (type);
    8009              : 
    8010           64 :   i = gfc_validate_kind (BT_INTEGER, expr->ts.kind, false);
    8011           64 :   bitsize = build_int_cst (TREE_TYPE (arg), gfc_integer_kinds[i].bit_size);
    8012              : 
    8013           64 :   allones = fold_build1_loc (input_location, BIT_NOT_EXPR, utype,
    8014              :                              build_int_cst (utype, 0));
    8015              : 
    8016           64 :   if (left)
    8017              :     {
    8018              :       /* Left-justified mask.  */
    8019           32 :       res = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (arg),
    8020              :                              bitsize, arg);
    8021           32 :       res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
    8022              :                              fold_convert (utype, res));
    8023              : 
    8024              :       /* Special case arg == 0, because SHIFT_EXPR wants a shift strictly
    8025              :          smaller than type width.  */
    8026           32 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
    8027           32 :                               build_int_cst (TREE_TYPE (arg), 0));
    8028           32 :       res = fold_build3_loc (input_location, COND_EXPR, utype, cond,
    8029              :                              build_int_cst (utype, 0), res);
    8030              :     }
    8031              :   else
    8032              :     {
    8033              :       /* Right-justified mask.  */
    8034           32 :       res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
    8035              :                              fold_convert (utype, arg));
    8036           32 :       res = fold_build1_loc (input_location, BIT_NOT_EXPR, utype, res);
    8037              : 
    8038              :       /* Special case agr == bit_size, because SHIFT_EXPR wants a shift
    8039              :          strictly smaller than type width.  */
    8040           32 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    8041              :                               arg, bitsize);
    8042           32 :       res = fold_build3_loc (input_location, COND_EXPR, utype,
    8043              :                              cond, allones, res);
    8044              :     }
    8045              : 
    8046           64 :   se->expr = fold_convert (type, res);
    8047           64 : }
    8048              : 
    8049              : 
    8050              : /* FRACTION (s) is translated into:
    8051              :      isfinite (s) ? frexp (s, &dummy_int) : NaN  */
    8052              : static void
    8053           60 : gfc_conv_intrinsic_fraction (gfc_se * se, gfc_expr * expr)
    8054              : {
    8055           60 :   tree arg, type, tmp, res, frexp, cond;
    8056              : 
    8057           60 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
    8058              : 
    8059           60 :   type = gfc_typenode_for_spec (&expr->ts);
    8060           60 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    8061           60 :   arg = gfc_evaluate_now (arg, &se->pre);
    8062              : 
    8063           60 :   cond = build_call_expr_loc (input_location,
    8064              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    8065              :                               1, arg);
    8066              : 
    8067           60 :   tmp = gfc_create_var (integer_type_node, NULL);
    8068           60 :   res = build_call_expr_loc (input_location, frexp, 2,
    8069              :                              fold_convert (type, arg),
    8070              :                              gfc_build_addr_expr (NULL_TREE, tmp));
    8071           60 :   res = fold_convert (type, res);
    8072              : 
    8073           60 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type,
    8074              :                               cond, res, gfc_build_nan (type, ""));
    8075           60 : }
    8076              : 
    8077              : 
    8078              : /* NEAREST (s, dir) is translated into
    8079              :      tmp = copysign (HUGE_VAL, dir);
    8080              :      return nextafter (s, tmp);
    8081              :  */
    8082              : static void
    8083         1595 : gfc_conv_intrinsic_nearest (gfc_se * se, gfc_expr * expr)
    8084              : {
    8085         1595 :   tree args[2], type, tmp, nextafter, copysign, huge_val;
    8086              : 
    8087         1595 :   nextafter = gfc_builtin_decl_for_float_kind (BUILT_IN_NEXTAFTER, expr->ts.kind);
    8088         1595 :   copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
    8089              : 
    8090         1595 :   type = gfc_typenode_for_spec (&expr->ts);
    8091         1595 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    8092              : 
    8093         1595 :   huge_val = gfc_build_inf_or_huge (type, expr->ts.kind);
    8094         1595 :   tmp = build_call_expr_loc (input_location, copysign, 2, huge_val,
    8095              :                              fold_convert (type, args[1]));
    8096         1595 :   se->expr = build_call_expr_loc (input_location, nextafter, 2,
    8097              :                                   fold_convert (type, args[0]), tmp);
    8098         1595 :   se->expr = fold_convert (type, se->expr);
    8099         1595 : }
    8100              : 
    8101              : 
    8102              : /* SPACING (s) is translated into
    8103              :     int e;
    8104              :     if (!isfinite (s))
    8105              :       res = NaN;
    8106              :     else if (s == 0)
    8107              :       res = tiny;
    8108              :     else
    8109              :     {
    8110              :       frexp (s, &e);
    8111              :       e = e - prec;
    8112              :       e = MAX_EXPR (e, emin);
    8113              :       res = scalbn (1., e);
    8114              :     }
    8115              :     return res;
    8116              : 
    8117              :  where prec is the precision of s, gfc_real_kinds[k].digits,
    8118              :        emin is min_exponent - 1, gfc_real_kinds[k].min_exponent - 1,
    8119              :    and tiny is tiny(s), gfc_real_kinds[k].tiny.  */
    8120              : 
    8121              : static void
    8122           70 : gfc_conv_intrinsic_spacing (gfc_se * se, gfc_expr * expr)
    8123              : {
    8124           70 :   tree arg, type, prec, emin, tiny, res, e;
    8125           70 :   tree cond, nan, tmp, frexp, scalbn;
    8126           70 :   int k;
    8127           70 :   stmtblock_t block;
    8128              : 
    8129           70 :   k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
    8130           70 :   prec = build_int_cst (integer_type_node, gfc_real_kinds[k].digits);
    8131           70 :   emin = build_int_cst (integer_type_node, gfc_real_kinds[k].min_exponent - 1);
    8132           70 :   tiny = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].tiny, expr->ts.kind, 0);
    8133              : 
    8134           70 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
    8135           70 :   scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
    8136              : 
    8137           70 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    8138           70 :   arg = gfc_evaluate_now (arg, &se->pre);
    8139              : 
    8140           70 :   type = gfc_typenode_for_spec (&expr->ts);
    8141           70 :   e = gfc_create_var (integer_type_node, NULL);
    8142           70 :   res = gfc_create_var (type, NULL);
    8143              : 
    8144              : 
    8145              :   /* Build the block for s /= 0.  */
    8146           70 :   gfc_start_block (&block);
    8147           70 :   tmp = build_call_expr_loc (input_location, frexp, 2, arg,
    8148              :                              gfc_build_addr_expr (NULL_TREE, e));
    8149           70 :   gfc_add_expr_to_block (&block, tmp);
    8150              : 
    8151           70 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node, e,
    8152              :                          prec);
    8153           70 :   gfc_add_modify (&block, e, fold_build2_loc (input_location, MAX_EXPR,
    8154              :                                               integer_type_node, tmp, emin));
    8155              : 
    8156           70 :   tmp = build_call_expr_loc (input_location, scalbn, 2,
    8157           70 :                          build_real_from_int_cst (type, integer_one_node), e);
    8158           70 :   gfc_add_modify (&block, res, tmp);
    8159              : 
    8160              :   /* Finish by building the IF statement for value zero.  */
    8161           70 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
    8162           70 :                           build_real_from_int_cst (type, integer_zero_node));
    8163           70 :   tmp = build3_v (COND_EXPR, cond, build2_v (MODIFY_EXPR, res, tiny),
    8164              :                   gfc_finish_block (&block));
    8165              : 
    8166              :   /* And deal with infinities and NaNs.  */
    8167           70 :   cond = build_call_expr_loc (input_location,
    8168              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    8169              :                               1, arg);
    8170           70 :   nan = gfc_build_nan (type, "");
    8171           70 :   tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, res, nan));
    8172              : 
    8173           70 :   gfc_add_expr_to_block (&se->pre, tmp);
    8174           70 :   se->expr = res;
    8175           70 : }
    8176              : 
    8177              : 
    8178              : /* RRSPACING (s) is translated into
    8179              :       int e;
    8180              :       real x;
    8181              :       x = fabs (s);
    8182              :       if (isfinite (x))
    8183              :       {
    8184              :         if (x != 0)
    8185              :         {
    8186              :           frexp (s, &e);
    8187              :           x = scalbn (x, precision - e);
    8188              :         }
    8189              :       }
    8190              :       else
    8191              :         x = NaN;
    8192              :       return x;
    8193              : 
    8194              :  where precision is gfc_real_kinds[k].digits.  */
    8195              : 
    8196              : static void
    8197           48 : gfc_conv_intrinsic_rrspacing (gfc_se * se, gfc_expr * expr)
    8198              : {
    8199           48 :   tree arg, type, e, x, cond, nan, stmt, tmp, frexp, scalbn, fabs;
    8200           48 :   int prec, k;
    8201           48 :   stmtblock_t block;
    8202              : 
    8203           48 :   k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
    8204           48 :   prec = gfc_real_kinds[k].digits;
    8205              : 
    8206           48 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
    8207           48 :   scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
    8208           48 :   fabs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
    8209              : 
    8210           48 :   type = gfc_typenode_for_spec (&expr->ts);
    8211           48 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    8212           48 :   arg = gfc_evaluate_now (arg, &se->pre);
    8213              : 
    8214           48 :   e = gfc_create_var (integer_type_node, NULL);
    8215           48 :   x = gfc_create_var (type, NULL);
    8216           48 :   gfc_add_modify (&se->pre, x,
    8217              :                   build_call_expr_loc (input_location, fabs, 1, arg));
    8218              : 
    8219              : 
    8220           48 :   gfc_start_block (&block);
    8221           48 :   tmp = build_call_expr_loc (input_location, frexp, 2, arg,
    8222              :                              gfc_build_addr_expr (NULL_TREE, e));
    8223           48 :   gfc_add_expr_to_block (&block, tmp);
    8224              : 
    8225           48 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
    8226           48 :                          build_int_cst (integer_type_node, prec), e);
    8227           48 :   tmp = build_call_expr_loc (input_location, scalbn, 2, x, tmp);
    8228           48 :   gfc_add_modify (&block, x, tmp);
    8229           48 :   stmt = gfc_finish_block (&block);
    8230              : 
    8231              :   /* if (x != 0) */
    8232           48 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, x,
    8233           48 :                           build_real_from_int_cst (type, integer_zero_node));
    8234           48 :   tmp = build3_v (COND_EXPR, cond, stmt, build_empty_stmt (input_location));
    8235              : 
    8236              :   /* And deal with infinities and NaNs.  */
    8237           48 :   cond = build_call_expr_loc (input_location,
    8238              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    8239              :                               1, x);
    8240           48 :   nan = gfc_build_nan (type, "");
    8241           48 :   tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, x, nan));
    8242              : 
    8243           48 :   gfc_add_expr_to_block (&se->pre, tmp);
    8244           48 :   se->expr = fold_convert (type, x);
    8245           48 : }
    8246              : 
    8247              : 
    8248              : /* SCALE (s, i) is translated into scalbn (s, i).  */
    8249              : static void
    8250           72 : gfc_conv_intrinsic_scale (gfc_se * se, gfc_expr * expr)
    8251              : {
    8252           72 :   tree args[2], type, scalbn;
    8253              : 
    8254           72 :   scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
    8255              : 
    8256           72 :   type = gfc_typenode_for_spec (&expr->ts);
    8257           72 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    8258           72 :   se->expr = build_call_expr_loc (input_location, scalbn, 2,
    8259              :                                   fold_convert (type, args[0]),
    8260              :                                   fold_convert (integer_type_node, args[1]));
    8261           72 :   se->expr = fold_convert (type, se->expr);
    8262           72 : }
    8263              : 
    8264              : 
    8265              : /* SET_EXPONENT (s, i) is translated into
    8266              :    isfinite(s) ? scalbn (frexp (s, &dummy_int), i) : NaN  */
    8267              : static void
    8268          262 : gfc_conv_intrinsic_set_exponent (gfc_se * se, gfc_expr * expr)
    8269              : {
    8270          262 :   tree args[2], type, tmp, frexp, scalbn, cond, nan, res;
    8271              : 
    8272          262 :   frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
    8273          262 :   scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
    8274              : 
    8275          262 :   type = gfc_typenode_for_spec (&expr->ts);
    8276          262 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    8277          262 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
    8278              : 
    8279          262 :   tmp = gfc_create_var (integer_type_node, NULL);
    8280          262 :   tmp = build_call_expr_loc (input_location, frexp, 2,
    8281              :                              fold_convert (type, args[0]),
    8282              :                              gfc_build_addr_expr (NULL_TREE, tmp));
    8283          262 :   res = build_call_expr_loc (input_location, scalbn, 2, tmp,
    8284              :                              fold_convert (integer_type_node, args[1]));
    8285          262 :   res = fold_convert (type, res);
    8286              : 
    8287              :   /* Call to isfinite */
    8288          262 :   cond = build_call_expr_loc (input_location,
    8289              :                               builtin_decl_explicit (BUILT_IN_ISFINITE),
    8290              :                               1, args[0]);
    8291          262 :   nan = gfc_build_nan (type, "");
    8292              : 
    8293          262 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
    8294              :                               res, nan);
    8295          262 : }
    8296              : 
    8297              : 
    8298              : static void
    8299        15969 : gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
    8300              : {
    8301        15969 :   gfc_actual_arglist *actual;
    8302        15969 :   tree arg1;
    8303        15969 :   tree type;
    8304        15969 :   tree size;
    8305        15969 :   gfc_se argse;
    8306        15969 :   gfc_expr *e;
    8307        15969 :   gfc_symbol *sym = NULL;
    8308              : 
    8309        15969 :   gfc_init_se (&argse, NULL);
    8310        15969 :   actual = expr->value.function.actual;
    8311              : 
    8312        15969 :   if (actual->expr->ts.type == BT_CLASS)
    8313          639 :     gfc_add_class_array_ref (actual->expr);
    8314              : 
    8315        15969 :   e = actual->expr;
    8316              : 
    8317              :   /* These are emerging from the interface mapping, when a class valued
    8318              :      function appears as the rhs in a realloc on assign statement, where
    8319              :      the size of the result is that of one of the actual arguments.  */
    8320        15969 :   if (e->expr_type == EXPR_VARIABLE
    8321        15481 :       && e->symtree->n.sym->ns == NULL /* This is distinctive!  */
    8322          573 :       && e->symtree->n.sym->ts.type == BT_CLASS
    8323           62 :       && e->ref && e->ref->type == REF_COMPONENT
    8324           44 :       && strcmp (e->ref->u.c.component->name, "_data") == 0)
    8325        15969 :     sym = e->symtree->n.sym;
    8326              : 
    8327        15969 :   if ((gfc_option.rtcheck & GFC_RTCHECK_POINTER)
    8328              :       && e
    8329          854 :       && (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION))
    8330              :     {
    8331          854 :       symbol_attribute attr;
    8332          854 :       char *msg;
    8333          854 :       tree temp;
    8334          854 :       tree cond;
    8335              : 
    8336          854 :       if (e->symtree->n.sym && IS_CLASS_ARRAY (e->symtree->n.sym))
    8337              :         {
    8338           33 :           attr = CLASS_DATA (e->symtree->n.sym)->attr;
    8339           33 :           attr.pointer = attr.class_pointer;
    8340              :         }
    8341              :       else
    8342          821 :         attr = gfc_expr_attr (e);
    8343              : 
    8344          854 :       if (attr.allocatable)
    8345          100 :         msg = xasprintf ("Allocatable argument '%s' is not allocated",
    8346          100 :                          e->symtree->n.sym->name);
    8347          754 :       else if (attr.pointer)
    8348           46 :         msg = xasprintf ("Pointer argument '%s' is not associated",
    8349           46 :                          e->symtree->n.sym->name);
    8350              :       else
    8351          708 :         goto end_arg_check;
    8352              : 
    8353          146 :       if (sym)
    8354              :         {
    8355            0 :           temp = gfc_class_data_get (sym->backend_decl);
    8356            0 :           temp = gfc_conv_descriptor_data_get (temp);
    8357              :         }
    8358              :       else
    8359              :         {
    8360          146 :           argse.descriptor_only = 1;
    8361          146 :           gfc_conv_expr_descriptor (&argse, actual->expr);
    8362          146 :           temp = gfc_conv_descriptor_data_get (argse.expr);
    8363              :         }
    8364              : 
    8365          146 :       cond = fold_build2_loc (input_location, EQ_EXPR,
    8366              :                               logical_type_node, temp,
    8367          146 :                               fold_convert (TREE_TYPE (temp),
    8368              :                                             null_pointer_node));
    8369          146 :       gfc_trans_runtime_check (true, false, cond, &argse.pre, &e->where, msg);
    8370              : 
    8371          146 :       free (msg);
    8372              :     }
    8373        15115 :  end_arg_check:
    8374              : 
    8375        15969 :   argse.data_not_needed = 1;
    8376        15969 :   if (gfc_is_class_array_function (e))
    8377              :     {
    8378              :       /* For functions that return a class array conv_expr_descriptor is not
    8379              :          able to get the descriptor right.  Therefore this special case.  */
    8380            7 :       gfc_conv_expr_reference (&argse, e);
    8381            7 :       argse.expr = gfc_class_data_get (argse.expr);
    8382              :     }
    8383        15962 :   else if (sym && sym->backend_decl)
    8384              :     {
    8385           32 :       gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (sym->backend_decl)));
    8386           32 :       argse.expr = gfc_class_data_get (sym->backend_decl);
    8387              :     }
    8388              :   else
    8389        15930 :     gfc_conv_expr_descriptor (&argse, actual->expr);
    8390        15969 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8391        15969 :   gfc_add_block_to_block (&se->post, &argse.post);
    8392        15969 :   arg1 = argse.expr;
    8393              : 
    8394        15969 :   actual = actual->next;
    8395        15969 :   if (actual->expr)
    8396              :     {
    8397         9564 :       stmtblock_t block;
    8398         9564 :       gfc_init_block (&block);
    8399         9564 :       gfc_init_se (&argse, NULL);
    8400         9564 :       gfc_conv_expr_type (&argse, actual->expr,
    8401              :                           gfc_array_index_type);
    8402         9564 :       gfc_add_block_to_block (&block, &argse.pre);
    8403         9564 :       tree tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    8404              :                              argse.expr, gfc_index_one_node);
    8405         9564 :       size = gfc_tree_array_size (&block, arg1, e, tmp);
    8406              : 
    8407              :       /* Unusually, for an intrinsic, size does not exclude
    8408              :          an optional arg2, so we must test for it.  */
    8409         9564 :       if (actual->expr->expr_type == EXPR_VARIABLE
    8410         2690 :             && actual->expr->symtree->n.sym->attr.dummy
    8411           31 :             && actual->expr->symtree->n.sym->attr.optional)
    8412              :         {
    8413           31 :           tree cond;
    8414           31 :           stmtblock_t block2;
    8415           31 :           gfc_init_block (&block2);
    8416           31 :           gfc_init_se (&argse, NULL);
    8417           31 :           argse.want_pointer = 1;
    8418           31 :           argse.data_not_needed = 1;
    8419           31 :           gfc_conv_expr (&argse, actual->expr);
    8420           31 :           gfc_add_block_to_block (&se->pre, &argse.pre);
    8421              :           /* 'block2' contains the arg2 absent case, 'block' the arg2 present
    8422              :               case; size_var can be used in both blocks. */
    8423           31 :           tree size_var = gfc_create_var (TREE_TYPE (size), "size");
    8424           31 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    8425           31 :                                  TREE_TYPE (size_var), size_var, size);
    8426           31 :           gfc_add_expr_to_block (&block, tmp);
    8427           31 :           size = gfc_tree_array_size (&block2, arg1, e, NULL_TREE);
    8428           31 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    8429           31 :                                  TREE_TYPE (size_var), size_var, size);
    8430           31 :           gfc_add_expr_to_block (&block2, tmp);
    8431           31 :           cond = gfc_conv_expr_present (actual->expr->symtree->n.sym);
    8432           31 :           tmp = build3_v (COND_EXPR, cond, gfc_finish_block (&block),
    8433              :                           gfc_finish_block (&block2));
    8434           31 :           gfc_add_expr_to_block (&se->pre, tmp);
    8435           31 :           size = size_var;
    8436           31 :         }
    8437              :       else
    8438         9533 :         gfc_add_block_to_block (&se->pre, &block);
    8439              :     }
    8440              :   else
    8441         6405 :     size = gfc_tree_array_size (&se->pre, arg1, e, NULL_TREE);
    8442        15969 :   type = gfc_typenode_for_spec (&expr->ts);
    8443        15969 :   se->expr = convert (type, size);
    8444        15969 : }
    8445              : 
    8446              : 
    8447              : /* Helper function to compute the size of a character variable,
    8448              :    excluding the terminating null characters.  The result has
    8449              :    gfc_array_index_type type.  */
    8450              : 
    8451              : tree
    8452         1996 : size_of_string_in_bytes (int kind, tree string_length)
    8453              : {
    8454         1996 :   tree bytesize;
    8455         1996 :   int i = gfc_validate_kind (BT_CHARACTER, kind, false);
    8456              : 
    8457         3992 :   bytesize = build_int_cst (gfc_array_index_type,
    8458         1996 :                             gfc_character_kinds[i].bit_size / 8);
    8459              : 
    8460         1996 :   return fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    8461              :                           bytesize,
    8462         1996 :                           fold_convert (gfc_array_index_type, string_length));
    8463              : }
    8464              : 
    8465              : 
    8466              : static void
    8467         1315 : gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr)
    8468              : {
    8469         1315 :   gfc_expr *arg;
    8470         1315 :   gfc_se argse;
    8471         1315 :   tree source_bytes;
    8472         1315 :   tree tmp;
    8473         1315 :   tree lower;
    8474         1315 :   tree upper;
    8475         1315 :   tree byte_size;
    8476         1315 :   int n;
    8477              : 
    8478         1315 :   gfc_init_se (&argse, NULL);
    8479         1315 :   arg = expr->value.function.actual->expr;
    8480              : 
    8481         1315 :   if (arg->rank || arg->ts.type == BT_ASSUMED)
    8482         1018 :     gfc_conv_expr_descriptor (&argse, arg);
    8483              :   else
    8484          297 :     gfc_conv_expr_reference (&argse, arg);
    8485              : 
    8486         1315 :   if (arg->ts.type == BT_ASSUMED)
    8487              :     {
    8488              :       /* This only works if an array descriptor has been passed; thus, extract
    8489              :          the size from the descriptor.  */
    8490          172 :       gcc_assert (TYPE_PRECISION (gfc_array_index_type)
    8491              :                   == TYPE_PRECISION (size_type_node));
    8492          172 :       tmp = arg->symtree->n.sym->backend_decl;
    8493          172 :       tmp = DECL_LANG_SPECIFIC (tmp)
    8494           60 :             && GFC_DECL_SAVED_DESCRIPTOR (tmp) != NULL_TREE
    8495          226 :             ? GFC_DECL_SAVED_DESCRIPTOR (tmp) : tmp;
    8496          172 :       if (POINTER_TYPE_P (TREE_TYPE (tmp)))
    8497          172 :         tmp = build_fold_indirect_ref_loc (input_location, tmp);
    8498              : 
    8499          172 :       tmp = gfc_conv_descriptor_elem_len_get (tmp);
    8500              : 
    8501          172 :       byte_size = fold_convert (gfc_array_index_type, tmp);
    8502              :     }
    8503         1143 :   else if (arg->ts.type == BT_CLASS)
    8504              :     {
    8505              :       /* Conv_expr_descriptor returns a component_ref to _data component of the
    8506              :          class object.  The class object may be a non-pointer object, e.g.
    8507              :          located on the stack, or a memory location pointed to, e.g. a
    8508              :          parameter, i.e., an indirect_ref.  */
    8509          971 :       if (POINTER_TYPE_P (TREE_TYPE (argse.expr))
    8510          595 :           && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (argse.expr))))
    8511          198 :         byte_size
    8512          198 :           = gfc_class_vtab_size_get (build_fold_indirect_ref (argse.expr));
    8513          397 :       else if (GFC_CLASS_TYPE_P (TREE_TYPE (argse.expr)))
    8514            0 :         byte_size = gfc_class_vtab_size_get (argse.expr);
    8515          397 :       else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (argse.expr))
    8516          397 :                && TREE_CODE (argse.expr) == COMPONENT_REF)
    8517          334 :         byte_size = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
    8518           63 :       else if (arg->rank > 0
    8519           21 :                || (arg->rank == 0
    8520           21 :                    && arg->ref && arg->ref->type == REF_COMPONENT))
    8521              :         {
    8522              :           /* The scalarizer added an additional temp.  To get the class' vptr
    8523              :              one has to look at the original backend_decl.  */
    8524           63 :           if (argse.class_container)
    8525           21 :             byte_size = gfc_class_vtab_size_get (argse.class_container);
    8526           42 :           else if (DECL_LANG_SPECIFIC (arg->symtree->n.sym->backend_decl))
    8527           84 :             byte_size = gfc_class_vtab_size_get (
    8528           42 :               GFC_DECL_SAVED_DESCRIPTOR (arg->symtree->n.sym->backend_decl));
    8529              :           else
    8530            0 :             gcc_unreachable ();
    8531              :         }
    8532              :       else
    8533            0 :         gcc_unreachable ();
    8534              :     }
    8535              :   else
    8536              :     {
    8537          548 :       if (arg->ts.type == BT_CHARACTER)
    8538           84 :         byte_size = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
    8539              :       else
    8540              :         {
    8541          464 :           if (arg->rank == 0)
    8542            0 :             byte_size = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
    8543              :                                                                 argse.expr));
    8544              :           else
    8545          464 :             byte_size = gfc_get_element_type (TREE_TYPE (argse.expr));
    8546          464 :           byte_size = fold_convert (gfc_array_index_type,
    8547              :                                     size_in_bytes (byte_size));
    8548              :         }
    8549              :     }
    8550              : 
    8551         1315 :   if (arg->rank == 0)
    8552          297 :     se->expr = byte_size;
    8553              :   else
    8554              :     {
    8555         1018 :       source_bytes = gfc_create_var (gfc_array_index_type, "bytes");
    8556         1018 :       gfc_add_modify (&argse.pre, source_bytes, byte_size);
    8557              : 
    8558         1018 :       if (arg->rank == -1)
    8559              :         {
    8560          365 :           tree cond, loop_var, exit_label;
    8561          365 :           stmtblock_t body;
    8562              : 
    8563          365 :           tmp = gfc_conv_descriptor_rank_get (argse.expr);
    8564          365 :           loop_var = gfc_create_var (gfc_array_dim_rank_type, "i");
    8565          365 :           gfc_add_modify (&argse.pre, loop_var, gfc_rank_cst[0]);
    8566          365 :           exit_label = gfc_build_label_decl (NULL_TREE);
    8567              : 
    8568              :           /* Create loop:
    8569              :              for (;;)
    8570              :                 {
    8571              :                   if (i >= rank)
    8572              :                     goto exit;
    8573              :                   source_bytes = source_bytes * array.dim[i].extent;
    8574              :                   i = i + 1;
    8575              :                 }
    8576              :               exit:  */
    8577          365 :           gfc_start_block (&body);
    8578          365 :           cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    8579              :                                   loop_var, tmp);
    8580          365 :           tmp = build1_v (GOTO_EXPR, exit_label);
    8581          365 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    8582              :                                  cond, tmp, build_empty_stmt (input_location));
    8583          365 :           gfc_add_expr_to_block (&body, tmp);
    8584              : 
    8585          365 :           lower = gfc_conv_descriptor_lbound_get (argse.expr, loop_var);
    8586          365 :           upper = gfc_conv_descriptor_ubound_get (argse.expr, loop_var);
    8587          365 :           tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
    8588          365 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    8589              :                                  gfc_array_index_type, tmp, source_bytes);
    8590          365 :           gfc_add_modify (&body, source_bytes, tmp);
    8591              : 
    8592          365 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    8593              :                                  gfc_array_dim_rank_type, loop_var,
    8594              :                                  gfc_rank_cst[1]);
    8595          365 :           gfc_add_modify_loc (input_location, &body, loop_var, tmp);
    8596              : 
    8597          365 :           tmp = gfc_finish_block (&body);
    8598              : 
    8599          365 :           tmp = fold_build1_loc (input_location, LOOP_EXPR, void_type_node,
    8600              :                                  tmp);
    8601          365 :           gfc_add_expr_to_block (&argse.pre, tmp);
    8602              : 
    8603          365 :           tmp = build1_v (LABEL_EXPR, exit_label);
    8604          365 :           gfc_add_expr_to_block (&argse.pre, tmp);
    8605              :         }
    8606              :       else
    8607              :         {
    8608              :           /* Obtain the size of the array in bytes.  */
    8609         1852 :           for (n = 0; n < arg->rank; n++)
    8610              :             {
    8611         1199 :               tree idx;
    8612         1199 :               idx = gfc_rank_cst[n];
    8613         1199 :               lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
    8614         1199 :               upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
    8615         1199 :               tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
    8616         1199 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
    8617              :                                      gfc_array_index_type, tmp, source_bytes);
    8618         1199 :               gfc_add_modify (&argse.pre, source_bytes, tmp);
    8619              :             }
    8620              :         }
    8621         1018 :       se->expr = source_bytes;
    8622              :     }
    8623              : 
    8624         1315 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8625         1315 : }
    8626              : 
    8627              : 
    8628              : static void
    8629          890 : gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
    8630              : {
    8631          890 :   gfc_expr *arg;
    8632          890 :   gfc_se argse;
    8633          890 :   tree type, result_type, tmp, class_decl = NULL;
    8634          890 :   gfc_symbol *sym;
    8635          890 :   bool unlimited = false;
    8636              : 
    8637          890 :   arg = expr->value.function.actual->expr;
    8638              : 
    8639          890 :   gfc_init_se (&argse, NULL);
    8640          890 :   result_type = gfc_get_int_type (expr->ts.kind);
    8641              : 
    8642          890 :   if (arg->rank == 0)
    8643              :     {
    8644          242 :       if (arg->ts.type == BT_CLASS)
    8645              :         {
    8646           92 :           unlimited = UNLIMITED_POLY (arg);
    8647           92 :           gfc_add_vptr_component (arg);
    8648           92 :           gfc_add_size_component (arg);
    8649           92 :           gfc_conv_expr (&argse, arg);
    8650           92 :           tmp = fold_convert (result_type, argse.expr);
    8651           92 :           class_decl = gfc_get_class_from_expr (argse.expr);
    8652           92 :           goto done;
    8653              :         }
    8654              : 
    8655          150 :       gfc_conv_expr_reference (&argse, arg);
    8656          150 :       type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
    8657              :                                                      argse.expr));
    8658              :     }
    8659              :   else
    8660              :     {
    8661          648 :       argse.want_pointer = 0;
    8662          648 :       gfc_conv_expr_descriptor (&argse, arg);
    8663          648 :       sym = arg->expr_type == EXPR_VARIABLE ? arg->symtree->n.sym : NULL;
    8664          648 :       if (arg->ts.type == BT_CLASS)
    8665              :         {
    8666           60 :           unlimited = UNLIMITED_POLY (arg);
    8667           60 :           if (TREE_CODE (argse.expr) == COMPONENT_REF)
    8668           54 :             tmp = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
    8669            6 :           else if (arg->rank > 0 && sym
    8670           12 :                    && DECL_LANG_SPECIFIC (sym->backend_decl))
    8671           12 :             tmp = gfc_class_vtab_size_get (
    8672            6 :                  GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl));
    8673              :           else
    8674            0 :             gcc_unreachable ();
    8675           60 :           tmp = fold_convert (result_type, tmp);
    8676           60 :           class_decl = gfc_get_class_from_expr (argse.expr);
    8677           60 :           goto done;
    8678              :         }
    8679          588 :       type = gfc_get_element_type (TREE_TYPE (argse.expr));
    8680              :     }
    8681              : 
    8682              :   /* Obtain the argument's word length.  */
    8683          738 :   if (arg->ts.type == BT_CHARACTER)
    8684          241 :     tmp = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
    8685              :   else
    8686          497 :     tmp = size_in_bytes (type);
    8687          738 :   tmp = fold_convert (result_type, tmp);
    8688              : 
    8689          890 : done:
    8690          890 :   if (unlimited && class_decl)
    8691           68 :     tmp = gfc_resize_class_size_with_len (NULL, class_decl, tmp);
    8692              : 
    8693          890 :   se->expr = fold_build2_loc (input_location, MULT_EXPR, result_type, tmp,
    8694              :                               build_int_cst (result_type, BITS_PER_UNIT));
    8695          890 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8696          890 : }
    8697              : 
    8698              : 
    8699              : /* Intrinsic string comparison functions.  */
    8700              : 
    8701              : static void
    8702           99 : gfc_conv_intrinsic_strcmp (gfc_se * se, gfc_expr * expr, enum tree_code op)
    8703              : {
    8704           99 :   tree args[4];
    8705              : 
    8706           99 :   gfc_conv_intrinsic_function_args (se, expr, args, 4);
    8707              : 
    8708           99 :   se->expr
    8709          198 :     = gfc_build_compare_string (args[0], args[1], args[2], args[3],
    8710           99 :                                 expr->value.function.actual->expr->ts.kind,
    8711              :                                 op);
    8712           99 :   se->expr = fold_build2_loc (input_location, op,
    8713              :                               gfc_typenode_for_spec (&expr->ts), se->expr,
    8714           99 :                               build_int_cst (TREE_TYPE (se->expr), 0));
    8715           99 : }
    8716              : 
    8717              : /* Generate a call to the adjustl/adjustr library function.  */
    8718              : static void
    8719          468 : gfc_conv_intrinsic_adjust (gfc_se * se, gfc_expr * expr, tree fndecl)
    8720              : {
    8721          468 :   tree args[3];
    8722          468 :   tree len;
    8723          468 :   tree type;
    8724          468 :   tree var;
    8725          468 :   tree tmp;
    8726              : 
    8727          468 :   gfc_conv_intrinsic_function_args (se, expr, &args[1], 2);
    8728          468 :   len = args[1];
    8729              : 
    8730          468 :   type = TREE_TYPE (args[2]);
    8731          468 :   var = gfc_conv_string_tmp (se, type, len);
    8732          468 :   args[0] = var;
    8733              : 
    8734          468 :   tmp = build_call_expr_loc (input_location,
    8735              :                          fndecl, 3, args[0], args[1], args[2]);
    8736          468 :   gfc_add_expr_to_block (&se->pre, tmp);
    8737          468 :   se->expr = var;
    8738          468 :   se->string_length = len;
    8739          468 : }
    8740              : 
    8741              : 
    8742              : /* Generate code for the TRANSFER intrinsic:
    8743              :         For scalar results:
    8744              :           DEST = TRANSFER (SOURCE, MOLD)
    8745              :         where:
    8746              :           typeof<DEST> = typeof<MOLD>
    8747              :         and:
    8748              :           MOLD is scalar.
    8749              : 
    8750              :         For array results:
    8751              :           DEST(1:N) = TRANSFER (SOURCE, MOLD[, SIZE])
    8752              :         where:
    8753              :           typeof<DEST> = typeof<MOLD>
    8754              :         and:
    8755              :           N = min (sizeof (SOURCE(:)), sizeof (DEST(:)),
    8756              :               sizeof (DEST(0) * SIZE).  */
    8757              : static void
    8758         4194 : gfc_conv_intrinsic_transfer (gfc_se * se, gfc_expr * expr)
    8759              : {
    8760         4194 :   tree tmp;
    8761         4194 :   tree tmpdecl;
    8762         4194 :   tree ptr;
    8763         4194 :   tree extent;
    8764         4194 :   tree source;
    8765         4194 :   tree source_type;
    8766         4194 :   tree source_bytes;
    8767         4194 :   tree mold_type;
    8768         4194 :   tree dest_word_len;
    8769         4194 :   tree size_words;
    8770         4194 :   tree size_bytes;
    8771         4194 :   tree upper;
    8772         4194 :   tree lower;
    8773         4194 :   tree stmt;
    8774         4194 :   tree class_ref = NULL_TREE;
    8775         4194 :   gfc_actual_arglist *arg;
    8776         4194 :   gfc_se argse;
    8777         4194 :   gfc_array_info *info;
    8778         4194 :   stmtblock_t block;
    8779         4194 :   int n;
    8780         4194 :   bool scalar_mold;
    8781         4194 :   gfc_expr *source_expr, *mold_expr, *class_expr;
    8782              : 
    8783         4194 :   info = NULL;
    8784         4194 :   if (se->loop)
    8785          532 :     info = &se->ss->info->data.array;
    8786              : 
    8787              :   /* Convert SOURCE.  The output from this stage is:-
    8788              :         source_bytes = length of the source in bytes
    8789              :         source = pointer to the source data.  */
    8790         4194 :   arg = expr->value.function.actual;
    8791         4194 :   source_expr = arg->expr;
    8792              : 
    8793              :   /* Ensure double transfer through LOGICAL preserves all
    8794              :      the needed bits.  */
    8795         4194 :   if (arg->expr->expr_type == EXPR_FUNCTION
    8796         3117 :         && arg->expr->value.function.esym == NULL
    8797         3093 :         && arg->expr->value.function.isym != NULL
    8798         3093 :         && arg->expr->value.function.isym->id == GFC_ISYM_TRANSFER
    8799           12 :         && arg->expr->ts.type == BT_LOGICAL
    8800           12 :         && expr->ts.type != arg->expr->ts.type)
    8801           12 :     arg->expr->value.function.name = "__transfer_in_transfer";
    8802              : 
    8803         4194 :   gfc_init_se (&argse, NULL);
    8804              : 
    8805         4194 :   source_bytes = gfc_create_var (gfc_array_index_type, NULL);
    8806              : 
    8807              :   /* Obtain the pointer to source and the length of source in bytes.  */
    8808         4194 :   if (arg->expr->rank == 0)
    8809              :     {
    8810         3820 :       gfc_conv_expr_reference (&argse, arg->expr);
    8811         3820 :       if (arg->expr->ts.type == BT_CLASS)
    8812              :         {
    8813           37 :           tmp = build_fold_indirect_ref_loc (input_location, argse.expr);
    8814           37 :           if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
    8815              :             {
    8816           19 :               source = gfc_class_data_get (tmp);
    8817           19 :               class_ref = tmp;
    8818              :             }
    8819              :           else
    8820              :             {
    8821              :               /* Array elements are evaluated as a reference to the data.
    8822              :                  To obtain the vptr for the element size, the argument
    8823              :                  expression must be stripped to the class reference and
    8824              :                  re-evaluated. The pre and post blocks are not needed.  */
    8825           18 :               gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
    8826           18 :               source = argse.expr;
    8827           18 :               class_expr = gfc_find_and_cut_at_last_class_ref (arg->expr);
    8828           18 :               gfc_init_se (&argse, NULL);
    8829           18 :               gfc_conv_expr (&argse, class_expr);
    8830           18 :               class_ref = argse.expr;
    8831              :             }
    8832              :         }
    8833              :       else
    8834         3783 :         source = argse.expr;
    8835              : 
    8836              :       /* Obtain the source word length.  */
    8837         3820 :       switch (arg->expr->ts.type)
    8838              :         {
    8839          300 :         case BT_CHARACTER:
    8840          300 :           tmp = size_of_string_in_bytes (arg->expr->ts.kind,
    8841              :                                          argse.string_length);
    8842          300 :           break;
    8843           37 :         case BT_CLASS:
    8844           37 :           if (class_ref != NULL_TREE)
    8845              :             {
    8846           37 :               tmp = gfc_class_vtab_size_get (class_ref);
    8847           37 :               if (UNLIMITED_POLY (source_expr))
    8848           30 :                 tmp = gfc_resize_class_size_with_len (NULL, class_ref, tmp);
    8849              :             }
    8850              :           else
    8851              :             {
    8852            0 :               tmp = gfc_class_vtab_size_get (argse.expr);
    8853            0 :               if (UNLIMITED_POLY (source_expr))
    8854            0 :                 tmp = gfc_resize_class_size_with_len (NULL, argse.expr, tmp);
    8855              :             }
    8856              :           break;
    8857         3483 :         default:
    8858         3483 :           source_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
    8859              :                                                                 source));
    8860         3483 :           tmp = fold_convert (gfc_array_index_type,
    8861              :                               size_in_bytes (source_type));
    8862         3483 :           break;
    8863              :         }
    8864              :     }
    8865              :   else
    8866              :     {
    8867          374 :       bool simply_contiguous = gfc_is_simply_contiguous (arg->expr,
    8868              :                                                          false, true);
    8869          374 :       argse.want_pointer = 0;
    8870              :       /* A non-contiguous SOURCE needs packing.  */
    8871          374 :       if (!simply_contiguous)
    8872           92 :         argse.force_tmp = 1;
    8873          374 :       gfc_conv_expr_descriptor (&argse, arg->expr);
    8874          374 :       source = gfc_conv_descriptor_data_get (argse.expr);
    8875          374 :       source_type = gfc_get_element_type (TREE_TYPE (argse.expr));
    8876              : 
    8877              :       /* Repack the source if not simply contiguous.  */
    8878          374 :       if (!simply_contiguous)
    8879              :         {
    8880           92 :           tmp = gfc_build_addr_expr (NULL_TREE, argse.expr);
    8881              : 
    8882           92 :           if (warn_array_temporaries)
    8883            0 :             gfc_warning (OPT_Warray_temporaries,
    8884              :                          "Creating array temporary at %L", &expr->where);
    8885              : 
    8886           92 :           source = build_call_expr_loc (input_location,
    8887              :                                     gfor_fndecl_in_pack, 1, tmp);
    8888           92 :           source = gfc_evaluate_now (source, &argse.pre);
    8889              : 
    8890              :           /* Free the temporary.  */
    8891           92 :           gfc_start_block (&block);
    8892           92 :           tmp = gfc_call_free (source);
    8893           92 :           gfc_add_expr_to_block (&block, tmp);
    8894           92 :           stmt = gfc_finish_block (&block);
    8895              : 
    8896              :           /* Clean up if it was repacked.  */
    8897           92 :           gfc_init_block (&block);
    8898           92 :           tmp = gfc_conv_array_data (argse.expr);
    8899           92 :           tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    8900              :                                  source, tmp);
    8901           92 :           tmp = build3_v (COND_EXPR, tmp, stmt,
    8902              :                           build_empty_stmt (input_location));
    8903           92 :           gfc_add_expr_to_block (&block, tmp);
    8904           92 :           gfc_add_block_to_block (&block, &se->post);
    8905           92 :           gfc_init_block (&se->post);
    8906           92 :           gfc_add_block_to_block (&se->post, &block);
    8907              :         }
    8908              : 
    8909              :       /* Obtain the source word length.  */
    8910          374 :       if (arg->expr->ts.type == BT_CHARACTER)
    8911          144 :         tmp = size_of_string_in_bytes (arg->expr->ts.kind,
    8912              :                                        argse.string_length);
    8913          230 :       else if (arg->expr->ts.type == BT_CLASS)
    8914              :         {
    8915           60 :           if (UNLIMITED_POLY (source_expr)
    8916           60 :               && DECL_LANG_SPECIFIC (source_expr->symtree->n.sym->backend_decl))
    8917           18 :             class_ref = GFC_DECL_SAVED_DESCRIPTOR
    8918              :               (source_expr->symtree->n.sym->backend_decl);
    8919              :           else
    8920           42 :             class_ref = TREE_OPERAND (argse.expr, 0);
    8921           60 :           tmp = gfc_class_vtab_size_get (class_ref);
    8922           60 :           if (UNLIMITED_POLY (arg->expr))
    8923           60 :             tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
    8924              :         }
    8925              :       else
    8926          170 :         tmp = fold_convert (gfc_array_index_type,
    8927              :                             size_in_bytes (source_type));
    8928              : 
    8929              :       /* Obtain the size of the array in bytes.  */
    8930          374 :       extent = gfc_create_var (gfc_array_index_type, NULL);
    8931         1152 :       for (n = 0; n < arg->expr->rank; n++)
    8932              :         {
    8933          404 :           tree idx;
    8934          404 :           idx = gfc_rank_cst[n];
    8935          404 :           gfc_add_modify (&argse.pre, source_bytes, tmp);
    8936          404 :           lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
    8937          404 :           upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
    8938          404 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8939              :                                  gfc_array_index_type, upper, lower);
    8940          404 :           gfc_add_modify (&argse.pre, extent, tmp);
    8941          404 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    8942              :                                  gfc_array_index_type, extent,
    8943              :                                  gfc_index_one_node);
    8944          404 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    8945              :                                  gfc_array_index_type, tmp, source_bytes);
    8946              :         }
    8947              :     }
    8948              : 
    8949         4194 :   gfc_add_modify (&argse.pre, source_bytes, tmp);
    8950         4194 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8951         4194 :   gfc_add_block_to_block (&se->post, &argse.post);
    8952              : 
    8953              :   /* Now convert MOLD.  The outputs are:
    8954              :         mold_type = the TREE type of MOLD
    8955              :         dest_word_len = destination word length in bytes.  */
    8956         4194 :   arg = arg->next;
    8957         4194 :   mold_expr = arg->expr;
    8958              : 
    8959         4194 :   gfc_init_se (&argse, NULL);
    8960              : 
    8961         4194 :   scalar_mold = arg->expr->rank == 0;
    8962              : 
    8963         4194 :   if (arg->expr->rank == 0)
    8964              :     {
    8965         3853 :       gfc_conv_expr_reference (&argse, mold_expr);
    8966         3853 :       mold_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
    8967              :                                                           argse.expr));
    8968              :     }
    8969              :   else
    8970              :     {
    8971          341 :       argse.want_pointer = 0;
    8972          341 :       gfc_conv_expr_descriptor (&argse, mold_expr);
    8973          341 :       mold_type = gfc_get_element_type (TREE_TYPE (argse.expr));
    8974              :     }
    8975              : 
    8976         4194 :   gfc_add_block_to_block (&se->pre, &argse.pre);
    8977         4194 :   gfc_add_block_to_block (&se->post, &argse.post);
    8978              : 
    8979         4194 :   if (strcmp (expr->value.function.name, "__transfer_in_transfer") == 0)
    8980              :     {
    8981              :       /* If this TRANSFER is nested in another TRANSFER, use a type
    8982              :          that preserves all bits.  */
    8983           12 :       if (mold_expr->ts.type == BT_LOGICAL)
    8984           12 :         mold_type = gfc_get_int_type (mold_expr->ts.kind);
    8985              :     }
    8986              : 
    8987              :   /* Obtain the destination word length.  */
    8988         4194 :   switch (mold_expr->ts.type)
    8989              :     {
    8990          527 :     case BT_CHARACTER:
    8991          527 :       tmp = size_of_string_in_bytes (mold_expr->ts.kind, argse.string_length);
    8992          527 :       mold_type = gfc_get_character_type_len (mold_expr->ts.kind,
    8993              :                                               argse.string_length);
    8994          527 :       break;
    8995            6 :     case BT_CLASS:
    8996            6 :       if (scalar_mold)
    8997            6 :         class_ref = argse.expr;
    8998              :       else
    8999            0 :         class_ref = TREE_OPERAND (argse.expr, 0);
    9000            6 :       tmp = gfc_class_vtab_size_get (class_ref);
    9001            6 :       if (UNLIMITED_POLY (arg->expr))
    9002            0 :         tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
    9003              :       break;
    9004         3661 :     default:
    9005         3661 :       tmp = fold_convert (gfc_array_index_type, size_in_bytes (mold_type));
    9006         3661 :       break;
    9007              :     }
    9008              : 
    9009              :   /* Do not fix dest_word_len if it is a variable, since the temporary can wind
    9010              :      up being used before the assignment.  */
    9011         4194 :   if (mold_expr->ts.type == BT_CHARACTER && mold_expr->ts.deferred)
    9012              :     dest_word_len = tmp;
    9013              :   else
    9014              :     {
    9015         4140 :       dest_word_len = gfc_create_var (gfc_array_index_type, NULL);
    9016         4140 :       gfc_add_modify (&se->pre, dest_word_len, tmp);
    9017              :     }
    9018              : 
    9019              :   /* Finally convert SIZE, if it is present.  */
    9020         4194 :   arg = arg->next;
    9021         4194 :   size_words = gfc_create_var (gfc_array_index_type, NULL);
    9022              : 
    9023         4194 :   if (arg->expr)
    9024              :     {
    9025          264 :       gfc_init_se (&argse, NULL);
    9026          264 :       gfc_conv_expr_reference (&argse, arg->expr);
    9027          264 :       tmp = convert (gfc_array_index_type,
    9028              :                      build_fold_indirect_ref_loc (input_location,
    9029              :                                               argse.expr));
    9030          264 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    9031          264 :       gfc_add_block_to_block (&se->post, &argse.post);
    9032              :     }
    9033              :   else
    9034              :     tmp = NULL_TREE;
    9035              : 
    9036              :   /* Separate array and scalar results.  */
    9037         4194 :   if (scalar_mold && tmp == NULL_TREE)
    9038         3662 :     goto scalar_transfer;
    9039              : 
    9040          532 :   size_bytes = gfc_create_var (gfc_array_index_type, NULL);
    9041          532 :   if (tmp != NULL_TREE)
    9042          264 :     tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9043              :                            tmp, dest_word_len);
    9044              :   else
    9045              :     tmp = source_bytes;
    9046              : 
    9047          532 :   gfc_add_modify (&se->pre, size_bytes, tmp);
    9048          532 :   gfc_add_modify (&se->pre, size_words,
    9049              :                        fold_build2_loc (input_location, CEIL_DIV_EXPR,
    9050              :                                         gfc_array_index_type,
    9051              :                                         size_bytes, dest_word_len));
    9052              : 
    9053              :   /* Evaluate the bounds of the result.  If the loop range exists, we have
    9054              :      to check if it is too large.  If so, we modify loop->to be consistent
    9055              :      with min(size, size(source)).  Otherwise, size is made consistent with
    9056              :      the loop range, so that the right number of bytes is transferred.*/
    9057          532 :   n = se->loop->order[0];
    9058          532 :   if (se->loop->to[n] != NULL_TREE)
    9059              :     {
    9060          247 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9061              :                              se->loop->to[n], se->loop->from[n]);
    9062          247 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    9063              :                              tmp, gfc_index_one_node);
    9064          247 :       tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
    9065              :                          tmp, size_words);
    9066          247 :       gfc_add_modify (&se->pre, size_words, tmp);
    9067          247 :       gfc_add_modify (&se->pre, size_bytes,
    9068              :                            fold_build2_loc (input_location, MULT_EXPR,
    9069              :                                             gfc_array_index_type,
    9070              :                                             size_words, dest_word_len));
    9071          494 :       upper = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    9072          247 :                                size_words, se->loop->from[n]);
    9073          247 :       upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9074              :                                upper, gfc_index_one_node);
    9075              :     }
    9076              :   else
    9077              :     {
    9078          285 :       upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9079              :                                size_words, gfc_index_one_node);
    9080          285 :       se->loop->from[n] = gfc_index_zero_node;
    9081              :     }
    9082              : 
    9083          532 :   se->loop->to[n] = upper;
    9084              : 
    9085              :   /* Build a destination descriptor, using the pointer, source, as the
    9086              :      data field.  */
    9087          532 :   gfc_trans_create_temp_array (&se->pre, &se->post, se->ss, mold_type,
    9088              :                                NULL_TREE, false, true, false, &expr->where);
    9089              : 
    9090              :   /* Cast the pointer to the result.  */
    9091          532 :   tmp = gfc_conv_descriptor_data_get (info->descriptor);
    9092          532 :   tmp = fold_convert (pvoid_type_node, tmp);
    9093              : 
    9094              :   /* Use memcpy to do the transfer.  */
    9095          532 :   tmp
    9096          532 :     = build_call_expr_loc (input_location,
    9097              :                            builtin_decl_explicit (BUILT_IN_MEMCPY), 3, tmp,
    9098              :                            fold_convert (pvoid_type_node, source),
    9099              :                            fold_convert (size_type_node,
    9100              :                                          fold_build2_loc (input_location,
    9101              :                                                           MIN_EXPR,
    9102              :                                                           gfc_array_index_type,
    9103              :                                                           size_bytes,
    9104              :                                                           source_bytes)));
    9105          532 :   gfc_add_expr_to_block (&se->pre, tmp);
    9106              : 
    9107          532 :   se->expr = info->descriptor;
    9108          532 :   if (expr->ts.type == BT_CHARACTER)
    9109              :     {
    9110          323 :       tmp = fold_convert (gfc_charlen_type_node,
    9111              :                           TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
    9112          323 :       se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    9113              :                                            gfc_charlen_type_node,
    9114              :                                            dest_word_len, tmp);
    9115              :     }
    9116              : 
    9117          532 :   if (IS_PDT (expr))
    9118            0 :       copy_pdt_components (se, expr);
    9119          532 :   return;
    9120              : 
    9121              : /* Deal with scalar results.  */
    9122         3662 : scalar_transfer:
    9123         3662 :   extent = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
    9124              :                             dest_word_len, source_bytes);
    9125         3662 :   extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    9126              :                             extent, gfc_index_zero_node);
    9127              : 
    9128         3662 :   if (expr->ts.type == BT_CHARACTER)
    9129              :     {
    9130          204 :       tree direct, indirect, free;
    9131              : 
    9132          204 :       ptr = convert (gfc_get_pchar_type (expr->ts.kind), source);
    9133          204 :       tmpdecl = gfc_create_var (gfc_get_pchar_type (expr->ts.kind),
    9134              :                                 "transfer");
    9135              : 
    9136              :       /* If source is longer than the destination, use a pointer to
    9137              :          the source directly.  */
    9138          204 :       gfc_init_block (&block);
    9139          204 :       gfc_add_modify (&block, tmpdecl, ptr);
    9140          204 :       direct = gfc_finish_block (&block);
    9141              : 
    9142              :       /* Otherwise, allocate a string with the length of the destination
    9143              :          and copy the source into it.  */
    9144          204 :       gfc_init_block (&block);
    9145          204 :       tmp = gfc_get_pchar_type (expr->ts.kind);
    9146          204 :       tmp = gfc_call_malloc (&block, tmp, dest_word_len);
    9147          204 :       gfc_add_modify (&block, tmpdecl,
    9148          204 :                       fold_convert (TREE_TYPE (ptr), tmp));
    9149          204 :       tmp = build_call_expr_loc (input_location,
    9150              :                              builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
    9151              :                              fold_convert (pvoid_type_node, tmpdecl),
    9152              :                              fold_convert (pvoid_type_node, ptr),
    9153              :                              fold_convert (size_type_node, extent));
    9154          204 :       gfc_add_expr_to_block (&block, tmp);
    9155          204 :       indirect = gfc_finish_block (&block);
    9156              : 
    9157              :       /* Wrap it up with the condition.  */
    9158          204 :       tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    9159              :                              dest_word_len, source_bytes);
    9160          204 :       tmp = build3_v (COND_EXPR, tmp, direct, indirect);
    9161          204 :       gfc_add_expr_to_block (&se->pre, tmp);
    9162              : 
    9163              :       /* Free the temporary string, if necessary.  */
    9164          204 :       free = gfc_call_free (tmpdecl);
    9165          204 :       tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    9166              :                              dest_word_len, source_bytes);
    9167          204 :       tmp = build3_v (COND_EXPR, tmp, free, build_empty_stmt (input_location));
    9168          204 :       gfc_add_expr_to_block (&se->post, tmp);
    9169              : 
    9170          204 :       se->expr = tmpdecl;
    9171          204 :       tmp = fold_convert (gfc_charlen_type_node,
    9172              :                           TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
    9173          204 :       se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    9174              :                                            gfc_charlen_type_node,
    9175              :                                            dest_word_len, tmp);
    9176              :     }
    9177              :   else
    9178              :     {
    9179         3458 :       tmpdecl = gfc_create_var (mold_type, "transfer");
    9180              : 
    9181         3458 :       ptr = convert (build_pointer_type (mold_type), source);
    9182              : 
    9183              :       /* For CLASS results, allocate the needed memory first.  */
    9184         3458 :       if (mold_expr->ts.type == BT_CLASS)
    9185              :         {
    9186            6 :           tree cdata;
    9187            6 :           cdata = gfc_class_data_get (tmpdecl);
    9188            6 :           tmp = gfc_call_malloc (&se->pre, TREE_TYPE (cdata), dest_word_len);
    9189            6 :           gfc_add_modify (&se->pre, cdata, tmp);
    9190              :         }
    9191              : 
    9192              :       /* Use memcpy to do the transfer.  */
    9193         3458 :       if (mold_expr->ts.type == BT_CLASS)
    9194            6 :         tmp = gfc_class_data_get (tmpdecl);
    9195              :       else
    9196         3452 :         tmp = gfc_build_addr_expr (NULL_TREE, tmpdecl);
    9197              : 
    9198         3458 :       tmp = build_call_expr_loc (input_location,
    9199              :                              builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
    9200              :                              fold_convert (pvoid_type_node, tmp),
    9201              :                              fold_convert (pvoid_type_node, ptr),
    9202              :                              fold_convert (size_type_node, extent));
    9203         3458 :       gfc_add_expr_to_block (&se->pre, tmp);
    9204              : 
    9205              :       /* For CLASS results, set the _vptr.  */
    9206         3458 :       if (mold_expr->ts.type == BT_CLASS)
    9207            6 :         gfc_reset_vptr (&se->pre, nullptr, tmpdecl, source_expr->ts.u.derived);
    9208         3458 :       se->expr = tmpdecl;
    9209              : 
    9210         3458 :       if (IS_PDT (expr))
    9211            6 :         copy_pdt_components (se, expr);
    9212              :     }
    9213              : }
    9214              : 
    9215              : 
    9216              : /* Generate code for the ALLOCATED intrinsic.
    9217              :    Generate inline code that directly check the address of the argument.  */
    9218              : 
    9219              : static void
    9220         7687 : gfc_conv_allocated (gfc_se *se, gfc_expr *expr)
    9221              : {
    9222         7687 :   gfc_se arg1se;
    9223         7687 :   tree tmp;
    9224         7687 :   gfc_expr *e = expr->value.function.actual->expr;
    9225              : 
    9226         7687 :   gfc_init_se (&arg1se, NULL);
    9227         7687 :   if (e->ts.type == BT_CLASS)
    9228              :     {
    9229              :       /* Make sure that class array expressions have both a _data
    9230              :          component reference and an array reference....  */
    9231          947 :       if (CLASS_DATA (e)->attr.dimension)
    9232          424 :         gfc_add_class_array_ref (e);
    9233              :       /* .... whilst scalars only need the _data component.  */
    9234              :       else
    9235          523 :         gfc_add_data_component (e);
    9236              :     }
    9237              : 
    9238         7687 :   gcc_assert (flag_coarray != GFC_FCOARRAY_LIB || !gfc_is_coindexed (e));
    9239              : 
    9240         7687 :   if (e->rank == 0)
    9241              :     {
    9242              :       /* Allocatable scalar.  */
    9243         3092 :       arg1se.want_pointer = 1;
    9244         3092 :       gfc_conv_expr (&arg1se, e);
    9245         3092 :       tmp = arg1se.expr;
    9246              :     }
    9247              :   else
    9248              :     {
    9249              :       /* Allocatable array.  */
    9250         4595 :       arg1se.descriptor_only = 1;
    9251         4595 :       gfc_conv_expr_descriptor (&arg1se, e);
    9252         4595 :       tmp = gfc_conv_descriptor_data_get (arg1se.expr);
    9253              :     }
    9254              : 
    9255         7687 :   tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
    9256         7687 :                          fold_convert (TREE_TYPE (tmp), null_pointer_node));
    9257              : 
    9258              :   /* Components of pointer array references sometimes come back with a pre block.  */
    9259         7687 :   if (arg1se.pre.head)
    9260          327 :     gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9261              : 
    9262         7687 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
    9263         7687 : }
    9264              : 
    9265              : 
    9266              : /* Generate code for the ASSOCIATED intrinsic.
    9267              :    If both POINTER and TARGET are arrays, generate a call to library function
    9268              :    _gfor_associated, and pass descriptors of POINTER and TARGET to it.
    9269              :    In other cases, generate inline code that directly compare the address of
    9270              :    POINTER with the address of TARGET.  */
    9271              : 
    9272              : static void
    9273         9892 : gfc_conv_associated (gfc_se *se, gfc_expr *expr)
    9274              : {
    9275         9892 :   gfc_actual_arglist *arg1;
    9276         9892 :   gfc_actual_arglist *arg2;
    9277         9892 :   gfc_se arg1se;
    9278         9892 :   gfc_se arg2se;
    9279         9892 :   tree tmp2;
    9280         9892 :   tree tmp;
    9281         9892 :   tree nonzero_arraylen = NULL_TREE;
    9282         9892 :   gfc_ss *ss;
    9283         9892 :   bool scalar;
    9284              : 
    9285         9892 :   gfc_init_se (&arg1se, NULL);
    9286         9892 :   gfc_init_se (&arg2se, NULL);
    9287         9892 :   arg1 = expr->value.function.actual;
    9288         9892 :   arg2 = arg1->next;
    9289              : 
    9290              :   /* Check whether the expression is a scalar or not; we cannot use
    9291              :      arg1->expr->rank as it can be nonzero for proc pointers.  */
    9292         9892 :   ss = gfc_walk_expr (arg1->expr);
    9293         9892 :   scalar = ss == gfc_ss_terminator;
    9294         9892 :   if (!scalar)
    9295         3938 :     gfc_free_ss_chain (ss);
    9296              : 
    9297         9892 :   if (!arg2->expr)
    9298              :     {
    9299              :       /* No optional target.  */
    9300         7435 :       if (scalar)
    9301              :         {
    9302              :           /* A pointer to a scalar.  */
    9303         5021 :           arg1se.want_pointer = 1;
    9304         5021 :           gfc_conv_expr (&arg1se, arg1->expr);
    9305         5021 :           if (arg1->expr->symtree->n.sym->attr.proc_pointer
    9306          185 :               && arg1->expr->symtree->n.sym->attr.dummy)
    9307           78 :             arg1se.expr = build_fold_indirect_ref_loc (input_location,
    9308              :                                                        arg1se.expr);
    9309         5021 :           if (arg1->expr->ts.type == BT_CLASS)
    9310              :             {
    9311          390 :               tmp2 = gfc_class_data_get (arg1se.expr);
    9312          390 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
    9313            0 :                 tmp2 = gfc_conv_descriptor_data_get (tmp2);
    9314              :             }
    9315              :           else
    9316         4631 :             tmp2 = arg1se.expr;
    9317              :         }
    9318              :       else
    9319              :         {
    9320              :           /* A pointer to an array.  */
    9321         2414 :           gfc_conv_expr_descriptor (&arg1se, arg1->expr);
    9322         2414 :           tmp2 = gfc_conv_descriptor_data_get (arg1se.expr);
    9323              :         }
    9324         7435 :       gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9325         7435 :       gfc_add_block_to_block (&se->post, &arg1se.post);
    9326         7435 :       tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp2,
    9327         7435 :                              fold_convert (TREE_TYPE (tmp2), null_pointer_node));
    9328         7435 :       se->expr = tmp;
    9329              :     }
    9330              :   else
    9331              :     {
    9332              :       /* An optional target.  */
    9333         2457 :       if (arg2->expr->ts.type == BT_CLASS
    9334           36 :           && arg2->expr->expr_type != EXPR_FUNCTION)
    9335           30 :         gfc_add_data_component (arg2->expr);
    9336              : 
    9337         2457 :       if (scalar)
    9338              :         {
    9339              :           /* A pointer to a scalar.  */
    9340          933 :           arg1se.want_pointer = 1;
    9341          933 :           gfc_conv_expr (&arg1se, arg1->expr);
    9342          933 :           if (arg1->expr->symtree->n.sym->attr.proc_pointer
    9343          128 :               && arg1->expr->symtree->n.sym->attr.dummy)
    9344           42 :             arg1se.expr = build_fold_indirect_ref_loc (input_location,
    9345              :                                                        arg1se.expr);
    9346          933 :           if (arg1->expr->ts.type == BT_CLASS)
    9347          254 :             arg1se.expr = gfc_class_data_get (arg1se.expr);
    9348              : 
    9349          933 :           arg2se.want_pointer = 1;
    9350          933 :           gfc_conv_expr (&arg2se, arg2->expr);
    9351          933 :           if (arg2->expr->symtree->n.sym->attr.proc_pointer
    9352           36 :               && arg2->expr->symtree->n.sym->attr.dummy)
    9353            0 :             arg2se.expr = build_fold_indirect_ref_loc (input_location,
    9354              :                                                        arg2se.expr);
    9355          933 :           if (arg2->expr->ts.type == BT_CLASS)
    9356              :             {
    9357            6 :               arg2se.expr = gfc_evaluate_now (arg2se.expr, &arg2se.pre);
    9358            6 :               arg2se.expr = gfc_class_data_get (arg2se.expr);
    9359              :             }
    9360          933 :           gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9361          933 :           gfc_add_block_to_block (&se->post, &arg1se.post);
    9362          933 :           gfc_add_block_to_block (&se->pre, &arg2se.pre);
    9363          933 :           gfc_add_block_to_block (&se->post, &arg2se.post);
    9364          933 :           tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    9365              :                                  arg1se.expr, arg2se.expr);
    9366          933 :           tmp2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9367              :                                   arg1se.expr, null_pointer_node);
    9368          933 :           se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    9369              :                                       logical_type_node, tmp, tmp2);
    9370              :         }
    9371              :       else
    9372              :         {
    9373              :           /* An array pointer of zero length is not associated if target is
    9374              :              present.  */
    9375         1524 :           arg1se.descriptor_only = 1;
    9376         1524 :           gfc_conv_expr_lhs (&arg1se, arg1->expr);
    9377         1524 :           if (arg1->expr->rank == -1)
    9378              :             {
    9379           84 :               tmp = gfc_conv_descriptor_rank_get (arg1se.expr);
    9380          168 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    9381           84 :                                      TREE_TYPE (tmp), tmp,
    9382           84 :                                      build_int_cst (TREE_TYPE (tmp), 1));
    9383              :             }
    9384              :           else
    9385         1440 :             tmp = gfc_rank_cst[arg1->expr->rank - 1];
    9386         1524 :           tmp = gfc_conv_descriptor_stride_get (arg1se.expr, tmp);
    9387         1524 :           if (arg2->expr->rank != 0)
    9388         1494 :             nonzero_arraylen = fold_build2_loc (input_location, NE_EXPR,
    9389              :                                                 logical_type_node, tmp,
    9390         1494 :                                                 build_int_cst (TREE_TYPE (tmp), 0));
    9391              : 
    9392              :           /* A pointer to an array, call library function _gfor_associated.  */
    9393         1524 :           arg1se.want_pointer = 1;
    9394         1524 :           gfc_conv_expr_descriptor (&arg1se, arg1->expr);
    9395         1524 :           gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9396         1524 :           gfc_add_block_to_block (&se->post, &arg1se.post);
    9397              : 
    9398         1524 :           arg2se.want_pointer = 1;
    9399         1524 :           arg2se.force_no_tmp = 1;
    9400         1524 :           if (arg2->expr->rank != 0)
    9401         1494 :             gfc_conv_expr_descriptor (&arg2se, arg2->expr);
    9402              :           else
    9403              :             {
    9404           30 :               gfc_conv_expr (&arg2se, arg2->expr);
    9405           30 :               arg2se.expr
    9406           30 :                 = gfc_conv_scalar_to_descriptor (&arg2se, arg2se.expr,
    9407           30 :                                                  gfc_expr_attr (arg2->expr));
    9408           30 :               arg2se.expr = gfc_build_addr_expr (NULL_TREE, arg2se.expr);
    9409              :             }
    9410         1524 :           gfc_add_block_to_block (&se->pre, &arg2se.pre);
    9411         1524 :           gfc_add_block_to_block (&se->post, &arg2se.post);
    9412         1524 :           se->expr = build_call_expr_loc (input_location,
    9413              :                                       gfor_fndecl_associated, 2,
    9414              :                                       arg1se.expr, arg2se.expr);
    9415         1524 :           se->expr = convert (logical_type_node, se->expr);
    9416         1524 :           if (arg2->expr->rank != 0)
    9417         1494 :             se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    9418              :                                         logical_type_node, se->expr,
    9419              :                                         nonzero_arraylen);
    9420              :         }
    9421              : 
    9422              :       /* If target is present zero character length pointers cannot
    9423              :          be associated.  */
    9424         2457 :       if (arg1->expr->ts.type == BT_CHARACTER)
    9425              :         {
    9426          631 :           tmp = arg1se.string_length;
    9427          631 :           tmp = fold_build2_loc (input_location, NE_EXPR,
    9428              :                                  logical_type_node, tmp,
    9429          631 :                                  build_zero_cst (TREE_TYPE (tmp)));
    9430          631 :           se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    9431              :                                       logical_type_node, se->expr, tmp);
    9432              :         }
    9433              :     }
    9434              : 
    9435         9892 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    9436         9892 : }
    9437              : 
    9438              : 
    9439              : /* Generate code for the SAME_TYPE_AS intrinsic.
    9440              :    Generate inline code that directly checks the vindices.  */
    9441              : 
    9442              : static void
    9443          418 : gfc_conv_same_type_as (gfc_se *se, gfc_expr *expr)
    9444              : {
    9445          418 :   gfc_expr *a, *b;
    9446          418 :   gfc_se se1, se2;
    9447          418 :   tree tmp;
    9448          418 :   tree conda = NULL_TREE, condb = NULL_TREE;
    9449              : 
    9450          418 :   gfc_init_se (&se1, NULL);
    9451          418 :   gfc_init_se (&se2, NULL);
    9452              : 
    9453          418 :   a = expr->value.function.actual->expr;
    9454          418 :   b = expr->value.function.actual->next->expr;
    9455              : 
    9456          418 :   bool unlimited_poly_a = UNLIMITED_POLY (a);
    9457          418 :   bool unlimited_poly_b = UNLIMITED_POLY (b);
    9458          418 :   if (unlimited_poly_a)
    9459              :     {
    9460          111 :       se1.want_pointer = 1;
    9461          111 :       gfc_add_vptr_component (a);
    9462              :     }
    9463          307 :   else if (a->ts.type == BT_CLASS)
    9464              :     {
    9465          265 :       gfc_add_vptr_component (a);
    9466          265 :       gfc_add_hash_component (a);
    9467              :     }
    9468           42 :   else if (a->ts.type == BT_DERIVED)
    9469           42 :     a = gfc_get_int_expr (gfc_default_integer_kind, NULL,
    9470           42 :                           a->ts.u.derived->hash_value);
    9471              : 
    9472          418 :   if (unlimited_poly_b)
    9473              :     {
    9474           72 :       se2.want_pointer = 1;
    9475           72 :       gfc_add_vptr_component (b);
    9476              :     }
    9477          346 :   else if (b->ts.type == BT_CLASS)
    9478              :     {
    9479          169 :       gfc_add_vptr_component (b);
    9480          169 :       gfc_add_hash_component (b);
    9481              :     }
    9482          177 :   else if (b->ts.type == BT_DERIVED)
    9483          177 :     b = gfc_get_int_expr (gfc_default_integer_kind, NULL,
    9484          177 :                           b->ts.u.derived->hash_value);
    9485              : 
    9486          418 :   gfc_conv_expr (&se1, a);
    9487          418 :   gfc_conv_expr (&se2, b);
    9488              : 
    9489          418 :   if (unlimited_poly_a)
    9490              :     {
    9491          111 :       conda = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9492              :                                se1.expr,
    9493          111 :                                build_int_cst (TREE_TYPE (se1.expr), 0));
    9494          111 :       se1.expr = gfc_vptr_hash_get (se1.expr);
    9495              :     }
    9496              : 
    9497          418 :   if (unlimited_poly_b)
    9498              :     {
    9499           72 :       condb = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9500              :                                se2.expr,
    9501           72 :                                build_int_cst (TREE_TYPE (se2.expr), 0));
    9502           72 :       se2.expr = gfc_vptr_hash_get (se2.expr);
    9503              :     }
    9504              : 
    9505          418 :   tmp = fold_build2_loc (input_location, EQ_EXPR,
    9506              :                          logical_type_node, se1.expr,
    9507          418 :                          fold_convert (TREE_TYPE (se1.expr), se2.expr));
    9508              : 
    9509          418 :   if (conda)
    9510          111 :     tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9511              :                            logical_type_node, conda, tmp);
    9512              : 
    9513          418 :   if (condb)
    9514           72 :     tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9515              :                            logical_type_node, condb, tmp);
    9516              : 
    9517          418 :   se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
    9518          418 : }
    9519              : 
    9520              : 
    9521              : /* Generate code for SELECTED_CHAR_KIND (NAME) intrinsic function.  */
    9522              : 
    9523              : static void
    9524           42 : gfc_conv_intrinsic_sc_kind (gfc_se *se, gfc_expr *expr)
    9525              : {
    9526           42 :   tree args[2];
    9527              : 
    9528           42 :   gfc_conv_intrinsic_function_args (se, expr, args, 2);
    9529           42 :   se->expr = build_call_expr_loc (input_location,
    9530              :                               gfor_fndecl_sc_kind, 2, args[0], args[1]);
    9531           42 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
    9532           42 : }
    9533              : 
    9534              : 
    9535              : /* Generate code for SELECTED_INT_KIND (R) intrinsic function.  */
    9536              : 
    9537              : static void
    9538           45 : gfc_conv_intrinsic_si_kind (gfc_se *se, gfc_expr *expr)
    9539              : {
    9540           45 :   tree arg, type;
    9541              : 
    9542           45 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    9543              : 
    9544              :   /* The argument to SELECTED_INT_KIND is INTEGER(4).  */
    9545           45 :   type = gfc_get_int_type (4);
    9546           45 :   arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
    9547              : 
    9548              :   /* Convert it to the required type.  */
    9549           45 :   type = gfc_typenode_for_spec (&expr->ts);
    9550           45 :   se->expr = build_call_expr_loc (input_location,
    9551              :                               gfor_fndecl_si_kind, 1, arg);
    9552           45 :   se->expr = fold_convert (type, se->expr);
    9553           45 : }
    9554              : 
    9555              : 
    9556              : /* Generate code for SELECTED_LOGICAL_KIND (BITS) intrinsic function.  */
    9557              : 
    9558              : static void
    9559            6 : gfc_conv_intrinsic_sl_kind (gfc_se *se, gfc_expr *expr)
    9560              : {
    9561            6 :   tree arg, type;
    9562              : 
    9563            6 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
    9564              : 
    9565              :   /* The argument to SELECTED_LOGICAL_KIND is INTEGER(4).  */
    9566            6 :   type = gfc_get_int_type (4);
    9567            6 :   arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
    9568              : 
    9569              :   /* Convert it to the required type.  */
    9570            6 :   type = gfc_typenode_for_spec (&expr->ts);
    9571            6 :   se->expr = build_call_expr_loc (input_location,
    9572              :                               gfor_fndecl_sl_kind, 1, arg);
    9573            6 :   se->expr = fold_convert (type, se->expr);
    9574            6 : }
    9575              : 
    9576              : 
    9577              : /* Generate code for SELECTED_REAL_KIND (P, R, RADIX) intrinsic function.  */
    9578              : 
    9579              : static void
    9580           82 : gfc_conv_intrinsic_sr_kind (gfc_se *se, gfc_expr *expr)
    9581              : {
    9582           82 :   gfc_actual_arglist *actual;
    9583           82 :   tree type;
    9584           82 :   gfc_se argse;
    9585           82 :   vec<tree, va_gc> *args = NULL;
    9586              : 
    9587          328 :   for (actual = expr->value.function.actual; actual; actual = actual->next)
    9588              :     {
    9589          246 :       gfc_init_se (&argse, se);
    9590              : 
    9591              :       /* Pass a NULL pointer for an absent arg.  */
    9592          246 :       if (actual->expr == NULL)
    9593           96 :         argse.expr = null_pointer_node;
    9594              :       else
    9595              :         {
    9596          150 :           gfc_typespec ts;
    9597          150 :           gfc_clear_ts (&ts);
    9598              : 
    9599          150 :           if (actual->expr->ts.kind != gfc_c_int_kind)
    9600              :             {
    9601              :               /* The arguments to SELECTED_REAL_KIND are INTEGER(4).  */
    9602            0 :               ts.type = BT_INTEGER;
    9603            0 :               ts.kind = gfc_c_int_kind;
    9604            0 :               gfc_convert_type (actual->expr, &ts, 2);
    9605              :             }
    9606          150 :           gfc_conv_expr_reference (&argse, actual->expr);
    9607              :         }
    9608              : 
    9609          246 :       gfc_add_block_to_block (&se->pre, &argse.pre);
    9610          246 :       gfc_add_block_to_block (&se->post, &argse.post);
    9611          246 :       vec_safe_push (args, argse.expr);
    9612              :     }
    9613              : 
    9614              :   /* Convert it to the required type.  */
    9615           82 :   type = gfc_typenode_for_spec (&expr->ts);
    9616           82 :   se->expr = build_call_expr_loc_vec (input_location,
    9617              :                                       gfor_fndecl_sr_kind, args);
    9618           82 :   se->expr = fold_convert (type, se->expr);
    9619           82 : }
    9620              : 
    9621              : 
    9622              : /* Generate code for TRIM (A) intrinsic function.  */
    9623              : 
    9624              : static void
    9625          582 : gfc_conv_intrinsic_trim (gfc_se * se, gfc_expr * expr)
    9626              : {
    9627          582 :   tree var;
    9628          582 :   tree len;
    9629          582 :   tree addr;
    9630          582 :   tree tmp;
    9631          582 :   tree cond;
    9632          582 :   tree fndecl;
    9633          582 :   tree function;
    9634          582 :   tree *args;
    9635          582 :   unsigned int num_args;
    9636              : 
    9637          582 :   num_args = gfc_intrinsic_argument_list_length (expr) + 2;
    9638          582 :   args = XALLOCAVEC (tree, num_args);
    9639              : 
    9640          582 :   var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
    9641          582 :   addr = gfc_build_addr_expr (ppvoid_type_node, var);
    9642          582 :   len = gfc_create_var (gfc_charlen_type_node, "len");
    9643              : 
    9644          582 :   gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
    9645          582 :   args[0] = gfc_build_addr_expr (NULL_TREE, len);
    9646          582 :   args[1] = addr;
    9647              : 
    9648          582 :   if (expr->ts.kind == 1)
    9649          550 :     function = gfor_fndecl_string_trim;
    9650           32 :   else if (expr->ts.kind == 4)
    9651           32 :     function = gfor_fndecl_string_trim_char4;
    9652              :   else
    9653            0 :     gcc_unreachable ();
    9654              : 
    9655          582 :   fndecl = build_addr (function);
    9656          582 :   tmp = build_call_array_loc (input_location,
    9657          582 :                           TREE_TYPE (TREE_TYPE (function)), fndecl,
    9658              :                           num_args, args);
    9659          582 :   gfc_add_expr_to_block (&se->pre, tmp);
    9660              : 
    9661              :   /* Free the temporary afterwards, if necessary.  */
    9662          582 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    9663          582 :                           len, build_int_cst (TREE_TYPE (len), 0));
    9664          582 :   tmp = gfc_call_free (var);
    9665          582 :   tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    9666          582 :   gfc_add_expr_to_block (&se->post, tmp);
    9667              : 
    9668          582 :   se->expr = var;
    9669          582 :   se->string_length = len;
    9670          582 : }
    9671              : 
    9672              : 
    9673              : /* Generate code for REPEAT (STRING, NCOPIES) intrinsic function.  */
    9674              : 
    9675              : static void
    9676          547 : gfc_conv_intrinsic_repeat (gfc_se * se, gfc_expr * expr)
    9677              : {
    9678          547 :   tree args[3], ncopies, dest, dlen, src, slen, ncopies_type;
    9679          547 :   tree type, cond, tmp, count, exit_label, n, max, largest;
    9680          547 :   tree size;
    9681          547 :   stmtblock_t block, body;
    9682          547 :   int i;
    9683              : 
    9684              :   /* We store in charsize the size of a character.  */
    9685          547 :   i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
    9686          547 :   size = build_int_cst (sizetype, gfc_character_kinds[i].bit_size / 8);
    9687              : 
    9688              :   /* Get the arguments.  */
    9689          547 :   gfc_conv_intrinsic_function_args (se, expr, args, 3);
    9690          547 :   slen = fold_convert (sizetype, gfc_evaluate_now (args[0], &se->pre));
    9691          547 :   src = args[1];
    9692          547 :   ncopies = gfc_evaluate_now (args[2], &se->pre);
    9693          547 :   ncopies_type = TREE_TYPE (ncopies);
    9694              : 
    9695              :   /* Check that NCOPIES is not negative.  */
    9696          547 :   cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, ncopies,
    9697              :                           build_int_cst (ncopies_type, 0));
    9698          547 :   gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    9699              :                            "Argument NCOPIES of REPEAT intrinsic is negative "
    9700              :                            "(its value is %ld)",
    9701              :                            fold_convert (long_integer_type_node, ncopies));
    9702              : 
    9703              :   /* If the source length is zero, any non negative value of NCOPIES
    9704              :      is valid, and nothing happens.  */
    9705          547 :   n = gfc_create_var (ncopies_type, "ncopies");
    9706          547 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
    9707              :                           size_zero_node);
    9708          547 :   tmp = fold_build3_loc (input_location, COND_EXPR, ncopies_type, cond,
    9709              :                          build_int_cst (ncopies_type, 0), ncopies);
    9710          547 :   gfc_add_modify (&se->pre, n, tmp);
    9711          547 :   ncopies = n;
    9712              : 
    9713              :   /* Check that ncopies is not too large: ncopies should be less than
    9714              :      (or equal to) MAX / slen, where MAX is the maximal integer of
    9715              :      the gfc_charlen_type_node type.  If slen == 0, we need a special
    9716              :      case to avoid the division by zero.  */
    9717          547 :   max = fold_build2_loc (input_location, TRUNC_DIV_EXPR, sizetype,
    9718          547 :                          fold_convert (sizetype,
    9719              :                                        TYPE_MAX_VALUE (gfc_charlen_type_node)),
    9720              :                          slen);
    9721         1090 :   largest = TYPE_PRECISION (sizetype) > TYPE_PRECISION (ncopies_type)
    9722          547 :               ? sizetype : ncopies_type;
    9723          547 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    9724              :                           fold_convert (largest, ncopies),
    9725              :                           fold_convert (largest, max));
    9726          547 :   tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
    9727              :                          size_zero_node);
    9728          547 :   cond = fold_build3_loc (input_location, COND_EXPR, logical_type_node, tmp,
    9729              :                           logical_false_node, cond);
    9730          547 :   gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
    9731              :                            "Argument NCOPIES of REPEAT intrinsic is too large");
    9732              : 
    9733              :   /* Compute the destination length.  */
    9734          547 :   dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
    9735              :                           fold_convert (gfc_charlen_type_node, slen),
    9736              :                           fold_convert (gfc_charlen_type_node, ncopies));
    9737          547 :   type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
    9738          547 :   dest = gfc_conv_string_tmp (se, build_pointer_type (type), dlen);
    9739              : 
    9740              :   /* Generate the code to do the repeat operation:
    9741              :        for (i = 0; i < ncopies; i++)
    9742              :          memmove (dest + (i * slen * size), src, slen*size);  */
    9743          547 :   gfc_start_block (&block);
    9744          547 :   count = gfc_create_var (sizetype, "count");
    9745          547 :   gfc_add_modify (&block, count, size_zero_node);
    9746          547 :   exit_label = gfc_build_label_decl (NULL_TREE);
    9747              : 
    9748              :   /* Start the loop body.  */
    9749          547 :   gfc_start_block (&body);
    9750              : 
    9751              :   /* Exit the loop if count >= ncopies.  */
    9752          547 :   cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, count,
    9753              :                           fold_convert (sizetype, ncopies));
    9754          547 :   tmp = build1_v (GOTO_EXPR, exit_label);
    9755          547 :   TREE_USED (exit_label) = 1;
    9756          547 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    9757              :                          build_empty_stmt (input_location));
    9758          547 :   gfc_add_expr_to_block (&body, tmp);
    9759              : 
    9760              :   /* Call memmove (dest + (i*slen*size), src, slen*size).  */
    9761          547 :   tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, slen,
    9762              :                          count);
    9763          547 :   tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, tmp,
    9764              :                          size);
    9765          547 :   tmp = fold_build_pointer_plus_loc (input_location,
    9766              :                                      fold_convert (pvoid_type_node, dest), tmp);
    9767          547 :   tmp = build_call_expr_loc (input_location,
    9768              :                              builtin_decl_explicit (BUILT_IN_MEMMOVE),
    9769              :                              3, tmp, src,
    9770              :                              fold_build2_loc (input_location, MULT_EXPR,
    9771              :                                               size_type_node, slen, size));
    9772          547 :   gfc_add_expr_to_block (&body, tmp);
    9773              : 
    9774              :   /* Increment count.  */
    9775          547 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, sizetype,
    9776              :                          count, size_one_node);
    9777          547 :   gfc_add_modify (&body, count, tmp);
    9778              : 
    9779              :   /* Build the loop.  */
    9780          547 :   tmp = build1_v (LOOP_EXPR, gfc_finish_block (&body));
    9781          547 :   gfc_add_expr_to_block (&block, tmp);
    9782              : 
    9783              :   /* Add the exit label.  */
    9784          547 :   tmp = build1_v (LABEL_EXPR, exit_label);
    9785          547 :   gfc_add_expr_to_block (&block, tmp);
    9786              : 
    9787              :   /* Finish the block.  */
    9788          547 :   tmp = gfc_finish_block (&block);
    9789          547 :   gfc_add_expr_to_block (&se->pre, tmp);
    9790              : 
    9791              :   /* Set the result value.  */
    9792          547 :   se->expr = dest;
    9793          547 :   se->string_length = dlen;
    9794          547 : }
    9795              : 
    9796              : 
    9797              : /* Generate code for the IARGC intrinsic.  */
    9798              : 
    9799              : static void
    9800           12 : gfc_conv_intrinsic_iargc (gfc_se * se, gfc_expr * expr)
    9801              : {
    9802           12 :   tree tmp;
    9803           12 :   tree fndecl;
    9804           12 :   tree type;
    9805              : 
    9806              :   /* Call the library function.  This always returns an INTEGER(4).  */
    9807           12 :   fndecl = gfor_fndecl_iargc;
    9808           12 :   tmp = build_call_expr_loc (input_location,
    9809              :                          fndecl, 0);
    9810              : 
    9811              :   /* Convert it to the required type.  */
    9812           12 :   type = gfc_typenode_for_spec (&expr->ts);
    9813           12 :   tmp = fold_convert (type, tmp);
    9814              : 
    9815           12 :   se->expr = tmp;
    9816           12 : }
    9817              : 
    9818              : 
    9819              : /* Generate code for the KILL intrinsic.  */
    9820              : 
    9821              : static void
    9822            8 : conv_intrinsic_kill (gfc_se *se, gfc_expr *expr)
    9823              : {
    9824            8 :   tree *args;
    9825            8 :   tree int4_type_node = gfc_get_int_type (4);
    9826            8 :   tree pid;
    9827            8 :   tree sig;
    9828            8 :   tree tmp;
    9829            8 :   unsigned int num_args;
    9830              : 
    9831            8 :   num_args = gfc_intrinsic_argument_list_length (expr);
    9832            8 :   args = XALLOCAVEC (tree, num_args);
    9833            8 :   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
    9834              : 
    9835              :   /* Convert PID to a INTEGER(4) entity.  */
    9836            8 :   pid = convert (int4_type_node, args[0]);
    9837              : 
    9838              :   /* Convert SIG to a INTEGER(4) entity.  */
    9839            8 :   sig = convert (int4_type_node, args[1]);
    9840              : 
    9841            8 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_kill, 2, pid, sig);
    9842              : 
    9843            8 :   se->expr = fold_convert (TREE_TYPE (args[0]), tmp);
    9844            8 : }
    9845              : 
    9846              : 
    9847              : static tree
    9848           15 : conv_intrinsic_kill_sub (gfc_code *code)
    9849              : {
    9850           15 :   stmtblock_t block;
    9851           15 :   gfc_se se, se_stat;
    9852           15 :   tree int4_type_node = gfc_get_int_type (4);
    9853           15 :   tree pid;
    9854           15 :   tree sig;
    9855           15 :   tree statp;
    9856           15 :   tree tmp;
    9857              : 
    9858              :   /* Make the function call.  */
    9859           15 :   gfc_init_block (&block);
    9860           15 :   gfc_init_se (&se, NULL);
    9861              : 
    9862              :   /* Convert PID to a INTEGER(4) entity.  */
    9863           15 :   gfc_conv_expr (&se, code->ext.actual->expr);
    9864           15 :   gfc_add_block_to_block (&block, &se.pre);
    9865           15 :   pid = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
    9866           15 :   gfc_add_block_to_block (&block, &se.post);
    9867              : 
    9868              :   /* Convert SIG to a INTEGER(4) entity.  */
    9869           15 :   gfc_conv_expr (&se, code->ext.actual->next->expr);
    9870           15 :   gfc_add_block_to_block (&block, &se.pre);
    9871           15 :   sig = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
    9872           15 :   gfc_add_block_to_block (&block, &se.post);
    9873              : 
    9874              :   /* Deal with an optional STATUS.  */
    9875           15 :   if (code->ext.actual->next->next->expr)
    9876              :     {
    9877           10 :       gfc_init_se (&se_stat, NULL);
    9878           10 :       gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
    9879           10 :       statp = gfc_create_var (gfc_get_int_type (4), "_statp");
    9880              :     }
    9881              :   else
    9882              :     statp = NULL_TREE;
    9883              : 
    9884           25 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_kill_sub, 3, pid, sig,
    9885           10 :         statp ? gfc_build_addr_expr (NULL_TREE, statp) : null_pointer_node);
    9886              : 
    9887           15 :   gfc_add_expr_to_block (&block, tmp);
    9888              : 
    9889           15 :   if (statp && statp != se_stat.expr)
    9890           10 :     gfc_add_modify (&block, se_stat.expr,
    9891           10 :                     fold_convert (TREE_TYPE (se_stat.expr), statp));
    9892              : 
    9893           15 :   return gfc_finish_block (&block);
    9894              : }
    9895              : 
    9896              : 
    9897              : 
    9898              : /* The loc intrinsic returns the address of its argument as
    9899              :    gfc_index_integer_kind integer.  */
    9900              : 
    9901              : static void
    9902         9203 : gfc_conv_intrinsic_loc (gfc_se * se, gfc_expr * expr)
    9903              : {
    9904         9203 :   tree temp_var;
    9905         9203 :   gfc_expr *arg_expr;
    9906              : 
    9907         9203 :   gcc_assert (!se->ss);
    9908              : 
    9909         9203 :   arg_expr = expr->value.function.actual->expr;
    9910         9203 :   if (arg_expr->rank == 0)
    9911              :     {
    9912         6785 :       if (arg_expr->ts.type == BT_CLASS)
    9913           24 :         gfc_add_data_component (arg_expr);
    9914         6785 :       gfc_conv_expr_reference (se, arg_expr);
    9915              :     }
    9916         2418 :   else if (gfc_is_simply_contiguous (arg_expr, false, false))
    9917         2358 :     gfc_conv_array_parameter (se, arg_expr, true, NULL, NULL, NULL);
    9918              :   else
    9919              :     {
    9920           60 :       gfc_conv_expr_descriptor (se, arg_expr);
    9921           60 :       se->expr = gfc_conv_descriptor_data_get (se->expr);
    9922              :     }
    9923         9203 :   se->expr = convert (gfc_get_int_type (gfc_index_integer_kind), se->expr);
    9924         9203 :   se->expr = gfc_evaluate_now (se->expr, &se->pre);
    9925              : 
    9926              :   /* Create a temporary variable for loc return value.  Without this,
    9927              :      we get an error an ICE in gcc/expr.cc(expand_expr_addr_expr_1).  */
    9928         9203 :   temp_var = gfc_create_var (gfc_get_int_type (gfc_index_integer_kind), NULL);
    9929         9203 :   gfc_add_modify (&se->pre, temp_var, se->expr);
    9930         9203 :   se->expr = temp_var;
    9931         9203 : }
    9932              : 
    9933              : /* The following routine generates code for the intrinsic functions from
    9934              :    the ISO_C_BINDING module: C_LOC, C_FUNLOC, C_ASSOCIATED, and
    9935              :    F_C_STRING.  */
    9936              : 
    9937              : static void
    9938        10092 : conv_isocbinding_function (gfc_se *se, gfc_expr *expr)
    9939              : {
    9940        10092 :   gfc_actual_arglist *arg = expr->value.function.actual;
    9941              : 
    9942        10092 :   if (expr->value.function.isym->id == GFC_ISYM_C_LOC)
    9943              :     {
    9944         7702 :       if (arg->expr->rank == 0)
    9945         2022 :         gfc_conv_expr_reference (se, arg->expr);
    9946         5680 :       else if (gfc_is_simply_contiguous (arg->expr, false, false))
    9947         4596 :         gfc_conv_array_parameter (se, arg->expr, true, NULL, NULL, NULL);
    9948              :       else
    9949              :         {
    9950         1084 :           gfc_conv_expr_descriptor (se, arg->expr);
    9951         1084 :           se->expr = gfc_conv_descriptor_data_get (se->expr);
    9952              :         }
    9953              : 
    9954              :       /* TODO -- the following two lines shouldn't be necessary, but if
    9955              :          they're removed, a bug is exposed later in the code path.
    9956              :          This workaround was thus introduced, but will have to be
    9957              :          removed; please see PR 35150 for details about the issue.  */
    9958         7702 :       se->expr = convert (pvoid_type_node, se->expr);
    9959         7702 :       se->expr = gfc_evaluate_now (se->expr, &se->pre);
    9960              :     }
    9961         2390 :   else if (expr->value.function.isym->id == GFC_ISYM_C_FUNLOC)
    9962              :     {
    9963          260 :       gfc_conv_expr_reference (se, arg->expr);
    9964          260 :       if (arg->expr->symtree->n.sym->attr.proc_pointer
    9965           29 :           && arg->expr->symtree->n.sym->attr.dummy)
    9966            7 :         se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    9967              :       /* The code below is necessary to create a reference from the calling
    9968              :          subprogram to the argument of C_FUNLOC() in the call graph.
    9969              :          Please see PR 117303 for more details. */
    9970          260 :       se->expr = convert (pvoid_type_node, se->expr);
    9971          260 :       se->expr = gfc_evaluate_now (se->expr, &se->pre);
    9972              :     }
    9973         2130 :   else if (expr->value.function.isym->id == GFC_ISYM_C_ASSOCIATED)
    9974              :     {
    9975         2054 :       gfc_se arg1se;
    9976         2054 :       gfc_se arg2se;
    9977              : 
    9978              :       /* Build the addr_expr for the first argument.  The argument is
    9979              :          already an *address* so we don't need to set want_pointer in
    9980              :          the gfc_se.  */
    9981         2054 :       gfc_init_se (&arg1se, NULL);
    9982         2054 :       gfc_conv_expr (&arg1se, arg->expr);
    9983         2054 :       gfc_add_block_to_block (&se->pre, &arg1se.pre);
    9984         2054 :       gfc_add_block_to_block (&se->post, &arg1se.post);
    9985              : 
    9986              :       /* See if we were given two arguments.  */
    9987         2054 :       if (arg->next->expr == NULL)
    9988              :         /* Only given one arg so generate a null and do a
    9989              :            not-equal comparison against the first arg.  */
    9990         1675 :         se->expr = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9991              :                                     arg1se.expr,
    9992         1675 :                                     fold_convert (TREE_TYPE (arg1se.expr),
    9993              :                                                   null_pointer_node));
    9994              :       else
    9995              :         {
    9996          379 :           tree eq_expr;
    9997          379 :           tree not_null_expr;
    9998              : 
    9999              :           /* Given two arguments so build the arg2se from second arg.  */
   10000          379 :           gfc_init_se (&arg2se, NULL);
   10001          379 :           gfc_conv_expr (&arg2se, arg->next->expr);
   10002          379 :           gfc_add_block_to_block (&se->pre, &arg2se.pre);
   10003          379 :           gfc_add_block_to_block (&se->post, &arg2se.post);
   10004              : 
   10005              :           /* Generate test to compare that the two args are equal.  */
   10006          379 :           eq_expr = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   10007              :                                      arg1se.expr, arg2se.expr);
   10008              :           /* Generate test to ensure that the first arg is not null.  */
   10009          379 :           not_null_expr = fold_build2_loc (input_location, NE_EXPR,
   10010              :                                            logical_type_node,
   10011              :                                            arg1se.expr, null_pointer_node);
   10012              : 
   10013              :           /* Finally, the generated test must check that both arg1 is not
   10014              :              NULL and that it is equal to the second arg.  */
   10015          379 :           se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   10016              :                                       logical_type_node,
   10017              :                                       not_null_expr, eq_expr);
   10018              :         }
   10019              :     }
   10020           76 :   else if (expr->value.function.isym->id == GFC_ISYM_F_C_STRING)
   10021              :     {
   10022              :       /* There are three cases:
   10023              :          f_c_string(string)          -> trim(string) // c_null_char
   10024              :          f_c_string(string, .false.) -> trim(string) // c_null_char
   10025              :          f_c_string(string, .true.)  -> string       // c_null_char  */
   10026              : 
   10027           76 :       gfc_expr *string = arg->expr;
   10028           76 :       gfc_expr *asis = arg->next->expr;
   10029           76 :       bool need_asis = false, need_trim = false;
   10030           76 :       gfc_se asis_se;
   10031              : 
   10032           76 :       if (!asis)
   10033              :         {
   10034              :           need_trim = true;
   10035              :           need_asis = false;
   10036              :         }
   10037           54 :       else if (asis->expr_type == EXPR_CONSTANT)
   10038              :         {
   10039           32 :           need_asis = asis->value.logical;
   10040           32 :           need_trim = !need_asis;
   10041              :         }
   10042              :       else
   10043              :         {
   10044              :           /* A conditional expression is needed.  */
   10045           22 :           need_asis = true;
   10046           22 :           need_trim = true;
   10047           22 :           gfc_init_se (&asis_se, se);
   10048           22 :           gfc_conv_expr (&asis_se, asis);
   10049           22 :           if (asis->expr_type == EXPR_VARIABLE
   10050           22 :               && asis->symtree->n.sym->attr.dummy
   10051           10 :               && asis->symtree->n.sym->attr.optional)
   10052              :             {
   10053            6 :               tree present = gfc_conv_expr_present (asis->symtree->n.sym);
   10054            6 :               asis_se.expr
   10055            6 :                 = build3_loc (input_location, COND_EXPR,
   10056              :                               logical_type_node, present,
   10057              :                               asis_se.expr, logical_false_node);
   10058              :             }
   10059           22 :           gfc_make_safe_expr (&asis_se);
   10060              :         }
   10061              : 
   10062              :       /* Handle the case of a constant string argument first.  */
   10063           76 :       if (string->expr_type == EXPR_CONSTANT)
   10064              :         {
   10065              :           /* Output for the asis "then" case goes tlen/tstr, and the
   10066              :              trimmed case in elen/estr.  */
   10067           34 :           tree elen, estr, tlen, tstr;
   10068           34 :           elen = estr = tlen = tstr = NULL_TREE;
   10069              : 
   10070           34 :           gfc_char_t *orig_string = string->value.character.string;
   10071           34 :           gfc_charlen_t orig_len = string->value.character.length;
   10072           34 :           gfc_charlen_t n;
   10073           34 :           gfc_char_t *buf
   10074           34 :             = (gfc_char_t *) alloca ((orig_len + 1) * sizeof (gfc_char_t));
   10075           34 :           memcpy (buf, orig_string, orig_len * sizeof (gfc_char_t));
   10076           34 :           buf[orig_len] = '\0';
   10077           34 :           int kind = gfc_default_character_kind;
   10078           34 :           gcc_assert (string->ts.kind == kind);
   10079              : 
   10080              :           /* Build the new string constant(s).  */
   10081           34 :           if (need_asis)
   10082              :             {
   10083           14 :               tstr = gfc_build_wide_string_const (kind, orig_len + 1, buf);
   10084           14 :               tlen = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tstr)));
   10085           14 :               if (!need_trim)
   10086              :                 {
   10087           10 :                   se->expr = tstr;
   10088           10 :                   se->string_length = tlen;
   10089           10 :                   return;
   10090              :                 }
   10091              :             }
   10092           24 :           if (need_trim)
   10093              :             {
   10094           72 :               for (n = orig_len; n; n--)
   10095           72 :                 if (buf[n - 1] != ' ')
   10096              :                   break;
   10097           24 :               buf[n] = '\0';
   10098           24 :               if (need_asis && n == orig_len)
   10099              :                 {
   10100              :                   /* Special case; trimming is a no-op.  Add side-effects
   10101              :                      from the condition and then just return the string
   10102              :                      without a conditional.  */
   10103            2 :                   gfc_add_block_to_block (&se->pre, &asis_se.pre);
   10104            2 :                   se->expr = tstr;
   10105            2 :                   se->string_length = tlen;
   10106            2 :                   return;
   10107              :                 }
   10108              :               else
   10109              :                 {
   10110           22 :                   estr = gfc_build_wide_string_const (kind, n + 1, buf);
   10111           22 :                   elen = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (estr)));
   10112              :                 }
   10113           22 :               if (!need_asis)
   10114              :                 {
   10115           20 :                   se->expr = estr;
   10116           20 :                   se->string_length = elen;
   10117           20 :                   return;
   10118              :                 }
   10119              :             }
   10120            0 :           gcc_assert (need_asis && need_trim);
   10121            2 :           gfc_add_block_to_block (&se->pre, &asis_se.pre);
   10122            2 :           se->expr
   10123            2 :             = fold_build3_loc (input_location, COND_EXPR,
   10124              :                                pchar_type_node, asis_se.expr,
   10125              :                                tstr, estr);
   10126            2 :           se->string_length
   10127            2 :             = fold_build3_loc (input_location, COND_EXPR,
   10128              :                                gfc_charlen_type_node, asis_se.expr,
   10129              :                                tlen, elen);
   10130            2 :           return;
   10131              :         }
   10132              :       else
   10133              :         /* We have to generate code to do the string transformation(s) at
   10134              :            runtime.  */
   10135              :         {
   10136           42 :           tree tmp;
   10137              : 
   10138              :           /* Convert input string. */
   10139           42 :           gfc_se sse;
   10140           42 :           gfc_init_se (&sse, se);
   10141           42 :           gfc_conv_expr (&sse, string);
   10142           42 :           gfc_conv_string_parameter (&sse);
   10143           42 :           gfc_make_safe_expr (&sse);
   10144           42 :           gfc_add_block_to_block (&se->pre, &sse.pre);
   10145              : 
   10146              :           /* Use a temporary for the (possibly trimmed) string length.  */
   10147           42 :           tree lenvar = gfc_create_var (gfc_charlen_type_node, NULL);
   10148           42 :           gfc_add_modify (&se->pre, lenvar, sse.string_length);
   10149              : 
   10150              :           /* Build the expression for a call to LEN_TRIM if we may need
   10151              :              to trim the string.  If it's conditional, handle that too.  */
   10152           42 :           if (need_trim)
   10153              :             {
   10154           36 :               tree trimlen
   10155           36 :                 = build_call_expr_loc (input_location,
   10156              :                                        gfor_fndecl_string_len_trim, 2,
   10157              :                                        lenvar, sse.expr);
   10158           36 :               if (need_asis)
   10159              :                 {
   10160           18 :                   gfc_add_block_to_block (&se->pre, &asis_se.pre);
   10161           18 :                   tmp = fold_build3_loc (input_location, COND_EXPR,
   10162              :                                          gfc_charlen_type_node, asis_se.expr,
   10163              :                                          lenvar, trimlen);
   10164           18 :                   gfc_add_modify (&se->pre, lenvar, tmp);
   10165              :                 }
   10166              :               else
   10167           18 :                 gfc_add_modify (&se->pre, lenvar, trimlen);
   10168              :             }
   10169              : 
   10170              :           /* Allocate a new string newvar that is lenvar+1 bytes long.
   10171              :              memcpy the first lenvar bytes from the input string, and
   10172              :              add a null character.  Note that lenvar, the length of
   10173              :              the (trimmed) original string, has type gfc_charlen_type_node,
   10174              :              but newlen is size_type_node.  */
   10175           42 :           tree string_type_node = build_pointer_type (char_type_node);
   10176           42 :           tree newvar = gfc_create_var (string_type_node, NULL);
   10177           42 :           tree newlen = fold_build2_loc (input_location, PLUS_EXPR,
   10178              :                                          size_type_node,
   10179              :                                          fold_convert (size_type_node,
   10180              :                                                        lenvar),
   10181              :                                          size_one_node);
   10182           42 :           gfc_add_modify (&se->pre, newvar,
   10183              :                           gfc_call_malloc (&se->pre, string_type_node,
   10184              :                                            newlen));
   10185           42 :           tmp = build_call_expr_loc (input_location,
   10186              :                                      builtin_decl_explicit (BUILT_IN_MEMCPY),
   10187              :                                      3,
   10188              :                                      fold_convert (pvoid_type_node, newvar),
   10189              :                                      fold_convert (pvoid_type_node, sse.expr),
   10190              :                                      fold_convert (size_type_node, lenvar));
   10191           42 :           gfc_add_expr_to_block (&se->pre, tmp);
   10192           42 :           tmp = fold_build2_loc (input_location, POINTER_PLUS_EXPR,
   10193              :                                  string_type_node, newvar,
   10194              :                                  fold_convert (size_type_node, lenvar));
   10195           42 :           tmp = fold_build1_loc (input_location, INDIRECT_REF,
   10196              :                                  char_type_node, tmp);
   10197           42 :           gfc_add_modify (&se->pre, tmp,
   10198              :                           fold_convert (char_type_node, integer_zero_node));
   10199              : 
   10200              :           /* Remember to free the string later.  */
   10201           42 :           tmp = gfc_call_free (newvar);
   10202           42 :           gfc_add_expr_to_block (&se->post, tmp);
   10203              : 
   10204              :           /* Return the result.  */
   10205           42 :           se->expr = newvar;
   10206           42 :           se->string_length = fold_convert (gfc_charlen_type_node, newlen);
   10207           42 :           return;
   10208              :         }
   10209              :     }
   10210              :   else
   10211            0 :     gcc_unreachable ();
   10212              : }
   10213              : 
   10214              : 
   10215              : /* The following routine generates code for the intrinsic
   10216              :    subroutines from the ISO_C_BINDING module:
   10217              :     * C_F_POINTER
   10218              :     * C_F_PROCPOINTER.  */
   10219              : 
   10220              : static tree
   10221         3501 : conv_isocbinding_subroutine (gfc_code *code)
   10222              : {
   10223         3501 :   gfc_expr *cptr, *fptr, *shape, *lower;
   10224         3501 :   gfc_se se, cptrse, fptrse, shapese, lowerse;
   10225         3501 :   gfc_ss *shape_ss, *lower_ss;
   10226         3501 :   tree desc, dim, tmp, stride, offset, lbound, ubound;
   10227         3501 :   stmtblock_t body, block;
   10228         3501 :   gfc_loopinfo loop;
   10229         3501 :   gfc_actual_arglist *arg;
   10230              : 
   10231         3501 :   arg = code->ext.actual;
   10232         3501 :   cptr = arg->expr;
   10233         3501 :   fptr = arg->next->expr;
   10234         3501 :   shape = arg->next->next ? arg->next->next->expr : NULL;
   10235         3419 :   lower = shape && arg->next->next->next ? arg->next->next->next->expr : NULL;
   10236              : 
   10237         3501 :   gfc_init_se (&se, NULL);
   10238         3501 :   gfc_init_se (&cptrse, NULL);
   10239         3501 :   gfc_conv_expr (&cptrse, cptr);
   10240         3501 :   gfc_add_block_to_block (&se.pre, &cptrse.pre);
   10241         3501 :   gfc_add_block_to_block (&se.post, &cptrse.post);
   10242              : 
   10243         3501 :   gfc_init_se (&fptrse, NULL);
   10244         3501 :   if (fptr->rank == 0)
   10245              :     {
   10246         3015 :       fptrse.want_pointer = 1;
   10247         3015 :       gfc_conv_expr (&fptrse, fptr);
   10248         3015 :       gfc_add_block_to_block (&se.pre, &fptrse.pre);
   10249         3015 :       gfc_add_block_to_block (&se.post, &fptrse.post);
   10250         3015 :       if (fptr->symtree->n.sym->attr.proc_pointer
   10251           81 :           && fptr->symtree->n.sym->attr.dummy)
   10252           19 :         fptrse.expr = build_fold_indirect_ref_loc (input_location, fptrse.expr);
   10253         3015 :       se.expr
   10254         3015 :         = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (fptrse.expr),
   10255              :                            fptrse.expr,
   10256         3015 :                            fold_convert (TREE_TYPE (fptrse.expr), cptrse.expr));
   10257         3015 :       gfc_add_expr_to_block (&se.pre, se.expr);
   10258         3015 :       gfc_add_block_to_block (&se.pre, &se.post);
   10259         3015 :       return gfc_finish_block (&se.pre);
   10260              :     }
   10261              : 
   10262          486 :   gfc_start_block (&block);
   10263              : 
   10264              :   /* Get the descriptor of the Fortran pointer.  */
   10265          486 :   fptrse.descriptor_only = 1;
   10266          486 :   gfc_conv_expr_descriptor (&fptrse, fptr);
   10267          486 :   gfc_add_block_to_block (&block, &fptrse.pre);
   10268          486 :   desc = fptrse.expr;
   10269              : 
   10270              :   /* Set the span field.  */
   10271          486 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
   10272          486 :   tmp = fold_convert (gfc_array_index_type, tmp);
   10273          486 :   gfc_conv_descriptor_span_set (&block, desc, tmp);
   10274              : 
   10275              :   /* Set data value, dtype, and offset.  */
   10276          486 :   tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc));
   10277          486 :   gfc_conv_descriptor_data_set (&block, desc, fold_convert (tmp, cptrse.expr));
   10278          486 :   gfc_conv_descriptor_dtype_set (&block, desc,
   10279          486 :                                  gfc_get_dtype (TREE_TYPE (desc)));
   10280              : 
   10281              :   /* Start scalarization of the bounds, using the shape argument.  */
   10282              : 
   10283          486 :   shape_ss = gfc_walk_expr (shape);
   10284          486 :   gcc_assert (shape_ss != gfc_ss_terminator);
   10285          486 :   gfc_init_se (&shapese, NULL);
   10286          486 :   if (lower)
   10287              :     {
   10288           12 :       lower_ss = gfc_walk_expr (lower);
   10289           12 :       gcc_assert (lower_ss != gfc_ss_terminator);
   10290           12 :       gfc_init_se (&lowerse, NULL);
   10291              :     }
   10292              : 
   10293          486 :   gfc_init_loopinfo (&loop);
   10294          486 :   gfc_add_ss_to_loop (&loop, shape_ss);
   10295          486 :   if (lower)
   10296           12 :     gfc_add_ss_to_loop (&loop, lower_ss);
   10297          486 :   gfc_conv_ss_startstride (&loop);
   10298          486 :   gfc_conv_loop_setup (&loop, &fptr->where);
   10299          486 :   gfc_mark_ss_chain_used (shape_ss, 1);
   10300          486 :   if (lower)
   10301           12 :     gfc_mark_ss_chain_used (lower_ss, 1);
   10302              : 
   10303          486 :   gfc_copy_loopinfo_to_se (&shapese, &loop);
   10304          486 :   shapese.ss = shape_ss;
   10305          486 :   if (lower)
   10306              :     {
   10307           12 :       gfc_copy_loopinfo_to_se (&lowerse, &loop);
   10308           12 :       lowerse.ss = lower_ss;
   10309              :     }
   10310              : 
   10311          486 :   stride = gfc_create_var (gfc_array_index_type, "stride");
   10312          486 :   offset = gfc_create_var (gfc_array_index_type, "offset");
   10313          486 :   gfc_add_modify (&block, stride, gfc_index_one_node);
   10314          486 :   gfc_add_modify (&block, offset, gfc_index_zero_node);
   10315              : 
   10316              :   /* Loop body.  */
   10317          486 :   gfc_start_scalarized_body (&loop, &body);
   10318              : 
   10319          486 :   dim = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
   10320              :                          loop.loopvar[0], loop.from[0]);
   10321              : 
   10322          486 :   if (lower)
   10323              :     {
   10324           12 :       gfc_conv_expr (&lowerse, lower);
   10325           12 :       gfc_add_block_to_block (&body, &lowerse.pre);
   10326           12 :       lbound = fold_convert (gfc_array_index_type, lowerse.expr);
   10327           12 :       gfc_add_block_to_block (&body, &lowerse.post);
   10328              :     }
   10329              :   else
   10330          474 :     lbound = gfc_index_one_node;
   10331              : 
   10332              :   /* Set bounds and stride.  */
   10333          486 :   gfc_conv_descriptor_lbound_set (&body, desc, dim, lbound);
   10334          486 :   gfc_conv_descriptor_stride_set (&body, desc, dim, stride);
   10335              : 
   10336          486 :   gfc_conv_expr (&shapese, shape);
   10337          486 :   gfc_add_block_to_block (&body, &shapese.pre);
   10338          486 :   ubound = fold_build2_loc (
   10339              :     input_location, MINUS_EXPR, gfc_array_index_type,
   10340              :     fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, lbound,
   10341              :                      fold_convert (gfc_array_index_type, shapese.expr)),
   10342              :     gfc_index_one_node);
   10343          486 :   gfc_conv_descriptor_ubound_set (&body, desc, dim, ubound);
   10344          486 :   gfc_add_block_to_block (&body, &shapese.post);
   10345              : 
   10346              :   /* Calculate offset.  */
   10347          486 :   tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   10348              :                          stride, lbound);
   10349          486 :   gfc_add_modify (&body, offset,
   10350              :                   fold_build2_loc (input_location, PLUS_EXPR,
   10351              :                                    gfc_array_index_type, offset, tmp));
   10352              : 
   10353              :   /* Update stride.  */
   10354          486 :   gfc_add_modify (
   10355              :     &body, stride,
   10356              :     fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, stride,
   10357              :                      fold_convert (gfc_array_index_type, shapese.expr)));
   10358              :   /* Finish scalarization loop.  */
   10359          486 :   gfc_trans_scalarizing_loops (&loop, &body);
   10360          486 :   gfc_add_block_to_block (&block, &loop.pre);
   10361          486 :   gfc_add_block_to_block (&block, &loop.post);
   10362          486 :   gfc_add_block_to_block (&block, &fptrse.post);
   10363          486 :   gfc_cleanup_loop (&loop);
   10364              : 
   10365          486 :   gfc_add_modify (&block, offset,
   10366              :                   fold_build1_loc (input_location, NEGATE_EXPR,
   10367              :                                    gfc_array_index_type, offset));
   10368          486 :   gfc_conv_descriptor_offset_set (&block, desc, offset);
   10369              : 
   10370          486 :   gfc_add_expr_to_block (&se.pre, gfc_finish_block (&block));
   10371          486 :   gfc_add_block_to_block (&se.pre, &se.post);
   10372          486 :   return gfc_finish_block (&se.pre);
   10373              : }
   10374              : 
   10375              : 
   10376              : /* The following routine generates code for both forms of the intrinsic
   10377              :    subroutine C_F_STRPOINTER from the ISO_C_BINDING module.  */
   10378              : static tree
   10379           60 : conv_isocbinding_subroutine_strpointer (gfc_code *code)
   10380              : {
   10381           60 :   gfc_actual_arglist *arg = code->ext.actual;
   10382           60 :   gfc_expr *arg0 = arg->expr;
   10383           60 :   gfc_expr *fstrptr = arg->next->expr;
   10384           60 :   gfc_expr *nchars = arg->next->next->expr;
   10385           60 :   tree ptr;
   10386           60 :   tree size = NULL_TREE;
   10387           60 :   tree nc = NULL_TREE;
   10388           60 :   tree fstrptr_ptr, fstrptr_len;
   10389           60 :   stmtblock_t block;
   10390           60 :   gfc_init_block (&block);
   10391           60 :   gfc_se se0, se1, se2;
   10392           60 :   gfc_init_se (&se0, NULL);
   10393           60 :   gfc_init_se (&se1, NULL);
   10394           60 :   gfc_init_se (&se2, NULL);
   10395              : 
   10396              :   /* arg0 can either be a simply contiguous rank-one character array,
   10397              :      or a scalar of type c_ptr that points to a contiguous array.
   10398              :      In the first case nchars may be omitted and defaults to the size
   10399              :      of the array.  */
   10400           60 :   if (arg0->rank == 1)
   10401              :     {
   10402           42 :       gfc_array_ref *ar = gfc_find_array_ref (arg0);
   10403           42 :       if (ar->as && ar->as->type == AS_ASSUMED_SIZE
   10404           12 :           && (ar->type == AR_FULL || ar->end[0] == nullptr))
   10405              :         /* No size available.  */
   10406           12 :         gfc_conv_array_parameter (&se0, arg0, true, NULL, NULL, NULL);
   10407              :       else
   10408              :         {
   10409           30 :           gfc_conv_array_parameter (&se0, arg0, true, NULL, NULL, &size);
   10410           30 :           gcc_assert (size);
   10411              :         }
   10412           42 :       ptr = se0.expr;
   10413              :     }
   10414           18 :   else if (arg0->rank == 0)
   10415              :     {
   10416              :       /* Scalar case.  arg0 is a C pointer to the string, and the
   10417              :          nchars argument is required.  */
   10418           18 :       gfc_conv_expr (&se0, arg0);
   10419           18 :       ptr = se0.expr;
   10420              :       /* We already issued a diagnostic for this in parsing.  */
   10421           18 :       gcc_assert (nchars);
   10422              :     }
   10423              :   else
   10424            0 :     gcc_unreachable ();
   10425              : 
   10426              :   /* Translate the fortran array pointer argument.  AFAICT the
   10427              :      representation here is that this returns the pointer location in
   10428              :      se1.expr and there is a separate decl for the length.
   10429              :      Of course none of this is properly documented....  :-(  */
   10430           60 :   gfc_conv_expr (&se1, fstrptr);
   10431           60 :   fstrptr_ptr = se1.expr;
   10432           60 :   gcc_assert (fstrptr->ts.u.cl && fstrptr->ts.u.cl->backend_decl);
   10433           60 :   fstrptr_len = fstrptr->ts.u.cl->backend_decl;
   10434              : 
   10435              :   /* Translate nchars, if provided.  If we have both the array size
   10436              :      and nchars, take the minimum value.  NC is the tree expr to hold
   10437              :      the value.  */
   10438           60 :   if (nchars)
   10439              :     {
   10440           30 :       gfc_conv_expr (&se2, nchars);
   10441           30 :       nc = se2.expr;
   10442           30 :       if (size)
   10443            0 :         nc = fold_build2_loc (input_location, MIN_EXPR,
   10444            0 :                               TREE_TYPE (nc), nc, size);
   10445              :       /* Check for the case where an optional dummy parameter is
   10446              :          passed as the optional nchars argument.  It's not supposed to
   10447              :          be omitted if we don't also have an array size; rather than
   10448              :          produce a run-time error, assume size 0.  */
   10449           30 :       if (nchars->expr_type == EXPR_VARIABLE
   10450           18 :           && nchars->symtree->n.sym->attr.dummy
   10451           18 :           && nchars->symtree->n.sym->attr.optional)
   10452              :         {
   10453           12 :           tree present = gfc_conv_expr_present (nchars->symtree->n.sym);
   10454           12 :           nc = build3_loc (input_location, COND_EXPR,
   10455           12 :                            TREE_TYPE (nc), present, nc,
   10456           24 :                            size ? size : build_int_cst (TREE_TYPE (nc), 0));
   10457              :         }
   10458              :     }
   10459              :   else
   10460              :     {
   10461           30 :       gcc_assert (size);
   10462              :       nc = size;
   10463              :     }
   10464              : 
   10465              :   /* Collect argument side-effect statements.  */
   10466           60 :   gfc_add_block_to_block (&block, &se0.pre);
   10467           60 :   gfc_add_block_to_block (&block, &se1.pre);
   10468           60 :   gfc_add_block_to_block (&block, &se2.pre);
   10469              : 
   10470              :   /* Generate a call to builtin_strnlen to get the C string length
   10471              :      for the output fstrptr.  */
   10472           60 :   ptr = gfc_evaluate_now (ptr, &block);
   10473           60 :   size = build_call_expr_loc (input_location,
   10474              :                               builtin_decl_explicit (BUILT_IN_STRNLEN), 2,
   10475              :                               fold_convert (const_ptr_type_node, ptr),
   10476              :                               fold_convert (size_type_node, nc));
   10477              : 
   10478              :   /* Stuff the raw C char pointer PTR and actual length SIZE into fstrptr.  */
   10479           60 :   gfc_add_modify (&block, fstrptr_ptr,
   10480           60 :                   fold_convert (TREE_TYPE (fstrptr_ptr), ptr));
   10481           60 :   gfc_add_modify (&block, fstrptr_len,
   10482              :                   fold_convert (gfc_charlen_type_node, size));
   10483              : 
   10484              :   /* Collect argument cleanups.  */
   10485           60 :   gfc_add_block_to_block (&block, &se2.post);
   10486           60 :   gfc_add_block_to_block (&block, &se1.post);
   10487           60 :   gfc_add_block_to_block (&block, &se0.post);
   10488              : 
   10489           60 :   return gfc_finish_block (&block);
   10490              : }
   10491              : 
   10492              : /* Save and restore floating-point state.  */
   10493              : 
   10494              : tree
   10495          943 : gfc_save_fp_state (stmtblock_t *block)
   10496              : {
   10497          943 :   tree type, fpstate, tmp;
   10498              : 
   10499          943 :   type = build_array_type (char_type_node,
   10500              :                            build_range_type (size_type_node, size_zero_node,
   10501              :                                              size_int (GFC_FPE_STATE_BUFFER_SIZE)));
   10502          943 :   fpstate = gfc_create_var (type, "fpstate");
   10503          943 :   fpstate = gfc_build_addr_expr (pvoid_type_node, fpstate);
   10504              : 
   10505          943 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_entry,
   10506              :                              1, fpstate);
   10507          943 :   gfc_add_expr_to_block (block, tmp);
   10508              : 
   10509          943 :   return fpstate;
   10510              : }
   10511              : 
   10512              : 
   10513              : void
   10514          943 : gfc_restore_fp_state (stmtblock_t *block, tree fpstate)
   10515              : {
   10516          943 :   tree tmp;
   10517              : 
   10518          943 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_exit,
   10519              :                              1, fpstate);
   10520          943 :   gfc_add_expr_to_block (block, tmp);
   10521          943 : }
   10522              : 
   10523              : 
   10524              : /* Generate code for arguments of IEEE functions.  */
   10525              : 
   10526              : static void
   10527        12457 : conv_ieee_function_args (gfc_se *se, gfc_expr *expr, tree *argarray,
   10528              :                          int nargs)
   10529              : {
   10530        12457 :   gfc_actual_arglist *actual;
   10531        12457 :   gfc_expr *e;
   10532        12457 :   gfc_se argse;
   10533        12457 :   int arg;
   10534              : 
   10535        12457 :   actual = expr->value.function.actual;
   10536        34461 :   for (arg = 0; arg < nargs; arg++, actual = actual->next)
   10537              :     {
   10538        22004 :       gcc_assert (actual);
   10539        22004 :       e = actual->expr;
   10540              : 
   10541        22004 :       gfc_init_se (&argse, se);
   10542        22004 :       gfc_conv_expr_val (&argse, e);
   10543              : 
   10544        22004 :       gfc_add_block_to_block (&se->pre, &argse.pre);
   10545        22004 :       gfc_add_block_to_block (&se->post, &argse.post);
   10546        22004 :       argarray[arg] = argse.expr;
   10547              :     }
   10548        12457 : }
   10549              : 
   10550              : 
   10551              : /* Generate code for intrinsics IEEE_IS_NAN, IEEE_IS_FINITE
   10552              :    and IEEE_UNORDERED, which translate directly to GCC type-generic
   10553              :    built-ins.  */
   10554              : 
   10555              : static void
   10556         1062 : conv_intrinsic_ieee_builtin (gfc_se * se, gfc_expr * expr,
   10557              :                              enum built_in_function code, int nargs)
   10558              : {
   10559         1062 :   tree args[2];
   10560         1062 :   gcc_assert ((unsigned) nargs <= ARRAY_SIZE (args));
   10561              : 
   10562         1062 :   conv_ieee_function_args (se, expr, args, nargs);
   10563         1062 :   se->expr = build_call_expr_loc_array (input_location,
   10564              :                                         builtin_decl_explicit (code),
   10565              :                                         nargs, args);
   10566         2388 :   STRIP_TYPE_NOPS (se->expr);
   10567         1062 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
   10568         1062 : }
   10569              : 
   10570              : 
   10571              : /* Generate code for intrinsics IEEE_SIGNBIT.  */
   10572              : 
   10573              : static void
   10574          624 : conv_intrinsic_ieee_signbit (gfc_se * se, gfc_expr * expr)
   10575              : {
   10576          624 :   tree arg, signbit;
   10577              : 
   10578          624 :   conv_ieee_function_args (se, expr, &arg, 1);
   10579          624 :   signbit = build_call_expr_loc (input_location,
   10580              :                                  builtin_decl_explicit (BUILT_IN_SIGNBIT),
   10581              :                                  1, arg);
   10582          624 :   signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10583              :                              signbit, integer_zero_node);
   10584          624 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), signbit);
   10585          624 : }
   10586              : 
   10587              : 
   10588              : /* Generate code for IEEE_IS_NORMAL intrinsic:
   10589              :      IEEE_IS_NORMAL(x) --> (__builtin_isnormal(x) || x == 0)  */
   10590              : 
   10591              : static void
   10592          312 : conv_intrinsic_ieee_is_normal (gfc_se * se, gfc_expr * expr)
   10593              : {
   10594          312 :   tree arg, isnormal, iszero;
   10595              : 
   10596              :   /* Convert arg, evaluate it only once.  */
   10597          312 :   conv_ieee_function_args (se, expr, &arg, 1);
   10598          312 :   arg = gfc_evaluate_now (arg, &se->pre);
   10599              : 
   10600          312 :   isnormal = build_call_expr_loc (input_location,
   10601              :                                   builtin_decl_explicit (BUILT_IN_ISNORMAL),
   10602              :                                   1, arg);
   10603          312 :   iszero = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
   10604          312 :                             build_real_from_int_cst (TREE_TYPE (arg),
   10605          312 :                                                      integer_zero_node));
   10606          312 :   se->expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   10607              :                               logical_type_node, isnormal, iszero);
   10608          312 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
   10609          312 : }
   10610              : 
   10611              : 
   10612              : /* Generate code for IEEE_IS_NEGATIVE intrinsic:
   10613              :      IEEE_IS_NEGATIVE(x) --> (__builtin_signbit(x) && !__builtin_isnan(x))  */
   10614              : 
   10615              : static void
   10616          312 : conv_intrinsic_ieee_is_negative (gfc_se * se, gfc_expr * expr)
   10617              : {
   10618          312 :   tree arg, signbit, isnan;
   10619              : 
   10620              :   /* Convert arg, evaluate it only once.  */
   10621          312 :   conv_ieee_function_args (se, expr, &arg, 1);
   10622          312 :   arg = gfc_evaluate_now (arg, &se->pre);
   10623              : 
   10624          312 :   isnan = build_call_expr_loc (input_location,
   10625              :                                builtin_decl_explicit (BUILT_IN_ISNAN),
   10626              :                                1, arg);
   10627          936 :   STRIP_TYPE_NOPS (isnan);
   10628              : 
   10629          312 :   signbit = build_call_expr_loc (input_location,
   10630              :                                  builtin_decl_explicit (BUILT_IN_SIGNBIT),
   10631              :                                  1, arg);
   10632          312 :   signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10633              :                              signbit, integer_zero_node);
   10634              : 
   10635          312 :   se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   10636              :                               logical_type_node, signbit,
   10637              :                               fold_build1_loc (input_location, TRUTH_NOT_EXPR,
   10638          312 :                                                TREE_TYPE(isnan), isnan));
   10639              : 
   10640          312 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
   10641          312 : }
   10642              : 
   10643              : 
   10644              : /* Generate code for IEEE_LOGB and IEEE_RINT.  */
   10645              : 
   10646              : static void
   10647          240 : conv_intrinsic_ieee_logb_rint (gfc_se * se, gfc_expr * expr,
   10648              :                                enum built_in_function code)
   10649              : {
   10650          240 :   tree arg, decl, call, fpstate;
   10651          240 :   int argprec;
   10652              : 
   10653          240 :   conv_ieee_function_args (se, expr, &arg, 1);
   10654          240 :   argprec = TYPE_PRECISION (TREE_TYPE (arg));
   10655          240 :   decl = builtin_decl_for_precision (code, argprec);
   10656              : 
   10657              :   /* Save floating-point state.  */
   10658          240 :   fpstate = gfc_save_fp_state (&se->pre);
   10659              : 
   10660              :   /* Make the function call.  */
   10661          240 :   call = build_call_expr_loc (input_location, decl, 1, arg);
   10662          240 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), call);
   10663              : 
   10664              :   /* Restore floating-point state.  */
   10665          240 :   gfc_restore_fp_state (&se->post, fpstate);
   10666          240 : }
   10667              : 
   10668              : 
   10669              : /* Generate code for IEEE_REM.  */
   10670              : 
   10671              : static void
   10672           84 : conv_intrinsic_ieee_rem (gfc_se * se, gfc_expr * expr)
   10673              : {
   10674           84 :   tree args[2], decl, call, fpstate;
   10675           84 :   int argprec;
   10676              : 
   10677           84 :   conv_ieee_function_args (se, expr, args, 2);
   10678              : 
   10679              :   /* If arguments have unequal size, convert them to the larger.  */
   10680           84 :   if (TYPE_PRECISION (TREE_TYPE (args[0]))
   10681           84 :       > TYPE_PRECISION (TREE_TYPE (args[1])))
   10682            6 :     args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
   10683           78 :   else if (TYPE_PRECISION (TREE_TYPE (args[1]))
   10684           78 :            > TYPE_PRECISION (TREE_TYPE (args[0])))
   10685           24 :     args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
   10686              : 
   10687           84 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   10688           84 :   decl = builtin_decl_for_precision (BUILT_IN_REMAINDER, argprec);
   10689              : 
   10690              :   /* Save floating-point state.  */
   10691           84 :   fpstate = gfc_save_fp_state (&se->pre);
   10692              : 
   10693              :   /* Make the function call.  */
   10694           84 :   call = build_call_expr_loc_array (input_location, decl, 2, args);
   10695           84 :   se->expr = fold_convert (TREE_TYPE (args[0]), call);
   10696              : 
   10697              :   /* Restore floating-point state.  */
   10698           84 :   gfc_restore_fp_state (&se->post, fpstate);
   10699           84 : }
   10700              : 
   10701              : 
   10702              : /* Generate code for IEEE_NEXT_AFTER.  */
   10703              : 
   10704              : static void
   10705          180 : conv_intrinsic_ieee_next_after (gfc_se * se, gfc_expr * expr)
   10706              : {
   10707          180 :   tree args[2], decl, call, fpstate;
   10708          180 :   int argprec;
   10709              : 
   10710          180 :   conv_ieee_function_args (se, expr, args, 2);
   10711              : 
   10712              :   /* Result has the characteristics of first argument.  */
   10713          180 :   args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
   10714          180 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   10715          180 :   decl = builtin_decl_for_precision (BUILT_IN_NEXTAFTER, argprec);
   10716              : 
   10717              :   /* Save floating-point state.  */
   10718          180 :   fpstate = gfc_save_fp_state (&se->pre);
   10719              : 
   10720              :   /* Make the function call.  */
   10721          180 :   call = build_call_expr_loc_array (input_location, decl, 2, args);
   10722          180 :   se->expr = fold_convert (TREE_TYPE (args[0]), call);
   10723              : 
   10724              :   /* Restore floating-point state.  */
   10725          180 :   gfc_restore_fp_state (&se->post, fpstate);
   10726          180 : }
   10727              : 
   10728              : 
   10729              : /* Generate code for IEEE_SCALB.  */
   10730              : 
   10731              : static void
   10732          228 : conv_intrinsic_ieee_scalb (gfc_se * se, gfc_expr * expr)
   10733              : {
   10734          228 :   tree args[2], decl, call, huge, type;
   10735          228 :   int argprec, n;
   10736              : 
   10737          228 :   conv_ieee_function_args (se, expr, args, 2);
   10738              : 
   10739              :   /* Result has the characteristics of first argument.  */
   10740          228 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   10741          228 :   decl = builtin_decl_for_precision (BUILT_IN_SCALBN, argprec);
   10742              : 
   10743          228 :   if (TYPE_PRECISION (TREE_TYPE (args[1])) > TYPE_PRECISION (integer_type_node))
   10744              :     {
   10745              :       /* We need to fold the integer into the range of a C int.  */
   10746           18 :       args[1] = gfc_evaluate_now (args[1], &se->pre);
   10747           18 :       type = TREE_TYPE (args[1]);
   10748              : 
   10749           18 :       n = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
   10750           18 :       huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
   10751              :                                    gfc_c_int_kind);
   10752           18 :       huge = fold_convert (type, huge);
   10753           18 :       args[1] = fold_build2_loc (input_location, MIN_EXPR, type, args[1],
   10754              :                                  huge);
   10755           18 :       args[1] = fold_build2_loc (input_location, MAX_EXPR, type, args[1],
   10756              :                                  fold_build1_loc (input_location, NEGATE_EXPR,
   10757              :                                                   type, huge));
   10758              :     }
   10759              : 
   10760          228 :   args[1] = fold_convert (integer_type_node, args[1]);
   10761              : 
   10762              :   /* Make the function call.  */
   10763          228 :   call = build_call_expr_loc_array (input_location, decl, 2, args);
   10764          228 :   se->expr = fold_convert (TREE_TYPE (args[0]), call);
   10765          228 : }
   10766              : 
   10767              : 
   10768              : /* Generate code for IEEE_COPY_SIGN.  */
   10769              : 
   10770              : static void
   10771          576 : conv_intrinsic_ieee_copy_sign (gfc_se * se, gfc_expr * expr)
   10772              : {
   10773          576 :   tree args[2], decl, sign;
   10774          576 :   int argprec;
   10775              : 
   10776          576 :   conv_ieee_function_args (se, expr, args, 2);
   10777              : 
   10778              :   /* Get the sign of the second argument.  */
   10779          576 :   sign = build_call_expr_loc (input_location,
   10780              :                               builtin_decl_explicit (BUILT_IN_SIGNBIT),
   10781              :                               1, args[1]);
   10782          576 :   sign = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10783              :                           sign, integer_zero_node);
   10784              : 
   10785              :   /* Create a value of one, with the right sign.  */
   10786          576 :   sign = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
   10787              :                           sign,
   10788              :                           fold_build1_loc (input_location, NEGATE_EXPR,
   10789              :                                            integer_type_node,
   10790              :                                            integer_one_node),
   10791              :                           integer_one_node);
   10792          576 :   args[1] = fold_convert (TREE_TYPE (args[0]), sign);
   10793              : 
   10794          576 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   10795          576 :   decl = builtin_decl_for_precision (BUILT_IN_COPYSIGN, argprec);
   10796              : 
   10797          576 :   se->expr = build_call_expr_loc_array (input_location, decl, 2, args);
   10798          576 : }
   10799              : 
   10800              : 
   10801              : /* Generate code for IEEE_CLASS.  */
   10802              : 
   10803              : static void
   10804          648 : conv_intrinsic_ieee_class (gfc_se *se, gfc_expr *expr)
   10805              : {
   10806          648 :   tree arg, c, t1, t2, t3, t4;
   10807              : 
   10808              :   /* Convert arg, evaluate it only once.  */
   10809          648 :   conv_ieee_function_args (se, expr, &arg, 1);
   10810          648 :   arg = gfc_evaluate_now (arg, &se->pre);
   10811              : 
   10812          648 :   c = build_call_expr_loc (input_location,
   10813              :                            builtin_decl_explicit (BUILT_IN_FPCLASSIFY), 6,
   10814              :                            build_int_cst (integer_type_node, IEEE_QUIET_NAN),
   10815              :                            build_int_cst (integer_type_node,
   10816              :                                           IEEE_POSITIVE_INF),
   10817              :                            build_int_cst (integer_type_node,
   10818              :                                           IEEE_POSITIVE_NORMAL),
   10819              :                            build_int_cst (integer_type_node,
   10820              :                                           IEEE_POSITIVE_DENORMAL),
   10821              :                            build_int_cst (integer_type_node,
   10822              :                                           IEEE_POSITIVE_ZERO),
   10823              :                            arg);
   10824          648 :   c = gfc_evaluate_now (c, &se->pre);
   10825          648 :   t1 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   10826              :                         c, build_int_cst (integer_type_node,
   10827              :                                           IEEE_QUIET_NAN));
   10828          648 :   t2 = build_call_expr_loc (input_location,
   10829              :                             builtin_decl_explicit (BUILT_IN_ISSIGNALING), 1,
   10830              :                             arg);
   10831          648 :   t2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10832          648 :                         t2, build_zero_cst (TREE_TYPE (t2)));
   10833          648 :   t1 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   10834              :                         logical_type_node, t1, t2);
   10835          648 :   t3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   10836              :                         c, build_int_cst (integer_type_node,
   10837              :                                           IEEE_POSITIVE_ZERO));
   10838          648 :   t4 = build_call_expr_loc (input_location,
   10839              :                             builtin_decl_explicit (BUILT_IN_SIGNBIT), 1,
   10840              :                             arg);
   10841          648 :   t4 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10842          648 :                         t4, build_zero_cst (TREE_TYPE (t4)));
   10843          648 :   t3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   10844              :                         logical_type_node, t3, t4);
   10845          648 :   int s = IEEE_NEGATIVE_ZERO + IEEE_POSITIVE_ZERO;
   10846          648 :   gcc_assert (IEEE_NEGATIVE_INF == s - IEEE_POSITIVE_INF);
   10847          648 :   gcc_assert (IEEE_NEGATIVE_NORMAL == s - IEEE_POSITIVE_NORMAL);
   10848          648 :   gcc_assert (IEEE_NEGATIVE_DENORMAL == s - IEEE_POSITIVE_DENORMAL);
   10849          648 :   gcc_assert (IEEE_NEGATIVE_SUBNORMAL == s - IEEE_POSITIVE_SUBNORMAL);
   10850          648 :   gcc_assert (IEEE_NEGATIVE_ZERO == s - IEEE_POSITIVE_ZERO);
   10851          648 :   t4 = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (c),
   10852          648 :                         build_int_cst (TREE_TYPE (c), s), c);
   10853          648 :   t3 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c),
   10854              :                         t3, t4, c);
   10855          648 :   t1 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c), t1,
   10856          648 :                         build_int_cst (TREE_TYPE (c), IEEE_SIGNALING_NAN),
   10857              :                         t3);
   10858          648 :   tree type = gfc_typenode_for_spec (&expr->ts);
   10859              :   /* Perform a quick sanity check that the return type is
   10860              :      IEEE_CLASS_TYPE derived type defined in
   10861              :      libgfortran/ieee/ieee_arithmetic.F90
   10862              :      Primarily check that it is a derived type with a single
   10863              :      member in it.  */
   10864          648 :   gcc_assert (TREE_CODE (type) == RECORD_TYPE);
   10865          648 :   tree field = NULL_TREE;
   10866         1296 :   for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
   10867          648 :     if (TREE_CODE (f) == FIELD_DECL)
   10868              :       {
   10869          648 :         gcc_assert (field == NULL_TREE);
   10870              :         field = f;
   10871              :       }
   10872          648 :   gcc_assert (field);
   10873          648 :   t1 = fold_convert (TREE_TYPE (field), t1);
   10874          648 :   se->expr = build_constructor_single (type, field, t1);
   10875          648 : }
   10876              : 
   10877              : 
   10878              : /* Generate code for IEEE_VALUE.  */
   10879              : 
   10880              : static void
   10881         1111 : conv_intrinsic_ieee_value (gfc_se *se, gfc_expr *expr)
   10882              : {
   10883         1111 :   tree args[2], arg, ret, tmp;
   10884         1111 :   stmtblock_t body;
   10885              : 
   10886              :   /* Convert args, evaluate the second one only once.  */
   10887         1111 :   conv_ieee_function_args (se, expr, args, 2);
   10888         1111 :   arg = gfc_evaluate_now (args[1], &se->pre);
   10889              : 
   10890         1111 :   tree type = TREE_TYPE (arg);
   10891              :   /* Perform a quick sanity check that the second argument's type is
   10892              :      IEEE_CLASS_TYPE derived type defined in
   10893              :      libgfortran/ieee/ieee_arithmetic.F90
   10894              :      Primarily check that it is a derived type with a single
   10895              :      member in it.  */
   10896         1111 :   gcc_assert (TREE_CODE (type) == RECORD_TYPE);
   10897         1111 :   tree field = NULL_TREE;
   10898         2222 :   for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
   10899         1111 :     if (TREE_CODE (f) == FIELD_DECL)
   10900              :       {
   10901         1111 :         gcc_assert (field == NULL_TREE);
   10902              :         field = f;
   10903              :       }
   10904         1111 :   gcc_assert (field);
   10905         1111 :   arg = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
   10906              :                          arg, field, NULL_TREE);
   10907         1111 :   arg = gfc_evaluate_now (arg, &se->pre);
   10908              : 
   10909         1111 :   type = gfc_typenode_for_spec (&expr->ts);
   10910         1111 :   gcc_assert (SCALAR_FLOAT_TYPE_P (type));
   10911         1111 :   ret = gfc_create_var (type, NULL);
   10912              : 
   10913         1111 :   gfc_init_block (&body);
   10914              : 
   10915         1111 :   tree end_label = gfc_build_label_decl (NULL_TREE);
   10916        13332 :   for (int c = IEEE_SIGNALING_NAN; c <= IEEE_POSITIVE_INF; ++c)
   10917              :     {
   10918        11110 :       tree label = gfc_build_label_decl (NULL_TREE);
   10919        11110 :       tree low = build_int_cst (TREE_TYPE (arg), c);
   10920        11110 :       tmp = build_case_label (low, low, label);
   10921        11110 :       gfc_add_expr_to_block (&body, tmp);
   10922              : 
   10923        11110 :       REAL_VALUE_TYPE real;
   10924        11110 :       int k;
   10925        11110 :       switch (c)
   10926              :         {
   10927         1111 :         case IEEE_SIGNALING_NAN:
   10928         1111 :           real_nan (&real, "", 0, TYPE_MODE (type));
   10929         1111 :           break;
   10930         1111 :         case IEEE_QUIET_NAN:
   10931         1111 :           real_nan (&real, "", 1, TYPE_MODE (type));
   10932         1111 :           break;
   10933         1111 :         case IEEE_NEGATIVE_INF:
   10934         1111 :           real_inf (&real);
   10935         1111 :           real = real_value_negate (&real);
   10936         1111 :           break;
   10937         1111 :         case IEEE_NEGATIVE_NORMAL:
   10938         1111 :           real_from_integer (&real, TYPE_MODE (type), -42, SIGNED);
   10939         1111 :           break;
   10940         1111 :         case IEEE_NEGATIVE_DENORMAL:
   10941         1111 :           k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
   10942         1111 :           real_from_mpfr (&real, gfc_real_kinds[k].tiny,
   10943              :                           type, GFC_RND_MODE);
   10944         1111 :           real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
   10945         1111 :           real = real_value_negate (&real);
   10946         1111 :           break;
   10947         1111 :         case IEEE_NEGATIVE_ZERO:
   10948         1111 :           real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
   10949         1111 :           real = real_value_negate (&real);
   10950         1111 :           break;
   10951         1111 :         case IEEE_POSITIVE_ZERO:
   10952              :           /* Make this also the default: label.  The other possibility
   10953              :              would be to add a separate default: label followed by
   10954              :              __builtin_unreachable ().  */
   10955         1111 :           label = gfc_build_label_decl (NULL_TREE);
   10956         1111 :           tmp = build_case_label (NULL_TREE, NULL_TREE, label);
   10957         1111 :           gfc_add_expr_to_block (&body, tmp);
   10958         1111 :           real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
   10959         1111 :           break;
   10960         1111 :         case IEEE_POSITIVE_DENORMAL:
   10961         1111 :           k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
   10962         1111 :           real_from_mpfr (&real, gfc_real_kinds[k].tiny,
   10963              :                           type, GFC_RND_MODE);
   10964         1111 :           real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
   10965         1111 :           break;
   10966         1111 :         case IEEE_POSITIVE_NORMAL:
   10967         1111 :           real_from_integer (&real, TYPE_MODE (type), 42, SIGNED);
   10968         1111 :           break;
   10969         1111 :         case IEEE_POSITIVE_INF:
   10970         1111 :           real_inf (&real);
   10971         1111 :           break;
   10972              :         default:
   10973              :           gcc_unreachable ();
   10974              :         }
   10975              : 
   10976        11110 :       tree val = build_real (type, real);
   10977        11110 :       gfc_add_modify (&body, ret, val);
   10978              : 
   10979        11110 :       tmp = build1_v (GOTO_EXPR, end_label);
   10980        11110 :       gfc_add_expr_to_block (&body, tmp);
   10981              :     }
   10982              : 
   10983         1111 :   tmp = gfc_finish_block (&body);
   10984         1111 :   tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, arg, tmp);
   10985         1111 :   gfc_add_expr_to_block (&se->pre, tmp);
   10986              : 
   10987         1111 :   tmp = build1_v (LABEL_EXPR, end_label);
   10988         1111 :   gfc_add_expr_to_block (&se->pre, tmp);
   10989              : 
   10990         1111 :   se->expr = ret;
   10991         1111 : }
   10992              : 
   10993              : 
   10994              : /* Generate code for IEEE_FMA.  */
   10995              : 
   10996              : static void
   10997          120 : conv_intrinsic_ieee_fma (gfc_se * se, gfc_expr * expr)
   10998              : {
   10999          120 :   tree args[3], decl, call;
   11000          120 :   int argprec;
   11001              : 
   11002          120 :   conv_ieee_function_args (se, expr, args, 3);
   11003              : 
   11004              :   /* All three arguments should have the same type.  */
   11005          120 :   gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
   11006          120 :   gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[2])));
   11007              : 
   11008              :   /* Call the type-generic FMA built-in.  */
   11009          120 :   argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
   11010          120 :   decl = builtin_decl_for_precision (BUILT_IN_FMA, argprec);
   11011          120 :   call = build_call_expr_loc_array (input_location, decl, 3, args);
   11012              : 
   11013              :   /* Convert to the final type.  */
   11014          120 :   se->expr = fold_convert (TREE_TYPE (args[0]), call);
   11015          120 : }
   11016              : 
   11017              : 
   11018              : /* Generate code for IEEE_{MIN,MAX}_NUM{,_MAG}.  */
   11019              : 
   11020              : static void
   11021         3072 : conv_intrinsic_ieee_minmax (gfc_se * se, gfc_expr * expr, int max,
   11022              :                             const char *name)
   11023              : {
   11024         3072 :   tree args[2], func;
   11025         3072 :   built_in_function fn;
   11026              : 
   11027         3072 :   conv_ieee_function_args (se, expr, args, 2);
   11028         3072 :   gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
   11029         3072 :   args[0] = gfc_evaluate_now (args[0], &se->pre);
   11030         3072 :   args[1] = gfc_evaluate_now (args[1], &se->pre);
   11031              : 
   11032         3072 :   if (startswith (name, "mag"))
   11033              :     {
   11034              :       /* IEEE_MIN_NUM_MAG and IEEE_MAX_NUM_MAG translate to C functions
   11035              :          fminmag() and fmaxmag(), which do not exist as built-ins.
   11036              : 
   11037              :          Following glibc, we emit this:
   11038              : 
   11039              :            fminmag (x, y) {
   11040              :              ax = ABS (x);
   11041              :              ay = ABS (y);
   11042              :              if (isless (ax, ay))
   11043              :                return x;
   11044              :              else if (isgreater (ax, ay))
   11045              :                return y;
   11046              :              else if (ax == ay)
   11047              :                return x < y ? x : y;
   11048              :              else if (issignaling (x) || issignaling (y))
   11049              :                return x + y;
   11050              :              else
   11051              :                return isnan (y) ? x : y;
   11052              :            }
   11053              : 
   11054              :            fmaxmag (x, y) {
   11055              :              ax = ABS (x);
   11056              :              ay = ABS (y);
   11057              :              if (isgreater (ax, ay))
   11058              :                return x;
   11059              :              else if (isless (ax, ay))
   11060              :                return y;
   11061              :              else if (ax == ay)
   11062              :                return x > y ? x : y;
   11063              :              else if (issignaling (x) || issignaling (y))
   11064              :                return x + y;
   11065              :              else
   11066              :                return isnan (y) ? x : y;
   11067              :            }
   11068              : 
   11069              :          */
   11070              : 
   11071         1536 :       tree abs0, abs1, sig0, sig1;
   11072         1536 :       tree cond1, cond2, cond3, cond4, cond5;
   11073         1536 :       tree res;
   11074         1536 :       tree type = TREE_TYPE (args[0]);
   11075              : 
   11076         1536 :       func = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
   11077         1536 :       abs0 = build_call_expr_loc (input_location, func, 1, args[0]);
   11078         1536 :       abs1 = build_call_expr_loc (input_location, func, 1, args[1]);
   11079         1536 :       abs0 = gfc_evaluate_now (abs0, &se->pre);
   11080         1536 :       abs1 = gfc_evaluate_now (abs1, &se->pre);
   11081              : 
   11082         1536 :       cond5 = build_call_expr_loc (input_location,
   11083              :                                    builtin_decl_explicit (BUILT_IN_ISNAN),
   11084              :                                    1, args[1]);
   11085         1536 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond5,
   11086              :                              args[0], args[1]);
   11087              : 
   11088         1536 :       sig0 = build_call_expr_loc (input_location,
   11089              :                                   builtin_decl_explicit (BUILT_IN_ISSIGNALING),
   11090              :                                   1, args[0]);
   11091         1536 :       sig1 = build_call_expr_loc (input_location,
   11092              :                                   builtin_decl_explicit (BUILT_IN_ISSIGNALING),
   11093              :                                   1, args[1]);
   11094         1536 :       cond4 = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
   11095              :                                logical_type_node, sig0, sig1);
   11096         1536 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond4,
   11097              :                              fold_build2_loc (input_location, PLUS_EXPR,
   11098              :                                               type, args[0], args[1]),
   11099              :                              res);
   11100              : 
   11101         1536 :       cond3 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   11102              :                                abs0, abs1);
   11103         2304 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond3,
   11104              :                              fold_build2_loc (input_location,
   11105              :                                               max ? MAX_EXPR : MIN_EXPR,
   11106              :                                               type, args[0], args[1]),
   11107              :                              res);
   11108              : 
   11109         2304 :       func = builtin_decl_explicit (max ? BUILT_IN_ISLESS : BUILT_IN_ISGREATER);
   11110         1536 :       cond2 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
   11111         1536 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond2,
   11112              :                              args[1], res);
   11113              : 
   11114         2304 :       func = builtin_decl_explicit (max ? BUILT_IN_ISGREATER : BUILT_IN_ISLESS);
   11115         1536 :       cond1 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
   11116         1536 :       res = fold_build3_loc (input_location, COND_EXPR, type, cond1,
   11117              :                              args[0], res);
   11118              : 
   11119         1536 :       se->expr = res;
   11120              :     }
   11121              :   else
   11122              :     {
   11123              :       /* IEEE_MIN_NUM and IEEE_MAX_NUM translate to fmin() and fmax().  */
   11124         1536 :       fn = max ? BUILT_IN_FMAX : BUILT_IN_FMIN;
   11125         1536 :       func = gfc_builtin_decl_for_float_kind (fn, expr->ts.kind);
   11126         1536 :       se->expr = build_call_expr_loc_array (input_location, func, 2, args);
   11127              :     }
   11128         3072 : }
   11129              : 
   11130              : 
   11131              : /* Generate code for comparison functions IEEE_QUIET_* and
   11132              :    IEEE_SIGNALING_*.  */
   11133              : 
   11134              : static void
   11135         3888 : conv_intrinsic_ieee_comparison (gfc_se * se, gfc_expr * expr, int signaling,
   11136              :                                 const char *name)
   11137              : {
   11138         3888 :   tree args[2];
   11139         3888 :   tree arg1, arg2, res;
   11140              : 
   11141              :   /* Evaluate arguments only once.  */
   11142         3888 :   conv_ieee_function_args (se, expr, args, 2);
   11143         3888 :   arg1 = gfc_evaluate_now (args[0], &se->pre);
   11144         3888 :   arg2 = gfc_evaluate_now (args[1], &se->pre);
   11145              : 
   11146         3888 :   if (startswith (name, "eq"))
   11147              :     {
   11148          648 :       if (signaling)
   11149          324 :         res = build_call_expr_loc (input_location,
   11150              :                                    builtin_decl_explicit (BUILT_IN_ISEQSIG),
   11151              :                                    2, arg1, arg2);
   11152              :       else
   11153          324 :         res = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   11154              :                                arg1, arg2);
   11155              :     }
   11156         3240 :   else if (startswith (name, "ne"))
   11157              :     {
   11158          648 :       if (signaling)
   11159              :         {
   11160          324 :           res = build_call_expr_loc (input_location,
   11161              :                                      builtin_decl_explicit (BUILT_IN_ISEQSIG),
   11162              :                                      2, arg1, arg2);
   11163          324 :           res = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
   11164              :                                  logical_type_node, res);
   11165              :         }
   11166              :       else
   11167          324 :         res = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   11168              :                                arg1, arg2);
   11169              :     }
   11170         2592 :   else if (startswith (name, "ge"))
   11171              :     {
   11172          648 :       if (signaling)
   11173          324 :         res = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   11174              :                                arg1, arg2);
   11175              :       else
   11176          324 :         res = build_call_expr_loc (input_location,
   11177              :                                    builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL),
   11178              :                                    2, arg1, arg2);
   11179              :     }
   11180         1944 :   else if (startswith (name, "gt"))
   11181              :     {
   11182          648 :       if (signaling)
   11183          324 :         res = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
   11184              :                                arg1, arg2);
   11185              :       else
   11186          324 :         res = build_call_expr_loc (input_location,
   11187              :                                    builtin_decl_explicit (BUILT_IN_ISGREATER),
   11188              :                                    2, arg1, arg2);
   11189              :     }
   11190         1296 :   else if (startswith (name, "le"))
   11191              :     {
   11192          648 :       if (signaling)
   11193          324 :         res = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
   11194              :                                arg1, arg2);
   11195              :       else
   11196          324 :         res = build_call_expr_loc (input_location,
   11197              :                                    builtin_decl_explicit (BUILT_IN_ISLESSEQUAL),
   11198              :                                    2, arg1, arg2);
   11199              :     }
   11200          648 :   else if (startswith (name, "lt"))
   11201              :     {
   11202          648 :       if (signaling)
   11203          324 :         res = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
   11204              :                                arg1, arg2);
   11205              :       else
   11206          324 :         res = build_call_expr_loc (input_location,
   11207              :                                    builtin_decl_explicit (BUILT_IN_ISLESS),
   11208              :                                    2, arg1, arg2);
   11209              :     }
   11210              :   else
   11211            0 :     gcc_unreachable ();
   11212              : 
   11213         3888 :   se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), res);
   11214         3888 : }
   11215              : 
   11216              : 
   11217              : /* Generate code for an intrinsic function from the IEEE_ARITHMETIC
   11218              :    module.  */
   11219              : 
   11220              : bool
   11221        13939 : gfc_conv_ieee_arithmetic_function (gfc_se * se, gfc_expr * expr)
   11222              : {
   11223        13939 :   const char *name = expr->value.function.name;
   11224              : 
   11225        13939 :   if (startswith (name, "_gfortran_ieee_is_nan"))
   11226          522 :     conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISNAN, 1);
   11227        13417 :   else if (startswith (name, "_gfortran_ieee_is_finite"))
   11228          372 :     conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISFINITE, 1);
   11229        13045 :   else if (startswith (name, "_gfortran_ieee_unordered"))
   11230          168 :     conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISUNORDERED, 2);
   11231        12877 :   else if (startswith (name, "_gfortran_ieee_signbit"))
   11232          624 :     conv_intrinsic_ieee_signbit (se, expr);
   11233        12253 :   else if (startswith (name, "_gfortran_ieee_is_normal"))
   11234          312 :     conv_intrinsic_ieee_is_normal (se, expr);
   11235        11941 :   else if (startswith (name, "_gfortran_ieee_is_negative"))
   11236          312 :     conv_intrinsic_ieee_is_negative (se, expr);
   11237        11629 :   else if (startswith (name, "_gfortran_ieee_copy_sign"))
   11238          576 :     conv_intrinsic_ieee_copy_sign (se, expr);
   11239        11053 :   else if (startswith (name, "_gfortran_ieee_scalb"))
   11240          228 :     conv_intrinsic_ieee_scalb (se, expr);
   11241        10825 :   else if (startswith (name, "_gfortran_ieee_next_after"))
   11242          180 :     conv_intrinsic_ieee_next_after (se, expr);
   11243        10645 :   else if (startswith (name, "_gfortran_ieee_rem"))
   11244           84 :     conv_intrinsic_ieee_rem (se, expr);
   11245        10561 :   else if (startswith (name, "_gfortran_ieee_logb"))
   11246          144 :     conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_LOGB);
   11247        10417 :   else if (startswith (name, "_gfortran_ieee_rint"))
   11248           96 :     conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_RINT);
   11249        10321 :   else if (startswith (name, "ieee_class_") && ISDIGIT (name[11]))
   11250          648 :     conv_intrinsic_ieee_class (se, expr);
   11251         9673 :   else if (startswith (name, "ieee_value_") && ISDIGIT (name[11]))
   11252         1111 :     conv_intrinsic_ieee_value (se, expr);
   11253         8562 :   else if (startswith (name, "_gfortran_ieee_fma"))
   11254          120 :     conv_intrinsic_ieee_fma (se, expr);
   11255         8442 :   else if (startswith (name, "_gfortran_ieee_min_num_"))
   11256         1536 :     conv_intrinsic_ieee_minmax (se, expr, 0, name + 23);
   11257         6906 :   else if (startswith (name, "_gfortran_ieee_max_num_"))
   11258         1536 :     conv_intrinsic_ieee_minmax (se, expr, 1, name + 23);
   11259         5370 :   else if (startswith (name, "_gfortran_ieee_quiet_"))
   11260         1944 :     conv_intrinsic_ieee_comparison (se, expr, 0, name + 21);
   11261         3426 :   else if (startswith (name, "_gfortran_ieee_signaling_"))
   11262         1944 :     conv_intrinsic_ieee_comparison (se, expr, 1, name + 25);
   11263              :   else
   11264              :     /* It is not among the functions we translate directly.  We return
   11265              :        false, so a library function call is emitted.  */
   11266              :     return false;
   11267              : 
   11268              :   return true;
   11269              : }
   11270              : 
   11271              : 
   11272              : /* Generate a direct call to malloc() for the MALLOC intrinsic.  */
   11273              : 
   11274              : static void
   11275           16 : gfc_conv_intrinsic_malloc (gfc_se * se, gfc_expr * expr)
   11276              : {
   11277           16 :   tree arg, res, restype;
   11278              : 
   11279           16 :   gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
   11280           16 :   arg = fold_convert (size_type_node, arg);
   11281           16 :   res = build_call_expr_loc (input_location,
   11282              :                              builtin_decl_explicit (BUILT_IN_MALLOC), 1, arg);
   11283           16 :   restype = gfc_typenode_for_spec (&expr->ts);
   11284           16 :   se->expr = fold_convert (restype, res);
   11285           16 : }
   11286              : 
   11287              : 
   11288              : /* Generate code for an intrinsic function.  Some map directly to library
   11289              :    calls, others get special handling.  In some cases the name of the function
   11290              :    used depends on the type specifiers.  */
   11291              : 
   11292              : void
   11293       274668 : gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
   11294              : {
   11295       274668 :   const char *name;
   11296       274668 :   int lib, kind;
   11297       274668 :   tree fndecl;
   11298              : 
   11299       274668 :   name = &expr->value.function.name[2];
   11300              : 
   11301       274668 :   if (expr->rank > 0)
   11302              :     {
   11303        50957 :       lib = gfc_is_intrinsic_libcall (expr);
   11304        50957 :       if (lib != 0)
   11305              :         {
   11306        19312 :           if (lib == 1)
   11307        11826 :             se->ignore_optional = 1;
   11308              : 
   11309        19312 :           switch (expr->value.function.isym->id)
   11310              :             {
   11311         5909 :             case GFC_ISYM_EOSHIFT:
   11312         5909 :             case GFC_ISYM_PACK:
   11313         5909 :             case GFC_ISYM_RESHAPE:
   11314         5909 :             case GFC_ISYM_REDUCE:
   11315              :               /* For all of those the first argument specifies the type and the
   11316              :                  third is optional.  */
   11317         5909 :               conv_generic_with_optional_char_arg (se, expr, 1, 3);
   11318         5909 :               break;
   11319              : 
   11320         1128 :             case GFC_ISYM_FINDLOC:
   11321         1128 :               gfc_conv_intrinsic_findloc (se, expr);
   11322         1128 :               break;
   11323              : 
   11324         2943 :             case GFC_ISYM_MINLOC:
   11325         2943 :               gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
   11326         2943 :               break;
   11327              : 
   11328         2447 :             case GFC_ISYM_MAXLOC:
   11329         2447 :               gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
   11330         2447 :               break;
   11331              : 
   11332         6885 :             default:
   11333         6885 :               gfc_conv_intrinsic_funcall (se, expr);
   11334         6885 :               break;
   11335              :             }
   11336              : 
   11337              :           return;
   11338              :         }
   11339              :     }
   11340              : 
   11341       255356 :   switch (expr->value.function.isym->id)
   11342              :     {
   11343            0 :     case GFC_ISYM_NONE:
   11344            0 :       gcc_unreachable ();
   11345              : 
   11346          547 :     case GFC_ISYM_REPEAT:
   11347          547 :       gfc_conv_intrinsic_repeat (se, expr);
   11348          547 :       break;
   11349              : 
   11350          582 :     case GFC_ISYM_TRIM:
   11351          582 :       gfc_conv_intrinsic_trim (se, expr);
   11352          582 :       break;
   11353              : 
   11354           42 :     case GFC_ISYM_SC_KIND:
   11355           42 :       gfc_conv_intrinsic_sc_kind (se, expr);
   11356           42 :       break;
   11357              : 
   11358           45 :     case GFC_ISYM_SI_KIND:
   11359           45 :       gfc_conv_intrinsic_si_kind (se, expr);
   11360           45 :       break;
   11361              : 
   11362            6 :     case GFC_ISYM_SL_KIND:
   11363            6 :       gfc_conv_intrinsic_sl_kind (se, expr);
   11364            6 :       break;
   11365              : 
   11366           82 :     case GFC_ISYM_SR_KIND:
   11367           82 :       gfc_conv_intrinsic_sr_kind (se, expr);
   11368           82 :       break;
   11369              : 
   11370          228 :     case GFC_ISYM_EXPONENT:
   11371          228 :       gfc_conv_intrinsic_exponent (se, expr);
   11372          228 :       break;
   11373              : 
   11374          316 :     case GFC_ISYM_SCAN:
   11375          316 :       kind = expr->value.function.actual->expr->ts.kind;
   11376          316 :       if (kind == 1)
   11377          250 :        fndecl = gfor_fndecl_string_scan;
   11378           66 :       else if (kind == 4)
   11379           66 :        fndecl = gfor_fndecl_string_scan_char4;
   11380              :       else
   11381            0 :        gcc_unreachable ();
   11382              : 
   11383          316 :       gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
   11384          316 :       break;
   11385              : 
   11386           94 :     case GFC_ISYM_VERIFY:
   11387           94 :       kind = expr->value.function.actual->expr->ts.kind;
   11388           94 :       if (kind == 1)
   11389           70 :        fndecl = gfor_fndecl_string_verify;
   11390           24 :       else if (kind == 4)
   11391           24 :        fndecl = gfor_fndecl_string_verify_char4;
   11392              :       else
   11393            0 :        gcc_unreachable ();
   11394              : 
   11395           94 :       gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
   11396           94 :       break;
   11397              : 
   11398         7687 :     case GFC_ISYM_ALLOCATED:
   11399         7687 :       gfc_conv_allocated (se, expr);
   11400         7687 :       break;
   11401              : 
   11402         9892 :     case GFC_ISYM_ASSOCIATED:
   11403         9892 :       gfc_conv_associated(se, expr);
   11404         9892 :       break;
   11405              : 
   11406          418 :     case GFC_ISYM_SAME_TYPE_AS:
   11407          418 :       gfc_conv_same_type_as (se, expr);
   11408          418 :       break;
   11409              : 
   11410         8069 :     case GFC_ISYM_ABS:
   11411         8069 :       gfc_conv_intrinsic_abs (se, expr);
   11412         8069 :       break;
   11413              : 
   11414          345 :     case GFC_ISYM_ADJUSTL:
   11415          345 :       if (expr->ts.kind == 1)
   11416          291 :        fndecl = gfor_fndecl_adjustl;
   11417           54 :       else if (expr->ts.kind == 4)
   11418           54 :        fndecl = gfor_fndecl_adjustl_char4;
   11419              :       else
   11420            0 :        gcc_unreachable ();
   11421              : 
   11422          345 :       gfc_conv_intrinsic_adjust (se, expr, fndecl);
   11423          345 :       break;
   11424              : 
   11425          123 :     case GFC_ISYM_ADJUSTR:
   11426          123 :       if (expr->ts.kind == 1)
   11427           68 :        fndecl = gfor_fndecl_adjustr;
   11428           55 :       else if (expr->ts.kind == 4)
   11429           55 :        fndecl = gfor_fndecl_adjustr_char4;
   11430              :       else
   11431            0 :        gcc_unreachable ();
   11432              : 
   11433          123 :       gfc_conv_intrinsic_adjust (se, expr, fndecl);
   11434          123 :       break;
   11435              : 
   11436          440 :     case GFC_ISYM_AIMAG:
   11437          440 :       gfc_conv_intrinsic_imagpart (se, expr);
   11438          440 :       break;
   11439              : 
   11440          146 :     case GFC_ISYM_AINT:
   11441          146 :       gfc_conv_intrinsic_aint (se, expr, RND_TRUNC);
   11442          146 :       break;
   11443              : 
   11444          432 :     case GFC_ISYM_ALL:
   11445          432 :       gfc_conv_intrinsic_anyall (se, expr, EQ_EXPR);
   11446          432 :       break;
   11447              : 
   11448           74 :     case GFC_ISYM_ANINT:
   11449           74 :       gfc_conv_intrinsic_aint (se, expr, RND_ROUND);
   11450           74 :       break;
   11451              : 
   11452           90 :     case GFC_ISYM_AND:
   11453           90 :       gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
   11454           90 :       break;
   11455              : 
   11456        39189 :     case GFC_ISYM_ANY:
   11457        39189 :       gfc_conv_intrinsic_anyall (se, expr, NE_EXPR);
   11458        39189 :       break;
   11459              : 
   11460          270 :     case GFC_ISYM_ACOSD:
   11461          270 :     case GFC_ISYM_ASIND:
   11462          270 :     case GFC_ISYM_ATAND:
   11463          270 :       gfc_conv_intrinsic_atrigd (se, expr, expr->value.function.isym->id);
   11464          270 :       break;
   11465              : 
   11466          102 :     case GFC_ISYM_COTAN:
   11467          102 :       gfc_conv_intrinsic_cotan (se, expr);
   11468          102 :       break;
   11469              : 
   11470          108 :     case GFC_ISYM_COTAND:
   11471          108 :       gfc_conv_intrinsic_cotand (se, expr);
   11472          108 :       break;
   11473              : 
   11474          138 :     case GFC_ISYM_ATAN2D:
   11475          138 :       gfc_conv_intrinsic_atan2d (se, expr);
   11476          138 :       break;
   11477              : 
   11478          145 :     case GFC_ISYM_BTEST:
   11479          145 :       gfc_conv_intrinsic_btest (se, expr);
   11480          145 :       break;
   11481              : 
   11482           54 :     case GFC_ISYM_BGE:
   11483           54 :       gfc_conv_intrinsic_bitcomp (se, expr, GE_EXPR);
   11484           54 :       break;
   11485              : 
   11486           54 :     case GFC_ISYM_BGT:
   11487           54 :       gfc_conv_intrinsic_bitcomp (se, expr, GT_EXPR);
   11488           54 :       break;
   11489              : 
   11490           54 :     case GFC_ISYM_BLE:
   11491           54 :       gfc_conv_intrinsic_bitcomp (se, expr, LE_EXPR);
   11492           54 :       break;
   11493              : 
   11494           54 :     case GFC_ISYM_BLT:
   11495           54 :       gfc_conv_intrinsic_bitcomp (se, expr, LT_EXPR);
   11496           54 :       break;
   11497              : 
   11498        10092 :     case GFC_ISYM_C_ASSOCIATED:
   11499        10092 :     case GFC_ISYM_C_FUNLOC:
   11500        10092 :     case GFC_ISYM_C_LOC:
   11501        10092 :     case GFC_ISYM_F_C_STRING:
   11502        10092 :       conv_isocbinding_function (se, expr);
   11503        10092 :       break;
   11504              : 
   11505         2038 :     case GFC_ISYM_ACHAR:
   11506         2038 :     case GFC_ISYM_CHAR:
   11507         2038 :       gfc_conv_intrinsic_char (se, expr);
   11508         2038 :       break;
   11509              : 
   11510        42019 :     case GFC_ISYM_CONVERSION:
   11511        42019 :     case GFC_ISYM_DBLE:
   11512        42019 :     case GFC_ISYM_DFLOAT:
   11513        42019 :     case GFC_ISYM_FLOAT:
   11514        42019 :     case GFC_ISYM_LOGICAL:
   11515        42019 :     case GFC_ISYM_REAL:
   11516        42019 :     case GFC_ISYM_REALPART:
   11517        42019 :     case GFC_ISYM_SNGL:
   11518        42019 :       gfc_conv_intrinsic_conversion (se, expr);
   11519        42019 :       break;
   11520              : 
   11521              :       /* Integer conversions are handled separately to make sure we get the
   11522              :          correct rounding mode.  */
   11523         3052 :     case GFC_ISYM_INT:
   11524         3052 :     case GFC_ISYM_INT2:
   11525         3052 :     case GFC_ISYM_INT8:
   11526         3052 :     case GFC_ISYM_LONG:
   11527         3052 :     case GFC_ISYM_UINT:
   11528         3052 :       gfc_conv_intrinsic_int (se, expr, RND_TRUNC);
   11529         3052 :       break;
   11530              : 
   11531          162 :     case GFC_ISYM_NINT:
   11532          162 :       gfc_conv_intrinsic_int (se, expr, RND_ROUND);
   11533          162 :       break;
   11534              : 
   11535           16 :     case GFC_ISYM_CEILING:
   11536           16 :       gfc_conv_intrinsic_int (se, expr, RND_CEIL);
   11537           16 :       break;
   11538              : 
   11539          116 :     case GFC_ISYM_FLOOR:
   11540          116 :       gfc_conv_intrinsic_int (se, expr, RND_FLOOR);
   11541          116 :       break;
   11542              : 
   11543         3588 :     case GFC_ISYM_MOD:
   11544         3588 :       gfc_conv_intrinsic_mod (se, expr, 0);
   11545         3588 :       break;
   11546              : 
   11547          442 :     case GFC_ISYM_MODULO:
   11548          442 :       gfc_conv_intrinsic_mod (se, expr, 1);
   11549          442 :       break;
   11550              : 
   11551         1018 :     case GFC_ISYM_CAF_GET:
   11552         1018 :       gfc_conv_intrinsic_caf_get (se, expr, NULL_TREE, false, NULL);
   11553         1018 :       break;
   11554              : 
   11555          265 :     case GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE:
   11556          265 :       gfc_conv_intrinsic_caf_is_present_remote (se, expr);
   11557          265 :       break;
   11558              : 
   11559          491 :     case GFC_ISYM_CMPLX:
   11560          491 :       gfc_conv_intrinsic_cmplx (se, expr, name[5] == '1');
   11561          491 :       break;
   11562              : 
   11563           10 :     case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
   11564           10 :       gfc_conv_intrinsic_iargc (se, expr);
   11565           10 :       break;
   11566              : 
   11567            6 :     case GFC_ISYM_COMPLEX:
   11568            6 :       gfc_conv_intrinsic_cmplx (se, expr, 1);
   11569            6 :       break;
   11570              : 
   11571          257 :     case GFC_ISYM_CONJG:
   11572          257 :       gfc_conv_intrinsic_conjg (se, expr);
   11573          257 :       break;
   11574              : 
   11575           16 :     case GFC_ISYM_COSHAPE:
   11576           16 :       conv_intrinsic_cobound (se, expr);
   11577           16 :       break;
   11578              : 
   11579          155 :     case GFC_ISYM_COUNT:
   11580          155 :       gfc_conv_intrinsic_count (se, expr);
   11581          155 :       break;
   11582              : 
   11583            0 :     case GFC_ISYM_CTIME:
   11584            0 :       gfc_conv_intrinsic_ctime (se, expr);
   11585            0 :       break;
   11586              : 
   11587           96 :     case GFC_ISYM_DIM:
   11588           96 :       gfc_conv_intrinsic_dim (se, expr);
   11589           96 :       break;
   11590              : 
   11591          113 :     case GFC_ISYM_DOT_PRODUCT:
   11592          113 :       gfc_conv_intrinsic_dot_product (se, expr);
   11593          113 :       break;
   11594              : 
   11595           13 :     case GFC_ISYM_DPROD:
   11596           13 :       gfc_conv_intrinsic_dprod (se, expr);
   11597           13 :       break;
   11598              : 
   11599           66 :     case GFC_ISYM_DSHIFTL:
   11600           66 :       gfc_conv_intrinsic_dshift (se, expr, true);
   11601           66 :       break;
   11602              : 
   11603           66 :     case GFC_ISYM_DSHIFTR:
   11604           66 :       gfc_conv_intrinsic_dshift (se, expr, false);
   11605           66 :       break;
   11606              : 
   11607            0 :     case GFC_ISYM_FDATE:
   11608            0 :       gfc_conv_intrinsic_fdate (se, expr);
   11609            0 :       break;
   11610              : 
   11611           60 :     case GFC_ISYM_FRACTION:
   11612           60 :       gfc_conv_intrinsic_fraction (se, expr);
   11613           60 :       break;
   11614              : 
   11615           24 :     case GFC_ISYM_IALL:
   11616           24 :       gfc_conv_intrinsic_arith (se, expr, BIT_AND_EXPR, false);
   11617           24 :       break;
   11618              : 
   11619          606 :     case GFC_ISYM_IAND:
   11620          606 :       gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
   11621          606 :       break;
   11622              : 
   11623           12 :     case GFC_ISYM_IANY:
   11624           12 :       gfc_conv_intrinsic_arith (se, expr, BIT_IOR_EXPR, false);
   11625           12 :       break;
   11626              : 
   11627          168 :     case GFC_ISYM_IBCLR:
   11628          168 :       gfc_conv_intrinsic_singlebitop (se, expr, 0);
   11629          168 :       break;
   11630              : 
   11631           27 :     case GFC_ISYM_IBITS:
   11632           27 :       gfc_conv_intrinsic_ibits (se, expr);
   11633           27 :       break;
   11634              : 
   11635          138 :     case GFC_ISYM_IBSET:
   11636          138 :       gfc_conv_intrinsic_singlebitop (se, expr, 1);
   11637          138 :       break;
   11638              : 
   11639         2033 :     case GFC_ISYM_IACHAR:
   11640         2033 :     case GFC_ISYM_ICHAR:
   11641              :       /* We assume ASCII character sequence.  */
   11642         2033 :       gfc_conv_intrinsic_ichar (se, expr);
   11643         2033 :       break;
   11644              : 
   11645            2 :     case GFC_ISYM_IARGC:
   11646            2 :       gfc_conv_intrinsic_iargc (se, expr);
   11647            2 :       break;
   11648              : 
   11649          694 :     case GFC_ISYM_IEOR:
   11650          694 :       gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
   11651          694 :       break;
   11652              : 
   11653          347 :     case GFC_ISYM_INDEX:
   11654          347 :       kind = expr->value.function.actual->expr->ts.kind;
   11655          347 :       if (kind == 1)
   11656          281 :        fndecl = gfor_fndecl_string_index;
   11657           66 :       else if (kind == 4)
   11658           66 :        fndecl = gfor_fndecl_string_index_char4;
   11659              :       else
   11660            0 :        gcc_unreachable ();
   11661              : 
   11662          347 :       gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
   11663          347 :       break;
   11664              : 
   11665          495 :     case GFC_ISYM_IOR:
   11666          495 :       gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
   11667          495 :       break;
   11668              : 
   11669           12 :     case GFC_ISYM_IPARITY:
   11670           12 :       gfc_conv_intrinsic_arith (se, expr, BIT_XOR_EXPR, false);
   11671           12 :       break;
   11672              : 
   11673            6 :     case GFC_ISYM_IS_IOSTAT_END:
   11674            6 :       gfc_conv_has_intvalue (se, expr, LIBERROR_END);
   11675            6 :       break;
   11676              : 
   11677           18 :     case GFC_ISYM_IS_IOSTAT_EOR:
   11678           18 :       gfc_conv_has_intvalue (se, expr, LIBERROR_EOR);
   11679           18 :       break;
   11680              : 
   11681          748 :     case GFC_ISYM_IS_CONTIGUOUS:
   11682          748 :       gfc_conv_intrinsic_is_contiguous (se, expr);
   11683          748 :       break;
   11684              : 
   11685          432 :     case GFC_ISYM_ISNAN:
   11686          432 :       gfc_conv_intrinsic_isnan (se, expr);
   11687          432 :       break;
   11688              : 
   11689            8 :     case GFC_ISYM_KILL:
   11690            8 :       conv_intrinsic_kill (se, expr);
   11691            8 :       break;
   11692              : 
   11693           90 :     case GFC_ISYM_LSHIFT:
   11694           90 :       gfc_conv_intrinsic_shift (se, expr, false, false);
   11695           90 :       break;
   11696              : 
   11697           24 :     case GFC_ISYM_RSHIFT:
   11698           24 :       gfc_conv_intrinsic_shift (se, expr, true, true);
   11699           24 :       break;
   11700              : 
   11701           78 :     case GFC_ISYM_SHIFTA:
   11702           78 :       gfc_conv_intrinsic_shift (se, expr, true, true);
   11703           78 :       break;
   11704              : 
   11705          234 :     case GFC_ISYM_SHIFTL:
   11706          234 :       gfc_conv_intrinsic_shift (se, expr, false, false);
   11707          234 :       break;
   11708              : 
   11709           66 :     case GFC_ISYM_SHIFTR:
   11710           66 :       gfc_conv_intrinsic_shift (se, expr, true, false);
   11711           66 :       break;
   11712              : 
   11713          318 :     case GFC_ISYM_ISHFT:
   11714          318 :       gfc_conv_intrinsic_ishft (se, expr);
   11715          318 :       break;
   11716              : 
   11717          658 :     case GFC_ISYM_ISHFTC:
   11718          658 :       gfc_conv_intrinsic_ishftc (se, expr);
   11719          658 :       break;
   11720              : 
   11721          432 :     case GFC_ISYM_LEADZ:
   11722          432 :       gfc_conv_intrinsic_leadz (se, expr);
   11723          432 :       break;
   11724              : 
   11725          444 :     case GFC_ISYM_TRAILZ:
   11726          444 :       gfc_conv_intrinsic_trailz (se, expr);
   11727          444 :       break;
   11728              : 
   11729          265 :     case GFC_ISYM_POPCNT:
   11730          265 :       gfc_conv_intrinsic_popcnt_poppar (se, expr, 0);
   11731          265 :       break;
   11732              : 
   11733          193 :     case GFC_ISYM_POPPAR:
   11734          193 :       gfc_conv_intrinsic_popcnt_poppar (se, expr, 1);
   11735          193 :       break;
   11736              : 
   11737         5589 :     case GFC_ISYM_LBOUND:
   11738         5589 :       gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_LBOUND);
   11739         5589 :       break;
   11740              : 
   11741          237 :     case GFC_ISYM_LCOBOUND:
   11742          237 :       conv_intrinsic_cobound (se, expr);
   11743          237 :       break;
   11744              : 
   11745          774 :     case GFC_ISYM_TRANSPOSE:
   11746              :       /* The scalarizer has already been set up for reversed dimension access
   11747              :          order ; now we just get the argument value normally.  */
   11748          774 :       gfc_conv_expr (se, expr->value.function.actual->expr);
   11749          774 :       break;
   11750              : 
   11751         5906 :     case GFC_ISYM_LEN:
   11752         5906 :       gfc_conv_intrinsic_len (se, expr);
   11753         5906 :       break;
   11754              : 
   11755         2359 :     case GFC_ISYM_LEN_TRIM:
   11756         2359 :       gfc_conv_intrinsic_len_trim (se, expr);
   11757         2359 :       break;
   11758              : 
   11759           18 :     case GFC_ISYM_LGE:
   11760           18 :       gfc_conv_intrinsic_strcmp (se, expr, GE_EXPR);
   11761           18 :       break;
   11762              : 
   11763           36 :     case GFC_ISYM_LGT:
   11764           36 :       gfc_conv_intrinsic_strcmp (se, expr, GT_EXPR);
   11765           36 :       break;
   11766              : 
   11767           18 :     case GFC_ISYM_LLE:
   11768           18 :       gfc_conv_intrinsic_strcmp (se, expr, LE_EXPR);
   11769           18 :       break;
   11770              : 
   11771           27 :     case GFC_ISYM_LLT:
   11772           27 :       gfc_conv_intrinsic_strcmp (se, expr, LT_EXPR);
   11773           27 :       break;
   11774              : 
   11775           16 :     case GFC_ISYM_MALLOC:
   11776           16 :       gfc_conv_intrinsic_malloc (se, expr);
   11777           16 :       break;
   11778              : 
   11779           32 :     case GFC_ISYM_MASKL:
   11780           32 :       gfc_conv_intrinsic_mask (se, expr, 1);
   11781           32 :       break;
   11782              : 
   11783           32 :     case GFC_ISYM_MASKR:
   11784           32 :       gfc_conv_intrinsic_mask (se, expr, 0);
   11785           32 :       break;
   11786              : 
   11787         1051 :     case GFC_ISYM_MAX:
   11788         1051 :       if (expr->ts.type == BT_CHARACTER)
   11789          138 :         gfc_conv_intrinsic_minmax_char (se, expr, 1);
   11790              :       else
   11791          913 :         gfc_conv_intrinsic_minmax (se, expr, GT_EXPR);
   11792              :       break;
   11793              : 
   11794         6364 :     case GFC_ISYM_MAXLOC:
   11795         6364 :       gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
   11796         6364 :       break;
   11797              : 
   11798          216 :     case GFC_ISYM_FINDLOC:
   11799          216 :       gfc_conv_intrinsic_findloc (se, expr);
   11800          216 :       break;
   11801              : 
   11802         1101 :     case GFC_ISYM_MAXVAL:
   11803         1101 :       gfc_conv_intrinsic_minmaxval (se, expr, GT_EXPR);
   11804         1101 :       break;
   11805              : 
   11806          959 :     case GFC_ISYM_MERGE:
   11807          959 :       gfc_conv_intrinsic_merge (se, expr);
   11808          959 :       break;
   11809              : 
   11810           42 :     case GFC_ISYM_MERGE_BITS:
   11811           42 :       gfc_conv_intrinsic_merge_bits (se, expr);
   11812           42 :       break;
   11813              : 
   11814          598 :     case GFC_ISYM_MIN:
   11815          598 :       if (expr->ts.type == BT_CHARACTER)
   11816          144 :         gfc_conv_intrinsic_minmax_char (se, expr, -1);
   11817              :       else
   11818          454 :         gfc_conv_intrinsic_minmax (se, expr, LT_EXPR);
   11819              :       break;
   11820              : 
   11821         7192 :     case GFC_ISYM_MINLOC:
   11822         7192 :       gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
   11823         7192 :       break;
   11824              : 
   11825         1316 :     case GFC_ISYM_MINVAL:
   11826         1316 :       gfc_conv_intrinsic_minmaxval (se, expr, LT_EXPR);
   11827         1316 :       break;
   11828              : 
   11829         1595 :     case GFC_ISYM_NEAREST:
   11830         1595 :       gfc_conv_intrinsic_nearest (se, expr);
   11831         1595 :       break;
   11832              : 
   11833           68 :     case GFC_ISYM_NORM2:
   11834           68 :       gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, true);
   11835           68 :       break;
   11836              : 
   11837          230 :     case GFC_ISYM_NOT:
   11838          230 :       gfc_conv_intrinsic_not (se, expr);
   11839          230 :       break;
   11840              : 
   11841           12 :     case GFC_ISYM_OR:
   11842           12 :       gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
   11843           12 :       break;
   11844              : 
   11845          468 :     case GFC_ISYM_OUT_OF_RANGE:
   11846          468 :       gfc_conv_intrinsic_out_of_range (se, expr);
   11847          468 :       break;
   11848              : 
   11849           36 :     case GFC_ISYM_PARITY:
   11850           36 :       gfc_conv_intrinsic_arith (se, expr, NE_EXPR, false);
   11851           36 :       break;
   11852              : 
   11853         5232 :     case GFC_ISYM_PRESENT:
   11854         5232 :       gfc_conv_intrinsic_present (se, expr);
   11855         5232 :       break;
   11856              : 
   11857          358 :     case GFC_ISYM_PRODUCT:
   11858          358 :       gfc_conv_intrinsic_arith (se, expr, MULT_EXPR, false);
   11859          358 :       break;
   11860              : 
   11861        13983 :     case GFC_ISYM_RANK:
   11862        13983 :       gfc_conv_intrinsic_rank (se, expr);
   11863        13983 :       break;
   11864              : 
   11865           48 :     case GFC_ISYM_RRSPACING:
   11866           48 :       gfc_conv_intrinsic_rrspacing (se, expr);
   11867           48 :       break;
   11868              : 
   11869          262 :     case GFC_ISYM_SET_EXPONENT:
   11870          262 :       gfc_conv_intrinsic_set_exponent (se, expr);
   11871          262 :       break;
   11872              : 
   11873           72 :     case GFC_ISYM_SCALE:
   11874           72 :       gfc_conv_intrinsic_scale (se, expr);
   11875           72 :       break;
   11876              : 
   11877         5024 :     case GFC_ISYM_SHAPE:
   11878         5024 :       gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_SHAPE);
   11879         5024 :       break;
   11880              : 
   11881          423 :     case GFC_ISYM_SIGN:
   11882          423 :       gfc_conv_intrinsic_sign (se, expr);
   11883          423 :       break;
   11884              : 
   11885        15969 :     case GFC_ISYM_SIZE:
   11886        15969 :       gfc_conv_intrinsic_size (se, expr);
   11887        15969 :       break;
   11888              : 
   11889         1315 :     case GFC_ISYM_SIZEOF:
   11890         1315 :     case GFC_ISYM_C_SIZEOF:
   11891         1315 :       gfc_conv_intrinsic_sizeof (se, expr);
   11892         1315 :       break;
   11893              : 
   11894          890 :     case GFC_ISYM_STORAGE_SIZE:
   11895          890 :       gfc_conv_intrinsic_storage_size (se, expr);
   11896          890 :       break;
   11897              : 
   11898           70 :     case GFC_ISYM_SPACING:
   11899           70 :       gfc_conv_intrinsic_spacing (se, expr);
   11900           70 :       break;
   11901              : 
   11902         2548 :     case GFC_ISYM_STRIDE:
   11903         2548 :       conv_intrinsic_stride (se, expr);
   11904         2548 :       break;
   11905              : 
   11906         2281 :     case GFC_ISYM_SUM:
   11907         2281 :       gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, false);
   11908         2281 :       break;
   11909              : 
   11910           53 :     case GFC_ISYM_TEAM_NUMBER:
   11911           53 :       conv_intrinsic_team_number (se, expr);
   11912           53 :       break;
   11913              : 
   11914         4523 :     case GFC_ISYM_TRANSFER:
   11915         4523 :       if (se->ss && se->ss->info->useflags)
   11916              :         /* Access the previously obtained result.  */
   11917          329 :         gfc_conv_tmp_array_ref (se);
   11918              :       else
   11919         4194 :         gfc_conv_intrinsic_transfer (se, expr);
   11920              :       break;
   11921              : 
   11922            0 :     case GFC_ISYM_TTYNAM:
   11923            0 :       gfc_conv_intrinsic_ttynam (se, expr);
   11924            0 :       break;
   11925              : 
   11926         5748 :     case GFC_ISYM_UBOUND:
   11927         5748 :       gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_UBOUND);
   11928         5748 :       break;
   11929              : 
   11930          272 :     case GFC_ISYM_UCOBOUND:
   11931          272 :       conv_intrinsic_cobound (se, expr);
   11932          272 :       break;
   11933              : 
   11934           18 :     case GFC_ISYM_XOR:
   11935           18 :       gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
   11936           18 :       break;
   11937              : 
   11938         9203 :     case GFC_ISYM_LOC:
   11939         9203 :       gfc_conv_intrinsic_loc (se, expr);
   11940         9203 :       break;
   11941              : 
   11942         1618 :     case GFC_ISYM_THIS_IMAGE:
   11943              :       /* For num_images() == 1, handle as LCOBOUND.  */
   11944         1618 :       if (expr->value.function.actual->expr
   11945          542 :           && flag_coarray == GFC_FCOARRAY_SINGLE)
   11946          212 :         conv_intrinsic_cobound (se, expr);
   11947              :       else
   11948         1406 :         trans_this_image (se, expr);
   11949              :       break;
   11950              : 
   11951          257 :     case GFC_ISYM_IMAGE_INDEX:
   11952          257 :       trans_image_index (se, expr);
   11953          257 :       break;
   11954              : 
   11955           38 :     case GFC_ISYM_IMAGE_STATUS:
   11956           38 :       conv_intrinsic_image_status (se, expr);
   11957           38 :       break;
   11958              : 
   11959          928 :     case GFC_ISYM_NUM_IMAGES:
   11960          928 :       trans_num_images (se, expr);
   11961          928 :       break;
   11962              : 
   11963         1448 :     case GFC_ISYM_ACCESS:
   11964         1448 :     case GFC_ISYM_CHDIR:
   11965         1448 :     case GFC_ISYM_CHMOD:
   11966         1448 :     case GFC_ISYM_DTIME:
   11967         1448 :     case GFC_ISYM_ETIME:
   11968         1448 :     case GFC_ISYM_EXTENDS_TYPE_OF:
   11969         1448 :     case GFC_ISYM_FGET:
   11970         1448 :     case GFC_ISYM_FGETC:
   11971         1448 :     case GFC_ISYM_FNUM:
   11972         1448 :     case GFC_ISYM_FPUT:
   11973         1448 :     case GFC_ISYM_FPUTC:
   11974         1448 :     case GFC_ISYM_FSTAT:
   11975         1448 :     case GFC_ISYM_FTELL:
   11976         1448 :     case GFC_ISYM_GETCWD:
   11977         1448 :     case GFC_ISYM_GETGID:
   11978         1448 :     case GFC_ISYM_GETPID:
   11979         1448 :     case GFC_ISYM_GETUID:
   11980         1448 :     case GFC_ISYM_GET_TEAM:
   11981         1448 :     case GFC_ISYM_HOSTNM:
   11982         1448 :     case GFC_ISYM_IERRNO:
   11983         1448 :     case GFC_ISYM_IRAND:
   11984         1448 :     case GFC_ISYM_ISATTY:
   11985         1448 :     case GFC_ISYM_JN2:
   11986         1448 :     case GFC_ISYM_LINK:
   11987         1448 :     case GFC_ISYM_LSTAT:
   11988         1448 :     case GFC_ISYM_MATMUL:
   11989         1448 :     case GFC_ISYM_MCLOCK:
   11990         1448 :     case GFC_ISYM_MCLOCK8:
   11991         1448 :     case GFC_ISYM_RAND:
   11992         1448 :     case GFC_ISYM_REDUCE:
   11993         1448 :     case GFC_ISYM_RENAME:
   11994         1448 :     case GFC_ISYM_SECOND:
   11995         1448 :     case GFC_ISYM_SECNDS:
   11996         1448 :     case GFC_ISYM_SIGNAL:
   11997         1448 :     case GFC_ISYM_STAT:
   11998         1448 :     case GFC_ISYM_SYMLNK:
   11999         1448 :     case GFC_ISYM_SYSTEM:
   12000         1448 :     case GFC_ISYM_TIME:
   12001         1448 :     case GFC_ISYM_TIME8:
   12002         1448 :     case GFC_ISYM_UMASK:
   12003         1448 :     case GFC_ISYM_UNLINK:
   12004         1448 :     case GFC_ISYM_YN2:
   12005         1448 :       gfc_conv_intrinsic_funcall (se, expr);
   12006         1448 :       break;
   12007              : 
   12008            0 :     case GFC_ISYM_EOSHIFT:
   12009            0 :     case GFC_ISYM_PACK:
   12010            0 :     case GFC_ISYM_RESHAPE:
   12011              :       /* For those, expr->rank should always be >0 and thus the if above the
   12012              :          switch should have matched.  */
   12013            0 :       gcc_unreachable ();
   12014         3928 :       break;
   12015              : 
   12016         3928 :     default:
   12017         3928 :       gfc_conv_intrinsic_lib_function (se, expr);
   12018         3928 :       break;
   12019              :     }
   12020              : }
   12021              : 
   12022              : 
   12023              : static gfc_ss *
   12024         1674 : walk_inline_intrinsic_transpose (gfc_ss *ss, gfc_expr *expr)
   12025              : {
   12026         1674 :   gfc_ss *arg_ss, *tmp_ss;
   12027         1674 :   gfc_actual_arglist *arg;
   12028              : 
   12029         1674 :   arg = expr->value.function.actual;
   12030              : 
   12031         1674 :   gcc_assert (arg->expr);
   12032              : 
   12033         1674 :   arg_ss = gfc_walk_subexpr (gfc_ss_terminator, arg->expr);
   12034         1674 :   gcc_assert (arg_ss != gfc_ss_terminator);
   12035              : 
   12036              :   for (tmp_ss = arg_ss; ; tmp_ss = tmp_ss->next)
   12037              :     {
   12038         1785 :       if (tmp_ss->info->type != GFC_SS_SCALAR
   12039              :           && tmp_ss->info->type != GFC_SS_REFERENCE)
   12040              :         {
   12041         1742 :           gcc_assert (tmp_ss->dimen == 2);
   12042              : 
   12043              :           /* We just invert dimensions.  */
   12044         1742 :           std::swap (tmp_ss->dim[0], tmp_ss->dim[1]);
   12045              :         }
   12046              : 
   12047              :       /* Stop when tmp_ss points to the last valid element of the chain...  */
   12048         1785 :       if (tmp_ss->next == gfc_ss_terminator)
   12049              :         break;
   12050              :     }
   12051              : 
   12052              :   /* ... so that we can attach the rest of the chain to it.  */
   12053         1674 :   tmp_ss->next = ss;
   12054              : 
   12055         1674 :   return arg_ss;
   12056              : }
   12057              : 
   12058              : 
   12059              : /* Move the given dimension of the given gfc_ss list to a nested gfc_ss list.
   12060              :    This has the side effect of reversing the nested list, so there is no
   12061              :    need to call gfc_reverse_ss on it (the given list is assumed not to be
   12062              :    reversed yet).   */
   12063              : 
   12064              : static gfc_ss *
   12065         3371 : nest_loop_dimension (gfc_ss *ss, int dim)
   12066              : {
   12067         3371 :   int ss_dim, i;
   12068         3371 :   gfc_ss *new_ss, *prev_ss = gfc_ss_terminator;
   12069         3371 :   gfc_loopinfo *new_loop;
   12070              : 
   12071         3371 :   gcc_assert (ss != gfc_ss_terminator);
   12072              : 
   12073         8118 :   for (; ss != gfc_ss_terminator; ss = ss->next)
   12074              :     {
   12075         4747 :       new_ss = gfc_get_ss ();
   12076         4747 :       new_ss->next = prev_ss;
   12077         4747 :       new_ss->parent = ss;
   12078         4747 :       new_ss->info = ss->info;
   12079         4747 :       new_ss->info->refcount++;
   12080         4747 :       if (ss->dimen != 0)
   12081              :         {
   12082         4684 :           gcc_assert (ss->info->type != GFC_SS_SCALAR
   12083              :                       && ss->info->type != GFC_SS_REFERENCE);
   12084              : 
   12085         4684 :           new_ss->dimen = 1;
   12086         4684 :           new_ss->dim[0] = ss->dim[dim];
   12087              : 
   12088         4684 :           gcc_assert (dim < ss->dimen);
   12089              : 
   12090         4684 :           ss_dim = --ss->dimen;
   12091        10430 :           for (i = dim; i < ss_dim; i++)
   12092         5746 :             ss->dim[i] = ss->dim[i + 1];
   12093              : 
   12094         4684 :           ss->dim[ss_dim] = 0;
   12095              :         }
   12096         4747 :       prev_ss = new_ss;
   12097              : 
   12098         4747 :       if (ss->nested_ss)
   12099              :         {
   12100           81 :           ss->nested_ss->parent = new_ss;
   12101           81 :           new_ss->nested_ss = ss->nested_ss;
   12102              :         }
   12103         4747 :       ss->nested_ss = new_ss;
   12104              :     }
   12105              : 
   12106         3371 :   new_loop = gfc_get_loopinfo ();
   12107         3371 :   gfc_init_loopinfo (new_loop);
   12108              : 
   12109         3371 :   gcc_assert (prev_ss != NULL);
   12110         3371 :   gcc_assert (prev_ss != gfc_ss_terminator);
   12111         3371 :   gfc_add_ss_to_loop (new_loop, prev_ss);
   12112         3371 :   return new_ss->parent;
   12113              : }
   12114              : 
   12115              : 
   12116              : /* Create the gfc_ss list for the SUM/PRODUCT arguments when the function
   12117              :    is to be inlined.  */
   12118              : 
   12119              : static gfc_ss *
   12120          575 : walk_inline_intrinsic_arith (gfc_ss *ss, gfc_expr *expr)
   12121              : {
   12122          575 :   gfc_ss *tmp_ss, *tail, *array_ss;
   12123          575 :   gfc_actual_arglist *arg1, *arg2, *arg3;
   12124          575 :   int sum_dim;
   12125          575 :   bool scalar_mask = false;
   12126              : 
   12127              :   /* The rank of the result will be determined later.  */
   12128          575 :   arg1 = expr->value.function.actual;
   12129          575 :   arg2 = arg1->next;
   12130          575 :   arg3 = arg2->next;
   12131          575 :   gcc_assert (arg3 != NULL);
   12132              : 
   12133          575 :   if (expr->rank == 0)
   12134              :     return ss;
   12135              : 
   12136          575 :   tmp_ss = gfc_ss_terminator;
   12137              : 
   12138          575 :   if (arg3->expr)
   12139              :     {
   12140          118 :       gfc_ss *mask_ss;
   12141              : 
   12142          118 :       mask_ss = gfc_walk_subexpr (tmp_ss, arg3->expr);
   12143          118 :       if (mask_ss == tmp_ss)
   12144           34 :         scalar_mask = 1;
   12145              : 
   12146              :       tmp_ss = mask_ss;
   12147              :     }
   12148              : 
   12149          575 :   array_ss = gfc_walk_subexpr (tmp_ss, arg1->expr);
   12150          575 :   gcc_assert (array_ss != tmp_ss);
   12151              : 
   12152              :   /* Odd thing: If the mask is scalar, it is used by the frontend after
   12153              :      the array (to make an if around the nested loop). Thus it shall
   12154              :      be after array_ss once the gfc_ss list is reversed.  */
   12155          575 :   if (scalar_mask)
   12156           34 :     tmp_ss = gfc_get_scalar_ss (array_ss, arg3->expr);
   12157              :   else
   12158              :     tmp_ss = array_ss;
   12159              : 
   12160              :   /* "Hide" the dimension on which we will sum in the first arg's scalarization
   12161              :      chain.  */
   12162          575 :   sum_dim = mpz_get_si (arg2->expr->value.integer) - 1;
   12163          575 :   tail = nest_loop_dimension (tmp_ss, sum_dim);
   12164          575 :   tail->next = ss;
   12165              : 
   12166          575 :   return tmp_ss;
   12167              : }
   12168              : 
   12169              : 
   12170              : /* Create the gfc_ss list for the arguments to MINLOC or MAXLOC when the
   12171              :    function is to be inlined.  */
   12172              : 
   12173              : static gfc_ss *
   12174         6101 : walk_inline_intrinsic_minmaxloc (gfc_ss *ss, gfc_expr *expr ATTRIBUTE_UNUSED)
   12175              : {
   12176         6101 :   if (expr->rank == 0)
   12177              :     return ss;
   12178              : 
   12179         6101 :   gfc_actual_arglist *array_arg = expr->value.function.actual;
   12180         6101 :   gfc_actual_arglist *dim_arg = array_arg->next;
   12181         6101 :   gfc_actual_arglist *mask_arg = dim_arg->next;
   12182         6101 :   gfc_actual_arglist *kind_arg = mask_arg->next;
   12183         6101 :   gfc_actual_arglist *back_arg = kind_arg->next;
   12184              : 
   12185         6101 :   gfc_expr *array = array_arg->expr;
   12186         6101 :   gfc_expr *dim = dim_arg->expr;
   12187         6101 :   gfc_expr *mask = mask_arg->expr;
   12188         6101 :   gfc_expr *back = back_arg->expr;
   12189              : 
   12190         6101 :   if (dim == nullptr)
   12191         3305 :     return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
   12192              : 
   12193         2796 :   gfc_ss *tmp_ss = gfc_ss_terminator;
   12194              : 
   12195         2796 :   bool scalar_mask = false;
   12196         2796 :   if (mask)
   12197              :     {
   12198         1866 :       gfc_ss *mask_ss = gfc_walk_subexpr (tmp_ss, mask);
   12199         1866 :       if (mask_ss == tmp_ss)
   12200              :         scalar_mask = true;
   12201         1174 :       else if (maybe_absent_optional_variable (mask))
   12202           20 :         mask_ss->info->can_be_null_ref = true;
   12203              : 
   12204              :       tmp_ss = mask_ss;
   12205              :     }
   12206              : 
   12207         2796 :   gfc_ss *array_ss = gfc_walk_subexpr (tmp_ss, array);
   12208         2796 :   gcc_assert (array_ss != tmp_ss);
   12209              : 
   12210         2796 :   tmp_ss = array_ss;
   12211              : 
   12212              :   /* Move the dimension on which we will sum to a separate nested scalarization
   12213              :      chain, "hiding" that dimension from the outer scalarization.  */
   12214         2796 :   int dim_val = mpz_get_si (dim->value.integer);
   12215         2796 :   gfc_ss *tail = nest_loop_dimension (tmp_ss, dim_val - 1);
   12216              : 
   12217         2796 :   if (back && array->rank > 1)
   12218              :     {
   12219              :       /* If there are nested scalarization loops, include BACK in the
   12220              :          scalarization chains to avoid evaluating it multiple times in a loop.
   12221              :          Otherwise, prefer to handle it outside of scalarization.  */
   12222         2796 :       gfc_ss *back_ss = gfc_get_scalar_ss (ss, back);
   12223         2796 :       back_ss->info->type = GFC_SS_REFERENCE;
   12224         2796 :       if (maybe_absent_optional_variable (back))
   12225           16 :         back_ss->info->can_be_null_ref = true;
   12226              : 
   12227         2796 :       tail->next = back_ss;
   12228         2796 :     }
   12229              :   else
   12230            0 :     tail->next = ss;
   12231              : 
   12232         2796 :   if (scalar_mask)
   12233              :     {
   12234          692 :       tmp_ss = gfc_get_scalar_ss (tmp_ss, mask);
   12235              :       /* MASK can be a forwarded optional argument, so make the necessary setup
   12236              :          to avoid the scalarizer generating any unguarded pointer dereference in
   12237              :          that case.  */
   12238          692 :       tmp_ss->info->type = GFC_SS_REFERENCE;
   12239          692 :       if (maybe_absent_optional_variable (mask))
   12240            4 :         tmp_ss->info->can_be_null_ref = true;
   12241              :     }
   12242              : 
   12243              :   return tmp_ss;
   12244              : }
   12245              : 
   12246              : 
   12247              : static gfc_ss *
   12248         8350 : walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr)
   12249              : {
   12250              : 
   12251         8350 :   switch (expr->value.function.isym->id)
   12252              :     {
   12253          575 :       case GFC_ISYM_PRODUCT:
   12254          575 :       case GFC_ISYM_SUM:
   12255          575 :         return walk_inline_intrinsic_arith (ss, expr);
   12256              : 
   12257         1674 :       case GFC_ISYM_TRANSPOSE:
   12258         1674 :         return walk_inline_intrinsic_transpose (ss, expr);
   12259              : 
   12260         6101 :       case GFC_ISYM_MAXLOC:
   12261         6101 :       case GFC_ISYM_MINLOC:
   12262         6101 :         return walk_inline_intrinsic_minmaxloc (ss, expr);
   12263              : 
   12264            0 :       default:
   12265            0 :         gcc_unreachable ();
   12266              :     }
   12267              :   gcc_unreachable ();
   12268              : }
   12269              : 
   12270              : 
   12271              : /* This generates code to execute before entering the scalarization loop.
   12272              :    Currently does nothing.  */
   12273              : 
   12274              : void
   12275        11704 : gfc_add_intrinsic_ss_code (gfc_loopinfo * loop ATTRIBUTE_UNUSED, gfc_ss * ss)
   12276              : {
   12277        11704 :   switch (ss->info->expr->value.function.isym->id)
   12278              :     {
   12279        11704 :     case GFC_ISYM_UBOUND:
   12280        11704 :     case GFC_ISYM_LBOUND:
   12281        11704 :     case GFC_ISYM_COSHAPE:
   12282        11704 :     case GFC_ISYM_UCOBOUND:
   12283        11704 :     case GFC_ISYM_LCOBOUND:
   12284        11704 :     case GFC_ISYM_MAXLOC:
   12285        11704 :     case GFC_ISYM_MINLOC:
   12286        11704 :     case GFC_ISYM_THIS_IMAGE:
   12287        11704 :     case GFC_ISYM_SHAPE:
   12288        11704 :       break;
   12289              : 
   12290            0 :     default:
   12291            0 :       gcc_unreachable ();
   12292              :     }
   12293        11704 : }
   12294              : 
   12295              : 
   12296              : /* The LBOUND, LCOBOUND, UBOUND, UCOBOUND, and SHAPE intrinsics with
   12297              :    one parameter are expanded into code inside the scalarization loop.  */
   12298              : 
   12299              : static gfc_ss *
   12300        10268 : gfc_walk_intrinsic_bound (gfc_ss * ss, gfc_expr * expr)
   12301              : {
   12302        10268 :   if (expr->value.function.actual->expr->ts.type == BT_CLASS)
   12303          465 :     gfc_add_class_array_ref (expr->value.function.actual->expr);
   12304              : 
   12305              :   /* The two argument version returns a scalar.  */
   12306        10268 :   if (expr->value.function.isym->id != GFC_ISYM_SHAPE
   12307         3593 :       && expr->value.function.isym->id != GFC_ISYM_COSHAPE
   12308         3577 :       && expr->value.function.actual->next->expr)
   12309              :     return ss;
   12310              : 
   12311        10268 :   return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
   12312              : }
   12313              : 
   12314              : 
   12315              : /* Walk an intrinsic array libcall.  */
   12316              : 
   12317              : static gfc_ss *
   12318        14628 : gfc_walk_intrinsic_libfunc (gfc_ss * ss, gfc_expr * expr)
   12319              : {
   12320        14628 :   gcc_assert (expr->rank > 0);
   12321        14628 :   return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
   12322              : }
   12323              : 
   12324              : 
   12325              : /* Return whether the function call expression EXPR will be expanded
   12326              :    inline by gfc_conv_intrinsic_function.  */
   12327              : 
   12328              : bool
   12329       309716 : gfc_inline_intrinsic_function_p (gfc_expr *expr)
   12330              : {
   12331       309716 :   gfc_actual_arglist *args, *dim_arg, *mask_arg;
   12332       309716 :   gfc_expr *maskexpr;
   12333              : 
   12334       309716 :   gfc_intrinsic_sym *isym = expr->value.function.isym;
   12335       309716 :   if (!isym)
   12336              :     return false;
   12337              : 
   12338       309674 :   switch (isym->id)
   12339              :     {
   12340         5371 :     case GFC_ISYM_PRODUCT:
   12341         5371 :     case GFC_ISYM_SUM:
   12342              :       /* Disable inline expansion if code size matters.  */
   12343         5371 :       if (optimize_size)
   12344              :         return false;
   12345              : 
   12346         4463 :       args = expr->value.function.actual;
   12347         4463 :       dim_arg = args->next;
   12348              : 
   12349              :       /* We need to be able to subset the SUM argument at compile-time.  */
   12350         4463 :       if (dim_arg->expr && dim_arg->expr->expr_type != EXPR_CONSTANT)
   12351              :         return false;
   12352              : 
   12353              :       /* FIXME: If MASK is optional for a more than two-dimensional
   12354              :          argument, the scalarizer gets confused if the mask is
   12355              :          absent.  See PR 82995.  For now, fall back to the library
   12356              :          function.  */
   12357              : 
   12358         3851 :       mask_arg = dim_arg->next;
   12359         3851 :       maskexpr = mask_arg->expr;
   12360              : 
   12361         3851 :       if (expr->rank > 0 && maskexpr && maskexpr->expr_type == EXPR_VARIABLE
   12362          276 :           && maskexpr->symtree->n.sym->attr.dummy
   12363           48 :           && maskexpr->symtree->n.sym->attr.optional)
   12364              :         return false;
   12365              : 
   12366              :       return true;
   12367              : 
   12368              :     case GFC_ISYM_TRANSPOSE:
   12369              :       return true;
   12370              : 
   12371        57312 :     case GFC_ISYM_MINLOC:
   12372        57312 :     case GFC_ISYM_MAXLOC:
   12373        57312 :       {
   12374        57312 :         if ((isym->id == GFC_ISYM_MINLOC
   12375        30583 :              && (flag_inline_intrinsics
   12376        30583 :                  & GFC_FLAG_INLINE_INTRINSIC_MINLOC) == 0)
   12377        46713 :             || (isym->id == GFC_ISYM_MAXLOC
   12378        26729 :                 && (flag_inline_intrinsics
   12379        26729 :                     & GFC_FLAG_INLINE_INTRINSIC_MAXLOC) == 0))
   12380              :           return false;
   12381              : 
   12382        37718 :         gfc_actual_arglist *array_arg = expr->value.function.actual;
   12383        37718 :         gfc_actual_arglist *dim_arg = array_arg->next;
   12384              : 
   12385        37718 :         gfc_expr *array = array_arg->expr;
   12386        37718 :         gfc_expr *dim = dim_arg->expr;
   12387              : 
   12388        37718 :         if (!(array->ts.type == BT_INTEGER
   12389              :               || array->ts.type == BT_REAL))
   12390              :           return false;
   12391              : 
   12392        34738 :         if (array->rank == 1)
   12393              :           return true;
   12394              : 
   12395        20791 :         if (dim != nullptr
   12396        13372 :             && dim->expr_type != EXPR_CONSTANT)
   12397              :           return false;
   12398              : 
   12399              :         return true;
   12400              :       }
   12401              : 
   12402              :     default:
   12403              :       return false;
   12404              :     }
   12405              : }
   12406              : 
   12407              : 
   12408              : /* Returns nonzero if the specified intrinsic function call maps directly to
   12409              :    an external library call.  Should only be used for functions that return
   12410              :    arrays.  */
   12411              : 
   12412              : int
   12413        88763 : gfc_is_intrinsic_libcall (gfc_expr * expr)
   12414              : {
   12415        88763 :   gcc_assert (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym);
   12416        88763 :   gcc_assert (expr->rank > 0);
   12417              : 
   12418        88763 :   if (gfc_inline_intrinsic_function_p (expr))
   12419              :     return 0;
   12420              : 
   12421        74114 :   switch (expr->value.function.isym->id)
   12422              :     {
   12423              :     case GFC_ISYM_ALL:
   12424              :     case GFC_ISYM_ANY:
   12425              :     case GFC_ISYM_COUNT:
   12426              :     case GFC_ISYM_FINDLOC:
   12427              :     case GFC_ISYM_JN2:
   12428              :     case GFC_ISYM_IANY:
   12429              :     case GFC_ISYM_IALL:
   12430              :     case GFC_ISYM_IPARITY:
   12431              :     case GFC_ISYM_MATMUL:
   12432              :     case GFC_ISYM_MAXLOC:
   12433              :     case GFC_ISYM_MAXVAL:
   12434              :     case GFC_ISYM_MINLOC:
   12435              :     case GFC_ISYM_MINVAL:
   12436              :     case GFC_ISYM_NORM2:
   12437              :     case GFC_ISYM_PARITY:
   12438              :     case GFC_ISYM_PRODUCT:
   12439              :     case GFC_ISYM_SUM:
   12440              :     case GFC_ISYM_SPREAD:
   12441              :     case GFC_ISYM_YN2:
   12442              :       /* Ignore absent optional parameters.  */
   12443              :       return 1;
   12444              : 
   12445        15963 :     case GFC_ISYM_CSHIFT:
   12446        15963 :     case GFC_ISYM_EOSHIFT:
   12447        15963 :     case GFC_ISYM_GET_TEAM:
   12448        15963 :     case GFC_ISYM_FAILED_IMAGES:
   12449        15963 :     case GFC_ISYM_STOPPED_IMAGES:
   12450        15963 :     case GFC_ISYM_PACK:
   12451        15963 :     case GFC_ISYM_REDUCE:
   12452        15963 :     case GFC_ISYM_RESHAPE:
   12453        15963 :     case GFC_ISYM_UNPACK:
   12454              :       /* Pass absent optional parameters.  */
   12455        15963 :       return 2;
   12456              : 
   12457              :     default:
   12458              :       return 0;
   12459              :     }
   12460              : }
   12461              : 
   12462              : /* Walk an intrinsic function.  */
   12463              : gfc_ss *
   12464        56278 : gfc_walk_intrinsic_function (gfc_ss * ss, gfc_expr * expr,
   12465              :                              gfc_intrinsic_sym * isym)
   12466              : {
   12467        56278 :   gcc_assert (isym);
   12468              : 
   12469        56278 :   if (isym->elemental)
   12470        18516 :     return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
   12471              :                                              expr->value.function.isym,
   12472        18516 :                                              GFC_SS_SCALAR);
   12473              : 
   12474        37762 :   if (expr->rank == 0 && expr->corank == 0)
   12475              :     return ss;
   12476              : 
   12477        33246 :   if (gfc_inline_intrinsic_function_p (expr))
   12478         8350 :     return walk_inline_intrinsic_function (ss, expr);
   12479              : 
   12480        24896 :   if (expr->rank != 0 && gfc_is_intrinsic_libcall (expr))
   12481        13579 :     return gfc_walk_intrinsic_libfunc (ss, expr);
   12482              : 
   12483              :   /* Special cases.  */
   12484        11317 :   switch (isym->id)
   12485              :     {
   12486        10268 :     case GFC_ISYM_COSHAPE:
   12487        10268 :     case GFC_ISYM_LBOUND:
   12488        10268 :     case GFC_ISYM_LCOBOUND:
   12489        10268 :     case GFC_ISYM_UBOUND:
   12490        10268 :     case GFC_ISYM_UCOBOUND:
   12491        10268 :     case GFC_ISYM_THIS_IMAGE:
   12492        10268 :     case GFC_ISYM_SHAPE:
   12493        10268 :       return gfc_walk_intrinsic_bound (ss, expr);
   12494              : 
   12495         1049 :     case GFC_ISYM_TRANSFER:
   12496         1049 :     case GFC_ISYM_CAF_GET:
   12497         1049 :       return gfc_walk_intrinsic_libfunc (ss, expr);
   12498              : 
   12499            0 :     default:
   12500              :       /* This probably meant someone forgot to add an intrinsic to the above
   12501              :          list(s) when they implemented it, or something's gone horribly
   12502              :          wrong.  */
   12503            0 :       gcc_unreachable ();
   12504              :     }
   12505              : }
   12506              : 
   12507              : static tree
   12508          112 : conv_co_collective (gfc_code *code)
   12509              : {
   12510          112 :   gfc_se argse;
   12511          112 :   stmtblock_t block, post_block;
   12512          112 :   tree fndecl, array = NULL_TREE, strlen, image_index, stat, errmsg, errmsg_len;
   12513          112 :   gfc_expr *image_idx_expr, *stat_expr, *errmsg_expr, *opr_expr;
   12514              : 
   12515          112 :   gfc_start_block (&block);
   12516          112 :   gfc_init_block (&post_block);
   12517              : 
   12518          112 :   if (code->resolved_isym->id == GFC_ISYM_CO_REDUCE)
   12519              :     {
   12520           17 :       opr_expr = code->ext.actual->next->expr;
   12521           17 :       image_idx_expr = code->ext.actual->next->next->expr;
   12522           17 :       stat_expr = code->ext.actual->next->next->next->expr;
   12523           17 :       errmsg_expr = code->ext.actual->next->next->next->next->expr;
   12524              :     }
   12525              :   else
   12526              :     {
   12527           95 :       opr_expr = NULL;
   12528           95 :       image_idx_expr = code->ext.actual->next->expr;
   12529           95 :       stat_expr = code->ext.actual->next->next->expr;
   12530           95 :       errmsg_expr = code->ext.actual->next->next->next->expr;
   12531              :     }
   12532              : 
   12533              :   /* stat.  */
   12534          112 :   if (stat_expr)
   12535              :     {
   12536           74 :       gfc_init_se (&argse, NULL);
   12537           74 :       gfc_conv_expr (&argse, stat_expr);
   12538           74 :       gfc_add_block_to_block (&block, &argse.pre);
   12539           74 :       gfc_add_block_to_block (&post_block, &argse.post);
   12540           74 :       stat = argse.expr;
   12541           74 :       if (flag_coarray != GFC_FCOARRAY_SINGLE)
   12542           42 :         stat = gfc_build_addr_expr (NULL_TREE, stat);
   12543              :     }
   12544           38 :   else if (flag_coarray == GFC_FCOARRAY_SINGLE)
   12545              :     stat = NULL_TREE;
   12546              :   else
   12547           26 :     stat = null_pointer_node;
   12548              : 
   12549              :   /* Early exit for GFC_FCOARRAY_SINGLE.  */
   12550          112 :   if (flag_coarray == GFC_FCOARRAY_SINGLE)
   12551              :     {
   12552           44 :       if (stat != NULL_TREE)
   12553              :         {
   12554              :           /* For optional stats, check the pointer is valid before zero'ing.  */
   12555           32 :           if (gfc_expr_attr (stat_expr).optional)
   12556              :             {
   12557           12 :               tree tmp;
   12558           12 :               stmtblock_t ass_block;
   12559           12 :               gfc_start_block (&ass_block);
   12560           12 :               gfc_add_modify (&ass_block, stat,
   12561           12 :                               fold_convert (TREE_TYPE (stat),
   12562              :                                             integer_zero_node));
   12563           12 :               tmp = fold_build2 (NE_EXPR, logical_type_node,
   12564              :                                  gfc_build_addr_expr (NULL_TREE, stat),
   12565              :                                  null_pointer_node);
   12566           12 :               tmp = fold_build3 (COND_EXPR, void_type_node, tmp,
   12567              :                                  gfc_finish_block (&ass_block),
   12568              :                                  build_empty_stmt (input_location));
   12569           12 :               gfc_add_expr_to_block (&block, tmp);
   12570              :             }
   12571              :           else
   12572           20 :             gfc_add_modify (&block, stat,
   12573           20 :                             fold_convert (TREE_TYPE (stat), integer_zero_node));
   12574              :         }
   12575           44 :       return gfc_finish_block (&block);
   12576              :     }
   12577              : 
   12578            5 :   gfc_symbol *derived = code->ext.actual->expr->ts.type == BT_DERIVED
   12579           68 :     ? code->ext.actual->expr->ts.u.derived : NULL;
   12580              : 
   12581              :   /* Handle the array.  */
   12582           68 :   gfc_init_se (&argse, NULL);
   12583           68 :   if (!derived || !derived->attr.alloc_comp
   12584            1 :       || code->resolved_isym->id != GFC_ISYM_CO_BROADCAST)
   12585              :     {
   12586           67 :       if (code->ext.actual->expr->rank == 0)
   12587              :         {
   12588           30 :           symbol_attribute attr;
   12589           30 :           gfc_clear_attr (&attr);
   12590           30 :           gfc_init_se (&argse, NULL);
   12591           30 :           gfc_conv_expr (&argse, code->ext.actual->expr);
   12592           30 :           gfc_add_block_to_block (&block, &argse.pre);
   12593           30 :           gfc_add_block_to_block (&post_block, &argse.post);
   12594           30 :           array = gfc_conv_scalar_to_descriptor (&argse, argse.expr, attr);
   12595           30 :           array = gfc_build_addr_expr (NULL_TREE, array);
   12596              :         }
   12597              :       else
   12598              :         {
   12599           37 :           argse.want_pointer = 1;
   12600           37 :           gfc_conv_expr_descriptor (&argse, code->ext.actual->expr);
   12601           37 :           array = argse.expr;
   12602              :         }
   12603              :     }
   12604              : 
   12605           68 :   gfc_add_block_to_block (&block, &argse.pre);
   12606           68 :   gfc_add_block_to_block (&post_block, &argse.post);
   12607              : 
   12608           68 :   if (code->ext.actual->expr->ts.type == BT_CHARACTER)
   12609           15 :     strlen = argse.string_length;
   12610              :   else
   12611           53 :     strlen = integer_zero_node;
   12612              : 
   12613              :   /* image_index.  */
   12614           68 :   if (image_idx_expr)
   12615              :     {
   12616           39 :       gfc_init_se (&argse, NULL);
   12617           39 :       gfc_conv_expr (&argse, image_idx_expr);
   12618           39 :       gfc_add_block_to_block (&block, &argse.pre);
   12619           39 :       gfc_add_block_to_block (&post_block, &argse.post);
   12620           39 :       image_index = fold_convert (integer_type_node, argse.expr);
   12621              :     }
   12622              :   else
   12623           29 :     image_index = integer_zero_node;
   12624              : 
   12625              :   /* errmsg.  */
   12626           68 :   if (errmsg_expr)
   12627              :     {
   12628           29 :       gfc_init_se (&argse, NULL);
   12629           29 :       gfc_conv_expr_reference (&argse, errmsg_expr);
   12630           29 :       gfc_add_block_to_block (&block, &argse.pre);
   12631           29 :       gfc_add_block_to_block (&post_block, &argse.post);
   12632           29 :       errmsg = argse.expr;
   12633           29 :       errmsg_len = fold_convert (size_type_node, argse.string_length);
   12634              :     }
   12635              :   else
   12636              :     {
   12637           39 :       errmsg = null_pointer_node;
   12638           39 :       errmsg_len = build_zero_cst (size_type_node);
   12639              :     }
   12640              : 
   12641              :   /* Generate the function call.  */
   12642           68 :   switch (code->resolved_isym->id)
   12643              :     {
   12644           24 :     case GFC_ISYM_CO_BROADCAST:
   12645           24 :       fndecl = gfor_fndecl_co_broadcast;
   12646           24 :       break;
   12647           10 :     case GFC_ISYM_CO_MAX:
   12648           10 :       fndecl = gfor_fndecl_co_max;
   12649           10 :       break;
   12650            6 :     case GFC_ISYM_CO_MIN:
   12651            6 :       fndecl = gfor_fndecl_co_min;
   12652            6 :       break;
   12653           12 :     case GFC_ISYM_CO_REDUCE:
   12654           12 :       fndecl = gfor_fndecl_co_reduce;
   12655           12 :       break;
   12656           16 :     case GFC_ISYM_CO_SUM:
   12657           16 :       fndecl = gfor_fndecl_co_sum;
   12658           16 :       break;
   12659            0 :     default:
   12660            0 :       gcc_unreachable ();
   12661              :     }
   12662              : 
   12663           68 :   if (derived && derived->attr.alloc_comp
   12664            1 :       && code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
   12665              :     /* The derived type has the attribute 'alloc_comp'.  */
   12666              :     {
   12667            2 :       tree tmp = gfc_bcast_alloc_comp (derived, code->ext.actual->expr,
   12668            1 :                                        code->ext.actual->expr->rank,
   12669              :                                        image_index, stat, errmsg, errmsg_len);
   12670            1 :       gfc_add_expr_to_block (&block, tmp);
   12671            1 :     }
   12672              :   else
   12673              :     {
   12674           67 :       if (code->resolved_isym->id == GFC_ISYM_CO_SUM
   12675           51 :           || code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
   12676           39 :         fndecl = build_call_expr_loc (input_location, fndecl, 5, array,
   12677              :                                       image_index, stat, errmsg, errmsg_len);
   12678           28 :       else if (code->resolved_isym->id != GFC_ISYM_CO_REDUCE)
   12679           16 :         fndecl = build_call_expr_loc (input_location, fndecl, 6, array,
   12680              :                                       image_index, stat, errmsg,
   12681              :                                       strlen, errmsg_len);
   12682              :       else
   12683              :         {
   12684           12 :           tree opr, opr_flags;
   12685              : 
   12686              :           // FIXME: Handle TS29113's bind(C) strings with descriptor.
   12687           12 :           int opr_flag_int;
   12688           12 :           if (gfc_is_proc_ptr_comp (opr_expr))
   12689              :             {
   12690            0 :               gfc_symbol *sym = gfc_get_proc_ptr_comp (opr_expr)->ts.interface;
   12691            0 :               opr_flag_int = sym->attr.dimension
   12692            0 :                 || (sym->ts.type == BT_CHARACTER
   12693            0 :                     && !sym->attr.is_bind_c)
   12694            0 :                 ? GFC_CAF_BYREF : 0;
   12695            0 :               opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
   12696            0 :                 && !sym->attr.is_bind_c
   12697            0 :                 ? GFC_CAF_HIDDENLEN : 0;
   12698            0 :               opr_flag_int |= sym->formal->sym->attr.value
   12699            0 :                 ? GFC_CAF_ARG_VALUE : 0;
   12700              :             }
   12701              :           else
   12702              :             {
   12703           12 :               opr_flag_int = gfc_return_by_reference (opr_expr->symtree->n.sym)
   12704           12 :                 ? GFC_CAF_BYREF : 0;
   12705           24 :               opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
   12706            0 :                 && !opr_expr->symtree->n.sym->attr.is_bind_c
   12707           12 :                 ? GFC_CAF_HIDDENLEN : 0;
   12708           12 :               opr_flag_int |= opr_expr->symtree->n.sym->formal->sym->attr.value
   12709           12 :                 ? GFC_CAF_ARG_VALUE : 0;
   12710              :             }
   12711           12 :           opr_flags = build_int_cst (integer_type_node, opr_flag_int);
   12712           12 :           gfc_conv_expr (&argse, opr_expr);
   12713           12 :           opr = argse.expr;
   12714           12 :           fndecl = build_call_expr_loc (input_location, fndecl, 8, array, opr,
   12715              :                                         opr_flags, image_index, stat, errmsg,
   12716              :                                         strlen, errmsg_len);
   12717              :         }
   12718              :     }
   12719              : 
   12720           68 :   gfc_add_expr_to_block (&block, fndecl);
   12721           68 :   gfc_add_block_to_block (&block, &post_block);
   12722              : 
   12723           68 :   return gfc_finish_block (&block);
   12724              : }
   12725              : 
   12726              : 
   12727              : static tree
   12728          101 : conv_intrinsic_atomic_op (gfc_code *code)
   12729              : {
   12730          101 :   gfc_se argse;
   12731          101 :   tree tmp, atom, value, old = NULL_TREE, stat = NULL_TREE;
   12732          101 :   stmtblock_t block, post_block;
   12733          101 :   gfc_expr *atom_expr = code->ext.actual->expr;
   12734          101 :   gfc_expr *stat_expr;
   12735          101 :   built_in_function fn;
   12736              : 
   12737          101 :   if (atom_expr->expr_type == EXPR_FUNCTION
   12738            0 :       && atom_expr->value.function.isym
   12739            0 :       && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
   12740            0 :     atom_expr = atom_expr->value.function.actual->expr;
   12741              : 
   12742          101 :   gfc_start_block (&block);
   12743          101 :   gfc_init_block (&post_block);
   12744              : 
   12745          101 :   gfc_init_se (&argse, NULL);
   12746          101 :   argse.want_pointer = 1;
   12747          101 :   gfc_conv_expr (&argse, atom_expr);
   12748          101 :   gfc_add_block_to_block (&block, &argse.pre);
   12749          101 :   gfc_add_block_to_block (&post_block, &argse.post);
   12750          101 :   atom = argse.expr;
   12751              : 
   12752          101 :   gfc_init_se (&argse, NULL);
   12753          101 :   if (flag_coarray == GFC_FCOARRAY_LIB
   12754           60 :       && code->ext.actual->next->expr->ts.kind == atom_expr->ts.kind)
   12755           58 :     argse.want_pointer = 1;
   12756          101 :   gfc_conv_expr (&argse, code->ext.actual->next->expr);
   12757          101 :   gfc_add_block_to_block (&block, &argse.pre);
   12758          101 :   gfc_add_block_to_block (&post_block, &argse.post);
   12759          101 :   value = argse.expr;
   12760              : 
   12761          101 :   switch (code->resolved_isym->id)
   12762              :     {
   12763           64 :     case GFC_ISYM_ATOMIC_ADD:
   12764           64 :     case GFC_ISYM_ATOMIC_AND:
   12765           64 :     case GFC_ISYM_ATOMIC_DEF:
   12766           64 :     case GFC_ISYM_ATOMIC_OR:
   12767           64 :     case GFC_ISYM_ATOMIC_XOR:
   12768           64 :       stat_expr = code->ext.actual->next->next->expr;
   12769           64 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   12770           38 :         old = null_pointer_node;
   12771              :       break;
   12772           37 :     default:
   12773           37 :       gfc_init_se (&argse, NULL);
   12774           37 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   12775           22 :         argse.want_pointer = 1;
   12776           37 :       gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
   12777           37 :       gfc_add_block_to_block (&block, &argse.pre);
   12778           37 :       gfc_add_block_to_block (&post_block, &argse.post);
   12779           37 :       old = argse.expr;
   12780           37 :       stat_expr = code->ext.actual->next->next->next->expr;
   12781              :     }
   12782              : 
   12783              :   /* STAT=  */
   12784          101 :   if (stat_expr != NULL)
   12785              :     {
   12786           82 :       gcc_assert (stat_expr->expr_type == EXPR_VARIABLE);
   12787           82 :       gfc_init_se (&argse, NULL);
   12788           82 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   12789           48 :         argse.want_pointer = 1;
   12790           82 :       gfc_conv_expr_val (&argse, stat_expr);
   12791           82 :       gfc_add_block_to_block (&block, &argse.pre);
   12792           82 :       gfc_add_block_to_block (&post_block, &argse.post);
   12793           82 :       stat = argse.expr;
   12794              :     }
   12795           19 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
   12796           12 :     stat = null_pointer_node;
   12797              : 
   12798          101 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   12799              :     {
   12800           60 :       tree image_index, caf_decl, offset, token;
   12801           60 :       int op;
   12802              : 
   12803           60 :       switch (code->resolved_isym->id)
   12804              :         {
   12805              :         case GFC_ISYM_ATOMIC_ADD:
   12806              :         case GFC_ISYM_ATOMIC_FETCH_ADD:
   12807              :           op = (int) GFC_CAF_ATOMIC_ADD;
   12808              :           break;
   12809           12 :         case GFC_ISYM_ATOMIC_AND:
   12810           12 :         case GFC_ISYM_ATOMIC_FETCH_AND:
   12811           12 :           op = (int) GFC_CAF_ATOMIC_AND;
   12812           12 :           break;
   12813           12 :         case GFC_ISYM_ATOMIC_OR:
   12814           12 :         case GFC_ISYM_ATOMIC_FETCH_OR:
   12815           12 :           op = (int) GFC_CAF_ATOMIC_OR;
   12816           12 :           break;
   12817           12 :         case GFC_ISYM_ATOMIC_XOR:
   12818           12 :         case GFC_ISYM_ATOMIC_FETCH_XOR:
   12819           12 :           op = (int) GFC_CAF_ATOMIC_XOR;
   12820           12 :           break;
   12821           15 :         case GFC_ISYM_ATOMIC_DEF:
   12822           15 :           op = 0;  /* Unused.  */
   12823           15 :           break;
   12824            0 :         default:
   12825            0 :           gcc_unreachable ();
   12826              :         }
   12827              : 
   12828           60 :       caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
   12829           60 :       if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
   12830            0 :         caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
   12831              : 
   12832           60 :       if (gfc_is_coindexed (atom_expr))
   12833           52 :         image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
   12834              :       else
   12835            8 :         image_index = integer_zero_node;
   12836              : 
   12837              :       /* Ensure VALUE names addressable storage: taking the address of a
   12838              :          constant is invalid in C, and scalars need a temporary as well.  */
   12839           60 :       if (!POINTER_TYPE_P (TREE_TYPE (value)))
   12840              :         {
   12841           42 :           tree elem
   12842           42 :             = fold_convert (TREE_TYPE (TREE_TYPE (atom)), value);
   12843           42 :           elem = gfc_trans_force_lval (&block, elem);
   12844           42 :           value = gfc_build_addr_expr (NULL_TREE, elem);
   12845              :         }
   12846           18 :       else if (TREE_CODE (value) == ADDR_EXPR
   12847           18 :                && TREE_CONSTANT (TREE_OPERAND (value, 0)))
   12848              :         {
   12849            0 :           tree elem
   12850            0 :             = fold_convert (TREE_TYPE (TREE_TYPE (atom)),
   12851              :                             build_fold_indirect_ref (value));
   12852            0 :           elem = gfc_trans_force_lval (&block, elem);
   12853            0 :           value = gfc_build_addr_expr (NULL_TREE, elem);
   12854              :         }
   12855              : 
   12856           60 :       gfc_init_se (&argse, NULL);
   12857           60 :       gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
   12858              :                                 atom_expr);
   12859              : 
   12860           60 :       gfc_add_block_to_block (&block, &argse.pre);
   12861           60 :       if (code->resolved_isym->id == GFC_ISYM_ATOMIC_DEF)
   12862           15 :         tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_def, 7,
   12863              :                                    token, offset, image_index, value, stat,
   12864              :                                    build_int_cst (integer_type_node,
   12865           15 :                                                   (int) atom_expr->ts.type),
   12866              :                                    build_int_cst (integer_type_node,
   12867           15 :                                                   (int) atom_expr->ts.kind));
   12868              :       else
   12869           45 :         tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_op, 9,
   12870           45 :                                    build_int_cst (integer_type_node, op),
   12871              :                                    token, offset, image_index, value, old, stat,
   12872              :                                    build_int_cst (integer_type_node,
   12873           45 :                                                   (int) atom_expr->ts.type),
   12874              :                                    build_int_cst (integer_type_node,
   12875           45 :                                                   (int) atom_expr->ts.kind));
   12876              : 
   12877           60 :       gfc_add_expr_to_block (&block, tmp);
   12878           60 :       gfc_add_block_to_block (&block, &argse.post);
   12879           60 :       gfc_add_block_to_block (&block, &post_block);
   12880           60 :       return gfc_finish_block (&block);
   12881              :     }
   12882              : 
   12883              : 
   12884           41 :   switch (code->resolved_isym->id)
   12885              :     {
   12886              :     case GFC_ISYM_ATOMIC_ADD:
   12887              :     case GFC_ISYM_ATOMIC_FETCH_ADD:
   12888              :       fn = BUILT_IN_ATOMIC_FETCH_ADD_N;
   12889              :       break;
   12890            8 :     case GFC_ISYM_ATOMIC_AND:
   12891            8 :     case GFC_ISYM_ATOMIC_FETCH_AND:
   12892            8 :       fn = BUILT_IN_ATOMIC_FETCH_AND_N;
   12893            8 :       break;
   12894           11 :     case GFC_ISYM_ATOMIC_DEF:
   12895           11 :       fn = BUILT_IN_ATOMIC_STORE_N;
   12896           11 :       break;
   12897            8 :     case GFC_ISYM_ATOMIC_OR:
   12898            8 :     case GFC_ISYM_ATOMIC_FETCH_OR:
   12899            8 :       fn = BUILT_IN_ATOMIC_FETCH_OR_N;
   12900            8 :       break;
   12901            8 :     case GFC_ISYM_ATOMIC_XOR:
   12902            8 :     case GFC_ISYM_ATOMIC_FETCH_XOR:
   12903            8 :       fn = BUILT_IN_ATOMIC_FETCH_XOR_N;
   12904            8 :       break;
   12905            0 :     default:
   12906            0 :       gcc_unreachable ();
   12907              :     }
   12908              : 
   12909           41 :   tmp = TREE_TYPE (TREE_TYPE (atom));
   12910           82 :   fn = (built_in_function) ((int) fn
   12911           41 :                             + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
   12912           41 :                             + 1);
   12913           41 :   tree itype = TREE_TYPE (TREE_TYPE (atom));
   12914           41 :   tmp = builtin_decl_explicit (fn);
   12915              : 
   12916           41 :   switch (code->resolved_isym->id)
   12917              :     {
   12918           26 :     case GFC_ISYM_ATOMIC_ADD:
   12919           26 :     case GFC_ISYM_ATOMIC_AND:
   12920           26 :     case GFC_ISYM_ATOMIC_DEF:
   12921           26 :     case GFC_ISYM_ATOMIC_OR:
   12922           26 :     case GFC_ISYM_ATOMIC_XOR:
   12923           26 :       tmp = build_call_expr_loc (input_location, tmp, 3, atom,
   12924              :                                  fold_convert (itype, value),
   12925              :                                  build_int_cst (integer_type_node, MEMMODEL_RELAXED));
   12926           26 :       gfc_add_expr_to_block (&block, tmp);
   12927           26 :       break;
   12928           15 :     default:
   12929           15 :       tmp = build_call_expr_loc (input_location, tmp, 3, atom,
   12930              :                                  fold_convert (itype, value),
   12931              :                                  build_int_cst (integer_type_node, MEMMODEL_RELAXED));
   12932           15 :       gfc_add_modify (&block, old, fold_convert (TREE_TYPE (old), tmp));
   12933           15 :       break;
   12934              :     }
   12935              : 
   12936           41 :   if (stat != NULL_TREE)
   12937           34 :     gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
   12938           41 :   gfc_add_block_to_block (&block, &post_block);
   12939           41 :   return gfc_finish_block (&block);
   12940              : }
   12941              : 
   12942              : 
   12943              : static tree
   12944          182 : conv_intrinsic_atomic_ref (gfc_code *code)
   12945              : {
   12946          182 :   gfc_se argse;
   12947          182 :   tree tmp, atom, value, stat = NULL_TREE;
   12948          182 :   stmtblock_t block, post_block;
   12949          182 :   built_in_function fn;
   12950          182 :   gfc_expr *atom_expr = code->ext.actual->next->expr;
   12951              : 
   12952          182 :   if (atom_expr->expr_type == EXPR_FUNCTION
   12953            0 :       && atom_expr->value.function.isym
   12954            0 :       && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
   12955            0 :     atom_expr = atom_expr->value.function.actual->expr;
   12956              : 
   12957          182 :   gfc_start_block (&block);
   12958          182 :   gfc_init_block (&post_block);
   12959          182 :   gfc_init_se (&argse, NULL);
   12960          182 :   argse.want_pointer = 1;
   12961          182 :   gfc_conv_expr (&argse, atom_expr);
   12962          182 :   gfc_add_block_to_block (&block, &argse.pre);
   12963          182 :   gfc_add_block_to_block (&post_block, &argse.post);
   12964          182 :   atom = argse.expr;
   12965              : 
   12966          182 :   gfc_init_se (&argse, NULL);
   12967          182 :   if (flag_coarray == GFC_FCOARRAY_LIB
   12968          119 :       && code->ext.actual->expr->ts.kind == atom_expr->ts.kind)
   12969          113 :     argse.want_pointer = 1;
   12970          182 :   gfc_conv_expr (&argse, code->ext.actual->expr);
   12971          182 :   gfc_add_block_to_block (&block, &argse.pre);
   12972          182 :   gfc_add_block_to_block (&post_block, &argse.post);
   12973          182 :   value = argse.expr;
   12974              : 
   12975              :   /* STAT=  */
   12976          182 :   if (code->ext.actual->next->next->expr != NULL)
   12977              :     {
   12978          164 :       gcc_assert (code->ext.actual->next->next->expr->expr_type
   12979              :                   == EXPR_VARIABLE);
   12980          164 :       gfc_init_se (&argse, NULL);
   12981          164 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   12982          108 :         argse.want_pointer = 1;
   12983          164 :       gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
   12984          164 :       gfc_add_block_to_block (&block, &argse.pre);
   12985          164 :       gfc_add_block_to_block (&post_block, &argse.post);
   12986          164 :       stat = argse.expr;
   12987              :     }
   12988           18 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
   12989           11 :     stat = null_pointer_node;
   12990              : 
   12991          182 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   12992              :     {
   12993          119 :       tree image_index, caf_decl, offset, token;
   12994          119 :       tree orig_value = NULL_TREE, vardecl = NULL_TREE;
   12995              : 
   12996          119 :       caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
   12997          119 :       if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
   12998            0 :         caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
   12999              : 
   13000          119 :       if (gfc_is_coindexed (atom_expr))
   13001          103 :         image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
   13002              :       else
   13003           16 :         image_index = integer_zero_node;
   13004              : 
   13005          119 :       gfc_init_se (&argse, NULL);
   13006          119 :       gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
   13007              :                                 atom_expr);
   13008          119 :       gfc_add_block_to_block (&block, &argse.pre);
   13009              : 
   13010              :       /* Different type, need type conversion.  */
   13011          119 :       if (!POINTER_TYPE_P (TREE_TYPE (value)))
   13012              :         {
   13013            6 :           vardecl = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
   13014            6 :           orig_value = value;
   13015            6 :           value = gfc_build_addr_expr (NULL_TREE, vardecl);
   13016              :         }
   13017              : 
   13018          119 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_ref, 7,
   13019              :                                  token, offset, image_index, value, stat,
   13020              :                                  build_int_cst (integer_type_node,
   13021          119 :                                                 (int) atom_expr->ts.type),
   13022              :                                  build_int_cst (integer_type_node,
   13023          119 :                                                 (int) atom_expr->ts.kind));
   13024          119 :       gfc_add_expr_to_block (&block, tmp);
   13025          119 :       if (vardecl != NULL_TREE)
   13026            6 :         gfc_add_modify (&block, orig_value,
   13027            6 :                         fold_convert (TREE_TYPE (orig_value), vardecl));
   13028          119 :       gfc_add_block_to_block (&block, &argse.post);
   13029          119 :       gfc_add_block_to_block (&block, &post_block);
   13030          119 :       return gfc_finish_block (&block);
   13031              :     }
   13032              : 
   13033           63 :   tmp = TREE_TYPE (TREE_TYPE (atom));
   13034          126 :   fn = (built_in_function) ((int) BUILT_IN_ATOMIC_LOAD_N
   13035           63 :                             + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
   13036           63 :                             + 1);
   13037           63 :   tmp = builtin_decl_explicit (fn);
   13038           63 :   tmp = build_call_expr_loc (input_location, tmp, 2, atom,
   13039              :                              build_int_cst (integer_type_node,
   13040              :                                             MEMMODEL_RELAXED));
   13041           63 :   gfc_add_modify (&block, value, fold_convert (TREE_TYPE (value), tmp));
   13042              : 
   13043           63 :   if (stat != NULL_TREE)
   13044           56 :     gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
   13045           63 :   gfc_add_block_to_block (&block, &post_block);
   13046           63 :   return gfc_finish_block (&block);
   13047              : }
   13048              : 
   13049              : 
   13050              : static tree
   13051           14 : conv_intrinsic_atomic_cas (gfc_code *code)
   13052              : {
   13053           14 :   gfc_se argse;
   13054           14 :   tree tmp, atom, old, new_val, comp, stat = NULL_TREE;
   13055           14 :   stmtblock_t block, post_block;
   13056           14 :   built_in_function fn;
   13057           14 :   gfc_expr *atom_expr = code->ext.actual->expr;
   13058              : 
   13059           14 :   if (atom_expr->expr_type == EXPR_FUNCTION
   13060            0 :       && atom_expr->value.function.isym
   13061            0 :       && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
   13062            0 :     atom_expr = atom_expr->value.function.actual->expr;
   13063              : 
   13064           14 :   gfc_init_block (&block);
   13065           14 :   gfc_init_block (&post_block);
   13066           14 :   gfc_init_se (&argse, NULL);
   13067           14 :   argse.want_pointer = 1;
   13068           14 :   gfc_conv_expr (&argse, atom_expr);
   13069           14 :   atom = argse.expr;
   13070              : 
   13071           14 :   gfc_init_se (&argse, NULL);
   13072           14 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   13073            8 :     argse.want_pointer = 1;
   13074           14 :   gfc_conv_expr (&argse, code->ext.actual->next->expr);
   13075           14 :   gfc_add_block_to_block (&block, &argse.pre);
   13076           14 :   gfc_add_block_to_block (&post_block, &argse.post);
   13077           14 :   old = argse.expr;
   13078              : 
   13079           14 :   gfc_init_se (&argse, NULL);
   13080           14 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   13081            8 :     argse.want_pointer = 1;
   13082           14 :   gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
   13083           14 :   gfc_add_block_to_block (&block, &argse.pre);
   13084           14 :   gfc_add_block_to_block (&post_block, &argse.post);
   13085           14 :   comp = argse.expr;
   13086              : 
   13087           14 :   gfc_init_se (&argse, NULL);
   13088           14 :   if (flag_coarray == GFC_FCOARRAY_LIB
   13089            8 :       && code->ext.actual->next->next->next->expr->ts.kind
   13090            8 :          == atom_expr->ts.kind)
   13091            8 :     argse.want_pointer = 1;
   13092           14 :   gfc_conv_expr (&argse, code->ext.actual->next->next->next->expr);
   13093           14 :   gfc_add_block_to_block (&block, &argse.pre);
   13094           14 :   gfc_add_block_to_block (&post_block, &argse.post);
   13095           14 :   new_val = argse.expr;
   13096              : 
   13097              :   /* STAT=  */
   13098           14 :   if (code->ext.actual->next->next->next->next->expr != NULL)
   13099              :     {
   13100           14 :       gcc_assert (code->ext.actual->next->next->next->next->expr->expr_type
   13101              :                   == EXPR_VARIABLE);
   13102           14 :       gfc_init_se (&argse, NULL);
   13103           14 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   13104            8 :         argse.want_pointer = 1;
   13105           14 :       gfc_conv_expr_val (&argse,
   13106           14 :                          code->ext.actual->next->next->next->next->expr);
   13107           14 :       gfc_add_block_to_block (&block, &argse.pre);
   13108           14 :       gfc_add_block_to_block (&post_block, &argse.post);
   13109           14 :       stat = argse.expr;
   13110              :     }
   13111            0 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
   13112            0 :     stat = null_pointer_node;
   13113              : 
   13114           14 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   13115              :     {
   13116            8 :       tree image_index, caf_decl, offset, token;
   13117              : 
   13118            8 :       caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
   13119            8 :       if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
   13120            0 :         caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
   13121              : 
   13122            8 :       if (gfc_is_coindexed (atom_expr))
   13123            8 :         image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
   13124              :       else
   13125            0 :         image_index = integer_zero_node;
   13126              : 
   13127            8 :       if (TREE_TYPE (TREE_TYPE (new_val)) != TREE_TYPE (TREE_TYPE (old)))
   13128              :         {
   13129            0 :           tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "new");
   13130            0 :           gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), new_val));
   13131            0 :           new_val = gfc_build_addr_expr (NULL_TREE, tmp);
   13132              :         }
   13133              : 
   13134            8 :       gfc_init_se (&argse, NULL);
   13135            8 :       gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
   13136              :                                 atom_expr);
   13137            8 :       gfc_add_block_to_block (&block, &argse.pre);
   13138              : 
   13139            8 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_cas, 9,
   13140              :                                  token, offset, image_index, old, comp, new_val,
   13141              :                                  stat, build_int_cst (integer_type_node,
   13142            8 :                                                       (int) atom_expr->ts.type),
   13143              :                                  build_int_cst (integer_type_node,
   13144            8 :                                                 (int) atom_expr->ts.kind));
   13145            8 :       gfc_add_expr_to_block (&block, tmp);
   13146            8 :       gfc_add_block_to_block (&block, &argse.post);
   13147            8 :       gfc_add_block_to_block (&block, &post_block);
   13148            8 :       return gfc_finish_block (&block);
   13149              :     }
   13150              : 
   13151            6 :   tmp = TREE_TYPE (TREE_TYPE (atom));
   13152           12 :   fn = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
   13153            6 :                             + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
   13154            6 :                             + 1);
   13155            6 :   tmp = builtin_decl_explicit (fn);
   13156              : 
   13157            6 :   gfc_add_modify (&block, old, comp);
   13158           12 :   tmp = build_call_expr_loc (input_location, tmp, 6, atom,
   13159              :                              gfc_build_addr_expr (NULL, old),
   13160            6 :                              fold_convert (TREE_TYPE (old), new_val),
   13161              :                              boolean_false_node,
   13162              :                              build_int_cst (integer_type_node, MEMMODEL_RELAXED),
   13163              :                              build_int_cst (integer_type_node, MEMMODEL_RELAXED));
   13164            6 :   gfc_add_expr_to_block (&block, tmp);
   13165              : 
   13166            6 :   if (stat != NULL_TREE)
   13167            6 :     gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
   13168            6 :   gfc_add_block_to_block (&block, &post_block);
   13169            6 :   return gfc_finish_block (&block);
   13170              : }
   13171              : 
   13172              : static tree
   13173          105 : conv_intrinsic_event_query (gfc_code *code)
   13174              : {
   13175          105 :   gfc_se se, argse;
   13176          105 :   tree stat = NULL_TREE, stat2 = NULL_TREE;
   13177          105 :   tree count = NULL_TREE, count2 = NULL_TREE;
   13178              : 
   13179          105 :   gfc_expr *event_expr = code->ext.actual->expr;
   13180              : 
   13181          105 :   if (code->ext.actual->next->next->expr)
   13182              :     {
   13183           18 :       gcc_assert (code->ext.actual->next->next->expr->expr_type
   13184              :                   == EXPR_VARIABLE);
   13185           18 :       gfc_init_se (&argse, NULL);
   13186           18 :       gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
   13187           18 :       stat = argse.expr;
   13188              :     }
   13189           87 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
   13190           58 :     stat = null_pointer_node;
   13191              : 
   13192          105 :   if (code->ext.actual->next->expr)
   13193              :     {
   13194          105 :       gcc_assert (code->ext.actual->next->expr->expr_type == EXPR_VARIABLE);
   13195          105 :       gfc_init_se (&argse, NULL);
   13196          105 :       gfc_conv_expr_val (&argse, code->ext.actual->next->expr);
   13197          105 :       count = argse.expr;
   13198              :     }
   13199              : 
   13200          105 :   gfc_start_block (&se.pre);
   13201          105 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   13202              :     {
   13203           70 :       tree tmp, token, image_index;
   13204           70 :       tree index = build_zero_cst (gfc_array_index_type);
   13205              : 
   13206           70 :       if (event_expr->expr_type == EXPR_FUNCTION
   13207            0 :           && event_expr->value.function.isym
   13208            0 :           && event_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
   13209            0 :         event_expr = event_expr->value.function.actual->expr;
   13210              : 
   13211           70 :       tree caf_decl = gfc_get_tree_for_caf_expr (event_expr);
   13212              : 
   13213           70 :       if (event_expr->symtree->n.sym->ts.type != BT_DERIVED
   13214           70 :           || event_expr->symtree->n.sym->ts.u.derived->from_intmod
   13215              :              != INTMOD_ISO_FORTRAN_ENV
   13216           70 :           || event_expr->symtree->n.sym->ts.u.derived->intmod_sym_id
   13217              :              != ISOFORTRAN_EVENT_TYPE)
   13218              :         {
   13219            0 :           gfc_error ("Sorry, the event component of derived type at %L is not "
   13220              :                      "yet supported", &event_expr->where);
   13221            0 :           return NULL_TREE;
   13222              :         }
   13223              : 
   13224           70 :       if (gfc_is_coindexed (event_expr))
   13225              :         {
   13226            0 :           gfc_error ("The event variable at %L shall not be coindexed",
   13227              :                      &event_expr->where);
   13228            0 :           return NULL_TREE;
   13229              :         }
   13230              : 
   13231           70 :       image_index = integer_zero_node;
   13232              : 
   13233           70 :       gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
   13234              :                                 event_expr);
   13235              : 
   13236              :       /* For arrays, obtain the array index.  */
   13237           70 :       if (gfc_expr_attr (event_expr).dimension)
   13238              :         {
   13239           52 :           tree desc, tmp, extent, lbound, ubound;
   13240           52 :           gfc_array_ref *ar, ar2;
   13241           52 :           int i;
   13242              : 
   13243              :           /* TODO: Extend this, once DT components are supported.  */
   13244           52 :           ar = &event_expr->ref->u.ar;
   13245           52 :           ar2 = *ar;
   13246           52 :           memset (ar, '\0', sizeof (*ar));
   13247           52 :           ar->as = ar2.as;
   13248           52 :           ar->type = AR_FULL;
   13249              : 
   13250           52 :           gfc_init_se (&argse, NULL);
   13251           52 :           argse.descriptor_only = 1;
   13252           52 :           gfc_conv_expr_descriptor (&argse, event_expr);
   13253           52 :           gfc_add_block_to_block (&se.pre, &argse.pre);
   13254           52 :           desc = argse.expr;
   13255           52 :           *ar = ar2;
   13256              : 
   13257           52 :           extent = build_one_cst (gfc_array_index_type);
   13258          156 :           for (i = 0; i < ar->dimen; i++)
   13259              :             {
   13260           52 :               gfc_init_se (&argse, NULL);
   13261           52 :               gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
   13262           52 :               gfc_add_block_to_block (&argse.pre, &argse.pre);
   13263           52 :               lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
   13264           52 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
   13265           52 :                                      TREE_TYPE (lbound), argse.expr, lbound);
   13266           52 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
   13267           52 :                                      TREE_TYPE (tmp), extent, tmp);
   13268           52 :               index = fold_build2_loc (input_location, PLUS_EXPR,
   13269           52 :                                        TREE_TYPE (tmp), index, tmp);
   13270           52 :               if (i < ar->dimen - 1)
   13271              :                 {
   13272            0 :                   ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
   13273            0 :                   tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
   13274            0 :                   extent = fold_build2_loc (input_location, MULT_EXPR,
   13275            0 :                                             TREE_TYPE (tmp), extent, tmp);
   13276              :                 }
   13277              :             }
   13278              :         }
   13279              : 
   13280           70 :       if (count != null_pointer_node && TREE_TYPE (count) != integer_type_node)
   13281              :         {
   13282            0 :           count2 = count;
   13283            0 :           count = gfc_create_var (integer_type_node, "count");
   13284              :         }
   13285              : 
   13286           70 :       if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
   13287              :         {
   13288            0 :           stat2 = stat;
   13289            0 :           stat = gfc_create_var (integer_type_node, "stat");
   13290              :         }
   13291              : 
   13292           70 :       index = fold_convert (size_type_node, index);
   13293          140 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_query, 5,
   13294              :                                    token, index, image_index, count
   13295           70 :                                    ? gfc_build_addr_expr (NULL, count) : count,
   13296           70 :                                    stat != null_pointer_node
   13297           12 :                                    ? gfc_build_addr_expr (NULL, stat) : stat);
   13298           70 :       gfc_add_expr_to_block (&se.pre, tmp);
   13299              : 
   13300           70 :       if (count2 != NULL_TREE)
   13301            0 :         gfc_add_modify (&se.pre, count2,
   13302            0 :                         fold_convert (TREE_TYPE (count2), count));
   13303              : 
   13304           70 :       if (stat2 != NULL_TREE)
   13305            0 :         gfc_add_modify (&se.pre, stat2,
   13306            0 :                         fold_convert (TREE_TYPE (stat2), stat));
   13307              : 
   13308           70 :       return gfc_finish_block (&se.pre);
   13309              :     }
   13310              : 
   13311           35 :   gfc_init_se (&argse, NULL);
   13312           35 :   gfc_conv_expr_val (&argse, code->ext.actual->expr);
   13313           35 :   gfc_add_modify (&se.pre, count, fold_convert (TREE_TYPE (count), argse.expr));
   13314              : 
   13315           35 :   if (stat != NULL_TREE)
   13316            6 :     gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
   13317              : 
   13318           35 :   return gfc_finish_block (&se.pre);
   13319              : }
   13320              : 
   13321              : 
   13322              : /* This is a peculiar case because of the need to do dependency checking.
   13323              :    It is called via trans-stmt.cc(gfc_trans_call), where it is picked out as
   13324              :    a special case and this function called instead of
   13325              :    gfc_conv_procedure_call.  */
   13326              : void
   13327          197 : gfc_conv_intrinsic_mvbits (gfc_se *se, gfc_actual_arglist *actual_args,
   13328              :                            gfc_loopinfo *loop)
   13329              : {
   13330          197 :   gfc_actual_arglist *actual;
   13331          197 :   gfc_se argse[5];
   13332          197 :   gfc_expr *arg[5];
   13333          197 :   gfc_ss *lss;
   13334          197 :   int n;
   13335              : 
   13336          197 :   tree from, frompos, len, to, topos;
   13337          197 :   tree lenmask, oldbits, newbits, bitsize;
   13338          197 :   tree type, utype, above, mask1, mask2;
   13339              : 
   13340          197 :   if (loop)
   13341           67 :     lss = loop->ss;
   13342              :   else
   13343          130 :     lss = gfc_ss_terminator;
   13344              : 
   13345          197 :   actual = actual_args;
   13346         1182 :   for (n = 0; n < 5; n++, actual = actual->next)
   13347              :     {
   13348          985 :       arg[n] = actual->expr;
   13349          985 :       gfc_init_se (&argse[n], NULL);
   13350              : 
   13351          985 :       if (lss != gfc_ss_terminator)
   13352              :         {
   13353          335 :           gfc_copy_loopinfo_to_se (&argse[n], loop);
   13354              :           /* Find the ss for the expression if it is there.  */
   13355          335 :           argse[n].ss = lss;
   13356          335 :           gfc_mark_ss_chain_used (lss, 1);
   13357              :         }
   13358              : 
   13359          985 :       gfc_conv_expr (&argse[n], arg[n]);
   13360              : 
   13361          985 :       if (loop)
   13362          335 :         lss = argse[n].ss;
   13363              :     }
   13364              : 
   13365          197 :   from    = argse[0].expr;
   13366          197 :   frompos = argse[1].expr;
   13367          197 :   len     = argse[2].expr;
   13368          197 :   to      = argse[3].expr;
   13369          197 :   topos   = argse[4].expr;
   13370              : 
   13371              :   /* The type of the result (TO).  */
   13372          197 :   type    = TREE_TYPE (to);
   13373          197 :   bitsize = build_int_cst (integer_type_node, TYPE_PRECISION (type));
   13374              : 
   13375              :   /* Optionally generate code for runtime argument check.  */
   13376          197 :   if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
   13377              :     {
   13378           18 :       tree nbits, below, ccond;
   13379           18 :       tree fp = fold_convert (long_integer_type_node, frompos);
   13380           18 :       tree ln = fold_convert (long_integer_type_node, len);
   13381           18 :       tree tp = fold_convert (long_integer_type_node, topos);
   13382           18 :       below = fold_build2_loc (input_location, LT_EXPR,
   13383              :                                logical_type_node, frompos,
   13384           18 :                                build_int_cst (TREE_TYPE (frompos), 0));
   13385           18 :       above = fold_build2_loc (input_location, GT_EXPR,
   13386              :                                logical_type_node, frompos,
   13387           18 :                                fold_convert (TREE_TYPE (frompos), bitsize));
   13388           18 :       ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
   13389              :                                logical_type_node, below, above);
   13390           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
   13391           18 :                                &arg[1]->where,
   13392              :                                "FROMPOS argument (%ld) out of range 0:%d "
   13393              :                                "in intrinsic MVBITS", fp, bitsize);
   13394           18 :       below = fold_build2_loc (input_location, LT_EXPR,
   13395              :                                logical_type_node, len,
   13396           18 :                                build_int_cst (TREE_TYPE (len), 0));
   13397           18 :       above = fold_build2_loc (input_location, GT_EXPR,
   13398              :                                logical_type_node, len,
   13399           18 :                                fold_convert (TREE_TYPE (len), bitsize));
   13400           18 :       ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
   13401              :                                logical_type_node, below, above);
   13402           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[2].pre,
   13403           18 :                                &arg[2]->where,
   13404              :                                "LEN argument (%ld) out of range 0:%d "
   13405              :                                "in intrinsic MVBITS", ln, bitsize);
   13406           18 :       below = fold_build2_loc (input_location, LT_EXPR,
   13407              :                                logical_type_node, topos,
   13408           18 :                                build_int_cst (TREE_TYPE (topos), 0));
   13409           18 :       above = fold_build2_loc (input_location, GT_EXPR,
   13410              :                                logical_type_node, topos,
   13411           18 :                                fold_convert (TREE_TYPE (topos), bitsize));
   13412           18 :       ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
   13413              :                                logical_type_node, below, above);
   13414           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
   13415           18 :                                &arg[4]->where,
   13416              :                                "TOPOS argument (%ld) out of range 0:%d "
   13417              :                                "in intrinsic MVBITS", tp, bitsize);
   13418              : 
   13419              :       /* The tests above ensure that FROMPOS, LEN and TOPOS fit into short
   13420              :          integers.  Additions below cannot overflow.  */
   13421           18 :       nbits = fold_convert (long_integer_type_node, bitsize);
   13422           18 :       above = fold_build2_loc (input_location, PLUS_EXPR,
   13423              :                                long_integer_type_node, fp, ln);
   13424           18 :       ccond = fold_build2_loc (input_location, GT_EXPR,
   13425              :                                logical_type_node, above, nbits);
   13426           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
   13427              :                                &arg[1]->where,
   13428              :                                "FROMPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
   13429              :                                "in intrinsic MVBITS", fp, ln, bitsize);
   13430           18 :       above = fold_build2_loc (input_location, PLUS_EXPR,
   13431              :                                long_integer_type_node, tp, ln);
   13432           18 :       ccond = fold_build2_loc (input_location, GT_EXPR,
   13433              :                                logical_type_node, above, nbits);
   13434           18 :       gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
   13435              :                                &arg[4]->where,
   13436              :                                "TOPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
   13437              :                                "in intrinsic MVBITS", tp, ln, bitsize);
   13438              :     }
   13439              : 
   13440         1182 :   for (n = 0; n < 5; n++)
   13441              :     {
   13442          985 :       gfc_add_block_to_block (&se->pre, &argse[n].pre);
   13443          985 :       gfc_add_block_to_block (&se->post, &argse[n].post);
   13444              :     }
   13445              : 
   13446              :   /* lenmask = (LEN >= bit_size (TYPE)) ? ~(TYPE)0 : ((TYPE)1 << LEN) - 1  */
   13447          197 :   above = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   13448          197 :                            len, fold_convert (TREE_TYPE (len), bitsize));
   13449          197 :   mask1 = build_int_cst (type, -1);
   13450          197 :   mask2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
   13451              :                            build_int_cst (type, 1), len);
   13452          197 :   mask2 = fold_build2_loc (input_location, MINUS_EXPR, type,
   13453              :                            mask2, build_int_cst (type, 1));
   13454          197 :   lenmask = fold_build3_loc (input_location, COND_EXPR, type,
   13455              :                              above, mask1, mask2);
   13456              : 
   13457              :   /* newbits = (((UTYPE)(FROM) >> FROMPOS) & lenmask) << TOPOS.
   13458              :    * For valid frompos+len <= bit_size(FROM) the conversion to unsigned is
   13459              :    * not strictly necessary; artificial bits from rshift will be masked.  */
   13460          197 :   utype = unsigned_type_for (type);
   13461          197 :   newbits = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
   13462              :                              fold_convert (utype, from), frompos);
   13463          197 :   newbits = fold_build2_loc (input_location, BIT_AND_EXPR, type,
   13464              :                              fold_convert (type, newbits), lenmask);
   13465          197 :   newbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
   13466              :                              newbits, topos);
   13467              : 
   13468              :   /* oldbits = TO & (~(lenmask << TOPOS)).  */
   13469          197 :   oldbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
   13470              :                              lenmask, topos);
   13471          197 :   oldbits = fold_build1_loc (input_location, BIT_NOT_EXPR, type, oldbits);
   13472          197 :   oldbits = fold_build2_loc (input_location, BIT_AND_EXPR, type, oldbits, to);
   13473              : 
   13474              :   /* TO = newbits | oldbits.  */
   13475          197 :   se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
   13476              :                               oldbits, newbits);
   13477              : 
   13478              :   /* Return the assignment.  */
   13479          197 :   se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
   13480              :                               void_type_node, to, se->expr);
   13481          197 : }
   13482              : 
   13483              : /* Comes from trans-stmt.cc, but we don't want the whole header included.  */
   13484              : extern void gfc_trans_sync_stat (struct sync_stat *sync_stat, gfc_se *se,
   13485              :                                  tree *stat, tree *errmsg, tree *errmsg_len);
   13486              : 
   13487              : static tree
   13488          269 : conv_intrinsic_move_alloc (gfc_code *code)
   13489              : {
   13490          269 :   stmtblock_t block;
   13491          269 :   gfc_expr *from_expr, *to_expr;
   13492          269 :   gfc_se from_se, to_se;
   13493          269 :   tree tmp, to_tree, from_tree, stat, errmsg, errmsg_len, fin_label = NULL_TREE;
   13494          269 :   bool coarray, from_is_class, from_is_scalar;
   13495          269 :   gfc_actual_arglist *arg = code->ext.actual;
   13496          269 :   sync_stat tmp_sync_stat = {nullptr, nullptr};
   13497              : 
   13498          269 :   gfc_start_block (&block);
   13499              : 
   13500          269 :   from_expr = arg->expr;
   13501          269 :   arg = arg->next;
   13502          269 :   to_expr = arg->expr;
   13503          269 :   arg = arg->next;
   13504              : 
   13505          807 :   while (arg)
   13506              :     {
   13507          538 :       if (arg->expr)
   13508              :         {
   13509            0 :           if (!strcmp ("stat", arg->name))
   13510            0 :             tmp_sync_stat.stat = arg->expr;
   13511            0 :           else if (!strcmp ("errmsg", arg->name))
   13512            0 :             tmp_sync_stat.errmsg = arg->expr;
   13513              :         }
   13514          538 :       arg = arg->next;
   13515              :     }
   13516              : 
   13517          269 :   gfc_init_se (&from_se, NULL);
   13518          269 :   gfc_init_se (&to_se, NULL);
   13519              : 
   13520          269 :   gfc_trans_sync_stat (&tmp_sync_stat, &from_se, &stat, &errmsg, &errmsg_len);
   13521          269 :   if (stat != null_pointer_node)
   13522            0 :     fin_label = gfc_build_label_decl (NULL_TREE);
   13523              : 
   13524          269 :   gcc_assert (from_expr->ts.type != BT_CLASS || to_expr->ts.type == BT_CLASS);
   13525          269 :   coarray = from_expr->corank != 0;
   13526              : 
   13527          269 :   from_is_class = from_expr->ts.type == BT_CLASS;
   13528          269 :   from_is_scalar = from_expr->rank == 0 && !coarray;
   13529          269 :   if (to_expr->ts.type == BT_CLASS || from_is_scalar)
   13530              :     {
   13531          169 :       from_se.want_pointer = 1;
   13532          169 :       if (from_is_scalar)
   13533          121 :         gfc_conv_expr (&from_se, from_expr);
   13534              :       else
   13535           48 :         gfc_conv_expr_descriptor (&from_se, from_expr);
   13536          169 :       if (from_is_class)
   13537           64 :         from_tree = gfc_class_data_get (from_se.expr);
   13538              :       else
   13539              :         {
   13540          105 :           gfc_symbol *vtab;
   13541          105 :           from_tree = from_se.expr;
   13542              : 
   13543          105 :           if (to_expr->ts.type == BT_CLASS)
   13544              :             {
   13545           42 :               vtab = gfc_find_vtab (&from_expr->ts);
   13546           42 :               gcc_assert (vtab);
   13547           42 :               from_se.expr = gfc_get_symbol_decl (vtab);
   13548              :             }
   13549              :         }
   13550          169 :       gfc_add_block_to_block (&block, &from_se.pre);
   13551              : 
   13552          169 :       to_se.want_pointer = 1;
   13553          169 :       if (to_expr->rank == 0)
   13554          121 :         gfc_conv_expr (&to_se, to_expr);
   13555              :       else
   13556           48 :         gfc_conv_expr_descriptor (&to_se, to_expr);
   13557          169 :       if (to_expr->ts.type == BT_CLASS)
   13558          106 :         to_tree = gfc_class_data_get (to_se.expr);
   13559              :       else
   13560           63 :         to_tree = to_se.expr;
   13561          169 :       gfc_add_block_to_block (&block, &to_se.pre);
   13562              : 
   13563              :       /* Deallocate "to".  */
   13564          169 :       if (to_expr->rank == 0)
   13565              :         {
   13566          121 :           tmp = gfc_deallocate_scalar_with_status (to_tree, stat, fin_label,
   13567              :                                                    true, to_expr, to_expr->ts,
   13568              :                                                    NULL_TREE, false, true,
   13569              :                                                    errmsg, errmsg_len);
   13570          121 :           gfc_add_expr_to_block (&block, tmp);
   13571              :         }
   13572              : 
   13573          169 :       if (from_is_scalar)
   13574              :         {
   13575              :           /* Assign (_data) pointers.  */
   13576          121 :           gfc_add_modify_loc (input_location, &block, to_tree,
   13577          121 :                               fold_convert (TREE_TYPE (to_tree), from_tree));
   13578              : 
   13579              :           /* Set "from" to NULL.  */
   13580          121 :           gfc_add_modify_loc (input_location, &block, from_tree,
   13581          121 :                               fold_convert (TREE_TYPE (from_tree),
   13582              :                                             null_pointer_node));
   13583              : 
   13584          121 :           gfc_add_block_to_block (&block, &from_se.post);
   13585              :         }
   13586          169 :       gfc_add_block_to_block (&block, &to_se.post);
   13587              : 
   13588              :       /* Set _vptr.  */
   13589          169 :       if (to_expr->ts.type == BT_CLASS)
   13590              :         {
   13591          106 :           gfc_class_set_vptr (&block, to_se.expr, from_se.expr);
   13592          106 :           if (from_is_class)
   13593           64 :             gfc_reset_vptr (&block, from_expr);
   13594          106 :           if (UNLIMITED_POLY (to_expr))
   13595              :             {
   13596           20 :               tree to_len = gfc_class_len_get (to_se.class_container);
   13597           20 :               tmp = from_expr->ts.type == BT_CHARACTER && from_se.string_length
   13598           20 :                       ? from_se.string_length
   13599              :                       : size_zero_node;
   13600           20 :               gfc_add_modify_loc (input_location, &block, to_len,
   13601           20 :                                   fold_convert (TREE_TYPE (to_len), tmp));
   13602              :             }
   13603              :         }
   13604              : 
   13605          169 :       if (from_is_scalar)
   13606              :         {
   13607          121 :           if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
   13608              :             {
   13609            6 :               gfc_add_modify_loc (input_location, &block, to_se.string_length,
   13610            6 :                                   fold_convert (TREE_TYPE (to_se.string_length),
   13611              :                                                 from_se.string_length));
   13612            6 :               if (from_expr->ts.deferred)
   13613            6 :                 gfc_add_modify_loc (
   13614              :                   input_location, &block, from_se.string_length,
   13615            6 :                   build_int_cst (TREE_TYPE (from_se.string_length), 0));
   13616              :             }
   13617          121 :           if (UNLIMITED_POLY (from_expr))
   13618            2 :             gfc_reset_len (&block, from_expr);
   13619              : 
   13620          121 :           return gfc_finish_block (&block);
   13621              :         }
   13622              : 
   13623           48 :       gfc_init_se (&to_se, NULL);
   13624           48 :       gfc_init_se (&from_se, NULL);
   13625              :     }
   13626              : 
   13627              :   /* Deallocate "to".  */
   13628          148 :   if (from_expr->rank == 0)
   13629              :     {
   13630            4 :       to_se.want_coarray = 1;
   13631            4 :       from_se.want_coarray = 1;
   13632              :     }
   13633          148 :   gfc_conv_expr_descriptor (&to_se, to_expr);
   13634          148 :   gfc_conv_expr_descriptor (&from_se, from_expr);
   13635          148 :   gfc_add_block_to_block (&block, &to_se.pre);
   13636          148 :   gfc_add_block_to_block (&block, &from_se.pre);
   13637              : 
   13638              :   /* For coarrays, call SYNC ALL if TO is already deallocated as MOVE_ALLOC
   13639              :      is an image control "statement", cf. IR F08/0040 in 12-006A.  */
   13640          148 :   if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
   13641              :     {
   13642            6 :       tree cond;
   13643              : 
   13644            6 :       tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
   13645              :                                         fin_label, true, to_expr,
   13646              :                                         GFC_CAF_COARRAY_DEALLOCATE_ONLY,
   13647              :                                         NULL_TREE, NULL_TREE,
   13648              :                                         gfc_conv_descriptor_token (to_se.expr),
   13649              :                                         true);
   13650            6 :       gfc_add_expr_to_block (&block, tmp);
   13651              : 
   13652            6 :       tmp = gfc_conv_descriptor_data_get (to_se.expr);
   13653            6 :       cond = fold_build2_loc (input_location, EQ_EXPR,
   13654              :                               logical_type_node, tmp,
   13655            6 :                               fold_convert (TREE_TYPE (tmp),
   13656              :                                             null_pointer_node));
   13657            6 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
   13658              :                                  3, null_pointer_node, null_pointer_node,
   13659              :                                  integer_zero_node);
   13660              : 
   13661            6 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
   13662              :                              tmp, build_empty_stmt (input_location));
   13663            6 :       gfc_add_expr_to_block (&block, tmp);
   13664            6 :     }
   13665              :   else
   13666              :     {
   13667          142 :       if (to_expr->ts.type == BT_DERIVED
   13668           25 :           && to_expr->ts.u.derived->attr.alloc_comp)
   13669              :         {
   13670           19 :           tmp = gfc_deallocate_alloc_comp (to_expr->ts.u.derived,
   13671              :                                            to_se.expr, to_expr->rank);
   13672           19 :           gfc_add_expr_to_block (&block, tmp);
   13673              :         }
   13674              : 
   13675          142 :       tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
   13676              :                                         fin_label, true, to_expr,
   13677              :                                         GFC_CAF_COARRAY_NOCOARRAY, NULL_TREE,
   13678              :                                         NULL_TREE, NULL_TREE, true);
   13679          142 :       gfc_add_expr_to_block (&block, tmp);
   13680              :     }
   13681              : 
   13682              :   /* Copy the array descriptor data.  */
   13683          148 :   gfc_add_modify_loc (input_location, &block, to_se.expr, from_se.expr);
   13684              : 
   13685              :   /* Set "from" to NULL.  */
   13686          148 :   gfc_conv_descriptor_data_set (&block, from_se.expr, null_pointer_node);
   13687              : 
   13688          148 :   if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
   13689              :     {
   13690              :       /* Copy the array descriptor data has overwritten the to-token and cleared
   13691              :          from.data.  Now also clear the from.token.  */
   13692            6 :       gfc_conv_descriptor_token_set (&block, from_se.expr, null_pointer_node);
   13693              :     }
   13694              : 
   13695          148 :   if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
   13696              :     {
   13697            7 :       gfc_add_modify_loc (input_location, &block, to_se.string_length,
   13698            7 :                           fold_convert (TREE_TYPE (to_se.string_length),
   13699              :                                         from_se.string_length));
   13700            7 :       if (from_expr->ts.deferred)
   13701            6 :         gfc_add_modify_loc (input_location, &block, from_se.string_length,
   13702            6 :                         build_int_cst (TREE_TYPE (from_se.string_length), 0));
   13703              :     }
   13704          148 :   if (fin_label)
   13705            0 :     gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, fin_label));
   13706              : 
   13707          148 :   gfc_add_block_to_block (&block, &to_se.post);
   13708          148 :   gfc_add_block_to_block (&block, &from_se.post);
   13709              : 
   13710          148 :   return gfc_finish_block (&block);
   13711              : }
   13712              : 
   13713              : 
   13714              : tree
   13715         7165 : gfc_conv_intrinsic_subroutine (gfc_code *code)
   13716              : {
   13717         7165 :   tree res;
   13718              : 
   13719         7165 :   gcc_assert (code->resolved_isym);
   13720              : 
   13721         7165 :   switch (code->resolved_isym->id)
   13722              :     {
   13723          269 :     case GFC_ISYM_MOVE_ALLOC:
   13724          269 :       res = conv_intrinsic_move_alloc (code);
   13725          269 :       break;
   13726              : 
   13727           14 :     case GFC_ISYM_ATOMIC_CAS:
   13728           14 :       res = conv_intrinsic_atomic_cas (code);
   13729           14 :       break;
   13730              : 
   13731          101 :     case GFC_ISYM_ATOMIC_ADD:
   13732          101 :     case GFC_ISYM_ATOMIC_AND:
   13733          101 :     case GFC_ISYM_ATOMIC_DEF:
   13734          101 :     case GFC_ISYM_ATOMIC_OR:
   13735          101 :     case GFC_ISYM_ATOMIC_XOR:
   13736          101 :     case GFC_ISYM_ATOMIC_FETCH_ADD:
   13737          101 :     case GFC_ISYM_ATOMIC_FETCH_AND:
   13738          101 :     case GFC_ISYM_ATOMIC_FETCH_OR:
   13739          101 :     case GFC_ISYM_ATOMIC_FETCH_XOR:
   13740          101 :       res = conv_intrinsic_atomic_op (code);
   13741          101 :       break;
   13742              : 
   13743          182 :     case GFC_ISYM_ATOMIC_REF:
   13744          182 :       res = conv_intrinsic_atomic_ref (code);
   13745          182 :       break;
   13746              : 
   13747          105 :     case GFC_ISYM_EVENT_QUERY:
   13748          105 :       res = conv_intrinsic_event_query (code);
   13749          105 :       break;
   13750              : 
   13751         3501 :     case GFC_ISYM_C_F_POINTER:
   13752         3501 :     case GFC_ISYM_C_F_PROCPOINTER:
   13753         3501 :       res = conv_isocbinding_subroutine (code);
   13754         3501 :       break;
   13755              : 
   13756           60 :     case GFC_ISYM_C_F_STRPOINTER:
   13757           60 :       res = conv_isocbinding_subroutine_strpointer (code);
   13758           60 :       break;
   13759              : 
   13760          360 :     case GFC_ISYM_CAF_SEND:
   13761          360 :       res = conv_caf_send_to_remote (code);
   13762          360 :       break;
   13763              : 
   13764          140 :     case GFC_ISYM_CAF_SENDGET:
   13765          140 :       res = conv_caf_sendget (code);
   13766          140 :       break;
   13767              : 
   13768          112 :     case GFC_ISYM_CO_BROADCAST:
   13769          112 :     case GFC_ISYM_CO_MIN:
   13770          112 :     case GFC_ISYM_CO_MAX:
   13771          112 :     case GFC_ISYM_CO_REDUCE:
   13772          112 :     case GFC_ISYM_CO_SUM:
   13773          112 :       res = conv_co_collective (code);
   13774          112 :       break;
   13775              : 
   13776           10 :     case GFC_ISYM_FREE:
   13777           10 :       res = conv_intrinsic_free (code);
   13778           10 :       break;
   13779              : 
   13780           55 :     case GFC_ISYM_FSTAT:
   13781           55 :     case GFC_ISYM_LSTAT:
   13782           55 :     case GFC_ISYM_STAT:
   13783           55 :       res = conv_intrinsic_fstat_lstat_stat_sub (code);
   13784           55 :       break;
   13785              : 
   13786           90 :     case GFC_ISYM_RANDOM_INIT:
   13787           90 :       res = conv_intrinsic_random_init (code);
   13788           90 :       break;
   13789              : 
   13790           15 :     case GFC_ISYM_KILL:
   13791           15 :       res = conv_intrinsic_kill_sub (code);
   13792           15 :       break;
   13793              : 
   13794              :     case GFC_ISYM_MVBITS:
   13795              :       res = NULL_TREE;
   13796              :       break;
   13797              : 
   13798          196 :     case GFC_ISYM_SYSTEM_CLOCK:
   13799          196 :       res = conv_intrinsic_system_clock (code);
   13800          196 :       break;
   13801              : 
   13802          102 :     case GFC_ISYM_SPLIT:
   13803          102 :       res = conv_intrinsic_split (code);
   13804          102 :       break;
   13805              : 
   13806              :     default:
   13807              :       res = NULL_TREE;
   13808              :       break;
   13809              :     }
   13810              : 
   13811         7165 :   return res;
   13812              : }
   13813              : 
   13814              : #include "gt-fortran-trans-intrinsic.h"
        

Generated by: LCOV version 2.5.0-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.