LCOV - code coverage report
Current view: top level - gcc/fortran - trans-stmt.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.7 % 3648 3491
Test Date: 2024-04-13 14:00:49 Functions: 95.7 % 70 67
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Statement translation -- generate GCC trees from gfc_code.
       2                 :             :    Copyright (C) 2002-2024 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                 :             : 
      23                 :             : #include "config.h"
      24                 :             : #include "system.h"
      25                 :             : #include "coretypes.h"
      26                 :             : #include "options.h"
      27                 :             : #include "tree.h"
      28                 :             : #include "gfortran.h"
      29                 :             : #include "trans.h"
      30                 :             : #include "stringpool.h"
      31                 :             : #include "fold-const.h"
      32                 :             : #include "trans-stmt.h"
      33                 :             : #include "trans-types.h"
      34                 :             : #include "trans-array.h"
      35                 :             : #include "trans-const.h"
      36                 :             : #include "dependency.h"
      37                 :             : 
      38                 :             : typedef struct iter_info
      39                 :             : {
      40                 :             :   tree var;
      41                 :             :   tree start;
      42                 :             :   tree end;
      43                 :             :   tree step;
      44                 :             :   gfc_loop_annot annot;
      45                 :             :   struct iter_info *next;
      46                 :             : }
      47                 :             : iter_info;
      48                 :             : 
      49                 :             : typedef struct forall_info
      50                 :             : {
      51                 :             :   iter_info *this_loop;
      52                 :             :   tree mask;
      53                 :             :   tree maskindex;
      54                 :             :   int nvar;
      55                 :             :   tree size;
      56                 :             :   struct forall_info  *prev_nest;
      57                 :             :   bool do_concurrent;
      58                 :             : }
      59                 :             : forall_info;
      60                 :             : 
      61                 :             : static void gfc_trans_where_2 (gfc_code *, tree, bool,
      62                 :             :                                forall_info *, stmtblock_t *);
      63                 :             : 
      64                 :             : /* Translate a F95 label number to a LABEL_EXPR.  */
      65                 :             : 
      66                 :             : tree
      67                 :        3496 : gfc_trans_label_here (gfc_code * code)
      68                 :             : {
      69                 :        3496 :   return build1_v (LABEL_EXPR, gfc_get_label_decl (code->here));
      70                 :             : }
      71                 :             : 
      72                 :             : 
      73                 :             : /* Given a variable expression which has been ASSIGNed to, find the decl
      74                 :             :    containing the auxiliary variables.  For variables in common blocks this
      75                 :             :    is a field_decl.  */
      76                 :             : 
      77                 :             : void
      78                 :         187 : gfc_conv_label_variable (gfc_se * se, gfc_expr * expr)
      79                 :             : {
      80                 :         187 :   gcc_assert (expr->symtree->n.sym->attr.assign == 1);
      81                 :         187 :   gfc_conv_expr (se, expr);
      82                 :             :   /* Deals with variable in common block. Get the field declaration.  */
      83                 :         187 :   if (TREE_CODE (se->expr) == COMPONENT_REF)
      84                 :           0 :     se->expr = TREE_OPERAND (se->expr, 1);
      85                 :             :   /* Deals with dummy argument. Get the parameter declaration.  */
      86                 :         187 :   else if (INDIRECT_REF_P (se->expr))
      87                 :          12 :     se->expr = TREE_OPERAND (se->expr, 0);
      88                 :         187 : }
      89                 :             : 
      90                 :             : /* Translate a label assignment statement.  */
      91                 :             : 
      92                 :             : tree
      93                 :         116 : gfc_trans_label_assign (gfc_code * code)
      94                 :             : {
      95                 :         116 :   tree label_tree;
      96                 :         116 :   gfc_se se;
      97                 :         116 :   tree len;
      98                 :         116 :   tree addr;
      99                 :         116 :   tree len_tree;
     100                 :         116 :   int label_len;
     101                 :             : 
     102                 :             :   /* Start a new block.  */
     103                 :         116 :   gfc_init_se (&se, NULL);
     104                 :         116 :   gfc_start_block (&se.pre);
     105                 :         116 :   gfc_conv_label_variable (&se, code->expr1);
     106                 :             : 
     107                 :         116 :   len = GFC_DECL_STRING_LEN (se.expr);
     108                 :         116 :   addr = GFC_DECL_ASSIGN_ADDR (se.expr);
     109                 :             : 
     110                 :         116 :   label_tree = gfc_get_label_decl (code->label1);
     111                 :             : 
     112                 :         116 :   if (code->label1->defined == ST_LABEL_TARGET
     113                 :         116 :       || code->label1->defined == ST_LABEL_DO_TARGET)
     114                 :             :     {
     115                 :         115 :       label_tree = gfc_build_addr_expr (pvoid_type_node, label_tree);
     116                 :         115 :       len_tree = build_int_cst (gfc_charlen_type_node, -1);
     117                 :             :     }
     118                 :             :   else
     119                 :             :     {
     120                 :           1 :       gfc_expr *format = code->label1->format;
     121                 :             : 
     122                 :           1 :       label_len = format->value.character.length;
     123                 :           1 :       len_tree = build_int_cst (gfc_charlen_type_node, label_len);
     124                 :           2 :       label_tree = gfc_build_wide_string_const (format->ts.kind, label_len + 1,
     125                 :           1 :                                                 format->value.character.string);
     126                 :           1 :       label_tree = gfc_build_addr_expr (pvoid_type_node, label_tree);
     127                 :             :     }
     128                 :             : 
     129                 :         116 :   gfc_add_modify (&se.pre, len, fold_convert (TREE_TYPE (len), len_tree));
     130                 :         116 :   gfc_add_modify (&se.pre, addr, label_tree);
     131                 :             : 
     132                 :         116 :   return gfc_finish_block (&se.pre);
     133                 :             : }
     134                 :             : 
     135                 :             : /* Translate a GOTO statement.  */
     136                 :             : 
     137                 :             : tree
     138                 :        1185 : gfc_trans_goto (gfc_code * code)
     139                 :             : {
     140                 :        1185 :   locus loc = code->loc;
     141                 :        1185 :   tree assigned_goto;
     142                 :        1185 :   tree target;
     143                 :        1185 :   tree tmp;
     144                 :        1185 :   gfc_se se;
     145                 :             : 
     146                 :        1185 :   if (code->label1 != NULL)
     147                 :        1115 :     return build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
     148                 :             : 
     149                 :             :   /* ASSIGNED GOTO.  */
     150                 :          70 :   gfc_init_se (&se, NULL);
     151                 :          70 :   gfc_start_block (&se.pre);
     152                 :          70 :   gfc_conv_label_variable (&se, code->expr1);
     153                 :          70 :   tmp = GFC_DECL_STRING_LEN (se.expr);
     154                 :          70 :   tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
     155                 :          70 :                          build_int_cst (TREE_TYPE (tmp), -1));
     156                 :          70 :   gfc_trans_runtime_check (true, false, tmp, &se.pre, &loc,
     157                 :             :                            "Assigned label is not a target label");
     158                 :             : 
     159                 :          70 :   assigned_goto = GFC_DECL_ASSIGN_ADDR (se.expr);
     160                 :             : 
     161                 :             :   /* We're going to ignore a label list.  It does not really change the
     162                 :             :      statement's semantics (because it is just a further restriction on
     163                 :             :      what's legal code); before, we were comparing label addresses here, but
     164                 :             :      that's a very fragile business and may break with optimization.  So
     165                 :             :      just ignore it.  */
     166                 :             : 
     167                 :          70 :   target = fold_build1_loc (input_location, GOTO_EXPR, void_type_node,
     168                 :             :                             assigned_goto);
     169                 :          70 :   gfc_add_expr_to_block (&se.pre, target);
     170                 :          70 :   return gfc_finish_block (&se.pre);
     171                 :             : }
     172                 :             : 
     173                 :             : 
     174                 :             : /* Translate an ENTRY statement.  Just adds a label for this entry point.  */
     175                 :             : tree
     176                 :        1339 : gfc_trans_entry (gfc_code * code)
     177                 :             : {
     178                 :        1339 :   return build1_v (LABEL_EXPR, code->ext.entry->label);
     179                 :             : }
     180                 :             : 
     181                 :             : 
     182                 :             : /* Replace a gfc_ss structure by another both in the gfc_se struct
     183                 :             :    and the gfc_loopinfo struct.  This is used in gfc_conv_elemental_dependencies
     184                 :             :    to replace a variable ss by the corresponding temporary.  */
     185                 :             : 
     186                 :             : static void
     187                 :         155 : replace_ss (gfc_se *se, gfc_ss *old_ss, gfc_ss *new_ss)
     188                 :             : {
     189                 :         155 :   gfc_ss **sess, **loopss;
     190                 :             : 
     191                 :             :   /* The old_ss is a ss for a single variable.  */
     192                 :         155 :   gcc_assert (old_ss->info->type == GFC_SS_SECTION);
     193                 :             : 
     194                 :         302 :   for (sess = &(se->ss); *sess != gfc_ss_terminator; sess = &((*sess)->next))
     195                 :         302 :     if (*sess == old_ss)
     196                 :             :       break;
     197                 :         155 :   gcc_assert (*sess != gfc_ss_terminator);
     198                 :             : 
     199                 :         155 :   *sess = new_ss;
     200                 :         155 :   new_ss->next = old_ss->next;
     201                 :             : 
     202                 :             :   /* Make sure that trailing references are not lost.  */
     203                 :         155 :   if (old_ss->info
     204                 :         155 :       && old_ss->info->data.array.ref
     205                 :         155 :       && old_ss->info->data.array.ref->next
     206                 :          13 :       && !(new_ss->info->data.array.ref
     207                 :           0 :            && new_ss->info->data.array.ref->next))
     208                 :          13 :     new_ss->info->data.array.ref = old_ss->info->data.array.ref;
     209                 :             : 
     210                 :         302 :   for (loopss = &(se->loop->ss); *loopss != gfc_ss_terminator;
     211                 :         147 :        loopss = &((*loopss)->loop_chain))
     212                 :         302 :     if (*loopss == old_ss)
     213                 :             :       break;
     214                 :         155 :   gcc_assert (*loopss != gfc_ss_terminator);
     215                 :             : 
     216                 :         155 :   *loopss = new_ss;
     217                 :         155 :   new_ss->loop_chain = old_ss->loop_chain;
     218                 :         155 :   new_ss->loop = old_ss->loop;
     219                 :             : 
     220                 :         155 :   gfc_free_ss (old_ss);
     221                 :         155 : }
     222                 :             : 
     223                 :             : 
     224                 :             : /* Check for dependencies between INTENT(IN) and INTENT(OUT) arguments of
     225                 :             :    elemental subroutines.  Make temporaries for output arguments if any such
     226                 :             :    dependencies are found.  Output arguments are chosen because internal_unpack
     227                 :             :    can be used, as is, to copy the result back to the variable.  */
     228                 :             : static void
     229                 :        1760 : gfc_conv_elemental_dependencies (gfc_se * se, gfc_se * loopse,
     230                 :             :                                  gfc_symbol * sym, gfc_actual_arglist * arg,
     231                 :             :                                  gfc_dep_check check_variable)
     232                 :             : {
     233                 :        1760 :   gfc_actual_arglist *arg0;
     234                 :        1760 :   gfc_expr *e;
     235                 :        1760 :   gfc_formal_arglist *formal;
     236                 :        1760 :   gfc_se parmse;
     237                 :        1760 :   gfc_ss *ss;
     238                 :        1760 :   gfc_symbol *fsym;
     239                 :        1760 :   tree data;
     240                 :        1760 :   tree size;
     241                 :        1760 :   tree tmp;
     242                 :             : 
     243                 :        1760 :   if (loopse->ss == NULL)
     244                 :           0 :     return;
     245                 :             : 
     246                 :        1760 :   ss = loopse->ss;
     247                 :        1760 :   arg0 = arg;
     248                 :        1760 :   formal = gfc_sym_get_dummy_args (sym);
     249                 :             : 
     250                 :             :   /* Loop over all the arguments testing for dependencies.  */
     251                 :        9398 :   for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
     252                 :             :     {
     253                 :        3819 :       e = arg->expr;
     254                 :        3819 :       if (e == NULL)
     255                 :          12 :         continue;
     256                 :             : 
     257                 :             :       /* Obtain the info structure for the current argument.  */
     258                 :        6689 :       for (ss = loopse->ss; ss && ss != gfc_ss_terminator; ss = ss->next)
     259                 :        6487 :         if (ss->info->expr == e)
     260                 :             :           break;
     261                 :             : 
     262                 :             :       /* If there is a dependency, create a temporary and use it
     263                 :             :          instead of the variable.  */
     264                 :        3807 :       fsym = formal ? formal->sym : NULL;
     265                 :        3807 :       if (e->expr_type == EXPR_VARIABLE
     266                 :        3052 :             && e->rank && fsym
     267                 :        2567 :             && fsym->attr.intent != INTENT_IN
     268                 :        1191 :             && !fsym->attr.value
     269                 :        4938 :             && gfc_check_fncall_dependency (e, fsym->attr.intent,
     270                 :             :                                             sym, arg0, check_variable))
     271                 :             :         {
     272                 :         155 :           tree initial, temptype;
     273                 :         155 :           stmtblock_t temp_post;
     274                 :         155 :           gfc_ss *tmp_ss;
     275                 :             : 
     276                 :         155 :           tmp_ss = gfc_get_array_ss (gfc_ss_terminator, NULL, ss->dimen,
     277                 :             :                                      GFC_SS_SECTION);
     278                 :         155 :           gfc_mark_ss_chain_used (tmp_ss, 1);
     279                 :         155 :           tmp_ss->info->expr = ss->info->expr;
     280                 :         155 :           replace_ss (loopse, ss, tmp_ss);
     281                 :             : 
     282                 :             :           /* Obtain the argument descriptor for unpacking.  */
     283                 :         155 :           gfc_init_se (&parmse, NULL);
     284                 :         155 :           parmse.want_pointer = 1;
     285                 :         155 :           gfc_conv_expr_descriptor (&parmse, e);
     286                 :         155 :           gfc_add_block_to_block (&se->pre, &parmse.pre);
     287                 :             : 
     288                 :             :           /* If we've got INTENT(INOUT) or a derived type with INTENT(OUT),
     289                 :             :              initialize the array temporary with a copy of the values.  */
     290                 :         155 :           if (fsym->attr.intent == INTENT_INOUT
     291                 :         111 :                 || (fsym->ts.type ==BT_DERIVED
     292                 :          51 :                       && fsym->attr.intent == INTENT_OUT))
     293                 :          95 :             initial = parmse.expr;
     294                 :             :           /* For class expressions, we always initialize with the copy of
     295                 :             :              the values.  */
     296                 :          60 :           else if (e->ts.type == BT_CLASS)
     297                 :           6 :             initial = parmse.expr;
     298                 :             :           else
     299                 :             :             initial = NULL_TREE;
     300                 :             : 
     301                 :         155 :           if (e->ts.type != BT_CLASS)
     302                 :             :             {
     303                 :             :              /* Find the type of the temporary to create; we don't use the type
     304                 :             :                 of e itself as this breaks for subcomponent-references in e
     305                 :             :                 (where the type of e is that of the final reference, but
     306                 :             :                 parmse.expr's type corresponds to the full derived-type).  */
     307                 :             :              /* TODO: Fix this somehow so we don't need a temporary of the whole
     308                 :             :                 array but instead only the components referenced.  */
     309                 :         149 :               temptype = TREE_TYPE (parmse.expr); /* Pointer to descriptor.  */
     310                 :         149 :               gcc_assert (TREE_CODE (temptype) == POINTER_TYPE);
     311                 :         149 :               temptype = TREE_TYPE (temptype);
     312                 :         149 :               temptype = gfc_get_element_type (temptype);
     313                 :             :             }
     314                 :             : 
     315                 :             :           else
     316                 :             :             /* For class arrays signal that the size of the dynamic type has to
     317                 :             :                be obtained from the vtable, using the 'initial' expression.  */
     318                 :             :             temptype = NULL_TREE;
     319                 :             : 
     320                 :             :           /* Generate the temporary.  Cleaning up the temporary should be the
     321                 :             :              very last thing done, so we add the code to a new block and add it
     322                 :             :              to se->post as last instructions.  */
     323                 :         155 :           size = gfc_create_var (gfc_array_index_type, NULL);
     324                 :         155 :           data = gfc_create_var (pvoid_type_node, NULL);
     325                 :         155 :           gfc_init_block (&temp_post);
     326                 :         310 :           tmp = gfc_trans_create_temp_array (&se->pre, &temp_post, tmp_ss,
     327                 :             :                                              temptype, initial, false, true,
     328                 :         155 :                                              false, &arg->expr->where);
     329                 :         155 :           gfc_add_modify (&se->pre, size, tmp);
     330                 :         155 :           tmp = fold_convert (pvoid_type_node, tmp_ss->info->data.array.data);
     331                 :         155 :           gfc_add_modify (&se->pre, data, tmp);
     332                 :             : 
     333                 :             :           /* Update other ss' delta.  */
     334                 :         155 :           gfc_set_delta (loopse->loop);
     335                 :             : 
     336                 :             :           /* Copy the result back using unpack.....  */
     337                 :         155 :           if (e->ts.type != BT_CLASS)
     338                 :         149 :             tmp = build_call_expr_loc (input_location,
     339                 :             :                         gfor_fndecl_in_unpack, 2, parmse.expr, data);
     340                 :             :           else
     341                 :             :             {
     342                 :             :               /* ... except for class results where the copy is
     343                 :             :                  unconditional.  */
     344                 :           6 :               tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
     345                 :           6 :               tmp = gfc_conv_descriptor_data_get (tmp);
     346                 :           6 :               tmp = build_call_expr_loc (input_location,
     347                 :             :                                          builtin_decl_explicit (BUILT_IN_MEMCPY),
     348                 :             :                                          3, tmp, data,
     349                 :             :                                          fold_convert (size_type_node, size));
     350                 :             :             }
     351                 :         155 :           gfc_add_expr_to_block (&se->post, tmp);
     352                 :             : 
     353                 :             :           /* parmse.pre is already added above.  */
     354                 :         155 :           gfc_add_block_to_block (&se->post, &parmse.post);
     355                 :         155 :           gfc_add_block_to_block (&se->post, &temp_post);
     356                 :             :         }
     357                 :             :     }
     358                 :             : }
     359                 :             : 
     360                 :             : 
     361                 :             : /* Given an executable statement referring to an intrinsic function call,
     362                 :             :    returns the intrinsic symbol.  */
     363                 :             : 
     364                 :             : static gfc_intrinsic_sym *
     365                 :        5187 : get_intrinsic_for_code (gfc_code *code)
     366                 :             : {
     367                 :        5187 :   if (code->op == EXEC_CALL)
     368                 :             :     {
     369                 :        4869 :       gfc_intrinsic_sym * const isym = code->resolved_isym;
     370                 :        4869 :       if (isym)
     371                 :             :         return isym;
     372                 :             :       else
     373                 :        4696 :         return gfc_get_intrinsic_for_expr (code->expr1);
     374                 :             :     }
     375                 :             : 
     376                 :             :   return NULL;
     377                 :             : }
     378                 :             : 
     379                 :             : 
     380                 :             : /* Translate the CALL statement.  Builds a call to an F95 subroutine.  */
     381                 :             : 
     382                 :             : tree
     383                 :       69505 : gfc_trans_call (gfc_code * code, bool dependency_check,
     384                 :             :                 tree mask, tree count1, bool invert)
     385                 :             : {
     386                 :       69505 :   gfc_se se;
     387                 :       69505 :   gfc_ss * ss;
     388                 :       69505 :   int has_alternate_specifier;
     389                 :       69505 :   gfc_dep_check check_variable;
     390                 :       69505 :   tree index = NULL_TREE;
     391                 :       69505 :   tree maskexpr = NULL_TREE;
     392                 :       69505 :   tree tmp;
     393                 :       69505 :   bool is_intrinsic_mvbits;
     394                 :             : 
     395                 :             :   /* A CALL starts a new block because the actual arguments may have to
     396                 :             :      be evaluated first.  */
     397                 :       69505 :   gfc_init_se (&se, NULL);
     398                 :       69505 :   gfc_start_block (&se.pre);
     399                 :             : 
     400                 :       69505 :   gcc_assert (code->resolved_sym);
     401                 :             : 
     402                 :       69505 :   ss = gfc_ss_terminator;
     403                 :       69505 :   if (code->resolved_sym->attr.elemental)
     404                 :        5187 :     ss = gfc_walk_elemental_function_args (ss, code->ext.actual,
     405                 :             :                                            get_intrinsic_for_code (code),
     406                 :             :                                            GFC_SS_REFERENCE);
     407                 :             : 
     408                 :             :   /* MVBITS is inlined but needs the dependency checking found here.  */
     409                 :      139010 :   is_intrinsic_mvbits = code->resolved_isym
     410                 :       69505 :                         && code->resolved_isym->id == GFC_ISYM_MVBITS;
     411                 :             : 
     412                 :             :   /* Is not an elemental subroutine call with array valued arguments.  */
     413                 :       69505 :   if (ss == gfc_ss_terminator)
     414                 :             :     {
     415                 :             : 
     416                 :       67745 :       if (is_intrinsic_mvbits)
     417                 :             :         {
     418                 :         106 :           has_alternate_specifier = 0;
     419                 :         106 :           gfc_conv_intrinsic_mvbits (&se, code->ext.actual, NULL);
     420                 :             :         }
     421                 :             :       else
     422                 :             :         {
     423                 :             :           /* Translate the call.  */
     424                 :       67639 :           has_alternate_specifier =
     425                 :       67639 :             gfc_conv_procedure_call (&se, code->resolved_sym,
     426                 :             :                                      code->ext.actual, code->expr1, NULL);
     427                 :             : 
     428                 :             :           /* A subroutine without side-effect, by definition, does nothing!  */
     429                 :       67639 :           TREE_SIDE_EFFECTS (se.expr) = 1;
     430                 :             :         }
     431                 :             : 
     432                 :             :       /* Chain the pieces together and return the block.  */
     433                 :       67745 :       if (has_alternate_specifier)
     434                 :             :         {
     435                 :         140 :           gfc_code *select_code;
     436                 :         140 :           gfc_symbol *sym;
     437                 :         140 :           select_code = code->next;
     438                 :         140 :           gcc_assert(select_code->op == EXEC_SELECT);
     439                 :         140 :           sym = select_code->expr1->symtree->n.sym;
     440                 :         140 :           se.expr = convert (gfc_typenode_for_spec (&sym->ts), se.expr);
     441                 :         140 :           if (sym->backend_decl == NULL)
     442                 :           1 :             sym->backend_decl = gfc_get_symbol_decl (sym);
     443                 :         140 :           gfc_add_modify (&se.pre, sym->backend_decl, se.expr);
     444                 :             :         }
     445                 :             :       else
     446                 :       67605 :         gfc_add_expr_to_block (&se.pre, se.expr);
     447                 :             : 
     448                 :       67745 :       gfc_add_block_to_block (&se.finalblock, &se.post);
     449                 :       67745 :       gfc_add_block_to_block (&se.pre, &se.finalblock);
     450                 :             :     }
     451                 :             : 
     452                 :             :   else
     453                 :             :     {
     454                 :             :       /* An elemental subroutine call with array valued arguments has
     455                 :             :          to be scalarized.  */
     456                 :        1760 :       gfc_loopinfo loop;
     457                 :        1760 :       stmtblock_t body;
     458                 :        1760 :       stmtblock_t block;
     459                 :        1760 :       gfc_se loopse;
     460                 :        1760 :       gfc_se depse;
     461                 :             : 
     462                 :             :       /* gfc_walk_elemental_function_args renders the ss chain in the
     463                 :             :          reverse order to the actual argument order.  */
     464                 :        1760 :       ss = gfc_reverse_ss (ss);
     465                 :             : 
     466                 :             :       /* Initialize the loop.  */
     467                 :        1760 :       gfc_init_se (&loopse, NULL);
     468                 :        1760 :       gfc_init_loopinfo (&loop);
     469                 :        1760 :       gfc_add_ss_to_loop (&loop, ss);
     470                 :             : 
     471                 :        1760 :       gfc_conv_ss_startstride (&loop);
     472                 :             :       /* TODO: gfc_conv_loop_setup generates a temporary for vector
     473                 :             :          subscripts.  This could be prevented in the elemental case
     474                 :             :          as temporaries are handled separately
     475                 :             :          (below in gfc_conv_elemental_dependencies).  */
     476                 :        1760 :       if (code->expr1)
     477                 :         213 :         gfc_conv_loop_setup (&loop, &code->expr1->where);
     478                 :             :       else
     479                 :        1547 :         gfc_conv_loop_setup (&loop, &code->loc);
     480                 :             : 
     481                 :        1760 :       gfc_mark_ss_chain_used (ss, 1);
     482                 :             : 
     483                 :             :       /* Convert the arguments, checking for dependencies.  */
     484                 :        1760 :       gfc_copy_loopinfo_to_se (&loopse, &loop);
     485                 :        1760 :       loopse.ss = ss;
     486                 :             : 
     487                 :             :       /* For operator assignment, do dependency checking.  */
     488                 :        1760 :       if (dependency_check)
     489                 :             :         check_variable = ELEM_CHECK_VARIABLE;
     490                 :             :       else
     491                 :        1448 :         check_variable = ELEM_DONT_CHECK_VARIABLE;
     492                 :             : 
     493                 :        1760 :       gfc_init_se (&depse, NULL);
     494                 :        1760 :       gfc_conv_elemental_dependencies (&depse, &loopse, code->resolved_sym,
     495                 :             :                                        code->ext.actual, check_variable);
     496                 :             : 
     497                 :        1760 :       gfc_add_block_to_block (&loop.pre,  &depse.pre);
     498                 :        1760 :       gfc_add_block_to_block (&loop.post, &depse.post);
     499                 :             : 
     500                 :             :       /* Generate the loop body.  */
     501                 :        1760 :       gfc_start_scalarized_body (&loop, &body);
     502                 :        1760 :       gfc_init_block (&block);
     503                 :             : 
     504                 :        1760 :       if (mask && count1)
     505                 :             :         {
     506                 :             :           /* Form the mask expression according to the mask.  */
     507                 :          44 :           index = count1;
     508                 :          44 :           maskexpr = gfc_build_array_ref (mask, index, NULL);
     509                 :          44 :           if (invert)
     510                 :          11 :             maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
     511                 :          11 :                                         TREE_TYPE (maskexpr), maskexpr);
     512                 :             :         }
     513                 :             : 
     514                 :        1760 :       if (is_intrinsic_mvbits)
     515                 :             :         {
     516                 :          67 :           has_alternate_specifier = 0;
     517                 :          67 :           gfc_conv_intrinsic_mvbits (&loopse, code->ext.actual, &loop);
     518                 :             :         }
     519                 :             :       else
     520                 :             :         {
     521                 :             :           /* Add the subroutine call to the block.  */
     522                 :        1693 :           gfc_conv_procedure_call (&loopse, code->resolved_sym,
     523                 :             :                                    code->ext.actual, code->expr1,
     524                 :             :                                    NULL);
     525                 :             :         }
     526                 :             : 
     527                 :        1760 :       if (mask && count1)
     528                 :             :         {
     529                 :          44 :           tmp = build3_v (COND_EXPR, maskexpr, loopse.expr,
     530                 :             :                           build_empty_stmt (input_location));
     531                 :          44 :           gfc_add_expr_to_block (&loopse.pre, tmp);
     532                 :          44 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
     533                 :             :                                  gfc_array_index_type,
     534                 :             :                                  count1, gfc_index_one_node);
     535                 :          44 :           gfc_add_modify (&loopse.pre, count1, tmp);
     536                 :             :         }
     537                 :             :       else
     538                 :        1716 :         gfc_add_expr_to_block (&loopse.pre, loopse.expr);
     539                 :             : 
     540                 :        1760 :       gfc_add_block_to_block (&block, &loopse.pre);
     541                 :        1760 :       gfc_add_block_to_block (&block, &loopse.post);
     542                 :             : 
     543                 :             :       /* Finish up the loop block and the loop.  */
     544                 :        1760 :       gfc_add_expr_to_block (&body, gfc_finish_block (&block));
     545                 :        1760 :       gfc_trans_scalarizing_loops (&loop, &body);
     546                 :        1760 :       gfc_add_block_to_block (&se.pre, &loop.pre);
     547                 :        1760 :       gfc_add_block_to_block (&se.pre, &loop.post);
     548                 :        1760 :       gfc_add_block_to_block (&se.pre, &loopse.finalblock);
     549                 :        1760 :       gfc_add_block_to_block (&se.pre, &se.post);
     550                 :        1760 :       gfc_cleanup_loop (&loop);
     551                 :             :     }
     552                 :             : 
     553                 :       69505 :   return gfc_finish_block (&se.pre);
     554                 :             : }
     555                 :             : 
     556                 :             : 
     557                 :             : /* Translate the RETURN statement.  */
     558                 :             : 
     559                 :             : tree
     560                 :        2942 : gfc_trans_return (gfc_code * code)
     561                 :             : {
     562                 :        2942 :   if (code->expr1)
     563                 :             :     {
     564                 :          50 :       gfc_se se;
     565                 :          50 :       tree tmp;
     566                 :          50 :       tree result;
     567                 :             : 
     568                 :             :       /* If code->expr is not NULL, this return statement must appear
     569                 :             :          in a subroutine and current_fake_result_decl has already
     570                 :             :          been generated.  */
     571                 :             : 
     572                 :          50 :       result = gfc_get_fake_result_decl (NULL, 0);
     573                 :          50 :       if (!result)
     574                 :             :         {
     575                 :           0 :           gfc_warning (0,
     576                 :             :                        "An alternate return at %L without a * dummy argument",
     577                 :           0 :                        &code->expr1->where);
     578                 :           0 :           return gfc_generate_return ();
     579                 :             :         }
     580                 :             : 
     581                 :             :       /* Start a new block for this statement.  */
     582                 :          50 :       gfc_init_se (&se, NULL);
     583                 :          50 :       gfc_start_block (&se.pre);
     584                 :             : 
     585                 :          50 :       gfc_conv_expr (&se, code->expr1);
     586                 :             : 
     587                 :             :       /* Note that the actually returned expression is a simple value and
     588                 :             :          does not depend on any pointers or such; thus we can clean-up with
     589                 :             :          se.post before returning.  */
     590                 :          50 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (result),
     591                 :          50 :                              result, fold_convert (TREE_TYPE (result),
     592                 :             :                              se.expr));
     593                 :          50 :       gfc_add_expr_to_block (&se.pre, tmp);
     594                 :          50 :       gfc_add_block_to_block (&se.pre, &se.post);
     595                 :             : 
     596                 :          50 :       tmp = gfc_generate_return ();
     597                 :          50 :       gfc_add_expr_to_block (&se.pre, tmp);
     598                 :          50 :       return gfc_finish_block (&se.pre);
     599                 :             :     }
     600                 :             : 
     601                 :        2892 :   return gfc_generate_return ();
     602                 :             : }
     603                 :             : 
     604                 :             : 
     605                 :             : /* Translate the PAUSE statement.  We have to translate this statement
     606                 :             :    to a runtime library call.  */
     607                 :             : 
     608                 :             : tree
     609                 :          28 : gfc_trans_pause (gfc_code * code)
     610                 :             : {
     611                 :          28 :   tree gfc_int8_type_node = gfc_get_int_type (8);
     612                 :          28 :   gfc_se se;
     613                 :          28 :   tree tmp;
     614                 :             : 
     615                 :             :   /* Start a new block for this statement.  */
     616                 :          28 :   gfc_init_se (&se, NULL);
     617                 :          28 :   gfc_start_block (&se.pre);
     618                 :             : 
     619                 :             : 
     620                 :          28 :   if (code->expr1 == NULL)
     621                 :             :     {
     622                 :          10 :       tmp = build_int_cst (size_type_node, 0);
     623                 :          10 :       tmp = build_call_expr_loc (input_location,
     624                 :             :                                  gfor_fndecl_pause_string, 2,
     625                 :          10 :                                  build_int_cst (pchar_type_node, 0), tmp);
     626                 :             :     }
     627                 :          18 :   else if (code->expr1->ts.type == BT_INTEGER)
     628                 :             :     {
     629                 :           9 :       gfc_conv_expr (&se, code->expr1);
     630                 :           9 :       tmp = build_call_expr_loc (input_location,
     631                 :             :                                  gfor_fndecl_pause_numeric, 1,
     632                 :             :                                  fold_convert (gfc_int8_type_node, se.expr));
     633                 :             :     }
     634                 :             :   else
     635                 :             :     {
     636                 :           9 :       gfc_conv_expr_reference (&se, code->expr1);
     637                 :           9 :       tmp = build_call_expr_loc (input_location,
     638                 :             :                              gfor_fndecl_pause_string, 2,
     639                 :             :                                  se.expr, fold_convert (size_type_node,
     640                 :             :                                                         se.string_length));
     641                 :             :     }
     642                 :             : 
     643                 :          28 :   gfc_add_expr_to_block (&se.pre, tmp);
     644                 :             : 
     645                 :          28 :   gfc_add_block_to_block (&se.pre, &se.post);
     646                 :             : 
     647                 :          28 :   return gfc_finish_block (&se.pre);
     648                 :             : }
     649                 :             : 
     650                 :             : 
     651                 :             : /* Translate the STOP statement.  We have to translate this statement
     652                 :             :    to a runtime library call.  */
     653                 :             : 
     654                 :             : tree
     655                 :      179390 : gfc_trans_stop (gfc_code *code, bool error_stop)
     656                 :             : {
     657                 :      179390 :   gfc_se se;
     658                 :      179390 :   tree tmp;
     659                 :      179390 :   tree quiet;
     660                 :             : 
     661                 :             :   /* Start a new block for this statement.  */
     662                 :      179390 :   gfc_init_se (&se, NULL);
     663                 :      179390 :   gfc_start_block (&se.pre);
     664                 :             : 
     665                 :      179390 :   if (code->expr2)
     666                 :             :     {
     667                 :          25 :       gfc_conv_expr_val (&se, code->expr2);
     668                 :          25 :       quiet = fold_convert (boolean_type_node, se.expr);
     669                 :             :     }
     670                 :             :   else
     671                 :      179365 :     quiet = boolean_false_node;
     672                 :             : 
     673                 :      179390 :   if (code->expr1 == NULL)
     674                 :             :     {
     675                 :        4955 :       tmp = build_int_cst (size_type_node, 0);
     676                 :        9910 :       tmp = build_call_expr_loc (input_location,
     677                 :             :                                  error_stop
     678                 :        4023 :                                  ? (flag_coarray == GFC_FCOARRAY_LIB
     679                 :        4023 :                                     ? gfor_fndecl_caf_error_stop_str
     680                 :             :                                     : gfor_fndecl_error_stop_string)
     681                 :         932 :                                  : (flag_coarray == GFC_FCOARRAY_LIB
     682                 :         932 :                                     ? gfor_fndecl_caf_stop_str
     683                 :             :                                     : gfor_fndecl_stop_string),
     684                 :        4955 :                                  3, build_int_cst (pchar_type_node, 0), tmp,
     685                 :             :                                  quiet);
     686                 :             :     }
     687                 :      174435 :   else if (code->expr1->ts.type == BT_INTEGER)
     688                 :             :     {
     689                 :      173990 :       gfc_conv_expr (&se, code->expr1);
     690                 :      347980 :       tmp = build_call_expr_loc (input_location,
     691                 :             :                                  error_stop
     692                 :       13852 :                                  ? (flag_coarray == GFC_FCOARRAY_LIB
     693                 :       13852 :                                     ? gfor_fndecl_caf_error_stop
     694                 :             :                                     : gfor_fndecl_error_stop_numeric)
     695                 :      160138 :                                  : (flag_coarray == GFC_FCOARRAY_LIB
     696                 :      160138 :                                     ? gfor_fndecl_caf_stop_numeric
     697                 :             :                                     : gfor_fndecl_stop_numeric), 2,
     698                 :             :                                  fold_convert (integer_type_node, se.expr),
     699                 :             :                                  quiet);
     700                 :             :     }
     701                 :             :   else
     702                 :             :     {
     703                 :         445 :       gfc_conv_expr_reference (&se, code->expr1);
     704                 :         890 :       tmp = build_call_expr_loc (input_location,
     705                 :             :                                  error_stop
     706                 :         337 :                                  ? (flag_coarray == GFC_FCOARRAY_LIB
     707                 :         337 :                                     ? gfor_fndecl_caf_error_stop_str
     708                 :             :                                     : gfor_fndecl_error_stop_string)
     709                 :         108 :                                  : (flag_coarray == GFC_FCOARRAY_LIB
     710                 :         108 :                                     ? gfor_fndecl_caf_stop_str
     711                 :             :                                     : gfor_fndecl_stop_string),
     712                 :             :                                  3, se.expr, fold_convert (size_type_node,
     713                 :             :                                                            se.string_length),
     714                 :             :                                  quiet);
     715                 :             :     }
     716                 :             : 
     717                 :      179390 :   gfc_add_expr_to_block (&se.pre, tmp);
     718                 :             : 
     719                 :      179390 :   gfc_add_block_to_block (&se.pre, &se.post);
     720                 :             : 
     721                 :      179390 :   return gfc_finish_block (&se.pre);
     722                 :             : }
     723                 :             : 
     724                 :             : /* Translate the FAIL IMAGE statement.  */
     725                 :             : 
     726                 :             : tree
     727                 :           3 : gfc_trans_fail_image (gfc_code *code ATTRIBUTE_UNUSED)
     728                 :             : {
     729                 :           3 :   if (flag_coarray == GFC_FCOARRAY_LIB)
     730                 :           2 :     return build_call_expr_loc (input_location,
     731                 :           2 :                                 gfor_fndecl_caf_fail_image, 0);
     732                 :             :   else
     733                 :             :     {
     734                 :           1 :       const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
     735                 :           1 :       gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
     736                 :           1 :       tree tmp = gfc_get_symbol_decl (exsym);
     737                 :           1 :       return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
     738                 :             :     }
     739                 :             : }
     740                 :             : 
     741                 :             : /* Translate the FORM TEAM statement.  */
     742                 :             : 
     743                 :             : tree
     744                 :          25 : gfc_trans_form_team (gfc_code *code)
     745                 :             : {
     746                 :          25 :   if (flag_coarray == GFC_FCOARRAY_LIB)
     747                 :             :     {
     748                 :           1 :       gfc_se se;
     749                 :           1 :       gfc_se argse1, argse2;
     750                 :           1 :       tree team_id, team_type, tmp;
     751                 :             : 
     752                 :           1 :       gfc_init_se (&se, NULL);
     753                 :           1 :       gfc_init_se (&argse1, NULL);
     754                 :           1 :       gfc_init_se (&argse2, NULL);
     755                 :           1 :       gfc_start_block (&se.pre);
     756                 :             : 
     757                 :           1 :       gfc_conv_expr_val (&argse1, code->expr1);
     758                 :           1 :       gfc_conv_expr_val (&argse2, code->expr2);
     759                 :           1 :       team_id = fold_convert (integer_type_node, argse1.expr);
     760                 :           1 :       team_type = gfc_build_addr_expr (ppvoid_type_node, argse2.expr);
     761                 :             : 
     762                 :           1 :       gfc_add_block_to_block (&se.pre, &argse1.pre);
     763                 :           1 :       gfc_add_block_to_block (&se.pre, &argse2.pre);
     764                 :           1 :       tmp = build_call_expr_loc (input_location,
     765                 :             :                                  gfor_fndecl_caf_form_team, 3,
     766                 :             :                                  team_id, team_type,
     767                 :             :                                  integer_zero_node);
     768                 :           1 :       gfc_add_expr_to_block (&se.pre, tmp);
     769                 :           1 :       gfc_add_block_to_block (&se.pre, &argse1.post);
     770                 :           1 :       gfc_add_block_to_block (&se.pre, &argse2.post);
     771                 :           1 :       return gfc_finish_block (&se.pre);
     772                 :             :     }
     773                 :             :   else
     774                 :             :     {
     775                 :          24 :       const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
     776                 :          24 :       gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
     777                 :          24 :       tree tmp = gfc_get_symbol_decl (exsym);
     778                 :          24 :       return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
     779                 :             :     }
     780                 :             : }
     781                 :             : 
     782                 :             : /* Translate the CHANGE TEAM statement.  */
     783                 :             : 
     784                 :             : tree
     785                 :          19 : gfc_trans_change_team (gfc_code *code)
     786                 :             : {
     787                 :          19 :   if (flag_coarray == GFC_FCOARRAY_LIB)
     788                 :             :     {
     789                 :           1 :       gfc_se argse;
     790                 :           1 :       tree team_type, tmp;
     791                 :             : 
     792                 :           1 :       gfc_init_se (&argse, NULL);
     793                 :           1 :       gfc_conv_expr_val (&argse, code->expr1);
     794                 :           1 :       team_type = gfc_build_addr_expr (ppvoid_type_node, argse.expr);
     795                 :             : 
     796                 :           1 :       tmp = build_call_expr_loc (input_location,
     797                 :             :                                  gfor_fndecl_caf_change_team, 2, team_type,
     798                 :             :                                  integer_zero_node);
     799                 :           1 :       gfc_add_expr_to_block (&argse.pre, tmp);
     800                 :           1 :       gfc_add_block_to_block (&argse.pre, &argse.post);
     801                 :           1 :       return gfc_finish_block (&argse.pre);
     802                 :             :     }
     803                 :             :   else
     804                 :             :     {
     805                 :          18 :       const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
     806                 :          18 :       gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
     807                 :          18 :       tree tmp = gfc_get_symbol_decl (exsym);
     808                 :          18 :       return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
     809                 :             :     }
     810                 :             : }
     811                 :             : 
     812                 :             : /* Translate the END TEAM statement.  */
     813                 :             : 
     814                 :             : tree
     815                 :          19 : gfc_trans_end_team (gfc_code *code ATTRIBUTE_UNUSED)
     816                 :             : {
     817                 :          19 :   if (flag_coarray == GFC_FCOARRAY_LIB)
     818                 :             :     {
     819                 :           1 :       return build_call_expr_loc (input_location,
     820                 :             :                                   gfor_fndecl_caf_end_team, 1,
     821                 :           1 :                                   build_int_cst (pchar_type_node, 0));
     822                 :             :     }
     823                 :             :   else
     824                 :             :     {
     825                 :          18 :       const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
     826                 :          18 :       gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
     827                 :          18 :       tree tmp = gfc_get_symbol_decl (exsym);
     828                 :          18 :       return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
     829                 :             :     }
     830                 :             : }
     831                 :             : 
     832                 :             : /* Translate the SYNC TEAM statement.  */
     833                 :             : 
     834                 :             : tree
     835                 :           0 : gfc_trans_sync_team (gfc_code *code)
     836                 :             : {
     837                 :           0 :   if (flag_coarray == GFC_FCOARRAY_LIB)
     838                 :             :     {
     839                 :           0 :       gfc_se argse;
     840                 :           0 :       tree team_type, tmp;
     841                 :             : 
     842                 :           0 :       gfc_init_se (&argse, NULL);
     843                 :           0 :       gfc_conv_expr_val (&argse, code->expr1);
     844                 :           0 :       team_type = gfc_build_addr_expr (ppvoid_type_node, argse.expr);
     845                 :             : 
     846                 :           0 :       tmp = build_call_expr_loc (input_location,
     847                 :             :                                  gfor_fndecl_caf_sync_team, 2,
     848                 :             :                                  team_type,
     849                 :             :                                  integer_zero_node);
     850                 :           0 :       gfc_add_expr_to_block (&argse.pre, tmp);
     851                 :           0 :       gfc_add_block_to_block (&argse.pre, &argse.post);
     852                 :           0 :       return gfc_finish_block (&argse.pre);
     853                 :             :     }
     854                 :             :   else
     855                 :             :     {
     856                 :           0 :       const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
     857                 :           0 :       gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
     858                 :           0 :       tree tmp = gfc_get_symbol_decl (exsym);
     859                 :           0 :       return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
     860                 :             :     }
     861                 :             : }
     862                 :             : 
     863                 :             : tree
     864                 :          84 : gfc_trans_lock_unlock (gfc_code *code, gfc_exec_op op)
     865                 :             : {
     866                 :          84 :   gfc_se se, argse;
     867                 :          84 :   tree stat = NULL_TREE, stat2 = NULL_TREE;
     868                 :          84 :   tree lock_acquired = NULL_TREE, lock_acquired2 = NULL_TREE;
     869                 :             : 
     870                 :             :   /* Short cut: For single images without STAT= or LOCK_ACQUIRED
     871                 :             :      return early. (ERRMSG= is always untouched for -fcoarray=single.)  */
     872                 :          84 :   if (!code->expr2 && !code->expr4 && flag_coarray != GFC_FCOARRAY_LIB)
     873                 :             :     return NULL_TREE;
     874                 :             : 
     875                 :          66 :   if (code->expr2)
     876                 :             :     {
     877                 :          28 :       gcc_assert (code->expr2->expr_type == EXPR_VARIABLE);
     878                 :          28 :       gfc_init_se (&argse, NULL);
     879                 :          28 :       gfc_conv_expr_val (&argse, code->expr2);
     880                 :          28 :       stat = argse.expr;
     881                 :             :     }
     882                 :          38 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
     883                 :          32 :     stat = null_pointer_node;
     884                 :             : 
     885                 :          66 :   if (code->expr4)
     886                 :             :     {
     887                 :          14 :       gcc_assert (code->expr4->expr_type == EXPR_VARIABLE);
     888                 :          14 :       gfc_init_se (&argse, NULL);
     889                 :          14 :       gfc_conv_expr_val (&argse, code->expr4);
     890                 :          14 :       lock_acquired = argse.expr;
     891                 :             :     }
     892                 :          52 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
     893                 :          40 :     lock_acquired = null_pointer_node;
     894                 :             : 
     895                 :          66 :   gfc_start_block (&se.pre);
     896                 :          66 :   if (flag_coarray == GFC_FCOARRAY_LIB)
     897                 :             :     {
     898                 :          48 :       tree tmp, token, image_index, errmsg, errmsg_len;
     899                 :          48 :       tree index = build_zero_cst (gfc_array_index_type);
     900                 :          48 :       tree caf_decl = gfc_get_tree_for_caf_expr (code->expr1);
     901                 :             : 
     902                 :          48 :       if (code->expr1->symtree->n.sym->ts.type != BT_DERIVED
     903                 :          48 :           || code->expr1->symtree->n.sym->ts.u.derived->from_intmod
     904                 :             :              != INTMOD_ISO_FORTRAN_ENV
     905                 :          48 :           || code->expr1->symtree->n.sym->ts.u.derived->intmod_sym_id
     906                 :             :              != ISOFORTRAN_LOCK_TYPE)
     907                 :             :         {
     908                 :           4 :           gfc_error ("Sorry, the lock component of derived type at %L is not "
     909                 :             :                      "yet supported", &code->expr1->where);
     910                 :           4 :           return NULL_TREE;
     911                 :             :         }
     912                 :             : 
     913                 :          44 :       gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
     914                 :             :                                 code->expr1);
     915                 :             : 
     916                 :          44 :       if (gfc_is_coindexed (code->expr1))
     917                 :          16 :         image_index = gfc_caf_get_image_index (&se.pre, code->expr1, caf_decl);
     918                 :             :       else
     919                 :          28 :         image_index = integer_zero_node;
     920                 :             : 
     921                 :             :       /* For arrays, obtain the array index.  */
     922                 :          44 :       if (gfc_expr_attr (code->expr1).dimension)
     923                 :             :         {
     924                 :          28 :           tree desc, tmp, extent, lbound, ubound;
     925                 :          28 :           gfc_array_ref *ar, ar2;
     926                 :          28 :           int i;
     927                 :             : 
     928                 :             :           /* TODO: Extend this, once DT components are supported.  */
     929                 :          28 :           ar = &code->expr1->ref->u.ar;
     930                 :          28 :           ar2 = *ar;
     931                 :          28 :           memset (ar, '\0', sizeof (*ar));
     932                 :          28 :           ar->as = ar2.as;
     933                 :          28 :           ar->type = AR_FULL;
     934                 :             : 
     935                 :          28 :           gfc_init_se (&argse, NULL);
     936                 :          28 :           argse.descriptor_only = 1;
     937                 :          28 :           gfc_conv_expr_descriptor (&argse, code->expr1);
     938                 :          28 :           gfc_add_block_to_block (&se.pre, &argse.pre);
     939                 :          28 :           desc = argse.expr;
     940                 :          28 :           *ar = ar2;
     941                 :             : 
     942                 :          28 :           extent = build_one_cst (gfc_array_index_type);
     943                 :          98 :           for (i = 0; i < ar->dimen; i++)
     944                 :             :             {
     945                 :          42 :               gfc_init_se (&argse, NULL);
     946                 :          42 :               gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
     947                 :          42 :               gfc_add_block_to_block (&argse.pre, &argse.pre);
     948                 :          42 :               lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
     949                 :          42 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
     950                 :          42 :                                      TREE_TYPE (lbound), argse.expr, lbound);
     951                 :          42 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
     952                 :          42 :                                      TREE_TYPE (tmp), extent, tmp);
     953                 :          42 :               index = fold_build2_loc (input_location, PLUS_EXPR,
     954                 :          42 :                                        TREE_TYPE (tmp), index, tmp);
     955                 :          42 :               if (i < ar->dimen - 1)
     956                 :             :                 {
     957                 :          14 :                   ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
     958                 :          14 :                   tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
     959                 :          14 :                   extent = fold_build2_loc (input_location, MULT_EXPR,
     960                 :          14 :                                             TREE_TYPE (tmp), extent, tmp);
     961                 :             :                 }
     962                 :             :             }
     963                 :             :         }
     964                 :             : 
     965                 :             :       /* errmsg.  */
     966                 :          44 :       if (code->expr3)
     967                 :             :         {
     968                 :           0 :           gfc_init_se (&argse, NULL);
     969                 :           0 :           argse.want_pointer = 1;
     970                 :           0 :           gfc_conv_expr (&argse, code->expr3);
     971                 :           0 :           gfc_add_block_to_block (&se.pre, &argse.pre);
     972                 :           0 :           errmsg = argse.expr;
     973                 :           0 :           errmsg_len = fold_convert (size_type_node, argse.string_length);
     974                 :             :         }
     975                 :             :       else
     976                 :             :         {
     977                 :          44 :           errmsg = null_pointer_node;
     978                 :          44 :           errmsg_len = build_zero_cst (size_type_node);
     979                 :             :         }
     980                 :             : 
     981                 :          44 :       if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
     982                 :             :         {
     983                 :           0 :           stat2 = stat;
     984                 :           0 :           stat = gfc_create_var (integer_type_node, "stat");
     985                 :             :         }
     986                 :             : 
     987                 :          44 :       if (lock_acquired != null_pointer_node
     988                 :          44 :           && TREE_TYPE (lock_acquired) != integer_type_node)
     989                 :             :         {
     990                 :           8 :           lock_acquired2 = lock_acquired;
     991                 :           8 :           lock_acquired = gfc_create_var (integer_type_node, "acquired");
     992                 :             :         }
     993                 :             : 
     994                 :          44 :       index = fold_convert (size_type_node, index);
     995                 :          44 :       if (op == EXEC_LOCK)
     996                 :          22 :         tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_lock, 7,
     997                 :             :                                    token, index, image_index,
     998                 :          22 :                                    lock_acquired != null_pointer_node
     999                 :           8 :                                    ? gfc_build_addr_expr (NULL, lock_acquired)
    1000                 :             :                                    : lock_acquired,
    1001                 :          22 :                                    stat != null_pointer_node
    1002                 :           8 :                                    ? gfc_build_addr_expr (NULL, stat) : stat,
    1003                 :             :                                    errmsg, errmsg_len);
    1004                 :             :       else
    1005                 :          22 :         tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_unlock, 6,
    1006                 :             :                                    token, index, image_index,
    1007                 :          22 :                                    stat != null_pointer_node
    1008                 :           8 :                                    ? gfc_build_addr_expr (NULL, stat) : stat,
    1009                 :             :                                    errmsg, errmsg_len);
    1010                 :          44 :       gfc_add_expr_to_block (&se.pre, tmp);
    1011                 :             : 
    1012                 :             :       /* It guarantees memory consistency within the same segment */
    1013                 :          44 :       tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
    1014                 :          44 :       tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1015                 :             :                         gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    1016                 :             :                         tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    1017                 :          44 :       ASM_VOLATILE_P (tmp) = 1;
    1018                 :             : 
    1019                 :          44 :       gfc_add_expr_to_block (&se.pre, tmp);
    1020                 :             : 
    1021                 :          44 :       if (stat2 != NULL_TREE)
    1022                 :           0 :         gfc_add_modify (&se.pre, stat2,
    1023                 :           0 :                         fold_convert (TREE_TYPE (stat2), stat));
    1024                 :             : 
    1025                 :          44 :       if (lock_acquired2 != NULL_TREE)
    1026                 :           8 :         gfc_add_modify (&se.pre, lock_acquired2,
    1027                 :           8 :                         fold_convert (TREE_TYPE (lock_acquired2),
    1028                 :             :                                       lock_acquired));
    1029                 :             : 
    1030                 :          44 :       return gfc_finish_block (&se.pre);
    1031                 :             :     }
    1032                 :             : 
    1033                 :          18 :   if (stat != NULL_TREE)
    1034                 :          12 :     gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
    1035                 :             : 
    1036                 :          18 :   if (lock_acquired != NULL_TREE)
    1037                 :           6 :     gfc_add_modify (&se.pre, lock_acquired,
    1038                 :           6 :                     fold_convert (TREE_TYPE (lock_acquired),
    1039                 :             :                                   boolean_true_node));
    1040                 :             : 
    1041                 :          18 :   return gfc_finish_block (&se.pre);
    1042                 :             : }
    1043                 :             : 
    1044                 :             : tree
    1045                 :          39 : gfc_trans_event_post_wait (gfc_code *code, gfc_exec_op op)
    1046                 :             : {
    1047                 :          39 :   gfc_se se, argse;
    1048                 :          39 :   tree stat = NULL_TREE, stat2 = NULL_TREE;
    1049                 :          39 :   tree until_count = NULL_TREE;
    1050                 :             : 
    1051                 :          39 :   if (code->expr2)
    1052                 :             :     {
    1053                 :           8 :       gcc_assert (code->expr2->expr_type == EXPR_VARIABLE);
    1054                 :           8 :       gfc_init_se (&argse, NULL);
    1055                 :           8 :       gfc_conv_expr_val (&argse, code->expr2);
    1056                 :           8 :       stat = argse.expr;
    1057                 :             :     }
    1058                 :          31 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
    1059                 :          16 :     stat = null_pointer_node;
    1060                 :             : 
    1061                 :          39 :   if (code->expr4)
    1062                 :             :     {
    1063                 :          12 :       gfc_init_se (&argse, NULL);
    1064                 :          12 :       gfc_conv_expr_val (&argse, code->expr4);
    1065                 :          12 :       until_count = fold_convert (integer_type_node, argse.expr);
    1066                 :             :     }
    1067                 :             :   else
    1068                 :          27 :     until_count = integer_one_node;
    1069                 :             : 
    1070                 :          39 :   if (flag_coarray != GFC_FCOARRAY_LIB)
    1071                 :             :     {
    1072                 :          19 :       gfc_start_block (&se.pre);
    1073                 :          19 :       gfc_init_se (&argse, NULL);
    1074                 :          19 :       gfc_conv_expr_val (&argse, code->expr1);
    1075                 :             : 
    1076                 :          19 :       if (op == EXEC_EVENT_POST)
    1077                 :          22 :         gfc_add_modify (&se.pre, argse.expr,
    1078                 :             :                         fold_build2_loc (input_location, PLUS_EXPR,
    1079                 :          11 :                                 TREE_TYPE (argse.expr), argse.expr,
    1080                 :          11 :                                 build_int_cst (TREE_TYPE (argse.expr), 1)));
    1081                 :             :       else
    1082                 :          16 :         gfc_add_modify (&se.pre, argse.expr,
    1083                 :             :                         fold_build2_loc (input_location, MINUS_EXPR,
    1084                 :           8 :                                 TREE_TYPE (argse.expr), argse.expr,
    1085                 :           8 :                                 fold_convert (TREE_TYPE (argse.expr),
    1086                 :             :                                               until_count)));
    1087                 :          19 :       if (stat != NULL_TREE)
    1088                 :           4 :         gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
    1089                 :             : 
    1090                 :          19 :       return gfc_finish_block (&se.pre);
    1091                 :             :     }
    1092                 :             : 
    1093                 :          20 :   gfc_start_block (&se.pre);
    1094                 :          20 :   tree tmp, token, image_index, errmsg, errmsg_len;
    1095                 :          20 :   tree index = build_zero_cst (gfc_array_index_type);
    1096                 :          20 :   tree caf_decl = gfc_get_tree_for_caf_expr (code->expr1);
    1097                 :             : 
    1098                 :          20 :   if (code->expr1->symtree->n.sym->ts.type != BT_DERIVED
    1099                 :          20 :       || code->expr1->symtree->n.sym->ts.u.derived->from_intmod
    1100                 :             :          != INTMOD_ISO_FORTRAN_ENV
    1101                 :          20 :       || code->expr1->symtree->n.sym->ts.u.derived->intmod_sym_id
    1102                 :             :          != ISOFORTRAN_EVENT_TYPE)
    1103                 :             :     {
    1104                 :           0 :       gfc_error ("Sorry, the event component of derived type at %L is not "
    1105                 :             :                  "yet supported", &code->expr1->where);
    1106                 :           0 :       return NULL_TREE;
    1107                 :             :     }
    1108                 :             : 
    1109                 :          20 :   gfc_init_se (&argse, NULL);
    1110                 :          20 :   gfc_get_caf_token_offset (&argse, &token, NULL, caf_decl, NULL_TREE,
    1111                 :             :                             code->expr1);
    1112                 :          20 :   gfc_add_block_to_block (&se.pre, &argse.pre);
    1113                 :             : 
    1114                 :          20 :   if (gfc_is_coindexed (code->expr1))
    1115                 :           6 :     image_index = gfc_caf_get_image_index (&se.pre, code->expr1, caf_decl);
    1116                 :             :   else
    1117                 :          14 :     image_index = integer_zero_node;
    1118                 :             : 
    1119                 :             :   /* For arrays, obtain the array index.  */
    1120                 :          20 :   if (gfc_expr_attr (code->expr1).dimension)
    1121                 :             :     {
    1122                 :           7 :       tree desc, tmp, extent, lbound, ubound;
    1123                 :           7 :       gfc_array_ref *ar, ar2;
    1124                 :           7 :       int i;
    1125                 :             : 
    1126                 :             :       /* TODO: Extend this, once DT components are supported.  */
    1127                 :           7 :       ar = &code->expr1->ref->u.ar;
    1128                 :           7 :       ar2 = *ar;
    1129                 :           7 :       memset (ar, '\0', sizeof (*ar));
    1130                 :           7 :       ar->as = ar2.as;
    1131                 :           7 :       ar->type = AR_FULL;
    1132                 :             : 
    1133                 :           7 :       gfc_init_se (&argse, NULL);
    1134                 :           7 :       argse.descriptor_only = 1;
    1135                 :           7 :       gfc_conv_expr_descriptor (&argse, code->expr1);
    1136                 :           7 :       gfc_add_block_to_block (&se.pre, &argse.pre);
    1137                 :           7 :       desc = argse.expr;
    1138                 :           7 :       *ar = ar2;
    1139                 :             : 
    1140                 :           7 :       extent = build_one_cst (gfc_array_index_type);
    1141                 :          21 :       for (i = 0; i < ar->dimen; i++)
    1142                 :             :         {
    1143                 :           7 :           gfc_init_se (&argse, NULL);
    1144                 :           7 :           gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
    1145                 :           7 :           gfc_add_block_to_block (&argse.pre, &argse.pre);
    1146                 :           7 :           lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
    1147                 :           7 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    1148                 :           7 :                                  TREE_TYPE (lbound), argse.expr, lbound);
    1149                 :           7 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    1150                 :           7 :                                  TREE_TYPE (tmp), extent, tmp);
    1151                 :           7 :           index = fold_build2_loc (input_location, PLUS_EXPR,
    1152                 :           7 :                                    TREE_TYPE (tmp), index, tmp);
    1153                 :           7 :           if (i < ar->dimen - 1)
    1154                 :             :             {
    1155                 :           0 :               ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
    1156                 :           0 :               tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
    1157                 :           0 :               extent = fold_build2_loc (input_location, MULT_EXPR,
    1158                 :           0 :                                         TREE_TYPE (tmp), extent, tmp);
    1159                 :             :             }
    1160                 :             :         }
    1161                 :             :     }
    1162                 :             : 
    1163                 :             :   /* errmsg.  */
    1164                 :          20 :   if (code->expr3)
    1165                 :             :     {
    1166                 :           0 :       gfc_init_se (&argse, NULL);
    1167                 :           0 :       argse.want_pointer = 1;
    1168                 :           0 :       gfc_conv_expr (&argse, code->expr3);
    1169                 :           0 :       gfc_add_block_to_block (&se.pre, &argse.pre);
    1170                 :           0 :       errmsg = argse.expr;
    1171                 :           0 :       errmsg_len = fold_convert (size_type_node, argse.string_length);
    1172                 :             :     }
    1173                 :             :   else
    1174                 :             :     {
    1175                 :          20 :       errmsg = null_pointer_node;
    1176                 :          20 :       errmsg_len = build_zero_cst (size_type_node);
    1177                 :             :     }
    1178                 :             : 
    1179                 :          20 :   if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
    1180                 :             :     {
    1181                 :           0 :       stat2 = stat;
    1182                 :           0 :       stat = gfc_create_var (integer_type_node, "stat");
    1183                 :             :     }
    1184                 :             : 
    1185                 :          20 :   index = fold_convert (size_type_node, index);
    1186                 :          20 :   if (op == EXEC_EVENT_POST)
    1187                 :          12 :     tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_post, 6,
    1188                 :             :                                token, index, image_index,
    1189                 :          12 :                                stat != null_pointer_node
    1190                 :           2 :                                ? gfc_build_addr_expr (NULL, stat) : stat,
    1191                 :             :                                errmsg, errmsg_len);
    1192                 :             :   else
    1193                 :           8 :     tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_wait, 6,
    1194                 :             :                                token, index, until_count,
    1195                 :           8 :                                stat != null_pointer_node
    1196                 :           2 :                                ? gfc_build_addr_expr (NULL, stat) : stat,
    1197                 :             :                                errmsg, errmsg_len);
    1198                 :          20 :   gfc_add_expr_to_block (&se.pre, tmp);
    1199                 :             : 
    1200                 :             :   /* It guarantees memory consistency within the same segment */
    1201                 :          20 :   tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
    1202                 :          20 :   tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1203                 :             :                     gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    1204                 :             :                     tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    1205                 :          20 :   ASM_VOLATILE_P (tmp) = 1;
    1206                 :          20 :   gfc_add_expr_to_block (&se.pre, tmp);
    1207                 :             : 
    1208                 :          20 :   if (stat2 != NULL_TREE)
    1209                 :           0 :     gfc_add_modify (&se.pre, stat2, fold_convert (TREE_TYPE (stat2), stat));
    1210                 :             : 
    1211                 :          20 :   return gfc_finish_block (&se.pre);
    1212                 :             : }
    1213                 :             : 
    1214                 :             : tree
    1215                 :         716 : gfc_trans_sync (gfc_code *code, gfc_exec_op type)
    1216                 :             : {
    1217                 :         716 :   gfc_se se, argse;
    1218                 :         716 :   tree tmp;
    1219                 :         716 :   tree images = NULL_TREE, stat = NULL_TREE,
    1220                 :         716 :        errmsg = NULL_TREE, errmsglen = NULL_TREE;
    1221                 :             : 
    1222                 :             :   /* Short cut: For single images without bound checking or without STAT=,
    1223                 :             :      return early. (ERRMSG= is always untouched for -fcoarray=single.)  */
    1224                 :         716 :   if (!code->expr2 && !(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    1225                 :         618 :       && flag_coarray != GFC_FCOARRAY_LIB)
    1226                 :             :     return NULL_TREE;
    1227                 :             : 
    1228                 :         391 :   gfc_init_se (&se, NULL);
    1229                 :         391 :   gfc_start_block (&se.pre);
    1230                 :             : 
    1231                 :         391 :   if (code->expr1 && code->expr1->rank == 0)
    1232                 :             :     {
    1233                 :          13 :       gfc_init_se (&argse, NULL);
    1234                 :          13 :       gfc_conv_expr_val (&argse, code->expr1);
    1235                 :          13 :       images = argse.expr;
    1236                 :             :     }
    1237                 :             : 
    1238                 :         391 :   if (code->expr2)
    1239                 :             :     {
    1240                 :          71 :       gcc_assert (code->expr2->expr_type == EXPR_VARIABLE
    1241                 :             :                   || code->expr2->expr_type == EXPR_FUNCTION);
    1242                 :          71 :       gfc_init_se (&argse, NULL);
    1243                 :          71 :       gfc_conv_expr_val (&argse, code->expr2);
    1244                 :          71 :       stat = argse.expr;
    1245                 :             :     }
    1246                 :             :   else
    1247                 :         320 :     stat = null_pointer_node;
    1248                 :             : 
    1249                 :         391 :   if (code->expr3 && flag_coarray == GFC_FCOARRAY_LIB)
    1250                 :             :     {
    1251                 :          14 :       gcc_assert (code->expr3->expr_type == EXPR_VARIABLE
    1252                 :             :                   || code->expr3->expr_type == EXPR_FUNCTION);
    1253                 :          14 :       gfc_init_se (&argse, NULL);
    1254                 :          14 :       argse.want_pointer = 1;
    1255                 :          14 :       gfc_conv_expr (&argse, code->expr3);
    1256                 :          14 :       gfc_conv_string_parameter (&argse);
    1257                 :          14 :       errmsg = gfc_build_addr_expr (NULL, argse.expr);
    1258                 :          14 :       errmsglen = fold_convert (size_type_node, argse.string_length);
    1259                 :             :     }
    1260                 :         377 :   else if (flag_coarray == GFC_FCOARRAY_LIB)
    1261                 :             :     {
    1262                 :         317 :       errmsg = null_pointer_node;
    1263                 :         317 :       errmsglen = build_int_cst (size_type_node, 0);
    1264                 :             :     }
    1265                 :             : 
    1266                 :             :   /* Check SYNC IMAGES(imageset) for valid image index.
    1267                 :             :      FIXME: Add a check for image-set arrays.  */
    1268                 :         391 :   if (code->expr1 && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    1269                 :          13 :       && code->expr1->rank == 0)
    1270                 :             :     {
    1271                 :          11 :       tree images2 = fold_convert (integer_type_node, images);
    1272                 :          11 :       tree cond;
    1273                 :          11 :       if (flag_coarray != GFC_FCOARRAY_LIB)
    1274                 :           4 :         cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1275                 :           4 :                                 images, build_int_cst (TREE_TYPE (images), 1));
    1276                 :             :       else
    1277                 :             :         {
    1278                 :           7 :           tree cond2;
    1279                 :           7 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
    1280                 :             :                                      2, integer_zero_node,
    1281                 :           7 :                                      build_int_cst (integer_type_node, -1));
    1282                 :           7 :           cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    1283                 :             :                                   images2, tmp);
    1284                 :           7 :           cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    1285                 :             :                                    images,
    1286                 :           7 :                                    build_int_cst (TREE_TYPE (images), 1));
    1287                 :           7 :           cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    1288                 :             :                                   logical_type_node, cond, cond2);
    1289                 :             :         }
    1290                 :          11 :       gfc_trans_runtime_check (true, false, cond, &se.pre,
    1291                 :          11 :                                &code->expr1->where, "Invalid image number "
    1292                 :             :                                "%d in SYNC IMAGES", images2);
    1293                 :             :     }
    1294                 :             : 
    1295                 :             :   /* Per F2008, 8.5.1, a SYNC MEMORY is implied by calling the
    1296                 :             :      image control statements SYNC IMAGES and SYNC ALL.  */
    1297                 :         391 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    1298                 :             :     {
    1299                 :         331 :       tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
    1300                 :         331 :       tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1301                 :             :                         gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    1302                 :             :                         tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    1303                 :         331 :       ASM_VOLATILE_P (tmp) = 1;
    1304                 :         331 :       gfc_add_expr_to_block (&se.pre, tmp);
    1305                 :             :     }
    1306                 :             : 
    1307                 :         391 :   if (flag_coarray != GFC_FCOARRAY_LIB)
    1308                 :             :     {
    1309                 :             :       /* Set STAT to zero.  */
    1310                 :          60 :       if (code->expr2)
    1311                 :          48 :         gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
    1312                 :             :     }
    1313                 :         331 :   else if (type == EXEC_SYNC_ALL || type == EXEC_SYNC_MEMORY)
    1314                 :             :     {
    1315                 :             :       /* SYNC ALL           =>   stat == null_pointer_node
    1316                 :             :          SYNC ALL(stat=s)   =>   stat has an integer type
    1317                 :             : 
    1318                 :             :          If "stat" has the wrong integer type, use a temp variable of
    1319                 :             :          the right type and later cast the result back into "stat".  */
    1320                 :         314 :       if (stat == null_pointer_node || TREE_TYPE (stat) == integer_type_node)
    1321                 :             :         {
    1322                 :         314 :           if (TREE_TYPE (stat) == integer_type_node)
    1323                 :          19 :             stat = gfc_build_addr_expr (NULL, stat);
    1324                 :             : 
    1325                 :         314 :           if(type == EXEC_SYNC_MEMORY)
    1326                 :          14 :             tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_memory,
    1327                 :             :                                        3, stat, errmsg, errmsglen);
    1328                 :             :           else
    1329                 :         300 :             tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
    1330                 :             :                                        3, stat, errmsg, errmsglen);
    1331                 :             : 
    1332                 :         314 :           gfc_add_expr_to_block (&se.pre, tmp);
    1333                 :             :         }
    1334                 :             :       else
    1335                 :             :         {
    1336                 :           0 :           tree tmp_stat = gfc_create_var (integer_type_node, "stat");
    1337                 :             : 
    1338                 :           0 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
    1339                 :             :                                      3, gfc_build_addr_expr (NULL, tmp_stat),
    1340                 :             :                                      errmsg, errmsglen);
    1341                 :           0 :           gfc_add_expr_to_block (&se.pre, tmp);
    1342                 :             : 
    1343                 :           0 :           gfc_add_modify (&se.pre, stat,
    1344                 :           0 :                           fold_convert (TREE_TYPE (stat), tmp_stat));
    1345                 :             :         }
    1346                 :             :     }
    1347                 :             :   else
    1348                 :             :     {
    1349                 :          17 :       tree len;
    1350                 :             : 
    1351                 :          17 :       gcc_assert (type == EXEC_SYNC_IMAGES);
    1352                 :             : 
    1353                 :          17 :       if (!code->expr1)
    1354                 :             :         {
    1355                 :           6 :           len = build_int_cst (integer_type_node, -1);
    1356                 :           6 :           images = null_pointer_node;
    1357                 :             :         }
    1358                 :          11 :       else if (code->expr1->rank == 0)
    1359                 :             :         {
    1360                 :           9 :           len = integer_one_node;
    1361                 :           9 :           images = gfc_build_addr_expr (NULL_TREE, images);
    1362                 :             :         }
    1363                 :             :       else
    1364                 :             :         {
    1365                 :             :           /* FIXME.  */
    1366                 :           2 :           if (code->expr1->ts.kind != gfc_c_int_kind)
    1367                 :           0 :             gfc_fatal_error ("Sorry, only support for integer kind %d "
    1368                 :             :                              "implemented for image-set at %L",
    1369                 :             :                              gfc_c_int_kind, &code->expr1->where);
    1370                 :             : 
    1371                 :           2 :           gfc_conv_array_parameter (&se, code->expr1, true, NULL, NULL, &len);
    1372                 :           2 :           images = se.expr;
    1373                 :             : 
    1374                 :           2 :           tmp = gfc_typenode_for_spec (&code->expr1->ts);
    1375                 :           2 :           if (GFC_ARRAY_TYPE_P (tmp) || GFC_DESCRIPTOR_TYPE_P (tmp))
    1376                 :           0 :             tmp = gfc_get_element_type (tmp);
    1377                 :             : 
    1378                 :           4 :           len = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    1379                 :           2 :                                  TREE_TYPE (len), len,
    1380                 :           2 :                                  fold_convert (TREE_TYPE (len),
    1381                 :             :                                                TYPE_SIZE_UNIT (tmp)));
    1382                 :           2 :           len = fold_convert (integer_type_node, len);
    1383                 :             :         }
    1384                 :             : 
    1385                 :             :       /* SYNC IMAGES(imgs)        => stat == null_pointer_node
    1386                 :             :          SYNC IMAGES(imgs,stat=s) => stat has an integer type
    1387                 :             : 
    1388                 :             :          If "stat" has the wrong integer type, use a temp variable of
    1389                 :             :          the right type and later cast the result back into "stat".  */
    1390                 :          17 :       if (stat == null_pointer_node || TREE_TYPE (stat) == integer_type_node)
    1391                 :             :         {
    1392                 :          17 :           if (TREE_TYPE (stat) == integer_type_node)
    1393                 :           4 :             stat = gfc_build_addr_expr (NULL, stat);
    1394                 :             : 
    1395                 :          17 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_images,
    1396                 :             :                                      5, fold_convert (integer_type_node, len),
    1397                 :             :                                      images, stat, errmsg, errmsglen);
    1398                 :          17 :           gfc_add_expr_to_block (&se.pre, tmp);
    1399                 :             :         }
    1400                 :             :       else
    1401                 :             :         {
    1402                 :           0 :           tree tmp_stat = gfc_create_var (integer_type_node, "stat");
    1403                 :             : 
    1404                 :           0 :           tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_images,
    1405                 :             :                                      5, fold_convert (integer_type_node, len),
    1406                 :             :                                      images, gfc_build_addr_expr (NULL, tmp_stat),
    1407                 :             :                                      errmsg, errmsglen);
    1408                 :           0 :           gfc_add_expr_to_block (&se.pre, tmp);
    1409                 :             : 
    1410                 :           0 :           gfc_add_modify (&se.pre, stat,
    1411                 :           0 :                           fold_convert (TREE_TYPE (stat), tmp_stat));
    1412                 :             :         }
    1413                 :             :     }
    1414                 :             : 
    1415                 :         391 :   return gfc_finish_block (&se.pre);
    1416                 :             : }
    1417                 :             : 
    1418                 :             : 
    1419                 :             : /* Generate GENERIC for the IF construct. This function also deals with
    1420                 :             :    the simple IF statement, because the front end translates the IF
    1421                 :             :    statement into an IF construct.
    1422                 :             : 
    1423                 :             :    We translate:
    1424                 :             : 
    1425                 :             :         IF (cond) THEN
    1426                 :             :            then_clause
    1427                 :             :         ELSEIF (cond2)
    1428                 :             :            elseif_clause
    1429                 :             :         ELSE
    1430                 :             :            else_clause
    1431                 :             :         ENDIF
    1432                 :             : 
    1433                 :             :    into:
    1434                 :             : 
    1435                 :             :         pre_cond_s;
    1436                 :             :         if (cond_s)
    1437                 :             :           {
    1438                 :             :             then_clause;
    1439                 :             :           }
    1440                 :             :         else
    1441                 :             :           {
    1442                 :             :             pre_cond_s
    1443                 :             :             if (cond_s)
    1444                 :             :               {
    1445                 :             :                 elseif_clause
    1446                 :             :               }
    1447                 :             :             else
    1448                 :             :               {
    1449                 :             :                 else_clause;
    1450                 :             :               }
    1451                 :             :           }
    1452                 :             : 
    1453                 :             :    where COND_S is the simplified version of the predicate. PRE_COND_S
    1454                 :             :    are the pre side-effects produced by the translation of the
    1455                 :             :    conditional.
    1456                 :             :    We need to build the chain recursively otherwise we run into
    1457                 :             :    problems with folding incomplete statements.  */
    1458                 :             : 
    1459                 :             : static tree
    1460                 :      205296 : gfc_trans_if_1 (gfc_code * code)
    1461                 :             : {
    1462                 :      205296 :   gfc_se if_se;
    1463                 :      205296 :   tree stmt, elsestmt;
    1464                 :      205296 :   locus saved_loc;
    1465                 :      205296 :   location_t loc;
    1466                 :             : 
    1467                 :             :   /* Check for an unconditional ELSE clause.  */
    1468                 :      205296 :   if (!code->expr1)
    1469                 :        6463 :     return gfc_trans_code (code->next);
    1470                 :             : 
    1471                 :             :   /* Initialize a statement builder for each block. Puts in NULL_TREEs.  */
    1472                 :      198833 :   gfc_init_se (&if_se, NULL);
    1473                 :      198833 :   gfc_start_block (&if_se.pre);
    1474                 :             : 
    1475                 :             :   /* Calculate the IF condition expression.  */
    1476                 :      198833 :   if (code->expr1->where.lb)
    1477                 :             :     {
    1478                 :      198833 :       gfc_save_backend_locus (&saved_loc);
    1479                 :      198833 :       gfc_set_backend_locus (&code->expr1->where);
    1480                 :             :     }
    1481                 :             : 
    1482                 :      198833 :   gfc_conv_expr_val (&if_se, code->expr1);
    1483                 :             : 
    1484                 :      198833 :   if (code->expr1->where.lb)
    1485                 :      198833 :     gfc_restore_backend_locus (&saved_loc);
    1486                 :             : 
    1487                 :             :   /* Translate the THEN clause.  */
    1488                 :      198833 :   stmt = gfc_trans_code (code->next);
    1489                 :             : 
    1490                 :             :   /* Translate the ELSE clause.  */
    1491                 :      198833 :   if (code->block)
    1492                 :        6705 :     elsestmt = gfc_trans_if_1 (code->block);
    1493                 :             :   else
    1494                 :      192128 :     elsestmt = build_empty_stmt (input_location);
    1495                 :             : 
    1496                 :             :   /* Build the condition expression and add it to the condition block.  */
    1497                 :      198833 :   loc = code->expr1->where.lb ? gfc_get_location (&code->expr1->where)
    1498                 :             :                               : input_location;
    1499                 :      198833 :   stmt = fold_build3_loc (loc, COND_EXPR, void_type_node, if_se.expr, stmt,
    1500                 :             :                           elsestmt);
    1501                 :             : 
    1502                 :      198833 :   gfc_add_expr_to_block (&if_se.pre, stmt);
    1503                 :             : 
    1504                 :             :   /* Finish off this statement.  */
    1505                 :      198833 :   return gfc_finish_block (&if_se.pre);
    1506                 :             : }
    1507                 :             : 
    1508                 :             : tree
    1509                 :      198591 : gfc_trans_if (gfc_code * code)
    1510                 :             : {
    1511                 :      198591 :   stmtblock_t body;
    1512                 :      198591 :   tree exit_label;
    1513                 :             : 
    1514                 :             :   /* Create exit label so it is available for trans'ing the body code.  */
    1515                 :      198591 :   exit_label = gfc_build_label_decl (NULL_TREE);
    1516                 :      198591 :   code->exit_label = exit_label;
    1517                 :             : 
    1518                 :             :   /* Translate the actual code in code->block.  */
    1519                 :      198591 :   gfc_init_block (&body);
    1520                 :      198591 :   gfc_add_expr_to_block (&body, gfc_trans_if_1 (code->block));
    1521                 :             : 
    1522                 :             :   /* Add exit label.  */
    1523                 :      198591 :   gfc_add_expr_to_block (&body, build1_v (LABEL_EXPR, exit_label));
    1524                 :             : 
    1525                 :      198591 :   return gfc_finish_block (&body);
    1526                 :             : }
    1527                 :             : 
    1528                 :             : 
    1529                 :             : /* Translate an arithmetic IF expression.
    1530                 :             : 
    1531                 :             :    IF (cond) label1, label2, label3 translates to
    1532                 :             : 
    1533                 :             :     if (cond <= 0)
    1534                 :             :       {
    1535                 :             :         if (cond < 0)
    1536                 :             :           goto label1;
    1537                 :             :         else // cond == 0
    1538                 :             :           goto label2;
    1539                 :             :       }
    1540                 :             :     else // cond > 0
    1541                 :             :       goto label3;
    1542                 :             : 
    1543                 :             :    An optimized version can be generated in case of equal labels.
    1544                 :             :    E.g., if label1 is equal to label2, we can translate it to
    1545                 :             : 
    1546                 :             :     if (cond <= 0)
    1547                 :             :       goto label1;
    1548                 :             :     else
    1549                 :             :       goto label3;
    1550                 :             : */
    1551                 :             : 
    1552                 :             : tree
    1553                 :          64 : gfc_trans_arithmetic_if (gfc_code * code)
    1554                 :             : {
    1555                 :          64 :   gfc_se se;
    1556                 :          64 :   tree tmp;
    1557                 :          64 :   tree branch1;
    1558                 :          64 :   tree branch2;
    1559                 :          64 :   tree zero;
    1560                 :             : 
    1561                 :             :   /* Start a new block.  */
    1562                 :          64 :   gfc_init_se (&se, NULL);
    1563                 :          64 :   gfc_start_block (&se.pre);
    1564                 :             : 
    1565                 :             :   /* Pre-evaluate COND.  */
    1566                 :          64 :   gfc_conv_expr_val (&se, code->expr1);
    1567                 :          64 :   se.expr = gfc_evaluate_now (se.expr, &se.pre);
    1568                 :             : 
    1569                 :             :   /* Build something to compare with.  */
    1570                 :          64 :   zero = gfc_build_const (TREE_TYPE (se.expr), integer_zero_node);
    1571                 :             : 
    1572                 :          64 :   if (code->label1->value != code->label2->value)
    1573                 :             :     {
    1574                 :             :       /* If (cond < 0) take branch1 else take branch2.
    1575                 :             :          First build jumps to the COND .LT. 0 and the COND .EQ. 0 cases.  */
    1576                 :          49 :       branch1 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
    1577                 :          49 :       branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label2));
    1578                 :             : 
    1579                 :          49 :       if (code->label1->value != code->label3->value)
    1580                 :          36 :         tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    1581                 :             :                                se.expr, zero);
    1582                 :             :       else
    1583                 :          13 :         tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1584                 :             :                                se.expr, zero);
    1585                 :             : 
    1586                 :          49 :       branch1 = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    1587                 :             :                                  tmp, branch1, branch2);
    1588                 :             :     }
    1589                 :             :   else
    1590                 :          15 :     branch1 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
    1591                 :             : 
    1592                 :          64 :   if (code->label1->value != code->label3->value
    1593                 :          45 :       && code->label2->value != code->label3->value)
    1594                 :             :     {
    1595                 :             :       /* if (cond <= 0) take branch1 else take branch2.  */
    1596                 :          37 :       branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label3));
    1597                 :          37 :       tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    1598                 :             :                              se.expr, zero);
    1599                 :          37 :       branch1 = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    1600                 :             :                                  tmp, branch1, branch2);
    1601                 :             :     }
    1602                 :             : 
    1603                 :             :   /* Append the COND_EXPR to the evaluation of COND, and return.  */
    1604                 :          64 :   gfc_add_expr_to_block (&se.pre, branch1);
    1605                 :          64 :   return gfc_finish_block (&se.pre);
    1606                 :             : }
    1607                 :             : 
    1608                 :             : 
    1609                 :             : /* Translate a CRITICAL block.  */
    1610                 :             : tree
    1611                 :          21 : gfc_trans_critical (gfc_code *code)
    1612                 :             : {
    1613                 :          21 :   stmtblock_t block;
    1614                 :          21 :   tree tmp, token = NULL_TREE;
    1615                 :             : 
    1616                 :          21 :   gfc_start_block (&block);
    1617                 :             : 
    1618                 :          21 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    1619                 :             :     {
    1620                 :           5 :       tree zero_size = build_zero_cst (size_type_node);
    1621                 :           5 :       token = gfc_get_symbol_decl (code->resolved_sym);
    1622                 :           5 :       token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (token));
    1623                 :           5 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_lock, 7,
    1624                 :             :                                  token, zero_size, integer_one_node,
    1625                 :             :                                  null_pointer_node, null_pointer_node,
    1626                 :             :                                  null_pointer_node, zero_size);
    1627                 :           5 :       gfc_add_expr_to_block (&block, tmp);
    1628                 :             : 
    1629                 :             :       /* It guarantees memory consistency within the same segment */
    1630                 :           5 :       tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
    1631                 :           5 :         tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1632                 :             :                           gfc_build_string_const (1, ""),
    1633                 :             :                           NULL_TREE, NULL_TREE,
    1634                 :             :                           tree_cons (NULL_TREE, tmp, NULL_TREE),
    1635                 :             :                           NULL_TREE);
    1636                 :           5 :       ASM_VOLATILE_P (tmp) = 1;
    1637                 :             : 
    1638                 :           5 :       gfc_add_expr_to_block (&block, tmp);
    1639                 :             :     }
    1640                 :             : 
    1641                 :          21 :   tmp = gfc_trans_code (code->block->next);
    1642                 :          21 :   gfc_add_expr_to_block (&block, tmp);
    1643                 :             : 
    1644                 :          21 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    1645                 :             :     {
    1646                 :           5 :       tree zero_size = build_zero_cst (size_type_node);
    1647                 :           5 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_unlock, 6,
    1648                 :             :                                  token, zero_size, integer_one_node,
    1649                 :             :                                  null_pointer_node, null_pointer_node,
    1650                 :             :                                  zero_size);
    1651                 :           5 :       gfc_add_expr_to_block (&block, tmp);
    1652                 :             : 
    1653                 :             :       /* It guarantees memory consistency within the same segment */
    1654                 :           5 :       tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
    1655                 :           5 :         tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    1656                 :             :                           gfc_build_string_const (1, ""),
    1657                 :             :                           NULL_TREE, NULL_TREE,
    1658                 :             :                           tree_cons (NULL_TREE, tmp, NULL_TREE),
    1659                 :             :                           NULL_TREE);
    1660                 :           5 :       ASM_VOLATILE_P (tmp) = 1;
    1661                 :             : 
    1662                 :           5 :       gfc_add_expr_to_block (&block, tmp);
    1663                 :             :     }
    1664                 :             : 
    1665                 :          21 :   return gfc_finish_block (&block);
    1666                 :             : }
    1667                 :             : 
    1668                 :             : 
    1669                 :             : /* Return true, when the class has a _len component.  */
    1670                 :             : 
    1671                 :             : static bool
    1672                 :         599 : class_has_len_component (gfc_symbol *sym)
    1673                 :             : {
    1674                 :         599 :   gfc_component *comp = sym->ts.u.derived->components;
    1675                 :        1705 :   while (comp)
    1676                 :             :     {
    1677                 :        1460 :       if (strcmp (comp->name, "_len") == 0)
    1678                 :             :         return true;
    1679                 :        1106 :       comp = comp->next;
    1680                 :             :     }
    1681                 :             :   return false;
    1682                 :             : }
    1683                 :             : 
    1684                 :             : 
    1685                 :             : static void
    1686                 :        1666 : copy_descriptor (stmtblock_t *block, tree dst, tree src, int rank)
    1687                 :             : {
    1688                 :        1666 :   int n;
    1689                 :        1666 :   tree dim;
    1690                 :        1666 :   tree tmp;
    1691                 :        1666 :   tree tmp2;
    1692                 :        1666 :   tree size;
    1693                 :        1666 :   tree offset;
    1694                 :             : 
    1695                 :        1666 :   offset = gfc_index_zero_node;
    1696                 :             : 
    1697                 :             :   /* Use memcpy to copy the descriptor. The size is the minimum of
    1698                 :             :      the sizes of 'src' and 'dst'. This avoids a non-trivial conversion.  */
    1699                 :        1666 :   tmp = TYPE_SIZE_UNIT (TREE_TYPE (src));
    1700                 :        1666 :   tmp2 = TYPE_SIZE_UNIT (TREE_TYPE (dst));
    1701                 :        1666 :   size = fold_build2_loc (input_location, MIN_EXPR,
    1702                 :        1666 :                           TREE_TYPE (tmp), tmp, tmp2);
    1703                 :        1666 :   tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    1704                 :        1666 :   tmp = build_call_expr_loc (input_location, tmp, 3,
    1705                 :             :                              gfc_build_addr_expr (NULL_TREE, dst),
    1706                 :             :                              gfc_build_addr_expr (NULL_TREE, src),
    1707                 :             :                              fold_convert (size_type_node, size));
    1708                 :        1666 :   gfc_add_expr_to_block (block, tmp);
    1709                 :             : 
    1710                 :             :   /* Set the offset correctly.  */
    1711                 :        8382 :   for (n = 0; n < rank; n++)
    1712                 :             :     {
    1713                 :        5050 :       dim = gfc_rank_cst[n];
    1714                 :        5050 :       tmp = gfc_conv_descriptor_lbound_get (src, dim);
    1715                 :        5050 :       tmp2 = gfc_conv_descriptor_stride_get (src, dim);
    1716                 :        5050 :       tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    1717                 :             :                              tmp, tmp2);
    1718                 :        5050 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
    1719                 :        5050 :                         TREE_TYPE (offset), offset, tmp);
    1720                 :        5050 :       offset = gfc_evaluate_now (offset, block);
    1721                 :             :     }
    1722                 :             : 
    1723                 :        1666 :   gfc_conv_descriptor_offset_set (block, dst, offset);
    1724                 :        1666 : }
    1725                 :             : 
    1726                 :             : 
    1727                 :             : /* Do proper initialization for ASSOCIATE names.  */
    1728                 :             : 
    1729                 :             : static void
    1730                 :        6435 : trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block)
    1731                 :             : {
    1732                 :        6435 :   gfc_expr *e;
    1733                 :        6435 :   tree tmp;
    1734                 :        6435 :   bool class_target;
    1735                 :        6435 :   bool unlimited;
    1736                 :        6435 :   tree desc;
    1737                 :        6435 :   tree offset;
    1738                 :        6435 :   tree dim;
    1739                 :        6435 :   int n;
    1740                 :        6435 :   tree charlen;
    1741                 :        6435 :   bool need_len_assign;
    1742                 :        6435 :   bool whole_array = true;
    1743                 :        6435 :   gfc_ref *ref;
    1744                 :        6435 :   gfc_symbol *sym2;
    1745                 :             : 
    1746                 :        6435 :   gcc_assert (sym->assoc);
    1747                 :        6435 :   e = sym->assoc->target;
    1748                 :             : 
    1749                 :        6435 :   class_target = (e->expr_type == EXPR_VARIABLE)
    1750                 :        5851 :                   && e->ts.type == BT_CLASS
    1751                 :        8656 :                   && (gfc_is_class_scalar_expr (e)
    1752                 :        1748 :                       || gfc_is_class_array_ref (e, NULL));
    1753                 :             : 
    1754                 :        6435 :   unlimited = UNLIMITED_POLY (e);
    1755                 :             : 
    1756                 :       14080 :   for (ref = e->ref; ref; ref = ref->next)
    1757                 :        7693 :     if (ref->type == REF_ARRAY
    1758                 :        3460 :         && ref->u.ar.type == AR_FULL
    1759                 :        3040 :         && ref->next)
    1760                 :             :       {
    1761                 :             :         whole_array =  false;
    1762                 :             :         break;
    1763                 :             :       }
    1764                 :             : 
    1765                 :             :   /* Assignments to the string length need to be generated, when
    1766                 :             :      ( sym is a char array or
    1767                 :             :        sym has a _len component)
    1768                 :             :      and the associated expression is unlimited polymorphic, which is
    1769                 :             :      not (yet) correctly in 'unlimited', because for an already associated
    1770                 :             :      BT_DERIVED the u-poly flag is not set, i.e.,
    1771                 :             :       __tmp_CHARACTER_0_1 => w => arg
    1772                 :             :        ^ generated temp      ^ from code, the w does not have the u-poly
    1773                 :             :      flag set, where UNLIMITED_POLY(e) expects it.  */
    1774                 :        5381 :   need_len_assign = ((unlimited || (e->ts.type == BT_DERIVED
    1775                 :        2177 :                      && e->ts.u.derived->attr.unlimited_polymorphic))
    1776                 :        1818 :       && (sym->ts.type == BT_CHARACTER
    1777                 :        1175 :           || ((sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED)
    1778                 :         599 :               && class_has_len_component (sym)))
    1779                 :        7432 :       && !sym->attr.select_rank_temporary);
    1780                 :             : 
    1781                 :             :   /* Do a `pointer assignment' with updated descriptor (or assign descriptor
    1782                 :             :      to array temporary) for arrays with either unknown shape or if associating
    1783                 :             :      to a variable. Select rank temporaries need somewhat different treatment
    1784                 :             :      to other associate names and case temporaries. This because the selector
    1785                 :             :      is assumed rank and so the offset in particular has to be changed. Also,
    1786                 :             :      the case temporaries carry both allocatable and target attributes if
    1787                 :             :      present in the selector. This means that an allocatation or change of
    1788                 :             :      association can occur and so has to be dealt with.  */
    1789                 :        6435 :   if (sym->attr.select_rank_temporary)
    1790                 :             :     {
    1791                 :        1329 :       gfc_se se;
    1792                 :        1329 :       tree class_decl = NULL_TREE;
    1793                 :        1329 :       int rank = 0;
    1794                 :        1329 :       bool class_ptr;
    1795                 :             : 
    1796                 :        1329 :       sym2 = e->symtree->n.sym;
    1797                 :        1329 :       gfc_init_se (&se, NULL);
    1798                 :        1329 :       if (e->ts.type == BT_CLASS)
    1799                 :             :         {
    1800                 :             :           /* Go straight to the class data.  */
    1801                 :         124 :           if (sym2->attr.dummy && !sym2->attr.optional)
    1802                 :             :             {
    1803                 :         100 :               class_decl = sym2->backend_decl;
    1804                 :         100 :               if (DECL_LANG_SPECIFIC (class_decl)
    1805                 :         100 :                   && GFC_DECL_SAVED_DESCRIPTOR (class_decl))
    1806                 :           0 :                 class_decl = GFC_DECL_SAVED_DESCRIPTOR (class_decl);
    1807                 :         100 :               if (POINTER_TYPE_P (TREE_TYPE (class_decl)))
    1808                 :         100 :                 class_decl = build_fold_indirect_ref_loc (input_location,
    1809                 :             :                                                           class_decl);
    1810                 :         100 :               gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (class_decl)));
    1811                 :         100 :               se.expr = gfc_class_data_get (class_decl);
    1812                 :             :             }
    1813                 :             :           else
    1814                 :             :             {
    1815                 :          24 :               class_decl = sym2->backend_decl;
    1816                 :          24 :               gfc_conv_expr_descriptor (&se, e);
    1817                 :          24 :               if (POINTER_TYPE_P (TREE_TYPE (se.expr)))
    1818                 :           0 :                 se.expr = build_fold_indirect_ref_loc (input_location,
    1819                 :             :                                                        se.expr);
    1820                 :             :             }
    1821                 :             : 
    1822                 :         124 :           if (CLASS_DATA (sym)->as && CLASS_DATA (sym)->as->rank > 0)
    1823                 :        1329 :             rank = CLASS_DATA (sym)->as->rank;
    1824                 :             :         }
    1825                 :             :       else
    1826                 :             :         {
    1827                 :        1205 :           gfc_conv_expr_descriptor (&se, e);
    1828                 :        1205 :           if (sym->as && sym->as->rank > 0)
    1829                 :        1329 :             rank = sym->as->rank;
    1830                 :             :         }
    1831                 :             : 
    1832                 :        1329 :       desc = sym->backend_decl;
    1833                 :             : 
    1834                 :             :       /* The SELECT TYPE mechanisms turn class temporaries into pointers, which
    1835                 :             :          point to the selector. */
    1836                 :        1329 :       class_ptr = class_decl != NULL_TREE && POINTER_TYPE_P (TREE_TYPE (desc));
    1837                 :         124 :       if (class_ptr)
    1838                 :             :         {
    1839                 :         124 :           tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (desc)), "class");
    1840                 :         124 :           tmp = gfc_build_addr_expr (NULL, tmp);
    1841                 :         124 :           gfc_add_modify (&se.pre, desc, tmp);
    1842                 :             : 
    1843                 :         124 :           tmp = gfc_class_vptr_get (class_decl);
    1844                 :         124 :           gfc_add_modify (&se.pre, gfc_class_vptr_get (desc), tmp);
    1845                 :         124 :           if (UNLIMITED_POLY (sym))
    1846                 :          80 :             gfc_add_modify (&se.pre, gfc_class_len_get (desc),
    1847                 :             :                             gfc_class_len_get (class_decl));
    1848                 :             : 
    1849                 :         124 :           desc = gfc_class_data_get (desc);
    1850                 :             :         }
    1851                 :             : 
    1852                 :             :       /* SELECT RANK temporaries can carry the allocatable and pointer
    1853                 :             :          attributes so the selector descriptor must be copied in and
    1854                 :             :          copied out.  */
    1855                 :        1329 :       if (rank > 0)
    1856                 :        1249 :         copy_descriptor (&se.pre, desc, se.expr, rank);
    1857                 :             :       else
    1858                 :             :         {
    1859                 :          80 :           tmp = gfc_conv_descriptor_data_get (se.expr);
    1860                 :          80 :           gfc_add_modify (&se.pre, desc,
    1861                 :          80 :                           fold_convert (TREE_TYPE (desc), tmp));
    1862                 :             :         }
    1863                 :             : 
    1864                 :             :       /* Deal with associate_name => selector. Class associate names are
    1865                 :             :          treated in the same way as in SELECT TYPE.  */
    1866                 :        1329 :       sym2 = sym->assoc->target->symtree->n.sym;
    1867                 :        1329 :       if (sym2->assoc && sym->assoc->target && sym2->ts.type != BT_CLASS)
    1868                 :             :         {
    1869                 :          54 :           sym2 = sym2->assoc->target->symtree->n.sym;
    1870                 :          54 :           se.expr = sym2->backend_decl;
    1871                 :             : 
    1872                 :          54 :           if (POINTER_TYPE_P (TREE_TYPE (se.expr)))
    1873                 :          54 :             se.expr = build_fold_indirect_ref_loc (input_location,
    1874                 :             :                                                    se.expr);
    1875                 :             :         }
    1876                 :             : 
    1877                 :             :       /* There could have been reallocation.  Copy descriptor back to the
    1878                 :             :          selector and update the offset.  */
    1879                 :        1329 :       if (sym->attr.allocatable || sym->attr.pointer
    1880                 :         963 :           || (sym->ts.type == BT_CLASS
    1881                 :         106 :               && (CLASS_DATA (sym)->attr.allocatable
    1882                 :         106 :                   || CLASS_DATA (sym)->attr.pointer)))
    1883                 :             :         {
    1884                 :         472 :           if (rank > 0)
    1885                 :         417 :             copy_descriptor (&se.post, se.expr, desc, rank);
    1886                 :             :           else
    1887                 :          55 :             gfc_conv_descriptor_data_set (&se.post, se.expr, desc);
    1888                 :             : 
    1889                 :             :           /* The dynamic type could have changed too.  */
    1890                 :         472 :           if (sym->ts.type == BT_CLASS)
    1891                 :             :             {
    1892                 :         124 :               tmp = sym->backend_decl;
    1893                 :         124 :               if (class_ptr)
    1894                 :         124 :                 tmp = build_fold_indirect_ref_loc (input_location, tmp);
    1895                 :         124 :               gfc_add_modify (&se.post, gfc_class_vptr_get (class_decl),
    1896                 :             :                               gfc_class_vptr_get (tmp));
    1897                 :         124 :               if (UNLIMITED_POLY (sym))
    1898                 :          80 :                 gfc_add_modify (&se.post, gfc_class_len_get (class_decl),
    1899                 :             :                                 gfc_class_len_get (tmp));
    1900                 :             :             }
    1901                 :             :         }
    1902                 :             : 
    1903                 :        1329 :       tmp = gfc_finish_block (&se.post);
    1904                 :             : 
    1905                 :        1329 :       gfc_add_init_cleanup (block, gfc_finish_block (&se.pre), tmp);
    1906                 :             :     }
    1907                 :             :   /* Now all the other kinds of associate variable.  */
    1908                 :        5106 :   else if (sym->attr.dimension && !class_target
    1909                 :         538 :            && (sym->as->type == AS_DEFERRED || sym->assoc->variable))
    1910                 :             :     {
    1911                 :         538 :       gfc_se se;
    1912                 :         538 :       tree desc;
    1913                 :         538 :       bool cst_array_ctor;
    1914                 :             : 
    1915                 :         538 :       desc = sym->backend_decl;
    1916                 :        1076 :       cst_array_ctor = e->expr_type == EXPR_ARRAY
    1917                 :          85 :               && gfc_constant_array_constructor_p (e->value.constructor)
    1918                 :         556 :               && e->ts.type != BT_CHARACTER;
    1919                 :             : 
    1920                 :             :       /* If association is to an expression, evaluate it and create temporary.
    1921                 :             :          Otherwise, get descriptor of target for pointer assignment.  */
    1922                 :         538 :       gfc_init_se (&se, NULL);
    1923                 :             : 
    1924                 :         538 :       if (sym->assoc->variable || cst_array_ctor)
    1925                 :             :         {
    1926                 :         338 :           se.direct_byref = 1;
    1927                 :         338 :           se.use_offset = 1;
    1928                 :         338 :           se.expr = desc;
    1929                 :         338 :           GFC_DECL_PTR_ARRAY_P (sym->backend_decl) = 1;
    1930                 :             :         }
    1931                 :             : 
    1932                 :         538 :       gfc_conv_expr_descriptor (&se, e);
    1933                 :             : 
    1934                 :         538 :       if (sym->ts.type == BT_CHARACTER
    1935                 :         213 :           && !sym->attr.select_type_temporary
    1936                 :         213 :           && sym->ts.u.cl->backend_decl
    1937                 :         213 :           && VAR_P (sym->ts.u.cl->backend_decl)
    1938                 :         175 :           && se.string_length != sym->ts.u.cl->backend_decl)
    1939                 :         175 :         gfc_add_modify (&se.pre, sym->ts.u.cl->backend_decl,
    1940                 :         175 :                           fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl),
    1941                 :             :                                         se.string_length));
    1942                 :             : 
    1943                 :             :       /* If we didn't already do the pointer assignment, set associate-name
    1944                 :             :          descriptor to the one generated for the temporary.  */
    1945                 :         538 :       if ((!sym->assoc->variable && !cst_array_ctor)
    1946                 :         338 :           || !whole_array)
    1947                 :             :         {
    1948                 :         200 :           int dim;
    1949                 :             : 
    1950                 :         200 :           if (whole_array)
    1951                 :         200 :             gfc_add_modify (&se.pre, desc, se.expr);
    1952                 :             : 
    1953                 :             :           /* The generated descriptor has lower bound zero (as array
    1954                 :             :              temporary), shift bounds so we get lower bounds of 1.  */
    1955                 :         519 :           for (dim = 0; dim < e->rank; ++dim)
    1956                 :         271 :             gfc_conv_shift_descriptor_lbound (&se.pre, desc,
    1957                 :             :                                               dim, gfc_index_one_node);
    1958                 :             :         }
    1959                 :             : 
    1960                 :             :       /* If this is a subreference array pointer associate name use the
    1961                 :             :          associate variable element size for the value of 'span'.  */
    1962                 :         538 :       if (sym->attr.subref_array_pointer && !se.direct_byref)
    1963                 :             :         {
    1964                 :           6 :           gcc_assert (e->expr_type == EXPR_VARIABLE);
    1965                 :           6 :           tmp = gfc_get_array_span (se.expr, e);
    1966                 :             : 
    1967                 :           6 :           gfc_conv_descriptor_span_set (&se.pre, desc, tmp);
    1968                 :             :         }
    1969                 :             : 
    1970                 :         538 :       if (e->expr_type == EXPR_FUNCTION
    1971                 :          83 :           && sym->ts.type == BT_DERIVED
    1972                 :          42 :           && sym->ts.u.derived
    1973                 :          42 :           && sym->ts.u.derived->attr.pdt_type)
    1974                 :             :         {
    1975                 :           0 :           tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived, se.expr,
    1976                 :           0 :                                          sym->as->rank);
    1977                 :           0 :           gfc_add_expr_to_block (&se.post, tmp);
    1978                 :             :         }
    1979                 :             : 
    1980                 :             :       /* Done, register stuff as init / cleanup code.  */
    1981                 :         538 :       gfc_add_init_cleanup (block, gfc_finish_block (&se.pre),
    1982                 :             :                             gfc_finish_block (&se.post));
    1983                 :         538 :     }
    1984                 :             : 
    1985                 :             :   /* Temporaries, arising from TYPE IS, just need the descriptor of class
    1986                 :             :      arrays to be assigned directly.  */
    1987                 :        4568 :   else if (class_target && sym->attr.dimension
    1988                 :        1166 :            && (sym->ts.type == BT_DERIVED || unlimited))
    1989                 :             :     {
    1990                 :        1166 :       gfc_se se;
    1991                 :             : 
    1992                 :        1166 :       gfc_init_se (&se, NULL);
    1993                 :        1166 :       se.descriptor_only = 1;
    1994                 :             :       /* In a select type the (temporary) associate variable shall point to
    1995                 :             :          a standard fortran array (lower bound == 1), but conv_expr ()
    1996                 :             :          just maps to the input array in the class object, whose lbound may
    1997                 :             :          be arbitrary.  conv_expr_descriptor solves this by inserting a
    1998                 :             :          temporary array descriptor.  */
    1999                 :        1166 :       gfc_conv_expr_descriptor (&se, e);
    2000                 :             : 
    2001                 :        1166 :       gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
    2002                 :             :                   || GFC_ARRAY_TYPE_P (TREE_TYPE (se.expr)));
    2003                 :        1166 :       gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym->backend_decl)));
    2004                 :             : 
    2005                 :        1166 :       if (GFC_ARRAY_TYPE_P (TREE_TYPE (se.expr)))
    2006                 :             :         {
    2007                 :           0 :           if (INDIRECT_REF_P (se.expr))
    2008                 :           0 :             tmp = TREE_OPERAND (se.expr, 0);
    2009                 :             :           else
    2010                 :             :             tmp = se.expr;
    2011                 :             : 
    2012                 :           0 :           gfc_add_modify (&se.pre, sym->backend_decl,
    2013                 :           0 :                           gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (tmp)));
    2014                 :             :         }
    2015                 :             :       else
    2016                 :        1166 :         gfc_add_modify (&se.pre, sym->backend_decl, se.expr);
    2017                 :             : 
    2018                 :        1166 :       if (unlimited)
    2019                 :             :         {
    2020                 :             :           /* Recover the dtype, which has been overwritten by the
    2021                 :             :              assignment from an unlimited polymorphic object.  */
    2022                 :         670 :           tmp = gfc_conv_descriptor_dtype (sym->backend_decl);
    2023                 :         670 :           gfc_add_modify (&se.pre, tmp,
    2024                 :         670 :                           gfc_get_dtype (TREE_TYPE (sym->backend_decl)));
    2025                 :             :         }
    2026                 :             : 
    2027                 :        1166 :       gfc_add_init_cleanup (block, gfc_finish_block (&se.pre),
    2028                 :             :                             gfc_finish_block (&se.post));
    2029                 :        1166 :     }
    2030                 :             : 
    2031                 :             :   /* Do a scalar pointer assignment; this is for scalar variable targets.  */
    2032                 :        3402 :   else if (gfc_is_associate_pointer (sym))
    2033                 :             :     {
    2034                 :        3105 :       gfc_se se;
    2035                 :             : 
    2036                 :        3105 :       gcc_assert (!sym->attr.dimension);
    2037                 :             : 
    2038                 :        3105 :       gfc_init_se (&se, NULL);
    2039                 :             : 
    2040                 :             :       /* Class associate-names come this way because they are
    2041                 :             :          unconditionally associate pointers and the symbol is scalar.  */
    2042                 :        3105 :       if (sym->ts.type == BT_CLASS && e->expr_type == EXPR_FUNCTION)
    2043                 :             :         {
    2044                 :          87 :           gfc_conv_expr (&se, e);
    2045                 :          87 :           se.expr = gfc_evaluate_now (se.expr, &se.pre);
    2046                 :             :           /* Finalize the expression and free if it is allocatable.  */
    2047                 :          87 :           gfc_finalize_tree_expr (&se, NULL, gfc_expr_attr (e), e->rank);
    2048                 :          87 :           gfc_add_block_to_block (&se.post, &se.finalblock);
    2049                 :          87 :           need_len_assign = false;
    2050                 :             :         }
    2051                 :        3018 :       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.dimension)
    2052                 :             :         {
    2053                 :         389 :           tree target_expr;
    2054                 :             :           /* For a class array we need a descriptor for the selector.  */
    2055                 :         389 :           gfc_conv_expr_descriptor (&se, e);
    2056                 :             :           /* Needed to get/set the _len component below.  */
    2057                 :         389 :           target_expr = se.expr;
    2058                 :             : 
    2059                 :             :           /* Obtain a temporary class container for the result.  */
    2060                 :         389 :           gfc_conv_class_to_class (&se, e, sym->ts, false, true, false, false);
    2061                 :         389 :           se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
    2062                 :             : 
    2063                 :             :           /* Set the offset.  */
    2064                 :         389 :           desc = gfc_class_data_get (se.expr);
    2065                 :         389 :           offset = gfc_index_zero_node;
    2066                 :         798 :           for (n = 0; n < e->rank; n++)
    2067                 :             :             {
    2068                 :         409 :               dim = gfc_rank_cst[n];
    2069                 :         409 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
    2070                 :             :                                      gfc_array_index_type,
    2071                 :             :                                      gfc_conv_descriptor_stride_get (desc, dim),
    2072                 :             :                                      gfc_conv_descriptor_lbound_get (desc, dim));
    2073                 :         409 :               offset = fold_build2_loc (input_location, MINUS_EXPR,
    2074                 :             :                                         gfc_array_index_type,
    2075                 :             :                                         offset, tmp);
    2076                 :             :             }
    2077                 :         389 :           if (need_len_assign)
    2078                 :             :             {
    2079                 :         157 :               if (e->symtree
    2080                 :         157 :                   && DECL_LANG_SPECIFIC (e->symtree->n.sym->backend_decl)
    2081                 :          66 :                   && GFC_DECL_SAVED_DESCRIPTOR (e->symtree->n.sym->backend_decl)
    2082                 :         175 :                   && TREE_CODE (target_expr) != COMPONENT_REF)
    2083                 :             :                 /* Use the original class descriptor stored in the saved
    2084                 :             :                    descriptor to get the target_expr.  */
    2085                 :          12 :                 target_expr =
    2086                 :           6 :                     GFC_DECL_SAVED_DESCRIPTOR (e->symtree->n.sym->backend_decl);
    2087                 :             :               else
    2088                 :             :                 /* Strip the _data component from the target_expr.  */
    2089                 :         151 :                 target_expr = TREE_OPERAND (target_expr, 0);
    2090                 :             :               /* Add a reference to the _len comp to the target expr.  */
    2091                 :         157 :               tmp = gfc_class_len_get (target_expr);
    2092                 :             :               /* Get the component-ref for the temp structure's _len comp.  */
    2093                 :         157 :               charlen = gfc_class_len_get (se.expr);
    2094                 :             :               /* Add the assign to the beginning of the block...  */
    2095                 :         157 :               gfc_add_modify (&se.pre, charlen,
    2096                 :         157 :                               fold_convert (TREE_TYPE (charlen), tmp));
    2097                 :             :               /* and the oposite way at the end of the block, to hand changes
    2098                 :             :                  on the string length back.  */
    2099                 :         157 :               gfc_add_modify (&se.post, tmp,
    2100                 :         157 :                               fold_convert (TREE_TYPE (tmp), charlen));
    2101                 :             :               /* Length assignment done, prevent adding it again below.  */
    2102                 :         157 :               need_len_assign = false;
    2103                 :             :             }
    2104                 :         389 :           gfc_conv_descriptor_offset_set (&se.pre, desc, offset);
    2105                 :         389 :         }
    2106                 :        2629 :       else if (sym->ts.type == BT_CLASS && e->ts.type == BT_CLASS
    2107                 :         554 :                && CLASS_DATA (e)->attr.dimension)
    2108                 :             :         {
    2109                 :             :           /* This is bound to be a class array element.  */
    2110                 :          74 :           gfc_conv_expr_reference (&se, e);
    2111                 :             :           /* Get the _vptr component of the class object.  */
    2112                 :          74 :           tmp = gfc_get_vptr_from_expr (se.expr);
    2113                 :             :           /* Obtain a temporary class container for the result.  */
    2114                 :          74 :           gfc_conv_derived_to_class (&se, e, sym->ts, tmp, false, false);
    2115                 :          74 :           se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
    2116                 :          74 :           need_len_assign = false;
    2117                 :             :         }
    2118                 :             :       else
    2119                 :             :         {
    2120                 :             :           /* For BT_CLASS and BT_DERIVED, this boils down to a pointer assign,
    2121                 :             :              which has the string length included.  For CHARACTERS it is still
    2122                 :             :              needed and will be done at the end of this routine.  */
    2123                 :        2555 :           gfc_conv_expr (&se, e);
    2124                 :        2555 :           need_len_assign = need_len_assign && sym->ts.type == BT_CHARACTER;
    2125                 :             :         }
    2126                 :             : 
    2127                 :        3105 :       if (sym->ts.type == BT_CHARACTER
    2128                 :         500 :           && !sym->attr.select_type_temporary
    2129                 :          97 :           && VAR_P (sym->ts.u.cl->backend_decl)
    2130                 :          55 :           && se.string_length != sym->ts.u.cl->backend_decl)
    2131                 :             :         {
    2132                 :          55 :           gfc_add_modify (&se.pre, sym->ts.u.cl->backend_decl,
    2133                 :          55 :                           fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl),
    2134                 :             :                                         se.string_length));
    2135                 :          55 :           if (e->expr_type == EXPR_FUNCTION)
    2136                 :             :             {
    2137                 :           6 :               tmp = gfc_call_free (sym->backend_decl);
    2138                 :           6 :               gfc_add_expr_to_block (&se.post, tmp);
    2139                 :             :             }
    2140                 :             :         }
    2141                 :             : 
    2142                 :         500 :       if (sym->ts.type == BT_CHARACTER && e->ts.type == BT_CHARACTER
    2143                 :        3202 :           && POINTER_TYPE_P (TREE_TYPE (se.expr)))
    2144                 :             :         {
    2145                 :             :           /* These are pointer types already.  */
    2146                 :          73 :           tmp = fold_convert (TREE_TYPE (sym->backend_decl), se.expr);
    2147                 :             :         }
    2148                 :             :       else
    2149                 :             :         {
    2150                 :        3032 :           tree ctree = gfc_get_class_from_expr (se.expr);
    2151                 :        3032 :           tmp = TREE_TYPE (sym->backend_decl);
    2152                 :             : 
    2153                 :             :           /* F2018:19.5.1.6 "If a selector has the POINTER attribute,
    2154                 :             :              it shall be associated; the associate name is associated
    2155                 :             :              with the target of the pointer and does not have the
    2156                 :             :              POINTER attribute."  */
    2157                 :        3032 :           if (sym->ts.type == BT_CLASS
    2158                 :        1030 :               && e->ts.type == BT_CLASS && e->rank == 0 && ctree
    2159                 :        3625 :               && (!GFC_CLASS_TYPE_P (TREE_TYPE (se.expr))
    2160                 :         586 :                   || CLASS_DATA (e)->attr.class_pointer))
    2161                 :             :             {
    2162                 :         227 :               tree stmp;
    2163                 :         227 :               tree dtmp;
    2164                 :         227 :               tree ctmp;
    2165                 :             : 
    2166                 :         227 :               ctmp = ctree;
    2167                 :         227 :               dtmp = TREE_TYPE (TREE_TYPE (sym->backend_decl));
    2168                 :         227 :               ctree = gfc_create_var (dtmp, "class");
    2169                 :             : 
    2170                 :         227 :               if (IS_INFERRED_TYPE (e)
    2171                 :           6 :                   && !GFC_CLASS_TYPE_P (TREE_TYPE (se.expr)))
    2172                 :             :                 stmp = se.expr;
    2173                 :             :               else
    2174                 :         227 :                 stmp = gfc_class_data_get (ctmp);
    2175                 :             : 
    2176                 :             :               /* Coarray scalar component expressions can emerge from
    2177                 :             :                  the front end as array elements of the _data field.  */
    2178                 :         227 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (stmp)))
    2179                 :           7 :                 stmp = gfc_conv_descriptor_data_get (stmp);
    2180                 :             : 
    2181                 :         227 :               if (!POINTER_TYPE_P (TREE_TYPE (stmp)))
    2182                 :           0 :                 stmp = gfc_build_addr_expr (NULL, stmp);
    2183                 :             : 
    2184                 :         227 :               dtmp = gfc_class_data_get (ctree);
    2185                 :         227 :               stmp = fold_convert (TREE_TYPE (dtmp), stmp);
    2186                 :         227 :               gfc_add_modify (&se.pre, dtmp, stmp);
    2187                 :         227 :               stmp = gfc_class_vptr_get (ctmp);
    2188                 :         227 :               dtmp = gfc_class_vptr_get (ctree);
    2189                 :         227 :               stmp = fold_convert (TREE_TYPE (dtmp), stmp);
    2190                 :         227 :               gfc_add_modify (&se.pre, dtmp, stmp);
    2191                 :         227 :               if (UNLIMITED_POLY (sym))
    2192                 :             :                 {
    2193                 :          60 :                   stmp = gfc_class_len_get (ctmp);
    2194                 :          60 :                   dtmp = gfc_class_len_get (ctree);
    2195                 :          60 :                   stmp = fold_convert (TREE_TYPE (dtmp), stmp);
    2196                 :          60 :                   gfc_add_modify (&se.pre, dtmp, stmp);
    2197                 :          60 :                   need_len_assign = false;
    2198                 :             :                 }
    2199                 :         227 :               se.expr = ctree;
    2200                 :             :             }
    2201                 :        3032 :           tmp = gfc_build_addr_expr (tmp, se.expr);
    2202                 :             :         }
    2203                 :             : 
    2204                 :        3105 :       gfc_add_modify (&se.pre, sym->backend_decl, tmp);
    2205                 :             : 
    2206                 :        3105 :       gfc_add_init_cleanup (block, gfc_finish_block( &se.pre),
    2207                 :             :                             gfc_finish_block (&se.post));
    2208                 :             :     }
    2209                 :             : 
    2210                 :             :   /* Do a simple assignment.  This is for scalar expressions, where we
    2211                 :             :      can simply use expression assignment.  */
    2212                 :             :   else
    2213                 :             :     {
    2214                 :         297 :       gfc_expr *lhs;
    2215                 :         297 :       tree res;
    2216                 :         297 :       gfc_se se;
    2217                 :         297 :       stmtblock_t final_block;
    2218                 :             : 
    2219                 :         297 :       gfc_init_se (&se, NULL);
    2220                 :             : 
    2221                 :             :       /* resolve.cc converts some associate names to allocatable so that
    2222                 :             :          allocation can take place automatically in gfc_trans_assignment.
    2223                 :             :          The frontend prevents them from being either allocated,
    2224                 :             :          deallocated or reallocated.  */
    2225                 :         297 :       if (sym->ts.type == BT_DERIVED
    2226                 :          61 :           && sym->ts.u.derived->attr.alloc_comp)
    2227                 :             :         {
    2228                 :           6 :           tmp = sym->backend_decl;
    2229                 :           6 :           tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, tmp,
    2230                 :           6 :                                 sym->attr.dimension ? sym->as->rank : 0);
    2231                 :           6 :           gfc_add_expr_to_block (&se.pre, tmp);
    2232                 :             :         }
    2233                 :             : 
    2234                 :         297 :       if (sym->attr.allocatable)
    2235                 :             :         {
    2236                 :          12 :           tmp = sym->backend_decl;
    2237                 :          12 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    2238                 :           0 :             tmp = gfc_conv_descriptor_data_get (tmp);
    2239                 :          12 :           gfc_add_modify (&se.pre, tmp, fold_convert (TREE_TYPE (tmp),
    2240                 :             :                                                     null_pointer_node));
    2241                 :             :         }
    2242                 :             : 
    2243                 :         297 :       lhs = gfc_lval_expr_from_sym (sym);
    2244                 :         297 :       lhs->must_finalize = 0;
    2245                 :         297 :       res = gfc_trans_assignment (lhs, e, false, true);
    2246                 :         297 :       gfc_add_expr_to_block (&se.pre, res);
    2247                 :             : 
    2248                 :         297 :       gfc_init_block (&final_block);
    2249                 :             : 
    2250                 :         297 :       if (sym->attr.associate_var
    2251                 :         297 :           && sym->ts.type == BT_DERIVED
    2252                 :          61 :           && sym->ts.u.derived->attr.defined_assign_comp
    2253                 :           0 :           && gfc_may_be_finalized (sym->ts)
    2254                 :         297 :           && e->expr_type == EXPR_FUNCTION)
    2255                 :             :         {
    2256                 :           0 :           gfc_expr *ef;
    2257                 :           0 :           ef = gfc_lval_expr_from_sym (sym);
    2258                 :           0 :           gfc_add_finalizer_call (&final_block, ef);
    2259                 :           0 :           gfc_free_expr (ef);
    2260                 :             :         }
    2261                 :             : 
    2262                 :         297 :       if (sym->ts.type == BT_DERIVED
    2263                 :          61 :           && sym->ts.u.derived->attr.alloc_comp)
    2264                 :             :         {
    2265                 :           6 :           tmp = sym->backend_decl;
    2266                 :           6 :           tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived,
    2267                 :             :                                            tmp, 0);
    2268                 :           6 :           gfc_add_expr_to_block (&final_block, tmp);
    2269                 :             :         }
    2270                 :             : 
    2271                 :         297 :       tmp = sym->backend_decl;
    2272                 :         297 :       if (e->expr_type == EXPR_FUNCTION
    2273                 :          70 :           && sym->ts.type == BT_DERIVED
    2274                 :          39 :           && sym->ts.u.derived
    2275                 :          39 :           && sym->ts.u.derived->attr.pdt_type)
    2276                 :             :         {
    2277                 :           6 :           tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived, tmp,
    2278                 :             :                                          0);
    2279                 :             :         }
    2280                 :         291 :       else if (e->expr_type == EXPR_FUNCTION
    2281                 :          64 :                && sym->ts.type == BT_CLASS
    2282                 :           0 :                && CLASS_DATA (sym)->ts.u.derived
    2283                 :           0 :                && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type)
    2284                 :             :         {
    2285                 :           0 :           tmp = gfc_class_data_get (tmp);
    2286                 :           0 :           tmp = gfc_deallocate_pdt_comp (CLASS_DATA (sym)->ts.u.derived,
    2287                 :             :                                          tmp, 0);
    2288                 :             :         }
    2289                 :         291 :       else if (sym->attr.allocatable)
    2290                 :             :         {
    2291                 :          12 :           tmp = sym->backend_decl;
    2292                 :             : 
    2293                 :          12 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    2294                 :           0 :             tmp = gfc_conv_descriptor_data_get (tmp);
    2295                 :             : 
    2296                 :             :           /* A simple call to free suffices here.  */
    2297                 :          12 :           tmp = gfc_call_free (tmp);
    2298                 :             : 
    2299                 :             :           /* Make sure that reallocation on assignment cannot occur.  */
    2300                 :          12 :           sym->attr.allocatable = 0;
    2301                 :             :         }
    2302                 :             :       else
    2303                 :             :         tmp = NULL_TREE;
    2304                 :             : 
    2305                 :         297 :       gfc_add_expr_to_block (&final_block, tmp);
    2306                 :         297 :       tmp = gfc_finish_block (&final_block);
    2307                 :         297 :       res = gfc_finish_block (&se.pre);
    2308                 :         297 :       gfc_add_init_cleanup (block, res, tmp);
    2309                 :         297 :       gfc_free_expr (lhs);
    2310                 :             :     }
    2311                 :             : 
    2312                 :             :   /* Set the stringlength, when needed.  */
    2313                 :        6435 :   if (need_len_assign)
    2314                 :             :     {
    2315                 :         643 :       gfc_se se;
    2316                 :         643 :       gfc_init_se (&se, NULL);
    2317                 :         643 :       if (e->symtree->n.sym->ts.type == BT_CHARACTER)
    2318                 :             :         {
    2319                 :             :           /* Deferred strings are dealt with in the preceding.  */
    2320                 :           0 :           gcc_assert (!e->symtree->n.sym->ts.deferred);
    2321                 :           0 :           tmp = e->symtree->n.sym->ts.u.cl->backend_decl;
    2322                 :             :         }
    2323                 :         643 :       else if (e->symtree->n.sym->attr.function
    2324                 :          12 :                && e->symtree->n.sym == e->symtree->n.sym->result)
    2325                 :             :         {
    2326                 :          12 :           tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
    2327                 :          12 :           tmp = gfc_class_len_get (tmp);
    2328                 :             :         }
    2329                 :             :       else
    2330                 :         631 :         tmp = gfc_class_len_get (gfc_get_symbol_decl (e->symtree->n.sym));
    2331                 :         643 :       gfc_get_symbol_decl (sym);
    2332                 :         643 :       charlen = sym->ts.type == BT_CHARACTER ? sym->ts.u.cl->backend_decl
    2333                 :           0 :                                         : gfc_class_len_get (sym->backend_decl);
    2334                 :             :       /* Prevent adding a noop len= len.  */
    2335                 :         643 :       if (tmp != charlen)
    2336                 :             :         {
    2337                 :         643 :           gfc_add_modify (&se.pre, charlen,
    2338                 :         643 :                           fold_convert (TREE_TYPE (charlen), tmp));
    2339                 :         643 :           gfc_add_init_cleanup (block, gfc_finish_block (&se.pre),
    2340                 :             :                                 gfc_finish_block (&se.post));
    2341                 :             :         }
    2342                 :             :     }
    2343                 :        6435 : }
    2344                 :             : 
    2345                 :             : 
    2346                 :             : /* Translate a BLOCK construct.  This is basically what we would do for a
    2347                 :             :    procedure body.  */
    2348                 :             : 
    2349                 :             : tree
    2350                 :       12605 : gfc_trans_block_construct (gfc_code* code)
    2351                 :             : {
    2352                 :       12605 :   gfc_namespace* ns;
    2353                 :       12605 :   gfc_symbol* sym;
    2354                 :       12605 :   gfc_wrapped_block block;
    2355                 :       12605 :   tree exit_label;
    2356                 :       12605 :   stmtblock_t body;
    2357                 :       12605 :   gfc_association_list *ass;
    2358                 :       12605 :   tree translated_body;
    2359                 :             : 
    2360                 :       12605 :   ns = code->ext.block.ns;
    2361                 :       12605 :   gcc_assert (ns);
    2362                 :       12605 :   sym = ns->proc_name;
    2363                 :       12605 :   gcc_assert (sym);
    2364                 :             : 
    2365                 :             :   /* Process local variables.  */
    2366                 :       12605 :   gcc_assert (!sym->tlink);
    2367                 :       12605 :   sym->tlink = sym;
    2368                 :       12605 :   gfc_process_block_locals (ns);
    2369                 :             : 
    2370                 :             :   /* Generate code including exit-label.  */
    2371                 :       12605 :   gfc_init_block (&body);
    2372                 :       12605 :   exit_label = gfc_build_label_decl (NULL_TREE);
    2373                 :       12605 :   code->exit_label = exit_label;
    2374                 :             : 
    2375                 :       12605 :   finish_oacc_declare (ns, sym, true);
    2376                 :             : 
    2377                 :       12605 :   translated_body = gfc_trans_code (ns->code);
    2378                 :       12605 :   if (ns->omp_structured_block)
    2379                 :         404 :     translated_body = build1 (OMP_STRUCTURED_BLOCK, void_type_node,
    2380                 :             :                               translated_body);
    2381                 :       12605 :   gfc_add_expr_to_block (&body, translated_body);
    2382                 :       12605 :   gfc_add_expr_to_block (&body, build1_v (LABEL_EXPR, exit_label));
    2383                 :             : 
    2384                 :             :   /* Finish everything.  */
    2385                 :       12605 :   gfc_start_wrapped_block (&block, gfc_finish_block (&body));
    2386                 :       12605 :   gfc_trans_deferred_vars (sym, &block);
    2387                 :       19040 :   for (ass = code->ext.block.assoc; ass; ass = ass->next)
    2388                 :        6435 :     trans_associate_var (ass->st->n.sym, &block);
    2389                 :             : 
    2390                 :       12605 :   return gfc_finish_wrapped_block (&block);
    2391                 :             : }
    2392                 :             : 
    2393                 :             : /* Translate the simple DO construct in a C-style manner.
    2394                 :             :    This is where the loop variable has integer type and step +-1.
    2395                 :             :    Following code will generate infinite loop in case where TO is INT_MAX
    2396                 :             :    (for +1 step) or INT_MIN (for -1 step)
    2397                 :             : 
    2398                 :             :    We translate a do loop from:
    2399                 :             : 
    2400                 :             :    DO dovar = from, to, step
    2401                 :             :       body
    2402                 :             :    END DO
    2403                 :             : 
    2404                 :             :    to:
    2405                 :             : 
    2406                 :             :    [Evaluate loop bounds and step]
    2407                 :             :     dovar = from;
    2408                 :             :     for (;;)
    2409                 :             :       {
    2410                 :             :         if (dovar > to)
    2411                 :             :           goto end_label;
    2412                 :             :         body;
    2413                 :             :         cycle_label:
    2414                 :             :         dovar += step;
    2415                 :             :       }
    2416                 :             :     end_label:
    2417                 :             : 
    2418                 :             :    This helps the optimizers by avoiding the extra pre-header condition and
    2419                 :             :    we save a register as we just compare the updated IV (not a value in
    2420                 :             :    previous step).  */
    2421                 :             : 
    2422                 :             : static tree
    2423                 :       24266 : gfc_trans_simple_do (gfc_code * code, stmtblock_t *pblock, tree dovar,
    2424                 :             :                      tree from, tree to, tree step, tree exit_cond)
    2425                 :             : {
    2426                 :       24266 :   stmtblock_t body;
    2427                 :       24266 :   tree type;
    2428                 :       24266 :   tree cond;
    2429                 :       24266 :   tree tmp;
    2430                 :       24266 :   tree saved_dovar = NULL;
    2431                 :       24266 :   tree cycle_label;
    2432                 :       24266 :   tree exit_label;
    2433                 :       24266 :   location_t loc;
    2434                 :       24266 :   type = TREE_TYPE (dovar);
    2435                 :       24266 :   bool is_step_positive = tree_int_cst_sgn (step) > 0;
    2436                 :             : 
    2437                 :       24266 :   loc = gfc_get_location (&code->ext.iterator->start->where);
    2438                 :             : 
    2439                 :             :   /* Initialize the DO variable: dovar = from.  */
    2440                 :       24266 :   gfc_add_modify_loc (loc, pblock, dovar,
    2441                 :       24266 :                       fold_convert (TREE_TYPE (dovar), from));
    2442                 :             : 
    2443                 :             :   /* Save value for do-tinkering checking.  */
    2444                 :       24266 :   if (gfc_option.rtcheck & GFC_RTCHECK_DO)
    2445                 :             :     {
    2446                 :         201 :       saved_dovar = gfc_create_var (type, ".saved_dovar");
    2447                 :         201 :       gfc_add_modify_loc (loc, pblock, saved_dovar, dovar);
    2448                 :             :     }
    2449                 :             : 
    2450                 :             :   /* Cycle and exit statements are implemented with gotos.  */
    2451                 :       24266 :   cycle_label = gfc_build_label_decl (NULL_TREE);
    2452                 :       24266 :   exit_label = gfc_build_label_decl (NULL_TREE);
    2453                 :             : 
    2454                 :             :   /* Put the labels where they can be found later.  See gfc_trans_do().  */
    2455                 :       24266 :   code->cycle_label = cycle_label;
    2456                 :       24266 :   code->exit_label = exit_label;
    2457                 :             : 
    2458                 :             :   /* Loop body.  */
    2459                 :       24266 :   gfc_start_block (&body);
    2460                 :             : 
    2461                 :             :   /* Exit the loop if there is an I/O result condition or error.  */
    2462                 :       24266 :   if (exit_cond)
    2463                 :             :     {
    2464                 :         350 :       tmp = build1_v (GOTO_EXPR, exit_label);
    2465                 :         350 :       tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
    2466                 :             :                              exit_cond, tmp,
    2467                 :             :                              build_empty_stmt (loc));
    2468                 :         350 :       gfc_add_expr_to_block (&body, tmp);
    2469                 :             :     }
    2470                 :             : 
    2471                 :             :   /* Evaluate the loop condition.  */
    2472                 :       24266 :   if (is_step_positive)
    2473                 :       24151 :     cond = fold_build2_loc (loc, GT_EXPR, logical_type_node, dovar,
    2474                 :             :                             fold_convert (type, to));
    2475                 :             :   else
    2476                 :         115 :     cond = fold_build2_loc (loc, LT_EXPR, logical_type_node, dovar,
    2477                 :             :                             fold_convert (type, to));
    2478                 :             : 
    2479                 :       24266 :   cond = gfc_evaluate_now_loc (loc, cond, &body);
    2480                 :       24266 :   if (code->ext.iterator->annot.unroll && cond != error_mark_node)
    2481                 :          12 :     cond
    2482                 :          12 :       = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    2483                 :          12 :                 build_int_cst (integer_type_node, annot_expr_unroll_kind),
    2484                 :             :                 build_int_cst (integer_type_node,
    2485                 :          12 :                                code->ext.iterator->annot.unroll));
    2486                 :             : 
    2487                 :       24266 :   if (code->ext.iterator->annot.ivdep && cond != error_mark_node)
    2488                 :           2 :     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    2489                 :           2 :                    build_int_cst (integer_type_node, annot_expr_ivdep_kind),
    2490                 :             :                    integer_zero_node);
    2491                 :       24266 :   if (code->ext.iterator->annot.vector && cond != error_mark_node)
    2492                 :           2 :     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    2493                 :           2 :                    build_int_cst (integer_type_node, annot_expr_vector_kind),
    2494                 :             :                    integer_zero_node);
    2495                 :       24266 :   if (code->ext.iterator->annot.novector && cond != error_mark_node)
    2496                 :           1 :     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    2497                 :           1 :                    build_int_cst (integer_type_node, annot_expr_no_vector_kind),
    2498                 :             :                    integer_zero_node);
    2499                 :             : 
    2500                 :             :   /* The loop exit.  */
    2501                 :       24266 :   tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
    2502                 :       24266 :   TREE_USED (exit_label) = 1;
    2503                 :       24266 :   tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
    2504                 :             :                          cond, tmp, build_empty_stmt (loc));
    2505                 :       24266 :   gfc_add_expr_to_block (&body, tmp);
    2506                 :             : 
    2507                 :             :   /* Check whether the induction variable is equal to INT_MAX
    2508                 :             :      (respectively to INT_MIN).  */
    2509                 :       24266 :   if (gfc_option.rtcheck & GFC_RTCHECK_DO)
    2510                 :             :     {
    2511                 :         207 :       tree boundary = is_step_positive ? TYPE_MAX_VALUE (type)
    2512                 :           6 :         : TYPE_MIN_VALUE (type);
    2513                 :             : 
    2514                 :         201 :       tmp = fold_build2_loc (loc, EQ_EXPR, logical_type_node,
    2515                 :             :                              dovar, boundary);
    2516                 :         201 :       gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
    2517                 :             :                                "Loop iterates infinitely");
    2518                 :             :     }
    2519                 :             : 
    2520                 :             :   /* Main loop body.  */
    2521                 :       24266 :   tmp = gfc_trans_code_cond (code->block->next, exit_cond);
    2522                 :       24266 :   gfc_add_expr_to_block (&body, tmp);
    2523                 :             : 
    2524                 :             :   /* Label for cycle statements (if needed).  */
    2525                 :       24266 :   if (TREE_USED (cycle_label))
    2526                 :             :     {
    2527                 :       24266 :       tmp = build1_v (LABEL_EXPR, cycle_label);
    2528                 :       24266 :       gfc_add_expr_to_block (&body, tmp);
    2529                 :             :     }
    2530                 :             : 
    2531                 :             :   /* Check whether someone has modified the loop variable.  */
    2532                 :       24266 :   if (gfc_option.rtcheck & GFC_RTCHECK_DO)
    2533                 :             :     {
    2534                 :         201 :       tmp = fold_build2_loc (loc, NE_EXPR, logical_type_node,
    2535                 :             :                              dovar, saved_dovar);
    2536                 :         201 :       gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
    2537                 :             :                                "Loop variable has been modified");
    2538                 :             :     }
    2539                 :             : 
    2540                 :             :   /* Increment the loop variable.  */
    2541                 :       24266 :   tmp = fold_build2_loc (loc, PLUS_EXPR, type, dovar, step);
    2542                 :       24266 :   gfc_add_modify_loc (loc, &body, dovar, tmp);
    2543                 :             : 
    2544                 :       24266 :   if (gfc_option.rtcheck & GFC_RTCHECK_DO)
    2545                 :         201 :     gfc_add_modify_loc (loc, &body, saved_dovar, dovar);
    2546                 :             : 
    2547                 :             :   /* Finish the loop body.  */
    2548                 :       24266 :   tmp = gfc_finish_block (&body);
    2549                 :       24266 :   tmp = fold_build1_loc (loc, LOOP_EXPR, void_type_node, tmp);
    2550                 :             : 
    2551                 :       24266 :   gfc_add_expr_to_block (pblock, tmp);
    2552                 :             : 
    2553                 :             :   /* Add the exit label.  */
    2554                 :       24266 :   tmp = build1_v (LABEL_EXPR, exit_label);
    2555                 :       24266 :   gfc_add_expr_to_block (pblock, tmp);
    2556                 :             : 
    2557                 :       24266 :   return gfc_finish_block (pblock);
    2558                 :             : }
    2559                 :             : 
    2560                 :             : /* Translate the DO construct.  This obviously is one of the most
    2561                 :             :    important ones to get right with any compiler, but especially
    2562                 :             :    so for Fortran.
    2563                 :             : 
    2564                 :             :    We special case some loop forms as described in gfc_trans_simple_do.
    2565                 :             :    For other cases we implement them with a separate loop count,
    2566                 :             :    as described in the standard.
    2567                 :             : 
    2568                 :             :    We translate a do loop from:
    2569                 :             : 
    2570                 :             :    DO dovar = from, to, step
    2571                 :             :       body
    2572                 :             :    END DO
    2573                 :             : 
    2574                 :             :    to:
    2575                 :             : 
    2576                 :             :    [evaluate loop bounds and step]
    2577                 :             :    empty = (step > 0 ? to < from : to > from);
    2578                 :             :    countm1 = (to - from) / step;
    2579                 :             :    dovar = from;
    2580                 :             :    if (empty) goto exit_label;
    2581                 :             :    for (;;)
    2582                 :             :      {
    2583                 :             :        body;
    2584                 :             : cycle_label:
    2585                 :             :        dovar += step
    2586                 :             :        countm1t = countm1;
    2587                 :             :        countm1--;
    2588                 :             :        if (countm1t == 0) goto exit_label;
    2589                 :             :      }
    2590                 :             : exit_label:
    2591                 :             : 
    2592                 :             :    countm1 is an unsigned integer.  It is equal to the loop count minus one,
    2593                 :             :    because the loop count itself can overflow.  */
    2594                 :             : 
    2595                 :             : tree
    2596                 :       25251 : gfc_trans_do (gfc_code * code, tree exit_cond)
    2597                 :             : {
    2598                 :       25251 :   gfc_se se;
    2599                 :       25251 :   tree dovar;
    2600                 :       25251 :   tree saved_dovar = NULL;
    2601                 :       25251 :   tree from;
    2602                 :       25251 :   tree to;
    2603                 :       25251 :   tree step;
    2604                 :       25251 :   tree countm1;
    2605                 :       25251 :   tree type;
    2606                 :       25251 :   tree utype;
    2607                 :       25251 :   tree cond;
    2608                 :       25251 :   tree cycle_label;
    2609                 :       25251 :   tree exit_label;
    2610                 :       25251 :   tree tmp;
    2611                 :       25251 :   stmtblock_t block;
    2612                 :       25251 :   stmtblock_t body;
    2613                 :       25251 :   location_t loc;
    2614                 :             : 
    2615                 :       25251 :   gfc_start_block (&block);
    2616                 :             : 
    2617                 :       25251 :   loc = gfc_get_location (&code->ext.iterator->start->where);
    2618                 :             : 
    2619                 :             :   /* Evaluate all the expressions in the iterator.  */
    2620                 :       25251 :   gfc_init_se (&se, NULL);
    2621                 :       25251 :   gfc_conv_expr_lhs (&se, code->ext.iterator->var);
    2622                 :       25251 :   gfc_add_block_to_block (&block, &se.pre);
    2623                 :       25251 :   dovar = se.expr;
    2624                 :       25251 :   type = TREE_TYPE (dovar);
    2625                 :             : 
    2626                 :       25251 :   gfc_init_se (&se, NULL);
    2627                 :       25251 :   gfc_conv_expr_val (&se, code->ext.iterator->start);
    2628                 :       25251 :   gfc_add_block_to_block (&block, &se.pre);
    2629                 :       25251 :   from = gfc_evaluate_now (se.expr, &block);
    2630                 :             : 
    2631                 :       25251 :   gfc_init_se (&se, NULL);
    2632                 :       25251 :   gfc_conv_expr_val (&se, code->ext.iterator->end);
    2633                 :       25251 :   gfc_add_block_to_block (&block, &se.pre);
    2634                 :       25251 :   to = gfc_evaluate_now (se.expr, &block);
    2635                 :             : 
    2636                 :       25251 :   gfc_init_se (&se, NULL);
    2637                 :       25251 :   gfc_conv_expr_val (&se, code->ext.iterator->step);
    2638                 :       25251 :   gfc_add_block_to_block (&block, &se.pre);
    2639                 :       25251 :   step = gfc_evaluate_now (se.expr, &block);
    2640                 :             : 
    2641                 :       25251 :   if (gfc_option.rtcheck & GFC_RTCHECK_DO)
    2642                 :             :     {
    2643                 :         213 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, step,
    2644                 :             :                              build_zero_cst (type));
    2645                 :         213 :       gfc_trans_runtime_check (true, false, tmp, &block, &code->loc,
    2646                 :             :                                "DO step value is zero");
    2647                 :             :     }
    2648                 :             : 
    2649                 :             :   /* Special case simple loops.  */
    2650                 :       25251 :   if (TREE_CODE (type) == INTEGER_TYPE
    2651                 :       25251 :       && (integer_onep (step)
    2652                 :        1018 :         || tree_int_cst_equal (step, integer_minus_one_node)))
    2653                 :       24266 :     return gfc_trans_simple_do (code, &block, dovar, from, to, step,
    2654                 :       24266 :                                 exit_cond);
    2655                 :             : 
    2656                 :         985 :   if (TREE_CODE (type) == INTEGER_TYPE)
    2657                 :         903 :     utype = unsigned_type_for (type);
    2658                 :             :   else
    2659                 :          82 :     utype = unsigned_type_for (gfc_array_index_type);
    2660                 :         985 :   countm1 = gfc_create_var (utype, "countm1");
    2661                 :             : 
    2662                 :             :   /* Cycle and exit statements are implemented with gotos.  */
    2663                 :         985 :   cycle_label = gfc_build_label_decl (NULL_TREE);
    2664                 :         985 :   exit_label = gfc_build_label_decl (NULL_TREE);
    2665                 :         985 :   TREE_USED (exit_label) = 1;
    2666                 :             : 
    2667                 :             :   /* Put these labels where they can be found later.  */
    2668                 :         985 :   code->cycle_label = cycle_label;
    2669                 :         985 :   code->exit_label = exit_label;
    2670                 :             : 
    2671                 :             :   /* Initialize the DO variable: dovar = from.  */
    2672                 :         985 :   gfc_add_modify (&block, dovar, from);
    2673                 :             : 
    2674                 :             :   /* Save value for do-tinkering checking.  */
    2675                 :         985 :   if (gfc_option.rtcheck & GFC_RTCHECK_DO)
    2676                 :             :     {
    2677                 :          12 :       saved_dovar = gfc_create_var (type, ".saved_dovar");
    2678                 :          12 :       gfc_add_modify_loc (loc, &block, saved_dovar, dovar);
    2679                 :             :     }
    2680                 :             : 
    2681                 :             :   /* Initialize loop count and jump to exit label if the loop is empty.
    2682                 :             :      This code is executed before we enter the loop body. We generate:
    2683                 :             :      if (step > 0)
    2684                 :             :        {
    2685                 :             :          countm1 = (to - from) / step;
    2686                 :             :          if (to < from)
    2687                 :             :            goto exit_label;
    2688                 :             :        }
    2689                 :             :      else
    2690                 :             :        {
    2691                 :             :          countm1 = (from - to) / -step;
    2692                 :             :          if (to > from)
    2693                 :             :            goto exit_label;
    2694                 :             :        }
    2695                 :             :    */
    2696                 :             : 
    2697                 :         985 :   if (TREE_CODE (type) == INTEGER_TYPE)
    2698                 :             :     {
    2699                 :         903 :       tree pos, neg, tou, fromu, stepu, tmp2;
    2700                 :             : 
    2701                 :             :       /* The distance from FROM to TO cannot always be represented in a signed
    2702                 :             :          type, thus use unsigned arithmetic, also to avoid any undefined
    2703                 :             :          overflow issues.  */
    2704                 :         903 :       tou = fold_convert (utype, to);
    2705                 :         903 :       fromu = fold_convert (utype, from);
    2706                 :         903 :       stepu = fold_convert (utype, step);
    2707                 :             : 
    2708                 :             :       /* For a positive step, when to < from, exit, otherwise compute
    2709                 :             :          countm1 = ((unsigned)to - (unsigned)from) / (unsigned)step  */
    2710                 :         903 :       tmp = fold_build2_loc (loc, LT_EXPR, logical_type_node, to, from);
    2711                 :         903 :       tmp2 = fold_build2_loc (loc, TRUNC_DIV_EXPR, utype,
    2712                 :             :                               fold_build2_loc (loc, MINUS_EXPR, utype,
    2713                 :             :                                                tou, fromu),
    2714                 :             :                               stepu);
    2715                 :         903 :       pos = build2 (COMPOUND_EXPR, void_type_node,
    2716                 :             :                     fold_build2 (MODIFY_EXPR, void_type_node,
    2717                 :             :                                  countm1, tmp2),
    2718                 :             :                     build3_loc (loc, COND_EXPR, void_type_node,
    2719                 :             :                                 gfc_unlikely (tmp, PRED_FORTRAN_LOOP_PREHEADER),
    2720                 :             :                                 build1_loc (loc, GOTO_EXPR, void_type_node,
    2721                 :             :                                             exit_label), NULL_TREE));
    2722                 :             : 
    2723                 :             :       /* For a negative step, when to > from, exit, otherwise compute
    2724                 :             :          countm1 = ((unsigned)from - (unsigned)to) / -(unsigned)step  */
    2725                 :         903 :       tmp = fold_build2_loc (loc, GT_EXPR, logical_type_node, to, from);
    2726                 :         903 :       tmp2 = fold_build2_loc (loc, TRUNC_DIV_EXPR, utype,
    2727                 :             :                               fold_build2_loc (loc, MINUS_EXPR, utype,
    2728                 :             :                                                fromu, tou),
    2729                 :             :                               fold_build1_loc (loc, NEGATE_EXPR, utype, stepu));
    2730                 :         903 :       neg = build2 (COMPOUND_EXPR, void_type_node,
    2731                 :             :                     fold_build2 (MODIFY_EXPR, void_type_node,
    2732                 :             :                                  countm1, tmp2),
    2733                 :             :                     build3_loc (loc, COND_EXPR, void_type_node,
    2734                 :             :                                 gfc_unlikely (tmp, PRED_FORTRAN_LOOP_PREHEADER),
    2735                 :             :                                 build1_loc (loc, GOTO_EXPR, void_type_node,
    2736                 :             :                                             exit_label), NULL_TREE));
    2737                 :             : 
    2738                 :         903 :       tmp = fold_build2_loc (loc, LT_EXPR, logical_type_node, step,
    2739                 :         903 :                              build_int_cst (TREE_TYPE (step), 0));
    2740                 :         903 :       tmp = fold_build3_loc (loc, COND_EXPR, void_type_node, tmp, neg, pos);
    2741                 :             : 
    2742                 :         903 :       gfc_add_expr_to_block (&block, tmp);
    2743                 :             :     }
    2744                 :             :   else
    2745                 :             :     {
    2746                 :          82 :       tree pos_step;
    2747                 :             : 
    2748                 :             :       /* TODO: We could use the same width as the real type.
    2749                 :             :          This would probably cause more problems that it solves
    2750                 :             :          when we implement "long double" types.  */
    2751                 :             : 
    2752                 :          82 :       tmp = fold_build2_loc (loc, MINUS_EXPR, type, to, from);
    2753                 :          82 :       tmp = fold_build2_loc (loc, RDIV_EXPR, type, tmp, step);
    2754                 :          82 :       tmp = fold_build1_loc (loc, FIX_TRUNC_EXPR, utype, tmp);
    2755                 :          82 :       gfc_add_modify (&block, countm1, tmp);
    2756                 :             : 
    2757                 :             :       /* We need a special check for empty loops:
    2758                 :             :          empty = (step > 0 ? to < from : to > from);  */
    2759                 :          82 :       pos_step = fold_build2_loc (loc, GT_EXPR, logical_type_node, step,
    2760                 :             :                                   build_zero_cst (type));
    2761                 :          82 :       tmp = fold_build3_loc (loc, COND_EXPR, logical_type_node, pos_step,
    2762                 :             :                              fold_build2_loc (loc, LT_EXPR,
    2763                 :             :                                               logical_type_node, to, from),
    2764                 :             :                              fold_build2_loc (loc, GT_EXPR,
    2765                 :             :                                               logical_type_node, to, from));
    2766                 :             :       /* If the loop is empty, go directly to the exit label.  */
    2767                 :          82 :       tmp = fold_build3_loc (loc, COND_EXPR, void_type_node, tmp,
    2768                 :             :                          build1_v (GOTO_EXPR, exit_label),
    2769                 :             :                          build_empty_stmt (input_location));
    2770                 :          82 :       gfc_add_expr_to_block (&block, tmp);
    2771                 :             :     }
    2772                 :             : 
    2773                 :             :   /* Loop body.  */
    2774                 :         985 :   gfc_start_block (&body);
    2775                 :             : 
    2776                 :             :   /* Main loop body.  */
    2777                 :         985 :   tmp = gfc_trans_code_cond (code->block->next, exit_cond);
    2778                 :         985 :   gfc_add_expr_to_block (&body, tmp);
    2779                 :             : 
    2780                 :             :   /* Label for cycle statements (if needed).  */
    2781                 :         985 :   if (TREE_USED (cycle_label))
    2782                 :             :     {
    2783                 :         985 :       tmp = build1_v (LABEL_EXPR, cycle_label);
    2784                 :         985 :       gfc_add_expr_to_block (&body, tmp);
    2785                 :             :     }
    2786                 :             : 
    2787                 :             :   /* Check whether someone has modified the loop variable.  */
    2788                 :         985 :   if (gfc_option.rtcheck & GFC_RTCHECK_DO)
    2789                 :             :     {
    2790                 :          12 :       tmp = fold_build2_loc (loc, NE_EXPR, logical_type_node, dovar,
    2791                 :             :                              saved_dovar);
    2792                 :          12 :       gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
    2793                 :             :                                "Loop variable has been modified");
    2794                 :             :     }
    2795                 :             : 
    2796                 :             :   /* Exit the loop if there is an I/O result condition or error.  */
    2797                 :         985 :   if (exit_cond)
    2798                 :             :     {
    2799                 :           1 :       tmp = build1_v (GOTO_EXPR, exit_label);
    2800                 :           1 :       tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
    2801                 :             :                              exit_cond, tmp,
    2802                 :             :                              build_empty_stmt (input_location));
    2803                 :           1 :       gfc_add_expr_to_block (&body, tmp);
    2804                 :             :     }
    2805                 :             : 
    2806                 :             :   /* Increment the loop variable.  */
    2807                 :         985 :   tmp = fold_build2_loc (loc, PLUS_EXPR, type, dovar, step);
    2808                 :         985 :   gfc_add_modify_loc (loc, &body, dovar, tmp);
    2809                 :             : 
    2810                 :         985 :   if (gfc_option.rtcheck & GFC_RTCHECK_DO)
    2811                 :          12 :     gfc_add_modify_loc (loc, &body, saved_dovar, dovar);
    2812                 :             : 
    2813                 :             :   /* Initialize countm1t.  */
    2814                 :         985 :   tree countm1t = gfc_create_var (utype, "countm1t");
    2815                 :         985 :   gfc_add_modify_loc (loc, &body, countm1t, countm1);
    2816                 :             : 
    2817                 :             :   /* Decrement the loop count.  */
    2818                 :         985 :   tmp = fold_build2_loc (loc, MINUS_EXPR, utype, countm1,
    2819                 :         985 :                          build_int_cst (utype, 1));
    2820                 :         985 :   gfc_add_modify_loc (loc, &body, countm1, tmp);
    2821                 :             : 
    2822                 :             :   /* End with the loop condition.  Loop until countm1t == 0.  */
    2823                 :         985 :   cond = fold_build2_loc (loc, EQ_EXPR, logical_type_node, countm1t,
    2824                 :         985 :                           build_int_cst (utype, 0));
    2825                 :         985 :   if (code->ext.iterator->annot.unroll && cond != error_mark_node)
    2826                 :           3 :     cond
    2827                 :           3 :       = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    2828                 :           3 :                 build_int_cst (integer_type_node, annot_expr_unroll_kind),
    2829                 :             :                 build_int_cst (integer_type_node,
    2830                 :           3 :                                code->ext.iterator->annot.unroll));
    2831                 :             : 
    2832                 :         985 :   if (code->ext.iterator->annot.ivdep && cond != error_mark_node)
    2833                 :           0 :     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    2834                 :           0 :                    build_int_cst (integer_type_node, annot_expr_ivdep_kind),
    2835                 :             :                    integer_zero_node);
    2836                 :         985 :   if (code->ext.iterator->annot.vector && cond != error_mark_node)
    2837                 :           0 :     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    2838                 :           0 :                    build_int_cst (integer_type_node, annot_expr_vector_kind),
    2839                 :             :                    integer_zero_node);
    2840                 :         985 :   if (code->ext.iterator->annot.novector && cond != error_mark_node)
    2841                 :           0 :     cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    2842                 :           0 :                    build_int_cst (integer_type_node, annot_expr_no_vector_kind),
    2843                 :             :                    integer_zero_node);
    2844                 :             : 
    2845                 :         985 :   tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
    2846                 :         985 :   tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
    2847                 :             :                          cond, tmp, build_empty_stmt (loc));
    2848                 :         985 :   gfc_add_expr_to_block (&body, tmp);
    2849                 :             : 
    2850                 :             :   /* End of loop body.  */
    2851                 :         985 :   tmp = gfc_finish_block (&body);
    2852                 :             : 
    2853                 :             :   /* The for loop itself.  */
    2854                 :         985 :   tmp = fold_build1_loc (loc, LOOP_EXPR, void_type_node, tmp);
    2855                 :         985 :   gfc_add_expr_to_block (&block, tmp);
    2856                 :             : 
    2857                 :             :   /* Add the exit label.  */
    2858                 :         985 :   tmp = build1_v (LABEL_EXPR, exit_label);
    2859                 :         985 :   gfc_add_expr_to_block (&block, tmp);
    2860                 :             : 
    2861                 :         985 :   return gfc_finish_block (&block);
    2862                 :             : }
    2863                 :             : 
    2864                 :             : 
    2865                 :             : /* Translate the DO WHILE construct.
    2866                 :             : 
    2867                 :             :    We translate
    2868                 :             : 
    2869                 :             :    DO WHILE (cond)
    2870                 :             :       body
    2871                 :             :    END DO
    2872                 :             : 
    2873                 :             :    to:
    2874                 :             : 
    2875                 :             :    for ( ; ; )
    2876                 :             :      {
    2877                 :             :        pre_cond;
    2878                 :             :        if (! cond) goto exit_label;
    2879                 :             :        body;
    2880                 :             : cycle_label:
    2881                 :             :      }
    2882                 :             : exit_label:
    2883                 :             : 
    2884                 :             :    Because the evaluation of the exit condition `cond' may have side
    2885                 :             :    effects, we can't do much for empty loop bodies.  The backend optimizers
    2886                 :             :    should be smart enough to eliminate any dead loops.  */
    2887                 :             : 
    2888                 :             : tree
    2889                 :         494 : gfc_trans_do_while (gfc_code * code)
    2890                 :             : {
    2891                 :         494 :   gfc_se cond;
    2892                 :         494 :   tree tmp;
    2893                 :         494 :   tree cycle_label;
    2894                 :         494 :   tree exit_label;
    2895                 :         494 :   stmtblock_t block;
    2896                 :             : 
    2897                 :             :   /* Everything we build here is part of the loop body.  */
    2898                 :         494 :   gfc_start_block (&block);
    2899                 :             : 
    2900                 :             :   /* Cycle and exit statements are implemented with gotos.  */
    2901                 :         494 :   cycle_label = gfc_build_label_decl (NULL_TREE);
    2902                 :         494 :   exit_label = gfc_build_label_decl (NULL_TREE);
    2903                 :             : 
    2904                 :             :   /* Put the labels where they can be found later. See gfc_trans_do().  */
    2905                 :         494 :   code->cycle_label = cycle_label;
    2906                 :         494 :   code->exit_label = exit_label;
    2907                 :             : 
    2908                 :             :   /* Create a GIMPLE version of the exit condition.  */
    2909                 :         494 :   gfc_init_se (&cond, NULL);
    2910                 :         494 :   gfc_conv_expr_val (&cond, code->expr1);
    2911                 :         494 :   gfc_add_block_to_block (&block, &cond.pre);
    2912                 :         494 :   cond.expr = fold_build1_loc (gfc_get_location (&code->expr1->where),
    2913                 :         494 :                                TRUTH_NOT_EXPR, TREE_TYPE (cond.expr),
    2914                 :             :                                cond.expr);
    2915                 :             : 
    2916                 :             :   /* Build "IF (! cond) GOTO exit_label".  */
    2917                 :         494 :   tmp = build1_v (GOTO_EXPR, exit_label);
    2918                 :         494 :   TREE_USED (exit_label) = 1;
    2919                 :         494 :   tmp = fold_build3_loc (gfc_get_location (&code->expr1->where), COND_EXPR,
    2920                 :             :                          void_type_node, cond.expr, tmp,
    2921                 :             :                          build_empty_stmt (gfc_get_location (
    2922                 :         494 :                                              &code->expr1->where)));
    2923                 :         494 :   gfc_add_expr_to_block (&block, tmp);
    2924                 :             : 
    2925                 :             :   /* The main body of the loop.  */
    2926                 :         494 :   tmp = gfc_trans_code (code->block->next);
    2927                 :         494 :   gfc_add_expr_to_block (&block, tmp);
    2928                 :             : 
    2929                 :             :   /* Label for cycle statements (if needed).  */
    2930                 :         494 :   if (TREE_USED (cycle_label))
    2931                 :             :     {
    2932                 :         494 :       tmp = build1_v (LABEL_EXPR, cycle_label);
    2933                 :         494 :       gfc_add_expr_to_block (&block, tmp);
    2934                 :             :     }
    2935                 :             : 
    2936                 :             :   /* End of loop body.  */
    2937                 :         494 :   tmp = gfc_finish_block (&block);
    2938                 :             : 
    2939                 :         494 :   gfc_init_block (&block);
    2940                 :             :   /* Build the loop.  */
    2941                 :         494 :   tmp = fold_build1_loc (gfc_get_location (&code->expr1->where), LOOP_EXPR,
    2942                 :             :                          void_type_node, tmp);
    2943                 :         494 :   gfc_add_expr_to_block (&block, tmp);
    2944                 :             : 
    2945                 :             :   /* Add the exit label.  */
    2946                 :         494 :   tmp = build1_v (LABEL_EXPR, exit_label);
    2947                 :         494 :   gfc_add_expr_to_block (&block, tmp);
    2948                 :             : 
    2949                 :         494 :   return gfc_finish_block (&block);
    2950                 :             : }
    2951                 :             : 
    2952                 :             : 
    2953                 :             : /* Deal with the particular case of SELECT_TYPE, where the vtable
    2954                 :             :    addresses are used for the selection. Since these are not sorted,
    2955                 :             :    the selection has to be made by a series of if statements.  */
    2956                 :             : 
    2957                 :             : static tree
    2958                 :        2616 : gfc_trans_select_type_cases (gfc_code * code)
    2959                 :             : {
    2960                 :        2616 :   gfc_code *c;
    2961                 :        2616 :   gfc_case *cp;
    2962                 :        2616 :   tree tmp;
    2963                 :        2616 :   tree cond;
    2964                 :        2616 :   tree low;
    2965                 :        2616 :   tree high;
    2966                 :        2616 :   gfc_se se;
    2967                 :        2616 :   gfc_se cse;
    2968                 :        2616 :   stmtblock_t block;
    2969                 :        2616 :   stmtblock_t body;
    2970                 :        2616 :   bool def = false;
    2971                 :        2616 :   gfc_expr *e;
    2972                 :        2616 :   gfc_start_block (&block);
    2973                 :             : 
    2974                 :             :   /* Calculate the switch expression.  */
    2975                 :        2616 :   gfc_init_se (&se, NULL);
    2976                 :        2616 :   gfc_conv_expr_val (&se, code->expr1);
    2977                 :        2616 :   gfc_add_block_to_block (&block, &se.pre);
    2978                 :             : 
    2979                 :             :   /* Generate an expression for the selector hash value, for
    2980                 :             :      use to resolve character cases.  */
    2981                 :        2616 :   e = gfc_copy_expr (code->expr1->value.function.actual->expr);
    2982                 :        2616 :   gfc_add_hash_component (e);
    2983                 :             : 
    2984                 :        2616 :   TREE_USED (code->exit_label) = 0;
    2985                 :             : 
    2986                 :        5232 : repeat:
    2987                 :       14420 :   for (c = code->block; c; c = c->block)
    2988                 :             :     {
    2989                 :        9188 :       cp = c->ext.block.case_list;
    2990                 :             : 
    2991                 :             :       /* Assume it's the default case.  */
    2992                 :        9188 :       low = NULL_TREE;
    2993                 :        9188 :       high = NULL_TREE;
    2994                 :        9188 :       tmp = NULL_TREE;
    2995                 :             : 
    2996                 :             :       /* Put the default case at the end.  */
    2997                 :        9188 :       if ((!def && !cp->low) || (def && cp->low))
    2998                 :        4594 :         continue;
    2999                 :             : 
    3000                 :        4594 :       if (cp->low && (cp->ts.type == BT_CLASS
    3001                 :        3027 :                       || cp->ts.type == BT_DERIVED))
    3002                 :             :         {
    3003                 :        1808 :           gfc_init_se (&cse, NULL);
    3004                 :        1808 :           gfc_conv_expr_val (&cse, cp->low);
    3005                 :        1808 :           gfc_add_block_to_block (&block, &cse.pre);
    3006                 :        1808 :           low = cse.expr;
    3007                 :             :         }
    3008                 :        2786 :       else if (cp->ts.type != BT_UNKNOWN)
    3009                 :             :         {
    3010                 :        1219 :           gcc_assert (cp->high);
    3011                 :        1219 :           gfc_init_se (&cse, NULL);
    3012                 :        1219 :           gfc_conv_expr_val (&cse, cp->high);
    3013                 :        1219 :           gfc_add_block_to_block (&block, &cse.pre);
    3014                 :        1219 :           high = cse.expr;
    3015                 :             :         }
    3016                 :             : 
    3017                 :        4594 :       gfc_init_block (&body);
    3018                 :             : 
    3019                 :             :       /* Add the statements for this case.  */
    3020                 :        4594 :       tmp = gfc_trans_code (c->next);
    3021                 :        4594 :       gfc_add_expr_to_block (&body, tmp);
    3022                 :             : 
    3023                 :             :       /* Break to the end of the SELECT TYPE construct.  The default
    3024                 :             :          case just falls through.  */
    3025                 :        4594 :       if (!def)
    3026                 :             :         {
    3027                 :        3027 :           TREE_USED (code->exit_label) = 1;
    3028                 :        3027 :           tmp = build1_v (GOTO_EXPR, code->exit_label);
    3029                 :        3027 :           gfc_add_expr_to_block (&body, tmp);
    3030                 :             :         }
    3031                 :             : 
    3032                 :        4594 :       tmp = gfc_finish_block (&body);
    3033                 :             : 
    3034                 :        4594 :       if (low != NULL_TREE)
    3035                 :             :         {
    3036                 :             :           /* Compare vtable pointers.  */
    3037                 :        1808 :           cond = fold_build2_loc (input_location, EQ_EXPR,
    3038                 :        1808 :                                   TREE_TYPE (se.expr), se.expr, low);
    3039                 :        1808 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    3040                 :             :                                  cond, tmp,
    3041                 :             :                                  build_empty_stmt (input_location));
    3042                 :             :         }
    3043                 :        2786 :       else if (high != NULL_TREE)
    3044                 :             :         {
    3045                 :             :           /* Compare hash values for character cases.  */
    3046                 :        1219 :           gfc_init_se (&cse, NULL);
    3047                 :        1219 :           gfc_conv_expr_val (&cse, e);
    3048                 :        1219 :           gfc_add_block_to_block (&block, &cse.pre);
    3049                 :             : 
    3050                 :        1219 :           cond = fold_build2_loc (input_location, EQ_EXPR,
    3051                 :        1219 :                                   TREE_TYPE (se.expr), high, cse.expr);
    3052                 :        1219 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    3053                 :             :                                  cond, tmp,
    3054                 :             :                                  build_empty_stmt (input_location));
    3055                 :             :         }
    3056                 :             : 
    3057                 :        4594 :       gfc_add_expr_to_block (&block, tmp);
    3058                 :             :     }
    3059                 :             : 
    3060                 :        5232 :   if (!def)
    3061                 :             :     {
    3062                 :        2616 :       def = true;
    3063                 :        2616 :       goto repeat;
    3064                 :             :     }
    3065                 :             : 
    3066                 :        2616 :   gfc_free_expr (e);
    3067                 :             : 
    3068                 :        2616 :   return gfc_finish_block (&block);
    3069                 :             : }
    3070                 :             : 
    3071                 :             : 
    3072                 :             : /* Translate the SELECT CASE construct for INTEGER case expressions,
    3073                 :             :    without killing all potential optimizations.  The problem is that
    3074                 :             :    Fortran allows unbounded cases, but the back-end does not, so we
    3075                 :             :    need to intercept those before we enter the equivalent SWITCH_EXPR
    3076                 :             :    we can build.
    3077                 :             : 
    3078                 :             :    For example, we translate this,
    3079                 :             : 
    3080                 :             :    SELECT CASE (expr)
    3081                 :             :       CASE (:100,101,105:115)
    3082                 :             :          block_1
    3083                 :             :       CASE (190:199,200:)
    3084                 :             :          block_2
    3085                 :             :       CASE (300)
    3086                 :             :          block_3
    3087                 :             :       CASE DEFAULT
    3088                 :             :          block_4
    3089                 :             :    END SELECT
    3090                 :             : 
    3091                 :             :    to the GENERIC equivalent,
    3092                 :             : 
    3093                 :             :      switch (expr)
    3094                 :             :        {
    3095                 :             :          case (minimum value for typeof(expr) ... 100:
    3096                 :             :          case 101:
    3097                 :             :          case 105 ... 114:
    3098                 :             :            block1:
    3099                 :             :            goto end_label;
    3100                 :             : 
    3101                 :             :          case 200 ... (maximum value for typeof(expr):
    3102                 :             :          case 190 ... 199:
    3103                 :             :            block2;
    3104                 :             :            goto end_label;
    3105                 :             : 
    3106                 :             :          case 300:
    3107                 :             :            block_3;
    3108                 :             :            goto end_label;
    3109                 :             : 
    3110                 :             :          default:
    3111                 :             :            block_4;
    3112                 :             :            goto end_label;
    3113                 :             :        }
    3114                 :             : 
    3115                 :             :      end_label:  */
    3116                 :             : 
    3117                 :             : static tree
    3118                 :         844 : gfc_trans_integer_select (gfc_code * code)
    3119                 :             : {
    3120                 :         844 :   gfc_code *c;
    3121                 :         844 :   gfc_case *cp;
    3122                 :         844 :   tree end_label;
    3123                 :         844 :   tree tmp;
    3124                 :         844 :   gfc_se se;
    3125                 :         844 :   stmtblock_t block;
    3126                 :         844 :   stmtblock_t body;
    3127                 :             : 
    3128                 :         844 :   gfc_start_block (&block);
    3129                 :             : 
    3130                 :             :   /* Calculate the switch expression.  */
    3131                 :         844 :   gfc_init_se (&se, NULL);
    3132                 :         844 :   gfc_conv_expr_val (&se, code->expr1);
    3133                 :         844 :   gfc_add_block_to_block (&block, &se.pre);
    3134                 :             : 
    3135                 :         844 :   end_label = gfc_build_label_decl (NULL_TREE);
    3136                 :             : 
    3137                 :         844 :   gfc_init_block (&body);
    3138                 :             : 
    3139                 :        2497 :   for (c = code->block; c; c = c->block)
    3140                 :             :     {
    3141                 :        3378 :       for (cp = c->ext.block.case_list; cp; cp = cp->next)
    3142                 :             :         {
    3143                 :        1725 :           tree low, high;
    3144                 :        1725 :           tree label;
    3145                 :             : 
    3146                 :             :           /* Assume it's the default case.  */
    3147                 :        1725 :           low = high = NULL_TREE;
    3148                 :             : 
    3149                 :        1725 :           if (cp->low)
    3150                 :             :             {
    3151                 :        1352 :               low = gfc_conv_mpz_to_tree (cp->low->value.integer,
    3152                 :             :                                           cp->low->ts.kind);
    3153                 :             : 
    3154                 :             :               /* If there's only a lower bound, set the high bound to the
    3155                 :             :                  maximum value of the case expression.  */
    3156                 :        1352 :               if (!cp->high)
    3157                 :          45 :                 high = TYPE_MAX_VALUE (TREE_TYPE (se.expr));
    3158                 :             :             }
    3159                 :             : 
    3160                 :        1725 :           if (cp->high)
    3161                 :             :             {
    3162                 :             :               /* Three cases are possible here:
    3163                 :             : 
    3164                 :             :                  1) There is no lower bound, e.g. CASE (:N).
    3165                 :             :                  2) There is a lower bound .NE. high bound, that is
    3166                 :             :                     a case range, e.g. CASE (N:M) where M>N (we make
    3167                 :             :                     sure that M>N during type resolution).
    3168                 :             :                  3) There is a lower bound, and it has the same value
    3169                 :             :                     as the high bound, e.g. CASE (N:N).  This is our
    3170                 :             :                     internal representation of CASE(N).
    3171                 :             : 
    3172                 :             :                  In the first and second case, we need to set a value for
    3173                 :             :                  high.  In the third case, we don't because the GCC middle
    3174                 :             :                  end represents a single case value by just letting high be
    3175                 :             :                  a NULL_TREE.  We can't do that because we need to be able
    3176                 :             :                  to represent unbounded cases.  */
    3177                 :             : 
    3178                 :        1351 :               if (!cp->low
    3179                 :        1307 :                   || (mpz_cmp (cp->low->value.integer,
    3180                 :        1307 :                                 cp->high->value.integer) != 0))
    3181                 :          73 :                 high = gfc_conv_mpz_to_tree (cp->high->value.integer,
    3182                 :             :                                              cp->high->ts.kind);
    3183                 :             : 
    3184                 :             :               /* Unbounded case.  */
    3185                 :        1351 :               if (!cp->low)
    3186                 :          44 :                 low = TYPE_MIN_VALUE (TREE_TYPE (se.expr));
    3187                 :             :             }
    3188                 :             : 
    3189                 :             :           /* Build a label.  */
    3190                 :        1725 :           label = gfc_build_label_decl (NULL_TREE);
    3191                 :             : 
    3192                 :             :           /* Add this case label.
    3193                 :             :              Add parameter 'label', make it match GCC backend.  */
    3194                 :        1725 :           tmp = build_case_label (low, high, label);
    3195                 :        1725 :           gfc_add_expr_to_block (&body, tmp);
    3196                 :             :         }
    3197                 :             : 
    3198                 :             :       /* Add the statements for this case.  */
    3199                 :        1653 :       tmp = gfc_trans_code (c->next);
    3200                 :        1653 :       gfc_add_expr_to_block (&body, tmp);
    3201                 :             : 
    3202                 :             :       /* Break to the end of the construct.  */
    3203                 :        1653 :       tmp = build1_v (GOTO_EXPR, end_label);
    3204                 :        1653 :       gfc_add_expr_to_block (&body, tmp);
    3205                 :             :     }
    3206                 :             : 
    3207                 :         844 :   tmp = gfc_finish_block (&body);
    3208                 :         844 :   tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, se.expr, tmp);
    3209                 :         844 :   gfc_add_expr_to_block (&block, tmp);
    3210                 :             : 
    3211                 :         844 :   tmp = build1_v (LABEL_EXPR, end_label);
    3212                 :         844 :   gfc_add_expr_to_block (&block, tmp);
    3213                 :             : 
    3214                 :         844 :   return gfc_finish_block (&block);
    3215                 :             : }
    3216                 :             : 
    3217                 :             : 
    3218                 :             : /* Translate the SELECT CASE construct for LOGICAL case expressions.
    3219                 :             : 
    3220                 :             :    There are only two cases possible here, even though the standard
    3221                 :             :    does allow three cases in a LOGICAL SELECT CASE construct: .TRUE.,
    3222                 :             :    .FALSE., and DEFAULT.
    3223                 :             : 
    3224                 :             :    We never generate more than two blocks here.  Instead, we always
    3225                 :             :    try to eliminate the DEFAULT case.  This way, we can translate this
    3226                 :             :    kind of SELECT construct to a simple
    3227                 :             : 
    3228                 :             :    if {} else {};
    3229                 :             : 
    3230                 :             :    expression in GENERIC.  */
    3231                 :             : 
    3232                 :             : static tree
    3233                 :          54 : gfc_trans_logical_select (gfc_code * code)
    3234                 :             : {
    3235                 :          54 :   gfc_code *c;
    3236                 :          54 :   gfc_code *t, *f, *d;
    3237                 :          54 :   gfc_case *cp;
    3238                 :          54 :   gfc_se se;
    3239                 :          54 :   stmtblock_t block;
    3240                 :             : 
    3241                 :             :   /* Assume we don't have any cases at all.  */
    3242                 :          54 :   t = f = d = NULL;
    3243                 :             : 
    3244                 :             :   /* Now see which ones we actually do have.  We can have at most two
    3245                 :             :      cases in a single case list: one for .TRUE. and one for .FALSE.
    3246                 :             :      The default case is always separate.  If the cases for .TRUE. and
    3247                 :             :      .FALSE. are in the same case list, the block for that case list
    3248                 :             :      always executed, and we don't generate code a COND_EXPR.  */
    3249                 :         171 :   for (c = code->block; c; c = c->block)
    3250                 :             :     {
    3251                 :         243 :       for (cp = c->ext.block.case_list; cp; cp = cp->next)
    3252                 :             :         {
    3253                 :         126 :           if (cp->low)
    3254                 :             :             {
    3255                 :          72 :               if (cp->low->value.logical == 0) /* .FALSE.  */
    3256                 :             :                 f = c;
    3257                 :             :               else /* if (cp->value.logical != 0), thus .TRUE.  */
    3258                 :          36 :                 t = c;
    3259                 :             :             }
    3260                 :             :           else
    3261                 :             :             d = c;
    3262                 :             :         }
    3263                 :             :     }
    3264                 :             : 
    3265                 :             :   /* Start a new block.  */
    3266                 :          54 :   gfc_start_block (&block);
    3267                 :             : 
    3268                 :             :   /* Calculate the switch expression.  We always need to do this
    3269                 :             :      because it may have side effects.  */
    3270                 :          54 :   gfc_init_se (&se, NULL);
    3271                 :          54 :   gfc_conv_expr_val (&se, code->expr1);
    3272                 :          54 :   gfc_add_block_to_block (&block, &se.pre);
    3273                 :             : 
    3274                 :          54 :   if (t == f && t != NULL)
    3275                 :             :     {
    3276                 :             :       /* Cases for .TRUE. and .FALSE. are in the same block.  Just
    3277                 :             :          translate the code for these cases, append it to the current
    3278                 :             :          block.  */
    3279                 :           9 :       gfc_add_expr_to_block (&block, gfc_trans_code (t->next));
    3280                 :             :     }
    3281                 :             :   else
    3282                 :             :     {
    3283                 :          45 :       tree true_tree, false_tree, stmt;
    3284                 :             : 
    3285                 :          45 :       true_tree = build_empty_stmt (input_location);
    3286                 :          45 :       false_tree = build_empty_stmt (input_location);
    3287                 :             : 
    3288                 :             :       /* If we have a case for .TRUE. and for .FALSE., discard the default case.
    3289                 :             :           Otherwise, if .TRUE. or .FALSE. is missing and there is a default case,
    3290                 :             :           make the missing case the default case.  */
    3291                 :          45 :       if (t != NULL && f != NULL)
    3292                 :          63 :         d = NULL;
    3293                 :          36 :       else if (d != NULL)
    3294                 :             :         {
    3295                 :          36 :           if (t == NULL)
    3296                 :             :             t = d;
    3297                 :             :           else
    3298                 :             :             f = d;
    3299                 :             :         }
    3300                 :             : 
    3301                 :             :       /* Translate the code for each of these blocks, and append it to
    3302                 :             :          the current block.  */
    3303                 :          18 :       if (t != NULL)
    3304                 :          45 :         true_tree = gfc_trans_code (t->next);
    3305                 :             : 
    3306                 :          45 :       if (f != NULL)
    3307                 :          45 :         false_tree = gfc_trans_code (f->next);
    3308                 :             : 
    3309                 :          45 :       stmt = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    3310                 :             :                               se.expr, true_tree, false_tree);
    3311                 :          45 :       gfc_add_expr_to_block (&block, stmt);
    3312                 :             :     }
    3313                 :             : 
    3314                 :          54 :   return gfc_finish_block (&block);
    3315                 :             : }
    3316                 :             : 
    3317                 :             : 
    3318                 :             : /* The jump table types are stored in static variables to avoid
    3319                 :             :    constructing them from scratch every single time.  */
    3320                 :             : static GTY(()) tree select_struct[2];
    3321                 :             : 
    3322                 :             : /* Translate the SELECT CASE construct for CHARACTER case expressions.
    3323                 :             :    Instead of generating compares and jumps, it is far simpler to
    3324                 :             :    generate a data structure describing the cases in order and call a
    3325                 :             :    library subroutine that locates the right case.
    3326                 :             :    This is particularly true because this is the only case where we
    3327                 :             :    might have to dispose of a temporary.
    3328                 :             :    The library subroutine returns a pointer to jump to or NULL if no
    3329                 :             :    branches are to be taken.  */
    3330                 :             : 
    3331                 :             : static tree
    3332                 :          85 : gfc_trans_character_select (gfc_code *code)
    3333                 :             : {
    3334                 :          85 :   tree init, end_label, tmp, type, case_num, label, fndecl;
    3335                 :          85 :   stmtblock_t block, body;
    3336                 :          85 :   gfc_case *cp, *d;
    3337                 :          85 :   gfc_code *c;
    3338                 :          85 :   gfc_se se, expr1se;
    3339                 :          85 :   int n, k;
    3340                 :          85 :   vec<constructor_elt, va_gc> *inits = NULL;
    3341                 :             : 
    3342                 :          85 :   tree pchartype = gfc_get_pchar_type (code->expr1->ts.kind);
    3343                 :             : 
    3344                 :             :   /* The jump table types are stored in static variables to avoid
    3345                 :             :      constructing them from scratch every single time.  */
    3346                 :          85 :   static tree ss_string1[2], ss_string1_len[2];
    3347                 :          85 :   static tree ss_string2[2], ss_string2_len[2];
    3348                 :          85 :   static tree ss_target[2];
    3349                 :             : 
    3350                 :          85 :   cp = code->block->ext.block.case_list;
    3351                 :         271 :   while (cp->left != NULL)
    3352                 :             :     cp = cp->left;
    3353                 :             : 
    3354                 :             :   /* Generate the body */
    3355                 :          85 :   gfc_start_block (&block);
    3356                 :          85 :   gfc_init_se (&expr1se, NULL);
    3357                 :          85 :   gfc_conv_expr_reference (&expr1se, code->expr1);
    3358                 :             : 
    3359                 :          85 :   gfc_add_block_to_block (&block, &expr1se.pre);
    3360                 :             : 
    3361                 :          85 :   end_label = gfc_build_label_decl (NULL_TREE);
    3362                 :             : 
    3363                 :          85 :   gfc_init_block (&body);
    3364                 :             : 
    3365                 :             :   /* Attempt to optimize length 1 selects.  */
    3366                 :          85 :   if (integer_onep (expr1se.string_length))
    3367                 :             :     {
    3368                 :         181 :       for (d = cp; d; d = d->right)
    3369                 :             :         {
    3370                 :         155 :           gfc_charlen_t i;
    3371                 :         155 :           if (d->low)
    3372                 :             :             {
    3373                 :         135 :               gcc_assert (d->low->expr_type == EXPR_CONSTANT
    3374                 :             :                           && d->low->ts.type == BT_CHARACTER);
    3375                 :         135 :               if (d->low->value.character.length > 1)
    3376                 :             :                 {
    3377                 :          12 :                   for (i = 1; i < d->low->value.character.length; i++)
    3378                 :          12 :                     if (d->low->value.character.string[i] != ' ')
    3379                 :             :                       break;
    3380                 :          12 :                   if (i != d->low->value.character.length)
    3381                 :             :                     {
    3382                 :          12 :                       if (optimize && d->high && i == 1)
    3383                 :             :                         {
    3384                 :          12 :                           gcc_assert (d->high->expr_type == EXPR_CONSTANT
    3385                 :             :                                       && d->high->ts.type == BT_CHARACTER);
    3386                 :          12 :                           if (d->high->value.character.length > 1
    3387                 :          12 :                               && (d->low->value.character.string[0]
    3388                 :          12 :                                   == d->high->value.character.string[0])
    3389                 :          12 :                               && d->high->value.character.string[1] != ' '
    3390                 :          24 :                               && ((d->low->value.character.string[1] < ' ')
    3391                 :             :                                   == (d->high->value.character.string[1]
    3392                 :          12 :                                       < ' ')))
    3393                 :          12 :                             continue;
    3394                 :             :                         }
    3395                 :             :                       break;
    3396                 :             :                     }
    3397                 :             :                 }
    3398                 :             :             }
    3399                 :         143 :           if (d->high)
    3400                 :             :             {
    3401                 :         123 :               gcc_assert (d->high->expr_type == EXPR_CONSTANT
    3402                 :             :                           && d->high->ts.type == BT_CHARACTER);
    3403                 :         123 :               if (d->high->value.character.length > 1)
    3404                 :             :                 {
    3405                 :          18 :                   for (i = 1; i < d->high->value.character.length; i++)
    3406                 :          12 :                     if (d->high->value.character.string[i] != ' ')
    3407                 :             :                       break;
    3408                 :           6 :                   if (i != d->high->value.character.length)
    3409                 :             :                     break;
    3410                 :             :                 }
    3411                 :             :             }
    3412                 :             :         }
    3413                 :          26 :       if (d == NULL)
    3414                 :             :         {
    3415                 :          26 :           tree ctype = gfc_get_char_type (code->expr1->ts.kind);
    3416                 :             : 
    3417                 :         108 :           for (c = code->block; c; c = c->block)
    3418                 :             :             {
    3419                 :         237 :               for (cp = c->ext.block.case_list; cp; cp = cp->next)
    3420                 :             :                 {
    3421                 :         155 :                   tree low, high;
    3422                 :         155 :                   tree label;
    3423                 :         155 :                   gfc_char_t r;
    3424                 :             : 
    3425                 :             :                   /* Assume it's the default case.  */
    3426                 :         155 :                   low = high = NULL_TREE;
    3427                 :             : 
    3428                 :         155 :                   if (cp->low)
    3429                 :             :                     {
    3430                 :             :                       /* CASE ('ab') or CASE ('ab':'az') will never match
    3431                 :             :                          any length 1 character.  */
    3432                 :         135 :                       if (cp->low->value.character.length > 1
    3433                 :          12 :                           && cp->low->value.character.string[1] != ' ')
    3434                 :          12 :                         continue;
    3435                 :             : 
    3436                 :         123 :                       if (cp->low->value.character.length > 0)
    3437                 :         117 :                         r = cp->low->value.character.string[0];
    3438                 :             :                       else
    3439                 :             :                         r = ' ';
    3440                 :         123 :                       low = build_int_cst (ctype, r);
    3441                 :             : 
    3442                 :             :                       /* If there's only a lower bound, set the high bound
    3443                 :             :                          to the maximum value of the case expression.  */
    3444                 :         123 :                       if (!cp->high)
    3445                 :           0 :                         high = TYPE_MAX_VALUE (ctype);
    3446                 :             :                     }
    3447                 :             : 
    3448                 :         143 :                   if (cp->high)
    3449                 :             :                     {
    3450                 :         123 :                       if (!cp->low
    3451                 :         123 :                           || (cp->low->value.character.string[0]
    3452                 :         123 :                               != cp->high->value.character.string[0]))
    3453                 :             :                         {
    3454                 :           7 :                           if (cp->high->value.character.length > 0)
    3455                 :           7 :                             r = cp->high->value.character.string[0];
    3456                 :             :                           else
    3457                 :             :                             r = ' ';
    3458                 :           7 :                           high = build_int_cst (ctype, r);
    3459                 :             :                         }
    3460                 :             : 
    3461                 :             :                       /* Unbounded case.  */
    3462                 :         123 :                       if (!cp->low)
    3463                 :           0 :                         low = TYPE_MIN_VALUE (ctype);
    3464                 :             :                     }
    3465                 :             : 
    3466                 :             :                   /* Build a label.  */
    3467                 :         143 :                   label = gfc_build_label_decl (NULL_TREE);
    3468                 :             : 
    3469                 :             :                   /* Add this case label.
    3470                 :             :                      Add parameter 'label', make it match GCC backend.  */
    3471                 :         143 :                   tmp = build_case_label (low, high, label);
    3472                 :         143 :                   gfc_add_expr_to_block (&body, tmp);
    3473                 :             :                 }
    3474                 :             : 
    3475                 :             :               /* Add the statements for this case.  */
    3476                 :          82 :               tmp = gfc_trans_code (c->next);
    3477                 :          82 :               gfc_add_expr_to_block (&body, tmp);
    3478                 :             : 
    3479                 :             :               /* Break to the end of the construct.  */
    3480                 :          82 :               tmp = build1_v (GOTO_EXPR, end_label);
    3481                 :          82 :               gfc_add_expr_to_block (&body, tmp);
    3482                 :             :             }
    3483                 :             : 
    3484                 :          52 :           tmp = gfc_string_to_single_character (expr1se.string_length,
    3485                 :             :                                                 expr1se.expr,
    3486                 :          26 :                                                 code->expr1->ts.kind);
    3487                 :          26 :           case_num = gfc_create_var (ctype, "case_num");
    3488                 :          26 :           gfc_add_modify (&block, case_num, tmp);
    3489                 :             : 
    3490                 :          26 :           gfc_add_block_to_block (&block, &expr1se.post);
    3491                 :             : 
    3492                 :          26 :           tmp = gfc_finish_block (&body);
    3493                 :          26 :           tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE,
    3494                 :             :                                  case_num, tmp);
    3495                 :          26 :           gfc_add_expr_to_block (&block, tmp);
    3496                 :             : 
    3497                 :          26 :           tmp = build1_v (LABEL_EXPR, end_label);
    3498                 :          26 :           gfc_add_expr_to_block (&block, tmp);
    3499                 :             : 
    3500                 :          26 :           return gfc_finish_block (&block);
    3501                 :             :         }
    3502                 :             :     }
    3503                 :             : 
    3504                 :          59 :   if (code->expr1->ts.kind == 1)
    3505                 :             :     k = 0;
    3506                 :           6 :   else if (code->expr1->ts.kind == 4)
    3507                 :             :     k = 1;
    3508                 :             :   else
    3509                 :           0 :     gcc_unreachable ();
    3510                 :             : 
    3511                 :          59 :   if (select_struct[k] == NULL)
    3512                 :             :     {
    3513                 :          53 :       tree *chain = NULL;
    3514                 :          53 :       select_struct[k] = make_node (RECORD_TYPE);
    3515                 :             : 
    3516                 :          53 :       if (code->expr1->ts.kind == 1)
    3517                 :          47 :         TYPE_NAME (select_struct[k]) = get_identifier ("_jump_struct_char1");
    3518                 :           6 :       else if (code->expr1->ts.kind == 4)
    3519                 :           6 :         TYPE_NAME (select_struct[k]) = get_identifier ("_jump_struct_char4");
    3520                 :             :       else
    3521                 :           0 :         gcc_unreachable ();
    3522                 :             : 
    3523                 :             : #undef ADD_FIELD
    3524                 :             : #define ADD_FIELD(NAME, TYPE)                                               \
    3525                 :             :   ss_##NAME[k] = gfc_add_field_to_struct (select_struct[k],                 \
    3526                 :             :                                           get_identifier (stringize(NAME)), \
    3527                 :             :                                           TYPE,                             \
    3528                 :             :                                           &chain)
    3529                 :             : 
    3530                 :          53 :       ADD_FIELD (string1, pchartype);
    3531                 :          53 :       ADD_FIELD (string1_len, gfc_charlen_type_node);
    3532                 :             : 
    3533                 :          53 :       ADD_FIELD (string2, pchartype);
    3534                 :          53 :       ADD_FIELD (string2_len, gfc_charlen_type_node);
    3535                 :             : 
    3536                 :          53 :       ADD_FIELD (target, integer_type_node);
    3537                 :             : #undef ADD_FIELD
    3538                 :             : 
    3539                 :          53 :       gfc_finish_type (select_struct[k]);
    3540                 :             :     }
    3541                 :             : 
    3542                 :             :   n = 0;
    3543                 :         311 :   for (d = cp; d; d = d->right)
    3544                 :         252 :     d->n = n++;
    3545                 :             : 
    3546                 :         263 :   for (c = code->block; c; c = c->block)
    3547                 :             :     {
    3548                 :         456 :       for (d = c->ext.block.case_list; d; d = d->next)
    3549                 :             :         {
    3550                 :         252 :           label = gfc_build_label_decl (NULL_TREE);
    3551                 :         452 :           tmp = build_case_label ((d->low == NULL && d->high == NULL)
    3552                 :             :                                   ? NULL
    3553                 :         200 :                                   : build_int_cst (integer_type_node, d->n),
    3554                 :             :                                   NULL, label);
    3555                 :         252 :           gfc_add_expr_to_block (&body, tmp);
    3556                 :             :         }
    3557                 :             : 
    3558                 :         204 :       tmp = gfc_trans_code (c->next);
    3559                 :         204 :       gfc_add_expr_to_block (&body, tmp);
    3560                 :             : 
    3561                 :         204 :       tmp = build1_v (GOTO_EXPR, end_label);
    3562                 :         204 :       gfc_add_expr_to_block (&body, tmp);
    3563                 :             :     }
    3564                 :             : 
    3565                 :             :   /* Generate the structure describing the branches */
    3566                 :         311 :   for (d = cp; d; d = d->right)
    3567                 :             :     {
    3568                 :         252 :       vec<constructor_elt, va_gc> *node = NULL;
    3569                 :             : 
    3570                 :         252 :       gfc_init_se (&se, NULL);
    3571                 :             : 
    3572                 :         252 :       if (d->low == NULL)
    3573                 :             :         {
    3574                 :          52 :           CONSTRUCTOR_APPEND_ELT (node, ss_string1[k], null_pointer_node);
    3575                 :          52 :           CONSTRUCTOR_APPEND_ELT (node, ss_string1_len[k], build_zero_cst (gfc_charlen_type_node));
    3576                 :             :         }
    3577                 :             :       else
    3578                 :             :         {
    3579                 :         200 :           gfc_conv_expr_reference (&se, d->low);
    3580                 :             : 
    3581                 :         200 :           CONSTRUCTOR_APPEND_ELT (node, ss_string1[k], se.expr);
    3582                 :         200 :           CONSTRUCTOR_APPEND_ELT (node, ss_string1_len[k], se.string_length);
    3583                 :             :         }
    3584                 :             : 
    3585                 :         252 :       if (d->high == NULL)
    3586                 :             :         {
    3587                 :          52 :           CONSTRUCTOR_APPEND_ELT (node, ss_string2[k], null_pointer_node);
    3588                 :          52 :           CONSTRUCTOR_APPEND_ELT (node, ss_string2_len[k], build_zero_cst (gfc_charlen_type_node));
    3589                 :             :         }
    3590                 :             :       else
    3591                 :             :         {
    3592                 :         200 :           gfc_init_se (&se, NULL);
    3593                 :         200 :           gfc_conv_expr_reference (&se, d->high);
    3594                 :             : 
    3595                 :         200 :           CONSTRUCTOR_APPEND_ELT (node, ss_string2[k], se.expr);
    3596                 :         200 :           CONSTRUCTOR_APPEND_ELT (node, ss_string2_len[k], se.string_length);
    3597                 :             :         }
    3598                 :             : 
    3599                 :         252 :       CONSTRUCTOR_APPEND_ELT (node, ss_target[k],
    3600                 :             :                               build_int_cst (integer_type_node, d->n));
    3601                 :             : 
    3602                 :         252 :       tmp = build_constructor (select_struct[k], node);
    3603                 :         252 :       CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, tmp);
    3604                 :             :     }
    3605                 :             : 
    3606                 :          59 :   type = build_array_type (select_struct[k],
    3607                 :          59 :                            build_index_type (size_int (n-1)));
    3608                 :             : 
    3609                 :          59 :   init = build_constructor (type, inits);
    3610                 :          59 :   TREE_CONSTANT (init) = 1;
    3611                 :          59 :   TREE_STATIC (init) = 1;
    3612                 :             :   /* Create a static variable to hold the jump table.  */
    3613                 :          59 :   tmp = gfc_create_var (type, "jumptable");
    3614                 :          59 :   TREE_CONSTANT (tmp) = 1;
    3615                 :          59 :   TREE_STATIC (tmp) = 1;
    3616                 :          59 :   TREE_READONLY (tmp) = 1;
    3617                 :          59 :   DECL_INITIAL (tmp) = init;
    3618                 :          59 :   init = tmp;
    3619                 :             : 
    3620                 :             :   /* Build the library call */
    3621                 :          59 :   init = gfc_build_addr_expr (pvoid_type_node, init);
    3622                 :             : 
    3623                 :          59 :   if (code->expr1->ts.kind == 1)
    3624                 :          53 :     fndecl = gfor_fndecl_select_string;
    3625                 :           6 :   else if (code->expr1->ts.kind == 4)
    3626                 :           6 :     fndecl = gfor_fndecl_select_string_char4;
    3627                 :             :   else
    3628                 :           0 :     gcc_unreachable ();
    3629                 :             : 
    3630                 :          59 :   tmp = build_call_expr_loc (input_location,
    3631                 :             :                          fndecl, 4, init,
    3632                 :             :                          build_int_cst (gfc_charlen_type_node, n),
    3633                 :             :                          expr1se.expr, expr1se.string_length);
    3634                 :          59 :   case_num = gfc_create_var (integer_type_node, "case_num");
    3635                 :          59 :   gfc_add_modify (&block, case_num, tmp);
    3636                 :             : 
    3637                 :          59 :   gfc_add_block_to_block (&block, &expr1se.post);
    3638                 :             : 
    3639                 :          59 :   tmp = gfc_finish_block (&body);
    3640                 :          59 :   tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE,
    3641                 :             :                          case_num, tmp);
    3642                 :          59 :   gfc_add_expr_to_block (&block, tmp);
    3643                 :             : 
    3644                 :          59 :   tmp = build1_v (LABEL_EXPR, end_label);
    3645                 :          59 :   gfc_add_expr_to_block (&block, tmp);
    3646                 :             : 
    3647                 :          59 :   return gfc_finish_block (&block);
    3648                 :             : }
    3649                 :             : 
    3650                 :             : 
    3651                 :             : /* Translate the three variants of the SELECT CASE construct.
    3652                 :             : 
    3653                 :             :    SELECT CASEs with INTEGER case expressions can be translated to an
    3654                 :             :    equivalent GENERIC switch statement, and for LOGICAL case
    3655                 :             :    expressions we build one or two if-else compares.
    3656                 :             : 
    3657                 :             :    SELECT CASEs with CHARACTER case expressions are a whole different
    3658                 :             :    story, because they don't exist in GENERIC.  So we sort them and
    3659                 :             :    do a binary search at runtime.
    3660                 :             : 
    3661                 :             :    Fortran has no BREAK statement, and it does not allow jumps from
    3662                 :             :    one case block to another.  That makes things a lot easier for
    3663                 :             :    the optimizers.  */
    3664                 :             : 
    3665                 :             : tree
    3666                 :         985 : gfc_trans_select (gfc_code * code)
    3667                 :             : {
    3668                 :         985 :   stmtblock_t block;
    3669                 :         985 :   tree body;
    3670                 :         985 :   tree exit_label;
    3671                 :             : 
    3672                 :         985 :   gcc_assert (code && code->expr1);
    3673                 :         985 :   gfc_init_block (&block);
    3674                 :             : 
    3675                 :             :   /* Build the exit label and hang it in.  */
    3676                 :         985 :   exit_label = gfc_build_label_decl (NULL_TREE);
    3677                 :         985 :   code->exit_label = exit_label;
    3678                 :             : 
    3679                 :             :   /* Empty SELECT constructs are legal.  */
    3680                 :         985 :   if (code->block == NULL)
    3681                 :           2 :     body = build_empty_stmt (input_location);
    3682                 :             : 
    3683                 :             :   /* Select the correct translation function.  */
    3684                 :             :   else
    3685                 :         983 :     switch (code->expr1->ts.type)
    3686                 :             :       {
    3687                 :          54 :       case BT_LOGICAL:
    3688                 :          54 :         body = gfc_trans_logical_select (code);
    3689                 :          54 :         break;
    3690                 :             : 
    3691                 :         844 :       case BT_INTEGER:
    3692                 :         844 :         body = gfc_trans_integer_select (code);
    3693                 :         844 :         break;
    3694                 :             : 
    3695                 :          85 :       case BT_CHARACTER:
    3696                 :          85 :         body = gfc_trans_character_select (code);
    3697                 :          85 :         break;
    3698                 :             : 
    3699                 :           0 :       default:
    3700                 :           0 :         gfc_internal_error ("gfc_trans_select(): Bad type for case expr.");
    3701                 :             :         /* Not reached */
    3702                 :             :       }
    3703                 :             : 
    3704                 :             :   /* Build everything together.  */
    3705                 :         985 :   gfc_add_expr_to_block (&block, body);
    3706                 :         985 :   gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
    3707                 :             : 
    3708                 :         985 :   return gfc_finish_block (&block);
    3709                 :             : }
    3710                 :             : 
    3711                 :             : tree
    3712                 :        2616 : gfc_trans_select_type (gfc_code * code)
    3713                 :             : {
    3714                 :        2616 :   stmtblock_t block;
    3715                 :        2616 :   tree body;
    3716                 :        2616 :   tree exit_label;
    3717                 :             : 
    3718                 :        2616 :   gcc_assert (code && code->expr1);
    3719                 :        2616 :   gfc_init_block (&block);
    3720                 :             : 
    3721                 :             :   /* Build the exit label and hang it in.  */
    3722                 :        2616 :   exit_label = gfc_build_label_decl (NULL_TREE);
    3723                 :        2616 :   code->exit_label = exit_label;
    3724                 :             : 
    3725                 :             :   /* Empty SELECT constructs are legal.  */
    3726                 :        2616 :   if (code->block == NULL)
    3727                 :           0 :     body = build_empty_stmt (input_location);
    3728                 :             :   else
    3729                 :        2616 :     body = gfc_trans_select_type_cases (code);
    3730                 :             : 
    3731                 :             :   /* Build everything together.  */
    3732                 :        2616 :   gfc_add_expr_to_block (&block, body);
    3733                 :             : 
    3734                 :        2616 :   if (TREE_USED (exit_label))
    3735                 :        2453 :     gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
    3736                 :             : 
    3737                 :        2616 :   return gfc_finish_block (&block);
    3738                 :             : }
    3739                 :             : 
    3740                 :             : 
    3741                 :             : static tree
    3742                 :         987 : gfc_trans_select_rank_cases (gfc_code * code)
    3743                 :             : {
    3744                 :         987 :   gfc_code *c;
    3745                 :         987 :   gfc_case *cp;
    3746                 :         987 :   tree tmp;
    3747                 :         987 :   tree cond;
    3748                 :         987 :   tree low;
    3749                 :         987 :   tree rank;
    3750                 :         987 :   gfc_se se;
    3751                 :         987 :   gfc_se cse;
    3752                 :         987 :   stmtblock_t block;
    3753                 :         987 :   stmtblock_t body;
    3754                 :         987 :   bool def = false;
    3755                 :             : 
    3756                 :         987 :   gfc_start_block (&block);
    3757                 :             : 
    3758                 :             :   /* Calculate the switch expression.  */
    3759                 :         987 :   gfc_init_se (&se, NULL);
    3760                 :         987 :   gfc_conv_expr_descriptor (&se, code->expr1);
    3761                 :         987 :   rank = gfc_conv_descriptor_rank (se.expr);
    3762                 :         987 :   rank = gfc_evaluate_now (rank, &block);
    3763                 :         987 :   symbol_attribute attr = gfc_expr_attr (code->expr1);
    3764                 :         987 :   if (!attr.pointer && !attr.allocatable)
    3765                 :             :     {
    3766                 :             :       /* Special case for assumed-rank ('rank(*)', internally -1):
    3767                 :             :          rank = (rank == 0 || ubound[rank-1] != -1) ? rank : -1.  */
    3768                 :         753 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3769                 :         753 :                               rank, build_int_cst (TREE_TYPE (rank), 0));
    3770                 :         753 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    3771                 :             :                              fold_convert (gfc_array_index_type, rank),
    3772                 :             :                              gfc_index_one_node);
    3773                 :         753 :       tmp = gfc_conv_descriptor_ubound_get (se.expr, tmp);
    3774                 :         753 :       tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    3775                 :         753 :                              tmp, build_int_cst (TREE_TYPE (tmp), -1));
    3776                 :         753 :       cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    3777                 :             :                               logical_type_node, cond, tmp);
    3778                 :         753 :       tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (rank),
    3779                 :         753 :                              cond, rank, build_int_cst (TREE_TYPE (rank), -1));
    3780                 :         753 :       rank = gfc_evaluate_now (tmp, &block);
    3781                 :             :     }
    3782                 :         987 :   TREE_USED (code->exit_label) = 0;
    3783                 :             : 
    3784                 :        1974 : repeat:
    3785                 :        6462 :   for (c = code->block; c; c = c->block)
    3786                 :             :     {
    3787                 :        4488 :       cp = c->ext.block.case_list;
    3788                 :             : 
    3789                 :             :       /* Assume it's the default case.  */
    3790                 :        4488 :       low = NULL_TREE;
    3791                 :        4488 :       tmp = NULL_TREE;
    3792                 :             : 
    3793                 :             :       /* Put the default case at the end.  */
    3794                 :        4488 :       if ((!def && !cp->low) || (def && cp->low))
    3795                 :        2244 :         continue;
    3796                 :             : 
    3797                 :        2244 :       if (cp->low)
    3798                 :             :         {
    3799                 :        1329 :           gfc_init_se (&cse, NULL);
    3800                 :        1329 :           gfc_conv_expr_val (&cse, cp->low);
    3801                 :        1329 :           gfc_add_block_to_block (&block, &cse.pre);
    3802                 :        1329 :           low = cse.expr;
    3803                 :             :         }
    3804                 :             : 
    3805                 :        2244 :       gfc_init_block (&body);
    3806                 :             : 
    3807                 :             :       /* Add the statements for this case.  */
    3808                 :        2244 :       tmp = gfc_trans_code (c->next);
    3809                 :        2244 :       gfc_add_expr_to_block (&body, tmp);
    3810                 :             : 
    3811                 :             :       /* Break to the end of the SELECT RANK construct.  The default
    3812                 :             :          case just falls through.  */
    3813                 :        2244 :       if (!def)
    3814                 :             :         {
    3815                 :        1329 :           TREE_USED (code->exit_label) = 1;
    3816                 :        1329 :           tmp = build1_v (GOTO_EXPR, code->exit_label);
    3817                 :        1329 :           gfc_add_expr_to_block (&body, tmp);
    3818                 :             :         }
    3819                 :             : 
    3820                 :        2244 :       tmp = gfc_finish_block (&body);
    3821                 :             : 
    3822                 :        2244 :       if (low != NULL_TREE)
    3823                 :             :         {
    3824                 :        2658 :           cond = fold_build2_loc (input_location, EQ_EXPR,
    3825                 :        1329 :                                   TREE_TYPE (rank), rank,
    3826                 :        1329 :                                   fold_convert (TREE_TYPE (rank), low));
    3827                 :        1329 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    3828                 :             :                                  cond, tmp,
    3829                 :             :                                  build_empty_stmt (input_location));
    3830                 :             :         }
    3831                 :             : 
    3832                 :        2244 :       gfc_add_expr_to_block (&block, tmp);
    3833                 :             :     }
    3834                 :             : 
    3835                 :        1974 :   if (!def)
    3836                 :             :     {
    3837                 :         987 :       def = true;
    3838                 :         987 :       goto repeat;
    3839                 :             :     }
    3840                 :             : 
    3841                 :         987 :   return gfc_finish_block (&block);
    3842                 :             : }
    3843                 :             : 
    3844                 :             : 
    3845                 :             : tree
    3846                 :         988 : gfc_trans_select_rank (gfc_code * code)
    3847                 :             : {
    3848                 :         988 :   stmtblock_t block;
    3849                 :         988 :   tree body;
    3850                 :         988 :   tree exit_label;
    3851                 :             : 
    3852                 :         988 :   gcc_assert (code && code->expr1);
    3853                 :         988 :   gfc_init_block (&block);
    3854                 :             : 
    3855                 :             :   /* Build the exit label and hang it in.  */
    3856                 :         988 :   exit_label = gfc_build_label_decl (NULL_TREE);
    3857                 :         988 :   code->exit_label = exit_label;
    3858                 :             : 
    3859                 :             :   /* Empty SELECT constructs are legal.  */
    3860                 :         988 :   if (code->block == NULL)
    3861                 :           1 :     body = build_empty_stmt (input_location);
    3862                 :             :   else
    3863                 :         987 :     body = gfc_trans_select_rank_cases (code);
    3864                 :             : 
    3865                 :             :   /* Build everything together.  */
    3866                 :         988 :   gfc_add_expr_to_block (&block, body);
    3867                 :             : 
    3868                 :         988 :   if (TREE_USED (exit_label))
    3869                 :         988 :     gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
    3870                 :             : 
    3871                 :         988 :   return gfc_finish_block (&block);
    3872                 :             : }
    3873                 :             : 
    3874                 :             : 
    3875                 :             : /* Traversal function to substitute a replacement symtree if the symbol
    3876                 :             :    in the expression is the same as that passed.  f == 2 signals that
    3877                 :             :    that variable itself is not to be checked - only the references.
    3878                 :             :    This group of functions is used when the variable expression in a
    3879                 :             :    FORALL assignment has internal references.  For example:
    3880                 :             :                 FORALL (i = 1:4) p(p(i)) = i
    3881                 :             :    The only recourse here is to store a copy of 'p' for the index
    3882                 :             :    expression.  */
    3883                 :             : 
    3884                 :             : static gfc_symtree *new_symtree;
    3885                 :             : static gfc_symtree *old_symtree;
    3886                 :             : 
    3887                 :             : static bool
    3888                 :         710 : forall_replace (gfc_expr *expr, gfc_symbol *sym, int *f)
    3889                 :             : {
    3890                 :         710 :   if (expr->expr_type != EXPR_VARIABLE)
    3891                 :             :     return false;
    3892                 :             : 
    3893                 :         406 :   if (*f == 2)
    3894                 :          62 :     *f = 1;
    3895                 :         344 :   else if (expr->symtree->n.sym == sym)
    3896                 :          72 :     expr->symtree = new_symtree;
    3897                 :             : 
    3898                 :             :   return false;
    3899                 :             : }
    3900                 :             : 
    3901                 :             : static void
    3902                 :         124 : forall_replace_symtree (gfc_expr *e, gfc_symbol *sym, int f)
    3903                 :             : {
    3904                 :           0 :   gfc_traverse_expr (e, sym, forall_replace, f);
    3905                 :           0 : }
    3906                 :             : 
    3907                 :             : static bool
    3908                 :         710 : forall_restore (gfc_expr *expr,
    3909                 :             :                 gfc_symbol *sym ATTRIBUTE_UNUSED,
    3910                 :             :                 int *f ATTRIBUTE_UNUSED)
    3911                 :             : {
    3912                 :         710 :   if (expr->expr_type != EXPR_VARIABLE)
    3913                 :             :     return false;
    3914                 :             : 
    3915                 :         406 :   if (expr->symtree == new_symtree)
    3916                 :          72 :     expr->symtree = old_symtree;
    3917                 :             : 
    3918                 :             :   return false;
    3919                 :             : }
    3920                 :             : 
    3921                 :             : static void
    3922                 :         124 : forall_restore_symtree (gfc_expr *e)
    3923                 :             : {
    3924                 :           0 :   gfc_traverse_expr (e, NULL, forall_restore, 0);
    3925                 :           0 : }
    3926                 :             : 
    3927                 :             : static void
    3928                 :          62 : forall_make_variable_temp (gfc_code *c, stmtblock_t *pre, stmtblock_t *post)
    3929                 :             : {
    3930                 :          62 :   gfc_se tse;
    3931                 :          62 :   gfc_se rse;
    3932                 :          62 :   gfc_expr *e;
    3933                 :          62 :   gfc_symbol *new_sym;
    3934                 :          62 :   gfc_symbol *old_sym;
    3935                 :          62 :   gfc_symtree *root;
    3936                 :          62 :   tree tmp;
    3937                 :             : 
    3938                 :             :   /* Build a copy of the lvalue.  */
    3939                 :          62 :   old_symtree = c->expr1->symtree;
    3940                 :          62 :   old_sym = old_symtree->n.sym;
    3941                 :          62 :   e = gfc_lval_expr_from_sym (old_sym);
    3942                 :          62 :   if (old_sym->attr.dimension)
    3943                 :             :     {
    3944                 :          30 :       gfc_init_se (&tse, NULL);
    3945                 :          30 :       gfc_conv_subref_array_arg (&tse, e, 0, INTENT_IN, false);
    3946                 :          30 :       gfc_add_block_to_block (pre, &tse.pre);
    3947                 :          30 :       gfc_add_block_to_block (post, &tse.post);
    3948                 :          30 :       tse.expr = build_fold_indirect_ref_loc (input_location, tse.expr);
    3949                 :             : 
    3950                 :          30 :       if (c->expr1->ref->u.ar.type != AR_SECTION)
    3951                 :             :         {
    3952                 :             :           /* Use the variable offset for the temporary.  */
    3953                 :          24 :           tmp = gfc_conv_array_offset (old_sym->backend_decl);
    3954                 :          24 :           gfc_conv_descriptor_offset_set (pre, tse.expr, tmp);
    3955                 :             :         }
    3956                 :             :     }
    3957                 :             :   else
    3958                 :             :     {
    3959                 :          32 :       gfc_init_se (&tse, NULL);
    3960                 :          32 :       gfc_init_se (&rse, NULL);
    3961                 :          32 :       gfc_conv_expr (&rse, e);
    3962                 :          32 :       if (e->ts.type == BT_CHARACTER)
    3963                 :             :         {
    3964                 :          32 :           tse.string_length = rse.string_length;
    3965                 :          32 :           tmp = gfc_get_character_type_len (gfc_default_character_kind,
    3966                 :             :                                             tse.string_length);
    3967                 :          32 :           tse.expr = gfc_conv_string_tmp (&tse, build_pointer_type (tmp),
    3968                 :             :                                           rse.string_length);
    3969                 :          32 :           gfc_add_block_to_block (pre, &tse.pre);
    3970                 :          32 :           gfc_add_block_to_block (post, &tse.post);
    3971                 :             :         }
    3972                 :             :       else
    3973                 :             :         {
    3974                 :           0 :           tmp = gfc_typenode_for_spec (&e->ts);
    3975                 :           0 :           tse.expr = gfc_create_var (tmp, "temp");
    3976                 :             :         }
    3977                 :             : 
    3978                 :          64 :       tmp = gfc_trans_scalar_assign (&tse, &rse, e->ts,
    3979                 :          32 :                                      e->expr_type == EXPR_VARIABLE, false);
    3980                 :          32 :       gfc_add_expr_to_block (pre, tmp);
    3981                 :             :     }
    3982                 :          62 :   gfc_free_expr (e);
    3983                 :             : 
    3984                 :             :   /* Create a new symbol to represent the lvalue.  */
    3985                 :          62 :   new_sym = gfc_new_symbol (old_sym->name, NULL);
    3986                 :          62 :   new_sym->ts = old_sym->ts;
    3987                 :          62 :   new_sym->attr.referenced = 1;
    3988                 :          62 :   new_sym->attr.temporary = 1;
    3989                 :          62 :   new_sym->attr.dimension = old_sym->attr.dimension;
    3990                 :          62 :   new_sym->attr.flavor = old_sym->attr.flavor;
    3991                 :             : 
    3992                 :             :   /* Use the temporary as the backend_decl.  */
    3993                 :          62 :   new_sym->backend_decl = tse.expr;
    3994                 :             : 
    3995                 :             :   /* Create a fake symtree for it.  */
    3996                 :          62 :   root = NULL;
    3997                 :          62 :   new_symtree = gfc_new_symtree (&root, old_sym->name);
    3998                 :          62 :   new_symtree->n.sym = new_sym;
    3999                 :          62 :   gcc_assert (new_symtree == root);
    4000                 :             : 
    4001                 :             :   /* Go through the expression reference replacing the old_symtree
    4002                 :             :      with the new.  */
    4003                 :          62 :   forall_replace_symtree (c->expr1, old_sym, 2);
    4004                 :             : 
    4005                 :             :   /* Now we have made this temporary, we might as well use it for
    4006                 :             :   the right hand side.  */
    4007                 :          62 :   forall_replace_symtree (c->expr2, old_sym, 1);
    4008                 :          62 : }
    4009                 :             : 
    4010                 :             : 
    4011                 :             : /* Handles dependencies in forall assignments.  */
    4012                 :             : static int
    4013                 :        1838 : check_forall_dependencies (gfc_code *c, stmtblock_t *pre, stmtblock_t *post)
    4014                 :             : {
    4015                 :        1838 :   gfc_ref *lref;
    4016                 :        1838 :   gfc_ref *rref;
    4017                 :        1838 :   int need_temp;
    4018                 :        1838 :   gfc_symbol *lsym;
    4019                 :             : 
    4020                 :        1838 :   lsym = c->expr1->symtree->n.sym;
    4021                 :        1838 :   need_temp = gfc_check_dependency (c->expr1, c->expr2, 0);
    4022                 :             : 
    4023                 :             :   /* Now check for dependencies within the 'variable'
    4024                 :             :      expression itself.  These are treated by making a complete
    4025                 :             :      copy of variable and changing all the references to it
    4026                 :             :      point to the copy instead.  Note that the shallow copy of
    4027                 :             :      the variable will not suffice for derived types with
    4028                 :             :      pointer components.  We therefore leave these to their
    4029                 :             :      own devices.  Likewise for allocatable components.  */
    4030                 :        1838 :   if (lsym->ts.type == BT_DERIVED
    4031                 :         148 :       && (lsym->ts.u.derived->attr.pointer_comp
    4032                 :         148 :           || lsym->ts.u.derived->attr.alloc_comp))
    4033                 :             :     return need_temp;
    4034                 :             : 
    4035                 :        1744 :   new_symtree = NULL;
    4036                 :        1744 :   if (find_forall_index (c->expr1, lsym, 2))
    4037                 :             :     {
    4038                 :          12 :       forall_make_variable_temp (c, pre, post);
    4039                 :          12 :       need_temp = 0;
    4040                 :             :     }
    4041                 :             : 
    4042                 :             :   /* Substrings with dependencies are treated in the same
    4043                 :             :      way.  */
    4044                 :        1744 :   if (c->expr1->ts.type == BT_CHARACTER
    4045                 :         684 :         && c->expr1->ref
    4046                 :         684 :         && c->expr2->expr_type == EXPR_VARIABLE
    4047                 :         491 :         && lsym == c->expr2->symtree->n.sym)
    4048                 :             :     {
    4049                 :         124 :       for (lref = c->expr1->ref; lref; lref = lref->next)
    4050                 :         117 :         if (lref->type == REF_SUBSTRING)
    4051                 :             :           break;
    4052                 :         124 :       for (rref = c->expr2->ref; rref; rref = rref->next)
    4053                 :         117 :         if (rref->type == REF_SUBSTRING)
    4054                 :             :           break;
    4055                 :             : 
    4056                 :          81 :       if (rref && lref
    4057                 :          81 :             && gfc_dep_compare_expr (rref->u.ss.start, lref->u.ss.start) < 0)
    4058                 :             :         {
    4059                 :          50 :           forall_make_variable_temp (c, pre, post);
    4060                 :          50 :           need_temp = 0;
    4061                 :             :         }
    4062                 :             :     }
    4063                 :             :   return need_temp;
    4064                 :             : }
    4065                 :             : 
    4066                 :             : 
    4067                 :             : static void
    4068                 :          62 : cleanup_forall_symtrees (gfc_code *c)
    4069                 :             : {
    4070                 :          62 :   forall_restore_symtree (c->expr1);
    4071                 :          62 :   forall_restore_symtree (c->expr2);
    4072                 :          62 :   free (new_symtree->n.sym);
    4073                 :          62 :   free (new_symtree);
    4074                 :          62 : }
    4075                 :             : 
    4076                 :             : 
    4077                 :             : /* Generate the loops for a FORALL block, specified by FORALL_TMP.  BODY
    4078                 :             :    is the contents of the FORALL block/stmt to be iterated.  MASK_FLAG
    4079                 :             :    indicates whether we should generate code to test the FORALLs mask
    4080                 :             :    array.  OUTER is the loop header to be used for initializing mask
    4081                 :             :    indices.
    4082                 :             : 
    4083                 :             :    The generated loop format is:
    4084                 :             :     count = (end - start + step) / step
    4085                 :             :     loopvar = start
    4086                 :             :     while (1)
    4087                 :             :       {
    4088                 :             :         if (count <=0 )
    4089                 :             :           goto end_of_loop
    4090                 :             :         <body>
    4091                 :             :         loopvar += step
    4092                 :             :         count --
    4093                 :             :       }
    4094                 :             :     end_of_loop:  */
    4095                 :             : 
    4096                 :             : static tree
    4097                 :        3470 : gfc_trans_forall_loop (forall_info *forall_tmp, tree body,
    4098                 :             :                        int mask_flag, stmtblock_t *outer)
    4099                 :             : {
    4100                 :        3470 :   int n, nvar;
    4101                 :        3470 :   tree tmp;
    4102                 :        3470 :   tree cond;
    4103                 :        3470 :   stmtblock_t block;
    4104                 :        3470 :   tree exit_label;
    4105                 :        3470 :   tree count;
    4106                 :        3470 :   tree var, start, end, step;
    4107                 :        3470 :   iter_info *iter;
    4108                 :             : 
    4109                 :             :   /* Initialize the mask index outside the FORALL nest.  */
    4110                 :        3470 :   if (mask_flag && forall_tmp->mask)
    4111                 :        1097 :     gfc_add_modify (outer, forall_tmp->maskindex, gfc_index_zero_node);
    4112                 :             : 
    4113                 :        3470 :   iter = forall_tmp->this_loop;
    4114                 :        3470 :   nvar = forall_tmp->nvar;
    4115                 :        9659 :   for (n = 0; n < nvar; n++)
    4116                 :             :     {
    4117                 :        6189 :       var = iter->var;
    4118                 :        6189 :       start = iter->start;
    4119                 :        6189 :       end = iter->end;
    4120                 :        6189 :       step = iter->step;
    4121                 :             : 
    4122                 :        6189 :       exit_label = gfc_build_label_decl (NULL_TREE);
    4123                 :        6189 :       TREE_USED (exit_label) = 1;
    4124                 :             : 
    4125                 :             :       /* The loop counter.  */
    4126                 :        6189 :       count = gfc_create_var (TREE_TYPE (var), "count");
    4127                 :             : 
    4128                 :             :       /* The body of the loop.  */
    4129                 :        6189 :       gfc_init_block (&block);
    4130                 :             : 
    4131                 :             :       /* The exit condition.  */
    4132                 :        6189 :       cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    4133                 :        6189 :                               count, build_int_cst (TREE_TYPE (count), 0));
    4134                 :             : 
    4135                 :             :       /* PR 83064 means that we cannot use annot_expr_parallel_kind until
    4136                 :             :        the autoparallelizer can handle this.  */
    4137                 :        6189 :       if (forall_tmp->do_concurrent || iter->annot.ivdep)
    4138                 :          70 :         cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    4139                 :             :                        build_int_cst (integer_type_node,
    4140                 :          70 :                                       annot_expr_ivdep_kind),
    4141                 :             :                        integer_zero_node);
    4142                 :             : 
    4143                 :        6189 :       if (iter->annot.unroll && cond != error_mark_node)
    4144                 :           1 :         cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    4145                 :             :                        build_int_cst (integer_type_node,
    4146                 :           1 :                                       annot_expr_unroll_kind),
    4147                 :           1 :                        build_int_cst (integer_type_node, iter->annot.unroll));
    4148                 :             : 
    4149                 :        6189 :       if (iter->annot.vector && cond != error_mark_node)
    4150                 :           1 :         cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    4151                 :             :                        build_int_cst (integer_type_node,
    4152                 :           1 :                                       annot_expr_vector_kind),
    4153                 :             :                        integer_zero_node);
    4154                 :             : 
    4155                 :        6189 :       if (iter->annot.novector && cond != error_mark_node)
    4156                 :           2 :         cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
    4157                 :             :                        build_int_cst (integer_type_node,
    4158                 :           2 :                                       annot_expr_no_vector_kind),
    4159                 :             :                        integer_zero_node);
    4160                 :             : 
    4161                 :        6189 :       tmp = build1_v (GOTO_EXPR, exit_label);
    4162                 :        6189 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    4163                 :             :                              cond, tmp, build_empty_stmt (input_location));
    4164                 :        6189 :       gfc_add_expr_to_block (&block, tmp);
    4165                 :             : 
    4166                 :             :       /* The main loop body.  */
    4167                 :        6189 :       gfc_add_expr_to_block (&block, body);
    4168                 :             : 
    4169                 :             :       /* Increment the loop variable.  */
    4170                 :        6189 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var,
    4171                 :             :                              step);
    4172                 :        6189 :       gfc_add_modify (&block, var, tmp);
    4173                 :             : 
    4174                 :             :       /* Advance to the next mask element.  Only do this for the
    4175                 :             :          innermost loop.  */
    4176                 :        6189 :       if (n == 0 && mask_flag && forall_tmp->mask)
    4177                 :             :         {
    4178                 :        1097 :           tree maskindex = forall_tmp->maskindex;
    4179                 :        1097 :           tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4180                 :             :                                  maskindex, gfc_index_one_node);
    4181                 :        1097 :           gfc_add_modify (&block, maskindex, tmp);
    4182                 :             :         }
    4183                 :             : 
    4184                 :             :       /* Decrement the loop counter.  */
    4185                 :        6189 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (var), count,
    4186                 :        6189 :                              build_int_cst (TREE_TYPE (var), 1));
    4187                 :        6189 :       gfc_add_modify (&block, count, tmp);
    4188                 :             : 
    4189                 :        6189 :       body = gfc_finish_block (&block);
    4190                 :             : 
    4191                 :             :       /* Loop var initialization.  */
    4192                 :        6189 :       gfc_init_block (&block);
    4193                 :        6189 :       gfc_add_modify (&block, var, start);
    4194                 :             : 
    4195                 :             : 
    4196                 :             :       /* Initialize the loop counter.  */
    4197                 :        6189 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (var), step,
    4198                 :             :                              start);
    4199                 :        6189 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), end,
    4200                 :             :                              tmp);
    4201                 :        6189 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (var),
    4202                 :             :                              tmp, step);
    4203                 :        6189 :       gfc_add_modify (&block, count, tmp);
    4204                 :             : 
    4205                 :             :       /* The loop expression.  */
    4206                 :        6189 :       tmp = build1_v (LOOP_EXPR, body);
    4207                 :        6189 :       gfc_add_expr_to_block (&block, tmp);
    4208                 :             : 
    4209                 :             :       /* The exit label.  */
    4210                 :        6189 :       tmp = build1_v (LABEL_EXPR, exit_label);
    4211                 :        6189 :       gfc_add_expr_to_block (&block, tmp);
    4212                 :             : 
    4213                 :        6189 :       body = gfc_finish_block (&block);
    4214                 :        6189 :       iter = iter->next;
    4215                 :             :     }
    4216                 :        3470 :   return body;
    4217                 :             : }
    4218                 :             : 
    4219                 :             : 
    4220                 :             : /* Generate the body and loops according to MASK_FLAG.  If MASK_FLAG
    4221                 :             :    is nonzero, the body is controlled by all masks in the forall nest.
    4222                 :             :    Otherwise, the innermost loop is not controlled by it's mask.  This
    4223                 :             :    is used for initializing that mask.  */
    4224                 :             : 
    4225                 :             : static tree
    4226                 :        3260 : gfc_trans_nested_forall_loop (forall_info * nested_forall_info, tree body,
    4227                 :             :                               int mask_flag)
    4228                 :             : {
    4229                 :        3260 :   tree tmp;
    4230                 :        3260 :   stmtblock_t header;
    4231                 :        3260 :   forall_info *forall_tmp;
    4232                 :        3260 :   tree mask, maskindex;
    4233                 :             : 
    4234                 :        3260 :   gfc_start_block (&header);
    4235                 :             : 
    4236                 :        3260 :   forall_tmp = nested_forall_info;
    4237                 :        9990 :   while (forall_tmp != NULL)
    4238                 :             :     {
    4239                 :             :       /* Generate body with masks' control.  */
    4240                 :        3470 :       if (mask_flag)
    4241                 :             :         {
    4242                 :        2727 :           mask = forall_tmp->mask;
    4243                 :        2727 :           maskindex = forall_tmp->maskindex;
    4244                 :             : 
    4245                 :             :           /* If a mask was specified make the assignment conditional.  */
    4246                 :        2727 :           if (mask)
    4247                 :             :             {
    4248                 :        1097 :               tmp = gfc_build_array_ref (mask, maskindex, NULL);
    4249                 :        1097 :               body = build3_v (COND_EXPR, tmp, body,
    4250                 :             :                                build_empty_stmt (input_location));
    4251                 :             :             }
    4252                 :             :         }
    4253                 :        3470 :       body = gfc_trans_forall_loop (forall_tmp, body, mask_flag, &header);
    4254                 :        3470 :       forall_tmp = forall_tmp->prev_nest;
    4255                 :        3470 :       mask_flag = 1;
    4256                 :             :     }
    4257                 :             : 
    4258                 :        3260 :   gfc_add_expr_to_block (&header, body);
    4259                 :        3260 :   return gfc_finish_block (&header);
    4260                 :             : }
    4261                 :             : 
    4262                 :             : 
    4263                 :             : /* Allocate data for holding a temporary array.  Returns either a local
    4264                 :             :    temporary array or a pointer variable.  */
    4265                 :             : 
    4266                 :             : static tree
    4267                 :        1401 : gfc_do_allocate (tree bytesize, tree size, tree * pdata, stmtblock_t * pblock,
    4268                 :             :                  tree elem_type)
    4269                 :             : {
    4270                 :        1401 :   tree tmpvar;
    4271                 :        1401 :   tree type;
    4272                 :        1401 :   tree tmp;
    4273                 :             : 
    4274                 :        1401 :   if (INTEGER_CST_P (size))
    4275                 :        1154 :     tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    4276                 :             :                            size, gfc_index_one_node);
    4277                 :             :   else
    4278                 :             :     tmp = NULL_TREE;
    4279                 :             : 
    4280                 :        1401 :   type = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
    4281                 :        1401 :   type = build_array_type (elem_type, type);
    4282                 :        1401 :   if (gfc_can_put_var_on_stack (bytesize) && INTEGER_CST_P (size))
    4283                 :             :     {
    4284                 :        1154 :       tmpvar = gfc_create_var (type, "temp");
    4285                 :        1154 :       *pdata = NULL_TREE;
    4286                 :             :     }
    4287                 :             :   else
    4288                 :             :     {
    4289                 :         247 :       tmpvar = gfc_create_var (build_pointer_type (type), "temp");
    4290                 :         247 :       *pdata = convert (pvoid_type_node, tmpvar);
    4291                 :             : 
    4292                 :         247 :       tmp = gfc_call_malloc (pblock, TREE_TYPE (tmpvar), bytesize);
    4293                 :         247 :       gfc_add_modify (pblock, tmpvar, tmp);
    4294                 :             :     }
    4295                 :        1401 :   return tmpvar;
    4296                 :             : }
    4297                 :             : 
    4298                 :             : 
    4299                 :             : /* Generate codes to copy the temporary to the actual lhs.  */
    4300                 :             : 
    4301                 :             : static tree
    4302                 :         224 : generate_loop_for_temp_to_lhs (gfc_expr *expr, tree tmp1, tree count3,
    4303                 :             :                                tree count1,
    4304                 :             :                                gfc_ss *lss, gfc_ss *rss,
    4305                 :             :                                tree wheremask, bool invert)
    4306                 :             : {
    4307                 :         224 :   stmtblock_t block, body1;
    4308                 :         224 :   gfc_loopinfo loop;
    4309                 :         224 :   gfc_se lse;
    4310                 :         224 :   gfc_se rse;
    4311                 :         224 :   tree tmp;
    4312                 :         224 :   tree wheremaskexpr;
    4313                 :             : 
    4314                 :         224 :   (void) rss; /* TODO: unused.  */
    4315                 :             : 
    4316                 :         224 :   gfc_start_block (&block);
    4317                 :             : 
    4318                 :         224 :   gfc_init_se (&rse, NULL);
    4319                 :         224 :   gfc_init_se (&lse, NULL);
    4320                 :             : 
    4321                 :         224 :   if (lss == gfc_ss_terminator)
    4322                 :             :     {
    4323                 :         149 :       gfc_init_block (&body1);
    4324                 :         149 :       gfc_conv_expr (&lse, expr);
    4325                 :         149 :       rse.expr = gfc_build_array_ref (tmp1, count1, NULL);
    4326                 :             :     }
    4327                 :             :   else
    4328                 :             :     {
    4329                 :             :       /* Initialize the loop.  */
    4330                 :          75 :       gfc_init_loopinfo (&loop);
    4331                 :             : 
    4332                 :             :       /* We may need LSS to determine the shape of the expression.  */
    4333                 :          75 :       gfc_add_ss_to_loop (&loop, lss);
    4334                 :             : 
    4335                 :          75 :       gfc_conv_ss_startstride (&loop);
    4336                 :          75 :       gfc_conv_loop_setup (&loop, &expr->where);
    4337                 :             : 
    4338                 :          75 :       gfc_mark_ss_chain_used (lss, 1);
    4339                 :             :       /* Start the loop body.  */
    4340                 :          75 :       gfc_start_scalarized_body (&loop, &body1);
    4341                 :             : 
    4342                 :             :       /* Translate the expression.  */
    4343                 :          75 :       gfc_copy_loopinfo_to_se (&lse, &loop);
    4344                 :          75 :       lse.ss = lss;
    4345                 :          75 :       gfc_conv_expr (&lse, expr);
    4346                 :             : 
    4347                 :             :       /* Form the expression of the temporary.  */
    4348                 :          75 :       rse.expr = gfc_build_array_ref (tmp1, count1, NULL);
    4349                 :             :     }
    4350                 :             : 
    4351                 :             :   /* Use the scalar assignment.  */
    4352                 :         224 :   rse.string_length = lse.string_length;
    4353                 :         448 :   tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
    4354                 :         224 :                                  expr->expr_type == EXPR_VARIABLE, false);
    4355                 :             : 
    4356                 :             :   /* Form the mask expression according to the mask tree list.  */
    4357                 :         224 :   if (wheremask)
    4358                 :             :     {
    4359                 :          27 :       wheremaskexpr = gfc_build_array_ref (wheremask, count3, NULL);
    4360                 :          27 :       if (invert)
    4361                 :           0 :         wheremaskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
    4362                 :           0 :                                          TREE_TYPE (wheremaskexpr),
    4363                 :             :                                          wheremaskexpr);
    4364                 :          27 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    4365                 :             :                              wheremaskexpr, tmp,
    4366                 :             :                              build_empty_stmt (input_location));
    4367                 :             :     }
    4368                 :             : 
    4369                 :         224 :   gfc_add_expr_to_block (&body1, tmp);
    4370                 :             : 
    4371                 :         224 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (count1),
    4372                 :             :                          count1, gfc_index_one_node);
    4373                 :         224 :   gfc_add_modify (&body1, count1, tmp);
    4374                 :             : 
    4375                 :         224 :   if (lss == gfc_ss_terminator)
    4376                 :         149 :       gfc_add_block_to_block (&block, &body1);
    4377                 :             :   else
    4378                 :             :     {
    4379                 :             :       /* Increment count3.  */
    4380                 :          75 :       if (count3)
    4381                 :             :         {
    4382                 :          27 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    4383                 :             :                                  gfc_array_index_type,
    4384                 :             :                                  count3, gfc_index_one_node);
    4385                 :          27 :           gfc_add_modify (&body1, count3, tmp);
    4386                 :             :         }
    4387                 :             : 
    4388                 :             :       /* Generate the copying loops.  */
    4389                 :          75 :       gfc_trans_scalarizing_loops (&loop, &body1);
    4390                 :             : 
    4391                 :          75 :       gfc_add_block_to_block (&block, &loop.pre);
    4392                 :          75 :       gfc_add_block_to_block (&block, &loop.post);
    4393                 :             : 
    4394                 :          75 :       gfc_cleanup_loop (&loop);
    4395                 :             :       /* TODO: Reuse lss and rss when copying temp->lhs.  Need to be careful
    4396                 :             :          as tree nodes in SS may not be valid in different scope.  */
    4397                 :             :     }
    4398                 :             : 
    4399                 :         224 :   tmp = gfc_finish_block (&block);
    4400                 :         224 :   return tmp;
    4401                 :             : }
    4402                 :             : 
    4403                 :             : 
    4404                 :             : /* Generate codes to copy rhs to the temporary. TMP1 is the address of
    4405                 :             :    temporary, LSS and RSS are formed in function compute_inner_temp_size(),
    4406                 :             :    and should not be freed.  WHEREMASK is the conditional execution mask
    4407                 :             :    whose sense may be inverted by INVERT.  */
    4408                 :             : 
    4409                 :             : static tree
    4410                 :         224 : generate_loop_for_rhs_to_temp (gfc_expr *expr2, tree tmp1, tree count3,
    4411                 :             :                                tree count1, gfc_ss *lss, gfc_ss *rss,
    4412                 :             :                                tree wheremask, bool invert)
    4413                 :             : {
    4414                 :         224 :   stmtblock_t block, body1;
    4415                 :         224 :   gfc_loopinfo loop;
    4416                 :         224 :   gfc_se lse;
    4417                 :         224 :   gfc_se rse;
    4418                 :         224 :   tree tmp;
    4419                 :         224 :   tree wheremaskexpr;
    4420                 :             : 
    4421                 :         224 :   gfc_start_block (&block);
    4422                 :             : 
    4423                 :         224 :   gfc_init_se (&rse, NULL);
    4424                 :         224 :   gfc_init_se (&lse, NULL);
    4425                 :             : 
    4426                 :         224 :   if (lss == gfc_ss_terminator)
    4427                 :             :     {
    4428                 :         149 :       gfc_init_block (&body1);
    4429                 :         149 :       gfc_conv_expr (&rse, expr2);
    4430                 :         149 :       lse.expr = gfc_build_array_ref (tmp1, count1, NULL);
    4431                 :             :     }
    4432                 :             :   else
    4433                 :             :     {
    4434                 :             :       /* Initialize the loop.  */
    4435                 :          75 :       gfc_init_loopinfo (&loop);
    4436                 :             : 
    4437                 :             :       /* We may need LSS to determine the shape of the expression.  */
    4438                 :          75 :       gfc_add_ss_to_loop (&loop, lss);
    4439                 :          75 :       gfc_add_ss_to_loop (&loop, rss);
    4440                 :             : 
    4441                 :          75 :       gfc_conv_ss_startstride (&loop);
    4442                 :          75 :       gfc_conv_loop_setup (&loop, &expr2->where);
    4443                 :             : 
    4444                 :          75 :       gfc_mark_ss_chain_used (rss, 1);
    4445                 :             :       /* Start the loop body.  */
    4446                 :          75 :       gfc_start_scalarized_body (&loop, &body1);
    4447                 :             : 
    4448                 :             :       /* Translate the expression.  */
    4449                 :          75 :       gfc_copy_loopinfo_to_se (&rse, &loop);
    4450                 :          75 :       rse.ss = rss;
    4451                 :          75 :       gfc_conv_expr (&rse, expr2);
    4452                 :             : 
    4453                 :             :       /* Form the expression of the temporary.  */
    4454                 :          75 :       lse.expr = gfc_build_array_ref (tmp1, count1, NULL);
    4455                 :             :     }
    4456                 :             : 
    4457                 :             :   /* Use the scalar assignment.  */
    4458                 :         224 :   lse.string_length = rse.string_length;
    4459                 :         448 :   tmp = gfc_trans_scalar_assign (&lse, &rse, expr2->ts,
    4460                 :         224 :                                  expr2->expr_type == EXPR_VARIABLE, false);
    4461                 :             : 
    4462                 :             :   /* Form the mask expression according to the mask tree list.  */
    4463                 :         224 :   if (wheremask)
    4464                 :             :     {
    4465                 :          27 :       wheremaskexpr = gfc_build_array_ref (wheremask, count3, NULL);
    4466                 :          27 :       if (invert)
    4467                 :           0 :         wheremaskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
    4468                 :           0 :                                          TREE_TYPE (wheremaskexpr),
    4469                 :             :                                          wheremaskexpr);
    4470                 :          27 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    4471                 :             :                              wheremaskexpr, tmp,
    4472                 :             :                              build_empty_stmt (input_location));
    4473                 :             :     }
    4474                 :             : 
    4475                 :         224 :   gfc_add_expr_to_block (&body1, tmp);
    4476                 :             : 
    4477                 :         224 :   if (lss == gfc_ss_terminator)
    4478                 :             :     {
    4479                 :         149 :       gfc_add_block_to_block (&block, &body1);
    4480                 :             : 
    4481                 :             :       /* Increment count1.  */
    4482                 :         149 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (count1),
    4483                 :             :                              count1, gfc_index_one_node);
    4484                 :         149 :       gfc_add_modify (&block, count1, tmp);
    4485                 :             :     }
    4486                 :             :   else
    4487                 :             :     {
    4488                 :             :       /* Increment count1.  */
    4489                 :          75 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4490                 :             :                              count1, gfc_index_one_node);
    4491                 :          75 :       gfc_add_modify (&body1, count1, tmp);
    4492                 :             : 
    4493                 :             :       /* Increment count3.  */
    4494                 :          75 :       if (count3)
    4495                 :             :         {
    4496                 :          27 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    4497                 :             :                                  gfc_array_index_type,
    4498                 :             :                                  count3, gfc_index_one_node);
    4499                 :          27 :           gfc_add_modify (&body1, count3, tmp);
    4500                 :             :         }
    4501                 :             : 
    4502                 :             :       /* Generate the copying loops.  */
    4503                 :          75 :       gfc_trans_scalarizing_loops (&loop, &body1);
    4504                 :             : 
    4505                 :          75 :       gfc_add_block_to_block (&block, &loop.pre);
    4506                 :          75 :       gfc_add_block_to_block (&block, &loop.post);
    4507                 :             : 
    4508                 :          75 :       gfc_cleanup_loop (&loop);
    4509                 :             :       /* TODO: Reuse lss and rss when copying temp->lhs.  Need to be careful
    4510                 :             :          as tree nodes in SS may not be valid in different scope.  */
    4511                 :             :     }
    4512                 :             : 
    4513                 :         224 :   tmp = gfc_finish_block (&block);
    4514                 :         224 :   return tmp;
    4515                 :             : }
    4516                 :             : 
    4517                 :             : 
    4518                 :             : /* Calculate the size of temporary needed in the assignment inside forall.
    4519                 :             :    LSS and RSS are filled in this function.  */
    4520                 :             : 
    4521                 :             : static tree
    4522                 :         779 : compute_inner_temp_size (gfc_expr *expr1, gfc_expr *expr2,
    4523                 :             :                          stmtblock_t * pblock,
    4524                 :             :                          gfc_ss **lss, gfc_ss **rss)
    4525                 :             : {
    4526                 :         779 :   gfc_loopinfo loop;
    4527                 :         779 :   tree size;
    4528                 :         779 :   int i;
    4529                 :         779 :   int save_flag;
    4530                 :         779 :   tree tmp;
    4531                 :             : 
    4532                 :         779 :   *lss = gfc_walk_expr (expr1);
    4533                 :         779 :   *rss = NULL;
    4534                 :             : 
    4535                 :         779 :   size = gfc_index_one_node;
    4536                 :         779 :   if (*lss != gfc_ss_terminator)
    4537                 :             :     {
    4538                 :         481 :       gfc_init_loopinfo (&loop);
    4539                 :             : 
    4540                 :             :       /* Walk the RHS of the expression.  */
    4541                 :         481 :       *rss = gfc_walk_expr (expr2);
    4542                 :         481 :       if (*rss == gfc_ss_terminator)
    4543                 :             :         /* The rhs is scalar.  Add a ss for the expression.  */
    4544                 :           0 :         *rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
    4545                 :             : 
    4546                 :             :       /* Associate the SS with the loop.  */
    4547                 :         481 :       gfc_add_ss_to_loop (&loop, *lss);
    4548                 :             :       /* We don't actually need to add the rhs at this point, but it might
    4549                 :             :          make guessing the loop bounds a bit easier.  */
    4550                 :         481 :       gfc_add_ss_to_loop (&loop, *rss);
    4551                 :             : 
    4552                 :             :       /* We only want the shape of the expression, not rest of the junk
    4553                 :             :          generated by the scalarizer.  */
    4554                 :         481 :       loop.array_parameter = 1;
    4555                 :             : 
    4556                 :             :       /* Calculate the bounds of the scalarization.  */
    4557                 :         481 :       save_flag = gfc_option.rtcheck;
    4558                 :         481 :       gfc_option.rtcheck &= ~GFC_RTCHECK_BOUNDS;
    4559                 :         481 :       gfc_conv_ss_startstride (&loop);
    4560                 :         481 :       gfc_option.rtcheck = save_flag;
    4561                 :         481 :       gfc_conv_loop_setup (&loop, &expr2->where);
    4562                 :             : 
    4563                 :             :       /* Figure out how many elements we need.  */
    4564                 :        1473 :       for (i = 0; i < loop.dimen; i++)
    4565                 :             :         {
    4566                 :         511 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    4567                 :             :                                  gfc_array_index_type,
    4568                 :             :                                  gfc_index_one_node, loop.from[i]);
    4569                 :         511 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    4570                 :             :                                  gfc_array_index_type, tmp, loop.to[i]);
    4571                 :         511 :           size = fold_build2_loc (input_location, MULT_EXPR,
    4572                 :             :                                   gfc_array_index_type, size, tmp);
    4573                 :             :         }
    4574                 :         481 :       gfc_add_block_to_block (pblock, &loop.pre);
    4575                 :         481 :       size = gfc_evaluate_now (size, pblock);
    4576                 :         481 :       gfc_add_block_to_block (pblock, &loop.post);
    4577                 :             : 
    4578                 :             :       /* TODO: write a function that cleans up a loopinfo without freeing
    4579                 :             :          the SS chains.  Currently a NOP.  */
    4580                 :             :     }
    4581                 :             : 
    4582                 :         779 :   return size;
    4583                 :             : }
    4584                 :             : 
    4585                 :             : 
    4586                 :             : /* Calculate the overall iterator number of the nested forall construct.
    4587                 :             :    This routine actually calculates the number of times the body of the
    4588                 :             :    nested forall specified by NESTED_FORALL_INFO is executed and multiplies
    4589                 :             :    that by the expression INNER_SIZE.  The BLOCK argument specifies the
    4590                 :             :    block in which to calculate the result, and the optional INNER_SIZE_BODY
    4591                 :             :    argument contains any statements that need to executed (inside the loop)
    4592                 :             :    to initialize or calculate INNER_SIZE.  */
    4593                 :             : 
    4594                 :             : static tree
    4595                 :        1318 : compute_overall_iter_number (forall_info *nested_forall_info, tree inner_size,
    4596                 :             :                              stmtblock_t *inner_size_body, stmtblock_t *block)
    4597                 :             : {
    4598                 :        1318 :   forall_info *forall_tmp = nested_forall_info;
    4599                 :        1318 :   tree tmp, number;
    4600                 :        1318 :   stmtblock_t body;
    4601                 :             : 
    4602                 :             :   /* We can eliminate the innermost unconditional loops with constant
    4603                 :             :      array bounds.  */
    4604                 :        1318 :   if (INTEGER_CST_P (inner_size))
    4605                 :             :     {
    4606                 :             :       while (forall_tmp
    4607                 :         272 :              && !forall_tmp->mask
    4608                 :        1526 :              && INTEGER_CST_P (forall_tmp->size))
    4609                 :             :         {
    4610                 :         117 :           inner_size = fold_build2_loc (input_location, MULT_EXPR,
    4611                 :             :                                         gfc_array_index_type,
    4612                 :             :                                         inner_size, forall_tmp->size);
    4613                 :         117 :           forall_tmp = forall_tmp->prev_nest;
    4614                 :             :         }
    4615                 :             : 
    4616                 :             :       /* If there are no loops left, we have our constant result.  */
    4617                 :        1227 :       if (!forall_tmp)
    4618                 :             :         return inner_size;
    4619                 :             :     }
    4620                 :             : 
    4621                 :             :   /* Otherwise, create a temporary variable to compute the result.  */
    4622                 :         246 :   number = gfc_create_var (gfc_array_index_type, "num");
    4623                 :         246 :   gfc_add_modify (block, number, gfc_index_zero_node);
    4624                 :             : 
    4625                 :         246 :   gfc_start_block (&body);
    4626                 :         246 :   if (inner_size_body)
    4627                 :         188 :     gfc_add_block_to_block (&body, inner_size_body);
    4628                 :         246 :   if (forall_tmp)
    4629                 :         230 :     tmp = fold_build2_loc (input_location, PLUS_EXPR,
    4630                 :             :                            gfc_array_index_type, number, inner_size);
    4631                 :             :   else
    4632                 :             :     tmp = inner_size;
    4633                 :         246 :   gfc_add_modify (&body, number, tmp);
    4634                 :         246 :   tmp = gfc_finish_block (&body);
    4635                 :             : 
    4636                 :             :   /* Generate loops.  */
    4637                 :         246 :   if (forall_tmp != NULL)
    4638                 :         230 :     tmp = gfc_trans_nested_forall_loop (forall_tmp, tmp, 1);
    4639                 :             : 
    4640                 :         246 :   gfc_add_expr_to_block (block, tmp);
    4641                 :             : 
    4642                 :         246 :   return number;
    4643                 :             : }
    4644                 :             : 
    4645                 :             : 
    4646                 :             : /* Allocate temporary for forall construct.  SIZE is the size of temporary
    4647                 :             :    needed.  PTEMP1 is returned for space free.  */
    4648                 :             : 
    4649                 :             : static tree
    4650                 :        1401 : allocate_temp_for_forall_nest_1 (tree type, tree size, stmtblock_t * block,
    4651                 :             :                                  tree * ptemp1)
    4652                 :             : {
    4653                 :        1401 :   tree bytesize;
    4654                 :        1401 :   tree unit;
    4655                 :        1401 :   tree tmp;
    4656                 :             : 
    4657                 :        1401 :   unit = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (type));
    4658                 :        1401 :   if (!integer_onep (unit))
    4659                 :         166 :     bytesize = fold_build2_loc (input_location, MULT_EXPR,
    4660                 :             :                                 gfc_array_index_type, size, unit);
    4661                 :             :   else
    4662                 :             :     bytesize = size;
    4663                 :             : 
    4664                 :        1401 :   *ptemp1 = NULL;
    4665                 :        1401 :   tmp = gfc_do_allocate (bytesize, size, ptemp1, block, type);
    4666                 :             : 
    4667                 :        1401 :   if (*ptemp1)
    4668                 :         247 :     tmp = build_fold_indirect_ref_loc (input_location, tmp);
    4669                 :        1401 :   return tmp;
    4670                 :             : }
    4671                 :             : 
    4672                 :             : 
    4673                 :             : /* Allocate temporary for forall construct according to the information in
    4674                 :             :    nested_forall_info.  INNER_SIZE is the size of temporary needed in the
    4675                 :             :    assignment inside forall.  PTEMP1 is returned for space free.  */
    4676                 :             : 
    4677                 :             : static tree
    4678                 :         987 : allocate_temp_for_forall_nest (forall_info * nested_forall_info, tree type,
    4679                 :             :                                tree inner_size, stmtblock_t * inner_size_body,
    4680                 :             :                                stmtblock_t * block, tree * ptemp1)
    4681                 :             : {
    4682                 :         987 :   tree size;
    4683                 :             : 
    4684                 :             :   /* Calculate the total size of temporary needed in forall construct.  */
    4685                 :         987 :   size = compute_overall_iter_number (nested_forall_info, inner_size,
    4686                 :             :                                       inner_size_body, block);
    4687                 :             : 
    4688                 :         987 :   return allocate_temp_for_forall_nest_1 (type, size, block, ptemp1);
    4689                 :             : }
    4690                 :             : 
    4691                 :             : 
    4692                 :             : /* Handle assignments inside forall which need temporary.
    4693                 :             : 
    4694                 :             :     forall (i=start:end:stride; maskexpr)
    4695                 :             :       e<i> = f<i>
    4696                 :             :     end forall
    4697                 :             :    (where e,f<i> are arbitrary expressions possibly involving i
    4698                 :             :     and there is a dependency between e<i> and f<i>)
    4699                 :             :    Translates to:
    4700                 :             :     masktmp(:) = maskexpr(:)
    4701                 :             : 
    4702                 :             :     maskindex = 0;
    4703                 :             :     count1 = 0;
    4704                 :             :     num = 0;
    4705                 :             :     for (i = start; i <= end; i += stride)
    4706                 :             :       num += SIZE (f<i>)
    4707                 :             :     count1 = 0;
    4708                 :             :     ALLOCATE (tmp(num))
    4709                 :             :     for (i = start; i <= end; i += stride)
    4710                 :             :       {
    4711                 :             :         if (masktmp[maskindex++])
    4712                 :             :           tmp[count1++] = f<i>
    4713                 :             :       }
    4714                 :             :     maskindex = 0;
    4715                 :             :     count1 = 0;
    4716                 :             :     for (i = start; i <= end; i += stride)
    4717                 :             :       {
    4718                 :             :         if (masktmp[maskindex++])
    4719                 :             :           e<i> = tmp[count1++]
    4720                 :             :       }
    4721                 :             :     DEALLOCATE (tmp)
    4722                 :             :   */
    4723                 :             : static void
    4724                 :         224 : gfc_trans_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
    4725                 :             :                             tree wheremask, bool invert,
    4726                 :             :                             forall_info * nested_forall_info,
    4727                 :             :                             stmtblock_t * block)
    4728                 :             : {
    4729                 :         224 :   tree type;
    4730                 :         224 :   tree inner_size;
    4731                 :         224 :   gfc_ss *lss, *rss;
    4732                 :         224 :   tree count, count1;
    4733                 :         224 :   tree tmp, tmp1;
    4734                 :         224 :   tree ptemp1;
    4735                 :         224 :   stmtblock_t inner_size_body;
    4736                 :             : 
    4737                 :             :   /* Create vars. count1 is the current iterator number of the nested
    4738                 :             :      forall.  */
    4739                 :         224 :   count1 = gfc_create_var (gfc_array_index_type, "count1");
    4740                 :             : 
    4741                 :             :   /* Count is the wheremask index.  */
    4742                 :         224 :   if (wheremask)
    4743                 :             :     {
    4744                 :          27 :       count = gfc_create_var (gfc_array_index_type, "count");
    4745                 :          27 :       gfc_add_modify (block, count, gfc_index_zero_node);
    4746                 :             :     }
    4747                 :             :   else
    4748                 :             :     count = NULL;
    4749                 :             : 
    4750                 :             :   /* Initialize count1.  */
    4751                 :         224 :   gfc_add_modify (block, count1, gfc_index_zero_node);
    4752                 :             : 
    4753                 :             :   /* Calculate the size of temporary needed in the assignment. Return loop, lss
    4754                 :             :      and rss which are used in function generate_loop_for_rhs_to_temp().  */
    4755                 :             :   /* The type of LHS. Used in function allocate_temp_for_forall_nest */
    4756                 :         224 :   if (expr1->ts.type == BT_CHARACTER)
    4757                 :             :     {
    4758                 :         103 :       type = NULL;
    4759                 :         103 :       if (expr1->ref && expr1->ref->type == REF_SUBSTRING)
    4760                 :             :         {
    4761                 :          72 :           gfc_se ssse;
    4762                 :          72 :           gfc_init_se (&ssse, NULL);
    4763                 :          72 :           gfc_conv_expr (&ssse, expr1);
    4764                 :          72 :           type = gfc_get_character_type_len (gfc_default_character_kind,
    4765                 :             :                                              ssse.string_length);
    4766                 :          72 :         }
    4767                 :             :       else
    4768                 :             :         {
    4769                 :          31 :           if (!expr1->ts.u.cl->backend_decl)
    4770                 :             :             {
    4771                 :           6 :               gfc_se tse;
    4772                 :           6 :               gcc_assert (expr1->ts.u.cl->length);
    4773                 :           6 :               gfc_init_se (&tse, NULL);
    4774                 :           6 :               gfc_conv_expr (&tse, expr1->ts.u.cl->length);
    4775                 :           6 :               expr1->ts.u.cl->backend_decl = tse.expr;
    4776                 :             :             }
    4777                 :          31 :           type = gfc_get_character_type_len (gfc_default_character_kind,
    4778                 :          31 :                                              expr1->ts.u.cl->backend_decl);
    4779                 :             :         }
    4780                 :             :     }
    4781                 :             :   else
    4782                 :         121 :     type = gfc_typenode_for_spec (&expr1->ts);
    4783                 :             : 
    4784                 :         224 :   gfc_init_block (&inner_size_body);
    4785                 :         224 :   inner_size = compute_inner_temp_size (expr1, expr2, &inner_size_body,
    4786                 :             :                                         &lss, &rss);
    4787                 :             : 
    4788                 :             :   /* Allocate temporary for nested forall construct according to the
    4789                 :             :      information in nested_forall_info and inner_size.  */
    4790                 :         224 :   tmp1 = allocate_temp_for_forall_nest (nested_forall_info, type, inner_size,
    4791                 :             :                                         &inner_size_body, block, &ptemp1);
    4792                 :             : 
    4793                 :             :   /* Generate codes to copy rhs to the temporary .  */
    4794                 :         224 :   tmp = generate_loop_for_rhs_to_temp (expr2, tmp1, count, count1, lss, rss,
    4795                 :             :                                        wheremask, invert);
    4796                 :             : 
    4797                 :             :   /* Generate body and loops according to the information in
    4798                 :             :      nested_forall_info.  */
    4799                 :         224 :   tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
    4800                 :         224 :   gfc_add_expr_to_block (block, tmp);
    4801                 :             : 
    4802                 :             :   /* Reset count1.  */
    4803                 :         224 :   gfc_add_modify (block, count1, gfc_index_zero_node);
    4804                 :             : 
    4805                 :             :   /* Reset count.  */
    4806                 :         224 :   if (wheremask)
    4807                 :          27 :     gfc_add_modify (block, count, gfc_index_zero_node);
    4808                 :             : 
    4809                 :             :   /* TODO: Second call to compute_inner_temp_size to initialize lss and
    4810                 :             :      rss;  there must be a better way.  */
    4811                 :         224 :   inner_size = compute_inner_temp_size (expr1, expr2, &inner_size_body,
    4812                 :             :                                         &lss, &rss);
    4813                 :             : 
    4814                 :             :   /* Generate codes to copy the temporary to lhs.  */
    4815                 :         224 :   tmp = generate_loop_for_temp_to_lhs (expr1, tmp1, count, count1,
    4816                 :             :                                        lss, rss,
    4817                 :             :                                        wheremask, invert);
    4818                 :             : 
    4819                 :             :   /* Generate body and loops according to the information in
    4820                 :             :      nested_forall_info.  */
    4821                 :         224 :   tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
    4822                 :         224 :   gfc_add_expr_to_block (block, tmp);
    4823                 :             : 
    4824                 :         224 :   if (ptemp1)
    4825                 :             :     {
    4826                 :             :       /* Free the temporary.  */
    4827                 :         145 :       tmp = gfc_call_free (ptemp1);
    4828                 :         145 :       gfc_add_expr_to_block (block, tmp);
    4829                 :             :     }
    4830                 :         224 : }
    4831                 :             : 
    4832                 :             : 
    4833                 :             : /* Translate pointer assignment inside FORALL which need temporary.  */
    4834                 :             : 
    4835                 :             : static void
    4836                 :          20 : gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
    4837                 :             :                                     forall_info * nested_forall_info,
    4838                 :             :                                     stmtblock_t * block)
    4839                 :             : {
    4840                 :          20 :   tree type;
    4841                 :          20 :   tree inner_size;
    4842                 :          20 :   gfc_ss *lss, *rss;
    4843                 :          20 :   gfc_se lse;
    4844                 :          20 :   gfc_se rse;
    4845                 :          20 :   gfc_array_info *info;
    4846                 :          20 :   gfc_loopinfo loop;
    4847                 :          20 :   tree desc;
    4848                 :          20 :   tree parm;
    4849                 :          20 :   tree parmtype;
    4850                 :          20 :   stmtblock_t body;
    4851                 :          20 :   tree count;
    4852                 :          20 :   tree tmp, tmp1, ptemp1;
    4853                 :             : 
    4854                 :          20 :   count = gfc_create_var (gfc_array_index_type, "count");
    4855                 :          20 :   gfc_add_modify (block, count, gfc_index_zero_node);
    4856                 :             : 
    4857                 :          20 :   inner_size = gfc_index_one_node;
    4858                 :          20 :   lss = gfc_walk_expr (expr1);
    4859                 :          20 :   rss = gfc_walk_expr (expr2);
    4860                 :          20 :   if (lss == gfc_ss_terminator)
    4861                 :             :     {
    4862                 :          11 :       type = gfc_typenode_for_spec (&expr1->ts);
    4863                 :          11 :       type = build_pointer_type (type);
    4864                 :             : 
    4865                 :             :       /* Allocate temporary for nested forall construct according to the
    4866                 :             :          information in nested_forall_info and inner_size.  */
    4867                 :          11 :       tmp1 = allocate_temp_for_forall_nest (nested_forall_info, type,
    4868                 :             :                                             inner_size, NULL, block, &ptemp1);
    4869                 :          11 :       gfc_start_block (&body);
    4870                 :          11 :       gfc_init_se (&lse, NULL);
    4871                 :          11 :       lse.expr = gfc_build_array_ref (tmp1, count, NULL);
    4872                 :          11 :       gfc_init_se (&rse, NULL);
    4873                 :          11 :       rse.want_pointer = 1;
    4874                 :          11 :       gfc_conv_expr (&rse, expr2);
    4875                 :          11 :       gfc_add_block_to_block (&body, &rse.pre);
    4876                 :          11 :       gfc_add_modify (&body, lse.expr,
    4877                 :          11 :                            fold_convert (TREE_TYPE (lse.expr), rse.expr));
    4878                 :          11 :       gfc_add_block_to_block (&body, &rse.post);
    4879                 :             : 
    4880                 :             :       /* Increment count.  */
    4881                 :          11 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4882                 :             :                              count, gfc_index_one_node);
    4883                 :          11 :       gfc_add_modify (&body, count, tmp);
    4884                 :             : 
    4885                 :          11 :       tmp = gfc_finish_block (&body);
    4886                 :             : 
    4887                 :             :       /* Generate body and loops according to the information in
    4888                 :             :          nested_forall_info.  */
    4889                 :          11 :       tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
    4890                 :          11 :       gfc_add_expr_to_block (block, tmp);
    4891                 :             : 
    4892                 :             :       /* Reset count.  */
    4893                 :          11 :       gfc_add_modify (block, count, gfc_index_zero_node);
    4894                 :             : 
    4895                 :          11 :       gfc_start_block (&body);
    4896                 :          11 :       gfc_init_se (&lse, NULL);
    4897                 :          11 :       gfc_init_se (&rse, NULL);
    4898                 :          11 :       rse.expr = gfc_build_array_ref (tmp1, count, NULL);
    4899                 :          11 :       lse.want_pointer = 1;
    4900                 :          11 :       gfc_conv_expr (&lse, expr1);
    4901                 :          11 :       gfc_add_block_to_block (&body, &lse.pre);
    4902                 :          11 :       gfc_add_modify (&body, lse.expr, rse.expr);
    4903                 :          11 :       gfc_add_block_to_block (&body, &lse.post);
    4904                 :             :       /* Increment count.  */
    4905                 :          11 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4906                 :             :                              count, gfc_index_one_node);
    4907                 :          11 :       gfc_add_modify (&body, count, tmp);
    4908                 :          11 :       tmp = gfc_finish_block (&body);
    4909                 :             : 
    4910                 :             :       /* Generate body and loops according to the information in
    4911                 :             :          nested_forall_info.  */
    4912                 :          11 :       tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
    4913                 :          11 :       gfc_add_expr_to_block (block, tmp);
    4914                 :             :     }
    4915                 :             :   else
    4916                 :             :     {
    4917                 :           9 :       gfc_init_loopinfo (&loop);
    4918                 :             : 
    4919                 :             :       /* Associate the SS with the loop.  */
    4920                 :           9 :       gfc_add_ss_to_loop (&loop, rss);
    4921                 :             : 
    4922                 :             :       /* Setup the scalarizing loops and bounds.  */
    4923                 :           9 :       gfc_conv_ss_startstride (&loop);
    4924                 :             : 
    4925                 :           9 :       gfc_conv_loop_setup (&loop, &expr2->where);
    4926                 :             : 
    4927                 :           9 :       info = &rss->info->data.array;
    4928                 :           9 :       desc = info->descriptor;
    4929                 :             : 
    4930                 :             :       /* Make a new descriptor.  */
    4931                 :           9 :       parmtype = gfc_get_element_type (TREE_TYPE (desc));
    4932                 :           9 :       parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, 0,
    4933                 :             :                                             loop.from, loop.to, 1,
    4934                 :             :                                             GFC_ARRAY_UNKNOWN, true);
    4935                 :             : 
    4936                 :             :       /* Allocate temporary for nested forall construct.  */
    4937                 :           9 :       tmp1 = allocate_temp_for_forall_nest (nested_forall_info, parmtype,
    4938                 :             :                                             inner_size, NULL, block, &ptemp1);
    4939                 :           9 :       gfc_start_block (&body);
    4940                 :           9 :       gfc_init_se (&lse, NULL);
    4941                 :           9 :       lse.expr = gfc_build_array_ref (tmp1, count, NULL);
    4942                 :           9 :       lse.direct_byref = 1;
    4943                 :           9 :       gfc_conv_expr_descriptor (&lse, expr2);
    4944                 :             : 
    4945                 :           9 :       gfc_add_block_to_block (&body, &lse.pre);
    4946                 :           9 :       gfc_add_block_to_block (&body, &lse.post);
    4947                 :             : 
    4948                 :             :       /* Increment count.  */
    4949                 :           9 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4950                 :             :                              count, gfc_index_one_node);
    4951                 :           9 :       gfc_add_modify (&body, count, tmp);
    4952                 :             : 
    4953                 :           9 :       tmp = gfc_finish_block (&body);
    4954                 :             : 
    4955                 :             :       /* Generate body and loops according to the information in
    4956                 :             :          nested_forall_info.  */
    4957                 :           9 :       tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
    4958                 :           9 :       gfc_add_expr_to_block (block, tmp);
    4959                 :             : 
    4960                 :             :       /* Reset count.  */
    4961                 :           9 :       gfc_add_modify (block, count, gfc_index_zero_node);
    4962                 :             : 
    4963                 :           9 :       parm = gfc_build_array_ref (tmp1, count, NULL);
    4964                 :           9 :       gfc_init_se (&lse, NULL);
    4965                 :           9 :       gfc_conv_expr_descriptor (&lse, expr1);
    4966                 :           9 :       gfc_add_modify (&lse.pre, lse.expr, parm);
    4967                 :           9 :       gfc_start_block (&body);
    4968                 :           9 :       gfc_add_block_to_block (&body, &lse.pre);
    4969                 :           9 :       gfc_add_block_to_block (&body, &lse.post);
    4970                 :             : 
    4971                 :             :       /* Increment count.  */
    4972                 :           9 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4973                 :             :                              count, gfc_index_one_node);
    4974                 :           9 :       gfc_add_modify (&body, count, tmp);
    4975                 :             : 
    4976                 :           9 :       tmp = gfc_finish_block (&body);
    4977                 :             : 
    4978                 :           9 :       tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
    4979                 :           9 :       gfc_add_expr_to_block (block, tmp);
    4980                 :             :     }
    4981                 :             :   /* Free the temporary.  */
    4982                 :          20 :   if (ptemp1)
    4983                 :             :     {
    4984                 :           1 :       tmp = gfc_call_free (ptemp1);
    4985                 :           1 :       gfc_add_expr_to_block (block, tmp);
    4986                 :             :     }
    4987                 :          20 : }
    4988                 :             : 
    4989                 :             : 
    4990                 :             : /* FORALL and WHERE statements are really nasty, especially when you nest
    4991                 :             :    them. All the rhs of a forall assignment must be evaluated before the
    4992                 :             :    actual assignments are performed. Presumably this also applies to all the
    4993                 :             :    assignments in an inner where statement.  */
    4994                 :             : 
    4995                 :             : /* Generate code for a FORALL statement.  Any temporaries are allocated as a
    4996                 :             :    linear array, relying on the fact that we process in the same order in all
    4997                 :             :    loops.
    4998                 :             : 
    4999                 :             :     forall (i=start:end:stride; maskexpr)
    5000                 :             :       e<i> = f<i>
    5001                 :             :       g<i> = h<i>
    5002                 :             :     end forall
    5003                 :             :    (where e,f,g,h<i> are arbitrary expressions possibly involving i)
    5004                 :             :    Translates to:
    5005                 :             :     count = ((end + 1 - start) / stride)
    5006                 :             :     masktmp(:) = maskexpr(:)
    5007                 :             : 
    5008                 :             :     maskindex = 0;
    5009                 :             :     for (i = start; i <= end; i += stride)
    5010                 :             :       {
    5011                 :             :         if (masktmp[maskindex++])
    5012                 :             :           e<i> = f<i>
    5013                 :             :       }
    5014                 :             :     maskindex = 0;
    5015                 :             :     for (i = start; i <= end; i += stride)
    5016                 :             :       {
    5017                 :             :         if (masktmp[maskindex++])
    5018                 :             :           g<i> = h<i>
    5019                 :             :       }
    5020                 :             : 
    5021                 :             :     Note that this code only works when there are no dependencies.
    5022                 :             :     Forall loop with array assignments and data dependencies are a real pain,
    5023                 :             :     because the size of the temporary cannot always be determined before the
    5024                 :             :     loop is executed.  This problem is compounded by the presence of nested
    5025                 :             :     FORALL constructs.
    5026                 :             :  */
    5027                 :             : 
    5028                 :             : static tree
    5029                 :        2024 : gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
    5030                 :             : {
    5031                 :        2024 :   stmtblock_t pre;
    5032                 :        2024 :   stmtblock_t post;
    5033                 :        2024 :   stmtblock_t block;
    5034                 :        2024 :   stmtblock_t body;
    5035                 :        2024 :   tree *var;
    5036                 :        2024 :   tree *start;
    5037                 :        2024 :   tree *end;
    5038                 :        2024 :   tree *step;
    5039                 :        2024 :   gfc_expr **varexpr;
    5040                 :        2024 :   tree tmp;
    5041                 :        2024 :   tree assign;
    5042                 :        2024 :   tree size;
    5043                 :        2024 :   tree maskindex;
    5044                 :        2024 :   tree mask;
    5045                 :        2024 :   tree pmask;
    5046                 :        2024 :   tree cycle_label = NULL_TREE;
    5047                 :        2024 :   int n;
    5048                 :        2024 :   int nvar;
    5049                 :        2024 :   int need_temp;
    5050                 :        2024 :   gfc_forall_iterator *fa;
    5051                 :        2024 :   gfc_se se;
    5052                 :        2024 :   gfc_code *c;
    5053                 :        2024 :   gfc_saved_var *saved_vars;
    5054                 :        2024 :   iter_info *this_forall;
    5055                 :        2024 :   forall_info *info;
    5056                 :        2024 :   bool need_mask;
    5057                 :             : 
    5058                 :             :   /* Do nothing if the mask is false.  */
    5059                 :        2024 :   if (code->expr1
    5060                 :         745 :       && code->expr1->expr_type == EXPR_CONSTANT
    5061                 :           2 :       && !code->expr1->value.logical)
    5062                 :           1 :     return build_empty_stmt (input_location);
    5063                 :             : 
    5064                 :        2023 :   n = 0;
    5065                 :             :   /* Count the FORALL index number.  */
    5066                 :        5970 :   for (fa = code->ext.forall_iterator; fa; fa = fa->next)
    5067                 :        3947 :     n++;
    5068                 :        2023 :   nvar = n;
    5069                 :             : 
    5070                 :             :   /* Allocate the space for var, start, end, step, varexpr.  */
    5071                 :        2023 :   var = XCNEWVEC (tree, nvar);
    5072                 :        2023 :   start = XCNEWVEC (tree, nvar);
    5073                 :        2023 :   end = XCNEWVEC (tree, nvar);
    5074                 :        2023 :   step = XCNEWVEC (tree, nvar);
    5075                 :        2023 :   varexpr = XCNEWVEC (gfc_expr *, nvar);
    5076                 :        2023 :   saved_vars = XCNEWVEC (gfc_saved_var, nvar);
    5077                 :             : 
    5078                 :             :   /* Allocate the space for info.  */
    5079                 :        2023 :   info = XCNEW (forall_info);
    5080                 :             : 
    5081                 :        2023 :   gfc_start_block (&pre);
    5082                 :        2023 :   gfc_init_block (&post);
    5083                 :        2023 :   gfc_init_block (&block);
    5084                 :             : 
    5085                 :        2023 :   n = 0;
    5086                 :        5970 :   for (fa = code->ext.forall_iterator; fa; fa = fa->next)
    5087                 :             :     {
    5088                 :        3947 :       gfc_symbol *sym = fa->var->symtree->n.sym;
    5089                 :             : 
    5090                 :             :       /* Allocate space for this_forall.  */
    5091                 :        3947 :       this_forall = XCNEW (iter_info);
    5092                 :             : 
    5093                 :             :       /* Create a temporary variable for the FORALL index.  */
    5094                 :        3947 :       tmp = gfc_typenode_for_spec (&sym->ts);
    5095                 :        3947 :       var[n] = gfc_create_var (tmp, sym->name);
    5096                 :        3947 :       gfc_shadow_sym (sym, var[n], &saved_vars[n]);
    5097                 :             : 
    5098                 :             :       /* Record it in this_forall.  */
    5099                 :        3947 :       this_forall->var = var[n];
    5100                 :             : 
    5101                 :             :       /* Replace the index symbol's backend_decl with the temporary decl.  */
    5102                 :        3947 :       sym->backend_decl = var[n];
    5103                 :             : 
    5104                 :             :       /* Work out the start, end and stride for the loop.  */
    5105                 :        3947 :       gfc_init_se (&se, NULL);
    5106                 :        3947 :       gfc_conv_expr_val (&se, fa->start);
    5107                 :             :       /* Record it in this_forall.  */
    5108                 :        3947 :       this_forall->start = se.expr;
    5109                 :        3947 :       gfc_add_block_to_block (&block, &se.pre);
    5110                 :        3947 :       start[n] = se.expr;
    5111                 :             : 
    5112                 :        3947 :       gfc_init_se (&se, NULL);
    5113                 :        3947 :       gfc_conv_expr_val (&se, fa->end);
    5114                 :             :       /* Record it in this_forall.  */
    5115                 :        3947 :       this_forall->end = se.expr;
    5116                 :        3947 :       gfc_make_safe_expr (&se);
    5117                 :        3947 :       gfc_add_block_to_block (&block, &se.pre);
    5118                 :        3947 :       end[n] = se.expr;
    5119                 :             : 
    5120                 :        3947 :       gfc_init_se (&se, NULL);
    5121                 :        3947 :       gfc_conv_expr_val (&se, fa->stride);
    5122                 :             :       /* Record it in this_forall.  */
    5123                 :        3947 :       this_forall->step = se.expr;
    5124                 :        3947 :       gfc_make_safe_expr (&se);
    5125                 :        3947 :       gfc_add_block_to_block (&block, &se.pre);
    5126                 :        3947 :       step[n] = se.expr;
    5127                 :             : 
    5128                 :             :       /* Copy loop annotations.  */
    5129                 :        3947 :       this_forall->annot = fa->annot;
    5130                 :             : 
    5131                 :             :       /* Set the NEXT field of this_forall to NULL.  */
    5132                 :        3947 :       this_forall->next = NULL;
    5133                 :             :       /* Link this_forall to the info construct.  */
    5134                 :        3947 :       if (info->this_loop)
    5135                 :             :         {
    5136                 :             :           iter_info *iter_tmp = info->this_loop;
    5137                 :        2855 :           while (iter_tmp->next != NULL)
    5138                 :             :             iter_tmp = iter_tmp->next;
    5139                 :        1924 :           iter_tmp->next = this_forall;
    5140                 :             :         }
    5141                 :             :       else
    5142                 :        2023 :         info->this_loop = this_forall;
    5143                 :             : 
    5144                 :        3947 :       n++;
    5145                 :             :     }
    5146                 :        2023 :   nvar = n;
    5147                 :             : 
    5148                 :             :   /* Calculate the size needed for the current forall level.  */
    5149                 :        2023 :   size = gfc_index_one_node;
    5150                 :        5970 :   for (n = 0; n < nvar; n++)
    5151                 :             :     {
    5152                 :             :       /* size = (end + step - start) / step.  */
    5153                 :        3947 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (start[n]),
    5154                 :        3947 :                              step[n], start[n]);
    5155                 :        3947 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (end[n]),
    5156                 :        3947 :                              end[n], tmp);
    5157                 :        3947 :       tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, TREE_TYPE (tmp),
    5158                 :             :                              tmp, step[n]);
    5159                 :        3947 :       tmp = convert (gfc_array_index_type, tmp);
    5160                 :             : 
    5161                 :        3947 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    5162                 :             :                               size, tmp);
    5163                 :             :     }
    5164                 :             : 
    5165                 :             :   /* Record the nvar and size of current forall level.  */
    5166                 :        2023 :   info->nvar = nvar;
    5167                 :        2023 :   info->size = size;
    5168                 :             : 
    5169                 :        2023 :   if (code->expr1)
    5170                 :             :     {
    5171                 :             :       /* If the mask is .true., consider the FORALL unconditional.  */
    5172                 :         744 :       if (code->expr1->expr_type == EXPR_CONSTANT
    5173                 :           1 :           && code->expr1->value.logical)
    5174                 :             :         need_mask = false;
    5175                 :             :       else
    5176                 :         743 :         need_mask = true;
    5177                 :             :     }
    5178                 :             :   else
    5179                 :             :     need_mask = false;
    5180                 :             : 
    5181                 :             :   /* First we need to allocate the mask.  */
    5182                 :         743 :   if (need_mask)
    5183                 :             :     {
    5184                 :             :       /* As the mask array can be very big, prefer compact boolean types.  */
    5185                 :         743 :       tree mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
    5186                 :         743 :       mask = allocate_temp_for_forall_nest (nested_forall_info, mask_type,
    5187                 :             :                                             size, NULL, &block, &pmask);
    5188                 :         743 :       maskindex = gfc_create_var_np (gfc_array_index_type, "mi");
    5189                 :             : 
    5190                 :             :       /* Record them in the info structure.  */
    5191                 :         743 :       info->maskindex = maskindex;
    5192                 :         743 :       info->mask = mask;
    5193                 :             :     }
    5194                 :             :   else
    5195                 :             :     {
    5196                 :             :       /* No mask was specified.  */
    5197                 :        1280 :       maskindex = NULL_TREE;
    5198                 :        1280 :       mask = pmask = NULL_TREE;
    5199                 :             :     }
    5200                 :             : 
    5201                 :             :   /* Link the current forall level to nested_forall_info.  */
    5202                 :        2023 :   info->prev_nest = nested_forall_info;
    5203                 :        2023 :   nested_forall_info = info;
    5204                 :             : 
    5205                 :             :   /* Copy the mask into a temporary variable if required.
    5206                 :             :      For now we assume a mask temporary is needed.  */
    5207                 :        2023 :   if (need_mask)
    5208                 :             :     {
    5209                 :             :       /* As the mask array can be very big, prefer compact boolean types.  */
    5210                 :         743 :       tree mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
    5211                 :             : 
    5212                 :         743 :       gfc_add_modify (&block, maskindex, gfc_index_zero_node);
    5213                 :             : 
    5214                 :             :       /* Start of mask assignment loop body.  */
    5215                 :         743 :       gfc_start_block (&body);
    5216                 :             : 
    5217                 :             :       /* Evaluate the mask expression.  */
    5218                 :         743 :       gfc_init_se (&se, NULL);
    5219                 :         743 :       gfc_conv_expr_val (&se, code->expr1);
    5220                 :         743 :       gfc_add_block_to_block (&body, &se.pre);
    5221                 :             : 
    5222                 :             :       /* Store the mask.  */
    5223                 :         743 :       se.expr = convert (mask_type, se.expr);
    5224                 :             : 
    5225                 :         743 :       tmp = gfc_build_array_ref (mask, maskindex, NULL);
    5226                 :         743 :       gfc_add_modify (&body, tmp, se.expr);
    5227                 :             : 
    5228                 :             :       /* Advance to the next mask element.  */
    5229                 :         743 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    5230                 :             :                              maskindex, gfc_index_one_node);
    5231                 :         743 :       gfc_add_modify (&body, maskindex, tmp);
    5232                 :             : 
    5233                 :             :       /* Generate the loops.  */
    5234                 :         743 :       tmp = gfc_finish_block (&body);
    5235                 :         743 :       tmp = gfc_trans_nested_forall_loop (info, tmp, 0);
    5236                 :         743 :       gfc_add_expr_to_block (&block, tmp);
    5237                 :             :     }
    5238                 :             : 
    5239                 :        2023 :   if (code->op == EXEC_DO_CONCURRENT)
    5240                 :             :     {
    5241                 :          49 :       gfc_init_block (&body);
    5242                 :          49 :       cycle_label = gfc_build_label_decl (NULL_TREE);
    5243                 :          49 :       code->cycle_label = cycle_label;
    5244                 :          49 :       tmp = gfc_trans_code (code->block->next);
    5245                 :          49 :       gfc_add_expr_to_block (&body, tmp);
    5246                 :             : 
    5247                 :          49 :       if (TREE_USED (cycle_label))
    5248                 :             :         {
    5249                 :          49 :           tmp = build1_v (LABEL_EXPR, cycle_label);
    5250                 :          49 :           gfc_add_expr_to_block (&body, tmp);
    5251                 :             :         }
    5252                 :             : 
    5253                 :          49 :       tmp = gfc_finish_block (&body);
    5254                 :          49 :       nested_forall_info->do_concurrent = true;
    5255                 :          49 :       tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
    5256                 :          49 :       gfc_add_expr_to_block (&block, tmp);
    5257                 :          49 :       goto done;
    5258                 :             :     }
    5259                 :             : 
    5260                 :        1974 :   c = code->block->next;
    5261                 :             : 
    5262                 :             :   /* TODO: loop merging in FORALL statements.  */
    5263                 :             :   /* Now that we've got a copy of the mask, generate the assignment loops.  */
    5264                 :        3966 :   while (c)
    5265                 :             :     {
    5266                 :        1992 :       switch (c->op)
    5267                 :             :         {
    5268                 :        1838 :         case EXEC_ASSIGN:
    5269                 :             :           /* A scalar or array assignment.  DO the simple check for
    5270                 :             :              lhs to rhs dependencies.  These make a temporary for the
    5271                 :             :              rhs and form a second forall block to copy to variable.  */
    5272                 :        1838 :           need_temp = check_forall_dependencies(c, &pre, &post);
    5273                 :             : 
    5274                 :             :           /* Temporaries due to array assignment data dependencies introduce
    5275                 :             :              no end of problems.  */
    5276                 :        1838 :           if (need_temp || flag_test_forall_temp)
    5277                 :         197 :             gfc_trans_assign_need_temp (c->expr1, c->expr2, NULL, false,
    5278                 :             :                                         nested_forall_info, &block);
    5279                 :             :           else
    5280                 :             :             {
    5281                 :             :               /* Use the normal assignment copying routines.  */
    5282                 :        1641 :               assign = gfc_trans_assignment (c->expr1, c->expr2, false, true);
    5283                 :             : 
    5284                 :             :               /* Generate body and loops.  */
    5285                 :        1641 :               tmp = gfc_trans_nested_forall_loop (nested_forall_info,
    5286                 :             :                                                   assign, 1);
    5287                 :        1641 :               gfc_add_expr_to_block (&block, tmp);
    5288                 :             :             }
    5289                 :             : 
    5290                 :             :           /* Cleanup any temporary symtrees that have been made to deal
    5291                 :             :              with dependencies.  */
    5292                 :        1838 :           if (new_symtree)
    5293                 :          62 :             cleanup_forall_symtrees (c);
    5294                 :             : 
    5295                 :             :           break;
    5296                 :             : 
    5297                 :          46 :         case EXEC_WHERE:
    5298                 :             :           /* Translate WHERE or WHERE construct nested in FORALL.  */
    5299                 :          46 :           gfc_trans_where_2 (c, NULL, false, nested_forall_info, &block);
    5300                 :          46 :           break;
    5301                 :             : 
    5302                 :             :         /* Pointer assignment inside FORALL.  */
    5303                 :          21 :         case EXEC_POINTER_ASSIGN:
    5304                 :          21 :           need_temp = gfc_check_dependency (c->expr1, c->expr2, 0);
    5305                 :             :           /* Avoid cases where a temporary would never be needed and where
    5306                 :             :              the temp code is guaranteed to fail.  */
    5307                 :          21 :           if (need_temp
    5308                 :           1 :               || (flag_test_forall_temp
    5309                 :           0 :                   && c->expr2->expr_type != EXPR_CONSTANT
    5310                 :           0 :                   && c->expr2->expr_type != EXPR_NULL))
    5311                 :          20 :             gfc_trans_pointer_assign_need_temp (c->expr1, c->expr2,
    5312                 :             :                                                 nested_forall_info, &block);
    5313                 :             :           else
    5314                 :             :             {
    5315                 :             :               /* Use the normal assignment copying routines.  */
    5316                 :           1 :               assign = gfc_trans_pointer_assignment (c->expr1, c->expr2);
    5317                 :             : 
    5318                 :             :               /* Generate body and loops.  */
    5319                 :           1 :               tmp = gfc_trans_nested_forall_loop (nested_forall_info,
    5320                 :             :                                                   assign, 1);
    5321                 :           1 :               gfc_add_expr_to_block (&block, tmp);
    5322                 :             :             }
    5323                 :             :           break;
    5324                 :             : 
    5325                 :          81 :         case EXEC_FORALL:
    5326                 :          81 :           tmp = gfc_trans_forall_1 (c, nested_forall_info);
    5327                 :          81 :           gfc_add_expr_to_block (&block, tmp);
    5328                 :          81 :           break;
    5329                 :             : 
    5330                 :             :         /* Explicit subroutine calls are prevented by the frontend but interface
    5331                 :             :            assignments can legitimately produce them.  */
    5332                 :           6 :         case EXEC_ASSIGN_CALL:
    5333                 :           6 :           assign = gfc_trans_call (c, true, NULL_TREE, NULL_TREE, false);
    5334                 :           6 :           tmp = gfc_trans_nested_forall_loop (nested_forall_info, assign, 1);
    5335                 :           6 :           gfc_add_expr_to_block (&block, tmp);
    5336                 :           6 :           break;
    5337                 :             : 
    5338                 :           0 :         default:
    5339                 :           0 :           gcc_unreachable ();
    5340                 :             :         }
    5341                 :             : 
    5342                 :        1992 :       c = c->next;
    5343                 :             :     }
    5344                 :             : 
    5345                 :        1974 : done:
    5346                 :             :   /* Restore the original index variables.  */
    5347                 :        5970 :   for (fa = code->ext.forall_iterator, n = 0; fa; fa = fa->next, n++)
    5348                 :        3947 :     gfc_restore_sym (fa->var->symtree->n.sym, &saved_vars[n]);
    5349                 :             : 
    5350                 :             :   /* Free the space for var, start, end, step, varexpr.  */
    5351                 :        2023 :   free (var);
    5352                 :        2023 :   free (start);
    5353                 :        2023 :   free (end);
    5354                 :        2023 :   free (step);
    5355                 :        2023 :   free (varexpr);
    5356                 :        2023 :   free (saved_vars);
    5357                 :             : 
    5358                 :        5970 :   for (this_forall = info->this_loop; this_forall;)
    5359                 :             :     {
    5360                 :        3947 :       iter_info *next = this_forall->next;
    5361                 :        3947 :       free (this_forall);
    5362                 :        3947 :       this_forall = next;
    5363                 :             :     }
    5364                 :             : 
    5365                 :             :   /* Free the space for this forall_info.  */
    5366                 :        2023 :   free (info);
    5367                 :             : 
    5368                 :        2023 :   if (pmask)
    5369                 :             :     {
    5370                 :             :       /* Free the temporary for the mask.  */
    5371                 :          57 :       tmp = gfc_call_free (pmask);
    5372                 :          57 :       gfc_add_expr_to_block (&block, tmp);
    5373                 :             :     }
    5374                 :        2023 :   if (maskindex)
    5375                 :         743 :     pushdecl (maskindex);
    5376                 :             : 
    5377                 :        2023 :   gfc_add_block_to_block (&pre, &block);
    5378                 :        2023 :   gfc_add_block_to_block (&pre, &post);
    5379                 :             : 
    5380                 :        2023 :   return gfc_finish_block (&pre);
    5381                 :             : }
    5382                 :             : 
    5383                 :             : 
    5384                 :             : /* Translate the FORALL statement or construct.  */
    5385                 :             : 
    5386                 :        1894 : tree gfc_trans_forall (gfc_code * code)
    5387                 :             : {
    5388                 :        1894 :   return gfc_trans_forall_1 (code, NULL);
    5389                 :             : }
    5390                 :             : 
    5391                 :             : 
    5392                 :             : /* Translate the DO CONCURRENT construct.  */
    5393                 :             : 
    5394                 :          49 : tree gfc_trans_do_concurrent (gfc_code * code)
    5395                 :             : {
    5396                 :          49 :   return gfc_trans_forall_1 (code, NULL);
    5397                 :             : }
    5398                 :             : 
    5399                 :             : 
    5400                 :             : /* Evaluate the WHERE mask expression, copy its value to a temporary.
    5401                 :             :    If the WHERE construct is nested in FORALL, compute the overall temporary
    5402                 :             :    needed by the WHERE mask expression multiplied by the iterator number of
    5403                 :             :    the nested forall.
    5404                 :             :    ME is the WHERE mask expression.
    5405                 :             :    MASK is the current execution mask upon input, whose sense may or may
    5406                 :             :    not be inverted as specified by the INVERT argument.
    5407                 :             :    CMASK is the updated execution mask on output, or NULL if not required.
    5408                 :             :    PMASK is the pending execution mask on output, or NULL if not required.
    5409                 :             :    BLOCK is the block in which to place the condition evaluation loops.  */
    5410                 :             : 
    5411                 :             : static void
    5412                 :         527 : gfc_evaluate_where_mask (gfc_expr * me, forall_info * nested_forall_info,
    5413                 :             :                          tree mask, bool invert, tree cmask, tree pmask,
    5414                 :             :                          tree mask_type, stmtblock_t * block)
    5415                 :             : {
    5416                 :         527 :   tree tmp, tmp1;
    5417                 :         527 :   gfc_ss *lss, *rss;
    5418                 :         527 :   gfc_loopinfo loop;
    5419                 :         527 :   stmtblock_t body, body1;
    5420                 :         527 :   tree count, cond, mtmp;
    5421                 :         527 :   gfc_se lse, rse;
    5422                 :             : 
    5423                 :         527 :   gfc_init_loopinfo (&loop);
    5424                 :             : 
    5425                 :         527 :   lss = gfc_walk_expr (me);
    5426                 :         527 :   rss = gfc_walk_expr (me);
    5427                 :             : 
    5428                 :             :   /* Variable to index the temporary.  */
    5429                 :         527 :   count = gfc_create_var (gfc_array_index_type, "count");
    5430                 :             :   /* Initialize count.  */
    5431                 :         527 :   gfc_add_modify (block, count, gfc_index_zero_node);
    5432                 :             : 
    5433                 :         527 :   gfc_start_block (&body);
    5434                 :             : 
    5435                 :         527 :   gfc_init_se (&rse, NULL);
    5436                 :         527 :   gfc_init_se (&lse, NULL);
    5437                 :             : 
    5438                 :         527 :   if (lss == gfc_ss_terminator)
    5439                 :             :     {
    5440                 :           0 :       gfc_init_block (&body1);
    5441                 :             :     }
    5442                 :             :   else
    5443                 :             :     {
    5444                 :             :       /* Initialize the loop.  */
    5445                 :         527 :       gfc_init_loopinfo (&loop);
    5446                 :             : 
    5447                 :             :       /* We may need LSS to determine the shape of the expression.  */
    5448                 :         527 :       gfc_add_ss_to_loop (&loop, lss);
    5449                 :         527 :       gfc_add_ss_to_loop (&loop, rss);
    5450                 :             : 
    5451                 :         527 :       gfc_conv_ss_startstride (&loop);
    5452                 :         527 :       gfc_conv_loop_setup (&loop, &me->where);
    5453                 :             : 
    5454                 :         527 :       gfc_mark_ss_chain_used (rss, 1);
    5455                 :             :       /* Start the loop body.  */
    5456                 :         527 :       gfc_start_scalarized_body (&loop, &body1);
    5457                 :             : 
    5458                 :             :       /* Translate the expression.  */
    5459                 :         527 :       gfc_copy_loopinfo_to_se (&rse, &loop);
    5460                 :         527 :       rse.ss = rss;
    5461                 :         527 :       gfc_conv_expr (&rse, me);
    5462                 :             :     }
    5463                 :             : 
    5464                 :             :   /* Variable to evaluate mask condition.  */
    5465                 :         527 :   cond = gfc_create_var (mask_type, "cond");
    5466                 :         527 :   if (mask && (cmask || pmask))
    5467                 :         234 :     mtmp = gfc_create_var (mask_type, "mask");
    5468                 :             :   else mtmp = NULL_TREE;
    5469                 :             : 
    5470                 :         527 :   gfc_add_block_to_block (&body1, &lse.pre);
    5471                 :         527 :   gfc_add_block_to_block (&body1, &rse.pre);
    5472                 :             : 
    5473                 :         527 :   gfc_add_modify (&body1, cond, fold_convert (mask_type, rse.expr));
    5474                 :             : 
    5475                 :         527 :   if (mask && (cmask || pmask))
    5476                 :             :     {
    5477                 :         234 :       tmp = gfc_build_array_ref (mask, count, NULL);
    5478                 :         234 :       if (invert)
    5479                 :          99 :         tmp = fold_build1_loc (input_location, TRUTH_NOT_EXPR, mask_type, tmp);
    5480                 :         234 :       gfc_add_modify (&body1, mtmp, tmp);
    5481                 :             :     }
    5482                 :             : 
    5483                 :         527 :   if (cmask)
    5484                 :             :     {
    5485                 :         509 :       tmp1 = gfc_build_array_ref (cmask, count, NULL);
    5486                 :         509 :       tmp = cond;
    5487                 :         509 :       if (mask)
    5488                 :         234 :         tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, mask_type,
    5489                 :             :                                mtmp, tmp);
    5490                 :         509 :       gfc_add_modify (&body1, tmp1, tmp);
    5491                 :             :     }
    5492                 :             : 
    5493                 :         527 :   if (pmask)
    5494                 :             :     {
    5495                 :         146 :       tmp1 = gfc_build_array_ref (pmask, count, NULL);
    5496                 :         146 :       tmp = fold_build1_loc (input_location, TRUTH_NOT_EXPR, mask_type, cond);
    5497                 :         146 :       if (mask)
    5498                 :         146 :         tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, mask_type, mtmp,
    5499                 :             :                                tmp);
    5500                 :         146 :       gfc_add_modify (&body1, tmp1, tmp);
    5501                 :             :     }
    5502                 :             : 
    5503                 :         527 :   gfc_add_block_to_block (&body1, &lse.post);
    5504                 :         527 :   gfc_add_block_to_block (&body1, &rse.post);
    5505                 :             : 
    5506                 :         527 :   if (lss == gfc_ss_terminator)
    5507                 :             :     {
    5508                 :           0 :       gfc_add_block_to_block (&body, &body1);
    5509                 :             :     }
    5510                 :             :   else
    5511                 :             :     {
    5512                 :             :       /* Increment count.  */
    5513                 :         527 :       tmp1 = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    5514                 :             :                               count, gfc_index_one_node);
    5515                 :         527 :       gfc_add_modify (&body1, count, tmp1);
    5516                 :             : 
    5517                 :             :       /* Generate the copying loops.  */
    5518                 :         527 :       gfc_trans_scalarizing_loops (&loop, &body1);
    5519                 :             : 
    5520                 :         527 :       gfc_add_block_to_block (&body, &loop.pre);
    5521                 :         527 :       gfc_add_block_to_block (&body, &loop.post);
    5522                 :             : 
    5523                 :         527 :       gfc_cleanup_loop (&loop);
    5524                 :             :       /* TODO: Reuse lss and rss when copying temp->lhs.  Need to be careful
    5525                 :             :          as tree nodes in SS may not be valid in different scope.  */
    5526                 :             :     }
    5527                 :             : 
    5528                 :         527 :   tmp1 = gfc_finish_block (&body);
    5529                 :             :   /* If the WHERE construct is inside FORALL, fill the full temporary.  */
    5530                 :         527 :   if (nested_forall_info != NULL)
    5531                 :          64 :     tmp1 = gfc_trans_nested_forall_loop (nested_forall_info, tmp1, 1);
    5532                 :             : 
    5533                 :         527 :   gfc_add_expr_to_block (block, tmp1);
    5534                 :         527 : }
    5535                 :             : 
    5536                 :             : 
    5537                 :             : /* Translate an assignment statement in a WHERE statement or construct
    5538                 :             :    statement. The MASK expression is used to control which elements
    5539                 :             :    of EXPR1 shall be assigned.  The sense of MASK is specified by
    5540                 :             :    INVERT.  */
    5541                 :             : 
    5542                 :             : static tree
    5543                 :         544 : gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2,
    5544                 :             :                         tree mask, bool invert,
    5545                 :             :                         tree count1, tree count2,
    5546                 :             :                         gfc_code *cnext)
    5547                 :             : {
    5548                 :         544 :   gfc_se lse;
    5549                 :         544 :   gfc_se rse;
    5550                 :         544 :   gfc_ss *lss;
    5551                 :         544 :   gfc_ss *lss_section;
    5552                 :         544 :   gfc_ss *rss;
    5553                 :             : 
    5554                 :         544 :   gfc_loopinfo loop;
    5555                 :         544 :   tree tmp;
    5556                 :         544 :   stmtblock_t block;
    5557                 :         544 :   stmtblock_t body;
    5558                 :         544 :   tree index, maskexpr;
    5559                 :             : 
    5560                 :             :   /* A defined assignment.  */
    5561                 :         544 :   if (cnext && cnext->resolved_sym)
    5562                 :          44 :     return gfc_trans_call (cnext, true, mask, count1, invert);
    5563                 :             : 
    5564                 :             : #if 0
    5565                 :             :   /* TODO: handle this special case.
    5566                 :             :      Special case a single function returning an array.  */
    5567                 :             :   if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
    5568                 :             :     {
    5569                 :             :       tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
    5570                 :             :       if (tmp)
    5571                 :             :         return tmp;
    5572                 :             :     }
    5573                 :             : #endif
    5574                 :             : 
    5575                 :             :  /* Assignment of the form lhs = rhs.  */
    5576                 :         500 :   gfc_start_block (&block);
    5577                 :             : 
    5578                 :         500 :   gfc_init_se (&lse, NULL);
    5579                 :         500 :   gfc_init_se (&rse, NULL);
    5580                 :             : 
    5581                 :             :   /* Walk the lhs.  */
    5582                 :         500 :   lss = gfc_walk_expr (expr1);
    5583                 :         500 :   rss = NULL;
    5584                 :             : 
    5585                 :             :   /* In each where-assign-stmt, the mask-expr and the variable being
    5586                 :             :      defined shall be arrays of the same shape.  */
    5587                 :         500 :   gcc_assert (lss != gfc_ss_terminator);
    5588                 :             : 
    5589                 :             :   /* The assignment needs scalarization.  */
    5590                 :             :   lss_section = lss;
    5591                 :             : 
    5592                 :             :   /* Find a non-scalar SS from the lhs.  */
    5593                 :             :   while (lss_section != gfc_ss_terminator
    5594                 :         500 :          && lss_section->info->type != GFC_SS_SECTION)
    5595                 :           0 :     lss_section = lss_section->next;
    5596                 :             : 
    5597                 :         500 :   gcc_assert (lss_section != gfc_ss_terminator);
    5598                 :             : 
    5599                 :             :   /* Initialize the scalarizer.  */
    5600                 :         500 :   gfc_init_loopinfo (&loop);
    5601                 :             : 
    5602                 :             :   /* Walk the rhs.  */
    5603                 :         500 :   rss = gfc_walk_expr (expr2);
    5604                 :         500 :   if (rss == gfc_ss_terminator)
    5605                 :             :     {
    5606                 :             :       /* The rhs is scalar.  Add a ss for the expression.  */
    5607                 :         343 :       rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
    5608                 :         343 :       rss->info->where = 1;
    5609                 :             :     }
    5610                 :             : 
    5611                 :             :   /* Associate the SS with the loop.  */
    5612                 :         500 :   gfc_add_ss_to_loop (&loop, lss);
    5613                 :         500 :   gfc_add_ss_to_loop (&loop, rss);
    5614                 :             : 
    5615                 :             :   /* Calculate the bounds of the scalarization.  */
    5616                 :         500 :   gfc_conv_ss_startstride (&loop);
    5617                 :             : 
    5618                 :             :   /* Resolve any data dependencies in the statement.  */
    5619                 :         500 :   gfc_conv_resolve_dependencies (&loop, lss_section, rss);
    5620                 :             : 
    5621                 :             :   /* Setup the scalarizing loops.  */
    5622                 :         500 :   gfc_conv_loop_setup (&loop, &expr2->where);
    5623                 :             : 
    5624                 :             :   /* Setup the gfc_se structures.  */
    5625                 :         500 :   gfc_copy_loopinfo_to_se (&lse, &loop);
    5626                 :         500 :   gfc_copy_loopinfo_to_se (&rse, &loop);
    5627                 :             : 
    5628                 :         500 :   rse.ss = rss;
    5629                 :         500 :   gfc_mark_ss_chain_used (rss, 1);
    5630                 :         500 :   if (loop.temp_ss == NULL)
    5631                 :             :     {
    5632                 :         422 :       lse.ss = lss;
    5633                 :         422 :       gfc_mark_ss_chain_used (lss, 1);
    5634                 :             :     }
    5635                 :             :   else
    5636                 :             :     {
    5637                 :          78 :       lse.ss = loop.temp_ss;
    5638                 :          78 :       gfc_mark_ss_chain_used (lss, 3);
    5639                 :          78 :       gfc_mark_ss_chain_used (loop.temp_ss, 3);
    5640                 :             :     }
    5641                 :             : 
    5642                 :             :   /* Start the scalarized loop body.  */
    5643                 :         500 :   gfc_start_scalarized_body (&loop, &body);
    5644                 :             : 
    5645                 :             :   /* Translate the expression.  */
    5646                 :         500 :   gfc_conv_expr (&rse, expr2);
    5647                 :         500 :   if (lss != gfc_ss_terminator && loop.temp_ss != NULL)
    5648                 :          78 :     gfc_conv_tmp_array_ref (&lse);
    5649                 :             :   else
    5650                 :         422 :     gfc_conv_expr (&lse, expr1);
    5651                 :             : 
    5652                 :             :   /* Form the mask expression according to the mask.  */
    5653                 :         500 :   index = count1;
    5654                 :         500 :   maskexpr = gfc_build_array_ref (mask, index, NULL);
    5655                 :         500 :   if (invert)
    5656                 :          24 :     maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
    5657                 :          24 :                                 TREE_TYPE (maskexpr), maskexpr);
    5658                 :             : 
    5659                 :             :   /* Use the scalar assignment as is.  */
    5660                 :        1000 :   tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
    5661                 :         500 :                                  false, loop.temp_ss == NULL);
    5662                 :             : 
    5663                 :         500 :   tmp = build3_v (COND_EXPR, maskexpr, tmp, build_empty_stmt (input_location));
    5664                 :             : 
    5665                 :         500 :   gfc_add_expr_to_block (&body, tmp);
    5666                 :             : 
    5667                 :         500 :   if (lss == gfc_ss_terminator)
    5668                 :             :     {
    5669                 :             :       /* Increment count1.  */
    5670                 :             :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    5671                 :             :                              count1, gfc_index_one_node);
    5672                 :             :       gfc_add_modify (&body, count1, tmp);
    5673                 :             : 
    5674                 :             :       /* Use the scalar assignment as is.  */
    5675                 :             :       gfc_add_block_to_block (&block, &body);
    5676                 :             :     }
    5677                 :             :   else
    5678                 :             :     {
    5679                 :         500 :       gcc_assert (lse.ss == gfc_ss_terminator
    5680                 :             :                   && rse.ss == gfc_ss_terminator);
    5681                 :             : 
    5682                 :         500 :       if (loop.temp_ss != NULL)
    5683                 :             :         {
    5684                 :             :           /* Increment count1 before finish the main body of a scalarized
    5685                 :             :              expression.  */
    5686                 :          78 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5687                 :             :                                  gfc_array_index_type, count1, gfc_index_one_node);
    5688                 :          78 :           gfc_add_modify (&body, count1, tmp);
    5689                 :          78 :           gfc_trans_scalarized_loop_boundary (&loop, &body);
    5690                 :             : 
    5691                 :             :           /* We need to copy the temporary to the actual lhs.  */
    5692                 :          78 :           gfc_init_se (&lse, NULL);
    5693                 :          78 :           gfc_init_se (&rse, NULL);
    5694                 :          78 :           gfc_copy_loopinfo_to_se (&lse, &loop);
    5695                 :          78 :           gfc_copy_loopinfo_to_se (&rse, &loop);
    5696                 :             : 
    5697                 :          78 :           rse.ss = loop.temp_ss;
    5698                 :          78 :           lse.ss = lss;
    5699                 :             : 
    5700                 :          78 :           gfc_conv_tmp_array_ref (&rse);
    5701                 :          78 :           gfc_conv_expr (&lse, expr1);
    5702                 :             : 
    5703                 :          78 :           gcc_assert (lse.ss == gfc_ss_terminator
    5704                 :             :                       && rse.ss == gfc_ss_terminator);
    5705                 :             : 
    5706                 :             :           /* Form the mask expression according to the mask tree list.  */
    5707                 :          78 :           index = count2;
    5708                 :          78 :           maskexpr = gfc_build_array_ref (mask, index, NULL);
    5709                 :          78 :           if (invert)
    5710                 :           0 :             maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
    5711                 :           0 :                                         TREE_TYPE (maskexpr), maskexpr);
    5712                 :             : 
    5713                 :             :           /* Use the scalar assignment as is.  */
    5714                 :          78 :           tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts, false, true);
    5715                 :          78 :           tmp = build3_v (COND_EXPR, maskexpr, tmp,
    5716                 :             :                           build_empty_stmt (input_location));
    5717                 :          78 :           gfc_add_expr_to_block (&body, tmp);
    5718                 :             : 
    5719                 :             :           /* Increment count2.  */
    5720                 :          78 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5721                 :             :                                  gfc_array_index_type, count2,
    5722                 :             :                                  gfc_index_one_node);
    5723                 :          78 :           gfc_add_modify (&body, count2, tmp);
    5724                 :             :         }
    5725                 :             :       else
    5726                 :             :         {
    5727                 :             :           /* Increment count1.  */
    5728                 :         422 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5729                 :             :                                  gfc_array_index_type, count1,
    5730                 :             :                                  gfc_index_one_node);
    5731                 :         422 :           gfc_add_modify (&body, count1, tmp);
    5732                 :             :         }
    5733                 :             : 
    5734                 :             :       /* Generate the copying loops.  */
    5735                 :         500 :       gfc_trans_scalarizing_loops (&loop, &body);
    5736                 :             : 
    5737                 :             :       /* Wrap the whole thing up.  */
    5738                 :         500 :       gfc_add_block_to_block (&block, &loop.pre);
    5739                 :         500 :       gfc_add_block_to_block (&block, &loop.post);
    5740                 :         500 :       gfc_cleanup_loop (&loop);
    5741                 :             :     }
    5742                 :             : 
    5743                 :         500 :   return gfc_finish_block (&block);
    5744                 :             : }
    5745                 :             : 
    5746                 :             : 
    5747                 :             : /* Translate the WHERE construct or statement.
    5748                 :             :    This function can be called iteratively to translate the nested WHERE
    5749                 :             :    construct or statement.
    5750                 :             :    MASK is the control mask.  */
    5751                 :             : 
    5752                 :             : static void
    5753                 :         349 : gfc_trans_where_2 (gfc_code * code, tree mask, bool invert,
    5754                 :             :                    forall_info * nested_forall_info, stmtblock_t * block)
    5755                 :             : {
    5756                 :         349 :   stmtblock_t inner_size_body;
    5757                 :         349 :   tree inner_size, size;
    5758                 :         349 :   gfc_ss *lss, *rss;
    5759                 :         349 :   tree mask_type;
    5760                 :         349 :   gfc_expr *expr1;
    5761                 :         349 :   gfc_expr *expr2;
    5762                 :         349 :   gfc_code *cblock;
    5763                 :         349 :   gfc_code *cnext;
    5764                 :         349 :   tree tmp;
    5765                 :         349 :   tree cond;
    5766                 :         349 :   tree count1, count2;
    5767                 :         349 :   bool need_cmask;
    5768                 :         349 :   bool need_pmask;
    5769                 :         349 :   int need_temp;
    5770                 :         349 :   tree pcmask = NULL_TREE;
    5771                 :         349 :   tree ppmask = NULL_TREE;
    5772                 :         349 :   tree cmask = NULL_TREE;
    5773                 :         349 :   tree pmask = NULL_TREE;
    5774                 :         349 :   gfc_actual_arglist *arg;
    5775                 :             : 
    5776                 :             :   /* the WHERE statement or the WHERE construct statement.  */
    5777                 :         349 :   cblock = code->block;
    5778                 :             : 
    5779                 :             :   /* As the mask array can be very big, prefer compact boolean types.  */
    5780                 :         349 :   mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
    5781                 :             : 
    5782                 :             :   /* Determine which temporary masks are needed.  */
    5783                 :         349 :   if (!cblock->block)
    5784                 :             :     {
    5785                 :             :       /* One clause: No ELSEWHEREs.  */
    5786                 :         168 :       need_cmask = (cblock->next != 0);
    5787                 :         168 :       need_pmask = false;
    5788                 :             :     }
    5789                 :         181 :   else if (cblock->block->block)
    5790                 :             :     {
    5791                 :             :       /* Three or more clauses: Conditional ELSEWHEREs.  */
    5792                 :             :       need_cmask = true;
    5793                 :             :       need_pmask = true;
    5794                 :             :     }
    5795                 :         102 :   else if (cblock->next)
    5796                 :             :     {
    5797                 :             :       /* Two clauses, the first non-empty.  */
    5798                 :          84 :       need_cmask = true;
    5799                 :          84 :       need_pmask = (mask != NULL_TREE
    5800                 :          84 :                     && cblock->block->next != 0);
    5801                 :             :     }
    5802                 :          18 :   else if (!cblock->block->next)
    5803                 :             :     {
    5804                 :             :       /* Two clauses, both empty.  */
    5805                 :             :       need_cmask = false;
    5806                 :             :       need_pmask = false;
    5807                 :             :     }
    5808                 :             :   /* Two clauses, the first empty, the second non-empty.  */
    5809                 :           9 :   else if (mask)
    5810                 :             :     {
    5811                 :           0 :       need_cmask = (cblock->block->expr1 != 0);
    5812                 :           0 :       need_pmask = true;
    5813                 :             :     }
    5814                 :             :   else
    5815                 :             :     {
    5816                 :             :       need_cmask = true;
    5817                 :             :       need_pmask = false;
    5818                 :             :     }
    5819                 :             : 
    5820                 :         349 :   if (need_cmask || need_pmask)
    5821                 :             :     {
    5822                 :             :       /* Calculate the size of temporary needed by the mask-expr.  */
    5823                 :         331 :       gfc_init_block (&inner_size_body);
    5824                 :         331 :       inner_size = compute_inner_temp_size (cblock->expr1, cblock->expr1,
    5825                 :             :                                             &inner_size_body, &lss, &rss);
    5826                 :             : 
    5827                 :         331 :       gfc_free_ss_chain (lss);
    5828                 :         331 :       gfc_free_ss_chain (rss);
    5829                 :             : 
    5830                 :             :       /* Calculate the total size of temporary needed.  */
    5831                 :         331 :       size = compute_overall_iter_number (nested_forall_info, inner_size,
    5832                 :             :                                           &inner_size_body, block);
    5833                 :             : 
    5834                 :             :       /* Check whether the size is negative.  */
    5835                 :         331 :       cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, size,
    5836                 :             :                               gfc_index_zero_node);
    5837                 :         331 :       size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    5838                 :             :                               cond, gfc_index_zero_node, size);
    5839                 :         331 :       size = gfc_evaluate_now (size, block);
    5840                 :             : 
    5841                 :             :       /* Allocate temporary for WHERE mask if needed.  */
    5842                 :         331 :       if (need_cmask)
    5843                 :         331 :         cmask = allocate_temp_for_forall_nest_1 (mask_type, size, block,
    5844                 :             :                                                  &pcmask);
    5845                 :             : 
    5846                 :             :       /* Allocate temporary for !mask if needed.  */
    5847                 :         331 :       if (need_pmask)
    5848                 :          83 :         pmask = allocate_temp_for_forall_nest_1 (mask_type, size, block,
    5849                 :             :                                                  &ppmask);
    5850                 :             :     }
    5851                 :             : 
    5852                 :         994 :   while (cblock)
    5853                 :             :     {
    5854                 :             :       /* Each time around this loop, the where clause is conditional
    5855                 :             :          on the value of mask and invert, which are updated at the
    5856                 :             :          bottom of the loop.  */
    5857                 :             : 
    5858                 :             :       /* Has mask-expr.  */
    5859                 :         645 :       if (cblock->expr1)
    5860                 :             :         {
    5861                 :             :           /* Ensure that the WHERE mask will be evaluated exactly once.
    5862                 :             :              If there are no statements in this WHERE/ELSEWHERE clause,
    5863                 :             :              then we don't need to update the control mask (cmask).
    5864                 :             :              If this is the last clause of the WHERE construct, then
    5865                 :             :              we don't need to update the pending control mask (pmask).  */
    5866                 :         527 :           if (mask)
    5867                 :         234 :             gfc_evaluate_where_mask (cblock->expr1, nested_forall_info,
    5868                 :             :                                      mask, invert,
    5869                 :         234 :                                      cblock->next  ? cmask : NULL_TREE,
    5870                 :         234 :                                      cblock->block ? pmask : NULL_TREE,
    5871                 :             :                                      mask_type, block);
    5872                 :             :           else
    5873                 :         293 :             gfc_evaluate_where_mask (cblock->expr1, nested_forall_info,
    5874                 :             :                                      NULL_TREE, false,
    5875                 :         293 :                                      (cblock->next || cblock->block)
    5876                 :             :                                      ? cmask : NULL_TREE,
    5877                 :             :                                      NULL_TREE, mask_type, block);
    5878                 :             : 
    5879                 :             :           invert = false;
    5880                 :             :         }
    5881                 :             :       /* It's a final elsewhere-stmt. No mask-expr is present.  */
    5882                 :             :       else
    5883                 :             :         cmask = mask;
    5884                 :             : 
    5885                 :             :       /* The body of this where clause are controlled by cmask with
    5886                 :             :          sense specified by invert.  */
    5887                 :             : 
    5888                 :             :       /* Get the assignment statement of a WHERE statement, or the first
    5889                 :             :          statement in where-body-construct of a WHERE construct.  */
    5890                 :         645 :       cnext = cblock->next;
    5891                 :        1272 :       while (cnext)
    5892                 :             :         {
    5893                 :         627 :           switch (cnext->op)
    5894                 :             :             {
    5895                 :             :             /* WHERE assignment statement.  */
    5896                 :          44 :             case EXEC_ASSIGN_CALL:
    5897                 :             : 
    5898                 :          44 :               arg = cnext->ext.actual;
    5899                 :          44 :               expr1 = expr2 = NULL;
    5900                 :         132 :               for (; arg; arg = arg->next)
    5901                 :             :                 {
    5902                 :          88 :                   if (!arg->expr)
    5903                 :           0 :                     continue;
    5904                 :          88 :                   if (expr1 == NULL)
    5905                 :             :                     expr1 = arg->expr;
    5906                 :             :                   else
    5907                 :          44 :                     expr2 = arg->expr;
    5908                 :             :                 }
    5909                 :          44 :               goto evaluate;
    5910                 :             : 
    5911                 :         527 :             case EXEC_ASSIGN:
    5912                 :         527 :               expr1 = cnext->expr1;
    5913                 :         527 :               expr2 = cnext->expr2;
    5914                 :         571 :     evaluate:
    5915                 :         571 :               if (nested_forall_info != NULL)
    5916                 :             :                 {
    5917                 :          65 :                   need_temp = gfc_check_dependency (expr1, expr2, 0);
    5918                 :          65 :                   if ((need_temp || flag_test_forall_temp)
    5919                 :          28 :                     && cnext->op != EXEC_ASSIGN_CALL)
    5920                 :          27 :                     gfc_trans_assign_need_temp (expr1, expr2,
    5921                 :             :                                                 cmask, invert,
    5922                 :             :                                                 nested_forall_info, block);
    5923                 :             :                   else
    5924                 :             :                     {
    5925                 :             :                       /* Variables to control maskexpr.  */
    5926                 :          38 :                       count1 = gfc_create_var (gfc_array_index_type, "count1");
    5927                 :          38 :                       count2 = gfc_create_var (gfc_array_index_type, "count2");
    5928                 :          38 :                       gfc_add_modify (block, count1, gfc_index_zero_node);
    5929                 :          38 :                       gfc_add_modify (block, count2, gfc_index_zero_node);
    5930                 :             : 
    5931                 :          38 :                       tmp = gfc_trans_where_assign (expr1, expr2,
    5932                 :             :                                                     cmask, invert,
    5933                 :             :                                                     count1, count2,
    5934                 :             :                                                     cnext);
    5935                 :             : 
    5936                 :          38 :                       tmp = gfc_trans_nested_forall_loop (nested_forall_info,
    5937                 :             :                                                           tmp, 1);
    5938                 :          38 :                       gfc_add_expr_to_block (block, tmp);
    5939                 :             :                     }
    5940                 :             :                 }
    5941                 :             :               else
    5942                 :             :                 {
    5943                 :             :                   /* Variables to control maskexpr.  */
    5944                 :         506 :                   count1 = gfc_create_var (gfc_array_index_type, "count1");
    5945                 :         506 :                   count2 = gfc_create_var (gfc_array_index_type, "count2");
    5946                 :         506 :                   gfc_add_modify (block, count1, gfc_index_zero_node);
    5947                 :         506 :                   gfc_add_modify (block, count2, gfc_index_zero_node);
    5948                 :             : 
    5949                 :         506 :                   tmp = gfc_trans_where_assign (expr1, expr2,
    5950                 :             :                                                 cmask, invert,
    5951                 :             :                                                 count1, count2,
    5952                 :             :                                                 cnext);
    5953                 :         506 :                   gfc_add_expr_to_block (block, tmp);
    5954                 :             : 
    5955                 :             :                 }
    5956                 :             :               break;
    5957                 :             : 
    5958                 :             :             /* WHERE or WHERE construct is part of a where-body-construct.  */
    5959                 :          56 :             case EXEC_WHERE:
    5960                 :          56 :               gfc_trans_where_2 (cnext, cmask, invert,
    5961                 :             :                                  nested_forall_info, block);
    5962                 :          56 :               break;
    5963                 :             : 
    5964                 :           0 :             default:
    5965                 :           0 :               gcc_unreachable ();
    5966                 :             :             }
    5967                 :             : 
    5968                 :             :          /* The next statement within the same where-body-construct.  */
    5969                 :         627 :          cnext = cnext->next;
    5970                 :             :        }
    5971                 :             :     /* The next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt.  */
    5972                 :         645 :     cblock = cblock->block;
    5973                 :         645 :     if (mask == NULL_TREE)
    5974                 :             :       {
    5975                 :             :         /* If we're the initial WHERE, we can simply invert the sense
    5976                 :             :            of the current mask to obtain the "mask" for the remaining
    5977                 :             :            ELSEWHEREs.  */
    5978                 :             :         invert = true;
    5979                 :             :         mask = cmask;
    5980                 :             :       }
    5981                 :             :     else
    5982                 :             :       {
    5983                 :             :         /* Otherwise, for nested WHERE's we need to use the pending mask.  */
    5984                 :         343 :         invert = false;
    5985                 :         343 :         mask = pmask;
    5986                 :             :       }
    5987                 :             :   }
    5988                 :             : 
    5989                 :             :   /* If we allocated a pending mask array, deallocate it now.  */
    5990                 :         349 :   if (ppmask)
    5991                 :             :     {
    5992                 :           1 :       tmp = gfc_call_free (ppmask);
    5993                 :           1 :       gfc_add_expr_to_block (block, tmp);
    5994                 :             :     }
    5995                 :             : 
    5996                 :             :   /* If we allocated a current mask array, deallocate it now.  */
    5997                 :         349 :   if (pcmask)
    5998                 :             :     {
    5999                 :          43 :       tmp = gfc_call_free (pcmask);
    6000                 :          43 :       gfc_add_expr_to_block (block, tmp);
    6001                 :             :     }
    6002                 :         349 : }
    6003                 :             : 
    6004                 :             : /* Translate a simple WHERE construct or statement without dependencies.
    6005                 :             :    CBLOCK is the "then" clause of the WHERE statement, where CBLOCK->EXPR
    6006                 :             :    is the mask condition, and EBLOCK if non-NULL is the "else" clause.
    6007                 :             :    Currently both CBLOCK and EBLOCK are restricted to single assignments.  */
    6008                 :             : 
    6009                 :             : static tree
    6010                 :          94 : gfc_trans_where_3 (gfc_code * cblock, gfc_code * eblock)
    6011                 :             : {
    6012                 :          94 :   stmtblock_t block, body;
    6013                 :          94 :   gfc_expr *cond, *tdst, *tsrc, *edst, *esrc;
    6014                 :          94 :   tree tmp, cexpr, tstmt, estmt;
    6015                 :          94 :   gfc_ss *css, *tdss, *tsss;
    6016                 :          94 :   gfc_se cse, tdse, tsse, edse, esse;
    6017                 :          94 :   gfc_loopinfo loop;
    6018                 :          94 :   gfc_ss *edss = 0;
    6019                 :          94 :   gfc_ss *esss = 0;
    6020                 :          94 :   bool maybe_workshare = false;
    6021                 :             : 
    6022                 :             :   /* Allow the scalarizer to workshare simple where loops.  */
    6023                 :          94 :   if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
    6024                 :             :       == OMPWS_WORKSHARE_FLAG)
    6025                 :             :     {
    6026                 :          13 :       maybe_workshare = true;
    6027                 :          13 :       ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
    6028                 :             :     }
    6029                 :             : 
    6030                 :          94 :   cond = cblock->expr1;
    6031                 :          94 :   tdst = cblock->next->expr1;
    6032                 :          94 :   tsrc = cblock->next->expr2;
    6033                 :          94 :   edst = eblock ? eblock->next->expr1 : NULL;
    6034                 :          13 :   esrc = eblock ? eblock->next->expr2 : NULL;
    6035                 :             : 
    6036                 :          94 :   gfc_start_block (&block);
    6037                 :          94 :   gfc_init_loopinfo (&loop);
    6038                 :             : 
    6039                 :             :   /* Handle the condition.  */
    6040                 :          94 :   gfc_init_se (&cse, NULL);
    6041                 :          94 :   css = gfc_walk_expr (cond);
    6042                 :          94 :   gfc_add_ss_to_loop (&loop, css);
    6043                 :             : 
    6044                 :             :   /* Handle the then-clause.  */
    6045                 :          94 :   gfc_init_se (&tdse, NULL);
    6046                 :          94 :   gfc_init_se (&tsse, NULL);
    6047                 :          94 :   tdss = gfc_walk_expr (tdst);
    6048                 :          94 :   tsss = gfc_walk_expr (tsrc);
    6049                 :          94 :   if (tsss == gfc_ss_terminator)
    6050                 :             :     {
    6051                 :          58 :       tsss = gfc_get_scalar_ss (gfc_ss_terminator, tsrc);
    6052                 :          58 :       tsss->info->where = 1;
    6053                 :             :     }
    6054                 :          94 :   gfc_add_ss_to_loop (&loop, tdss);
    6055                 :          94 :   gfc_add_ss_to_loop (&loop, tsss);
    6056                 :             : 
    6057                 :          94 :   if (eblock)
    6058                 :             :     {
    6059                 :             :       /* Handle the else clause.  */
    6060                 :          13 :       gfc_init_se (&edse, NULL);
    6061                 :          13 :       gfc_init_se (&esse, NULL);
    6062                 :          13 :       edss = gfc_walk_expr (edst);
    6063                 :          13 :       esss = gfc_walk_expr (esrc);
    6064                 :          13 :       if (esss == gfc_ss_terminator)
    6065                 :             :         {
    6066                 :          13 :           esss = gfc_get_scalar_ss (gfc_ss_terminator, esrc);
    6067                 :          13 :           esss->info->where = 1;
    6068                 :             :         }
    6069                 :          13 :       gfc_add_ss_to_loop (&loop, edss);
    6070                 :          13 :       gfc_add_ss_to_loop (&loop, esss);
    6071                 :             :     }
    6072                 :             : 
    6073                 :          94 :   gfc_conv_ss_startstride (&loop);
    6074                 :          94 :   gfc_conv_loop_setup (&loop, &tdst->where);
    6075                 :             : 
    6076                 :          94 :   gfc_mark_ss_chain_used (css, 1);
    6077                 :          94 :   gfc_mark_ss_chain_used (tdss, 1);
    6078                 :          94 :   gfc_mark_ss_chain_used (tsss, 1);
    6079                 :          94 :   if (eblock)
    6080                 :             :     {
    6081                 :          13 :       gfc_mark_ss_chain_used (edss, 1);
    6082                 :          13 :       gfc_mark_ss_chain_used (esss, 1);
    6083                 :             :     }
    6084                 :             : 
    6085                 :          94 :   gfc_start_scalarized_body (&loop, &body);
    6086                 :             : 
    6087                 :          94 :   gfc_copy_loopinfo_to_se (&cse, &loop);
    6088                 :          94 :   gfc_copy_loopinfo_to_se (&tdse, &loop);
    6089                 :          94 :   gfc_copy_loopinfo_to_se (&tsse, &loop);
    6090                 :          94 :   cse.ss = css;
    6091                 :          94 :   tdse.ss = tdss;
    6092                 :          94 :   tsse.ss = tsss;
    6093                 :          94 :   if (eblock)
    6094                 :             :     {
    6095                 :          13 :       gfc_copy_loopinfo_to_se (&edse, &loop);
    6096                 :          13 :       gfc_copy_loopinfo_to_se (&esse, &loop);
    6097                 :          13 :       edse.ss = edss;
    6098                 :          13 :       esse.ss = esss;
    6099                 :             :     }
    6100                 :             : 
    6101                 :          94 :   gfc_conv_expr (&cse, cond);
    6102                 :          94 :   gfc_add_block_to_block (&body, &cse.pre);
    6103                 :          94 :   cexpr = cse.expr;
    6104                 :             : 
    6105                 :          94 :   gfc_conv_expr (&tsse, tsrc);
    6106                 :          94 :   if (tdss != gfc_ss_terminator && loop.temp_ss != NULL)
    6107                 :           0 :     gfc_conv_tmp_array_ref (&tdse);
    6108                 :             :   else
    6109                 :          94 :     gfc_conv_expr (&tdse, tdst);
    6110                 :             : 
    6111                 :          94 :   if (eblock)
    6112                 :             :     {
    6113                 :          13 :       gfc_conv_expr (&esse, esrc);
    6114                 :          13 :       if (edss != gfc_ss_terminator && loop.temp_ss != NULL)
    6115                 :           0 :         gfc_conv_tmp_array_ref (&edse);
    6116                 :             :       else
    6117                 :          13 :         gfc_conv_expr (&edse, edst);
    6118                 :             :     }
    6119                 :             : 
    6120                 :          94 :   tstmt = gfc_trans_scalar_assign (&tdse, &tsse, tdst->ts, false, true);
    6121                 :          94 :   estmt = eblock ? gfc_trans_scalar_assign (&edse, &esse, edst->ts,
    6122                 :             :                                             false, true)
    6123                 :          81 :                  : build_empty_stmt (input_location);
    6124                 :          94 :   tmp = build3_v (COND_EXPR, cexpr, tstmt, estmt);
    6125                 :          94 :   gfc_add_expr_to_block (&body, tmp);
    6126                 :          94 :   gfc_add_block_to_block (&body, &cse.post);
    6127                 :             : 
    6128                 :          94 :   if (maybe_workshare)
    6129                 :          13 :     ompws_flags &= ~OMPWS_SCALARIZER_BODY;
    6130                 :          94 :   gfc_trans_scalarizing_loops (&loop, &body);
    6131                 :          94 :   gfc_add_block_to_block (&block, &loop.pre);
    6132                 :          94 :   gfc_add_block_to_block (&block, &loop.post);
    6133                 :          94 :   gfc_cleanup_loop (&loop);
    6134                 :             : 
    6135                 :          94 :   return gfc_finish_block (&block);
    6136                 :             : }
    6137                 :             : 
    6138                 :             : /* As the WHERE or WHERE construct statement can be nested, we call
    6139                 :             :    gfc_trans_where_2 to do the translation, and pass the initial
    6140                 :             :    NULL values for both the control mask and the pending control mask.  */
    6141                 :             : 
    6142                 :             : tree
    6143                 :         341 : gfc_trans_where (gfc_code * code)
    6144                 :             : {
    6145                 :         341 :   stmtblock_t block;
    6146                 :         341 :   gfc_code *cblock;
    6147                 :         341 :   gfc_code *eblock;
    6148                 :             : 
    6149                 :         341 :   cblock = code->block;
    6150                 :         341 :   if (cblock->next
    6151                 :         314 :       && cblock->next->op == EXEC_ASSIGN
    6152                 :         269 :       && !cblock->next->next)
    6153                 :             :     {
    6154                 :         267 :       eblock = cblock->block;
    6155                 :         267 :       if (!eblock)
    6156                 :             :         {
    6157                 :             :           /* A simple "WHERE (cond) x = y" statement or block is
    6158                 :             :              dependence free if cond is not dependent upon writing x,
    6159                 :             :              and the source y is unaffected by the destination x.  */
    6160                 :         162 :           if (!gfc_check_dependency (cblock->next->expr1,
    6161                 :             :                                      cblock->expr1, 0)
    6162                 :         268 :               && !gfc_check_dependency (cblock->next->expr1,
    6163                 :         106 :                                         cblock->next->expr2, 0))
    6164                 :          81 :             return gfc_trans_where_3 (cblock, NULL);
    6165                 :             :         }
    6166                 :         105 :       else if (!eblock->expr1
    6167                 :          35 :                && !eblock->block
    6168                 :          35 :                && eblock->next
    6169                 :          26 :                && eblock->next->op == EXEC_ASSIGN
    6170                 :          25 :                && !eblock->next->next)
    6171                 :             :         {
    6172                 :             :           /* A simple "WHERE (cond) x1 = y1 ELSEWHERE x2 = y2 ENDWHERE"
    6173                 :             :              block is dependence free if cond is not dependent on writes
    6174                 :             :              to x1 and x2, y1 is not dependent on writes to x2, and y2
    6175                 :             :              is not dependent on writes to x1, and both y's are not
    6176                 :             :              dependent upon their own x's.  In addition to this, the
    6177                 :             :              final two dependency checks below exclude all but the same
    6178                 :             :              array reference if the where and elswhere destinations
    6179                 :             :              are the same.  In short, this is VERY conservative and this
    6180                 :             :              is needed because the two loops, required by the standard
    6181                 :             :              are coalesced in gfc_trans_where_3.  */
    6182                 :          25 :           if (!gfc_check_dependency (cblock->next->expr1,
    6183                 :             :                                     cblock->expr1, 0)
    6184                 :          25 :               && !gfc_check_dependency (eblock->next->expr1,
    6185                 :             :                                        cblock->expr1, 0)
    6186                 :          25 :               && !gfc_check_dependency (cblock->next->expr1,
    6187                 :          25 :                                        eblock->next->expr2, 1)
    6188                 :          19 :               && !gfc_check_dependency (eblock->next->expr1,
    6189                 :          19 :                                        cblock->next->expr2, 1)
    6190                 :          19 :               && !gfc_check_dependency (cblock->next->expr1,
    6191                 :          19 :                                        cblock->next->expr2, 1)
    6192                 :          19 :               && !gfc_check_dependency (eblock->next->expr1,
    6193                 :          19 :                                        eblock->next->expr2, 1)
    6194                 :          19 :               && !gfc_check_dependency (cblock->next->expr1,
    6195                 :          19 :                                        eblock->next->expr1, 0)
    6196                 :          44 :               && !gfc_check_dependency (eblock->next->expr1,
    6197                 :          19 :                                        cblock->next->expr1, 0))
    6198                 :          13 :             return gfc_trans_where_3 (cblock, eblock);
    6199                 :             :         }
    6200                 :             :     }
    6201                 :             : 
    6202                 :         247 :   gfc_start_block (&block);
    6203                 :             : 
    6204                 :         247 :   gfc_trans_where_2 (code, NULL, false, NULL, &block);
    6205                 :             : 
    6206                 :         247 :   return gfc_finish_block (&block);
    6207                 :             : }
    6208                 :             : 
    6209                 :             : 
    6210                 :             : /* CYCLE a DO loop. The label decl has already been created by
    6211                 :             :    gfc_trans_do(), it's in TREE_PURPOSE (backend_decl) of the gfc_code
    6212                 :             :    node at the head of the loop. We must mark the label as used.  */
    6213                 :             : 
    6214                 :             : tree
    6215                 :         105 : gfc_trans_cycle (gfc_code * code)
    6216                 :             : {
    6217                 :         105 :   tree cycle_label;
    6218                 :             : 
    6219                 :         105 :   cycle_label = code->ext.which_construct->cycle_label;
    6220                 :         105 :   gcc_assert (cycle_label);
    6221                 :             : 
    6222                 :         105 :   TREE_USED (cycle_label) = 1;
    6223                 :         105 :   return build1_v (GOTO_EXPR, cycle_label);
    6224                 :             : }
    6225                 :             : 
    6226                 :             : 
    6227                 :             : /* EXIT a DO loop. Similar to CYCLE, but now the label is in
    6228                 :             :    TREE_VALUE (backend_decl) of the gfc_code node at the head of the
    6229                 :             :    loop.  */
    6230                 :             : 
    6231                 :             : tree
    6232                 :         683 : gfc_trans_exit (gfc_code * code)
    6233                 :             : {
    6234                 :         683 :   tree exit_label;
    6235                 :             : 
    6236                 :         683 :   exit_label = code->ext.which_construct->exit_label;
    6237                 :         683 :   gcc_assert (exit_label);
    6238                 :             : 
    6239                 :         683 :   TREE_USED (exit_label) = 1;
    6240                 :         683 :   return build1_v (GOTO_EXPR, exit_label);
    6241                 :             : }
    6242                 :             : 
    6243                 :             : 
    6244                 :             : /* Get the initializer expression for the code and expr of an allocate.
    6245                 :             :    When no initializer is needed return NULL.  */
    6246                 :             : 
    6247                 :             : static gfc_expr *
    6248                 :       11415 : allocate_get_initializer (gfc_code * code, gfc_expr * expr)
    6249                 :             : {
    6250                 :       11415 :   if (!gfc_bt_struct (expr->ts.type) && expr->ts.type != BT_CLASS)
    6251                 :             :     return NULL;
    6252                 :             : 
    6253                 :             :   /* An explicit type was given in allocate ( T:: object).  */
    6254                 :        3442 :   if (code->ext.alloc.ts.type == BT_DERIVED
    6255                 :        3442 :       && (code->ext.alloc.ts.u.derived->attr.alloc_comp
    6256                 :         483 :           || gfc_has_default_initializer (code->ext.alloc.ts.u.derived)))
    6257                 :         334 :     return gfc_default_initializer (&code->ext.alloc.ts);
    6258                 :             : 
    6259                 :         535 :   if (gfc_bt_struct (expr->ts.type)
    6260                 :        3108 :       && (expr->ts.u.derived->attr.alloc_comp
    6261                 :        1797 :           || gfc_has_default_initializer (expr->ts.u.derived)))
    6262                 :        1169 :     return gfc_default_initializer (&expr->ts);
    6263                 :             : 
    6264                 :        1939 :   if (expr->ts.type == BT_CLASS
    6265                 :        1939 :       && (CLASS_DATA (expr)->ts.u.derived->attr.alloc_comp
    6266                 :         445 :           || gfc_has_default_initializer (CLASS_DATA (expr)->ts.u.derived)))
    6267                 :         169 :     return gfc_default_initializer (&CLASS_DATA (expr)->ts);
    6268                 :             : 
    6269                 :             :   return NULL;
    6270                 :             : }
    6271                 :             : 
    6272                 :             : /* Translate the ALLOCATE statement.  */
    6273                 :             : 
    6274                 :             : tree
    6275                 :       12179 : gfc_trans_allocate (gfc_code * code, gfc_omp_namelist *omp_allocate)
    6276                 :             : {
    6277                 :       12179 :   gfc_alloc *al;
    6278                 :       12179 :   gfc_expr *expr, *e3rhs = NULL, *init_expr;
    6279                 :       12179 :   gfc_se se, se_sz;
    6280                 :       12179 :   tree tmp;
    6281                 :       12179 :   tree parm;
    6282                 :       12179 :   tree stat;
    6283                 :       12179 :   tree errmsg;
    6284                 :       12179 :   tree errlen;
    6285                 :       12179 :   tree label_errmsg;
    6286                 :       12179 :   tree label_finish;
    6287                 :       12179 :   tree memsz;
    6288                 :       12179 :   tree al_vptr, al_len;
    6289                 :             :   /* If an expr3 is present, then store the tree for accessing its
    6290                 :             :      _vptr, and _len components in the variables, respectively.  The
    6291                 :             :      element size, i.e. _vptr%size, is stored in expr3_esize.  Any of
    6292                 :             :      the trees may be the NULL_TREE indicating that this is not
    6293                 :             :      available for expr3's type.  */
    6294                 :       12179 :   tree expr3, expr3_vptr, expr3_len, expr3_esize;
    6295                 :             :   /* Classify what expr3 stores.  */
    6296                 :       12179 :   enum { E3_UNSET = 0, E3_SOURCE, E3_MOLD, E3_DESC } e3_is;
    6297                 :       12179 :   stmtblock_t block;
    6298                 :       12179 :   stmtblock_t post;
    6299                 :       12179 :   stmtblock_t final_block;
    6300                 :       12179 :   tree nelems;
    6301                 :       12179 :   bool upoly_expr, tmp_expr3_len_flag = false, al_len_needs_set, is_coarray;
    6302                 :       12179 :   bool needs_caf_sync, caf_refs_comp;
    6303                 :       12179 :   bool e3_has_nodescriptor = false;
    6304                 :       12179 :   gfc_symtree *newsym = NULL;
    6305                 :       12179 :   symbol_attribute caf_attr;
    6306                 :       12179 :   gfc_actual_arglist *param_list;
    6307                 :             : 
    6308                 :       12179 :   if (!code->ext.alloc.list)
    6309                 :             :     return NULL_TREE;
    6310                 :             : 
    6311                 :       12179 :   stat = tmp = memsz = al_vptr = al_len = NULL_TREE;
    6312                 :       12179 :   expr3 = expr3_vptr = expr3_len = expr3_esize = NULL_TREE;
    6313                 :       12179 :   label_errmsg = label_finish = errmsg = errlen = NULL_TREE;
    6314                 :       12179 :   e3_is = E3_UNSET;
    6315                 :       12179 :   is_coarray = needs_caf_sync = false;
    6316                 :             : 
    6317                 :       12179 :   gfc_init_block (&block);
    6318                 :       12179 :   gfc_init_block (&post);
    6319                 :       12179 :   gfc_init_block (&final_block);
    6320                 :             : 
    6321                 :             :   /* STAT= (and maybe ERRMSG=) is present.  */
    6322                 :       12179 :   if (code->expr1)
    6323                 :             :     {
    6324                 :             :       /* STAT=.  */
    6325                 :         267 :       tree gfc_int4_type_node = gfc_get_int_type (4);
    6326                 :         267 :       stat = gfc_create_var (gfc_int4_type_node, "stat");
    6327                 :             : 
    6328                 :             :       /* ERRMSG= only makes sense with STAT=.  */
    6329                 :         267 :       if (code->expr2)
    6330                 :             :         {
    6331                 :          68 :           gfc_init_se (&se, NULL);
    6332                 :          68 :           se.want_pointer = 1;
    6333                 :          68 :           gfc_conv_expr_lhs (&se, code->expr2);
    6334                 :          68 :           errmsg = se.expr;
    6335                 :          68 :           errlen = se.string_length;
    6336                 :             :         }
    6337                 :             :       else
    6338                 :             :         {
    6339                 :         199 :           errmsg = null_pointer_node;
    6340                 :         199 :           errlen = build_int_cst (gfc_charlen_type_node, 0);
    6341                 :             :         }
    6342                 :             : 
    6343                 :             :       /* GOTO destinations.  */
    6344                 :         267 :       label_errmsg = gfc_build_label_decl (NULL_TREE);
    6345                 :         267 :       label_finish = gfc_build_label_decl (NULL_TREE);
    6346                 :         267 :       TREE_USED (label_finish) = 0;
    6347                 :             :     }
    6348                 :             : 
    6349                 :             :   /* When an expr3 is present evaluate it only once.  The standards prevent a
    6350                 :             :      dependency of expr3 on the objects in the allocate list.  An expr3 can
    6351                 :             :      be pre-evaluated in all cases.  One just has to make sure, to use the
    6352                 :             :      correct way, i.e., to get the descriptor or to get a reference
    6353                 :             :      expression.  */
    6354                 :       12179 :   if (code->expr3)
    6355                 :             :     {
    6356                 :        3331 :       bool vtab_needed = false, temp_var_needed = false,
    6357                 :        3331 :           temp_obj_created = false;
    6358                 :             : 
    6359                 :        3331 :       is_coarray = gfc_is_coarray (code->expr3);
    6360                 :             : 
    6361                 :         262 :       if (code->expr3->expr_type == EXPR_FUNCTION && !code->expr3->mold
    6362                 :        3557 :           && (gfc_is_class_array_function (code->expr3)
    6363                 :         203 :               || gfc_is_alloc_class_scalar_function (code->expr3)))
    6364                 :          61 :         code->expr3->must_finalize = 1;
    6365                 :             : 
    6366                 :             :       /* Figure whether we need the vtab from expr3.  */
    6367                 :        6674 :       for (al = code->ext.alloc.list; !vtab_needed && al != NULL;
    6368                 :        3343 :            al = al->next)
    6369                 :        3343 :         vtab_needed = (al->expr->ts.type == BT_CLASS);
    6370                 :             : 
    6371                 :        3331 :       gfc_init_se (&se, NULL);
    6372                 :             :       /* When expr3 is a variable, i.e., a very simple expression, then
    6373                 :             :          convert it once here.  If one has a source expression that has
    6374                 :             :          substring references, part-refs, or %re/%im inquiries, wrap the
    6375                 :             :          entity in parentheses to force evaluation of the expression.  */
    6376                 :        3331 :       if (code->expr3->expr_type == EXPR_VARIABLE
    6377                 :        3331 :           && is_subref_array (code->expr3))
    6378                 :          60 :         code->expr3 = gfc_get_parentheses (code->expr3);
    6379                 :             : 
    6380                 :        3331 :       if (code->expr3->expr_type == EXPR_VARIABLE
    6381                 :        2253 :           || code->expr3->expr_type == EXPR_ARRAY
    6382                 :        1329 :           || code->expr3->expr_type == EXPR_CONSTANT)
    6383                 :             :         {
    6384                 :        2375 :           if (!code->expr3->mold
    6385                 :         186 :               || code->expr3->ts.type == BT_CHARACTER
    6386                 :         102 :               || vtab_needed
    6387                 :          69 :               || code->ext.alloc.arr_spec_from_expr3)
    6388                 :             :             {
    6389                 :             :               /* Convert expr3 to a tree.  For all "simple" expression just
    6390                 :             :                  get the descriptor or the reference, respectively, depending
    6391                 :             :                  on the rank of the expr.  */
    6392                 :        2375 :               if (code->ext.alloc.arr_spec_from_expr3 || code->expr3->rank != 0)
    6393                 :        1454 :                 gfc_conv_expr_descriptor (&se, code->expr3);
    6394                 :             :               else
    6395                 :             :                 {
    6396                 :         921 :                   gfc_conv_expr_reference (&se, code->expr3);
    6397                 :             : 
    6398                 :             :                   /* gfc_conv_expr_reference wraps POINTER_PLUS_EXPR in a
    6399                 :             :                      NOP_EXPR, which prevents gfortran from getting the vptr
    6400                 :             :                      from the source=-expression.  Remove the NOP_EXPR and go
    6401                 :             :                      with the POINTER_PLUS_EXPR in this case.  */
    6402                 :         921 :                   if (code->expr3->ts.type == BT_CLASS
    6403                 :         234 :                       && TREE_CODE (se.expr) == NOP_EXPR
    6404                 :        1063 :                       && (TREE_CODE (TREE_OPERAND (se.expr, 0))
    6405                 :             :                                                             == POINTER_PLUS_EXPR
    6406                 :         124 :                           || is_coarray))
    6407                 :          30 :                     se.expr = TREE_OPERAND (se.expr, 0);
    6408                 :             :                 }
    6409                 :             :               /* Create a temp variable only for component refs to prevent
    6410                 :             :                  having to go through the full deref-chain each time and to
    6411                 :             :                  simplify computation of array properties.  */
    6412                 :        2375 :               temp_var_needed = TREE_CODE (se.expr) == COMPONENT_REF;
    6413                 :             :             }
    6414                 :             :         }
    6415                 :             :       else
    6416                 :             :         {
    6417                 :             :           /* In all other cases evaluate the expr3.  */
    6418                 :         956 :           symbol_attribute attr;
    6419                 :             :           /* Get the descriptor for all arrays, that are not allocatable or
    6420                 :             :              pointer, because the latter are descriptors already.
    6421                 :             :              The exception are function calls returning a class object:
    6422                 :             :              The descriptor is stored in their results _data component, which
    6423                 :             :              is easier to access, when first a temporary variable for the
    6424                 :             :              result is created and the descriptor retrieved from there.  */
    6425                 :         956 :           attr = gfc_expr_attr (code->expr3);
    6426                 :         956 :           if (code->expr3->rank != 0
    6427                 :         227 :               && ((!attr.allocatable && !attr.pointer)
    6428                 :          64 :                   || (code->expr3->expr_type == EXPR_FUNCTION
    6429                 :          64 :                       && (code->expr3->ts.type != BT_CLASS
    6430                 :          46 :                           || (code->expr3->value.function.isym
    6431                 :          12 :                               && code->expr3->value.function.isym
    6432                 :          12 :                                                          ->transformational)))))
    6433                 :         193 :             gfc_conv_expr_descriptor (&se, code->expr3);
    6434                 :             :           else
    6435                 :         763 :             gfc_conv_expr_reference (&se, code->expr3);
    6436                 :         956 :           if (code->expr3->ts.type == BT_CLASS)
    6437                 :         116 :             gfc_conv_class_to_class (&se, code->expr3,
    6438                 :             :                                      code->expr3->ts,
    6439                 :             :                                      false, true,
    6440                 :             :                                      false, false);
    6441                 :         956 :           temp_obj_created = temp_var_needed = !VAR_P (se.expr);
    6442                 :             :         }
    6443                 :        3331 :       gfc_add_block_to_block (&block, &se.pre);
    6444                 :        3331 :       if (code->expr3->must_finalize)
    6445                 :             :         {
    6446                 :          61 :           gfc_add_block_to_block (&final_block, &se.finalblock);
    6447                 :          61 :           gfc_add_block_to_block (&final_block, &se.post);
    6448                 :             :         }
    6449                 :             :       else
    6450                 :        3270 :         gfc_add_block_to_block (&post, &se.post);
    6451                 :             : 
    6452                 :             :       /* Special case when string in expr3 is zero.  */
    6453                 :        3331 :       if (code->expr3->ts.type == BT_CHARACTER
    6454                 :        3331 :           && integer_zerop (se.string_length))
    6455                 :             :         {
    6456                 :           6 :           gfc_init_se (&se, NULL);
    6457                 :           6 :           temp_var_needed = false;
    6458                 :           6 :           expr3_len = build_zero_cst (gfc_charlen_type_node);
    6459                 :           6 :           e3_is = E3_MOLD;
    6460                 :             :         }
    6461                 :             :       /* Prevent aliasing, i.e., se.expr may be already a
    6462                 :             :              variable declaration.  */
    6463                 :        3325 :       else if (se.expr != NULL_TREE && temp_var_needed)
    6464                 :             :         {
    6465                 :         800 :           tree var, desc;
    6466                 :         800 :           tmp = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)) || is_coarray ?
    6467                 :             :                 se.expr
    6468                 :         732 :               : build_fold_indirect_ref_loc (input_location, se.expr);
    6469                 :             : 
    6470                 :             :           /* Get the array descriptor and prepare it to be assigned to the
    6471                 :             :              temporary variable var.  For classes the array descriptor is
    6472                 :             :              in the _data component and the object goes into the
    6473                 :             :              GFC_DECL_SAVED_DESCRIPTOR.  */
    6474                 :         800 :           if (code->expr3->ts.type == BT_CLASS
    6475                 :         159 :               && code->expr3->rank != 0)
    6476                 :             :             {
    6477                 :             :               /* When an array_ref was in expr3, then the descriptor is the
    6478                 :             :                  first operand.  */
    6479                 :          77 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)) || is_coarray)
    6480                 :             :                 {
    6481                 :          43 :                   desc = TREE_OPERAND (tmp, 0);
    6482                 :             :                 }
    6483                 :             :               else
    6484                 :             :                 {
    6485                 :          34 :                   desc = tmp;
    6486                 :          34 :                   tmp = gfc_class_data_get (tmp);
    6487                 :             :                 }
    6488                 :          77 :               if (code->ext.alloc.arr_spec_from_expr3)
    6489                 :          32 :                 e3_is = E3_DESC;
    6490                 :             :             }
    6491                 :             :           else
    6492                 :         735 :             desc = !is_coarray ? se.expr
    6493                 :          12 :                                : TREE_OPERAND (TREE_OPERAND (se.expr, 0), 0);
    6494                 :             :           /* We need a regular (non-UID) symbol here, therefore give a
    6495                 :             :              prefix.  */
    6496                 :         800 :           var = gfc_create_var (TREE_TYPE (tmp), "source");
    6497                 :         800 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)) || is_coarray)
    6498                 :             :             {
    6499                 :         102 :               gfc_allocate_lang_decl (var);
    6500                 :         102 :               GFC_DECL_SAVED_DESCRIPTOR (var) = desc;
    6501                 :             :             }
    6502                 :         800 :           gfc_add_modify_loc (input_location, &block, var, tmp);
    6503                 :             : 
    6504                 :         800 :           expr3 = var;
    6505                 :         800 :           if (se.string_length)
    6506                 :             :             /* Evaluate it assuming that it also is complicated like expr3.  */
    6507                 :          13 :             expr3_len = gfc_evaluate_now (se.string_length, &block);
    6508                 :             :         }
    6509                 :             :       else
    6510                 :             :         {
    6511                 :        2525 :           expr3 = se.expr;
    6512                 :        2525 :           expr3_len = se.string_length;
    6513                 :             :         }
    6514                 :             : 
    6515                 :             :       /* Deallocate any allocatable components in expressions that use a
    6516                 :             :          temporary object, i.e. are not a simple alias of to an EXPR_VARIABLE.
    6517                 :             :          E.g. temporaries of a function call need freeing of their components
    6518                 :             :          here. Explicit derived type allocation of class entities uses expr3
    6519                 :             :          to carry the default initializer. This must not be deallocated or
    6520                 :             :          finalized.  */
    6521                 :        3331 :       if ((code->expr3->ts.type == BT_DERIVED
    6522                 :        2116 :            || code->expr3->ts.type == BT_CLASS)
    6523                 :        1692 :           && (code->expr3->expr_type != EXPR_VARIABLE || temp_obj_created)
    6524                 :        1149 :           && code->expr3->ts.u.derived->attr.alloc_comp
    6525                 :         286 :           && !code->expr3->must_finalize
    6526                 :         280 :           && !code->ext.alloc.expr3_not_explicit)
    6527                 :             :         {
    6528                 :         200 :           tmp = gfc_deallocate_alloc_comp (code->expr3->ts.u.derived,
    6529                 :             :                                            expr3, code->expr3->rank);
    6530                 :         200 :           gfc_prepend_expr_to_block (&post, tmp);
    6531                 :             :         }
    6532                 :             : 
    6533                 :             :       /* Store what the expr3 is to be used for.  */
    6534                 :        3331 :       if (e3_is == E3_UNSET)
    6535                 :        3293 :         e3_is = expr3 != NULL_TREE ?
    6536                 :        3293 :               (code->ext.alloc.arr_spec_from_expr3 ?
    6537                 :             :                  E3_DESC
    6538                 :        2257 :                : (code->expr3->mold ? E3_MOLD : E3_SOURCE))
    6539                 :             :             : E3_UNSET;
    6540                 :             : 
    6541                 :             :       /* Figure how to get the _vtab entry.  This also obtains the tree
    6542                 :             :          expression for accessing the _len component, because only
    6543                 :             :          unlimited polymorphic objects, which are a subcategory of class
    6544                 :             :          types, have a _len component.  */
    6545                 :        3331 :       if (code->expr3->ts.type == BT_CLASS)
    6546                 :             :         {
    6547                 :         477 :           gfc_expr *rhs;
    6548                 :         477 :           tmp = expr3 != NULL_TREE && POINTER_TYPE_P (TREE_TYPE (expr3)) ?
    6549                 :         234 :                 build_fold_indirect_ref (expr3): expr3;
    6550                 :             :           /* Polymorphic SOURCE: VPTR must be determined at run time.
    6551                 :             :              expr3 may be a temporary array declaration, therefore check for
    6552                 :             :              GFC_CLASS_TYPE_P before trying to get the _vptr component.  */
    6553                 :         477 :           if (tmp != NULL_TREE
    6554                 :         477 :               && (e3_is == E3_DESC
    6555                 :         403 :                   || (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
    6556                 :         255 :                       && (VAR_P (tmp) || !code->expr3->ref))
    6557                 :         172 :                   || (VAR_P (tmp) && DECL_LANG_SPECIFIC (tmp))))
    6558                 :         404 :             tmp = gfc_class_vptr_get (expr3);
    6559                 :             :           else
    6560                 :             :             {
    6561                 :          73 :               rhs = gfc_find_and_cut_at_last_class_ref (code->expr3);
    6562                 :          73 :               gfc_add_vptr_component (rhs);
    6563                 :          73 :               gfc_init_se (&se, NULL);
    6564                 :          73 :               se.want_pointer = 1;
    6565                 :          73 :               gfc_conv_expr (&se, rhs);
    6566                 :          73 :               tmp = se.expr;
    6567                 :          73 :               gfc_free_expr (rhs);
    6568                 :             :             }
    6569                 :             :           /* Set the element size.  */
    6570                 :         477 :           expr3_esize = gfc_vptr_size_get (tmp);
    6571                 :         477 :           if (vtab_needed)
    6572                 :         471 :             expr3_vptr = tmp;
    6573                 :             :           /* Initialize the ref to the _len component.  */
    6574                 :         477 :           if (expr3_len == NULL_TREE && UNLIMITED_POLY (code->expr3))
    6575                 :             :             {
    6576                 :             :               /* Same like for retrieving the _vptr.  */
    6577                 :         139 :               if (expr3 != NULL_TREE && !code->expr3->ref)
    6578                 :          79 :                 expr3_len = gfc_class_len_get (expr3);
    6579                 :             :               else
    6580                 :             :                 {
    6581                 :          60 :                   rhs = gfc_find_and_cut_at_last_class_ref (code->expr3);
    6582                 :          60 :                   gfc_add_len_component (rhs);
    6583                 :          60 :                   gfc_init_se (&se, NULL);
    6584                 :          60 :                   gfc_conv_expr (&se, rhs);
    6585                 :          60 :                   expr3_len = se.expr;
    6586                 :          60 :                   gfc_free_expr (rhs);
    6587                 :             :                 }
    6588                 :             :             }
    6589                 :             :         }
    6590                 :             :       else
    6591                 :             :         {
    6592                 :             :           /* When the object to allocate is polymorphic type, then it
    6593                 :             :              needs its vtab set correctly, so deduce the required _vtab
    6594                 :             :              and _len from the source expression.  */
    6595                 :        2854 :           if (vtab_needed)
    6596                 :             :             {
    6597                 :             :               /* VPTR is fixed at compile time.  */
    6598                 :        1134 :               gfc_symbol *vtab;
    6599                 :             : 
    6600                 :        1134 :               vtab = gfc_find_vtab (&code->expr3->ts);
    6601                 :        1134 :               gcc_assert (vtab);
    6602                 :        1134 :               expr3_vptr = gfc_get_symbol_decl (vtab);
    6603                 :        1134 :               expr3_vptr = gfc_build_addr_expr (NULL_TREE,
    6604                 :             :                                                 expr3_vptr);
    6605                 :             :             }
    6606                 :             :           /* _len component needs to be set, when ts is a character
    6607                 :             :              array.  */
    6608                 :        2854 :           if (expr3_len == NULL_TREE
    6609                 :        2245 :               && code->expr3->ts.type == BT_CHARACTER)
    6610                 :             :             {
    6611                 :           0 :               if (code->expr3->ts.u.cl
    6612                 :           0 :                   && code->expr3->ts.u.cl->length)
    6613                 :             :                 {
    6614                 :           0 :                   gfc_init_se (&se, NULL);
    6615                 :           0 :                   gfc_conv_expr (&se, code->expr3->ts.u.cl->length);
    6616                 :           0 :                   gfc_add_block_to_block (&block, &se.pre);
    6617                 :           0 :                   expr3_len = gfc_evaluate_now (se.expr, &block);
    6618                 :             :                 }
    6619                 :           0 :               gcc_assert (expr3_len);
    6620                 :             :             }
    6621                 :             :           /* For character arrays only the kind's size is needed, because
    6622                 :             :              the array mem_size is _len * (elem_size = kind_size).
    6623                 :             :              For all other get the element size in the normal way.  */
    6624                 :        2854 :           if (code->expr3->ts.type == BT_CHARACTER)
    6625                 :         609 :             expr3_esize = TYPE_SIZE_UNIT (
    6626                 :             :                   gfc_get_char_type (code->expr3->ts.kind));
    6627                 :             :           else
    6628                 :        2245 :             expr3_esize = TYPE_SIZE_UNIT (
    6629                 :             :                   gfc_typenode_for_spec (&code->expr3->ts));
    6630                 :             :         }
    6631                 :        3331 :       gcc_assert (expr3_esize);
    6632                 :        3331 :       expr3_esize = fold_convert (sizetype, expr3_esize);
    6633                 :        3331 :       if (e3_is == E3_MOLD)
    6634                 :             :         /* The expr3 is no longer valid after this point.  */
    6635                 :         125 :         expr3 = NULL_TREE;
    6636                 :             :     }
    6637                 :        8848 :   else if (code->ext.alloc.ts.type != BT_UNKNOWN)
    6638                 :             :     {
    6639                 :             :       /* Compute the explicit typespec given only once for all objects
    6640                 :             :          to allocate.  */
    6641                 :        1103 :       if (code->ext.alloc.ts.type != BT_CHARACTER)
    6642                 :         725 :         expr3_esize = TYPE_SIZE_UNIT (
    6643                 :             :               gfc_typenode_for_spec (&code->ext.alloc.ts));
    6644                 :         378 :       else if (code->ext.alloc.ts.u.cl->length != NULL)
    6645                 :             :         {
    6646                 :         372 :           gfc_expr *sz;
    6647                 :         372 :           sz = gfc_copy_expr (code->ext.alloc.ts.u.cl->length);
    6648                 :         372 :           gfc_init_se (&se_sz, NULL);
    6649                 :         372 :           gfc_conv_expr (&se_sz, sz);
    6650                 :         372 :           gfc_free_expr (sz);
    6651                 :         372 :           tmp = gfc_get_char_type (code->ext.alloc.ts.kind);
    6652                 :         372 :           tmp = TYPE_SIZE_UNIT (tmp);
    6653                 :         372 :           tmp = fold_convert (TREE_TYPE (se_sz.expr), tmp);
    6654                 :         372 :           gfc_add_block_to_block (&block, &se_sz.pre);
    6655                 :         372 :           expr3_esize = fold_build2_loc (input_location, MULT_EXPR,
    6656                 :         372 :                                          TREE_TYPE (se_sz.expr),
    6657                 :             :                                          tmp, se_sz.expr);
    6658                 :         372 :           expr3_esize = gfc_evaluate_now (expr3_esize, &block);
    6659                 :             :         }
    6660                 :             :       else
    6661                 :             :         expr3_esize = NULL_TREE;
    6662                 :             :     }
    6663                 :             : 
    6664                 :             :   /* The routine gfc_trans_assignment () already implements all
    6665                 :             :      techniques needed.  Unfortunately we may have a temporary
    6666                 :             :      variable for the source= expression here.  When that is the
    6667                 :             :      case convert this variable into a temporary gfc_expr of type
    6668                 :             :      EXPR_VARIABLE and used it as rhs for the assignment.  The
    6669                 :             :      advantage is, that we get scalarizer support for free,
    6670                 :             :      don't have to take care about scalar to array treatment and
    6671                 :             :      will benefit of every enhancements gfc_trans_assignment ()
    6672                 :             :      gets.
    6673                 :             :      No need to check whether e3_is is E3_UNSET, because that is
    6674                 :             :      done by expr3 != NULL_TREE.
    6675                 :             :      Exclude variables since the following block does not handle
    6676                 :             :      array sections.  In any case, there is no harm in sending
    6677                 :             :      variables to gfc_trans_assignment because there is no
    6678                 :             :      evaluation of variables.  */
    6679                 :       12179 :   if (code->expr3)
    6680                 :             :     {
    6681                 :        3331 :       if (code->expr3->expr_type != EXPR_VARIABLE
    6682                 :        2253 :           && e3_is != E3_MOLD && expr3 != NULL_TREE
    6683                 :        5540 :           && DECL_P (expr3) && DECL_ARTIFICIAL (expr3))
    6684                 :             :         {
    6685                 :             :           /* Build a temporary symtree and symbol.  Do not add it to the current
    6686                 :             :              namespace to prevent accidentaly modifying a colliding
    6687                 :             :              symbol's as.  */
    6688                 :        1854 :           newsym = XCNEW (gfc_symtree);
    6689                 :             :           /* The name of the symtree should be unique, because gfc_create_var ()
    6690                 :             :              took care about generating the identifier.  */
    6691                 :        1854 :           newsym->name
    6692                 :        1854 :             = gfc_get_string ("%s", IDENTIFIER_POINTER (DECL_NAME (expr3)));
    6693                 :        1854 :           newsym->n.sym = gfc_new_symbol (newsym->name, NULL);
    6694                 :             :           /* The backend_decl is known.  It is expr3, which is inserted
    6695                 :             :              here.  */
    6696                 :        1854 :           newsym->n.sym->backend_decl = expr3;
    6697                 :        1854 :           e3rhs = gfc_get_expr ();
    6698                 :        1854 :           e3rhs->rank = code->expr3->rank;
    6699                 :        1854 :           e3rhs->symtree = newsym;
    6700                 :             :           /* Mark the symbol referenced or gfc_trans_assignment will bug.  */
    6701                 :        1854 :           newsym->n.sym->attr.referenced = 1;
    6702                 :        1854 :           e3rhs->expr_type = EXPR_VARIABLE;
    6703                 :        1854 :           e3rhs->where = code->expr3->where;
    6704                 :             :           /* Set the symbols type, upto it was BT_UNKNOWN.  */
    6705                 :        1854 :           if (IS_CLASS_ARRAY (code->expr3)
    6706                 :          47 :               && code->expr3->expr_type == EXPR_FUNCTION
    6707                 :          35 :               && code->expr3->value.function.isym
    6708                 :          12 :               && code->expr3->value.function.isym->transformational)
    6709                 :             :             {
    6710                 :          12 :               e3rhs->ts = CLASS_DATA (code->expr3)->ts;
    6711                 :             :             }
    6712                 :        1842 :           else if (code->expr3->ts.type == BT_CLASS
    6713                 :        1842 :                    && !GFC_CLASS_TYPE_P (TREE_TYPE (expr3)))
    6714                 :          39 :             e3rhs->ts = CLASS_DATA (code->expr3)->ts;
    6715                 :             :           else
    6716                 :        1803 :             e3rhs->ts = code->expr3->ts;
    6717                 :        1854 :           newsym->n.sym->ts = e3rhs->ts;
    6718                 :             :           /* Check whether the expr3 is array valued.  */
    6719                 :        1854 :           if (e3rhs->rank)
    6720                 :             :             {
    6721                 :        1144 :               gfc_array_spec *arr;
    6722                 :        1144 :               arr = gfc_get_array_spec ();
    6723                 :        1144 :               arr->rank = e3rhs->rank;
    6724                 :        1144 :               arr->type = AS_DEFERRED;
    6725                 :             :               /* Set the dimension and pointer attribute for arrays
    6726                 :             :              to be on the safe side.  */
    6727                 :        1144 :               newsym->n.sym->attr.dimension = 1;
    6728                 :        1144 :               newsym->n.sym->attr.pointer = 1;
    6729                 :        1144 :               newsym->n.sym->as = arr;
    6730                 :        1144 :               if (IS_CLASS_ARRAY (code->expr3)
    6731                 :          47 :                   && code->expr3->expr_type == EXPR_FUNCTION
    6732                 :          35 :                   && code->expr3->value.function.isym
    6733                 :          12 :                   && code->expr3->value.function.isym->transformational)
    6734                 :             :                 {
    6735                 :          12 :                   gfc_array_spec *tarr;
    6736                 :          12 :                   tarr = gfc_get_array_spec ();
    6737                 :          12 :                   *tarr = *arr;
    6738                 :          12 :                   e3rhs->ts.u.derived->as = tarr;
    6739                 :             :                 }
    6740                 :        1144 :               gfc_add_full_array_ref (e3rhs, arr);
    6741                 :             :             }
    6742                 :         710 :           else if (POINTER_TYPE_P (TREE_TYPE (expr3)))
    6743                 :          37 :             newsym->n.sym->attr.pointer = 1;
    6744                 :             :           /* The string length is known, too.  Set it for char arrays.  */
    6745                 :        1854 :           if (e3rhs->ts.type == BT_CHARACTER)
    6746                 :         229 :             newsym->n.sym->ts.u.cl->backend_decl = expr3_len;
    6747                 :        1854 :           gfc_commit_symbol (newsym->n.sym);
    6748                 :             :         }
    6749                 :             :       else
    6750                 :        1477 :         e3rhs = gfc_copy_expr (code->expr3);
    6751                 :             : 
    6752                 :             :       // We need to propagate the bounds of the expr3 for source=/mold=.
    6753                 :             :       // However, for non-named arrays, the lbound has to be 1 and neither the
    6754                 :             :       // bound used inside the called function even when returning an
    6755                 :             :       // allocatable/pointer nor the zero used internally.
    6756                 :        3331 :       if (e3_is == E3_DESC
    6757                 :        1068 :           && code->expr3->expr_type != EXPR_VARIABLE)
    6758                 :       12179 :         e3_has_nodescriptor = true;
    6759                 :             :     }
    6760                 :             : 
    6761                 :             :   /* Loop over all objects to allocate.  */
    6762                 :       26944 :   for (al = code->ext.alloc.list; al != NULL; al = al->next)
    6763                 :             :     {
    6764                 :       14765 :       expr = gfc_copy_expr (al->expr);
    6765                 :             :       /* UNLIMITED_POLY () needs the _data component to be set, when
    6766                 :             :          expr is a unlimited polymorphic object.  But the _data component
    6767                 :             :          has not been set yet, so check the derived type's attr for the
    6768                 :             :          unlimited polymorphic flag to be safe.  */
    6769                 :       14765 :       upoly_expr = UNLIMITED_POLY (expr)
    6770                 :       28989 :                     || (expr->ts.type == BT_DERIVED
    6771                 :        2266 :                         && expr->ts.u.derived->attr.unlimited_polymorphic);
    6772                 :       14765 :       gfc_init_se (&se, NULL);
    6773                 :             : 
    6774                 :             :       /* For class types prepare the expressions to ref the _vptr
    6775                 :             :          and the _len component.  The latter for unlimited polymorphic
    6776                 :             :          types only.  */
    6777                 :       14765 :       if (expr->ts.type == BT_CLASS)
    6778                 :             :         {
    6779                 :        3274 :           gfc_expr *expr_ref_vptr, *expr_ref_len;
    6780                 :        3274 :           gfc_add_data_component (expr);
    6781                 :             :           /* Prep the vptr handle.  */
    6782                 :        3274 :           expr_ref_vptr = gfc_copy_expr (al->expr);
    6783                 :        3274 :           gfc_add_vptr_component (expr_ref_vptr);
    6784                 :        3274 :           se.want_pointer = 1;
    6785                 :        3274 :           gfc_conv_expr (&se, expr_ref_vptr);
    6786                 :        3274 :           al_vptr = se.expr;
    6787                 :        3274 :           se.want_pointer = 0;
    6788                 :        3274 :           gfc_free_expr (expr_ref_vptr);
    6789                 :             :           /* Allocated unlimited polymorphic objects always have a _len
    6790                 :             :              component.  */
    6791                 :        3274 :           if (upoly_expr)
    6792                 :             :             {
    6793                 :         541 :               expr_ref_len = gfc_copy_expr (al->expr);
    6794                 :         541 :               gfc_add_len_component (expr_ref_len);
    6795                 :         541 :               gfc_conv_expr (&se, expr_ref_len);
    6796                 :         541 :               al_len = se.expr;
    6797                 :         541 :               gfc_free_expr (expr_ref_len);
    6798                 :             :             }
    6799                 :             :           else
    6800                 :             :             /* In a loop ensure that all loop variable dependent variables
    6801                 :             :                are initialized at the same spot in all execution paths.  */
    6802                 :             :             al_len = NULL_TREE;
    6803                 :             :         }
    6804                 :             :       else
    6805                 :             :         al_vptr = al_len = NULL_TREE;
    6806                 :             : 
    6807                 :       14765 :       se.want_pointer = 1;
    6808                 :       14765 :       se.descriptor_only = 1;
    6809                 :             : 
    6810                 :       14765 :       gfc_conv_expr (&se, expr);
    6811                 :       14765 :       if (expr->ts.type == BT_CHARACTER && expr->ts.deferred)
    6812                 :             :         /* se.string_length now stores the .string_length variable of expr
    6813                 :             :            needed to allocate character(len=:) arrays.  */
    6814                 :         878 :         al_len = se.string_length;
    6815                 :             : 
    6816                 :       14765 :       al_len_needs_set = al_len != NULL_TREE;
    6817                 :             :       /* When allocating an array one cannot use much of the
    6818                 :             :          pre-evaluated expr3 expressions, because for most of them the
    6819                 :             :          scalarizer is needed which is not available in the pre-evaluation
    6820                 :             :          step.  Therefore gfc_array_allocate () is responsible (and able)
    6821                 :             :          to handle the complete array allocation.  Only the element size
    6822                 :             :          needs to be provided, which is done most of the time by the
    6823                 :             :          pre-evaluation step.  */
    6824                 :       14765 :       nelems = NULL_TREE;
    6825                 :       14765 :       if (expr3_len && (code->expr3->ts.type == BT_CHARACTER
    6826                 :         754 :                         || code->expr3->ts.type == BT_CLASS))
    6827                 :             :         {
    6828                 :             :           /* When al is an array, then the element size for each element
    6829                 :             :              in the array is needed, which is the product of the len and
    6830                 :             :              esize for char arrays.  For unlimited polymorphics len can be
    6831                 :             :              zero, therefore take the maximum of len and one.  */
    6832                 :        1508 :           tmp = fold_build2_loc (input_location, MAX_EXPR,
    6833                 :         754 :                                  TREE_TYPE (expr3_len),
    6834                 :         754 :                                  expr3_len, fold_convert (TREE_TYPE (expr3_len),
    6835                 :             :                                                           integer_one_node));
    6836                 :        1508 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    6837                 :         754 :                                  TREE_TYPE (expr3_esize), expr3_esize,
    6838                 :         754 :                                  fold_convert (TREE_TYPE (expr3_esize), tmp));
    6839                 :             :         }
    6840                 :             :       else
    6841                 :             :         tmp = expr3_esize;
    6842                 :             : 
    6843                 :       14765 :       gfc_omp_namelist *omp_alloc_item = NULL;
    6844                 :       14765 :       if (omp_allocate)
    6845                 :             :         {
    6846                 :             :           gfc_omp_namelist *n = NULL;
    6847                 :             :           gfc_omp_namelist *n_null = NULL;
    6848                 :         127 :           for (n = omp_allocate; n; n = n->next)
    6849                 :             :             {
    6850                 :          85 :               if (n->sym == NULL)
    6851                 :             :                 {
    6852                 :          41 :                   n_null = n;
    6853                 :          41 :                   continue;
    6854                 :             :                 }
    6855                 :          44 :               if (expr->expr_type == EXPR_VARIABLE
    6856                 :          44 :                   && expr->symtree->n.sym == n->sym)
    6857                 :             :                 {
    6858                 :          22 :                   gfc_ref *ref;
    6859                 :          32 :                   for (ref = expr->ref; ref; ref = ref->next)
    6860                 :          13 :                     if (ref->type == REF_COMPONENT)
    6861                 :             :                       break;
    6862                 :             :                   if (ref == NULL)
    6863                 :             :                     break;
    6864                 :             :                 }
    6865                 :             :             }
    6866                 :          61 :           omp_alloc_item = n ? n : n_null;
    6867                 :             : 
    6868                 :             :         }
    6869                 :             : 
    6870                 :       28456 :       if (!gfc_array_allocate (&se, expr, stat, errmsg, errlen,
    6871                 :             :                                label_finish, tmp, &nelems,
    6872                 :             :                                e3rhs ? e3rhs : code->expr3,
    6873                 :             :                                e3_is == E3_DESC ? expr3 : NULL_TREE,
    6874                 :             :                                e3_has_nodescriptor, omp_alloc_item))
    6875                 :             :         {
    6876                 :             :           /* A scalar or derived type.  First compute the size to
    6877                 :             :              allocate.
    6878                 :             : 
    6879                 :             :              expr3_len is set when expr3 is an unlimited polymorphic
    6880                 :             :              object or a deferred length string.  */
    6881                 :        4729 :           if (expr3_len != NULL_TREE)
    6882                 :             :             {
    6883                 :         450 :               tmp = fold_convert (TREE_TYPE (expr3_esize), expr3_len);
    6884                 :         450 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
    6885                 :         450 :                                      TREE_TYPE (expr3_esize),
    6886                 :             :                                       expr3_esize, tmp);
    6887                 :         450 :               if (code->expr3->ts.type != BT_CLASS)
    6888                 :             :                 /* expr3 is a deferred length string, i.e., we are
    6889                 :             :                    done.  */
    6890                 :             :                 memsz = tmp;
    6891                 :             :               else
    6892                 :             :                 {
    6893                 :             :                   /* For unlimited polymorphic enties build
    6894                 :             :                           (len > 0) ? element_size * len : element_size
    6895                 :             :                      to compute the number of bytes to allocate.
    6896                 :             :                      This allows the allocation of unlimited polymorphic
    6897                 :             :                      objects from an expr3 that is also unlimited
    6898                 :             :                      polymorphic and stores a _len dependent object,
    6899                 :             :                      e.g., a string.  */
    6900                 :          85 :                   memsz = fold_build2_loc (input_location, GT_EXPR,
    6901                 :             :                                            logical_type_node, expr3_len,
    6902                 :             :                                            build_zero_cst
    6903                 :          85 :                                            (TREE_TYPE (expr3_len)));
    6904                 :          85 :                   memsz = fold_build3_loc (input_location, COND_EXPR,
    6905                 :          85 :                                          TREE_TYPE (expr3_esize),
    6906                 :             :                                          memsz, tmp, expr3_esize);
    6907                 :             :                 }
    6908                 :             :             }
    6909                 :        4279 :           else if (expr3_esize != NULL_TREE)
    6910                 :             :             /* Any other object in expr3 just needs element size in
    6911                 :             :                bytes.  */
    6912                 :             :             memsz = expr3_esize;
    6913                 :        2672 :           else if ((expr->ts.type == BT_CHARACTER && expr->ts.deferred)
    6914                 :        2672 :                    || (upoly_expr
    6915                 :           0 :                        && code->ext.alloc.ts.type == BT_CHARACTER))
    6916                 :             :             {
    6917                 :             :               /* Allocating deferred length char arrays need the length
    6918                 :             :                  to allocate in the alloc_type_spec.  But also unlimited
    6919                 :             :                  polymorphic objects may be allocated as char arrays.
    6920                 :             :                  Both are handled here.  */
    6921                 :           0 :               gfc_init_se (&se_sz, NULL);
    6922                 :           0 :               gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
    6923                 :           0 :               gfc_add_block_to_block (&se.pre, &se_sz.pre);
    6924                 :           0 :               se_sz.expr = gfc_evaluate_now (se_sz.expr, &se.pre);
    6925                 :           0 :               gfc_add_block_to_block (&se.pre, &se_sz.post);
    6926                 :           0 :               expr3_len = se_sz.expr;
    6927                 :           0 :               tmp_expr3_len_flag = true;
    6928                 :           0 :               tmp = TYPE_SIZE_UNIT (
    6929                 :             :                     gfc_get_char_type (code->ext.alloc.ts.kind));
    6930                 :           0 :               memsz = fold_build2_loc (input_location, MULT_EXPR,
    6931                 :           0 :                                        TREE_TYPE (tmp),
    6932                 :           0 :                                        fold_convert (TREE_TYPE (tmp),
    6933                 :             :                                                      expr3_len),
    6934                 :             :                                        tmp);
    6935                 :             :             }
    6936                 :        2672 :           else if (expr->ts.type == BT_CHARACTER)
    6937                 :             :             {
    6938                 :             :               /* Compute the number of bytes needed to allocate a fixed
    6939                 :             :                  length char array.  */
    6940                 :         164 :               gcc_assert (se.string_length != NULL_TREE);
    6941                 :         164 :               tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind));
    6942                 :         328 :               memsz = fold_build2_loc (input_location, MULT_EXPR,
    6943                 :         164 :                                        TREE_TYPE (tmp), tmp,
    6944                 :         164 :                                        fold_convert (TREE_TYPE (tmp),
    6945                 :             :                                                      se.string_length));
    6946                 :             :             }
    6947                 :        2508 :           else if (code->ext.alloc.ts.type != BT_UNKNOWN)
    6948                 :             :             /* Handle all types, where the alloc_type_spec is set.  */
    6949                 :           0 :             memsz = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&code->ext.alloc.ts));
    6950                 :             :           else
    6951                 :             :             /* Handle size computation of the type declared to alloc.  */
    6952                 :        2508 :             memsz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (se.expr)));
    6953                 :             : 
    6954                 :        4729 :           bool use_coarray_alloc
    6955                 :        4729 :             = (flag_coarray == GFC_FCOARRAY_LIB
    6956                 :        4729 :                && (caf_attr = gfc_caf_attr (expr, true, &caf_refs_comp))
    6957                 :          80 :                                 .codimension);
    6958                 :        4729 :           tree omp_cond = NULL_TREE;
    6959                 :        4729 :           tree omp_alt_alloc = NULL_TREE;
    6960                 :        4729 :           tree succ_add_expr = NULL_TREE;
    6961                 :        4729 :           if (!use_coarray_alloc && omp_alloc_item)
    6962                 :             :             {
    6963                 :          28 :               tree align, alloc, sz;
    6964                 :          28 :               gfc_se se2;
    6965                 :             : 
    6966                 :          28 :               omp_cond = boolean_true_node;
    6967                 :          28 :               if (omp_alloc_item->u2.allocator)
    6968                 :             :                 {
    6969                 :           2 :                   gfc_init_se (&se2, NULL);
    6970                 :           2 :                   gfc_conv_expr (&se2, omp_alloc_item->u2.allocator);
    6971                 :           2 :                   gfc_add_block_to_block (&se.pre, &se2.pre);
    6972                 :           2 :                   alloc = gfc_evaluate_now (se2.expr, &se.pre);
    6973                 :           2 :                   gfc_add_block_to_block (&se.pre, &se2.post);
    6974                 :             :                 }
    6975                 :             :               else
    6976                 :          26 :                 alloc = build_zero_cst (ptr_type_node);
    6977                 :          28 :               tmp = TREE_TYPE (TREE_TYPE (se.expr));
    6978                 :          28 :               if (tmp == void_type_node)
    6979                 :           3 :                 tmp = gfc_typenode_for_spec (&expr->ts, 0);
    6980                 :          28 :               if (omp_alloc_item->u.align)
    6981                 :             :                 {
    6982                 :          14 :                   gfc_init_se (&se2, NULL);
    6983                 :          14 :                   gfc_conv_expr (&se2, omp_alloc_item->u.align);
    6984                 :          14 :                   gcc_assert (CONSTANT_CLASS_P (se2.expr)
    6985                 :             :                               && se2.pre.head == NULL
    6986                 :             :                               && se2.post.head == NULL);
    6987                 :          14 :                   align = build_int_cst (size_type_node,
    6988                 :          14 :                                          MAX (tree_to_uhwi (se2.expr),
    6989                 :             :                                          TYPE_ALIGN_UNIT (tmp)));
    6990                 :             :                 }
    6991                 :             :               else
    6992                 :          14 :                 align = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (tmp));
    6993                 :          28 :               sz = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
    6994                 :             :                             fold_convert (size_type_node, memsz),
    6995                 :          28 :                             build_int_cst (size_type_node, 1));
    6996                 :          28 :               omp_alt_alloc = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
    6997                 :          28 :               DECL_ATTRIBUTES (omp_alt_alloc)
    6998                 :          28 :                 = tree_cons (get_identifier ("omp allocator"),
    6999                 :             :                              build_tree_list (NULL_TREE, alloc),
    7000                 :          28 :                              DECL_ATTRIBUTES (omp_alt_alloc));
    7001                 :          28 :               omp_alt_alloc = build_call_expr (omp_alt_alloc, 3, align, sz, alloc);
    7002                 :          28 :               succ_add_expr = gfc_omp_call_add_alloc (se.expr);
    7003                 :             :             }
    7004                 :             : 
    7005                 :             :           /* Store the caf-attributes for latter use.  */
    7006                 :        4729 :           if (use_coarray_alloc)
    7007                 :             :             {
    7008                 :             :               /* Scalar allocatable components in coarray'ed derived types make
    7009                 :             :                  it here and are treated now.  */
    7010                 :          75 :               tree caf_decl, token;
    7011                 :          75 :               gfc_se caf_se;
    7012                 :             : 
    7013                 :          75 :               is_coarray = true;
    7014                 :             :               /* Set flag, to add synchronize after the allocate.  */
    7015                 :         150 :               needs_caf_sync = needs_caf_sync
    7016                 :          75 :                   || caf_attr.coarray_comp || !caf_refs_comp;
    7017                 :             : 
    7018                 :          75 :               gfc_init_se (&caf_se, NULL);
    7019                 :             : 
    7020                 :          75 :               caf_decl = gfc_get_tree_for_caf_expr (expr);
    7021                 :          75 :               gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl,
    7022                 :             :                                         NULL_TREE, NULL);
    7023                 :          75 :               gfc_add_block_to_block (&se.pre, &caf_se.pre);
    7024                 :          75 :               gfc_allocate_allocatable (&se.pre, se.expr, memsz,
    7025                 :             :                                         gfc_build_addr_expr (NULL_TREE, token),
    7026                 :             :                                         NULL_TREE, NULL_TREE, NULL_TREE,
    7027                 :             :                                         label_finish, expr, 1);
    7028                 :             :             }
    7029                 :             :           /* Allocate - for non-pointers with re-alloc checking.  */
    7030                 :        4654 :           else if (gfc_expr_attr (expr).allocatable)
    7031                 :        3160 :             gfc_allocate_allocatable (&se.pre, se.expr, memsz,
    7032                 :             :                                       NULL_TREE, stat, errmsg, errlen,
    7033                 :             :                                       label_finish, expr, 0,
    7034                 :             :                                       omp_cond, omp_alt_alloc, succ_add_expr);
    7035                 :             :           else
    7036                 :        1494 :             gfc_allocate_using_malloc (&se.pre, se.expr, memsz, stat,
    7037                 :             :                                       omp_cond, omp_alt_alloc, succ_add_expr);
    7038                 :             :         }
    7039                 :             :       else
    7040                 :             :         {
    7041                 :             :           /* Allocating coarrays needs a sync after the allocate executed.
    7042                 :             :              Set the flag to add the sync after all objects are allocated.  */
    7043                 :       10036 :           if (flag_coarray == GFC_FCOARRAY_LIB
    7044                 :       10036 :               && (caf_attr = gfc_caf_attr (expr, true, &caf_refs_comp))
    7045                 :         323 :                  .codimension)
    7046                 :             :             {
    7047                 :         284 :               is_coarray = true;
    7048                 :         284 :               needs_caf_sync = needs_caf_sync
    7049                 :         284 :                   || caf_attr.coarray_comp || !caf_refs_comp;
    7050                 :             :             }
    7051                 :             : 
    7052                 :       10036 :           if (expr->ts.type == BT_CHARACTER && al_len != NULL_TREE
    7053                 :         877 :               && expr3_len != NULL_TREE)
    7054                 :             :             {
    7055                 :             :               /* Arrays need to have a _len set before the array
    7056                 :             :                  descriptor is filled.  */
    7057                 :         222 :               gfc_add_modify (&block, al_len,
    7058                 :         222 :                               fold_convert (TREE_TYPE (al_len), expr3_len));
    7059                 :             :               /* Prevent setting the length twice.  */
    7060                 :         222 :               al_len_needs_set = false;
    7061                 :             :             }
    7062                 :        9814 :           else if (expr->ts.type == BT_CHARACTER && al_len != NULL_TREE
    7063                 :         198 :               && code->ext.alloc.ts.u.cl->length)
    7064                 :             :             {
    7065                 :             :               /* Cover the cases where a string length is explicitly
    7066                 :             :                  specified by a type spec for deferred length character
    7067                 :             :                  arrays or unlimited polymorphic objects without a
    7068                 :             :                  source= or mold= expression.  */
    7069                 :         198 :               gfc_init_se (&se_sz, NULL);
    7070                 :         198 :               gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
    7071                 :         198 :               gfc_add_block_to_block (&block, &se_sz.pre);
    7072                 :         198 :               gfc_add_modify (&block, al_len,
    7073                 :         198 :                               fold_convert (TREE_TYPE (al_len),
    7074                 :             :                                             se_sz.expr));
    7075                 :         198 :               al_len_needs_set = false;
    7076                 :             :             }
    7077                 :             :         }
    7078                 :             : 
    7079                 :       14765 :       gfc_add_block_to_block (&block, &se.pre);
    7080                 :             : 
    7081                 :             :       /* Error checking -- Note: ERRMSG only makes sense with STAT.  */
    7082                 :       14765 :       if (code->expr1)
    7083                 :             :         {
    7084                 :         279 :           tmp = build1_v (GOTO_EXPR, label_errmsg);
    7085                 :         279 :           parm = fold_build2_loc (input_location, NE_EXPR,
    7086                 :             :                                   logical_type_node, stat,
    7087                 :         279 :                                   build_int_cst (TREE_TYPE (stat), 0));
    7088                 :         279 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    7089                 :             :                                  gfc_unlikely (parm, PRED_FORTRAN_FAIL_ALLOC),
    7090                 :             :                                  tmp, build_empty_stmt (input_location));
    7091                 :         279 :           gfc_add_expr_to_block (&block, tmp);
    7092                 :             :         }
    7093                 :             : 
    7094                 :             :       /* Set the vptr only when no source= is set.  When source= is set, then
    7095                 :             :          the trans_assignment below will set the vptr.  */
    7096                 :       14765 :       if (al_vptr != NULL_TREE && (!code->expr3 || code->expr3->mold))
    7097                 :             :         {
    7098                 :        1614 :           if (expr3_vptr != NULL_TREE)
    7099                 :             :             /* The vtab is already known, so just assign it.  */
    7100                 :          71 :             gfc_add_modify (&block, al_vptr,
    7101                 :          71 :                             fold_convert (TREE_TYPE (al_vptr), expr3_vptr));
    7102                 :             :           else
    7103                 :             :             {
    7104                 :             :               /* VPTR is fixed at compile time.  */
    7105                 :        1543 :               gfc_symbol *vtab;
    7106                 :        1543 :               gfc_typespec *ts;
    7107                 :             : 
    7108                 :        1543 :               if (code->expr3)
    7109                 :             :                 /* Although expr3 is pre-evaluated above, it may happen,
    7110                 :             :                    that for arrays or in mold= cases the pre-evaluation
    7111                 :             :                    was not successful.  In these rare cases take the vtab
    7112                 :             :                    from the typespec of expr3 here.  */
    7113                 :           0 :                 ts = &code->expr3->ts;
    7114                 :        1543 :               else if (code->ext.alloc.ts.type == BT_DERIVED || upoly_expr)
    7115                 :             :                 /* The alloc_type_spec gives the type to allocate or the
    7116                 :             :                    al is unlimited polymorphic, which enforces the use of
    7117                 :             :                    an alloc_type_spec that is not necessarily a BT_DERIVED.  */
    7118                 :         682 :                 ts = &code->ext.alloc.ts;
    7119                 :             :               else
    7120                 :             :                 /* Prepare for setting the vtab as declared.  */
    7121                 :         861 :                 ts = &expr->ts;
    7122                 :             : 
    7123                 :        1543 :               vtab = gfc_find_vtab (ts);
    7124                 :        1543 :               gcc_assert (vtab);
    7125                 :        1543 :               tmp = gfc_build_addr_expr (NULL_TREE,
    7126                 :             :                                          gfc_get_symbol_decl (vtab));
    7127                 :        1543 :               gfc_add_modify (&block, al_vptr,
    7128                 :        1543 :                               fold_convert (TREE_TYPE (al_vptr), tmp));
    7129                 :             :             }
    7130                 :             :         }
    7131                 :             : 
    7132                 :             :       /* Add assignment for string length.  */
    7133                 :       14765 :       if (al_len != NULL_TREE && al_len_needs_set)
    7134                 :             :         {
    7135                 :         999 :           if (expr3_len != NULL_TREE)
    7136                 :             :             {
    7137                 :         507 :               gfc_add_modify (&block, al_len,
    7138                 :         507 :                               fold_convert (TREE_TYPE (al_len),
    7139                 :             :                                             expr3_len));
    7140                 :             :               /* When tmp_expr3_len_flag is set, then expr3_len is
    7141                 :             :                  abused to carry the length information from the
    7142                 :             :                  alloc_type.  Clear it to prevent setting incorrect len
    7143                 :             :                  information in future loop iterations.  */
    7144                 :         507 :               if (tmp_expr3_len_flag)
    7145                 :             :                 /* No need to reset tmp_expr3_len_flag, because the
    7146                 :             :                    presence of an expr3 cannot change within in the
    7147                 :             :                    loop.  */
    7148                 :         492 :                 expr3_len = NULL_TREE;
    7149                 :             :             }
    7150                 :         492 :           else if (code->ext.alloc.ts.type == BT_CHARACTER
    7151                 :         178 :               && code->ext.alloc.ts.u.cl->length)
    7152                 :             :             {
    7153                 :             :               /* Cover the cases where a string length is explicitly
    7154                 :             :                  specified by a type spec for deferred length character
    7155                 :             :                  arrays or unlimited polymorphic objects without a
    7156                 :             :                  source= or mold= expression.  */
    7157                 :         178 :               if (expr3_esize == NULL_TREE || code->ext.alloc.ts.kind != 1)
    7158                 :             :                 {
    7159                 :          66 :                   gfc_init_se (&se_sz, NULL);
    7160                 :          66 :                   gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
    7161                 :          66 :                   gfc_add_block_to_block (&block, &se_sz.pre);
    7162                 :          66 :                   gfc_add_modify (&block, al_len,
    7163                 :          66 :                                   fold_convert (TREE_TYPE (al_len),
    7164                 :             :                                                 se_sz.expr));
    7165                 :             :                 }
    7166                 :             :               else
    7167                 :         112 :                 gfc_add_modify (&block, al_len,
    7168                 :         112 :                                 fold_convert (TREE_TYPE (al_len),
    7169                 :             :                                               expr3_esize));
    7170                 :             :             }
    7171                 :             :           else
    7172                 :             :             /* No length information needed, because type to allocate
    7173                 :             :                has no length.  Set _len to 0.  */
    7174                 :         314 :             gfc_add_modify (&block, al_len,
    7175                 :         314 :                             fold_convert (TREE_TYPE (al_len),
    7176                 :             :                                           integer_zero_node));
    7177                 :             :         }
    7178                 :             : 
    7179                 :       14765 :       init_expr = NULL;
    7180                 :       14765 :       if (code->expr3 && !code->expr3->mold && e3_is != E3_MOLD)
    7181                 :             :         {
    7182                 :             :           /* Initialization via SOURCE block (or static default initializer).
    7183                 :             :              Switch off automatic reallocation since we have just done the
    7184                 :             :              ALLOCATE.  */
    7185                 :        3227 :           int realloc_lhs = flag_realloc_lhs;
    7186                 :        3227 :           gfc_expr *init_expr = gfc_expr_to_initialize (expr);
    7187                 :        3227 :           gfc_expr *rhs = e3rhs ? e3rhs : gfc_copy_expr (code->expr3);
    7188                 :        3227 :           flag_realloc_lhs = 0;
    7189                 :             : 
    7190                 :             :           /* Set the symbol to be artificial so that the result is not finalized.  */
    7191                 :        3227 :           init_expr->symtree->n.sym->attr.artificial = 1;
    7192                 :        3227 :           tmp = gfc_trans_assignment (init_expr, rhs, true, false, true,
    7193                 :             :                                       false);
    7194                 :        3227 :           init_expr->symtree->n.sym->attr.artificial = 0;
    7195                 :             : 
    7196                 :        3227 :           flag_realloc_lhs = realloc_lhs;
    7197                 :             :           /* Free the expression allocated for init_expr.  */
    7198                 :        3227 :           gfc_free_expr (init_expr);
    7199                 :        3227 :           if (rhs != e3rhs)
    7200                 :           0 :             gfc_free_expr (rhs);
    7201                 :        3227 :           gfc_add_expr_to_block (&block, tmp);
    7202                 :        3227 :         }
    7203                 :             :       /* Set KIND and LEN PDT components and allocate those that are
    7204                 :             :          parameterized.  */
    7205                 :       11538 :       else if (expr->ts.type == BT_DERIVED
    7206                 :        2993 :                && expr->ts.u.derived->attr.pdt_type)
    7207                 :             :         {
    7208                 :          64 :           if (code->expr3 && code->expr3->param_list)
    7209                 :             :             param_list = code->expr3->param_list;
    7210                 :          64 :           else if (expr->param_list)
    7211                 :             :             param_list = expr->param_list;
    7212                 :             :           else
    7213                 :          25 :             param_list = expr->symtree->n.sym->param_list;
    7214                 :          64 :           tmp = gfc_allocate_pdt_comp (expr->ts.u.derived, se.expr,
    7215                 :             :                                        expr->rank, param_list);
    7216                 :          64 :           gfc_add_expr_to_block (&block, tmp);
    7217                 :             :         }
    7218                 :             :       /* Ditto for CLASS expressions.  */
    7219                 :       11474 :       else if (expr->ts.type == BT_CLASS
    7220                 :         572 :                && CLASS_DATA (expr)->ts.u.derived->attr.pdt_type)
    7221                 :             :         {
    7222                 :           0 :           if (code->expr3 && code->expr3->param_list)
    7223                 :             :             param_list = code->expr3->param_list;
    7224                 :           0 :           else if (expr->param_list)
    7225                 :             :             param_list = expr->param_list;
    7226                 :             :           else
    7227                 :           0 :             param_list = expr->symtree->n.sym->param_list;
    7228                 :           0 :           tmp = gfc_allocate_pdt_comp (CLASS_DATA (expr)->ts.u.derived,
    7229                 :             :                                        se.expr, expr->rank, param_list);
    7230                 :           0 :           gfc_add_expr_to_block (&block, tmp);
    7231                 :             :         }
    7232                 :       11474 :       else if (code->expr3 && code->expr3->mold
    7233                 :         236 :                && code->expr3->ts.type == BT_CLASS)
    7234                 :             :         {
    7235                 :             :           /* Use class_init_assign to initialize expr.  */
    7236                 :          59 :           gfc_code *ini;
    7237                 :          59 :           ini = gfc_get_code (EXEC_INIT_ASSIGN);
    7238                 :          59 :           ini->expr1 = gfc_find_and_cut_at_last_class_ref (expr, true);
    7239                 :          59 :           tmp = gfc_trans_class_init_assign (ini);
    7240                 :          59 :           gfc_free_statements (ini);
    7241                 :          59 :           gfc_add_expr_to_block (&block, tmp);
    7242                 :          59 :         }
    7243                 :       11415 :       else if ((init_expr = allocate_get_initializer (code, expr)))
    7244                 :             :         {
    7245                 :             :           /* Use class_init_assign to initialize expr.  */
    7246                 :        1672 :           gfc_code *ini;
    7247                 :        1672 :           int realloc_lhs = flag_realloc_lhs;
    7248                 :        1672 :           ini = gfc_get_code (EXEC_INIT_ASSIGN);
    7249                 :        1672 :           ini->expr1 = gfc_expr_to_initialize (expr);
    7250                 :        1672 :           ini->expr2 = init_expr;
    7251                 :        1672 :           flag_realloc_lhs = 0;
    7252                 :        1672 :           tmp= gfc_trans_init_assign (ini);
    7253                 :        1672 :           flag_realloc_lhs = realloc_lhs;
    7254                 :        1672 :           gfc_free_statements (ini);
    7255                 :             :           /* Init_expr is freeed by above free_statements, just need to null
    7256                 :             :              it here.  */
    7257                 :        1672 :           init_expr = NULL;
    7258                 :        1672 :           gfc_add_expr_to_block (&block, tmp);
    7259                 :             :         }
    7260                 :             : 
    7261                 :             :       /* Nullify all pointers in derived type coarrays.  This registers a
    7262                 :             :          token for them which allows their allocation.  */
    7263                 :       14765 :       if (is_coarray)
    7264                 :             :         {
    7265                 :         398 :           gfc_symbol *type = NULL;
    7266                 :         398 :           symbol_attribute caf_attr;
    7267                 :         398 :           int rank = 0;
    7268                 :         398 :           if (code->ext.alloc.ts.type == BT_DERIVED
    7269                 :           8 :               && code->ext.alloc.ts.u.derived->attr.pointer_comp)
    7270                 :             :             {
    7271                 :           0 :               type = code->ext.alloc.ts.u.derived;
    7272                 :           0 :               rank = type->attr.dimension ? type->as->rank : 0;
    7273                 :           0 :               gfc_clear_attr (&caf_attr);
    7274                 :             :             }
    7275                 :         398 :           else if (expr->ts.type == BT_DERIVED
    7276                 :         102 :                    && expr->ts.u.derived->attr.pointer_comp)
    7277                 :             :             {
    7278                 :          13 :               type = expr->ts.u.derived;
    7279                 :          13 :               rank = expr->rank;
    7280                 :          13 :               caf_attr = gfc_caf_attr (expr, true);
    7281                 :             :             }
    7282                 :             : 
    7283                 :             :           /* Initialize the tokens of pointer components in derived type
    7284                 :             :              coarrays.  */
    7285                 :          13 :           if (type)
    7286                 :             :             {
    7287                 :          26 :               tmp = (caf_attr.codimension && !caf_attr.dimension)
    7288                 :          13 :                   ? gfc_conv_descriptor_data_get (se.expr) : se.expr;
    7289                 :          13 :               tmp = gfc_nullify_alloc_comp (type, tmp, rank,
    7290                 :             :                                             GFC_STRUCTURE_CAF_MODE_IN_COARRAY);
    7291                 :          13 :               gfc_add_expr_to_block (&block, tmp);
    7292                 :             :             }
    7293                 :             :         }
    7294                 :             : 
    7295                 :       14765 :       gfc_free_expr (expr);
    7296                 :             :     } // for-loop
    7297                 :             : 
    7298                 :       12179 :   if (e3rhs)
    7299                 :             :     {
    7300                 :        3331 :       if (newsym)
    7301                 :             :         {
    7302                 :        1854 :           gfc_free_symbol (newsym->n.sym);
    7303                 :        1854 :           XDELETE (newsym);
    7304                 :             :         }
    7305                 :        3331 :       gfc_free_expr (e3rhs);
    7306                 :             :     }
    7307                 :             :   /* STAT.  */
    7308                 :       12179 :   if (code->expr1)
    7309                 :             :     {
    7310                 :         267 :       tmp = build1_v (LABEL_EXPR, label_errmsg);
    7311                 :         267 :       gfc_add_expr_to_block (&block, tmp);
    7312                 :             :     }
    7313                 :             : 
    7314                 :             :   /* ERRMSG - only useful if STAT is present.  */
    7315                 :       12179 :   if (code->expr1 && code->expr2)
    7316                 :             :     {
    7317                 :          68 :       const char *msg = "Attempt to allocate an allocated object";
    7318                 :          68 :       const char *oommsg = "Insufficient virtual memory";
    7319                 :          68 :       tree slen, dlen, errmsg_str, oom_str, oom_loc;
    7320                 :          68 :       stmtblock_t errmsg_block;
    7321                 :             : 
    7322                 :          68 :       gfc_init_block (&errmsg_block);
    7323                 :             : 
    7324                 :          68 :       errmsg_str = gfc_create_var (pchar_type_node, "ERRMSG");
    7325                 :          68 :       gfc_add_modify (&errmsg_block, errmsg_str,
    7326                 :             :                 gfc_build_addr_expr (pchar_type_node,
    7327                 :             :                         gfc_build_localized_cstring_const (msg)));
    7328                 :             : 
    7329                 :          68 :       slen = build_int_cst (gfc_charlen_type_node, strlen (msg));
    7330                 :          68 :       dlen = gfc_get_expr_charlen (code->expr2);
    7331                 :          68 :       slen = fold_build2_loc (input_location, MIN_EXPR,
    7332                 :          68 :                               TREE_TYPE (slen), dlen, slen);
    7333                 :             : 
    7334                 :          68 :       gfc_trans_string_copy (&errmsg_block, dlen, errmsg,
    7335                 :          68 :                              code->expr2->ts.kind,
    7336                 :             :                              slen, errmsg_str,
    7337                 :             :                              gfc_default_character_kind);
    7338                 :          68 :       dlen = gfc_finish_block (&errmsg_block);
    7339                 :             : 
    7340                 :          68 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7341                 :          68 :                              stat, build_int_cst (TREE_TYPE (stat),
    7342                 :          68 :                                                   LIBERROR_ALLOCATION));
    7343                 :             : 
    7344                 :          68 :       tmp = build3_v (COND_EXPR, tmp,
    7345                 :             :                       dlen, build_empty_stmt (input_location));
    7346                 :             : 
    7347                 :          68 :       gfc_add_expr_to_block (&block, tmp);
    7348                 :             : 
    7349                 :          68 :       oom_str = gfc_create_var (pchar_type_node, "OOMMSG");
    7350                 :          68 :       oom_loc = gfc_build_localized_cstring_const (oommsg);
    7351                 :          68 :       gfc_add_modify (&errmsg_block, oom_str,
    7352                 :             :                       gfc_build_addr_expr (pchar_type_node, oom_loc));
    7353                 :             : 
    7354                 :          68 :       slen = build_int_cst (gfc_charlen_type_node, strlen (oommsg));
    7355                 :          68 :       dlen = gfc_get_expr_charlen (code->expr2);
    7356                 :          68 :       slen = fold_build2_loc (input_location, MIN_EXPR,
    7357                 :          68 :                               TREE_TYPE (slen), dlen, slen);
    7358                 :             : 
    7359                 :          68 :       gfc_trans_string_copy (&errmsg_block, dlen, errmsg,
    7360                 :          68 :                              code->expr2->ts.kind,
    7361                 :             :                              slen, oom_str,
    7362                 :             :                              gfc_default_character_kind);
    7363                 :          68 :       dlen = gfc_finish_block (&errmsg_block);
    7364                 :             : 
    7365                 :          68 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7366                 :          68 :                              stat, build_int_cst (TREE_TYPE (stat),
    7367                 :          68 :                                                   LIBERROR_NO_MEMORY));
    7368                 :             : 
    7369                 :          68 :       tmp = build3_v (COND_EXPR, tmp,
    7370                 :             :                       dlen, build_empty_stmt (input_location));
    7371                 :             : 
    7372                 :          68 :       gfc_add_expr_to_block (&block, tmp);
    7373                 :             :     }
    7374                 :             : 
    7375                 :             :   /* STAT block.  */
    7376                 :       12179 :   if (code->expr1)
    7377                 :             :     {
    7378                 :         267 :       if (TREE_USED (label_finish))
    7379                 :             :         {
    7380                 :          11 :           tmp = build1_v (LABEL_EXPR, label_finish);
    7381                 :          11 :           gfc_add_expr_to_block (&block, tmp);
    7382                 :             :         }
    7383                 :             : 
    7384                 :         267 :       gfc_init_se (&se, NULL);
    7385                 :         267 :       gfc_conv_expr_lhs (&se, code->expr1);
    7386                 :         267 :       tmp = convert (TREE_TYPE (se.expr), stat);
    7387                 :         267 :       gfc_add_modify (&block, se.expr, tmp);
    7388                 :             :     }
    7389                 :             : 
    7390                 :       12179 :   if (needs_caf_sync)
    7391                 :             :     {
    7392                 :             :       /* Add a sync all after the allocation has been executed.  */
    7393                 :         135 :       tree zero_size = build_zero_cst (size_type_node);
    7394                 :         135 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
    7395                 :             :                                  3, null_pointer_node, null_pointer_node,
    7396                 :             :                                  zero_size);
    7397                 :         135 :       gfc_add_expr_to_block (&post, tmp);
    7398                 :             :     }
    7399                 :             : 
    7400                 :       12179 :   gfc_add_block_to_block (&block, &se.post);
    7401                 :       12179 :   gfc_add_block_to_block (&block, &post);
    7402                 :       12179 :   if (code->expr3 && code->expr3->must_finalize)
    7403                 :          61 :     gfc_add_block_to_block (&block, &final_block);
    7404                 :             : 
    7405                 :       12179 :   return gfc_finish_block (&block);
    7406                 :             : }
    7407                 :             : 
    7408                 :             : 
    7409                 :             : /* Translate a DEALLOCATE statement.  */
    7410                 :             : 
    7411                 :             : tree
    7412                 :        7321 : gfc_trans_deallocate (gfc_code *code)
    7413                 :             : {
    7414                 :        7321 :   gfc_se se;
    7415                 :        7321 :   gfc_alloc *al;
    7416                 :        7321 :   tree apstat, pstat, stat, errmsg, errlen, tmp;
    7417                 :        7321 :   tree label_finish, label_errmsg;
    7418                 :        7321 :   stmtblock_t block;
    7419                 :             : 
    7420                 :        7321 :   pstat = apstat = stat = errmsg = errlen = tmp = NULL_TREE;
    7421                 :        7321 :   label_finish = label_errmsg = NULL_TREE;
    7422                 :             : 
    7423                 :        7321 :   gfc_start_block (&block);
    7424                 :             : 
    7425                 :             :   /* Count the number of failed deallocations.  If deallocate() was
    7426                 :             :      called with STAT= , then set STAT to the count.  If deallocate
    7427                 :             :      was called with ERRMSG, then set ERRMG to a string.  */
    7428                 :        7321 :   if (code->expr1)
    7429                 :             :     {
    7430                 :        1963 :       tree gfc_int4_type_node = gfc_get_int_type (4);
    7431                 :             : 
    7432                 :        1963 :       stat = gfc_create_var (gfc_int4_type_node, "stat");
    7433                 :        1963 :       pstat = gfc_build_addr_expr (NULL_TREE, stat);
    7434                 :             : 
    7435                 :             :       /* GOTO destinations.  */
    7436                 :        1963 :       label_errmsg = gfc_build_label_decl (NULL_TREE);
    7437                 :        1963 :       label_finish = gfc_build_label_decl (NULL_TREE);
    7438                 :        1963 :       TREE_USED (label_finish) = 0;
    7439                 :             :     }
    7440                 :             : 
    7441                 :             :   /* Set ERRMSG - only needed if STAT is available.  */
    7442                 :        7321 :   if (code->expr1 && code->expr2)
    7443                 :             :     {
    7444                 :          51 :       gfc_init_se (&se, NULL);
    7445                 :          51 :       se.want_pointer = 1;
    7446                 :          51 :       gfc_conv_expr_lhs (&se, code->expr2);
    7447                 :          51 :       errmsg = se.expr;
    7448                 :          51 :       errlen = se.string_length;
    7449                 :             :     }
    7450                 :             : 
    7451                 :       16457 :   for (al = code->ext.alloc.list; al != NULL; al = al->next)
    7452                 :             :     {
    7453                 :        9136 :       gfc_expr *expr = gfc_copy_expr (al->expr);
    7454                 :        9136 :       bool is_coarray = false, is_coarray_array = false;
    7455                 :        9136 :       int caf_mode = 0;
    7456                 :             : 
    7457                 :        9136 :       gcc_assert (expr->expr_type == EXPR_VARIABLE);
    7458                 :             : 
    7459                 :        9136 :       if (expr->ts.type == BT_CLASS)
    7460                 :        1614 :         gfc_add_data_component (expr);
    7461                 :             : 
    7462                 :        9136 :       gfc_init_se (&se, NULL);
    7463                 :        9136 :       gfc_start_block (&se.pre);
    7464                 :             : 
    7465                 :        9136 :       se.want_pointer = 1;
    7466                 :        9136 :       se.descriptor_only = 1;
    7467                 :        9136 :       gfc_conv_expr (&se, expr);
    7468                 :             : 
    7469                 :             :       /* Deallocate PDT components that are parameterized.  */
    7470                 :        9136 :       tmp = NULL;
    7471                 :        9136 :       if (expr->ts.type == BT_DERIVED
    7472                 :        2148 :           && expr->ts.u.derived->attr.pdt_type
    7473                 :          59 :           && expr->symtree->n.sym->param_list)
    7474                 :          39 :         tmp = gfc_deallocate_pdt_comp (expr->ts.u.derived, se.expr, expr->rank);
    7475                 :        9097 :       else if (expr->ts.type == BT_CLASS
    7476                 :         859 :                && CLASS_DATA (expr)->ts.u.derived->attr.pdt_type
    7477                 :           0 :                && expr->symtree->n.sym->param_list)
    7478                 :           0 :         tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr)->ts.u.derived,
    7479                 :             :                                        se.expr, expr->rank);
    7480                 :             : 
    7481                 :          39 :       if (tmp)
    7482                 :          39 :         gfc_add_expr_to_block (&block, tmp);
    7483                 :             : 
    7484                 :        9136 :       if (flag_coarray == GFC_FCOARRAY_LIB
    7485                 :        9136 :           || flag_coarray == GFC_FCOARRAY_SINGLE)
    7486                 :             :         {
    7487                 :         391 :           bool comp_ref;
    7488                 :         391 :           symbol_attribute caf_attr = gfc_caf_attr (expr, false, &comp_ref);
    7489                 :         391 :           if (caf_attr.codimension)
    7490                 :             :             {
    7491                 :         280 :               is_coarray = true;
    7492                 :         138 :               is_coarray_array = caf_attr.dimension || !comp_ref
    7493                 :         340 :                   || caf_attr.coarray_comp;
    7494                 :             : 
    7495                 :         280 :               if (flag_coarray == GFC_FCOARRAY_LIB)
    7496                 :             :                 /* When the expression to deallocate is referencing a
    7497                 :             :                    component, then only deallocate it, but do not
    7498                 :             :                    deregister.  */
    7499                 :         181 :                 caf_mode = GFC_STRUCTURE_CAF_MODE_IN_COARRAY
    7500                 :         181 :                     | (comp_ref && !caf_attr.coarray_comp
    7501                 :             :                        ? GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY : 0);
    7502                 :             :             }
    7503                 :             :         }
    7504                 :             : 
    7505                 :        9136 :       if (expr->rank || is_coarray_array)
    7506                 :             :         {
    7507                 :        6242 :           gfc_ref *ref;
    7508                 :             : 
    7509                 :        5392 :           if (gfc_bt_struct (expr->ts.type)
    7510                 :         850 :               && expr->ts.u.derived->attr.alloc_comp
    7511                 :        6624 :               && !gfc_is_finalizable (expr->ts.u.derived, NULL))
    7512                 :             :             {
    7513                 :         379 :               gfc_ref *last = NULL;
    7514                 :             : 
    7515                 :        1054 :               for (ref = expr->ref; ref; ref = ref->next)
    7516                 :         675 :                 if (ref->type == REF_COMPONENT)
    7517                 :         250 :                   last = ref;
    7518                 :             : 
    7519                 :             :               /* Do not deallocate the components of a derived type
    7520                 :             :                  ultimate pointer component.  */
    7521                 :         379 :               if (!(last && last->u.c.component->attr.pointer)
    7522                 :         152 :                     && !(!last && expr->symtree->n.sym->attr.pointer))
    7523                 :             :                 {
    7524                 :          28 :                   if (is_coarray && expr->rank == 0
    7525                 :          22 :                       && (!last || !last->u.c.component->attr.dimension)
    7526                 :         383 :                       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)))
    7527                 :             :                     {
    7528                 :             :                       /* Add the ref to the data member only, when this is not
    7529                 :             :                          a regular array or deallocate_alloc_comp will try to
    7530                 :             :                          add another one.  */
    7531                 :          22 :                       tmp = gfc_conv_descriptor_data_get (se.expr);
    7532                 :             :                     }
    7533                 :             :                   else
    7534                 :         339 :                     tmp = se.expr;
    7535                 :         361 :                   tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp,
    7536                 :             :                                                    expr->rank, caf_mode);
    7537                 :         361 :                   gfc_add_expr_to_block (&se.pre, tmp);
    7538                 :             :                 }
    7539                 :             :             }
    7540                 :             : 
    7541                 :        6242 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)))
    7542                 :             :             {
    7543                 :        6203 :               gfc_coarray_deregtype caf_dtype;
    7544                 :             : 
    7545                 :        6203 :               if (is_coarray)
    7546                 :         232 :                 caf_dtype = gfc_caf_is_dealloc_only (caf_mode)
    7547                 :         232 :                     ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
    7548                 :             :                     : GFC_CAF_COARRAY_DEREGISTER;
    7549                 :             :               else
    7550                 :             :                 caf_dtype = GFC_CAF_COARRAY_NOCOARRAY;
    7551                 :        6203 :               tmp = gfc_deallocate_with_status (se.expr, pstat, errmsg, errlen,
    7552                 :             :                                                 label_finish, false, expr,
    7553                 :             :                                                 caf_dtype);
    7554                 :        6203 :               gfc_add_expr_to_block (&se.pre, tmp);
    7555                 :             :             }
    7556                 :          39 :           else if (TREE_CODE (se.expr) == COMPONENT_REF
    7557                 :          39 :                    && TREE_CODE (TREE_TYPE (se.expr)) == ARRAY_TYPE
    7558                 :          78 :                    && TREE_CODE (TREE_TYPE (TREE_TYPE (se.expr)))
    7559                 :             :                         == RECORD_TYPE)
    7560                 :             :             {
    7561                 :             :               /* class.cc(finalize_component) generates these, when a
    7562                 :             :                  finalizable entity has a non-allocatable derived type array
    7563                 :             :                  component, which has allocatable components. Obtain the
    7564                 :             :                  derived type of the array and deallocate the allocatable
    7565                 :             :                  components. */
    7566                 :          45 :               for (ref = expr->ref; ref; ref = ref->next)
    7567                 :             :                 {
    7568                 :          45 :                   if (ref->u.c.component->attr.dimension
    7569                 :          39 :                       && ref->u.c.component->ts.type == BT_DERIVED)
    7570                 :             :                     break;
    7571                 :             :                 }
    7572                 :             : 
    7573                 :          39 :               if (ref && ref->u.c.component->ts.u.derived->attr.alloc_comp
    7574                 :          78 :                   && !gfc_is_finalizable (ref->u.c.component->ts.u.derived,
    7575                 :             :                                           NULL))
    7576                 :             :                 {
    7577                 :          39 :                   tmp = gfc_deallocate_alloc_comp
    7578                 :          39 :                                 (ref->u.c.component->ts.u.derived,
    7579                 :             :                                  se.expr, expr->rank);
    7580                 :          39 :                   gfc_add_expr_to_block (&se.pre, tmp);
    7581                 :             :                 }
    7582                 :             :             }
    7583                 :             : 
    7584                 :        6242 :           if (al->expr->ts.type == BT_CLASS)
    7585                 :             :             {
    7586                 :         868 :               gfc_reset_vptr (&se.pre, al->expr);
    7587                 :         868 :               if (UNLIMITED_POLY (al->expr)
    7588                 :         629 :                   || (al->expr->ts.type == BT_DERIVED
    7589                 :           0 :                       && al->expr->ts.u.derived->attr.unlimited_polymorphic))
    7590                 :             :                 /* Clear _len, too.  */
    7591                 :         239 :                 gfc_reset_len (&se.pre, al->expr);
    7592                 :             :             }
    7593                 :             :         }
    7594                 :             :       else
    7595                 :             :         {
    7596                 :        5788 :           tmp = gfc_deallocate_scalar_with_status (se.expr, pstat, label_finish,
    7597                 :             :                                                    false, al->expr,
    7598                 :        2894 :                                                    al->expr->ts, NULL_TREE,
    7599                 :             :                                                    is_coarray);
    7600                 :        2894 :           gfc_add_expr_to_block (&se.pre, tmp);
    7601                 :             : 
    7602                 :             :           /* Set to zero after deallocation.  */
    7603                 :        2894 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    7604                 :             :                                  se.expr,
    7605                 :        2894 :                                  build_int_cst (TREE_TYPE (se.expr), 0));
    7606                 :        2894 :           gfc_add_expr_to_block (&se.pre, tmp);
    7607                 :             : 
    7608                 :        2894 :           if (al->expr->ts.type == BT_CLASS)
    7609                 :             :             {
    7610                 :         746 :               gfc_reset_vptr (&se.pre, al->expr);
    7611                 :         746 :               if (UNLIMITED_POLY (al->expr)
    7612                 :         568 :                   || (al->expr->ts.type == BT_DERIVED
    7613                 :           0 :                       && al->expr->ts.u.derived->attr.unlimited_polymorphic))
    7614                 :             :                 /* Clear _len, too.  */
    7615                 :         178 :                 gfc_reset_len (&se.pre, al->expr);
    7616                 :             :             }
    7617                 :             :         }
    7618                 :             : 
    7619                 :        9136 :       if (code->expr1)
    7620                 :             :         {
    7621                 :        2002 :           tree cond;
    7622                 :             : 
    7623                 :        2002 :           cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, stat,
    7624                 :        2002 :                                   build_int_cst (TREE_TYPE (stat), 0));
    7625                 :        2002 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    7626                 :             :                                  gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC),
    7627                 :             :                                  build1_v (GOTO_EXPR, label_errmsg),
    7628                 :             :                                  build_empty_stmt (input_location));
    7629                 :        2002 :           gfc_add_expr_to_block (&se.pre, tmp);
    7630                 :             :         }
    7631                 :             : 
    7632                 :        9136 :       tmp = gfc_finish_block (&se.pre);
    7633                 :        9136 :       gfc_add_expr_to_block (&block, tmp);
    7634                 :        9136 :       gfc_free_expr (expr);
    7635                 :             :     }
    7636                 :             : 
    7637                 :        7321 :   if (code->expr1)
    7638                 :             :     {
    7639                 :        1963 :       tmp = build1_v (LABEL_EXPR, label_errmsg);
    7640                 :        1963 :       gfc_add_expr_to_block (&block, tmp);
    7641                 :             :     }
    7642                 :             : 
    7643                 :             :   /* Set ERRMSG - only needed if STAT is available.  */
    7644                 :        7321 :   if (code->expr1 && code->expr2)
    7645                 :             :     {
    7646                 :          51 :       const char *msg = "Attempt to deallocate an unallocated object";
    7647                 :          51 :       stmtblock_t errmsg_block;
    7648                 :          51 :       tree errmsg_str, slen, dlen, cond;
    7649                 :             : 
    7650                 :          51 :       gfc_init_block (&errmsg_block);
    7651                 :             : 
    7652                 :          51 :       errmsg_str = gfc_create_var (pchar_type_node, "ERRMSG");
    7653                 :          51 :       gfc_add_modify (&errmsg_block, errmsg_str,
    7654                 :             :                 gfc_build_addr_expr (pchar_type_node,
    7655                 :             :                         gfc_build_localized_cstring_const (msg)));
    7656                 :          51 :       slen = build_int_cst (gfc_charlen_type_node, strlen (msg));
    7657                 :          51 :       dlen = gfc_get_expr_charlen (code->expr2);
    7658                 :             : 
    7659                 :          51 :       gfc_trans_string_copy (&errmsg_block, dlen, errmsg, code->expr2->ts.kind,
    7660                 :             :                              slen, errmsg_str, gfc_default_character_kind);
    7661                 :          51 :       tmp = gfc_finish_block (&errmsg_block);
    7662                 :             : 
    7663                 :          51 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, stat,
    7664                 :          51 :                              build_int_cst (TREE_TYPE (stat), 0));
    7665                 :          51 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    7666                 :             :                              gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC), tmp,
    7667                 :             :                              build_empty_stmt (input_location));
    7668                 :             : 
    7669                 :          51 :       gfc_add_expr_to_block (&block, tmp);
    7670                 :             :     }
    7671                 :             : 
    7672                 :        7321 :   if (code->expr1 && TREE_USED (label_finish))
    7673                 :             :     {
    7674                 :           7 :       tmp = build1_v (LABEL_EXPR, label_finish);
    7675                 :           7 :       gfc_add_expr_to_block (&block, tmp);
    7676                 :             :     }
    7677                 :             : 
    7678                 :             :   /* Set STAT.  */
    7679                 :        7321 :   if (code->expr1)
    7680                 :             :     {
    7681                 :        1963 :       gfc_init_se (&se, NULL);
    7682                 :        1963 :       gfc_conv_expr_lhs (&se, code->expr1);
    7683                 :        1963 :       tmp = convert (TREE_TYPE (se.expr), stat);
    7684                 :        1963 :       gfc_add_modify (&block, se.expr, tmp);
    7685                 :             :     }
    7686                 :             : 
    7687                 :        7321 :   return gfc_finish_block (&block);
    7688                 :             : }
    7689                 :             : 
    7690                 :             : #include "gt-fortran-trans-stmt.h"
        

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