LCOV - code coverage report
Current view: top level - gcc/fortran - trans-openmp.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.0 % 6434 6175
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 123 123
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* OpenMP directive translation -- generate GCC trees from gfc_code.
       2              :    Copyright (C) 2005-2026 Free Software Foundation, Inc.
       3              :    Contributed by Jakub Jelinek <jakub@redhat.com>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : 
      22              : #include "config.h"
      23              : #include "system.h"
      24              : #include "coretypes.h"
      25              : #include "options.h"
      26              : #include "tree.h"
      27              : #include "gfortran.h"
      28              : #include "basic-block.h"
      29              : #include "tree-ssa.h"
      30              : #include "tree-ssa-loop-niter.h"  /* for simplify_replace_tree.  */
      31              : #include "function.h"
      32              : #include "gimple.h"
      33              : #include "gimple-expr.h"
      34              : #include "trans.h"
      35              : #include "stringpool.h"
      36              : #include "fold-const.h"
      37              : #include "gimplify.h" /* For create_tmp_var_raw.  */
      38              : #include "trans-stmt.h"
      39              : #include "trans-types.h"
      40              : #include "trans-array.h"
      41              : #include "trans-const.h"
      42              : #include "trans-descriptor.h"
      43              : #include "arith.h"
      44              : #include "constructor.h"
      45              : #include "gomp-constants.h"
      46              : #include "omp-general.h"
      47              : #include "omp-low.h"
      48              : #include "memmodel.h"  /* For MEMMODEL_ enums.  */
      49              : #include "dependency.h"
      50              : #include "gimple-iterator.h" /* For gsi_iterator_update.  */
      51              : #include "gimplify-me.h"  /* For force_gimple_operand.  */
      52              : 
      53              : #undef GCC_DIAG_STYLE
      54              : #define GCC_DIAG_STYLE __gcc_tdiag__
      55              : #include "diagnostic-core.h"
      56              : #undef GCC_DIAG_STYLE
      57              : #define GCC_DIAG_STYLE __gcc_gfc__
      58              : #include "attribs.h"
      59              : #include "function.h"
      60              : 
      61              : int ompws_flags;
      62              : 
      63              : /* True if OpenMP should regard this DECL as being a scalar which has Fortran's
      64              :    allocatable or pointer attribute.  */
      65              : 
      66              : bool
      67         6546 : gfc_omp_is_allocatable_or_ptr (const_tree decl)
      68              : {
      69         6546 :   return (DECL_P (decl)
      70         6546 :           && (GFC_DECL_GET_SCALAR_POINTER (decl)
      71         4815 :               || GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)));
      72              : }
      73              : 
      74              : /* True if the argument is an optional argument; except that false is also
      75              :    returned for arguments with the value attribute (nonpointers) and for
      76              :    assumed-shape variables (decl is a local variable containing arg->data).
      77              :    Note that for 'procedure(), optional' the value false is used as that's
      78              :    always a pointer and no additional indirection is used.
      79              :    Note that pvoid_type_node is for 'type(c_ptr), value' (and c_funloc).  */
      80              : 
      81              : static bool
      82        50148 : gfc_omp_is_optional_argument (const_tree decl)
      83              : {
      84              :   /* Note: VAR_DECL can occur with BIND(C) and array descriptors.  */
      85        33214 :   return ((TREE_CODE (decl) == PARM_DECL || VAR_P (decl))
      86        50148 :           && DECL_LANG_SPECIFIC (decl)
      87        22767 :           && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
      88        22583 :           && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))
      89        22348 :           && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != FUNCTION_TYPE
      90        72471 :           && GFC_DECL_OPTIONAL_ARGUMENT (decl));
      91              : }
      92              : 
      93              : /* Check whether this DECL belongs to a Fortran optional argument.
      94              :    With 'for_present_check' set to false, decls which are optional parameters
      95              :    themselves are returned as tree - or a NULL_TREE otherwise. Those decls are
      96              :    always pointers.  With 'for_present_check' set to true, the decl for checking
      97              :    whether an argument is present is returned; for arguments with value
      98              :    attribute this is the hidden argument and of BOOLEAN_TYPE.  If the decl is
      99              :    unrelated to optional arguments, NULL_TREE is returned.  */
     100              : 
     101              : tree
     102        23054 : gfc_omp_check_optional_argument (tree decl, bool for_present_check)
     103              : {
     104        23054 :   if (!for_present_check)
     105         2369 :     return gfc_omp_is_optional_argument (decl) ? decl : NULL_TREE;
     106              : 
     107        20685 :   if (!DECL_LANG_SPECIFIC (decl))
     108              :     return NULL_TREE;
     109              : 
     110         5418 :   tree orig_decl = decl;
     111              : 
     112              :   /* For assumed-shape arrays, a local decl with arg->data is used.  */
     113         5418 :   if (TREE_CODE (decl) != PARM_DECL
     114         5418 :       && (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
     115         2022 :           || GFC_ARRAY_TYPE_P (TREE_TYPE (decl))))
     116          809 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     117              : 
     118              :   /* Note: With BIND(C), array descriptors are converted to a VAR_DECL.  */
     119         5418 :   if (decl == NULL_TREE
     120         5284 :       || (TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != VAR_DECL)
     121         5284 :       || !DECL_LANG_SPECIFIC (decl)
     122        10230 :       || !GFC_DECL_OPTIONAL_ARGUMENT (decl))
     123              :     return NULL_TREE;
     124              : 
     125              :    /* Scalars with VALUE attribute which are passed by value use a hidden
     126              :       argument to denote the present status.  They are passed as nonpointer type
     127              :       with one exception: 'type(c_ptr), value' as 'void*'.  */
     128              :    /* Cf. trans-expr.cc's gfc_conv_expr_present.  */
     129         2829 :    if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
     130         2829 :        || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
     131              :     {
     132          211 :       char name[GFC_MAX_SYMBOL_LEN + 2];
     133          211 :       tree tree_name;
     134              : 
     135          211 :       name[0] = '.';
     136          211 :       strcpy (&name[1], IDENTIFIER_POINTER (DECL_NAME (decl)));
     137          211 :       tree_name = get_identifier (name);
     138              : 
     139              :       /* Walk function argument list to find the hidden arg.  */
     140          211 :       decl = DECL_ARGUMENTS (DECL_CONTEXT (decl));
     141         1473 :       for ( ; decl != NULL_TREE; decl = TREE_CHAIN (decl))
     142         1473 :         if (DECL_NAME (decl) == tree_name
     143         1473 :             && DECL_ARTIFICIAL (decl))
     144              :           break;
     145              : 
     146          211 :       gcc_assert (decl);
     147          211 :       return decl;
     148              :     }
     149              : 
     150         2618 :   return fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
     151         2618 :                           orig_decl, null_pointer_node);
     152              : }
     153              : 
     154              : 
     155              : /* Returns tree with NULL if it is not an array descriptor and with the tree to
     156              :    access the 'data' component otherwise.  With type_only = true, it returns the
     157              :    TREE_TYPE without creating a new tree.  */
     158              : 
     159              : tree
     160        20500 : gfc_omp_array_data (tree decl, bool type_only)
     161              : {
     162        20500 :   tree type = TREE_TYPE (decl);
     163              : 
     164        20500 :   if (POINTER_TYPE_P (type))
     165        10356 :     type = TREE_TYPE (type);
     166              : 
     167        20500 :   if (!GFC_DESCRIPTOR_TYPE_P (type))
     168              :     return NULL_TREE;
     169              : 
     170         4660 :   if (type_only)
     171         3432 :     return GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
     172              : 
     173         1228 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     174          422 :     decl = build_fold_indirect_ref (decl);
     175              : 
     176         1228 :   decl = gfc_conv_descriptor_data_get (decl);
     177         1228 :   STRIP_NOPS (decl);
     178         1228 :   return decl;
     179              : }
     180              : 
     181              : /* Returns true if DECL is an array for which the actual array data has to be
     182              :    privatized; the caller must ensure that DECL is an array descriptor,
     183              :    i.e. 'omp_array_data' returns true.  */
     184              : 
     185              : bool
     186          126 : gfc_omp_array_data_privatize (tree decl)
     187              : {
     188          126 :   tree type = TREE_TYPE (decl);
     189              : 
     190          126 :   if (POINTER_TYPE_P (type))
     191           15 :     type = TREE_TYPE (type);
     192              : 
     193          126 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
     194              : 
     195          126 :   return (GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_POINTER
     196          126 :           && GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_POINTER_CONT);
     197              : }
     198              : 
     199              : /* Return the byte-size of the passed array descriptor. */
     200              : 
     201              : tree
     202           26 : gfc_omp_array_size (tree decl, gimple_seq *pre_p)
     203              : {
     204           26 :   stmtblock_t block;
     205           26 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     206           26 :     decl = build_fold_indirect_ref (decl);
     207           26 :   tree type = TREE_TYPE (decl);
     208           26 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
     209           26 :   bool allocatable = (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
     210            0 :                       || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
     211           26 :                       || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT);
     212           26 :   gfc_init_block (&block);
     213           78 :   tree size = gfc_full_array_size (&block, decl,
     214           26 :                                    GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)));
     215           26 :   size = fold_convert (size_type_node, size);
     216           26 :   tree elemsz = gfc_get_element_type (TREE_TYPE (decl));
     217           26 :   if (TREE_CODE (elemsz) == ARRAY_TYPE && TYPE_STRING_FLAG (elemsz))
     218            6 :     elemsz = gfc_conv_descriptor_elem_len_get (decl);
     219              :   else
     220           20 :     elemsz = TYPE_SIZE_UNIT (elemsz);
     221           26 :   size = fold_build2 (MULT_EXPR, size_type_node, size, elemsz);
     222           26 :   if (!allocatable)
     223            0 :     gimplify_and_add (gfc_finish_block (&block), pre_p);
     224              :   else
     225              :     {
     226           26 :       tree var = create_tmp_var (size_type_node);
     227           26 :       gfc_add_expr_to_block (&block, build2 (MODIFY_EXPR, sizetype, var, size));
     228           26 :       tree tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
     229              :                                   gfc_conv_descriptor_data_get (decl),
     230              :                                   null_pointer_node);
     231           26 :       tmp = build3_loc (input_location, COND_EXPR, void_type_node, tmp,
     232              :                         gfc_finish_block (&block),
     233              :                         build2 (MODIFY_EXPR, sizetype, var, size_zero_node));
     234           26 :       gimplify_and_add (tmp, pre_p);
     235           26 :       size = var;
     236              :     }
     237           26 :   return size;
     238              : }
     239              : 
     240              : 
     241              : /* True if OpenMP should privatize what this DECL points to rather
     242              :    than the DECL itself.  */
     243              : 
     244              : bool
     245       514915 : gfc_omp_privatize_by_reference (const_tree decl)
     246              : {
     247       514915 :   tree type = TREE_TYPE (decl);
     248              : 
     249       514915 :   if (TREE_CODE (type) == REFERENCE_TYPE
     250       514915 :       && (!DECL_ARTIFICIAL (decl) || TREE_CODE (decl) == PARM_DECL))
     251              :     return true;
     252              : 
     253       489455 :   if (TREE_CODE (type) == POINTER_TYPE
     254       489455 :       && gfc_omp_is_optional_argument (decl))
     255              :     return true;
     256              : 
     257       479906 :   if (TREE_CODE (type) == POINTER_TYPE)
     258              :     {
     259        36014 :       while (TREE_CODE (decl) == COMPONENT_REF)
     260            0 :         decl = TREE_OPERAND (decl, 1);
     261              : 
     262              :       /* Array POINTER/ALLOCATABLE have aggregate types, all user variables
     263              :          that have POINTER_TYPE type and aren't scalar pointers, scalar
     264              :          allocatables, Cray pointees or C pointers are supposed to be
     265              :          privatized by reference.  */
     266        36014 :       if (GFC_DECL_GET_SCALAR_POINTER (decl)
     267        34273 :           || GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
     268        31205 :           || GFC_DECL_CRAY_POINTEE (decl)
     269        31199 :           || GFC_DECL_ASSOCIATE_VAR_P (decl)
     270        42015 :           || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
     271              :         return false;
     272              : 
     273        22917 :       if (!DECL_ARTIFICIAL (decl)
     274        22917 :           && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
     275              :         return true;
     276              : 
     277              :       /* Some arrays are expanded as DECL_ARTIFICIAL pointers
     278              :          by the frontend.  */
     279        14507 :       if (DECL_LANG_SPECIFIC (decl)
     280        14507 :           && GFC_DECL_SAVED_DESCRIPTOR (decl))
     281         5567 :         return true;
     282              :     }
     283              : 
     284              :   return false;
     285              : }
     286              : 
     287              : /* OMP_CLAUSE_DEFAULT_UNSPECIFIED unless OpenMP sharing attribute
     288              :    of DECL is predetermined.  */
     289              : 
     290              : enum omp_clause_default_kind
     291         8938 : gfc_omp_predetermined_sharing (tree decl)
     292              : {
     293              :   /* Associate names preserve the association established during ASSOCIATE.
     294              :      As they are implemented either as pointers to the selector or array
     295              :      descriptor and shouldn't really change in the ASSOCIATE region,
     296              :      this decl can be either shared or firstprivate.  If it is a pointer,
     297              :      use firstprivate, as it is cheaper that way, otherwise make it shared.  */
     298         8938 :   if (GFC_DECL_ASSOCIATE_VAR_P (decl))
     299              :     {
     300           45 :       if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
     301              :         return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
     302              :       else
     303           18 :         return OMP_CLAUSE_DEFAULT_SHARED;
     304              :     }
     305              : 
     306         8893 :   if (DECL_ARTIFICIAL (decl)
     307         1611 :       && ! GFC_DECL_RESULT (decl)
     308        10480 :       && ! (DECL_LANG_SPECIFIC (decl)
     309          399 :             && GFC_DECL_SAVED_DESCRIPTOR (decl)))
     310              :     return OMP_CLAUSE_DEFAULT_SHARED;
     311              : 
     312              :   /* Cray pointees shouldn't be listed in any clauses and should be
     313              :      gimplified to dereference of the corresponding Cray pointer.
     314              :      Make them all private, so that they are emitted in the debug
     315              :      information.  */
     316         7632 :   if (GFC_DECL_CRAY_POINTEE (decl))
     317              :     return OMP_CLAUSE_DEFAULT_PRIVATE;
     318              : 
     319              :   /* Assumed-size arrays are predetermined shared.  */
     320         7596 :   if (TREE_CODE (decl) == PARM_DECL
     321         2080 :       && GFC_ARRAY_TYPE_P (TREE_TYPE (decl))
     322          735 :       && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (decl)) == GFC_ARRAY_UNKNOWN
     323         8331 :       && GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (decl),
     324              :                                 GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)) - 1)
     325              :          == NULL)
     326              :     return OMP_CLAUSE_DEFAULT_SHARED;
     327              : 
     328              :   /* Dummy procedures aren't considered variables by OpenMP, thus are
     329              :      disallowed in OpenMP clauses.  They are represented as PARM_DECLs
     330              :      in the middle-end, so return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE here
     331              :      to avoid complaining about their uses with default(none).  */
     332         7528 :   if (TREE_CODE (decl) == PARM_DECL
     333         2012 :       && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
     334         8332 :       && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == FUNCTION_TYPE)
     335              :     return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
     336              : 
     337              :   /* COMMON and EQUIVALENCE decls are shared.  They
     338              :      are only referenced through DECL_VALUE_EXPR of the variables
     339              :      contained in them.  If those are privatized, they will not be
     340              :      gimplified to the COMMON or EQUIVALENCE decls.  */
     341         7512 :   if (GFC_DECL_COMMON_OR_EQUIV (decl) && ! DECL_HAS_VALUE_EXPR_P (decl))
     342              :     return OMP_CLAUSE_DEFAULT_SHARED;
     343              : 
     344         7483 :   if (GFC_DECL_RESULT (decl) && ! DECL_HAS_VALUE_EXPR_P (decl))
     345              :     return OMP_CLAUSE_DEFAULT_SHARED;
     346              : 
     347              :   /* These are either array or derived parameters, or vtables.
     348              :      In the former cases, the OpenMP standard doesn't consider them to be
     349              :      variables at all (they can't be redefined), but they can nevertheless appear
     350              :      in parallel/task regions and for default(none) purposes treat them as shared.
     351              :      For vtables likely the same handling is desirable.  */
     352         5463 :   if (VAR_P (decl) && TREE_READONLY (decl)
     353         7462 :       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
     354            3 :     return OMP_CLAUSE_DEFAULT_SHARED;
     355              : 
     356              :   return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
     357              : }
     358              : 
     359              : 
     360              : /* OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED unless OpenMP mapping attribute
     361              :    of DECL is predetermined.  */
     362              : 
     363              : enum omp_clause_defaultmap_kind
     364         4308 : gfc_omp_predetermined_mapping (tree decl)
     365              : {
     366         4308 :   if (DECL_ARTIFICIAL (decl)
     367          954 :       && ! GFC_DECL_RESULT (decl)
     368         5256 :       && ! (DECL_LANG_SPECIFIC (decl)
     369           79 :             && GFC_DECL_SAVED_DESCRIPTOR (decl)))
     370              :     return OMP_CLAUSE_DEFAULTMAP_TO;
     371              : 
     372              :   /* Dummy procedures aren't considered variables by OpenMP, thus are
     373              :      disallowed in OpenMP clauses.  They are represented as PARM_DECLs
     374              :      in the middle-end, so return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE here
     375              :      to avoid complaining about their uses with defaultmap(none).  */
     376         3409 :   if (TREE_CODE (decl) == PARM_DECL
     377         1878 :       && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
     378         3814 :       && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == FUNCTION_TYPE)
     379              :     return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
     380              : 
     381              :   /* These are either array or derived parameters, or vtables.  */
     382         1531 :   if (VAR_P (decl) && TREE_READONLY (decl)
     383         3402 :       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
     384            0 :     return OMP_CLAUSE_DEFAULTMAP_TO;
     385              : 
     386              :   return OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
     387              : }
     388              : 
     389              : 
     390              : /* Return decl that should be used when reporting DEFAULT(NONE)
     391              :    diagnostics.  */
     392              : 
     393              : tree
     394          128 : gfc_omp_report_decl (tree decl)
     395              : {
     396          128 :   if (DECL_ARTIFICIAL (decl)
     397            3 :       && DECL_LANG_SPECIFIC (decl)
     398          131 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     399            3 :     return GFC_DECL_SAVED_DESCRIPTOR (decl);
     400              : 
     401              :   return decl;
     402              : }
     403              : 
     404              : /* Return true if TYPE has any allocatable components;
     405              :    if ptr_ok, the decl itself is permitted to have the POINTER attribute.
     406              :    if shallow_alloc_only, returns only true if any of the fields is an
     407              :    allocatable; called with true by gfc_omp_replace_alloc_by_to_mapping.  */
     408              : 
     409              : static bool
     410       161203 : gfc_has_alloc_comps (tree type, tree decl, bool ptr_ok,
     411              :                      bool shallow_alloc_only=false)
     412              : {
     413       161203 :   tree field, ftype;
     414              : 
     415       161203 :   if (POINTER_TYPE_P (type))
     416              :     {
     417         4237 :       if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
     418         4237 :           || (ptr_ok && GFC_DECL_GET_SCALAR_POINTER (decl)))
     419         2837 :         type = TREE_TYPE (type);
     420         1400 :       else if (GFC_DECL_GET_SCALAR_POINTER (decl))
     421              :         return false;
     422              :     }
     423              : 
     424       160930 :   if (!ptr_ok
     425       134939 :       && GFC_DESCRIPTOR_TYPE_P (type)
     426       164320 :       && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
     427         2967 :           || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT))
     428              :     return false;
     429              : 
     430       160502 :   if (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type))
     431        14306 :     type = gfc_get_element_type (type);
     432              : 
     433       160502 :   if (TREE_CODE (type) != RECORD_TYPE)
     434              :     return false;
     435              : 
     436        11161 :   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
     437              :     {
     438         9726 :       ftype = TREE_TYPE (field);
     439         9726 :       if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
     440              :         return true;
     441         9158 :       if (GFC_DESCRIPTOR_TYPE_P (ftype)
     442         9158 :           && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
     443              :         return true;
     444         6676 :       if (!shallow_alloc_only
     445         6676 :           && gfc_has_alloc_comps (ftype, field, false))
     446              :         return true;
     447              :     }
     448              :   return false;
     449              : }
     450              : 
     451              : /* gfc_omp_replace_alloc_by_to_mapping is used with gfc_omp_deep_mapping... to
     452              :    handle the following:
     453              : 
     454              :    For map(alloc: dt), the array descriptors of allocatable components should
     455              :    be mapped as 'to'; this could be done by (A) adding 'map(to: dt%alloc_comp)'
     456              :    for each component (and avoiding to increment the reference count).
     457              :    Or (B) by just mapping all of 'dt' as 'to'.
     458              : 
     459              :    If 'dt' contains several allocatable components and not much other data,
     460              :    (A) is more efficient. If 'dt' contains a large const-size array, (A) will
     461              :    copy it to the device instead of only 'alloc'ating it.
     462              : 
     463              :    IMPLEMENTATION CHOICE: We do (A). It avoids the ref-count issue and it is
     464              :    expected that, for real-world code, derived types with allocatable
     465              :    components only have few other components and either no const-size arrays.
     466              :    This copying is done irrespectively whether the allocatables are allocated.
     467              : 
     468              :    If users wanted to save memory, they have to use 'map(alloc:dt%comp)' as
     469              :    also with 'map(alloc:dt)' all components get copied.
     470              : 
     471              :    For the copy to the device, only allocatable arrays are relevant as their
     472              :    the bounds are required; the pointer is set separately (GOMP_MAP_ATTACH)
     473              :    and the only setting required for scalars. However, when later copying out
     474              :    of the device, an unallocated allocatable must remain unallocated/NULL on
     475              :    the host; to achieve this we also must have it set to NULL on the device
     476              :    to avoid issues with uninitialized memory being copied back for the pointer
     477              :    address. If we could set the pointer to NULL, gfc_has_alloc_comps's
     478              :    shallow_alloc_only could be restricted to return true only for arrays.
     479              : 
     480              :    We only need to return true if there are allocatable-array components. */
     481              : 
     482              : static bool
     483           62 : gfc_omp_replace_alloc_by_to_mapping (tree type, tree decl, bool ptr_ok)
     484              : {
     485           18 :   return gfc_has_alloc_comps (type, decl, ptr_ok, true);
     486              : }
     487              : 
     488              : 
     489              : static bool
     490        70279 : gfc_is_polymorphic_nonptr (tree type)
     491              : {
     492        70279 :   if (POINTER_TYPE_P (type))
     493         3824 :     type = TREE_TYPE (type);
     494        70279 :   return GFC_CLASS_TYPE_P (type);
     495              : }
     496              : 
     497              : /* If TYPE is a class container, return 'p', 'a' or 't' for a POINTER,
     498              :    ALLOCATABLE or other entity (cf. gfc_build_class_symbol), else 0.  */
     499              : 
     500              : static char
     501        81294 : gfc_class_container_kind (tree type)
     502              : {
     503        81294 :   tree name;
     504        81294 :   const char *s;
     505              : 
     506        81294 :   if (POINTER_TYPE_P (type))
     507         6014 :     type = TREE_TYPE (type);
     508              : 
     509        81294 :   if (!GFC_CLASS_TYPE_P (type))
     510              :     return 0;
     511              : 
     512          164 :   name = TYPE_NAME (type);
     513          164 :   if (name && TREE_CODE (name) == TYPE_DECL)
     514            0 :     name = DECL_NAME (name);
     515            0 :   if (!name)
     516              :     return 0;
     517              : 
     518          164 :   s = IDENTIFIER_POINTER (name);
     519          164 :   return startswith (s, "__class_") ? s[strlen (s) - 1] : 0;
     520              : }
     521              : 
     522              : /* Return true if TYPE is unlimited polymorphic but not with pointer attribute;
     523              :    unlimited means also intrinsic types are handled and _len is used.  */
     524              : 
     525              : static bool
     526           77 : gfc_is_unlimited_polymorphic_nonptr (tree type)
     527              : {
     528           77 :   if (POINTER_TYPE_P (type))
     529            0 :     type = TREE_TYPE (type);
     530           77 :   if (!GFC_CLASS_TYPE_P (type))
     531              :     return false;
     532              : 
     533           77 :   tree field = TYPE_FIELDS (type); /* _data */
     534           77 :   gcc_assert (field);
     535           77 :   field = DECL_CHAIN (field); /* _vptr */
     536           77 :   gcc_assert (field);
     537           77 :   field = DECL_CHAIN (field);
     538           77 :   if (!field)
     539              :     return false;
     540           26 :   gcc_assert (strcmp ("_len", IDENTIFIER_POINTER (DECL_NAME (field))) == 0);
     541              :   return true;
     542              : }
     543              : 
     544              : /* Return true if the DECL is for an allocatable array or scalar.  */
     545              : 
     546              : bool
     547         4308 : gfc_omp_allocatable_p (tree decl)
     548              : {
     549         4308 :   if (!DECL_P (decl))
     550              :     return false;
     551              : 
     552         4308 :   if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl))
     553              :     return true;
     554              : 
     555         4080 :   tree type = TREE_TYPE (decl);
     556         4080 :   if (gfc_omp_privatize_by_reference (decl))
     557         1850 :     type = TREE_TYPE (type);
     558              : 
     559         4080 :   if (GFC_DESCRIPTOR_TYPE_P (type)
     560         4080 :       && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
     561          276 :           || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE))
     562              :     return true;
     563              : 
     564         3953 :   if (gfc_class_container_kind (type) == 'a')
     565            4 :     return true;
     566              : 
     567              :   return false;
     568              : }
     569              : 
     570              : 
     571              : /* Return true if the DECL is for a pointer array or scalar.  */
     572              : 
     573              : bool
     574         1026 : gfc_omp_pointer_p (tree decl)
     575              : {
     576         1026 :   if (!DECL_P (decl))
     577              :     return false;
     578              : 
     579         1026 :   if (GFC_DECL_GET_SCALAR_POINTER (decl))
     580              :     return true;
     581              : 
     582          861 :   tree type = TREE_TYPE (decl);
     583          861 :   if (gfc_omp_privatize_by_reference (decl))
     584          531 :     type = TREE_TYPE (type);
     585              : 
     586          861 :   if (GFC_DESCRIPTOR_TYPE_P (type)
     587          861 :       && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
     588           90 :           || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT
     589           90 :           || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_POINTER
     590           90 :           || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT))
     591              :     return true;
     592              : 
     593          675 :   if (gfc_class_container_kind (type) == 'p')
     594            0 :     return true;
     595              : 
     596              :   return false;
     597              : }
     598              : 
     599              : 
     600              : /* Return true if DECL in private clause needs
     601              :    OMP_CLAUSE_PRIVATE_OUTER_REF on the private clause.  */
     602              : bool
     603        46822 : gfc_omp_private_outer_ref (tree decl)
     604              : {
     605        46822 :   tree type = TREE_TYPE (decl);
     606              : 
     607        46822 :   if (gfc_omp_privatize_by_reference (decl))
     608         1433 :     type = TREE_TYPE (type);
     609              : 
     610        46822 :   if (GFC_DESCRIPTOR_TYPE_P (type)
     611        46822 :       && GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
     612              :     return true;
     613              : 
     614        46691 :   if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl))
     615              :     return true;
     616              : 
     617        46514 :   if (gfc_has_alloc_comps (type, decl, false))
     618              :     return true;
     619              : 
     620              :   return false;
     621              : }
     622              : 
     623              : /* Return true if CLAUSE privatizes a decl that gfc_omp_private_outer_ref
     624              :    accepts, but without an outer reference: gimplify adds such a clause for
     625              :    an addressable decl local to a SIMD body, which the body itself
     626              :    initializes and finalizes.  */
     627              : 
     628              : static bool
     629        55294 : gfc_omp_body_local_private_p (tree clause)
     630              : {
     631        55294 :   return (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_PRIVATE
     632        31945 :           && !OMP_CLAUSE_PRIVATE_OUTER_REF (clause)
     633        86913 :           && gfc_omp_private_outer_ref (OMP_CLAUSE_DECL (clause)));
     634              : }
     635              : 
     636              : /* Callback for gfc_omp_unshare_expr.  */
     637              : 
     638              : static tree
     639        93359 : gfc_omp_unshare_expr_r (tree *tp, int *walk_subtrees, void *)
     640              : {
     641        93359 :   tree t = *tp;
     642        93359 :   enum tree_code code = TREE_CODE (t);
     643              : 
     644              :   /* Stop at types, decls, constants like copy_tree_r.  */
     645        93359 :   if (TREE_CODE_CLASS (code) == tcc_type
     646              :       || TREE_CODE_CLASS (code) == tcc_declaration
     647        93359 :       || TREE_CODE_CLASS (code) == tcc_constant
     648        61821 :       || code == BLOCK)
     649        31538 :     *walk_subtrees = 0;
     650        61821 :   else if (handled_component_p (t)
     651        46813 :            || TREE_CODE (t) == MEM_REF)
     652              :     {
     653        15084 :       *tp = unshare_expr (t);
     654        15084 :       *walk_subtrees = 0;
     655              :     }
     656              : 
     657        93359 :   return NULL_TREE;
     658              : }
     659              : 
     660              : /* Unshare in expr anything that the FE which normally doesn't
     661              :    care much about tree sharing (because during gimplification
     662              :    everything is unshared) could cause problems with tree sharing
     663              :    at omp-low.cc time.  */
     664              : 
     665              : static tree
     666         5223 : gfc_omp_unshare_expr (tree expr)
     667              : {
     668         5223 :   walk_tree (&expr, gfc_omp_unshare_expr_r, NULL, NULL);
     669         5223 :   return expr;
     670              : }
     671              : 
     672              : enum walk_alloc_comps
     673              : {
     674              :   WALK_ALLOC_COMPS_DTOR,
     675              :   WALK_ALLOC_COMPS_DEFAULT_CTOR,
     676              :   WALK_ALLOC_COMPS_COPY_CTOR
     677              : };
     678              : 
     679              : /* Handle allocatable components in OpenMP clauses.  */
     680              : 
     681              : static tree
     682         2803 : gfc_walk_alloc_comps (tree decl, tree dest, tree var,
     683              :                       enum walk_alloc_comps kind)
     684              : {
     685         2803 :   stmtblock_t block, tmpblock;
     686         2803 :   tree type = TREE_TYPE (decl), then_b, tem, field;
     687         2803 :   gfc_init_block (&block);
     688              : 
     689         2803 :   if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
     690              :     {
     691         1092 :       if (GFC_DESCRIPTOR_TYPE_P (type))
     692              :         {
     693          548 :           gfc_init_block (&tmpblock);
     694         1644 :           tem = gfc_full_array_size (&tmpblock, decl,
     695          548 :                                      GFC_TYPE_ARRAY_RANK (type));
     696          548 :           then_b = gfc_finish_block (&tmpblock);
     697          548 :           gfc_add_expr_to_block (&block, gfc_omp_unshare_expr (then_b));
     698          548 :           tem = gfc_omp_unshare_expr (tem);
     699          548 :           tem = fold_build2_loc (input_location, MINUS_EXPR,
     700              :                                  gfc_array_index_type, tem,
     701              :                                  gfc_index_one_node);
     702              :         }
     703              :       else
     704              :         {
     705          544 :           bool compute_nelts = false;
     706          544 :           if (!TYPE_DOMAIN (type)
     707          544 :               || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE
     708          544 :               || TYPE_MIN_VALUE (TYPE_DOMAIN (type)) == error_mark_node
     709         1088 :               || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == error_mark_node)
     710              :             compute_nelts = true;
     711          544 :           else if (VAR_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
     712              :             {
     713           80 :               tree a = DECL_ATTRIBUTES (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
     714           80 :               if (lookup_attribute ("omp dummy var", a))
     715              :                 compute_nelts = true;
     716              :             }
     717              :           if (compute_nelts)
     718              :             {
     719           80 :               tem = fold_build2 (EXACT_DIV_EXPR, sizetype,
     720              :                                  TYPE_SIZE_UNIT (type),
     721              :                                  TYPE_SIZE_UNIT (TREE_TYPE (type)));
     722           80 :               tem = size_binop (MINUS_EXPR, tem, size_one_node);
     723              :             }
     724              :           else
     725          464 :             tem = array_type_nelts_minus_one (type);
     726          544 :           tem = fold_convert (gfc_array_index_type, tem);
     727              :         }
     728              : 
     729         1092 :       tree nelems = gfc_evaluate_now (tem, &block);
     730         1092 :       tree index = gfc_create_var (gfc_array_index_type, "S");
     731              : 
     732         1092 :       gfc_init_block (&tmpblock);
     733         1092 :       tem = gfc_conv_array_data (decl);
     734         1092 :       tree declvar = build_fold_indirect_ref_loc (input_location, tem);
     735         1092 :       tree declvref = gfc_build_array_ref (declvar, index, NULL);
     736         1092 :       tree destvar, destvref = NULL_TREE;
     737         1092 :       if (dest)
     738              :         {
     739          546 :           tem = gfc_conv_array_data (dest);
     740          546 :           destvar = build_fold_indirect_ref_loc (input_location, tem);
     741          546 :           destvref = gfc_build_array_ref (destvar, index, NULL);
     742              :         }
     743         1092 :       gfc_add_expr_to_block (&tmpblock,
     744              :                              gfc_walk_alloc_comps (declvref, destvref,
     745              :                                                    var, kind));
     746              : 
     747         1092 :       gfc_loopinfo loop;
     748         1092 :       gfc_init_loopinfo (&loop);
     749         1092 :       loop.dimen = 1;
     750         1092 :       loop.from[0] = gfc_index_zero_node;
     751         1092 :       loop.loopvar[0] = index;
     752         1092 :       loop.to[0] = nelems;
     753         1092 :       gfc_trans_scalarizing_loops (&loop, &tmpblock);
     754         1092 :       gfc_add_block_to_block (&block, &loop.pre);
     755         1092 :       return gfc_finish_block (&block);
     756              :     }
     757         1711 :   else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (var))
     758              :     {
     759          536 :       decl = build_fold_indirect_ref_loc (input_location, decl);
     760          536 :       if (dest)
     761          268 :         dest = build_fold_indirect_ref_loc (input_location, dest);
     762          536 :       type = TREE_TYPE (decl);
     763              :     }
     764              : 
     765         1711 :   gcc_assert (TREE_CODE (type) == RECORD_TYPE);
     766        11494 :   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
     767              :     {
     768         9783 :       tree ftype = TREE_TYPE (field);
     769         9783 :       tree declf, destf = NULL_TREE;
     770         9783 :       bool has_alloc_comps = gfc_has_alloc_comps (ftype, field, false);
     771         9783 :       if ((!GFC_DESCRIPTOR_TYPE_P (ftype)
     772         1710 :            || GFC_TYPE_ARRAY_AKIND (ftype) != GFC_ARRAY_ALLOCATABLE)
     773         8073 :           && !GFC_DECL_GET_SCALAR_ALLOCATABLE (field)
     774        16159 :           && !has_alloc_comps)
     775         5952 :         continue;
     776         3831 :       declf = fold_build3_loc (input_location, COMPONENT_REF, ftype,
     777              :                                decl, field, NULL_TREE);
     778         3831 :       if (dest)
     779         1916 :         destf = fold_build3_loc (input_location, COMPONENT_REF, ftype,
     780              :                                  dest, field, NULL_TREE);
     781              : 
     782         3831 :       tem = NULL_TREE;
     783         3831 :       switch (kind)
     784              :         {
     785              :         case WALK_ALLOC_COMPS_DTOR:
     786              :           break;
     787          962 :         case WALK_ALLOC_COMPS_DEFAULT_CTOR:
     788          962 :           if (GFC_DESCRIPTOR_TYPE_P (ftype)
     789          962 :               && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
     790              :             {
     791          431 :               gfc_add_modify (&block, unshare_expr (destf),
     792              :                               unshare_expr (declf));
     793          431 :               tem = gfc_duplicate_allocatable_nocopy
     794          431 :                                         (destf, declf, ftype,
     795          431 :                                          GFC_TYPE_ARRAY_RANK (ftype));
     796              :             }
     797          531 :           else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
     798          425 :             tem = gfc_duplicate_allocatable_nocopy (destf, declf, ftype, 0);
     799              :           break;
     800          954 :         case WALK_ALLOC_COMPS_COPY_CTOR:
     801          954 :           if (GFC_DESCRIPTOR_TYPE_P (ftype)
     802          954 :               && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
     803          848 :             tem = gfc_duplicate_allocatable (destf, declf, ftype,
     804          424 :                                              GFC_TYPE_ARRAY_RANK (ftype),
     805              :                                              NULL_TREE);
     806          530 :           else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
     807          424 :             tem = gfc_duplicate_allocatable (destf, declf, ftype, 0,
     808              :                                              NULL_TREE);
     809              :           break;
     810              :         }
     811         1704 :       if (tem)
     812         1704 :         gfc_add_expr_to_block (&block, gfc_omp_unshare_expr (tem));
     813         3831 :       if (has_alloc_comps)
     814              :         {
     815         1272 :           gfc_init_block (&tmpblock);
     816         1272 :           gfc_add_expr_to_block (&tmpblock,
     817              :                                  gfc_walk_alloc_comps (declf, destf,
     818              :                                                        field, kind));
     819         1272 :           then_b = gfc_finish_block (&tmpblock);
     820         1272 :           if (GFC_DESCRIPTOR_TYPE_P (ftype)
     821         1272 :               && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
     822          424 :             tem = gfc_conv_descriptor_data_get (unshare_expr (declf));
     823          848 :           else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
     824          424 :             tem = unshare_expr (declf);
     825              :           else
     826              :             tem = NULL_TREE;
     827          848 :           if (tem)
     828              :             {
     829          848 :               tem = fold_convert (pvoid_type_node, tem);
     830          848 :               tem = fold_build2_loc (input_location, NE_EXPR,
     831              :                                      logical_type_node, tem,
     832              :                                      null_pointer_node);
     833          848 :               then_b = build3_loc (input_location, COND_EXPR, void_type_node,
     834              :                                    tem, then_b,
     835              :                                    build_empty_stmt (input_location));
     836              :             }
     837         1272 :           gfc_add_expr_to_block (&block, then_b);
     838              :         }
     839         3831 :       if (kind == WALK_ALLOC_COMPS_DTOR)
     840              :         {
     841         1915 :           if (GFC_DESCRIPTOR_TYPE_P (ftype)
     842         1915 :               && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
     843              :             {
     844          855 :               tem = gfc_conv_descriptor_data_get (unshare_expr (declf));
     845          855 :               tem = gfc_deallocate_with_status (tem, NULL_TREE, NULL_TREE,
     846              :                                                 NULL_TREE, NULL_TREE, true,
     847              :                                                 NULL,
     848              :                                                 GFC_CAF_COARRAY_NOCOARRAY);
     849          855 :               gfc_add_expr_to_block (&block, gfc_omp_unshare_expr (tem));
     850              :             }
     851         1060 :           else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
     852              :             {
     853          848 :               tem = gfc_call_free (unshare_expr (declf));
     854          848 :               gfc_add_expr_to_block (&block, gfc_omp_unshare_expr (tem));
     855              :             }
     856              :         }
     857              :     }
     858              : 
     859         1711 :   return gfc_finish_block (&block);
     860              : }
     861              : 
     862              : /* Return code to initialize DECL with its default constructor, or
     863              :    NULL if there's nothing to do.  */
     864              : 
     865              : tree
     866        21654 : gfc_omp_clause_default_ctor (tree clause, tree decl, tree outer)
     867              : {
     868        21654 :   tree type = TREE_TYPE (decl), size, ptr, cond, then_b, else_b;
     869        21654 :   stmtblock_t block, cond_block;
     870              : 
     871        21654 :   switch (OMP_CLAUSE_CODE (clause))
     872              :     {
     873              :     case OMP_CLAUSE__LOOPTEMP_:
     874              :     case OMP_CLAUSE__REDUCTEMP_:
     875              :     case OMP_CLAUSE__CONDTEMP_:
     876              :     case OMP_CLAUSE__SCANTEMP_:
     877              :       return NULL;
     878        21611 :     case OMP_CLAUSE_PRIVATE:
     879        21611 :     case OMP_CLAUSE_LASTPRIVATE:
     880        21611 :     case OMP_CLAUSE_LINEAR:
     881        21611 :     case OMP_CLAUSE_REDUCTION:
     882        21611 :     case OMP_CLAUSE_IN_REDUCTION:
     883        21611 :     case OMP_CLAUSE_TASK_REDUCTION:
     884        21611 :       break;
     885            0 :     default:
     886            0 :       gcc_unreachable ();
     887              :     }
     888              : 
     889        21611 :   if (gfc_omp_body_local_private_p (clause))
     890              :     return NULL_TREE;
     891              : 
     892        21608 :   if ((! GFC_DESCRIPTOR_TYPE_P (type)
     893          271 :        || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
     894        42970 :       && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
     895          223 :           || !POINTER_TYPE_P (type)))
     896              :     {
     897        21139 :       if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
     898              :         {
     899           52 :           gcc_assert (outer);
     900           52 :           gfc_start_block (&block);
     901          104 :           tree tem = gfc_walk_alloc_comps (outer, decl,
     902           52 :                                            OMP_CLAUSE_DECL (clause),
     903              :                                            WALK_ALLOC_COMPS_DEFAULT_CTOR);
     904           52 :           gfc_add_expr_to_block (&block, tem);
     905           52 :           return gfc_finish_block (&block);
     906              :         }
     907              :       return NULL_TREE;
     908              :     }
     909              : 
     910          470 :   gcc_assert (outer != NULL_TREE
     911              :               || (!GFC_DESCRIPTOR_TYPE_P (type)
     912              :                   && !gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause),
     913              :                                            false)));
     914              : 
     915              :   /* Allocatable arrays and scalars in PRIVATE clauses need to be set to
     916              :      "not currently allocated" allocation status if outer
     917              :      array is "not currently allocated", otherwise should be allocated.  */
     918          469 :   gfc_start_block (&block);
     919              : 
     920          469 :   gfc_init_block (&cond_block);
     921              : 
     922          469 :   if (GFC_DESCRIPTOR_TYPE_P (type))
     923              :     {
     924          246 :       gfc_add_modify (&cond_block, decl, outer);
     925          246 :       tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
     926          246 :       size = gfc_conv_descriptor_ubound_get (decl, rank);
     927          246 :       size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
     928              :                               size,
     929              :                               gfc_conv_descriptor_lbound_get (decl, rank));
     930          246 :       size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
     931              :                               size, gfc_index_one_node);
     932          246 :       if (GFC_TYPE_ARRAY_RANK (type) > 1)
     933          130 :         size = fold_build2_loc (input_location, MULT_EXPR,
     934              :                                 gfc_array_index_type, size,
     935              :                                 gfc_conv_descriptor_stride_get (decl, rank));
     936          246 :       tree esize = fold_convert (gfc_array_index_type,
     937              :                                  TYPE_SIZE_UNIT (gfc_get_element_type (type)));
     938          246 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
     939              :                               size, esize);
     940          246 :       size = unshare_expr (size);
     941          246 :       size = gfc_evaluate_now (fold_convert (size_type_node, size),
     942              :                                &cond_block);
     943              :     }
     944              :   else
     945          223 :     size = fold_convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (type)));
     946          469 :   ptr = gfc_create_var (pvoid_type_node, NULL);
     947          469 :   gfc_allocate_using_malloc (&cond_block, ptr, size, NULL_TREE);
     948          469 :   if (GFC_DESCRIPTOR_TYPE_P (type))
     949          246 :     gfc_conv_descriptor_data_set (&cond_block, unshare_expr (decl), ptr);
     950              :   else
     951          223 :     gfc_add_modify (&cond_block, unshare_expr (decl),
     952          223 :                     fold_convert (TREE_TYPE (decl), ptr));
     953          469 :   if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
     954              :     {
     955          124 :       tree tem = gfc_walk_alloc_comps (outer, decl,
     956           62 :                                        OMP_CLAUSE_DECL (clause),
     957              :                                        WALK_ALLOC_COMPS_DEFAULT_CTOR);
     958           62 :       gfc_add_expr_to_block (&cond_block, tem);
     959              :     }
     960          469 :   then_b = gfc_finish_block (&cond_block);
     961              : 
     962              :   /* Reduction clause requires allocated ALLOCATABLE.  */
     963          469 :   if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_REDUCTION
     964          324 :       && OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_IN_REDUCTION
     965          793 :       && OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_TASK_REDUCTION)
     966              :     {
     967          324 :       gfc_init_block (&cond_block);
     968          324 :       if (GFC_DESCRIPTOR_TYPE_P (type))
     969          126 :         gfc_conv_descriptor_data_set (&cond_block, unshare_expr (decl),
     970              :                                       null_pointer_node);
     971              :       else
     972          198 :         gfc_add_modify (&cond_block, unshare_expr (decl),
     973          198 :                         build_zero_cst (TREE_TYPE (decl)));
     974          324 :       else_b = gfc_finish_block (&cond_block);
     975              : 
     976          324 :       tree tem = fold_convert (pvoid_type_node,
     977              :                                GFC_DESCRIPTOR_TYPE_P (type)
     978              :                                ? gfc_conv_descriptor_data_get (outer) : outer);
     979          324 :       tem = unshare_expr (tem);
     980          324 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
     981              :                               tem, null_pointer_node);
     982          324 :       gfc_add_expr_to_block (&block,
     983              :                              build3_loc (input_location, COND_EXPR,
     984              :                                          void_type_node, cond, then_b,
     985              :                                          else_b));
     986              :       /* Avoid -W*uninitialized warnings.  */
     987          324 :       if (DECL_P (decl))
     988          224 :         suppress_warning (decl, OPT_Wuninitialized);
     989              :     }
     990              :   else
     991          145 :     gfc_add_expr_to_block (&block, then_b);
     992              : 
     993          469 :   return gfc_finish_block (&block);
     994              : }
     995              : 
     996              : /* Build and return code for a copy constructor from SRC to DEST.  */
     997              : 
     998              : tree
     999         9349 : gfc_omp_clause_copy_ctor (tree clause, tree dest, tree src)
    1000              : {
    1001         9349 :   tree type = TREE_TYPE (dest), ptr, size, call;
    1002         9349 :   tree decl_type = TREE_TYPE (OMP_CLAUSE_DECL (clause));
    1003         9349 :   tree orig_decl = OMP_CLAUSE_DECL (clause);
    1004         9349 :   tree cond, then_b, else_b;
    1005         9349 :   stmtblock_t block, cond_block;
    1006              : 
    1007         9349 :   gcc_assert (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_FIRSTPRIVATE
    1008              :               || OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_LINEAR);
    1009              : 
    1010         9349 :   if (DECL_ARTIFICIAL (orig_decl)
    1011         6225 :       && DECL_LANG_SPECIFIC (orig_decl)
    1012         9538 :       && GFC_DECL_SAVED_DESCRIPTOR (orig_decl))
    1013              :     {
    1014          173 :       orig_decl = GFC_DECL_SAVED_DESCRIPTOR (orig_decl);
    1015          173 :       decl_type = TREE_TYPE (orig_decl);
    1016              :     }
    1017              : 
    1018              :   /* Privatize pointer association only; cf. gfc_omp_predetermined_sharing.
    1019              :      This includes scalar class pointers, whose tree type is still the class
    1020              :      record even though the Fortran entity has POINTER semantics.  */
    1021         9349 :   if (DECL_P (orig_decl)
    1022         9349 :       && (GFC_DECL_ASSOCIATE_VAR_P (orig_decl)
    1023         9322 :           || GFC_DECL_GET_SCALAR_POINTER (orig_decl)
    1024         9291 :           || gfc_class_container_kind (decl_type) == 'p'))
    1025           64 :     return build2 (MODIFY_EXPR, TREE_TYPE (dest), dest, src);
    1026              : 
    1027         9285 :   if (gfc_is_polymorphic_nonptr (decl_type))
    1028              :     {
    1029           40 :       if (POINTER_TYPE_P (decl_type))
    1030           27 :         decl_type = TREE_TYPE (decl_type);
    1031           40 :       decl_type = TREE_TYPE (TYPE_FIELDS (decl_type));
    1032           40 :       if (GFC_DESCRIPTOR_TYPE_P (decl_type) || GFC_ARRAY_TYPE_P (decl_type))
    1033            4 :         fatal_error (input_location,
    1034              :                      "Sorry, polymorphic arrays not yet supported for "
    1035              :                      "firstprivate");
    1036           36 :       tree src_len;
    1037           36 :       tree nelems = build_int_cst (size_type_node, 1);  /* Scalar.  */
    1038           36 :       tree src_data = gfc_class_data_get (unshare_expr (src));
    1039           36 :       tree dest_data = gfc_class_data_get (unshare_expr (dest));
    1040           36 :       bool unlimited = gfc_is_unlimited_polymorphic_nonptr (type);
    1041              : 
    1042           36 :       gfc_start_block (&block);
    1043           36 :       gfc_add_modify (&block, gfc_class_vptr_get (dest),
    1044              :                       gfc_class_vptr_get (src));
    1045           36 :       gfc_init_block (&cond_block);
    1046              : 
    1047           36 :       if (unlimited)
    1048              :         {
    1049           24 :           src_len = gfc_class_len_get (src);
    1050           24 :           gfc_add_modify (&cond_block, gfc_class_len_get (unshare_expr (dest)), src_len);
    1051              :         }
    1052              : 
    1053              :       /* Use: size = class._vtab._size * (class._len > 0 ? class._len : 1).  */
    1054           36 :       size = fold_convert (size_type_node, gfc_class_vtab_size_get (src));
    1055           36 :       if (unlimited)
    1056              :         {
    1057           24 :           cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
    1058              :                                   unshare_expr (src_len),
    1059           24 :                                   build_zero_cst (TREE_TYPE (src_len)));
    1060           24 :           cond = build3_loc (input_location, COND_EXPR, size_type_node, cond,
    1061              :                              fold_convert (size_type_node,
    1062              :                                            unshare_expr (src_len)),
    1063              :                              build_int_cst (size_type_node, 1));
    1064           24 :           size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    1065              :                                   size, cond);
    1066              :         }
    1067              : 
    1068              :       /* Malloc memory + call class->_vpt->_copy.  */
    1069           36 :       call = builtin_decl_explicit (BUILT_IN_MALLOC);
    1070           36 :       call = build_call_expr_loc (input_location, call, 1, size);
    1071           36 :       gfc_add_modify (&cond_block, dest_data,
    1072           36 :                       fold_convert (TREE_TYPE (dest_data), call));
    1073           36 :       gfc_add_expr_to_block (&cond_block,
    1074              :                              gfc_copy_class_to_class (src, dest, nelems,
    1075              :                                                       unlimited));
    1076              : 
    1077           36 :       gcc_assert (TREE_CODE (dest_data) == COMPONENT_REF);
    1078           36 :       if (!GFC_DECL_GET_SCALAR_ALLOCATABLE (TREE_OPERAND (dest_data, 1)))
    1079              :         {
    1080           12 :           gfc_add_block_to_block (&block, &cond_block);
    1081              :         }
    1082              :       else
    1083              :         {
    1084              :           /* Create: if (class._data != 0) <cond_block> else class._data = NULL; */
    1085           24 :           cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    1086              :                                   src_data, null_pointer_node);
    1087           24 :           gfc_add_expr_to_block (&block, build3_loc (input_location, COND_EXPR,
    1088              :                                  void_type_node, cond,
    1089              :                                  gfc_finish_block (&cond_block),
    1090              :                                  fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    1091              :                                  unshare_expr (dest_data), null_pointer_node)));
    1092              :         }
    1093           36 :       return gfc_finish_block (&block);
    1094              :     }
    1095              : 
    1096         9245 :   if ((! GFC_DESCRIPTOR_TYPE_P (type)
    1097          151 :        || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
    1098         9275 :       && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
    1099           77 :           || !POINTER_TYPE_P (type)))
    1100              :     {
    1101         9049 :       if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
    1102              :         {
    1103           20 :           gfc_start_block (&block);
    1104           20 :           gfc_add_modify (&block, dest, src);
    1105           20 :           tree tem = gfc_walk_alloc_comps (src, dest, OMP_CLAUSE_DECL (clause),
    1106              :                                            WALK_ALLOC_COMPS_COPY_CTOR);
    1107           20 :           gfc_add_expr_to_block (&block, tem);
    1108           20 :           return gfc_finish_block (&block);
    1109              :         }
    1110              :       else
    1111         9029 :         return build2_v (MODIFY_EXPR, dest, src);
    1112              :     }
    1113              : 
    1114              :   /* Allocatable arrays in FIRSTPRIVATE clauses need to be allocated
    1115              :      and copied from SRC.  */
    1116          196 :   gfc_start_block (&block);
    1117              : 
    1118          196 :   gfc_init_block (&cond_block);
    1119              : 
    1120          196 :   gfc_add_modify (&cond_block, dest, fold_convert (TREE_TYPE (dest), src));
    1121          196 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1122              :     {
    1123          121 :       tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
    1124          121 :       size = gfc_conv_descriptor_ubound_get (dest, rank);
    1125          121 :       size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    1126              :                               size,
    1127              :                               gfc_conv_descriptor_lbound_get (dest, rank));
    1128          121 :       size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    1129              :                               size, gfc_index_one_node);
    1130          121 :       if (GFC_TYPE_ARRAY_RANK (type) > 1)
    1131           42 :         size = fold_build2_loc (input_location, MULT_EXPR,
    1132              :                                 gfc_array_index_type, size,
    1133              :                                 gfc_conv_descriptor_stride_get (dest, rank));
    1134          121 :       tree esize = fold_convert (gfc_array_index_type,
    1135              :                                  TYPE_SIZE_UNIT (gfc_get_element_type (type)));
    1136          121 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    1137              :                               size, esize);
    1138          121 :       size = unshare_expr (size);
    1139          121 :       size = gfc_evaluate_now (fold_convert (size_type_node, size),
    1140              :                                &cond_block);
    1141              :     }
    1142              :   else
    1143           75 :     size = fold_convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (type)));
    1144          196 :   ptr = gfc_create_var (pvoid_type_node, NULL);
    1145          196 :   gfc_allocate_using_malloc (&cond_block, ptr, size, NULL_TREE);
    1146          196 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1147          121 :     gfc_conv_descriptor_data_set (&cond_block, unshare_expr (dest), ptr);
    1148              :   else
    1149           75 :     gfc_add_modify (&cond_block, unshare_expr (dest),
    1150           75 :                     fold_convert (TREE_TYPE (dest), ptr));
    1151              : 
    1152          196 :   tree srcptr = GFC_DESCRIPTOR_TYPE_P (type)
    1153          196 :                 ? gfc_conv_descriptor_data_get (src) : src;
    1154          196 :   srcptr = unshare_expr (srcptr);
    1155          196 :   srcptr = fold_convert (pvoid_type_node, srcptr);
    1156          196 :   call = build_call_expr_loc (input_location,
    1157              :                               builtin_decl_explicit (BUILT_IN_MEMCPY), 3, ptr,
    1158              :                               srcptr, size);
    1159          196 :   gfc_add_expr_to_block (&cond_block, fold_convert (void_type_node, call));
    1160          196 :   if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
    1161              :     {
    1162           48 :       tree tem = gfc_walk_alloc_comps (src, dest,
    1163           24 :                                        OMP_CLAUSE_DECL (clause),
    1164              :                                        WALK_ALLOC_COMPS_COPY_CTOR);
    1165           24 :       gfc_add_expr_to_block (&cond_block, tem);
    1166              :     }
    1167          196 :   then_b = gfc_finish_block (&cond_block);
    1168              : 
    1169          196 :   gfc_init_block (&cond_block);
    1170          196 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1171          121 :     gfc_conv_descriptor_data_set (&cond_block, unshare_expr (dest),
    1172              :                                   null_pointer_node);
    1173              :   else
    1174           75 :     gfc_add_modify (&cond_block, unshare_expr (dest),
    1175           75 :                     build_zero_cst (TREE_TYPE (dest)));
    1176          196 :   else_b = gfc_finish_block (&cond_block);
    1177              : 
    1178          196 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1179              :                           unshare_expr (srcptr), null_pointer_node);
    1180          196 :   gfc_add_expr_to_block (&block,
    1181              :                          build3_loc (input_location, COND_EXPR,
    1182              :                                      void_type_node, cond, then_b, else_b));
    1183              :   /* Avoid -W*uninitialized warnings.  */
    1184          196 :   if (DECL_P (dest))
    1185          127 :     suppress_warning (dest, OPT_Wuninitialized);
    1186              : 
    1187          196 :   return gfc_finish_block (&block);
    1188              : }
    1189              : 
    1190              : /* Similarly, except use an intrinsic or pointer assignment operator
    1191              :    instead.  */
    1192              : 
    1193              : tree
    1194         6941 : gfc_omp_clause_assign_op (tree clause, tree dest, tree src)
    1195              : {
    1196         6941 :   tree type = TREE_TYPE (dest), ptr, size, call, nonalloc;
    1197         6941 :   tree cond, then_b, else_b;
    1198         6941 :   stmtblock_t block, cond_block, cond_block2, inner_block;
    1199              : 
    1200         6941 :   if ((! GFC_DESCRIPTOR_TYPE_P (type)
    1201          236 :        || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
    1202        13677 :       && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
    1203          222 :           || !POINTER_TYPE_P (type)))
    1204              :     {
    1205         6514 :       if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
    1206              :         {
    1207           30 :           gfc_start_block (&block);
    1208              :           /* First dealloc any allocatable components in DEST.  */
    1209           60 :           tree tem = gfc_walk_alloc_comps (dest, NULL_TREE,
    1210           30 :                                            OMP_CLAUSE_DECL (clause),
    1211              :                                            WALK_ALLOC_COMPS_DTOR);
    1212           30 :           gfc_add_expr_to_block (&block, tem);
    1213              :           /* Then copy over toplevel data.  */
    1214           30 :           gfc_add_modify (&block, dest, src);
    1215              :           /* Finally allocate any allocatable components and copy.  */
    1216           30 :           tem = gfc_walk_alloc_comps (src, dest, OMP_CLAUSE_DECL (clause),
    1217              :                                            WALK_ALLOC_COMPS_COPY_CTOR);
    1218           30 :           gfc_add_expr_to_block (&block, tem);
    1219           30 :           return gfc_finish_block (&block);
    1220              :         }
    1221              :       else
    1222         6484 :         return build2_v (MODIFY_EXPR, dest, src);
    1223              :     }
    1224              : 
    1225          427 :   gfc_start_block (&block);
    1226              : 
    1227          427 :   if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
    1228              :     {
    1229           32 :       then_b = gfc_walk_alloc_comps (dest, NULL_TREE, OMP_CLAUSE_DECL (clause),
    1230              :                                      WALK_ALLOC_COMPS_DTOR);
    1231           32 :       tree tem = fold_convert (pvoid_type_node,
    1232              :                                GFC_DESCRIPTOR_TYPE_P (type)
    1233              :                                ? gfc_conv_descriptor_data_get (dest) : dest);
    1234           32 :       tem = unshare_expr (tem);
    1235           32 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1236              :                               tem, null_pointer_node);
    1237           32 :       tem = build3_loc (input_location, COND_EXPR, void_type_node, cond,
    1238              :                         then_b, build_empty_stmt (input_location));
    1239           32 :       gfc_add_expr_to_block (&block, tem);
    1240              :     }
    1241              : 
    1242          427 :   gfc_init_block (&cond_block);
    1243              : 
    1244          427 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1245              :     {
    1246          205 :       tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
    1247          205 :       size = gfc_conv_descriptor_ubound_get (src, rank);
    1248          205 :       size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    1249              :                               size,
    1250              :                               gfc_conv_descriptor_lbound_get (src, rank));
    1251          205 :       size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    1252              :                               size, gfc_index_one_node);
    1253          205 :       if (GFC_TYPE_ARRAY_RANK (type) > 1)
    1254           88 :         size = fold_build2_loc (input_location, MULT_EXPR,
    1255              :                                 gfc_array_index_type, size,
    1256              :                                 gfc_conv_descriptor_stride_get (src, rank));
    1257          205 :       tree esize = fold_convert (gfc_array_index_type,
    1258              :                                  TYPE_SIZE_UNIT (gfc_get_element_type (type)));
    1259          205 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    1260              :                               size, esize);
    1261          205 :       size = unshare_expr (size);
    1262          205 :       size = gfc_evaluate_now (fold_convert (size_type_node, size),
    1263              :                                &cond_block);
    1264              :     }
    1265              :   else
    1266          222 :     size = fold_convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (type)));
    1267          427 :   ptr = gfc_create_var (pvoid_type_node, NULL);
    1268              : 
    1269          427 :   tree destptr = GFC_DESCRIPTOR_TYPE_P (type)
    1270          427 :                  ? gfc_conv_descriptor_data_get (dest) : dest;
    1271          427 :   destptr = unshare_expr (destptr);
    1272          427 :   destptr = fold_convert (pvoid_type_node, destptr);
    1273          427 :   gfc_add_modify (&cond_block, ptr, destptr);
    1274              : 
    1275          427 :   nonalloc = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    1276              :                               destptr, null_pointer_node);
    1277          427 :   cond = nonalloc;
    1278          427 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1279              :     {
    1280              :       int i;
    1281          498 :       for (i = 0; i < GFC_TYPE_ARRAY_RANK (type); i++)
    1282              :         {
    1283          293 :           tree rank = gfc_rank_cst[i];
    1284          293 :           tree tem = gfc_conv_descriptor_ubound_get (src, rank);
    1285          293 :           tem = fold_build2_loc (input_location, MINUS_EXPR,
    1286              :                                  gfc_array_index_type, tem,
    1287              :                                  gfc_conv_descriptor_lbound_get (src, rank));
    1288          293 :           tem = fold_build2_loc (input_location, PLUS_EXPR,
    1289              :                                  gfc_array_index_type, tem,
    1290              :                                  gfc_conv_descriptor_lbound_get (dest, rank));
    1291          293 :           tem = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1292              :                                  tem, gfc_conv_descriptor_ubound_get (dest,
    1293              :                                                                       rank));
    1294          293 :           cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    1295              :                                   logical_type_node, cond, tem);
    1296              :         }
    1297              :     }
    1298              : 
    1299          427 :   gfc_init_block (&cond_block2);
    1300              : 
    1301          427 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1302              :     {
    1303          205 :       gfc_init_block (&inner_block);
    1304          205 :       gfc_allocate_using_malloc (&inner_block, ptr, size, NULL_TREE);
    1305          205 :       then_b = gfc_finish_block (&inner_block);
    1306              : 
    1307          205 :       gfc_init_block (&inner_block);
    1308          205 :       gfc_add_modify (&inner_block, ptr,
    1309              :                       gfc_call_realloc (&inner_block, ptr, size));
    1310          205 :       else_b = gfc_finish_block (&inner_block);
    1311              : 
    1312          205 :       gfc_add_expr_to_block (&cond_block2,
    1313              :                              build3_loc (input_location, COND_EXPR,
    1314              :                                          void_type_node,
    1315              :                                          unshare_expr (nonalloc),
    1316              :                                          then_b, else_b));
    1317          205 :       gfc_add_modify (&cond_block2, dest, src);
    1318          205 :       gfc_conv_descriptor_data_set (&cond_block2, unshare_expr (dest), ptr);
    1319              :     }
    1320              :   else
    1321              :     {
    1322          222 :       gfc_allocate_using_malloc (&cond_block2, ptr, size, NULL_TREE);
    1323          222 :       gfc_add_modify (&cond_block2, unshare_expr (dest),
    1324              :                       fold_convert (type, ptr));
    1325              :     }
    1326          427 :   then_b = gfc_finish_block (&cond_block2);
    1327          427 :   else_b = build_empty_stmt (input_location);
    1328              : 
    1329          427 :   gfc_add_expr_to_block (&cond_block,
    1330              :                          build3_loc (input_location, COND_EXPR,
    1331              :                                      void_type_node, unshare_expr (cond),
    1332              :                                      then_b, else_b));
    1333              : 
    1334          427 :   tree srcptr = GFC_DESCRIPTOR_TYPE_P (type)
    1335          427 :                 ? gfc_conv_descriptor_data_get (src) : src;
    1336          427 :   srcptr = unshare_expr (srcptr);
    1337          427 :   srcptr = fold_convert (pvoid_type_node, srcptr);
    1338          427 :   call = build_call_expr_loc (input_location,
    1339              :                               builtin_decl_explicit (BUILT_IN_MEMCPY), 3, ptr,
    1340              :                               srcptr, size);
    1341          427 :   gfc_add_expr_to_block (&cond_block, fold_convert (void_type_node, call));
    1342          427 :   if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
    1343              :     {
    1344           64 :       tree tem = gfc_walk_alloc_comps (src, dest,
    1345           32 :                                        OMP_CLAUSE_DECL (clause),
    1346              :                                        WALK_ALLOC_COMPS_COPY_CTOR);
    1347           32 :       gfc_add_expr_to_block (&cond_block, tem);
    1348              :     }
    1349          427 :   then_b = gfc_finish_block (&cond_block);
    1350              : 
    1351          427 :   if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_COPYIN)
    1352              :     {
    1353           66 :       gfc_init_block (&cond_block);
    1354           66 :       if (GFC_DESCRIPTOR_TYPE_P (type))
    1355              :         {
    1356           48 :           tree tmp = gfc_conv_descriptor_data_get (unshare_expr (dest));
    1357           48 :           tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
    1358              :                                             NULL_TREE, NULL_TREE, true, NULL,
    1359              :                                             GFC_CAF_COARRAY_NOCOARRAY);
    1360           48 :           gfc_add_expr_to_block (&cond_block, tmp);
    1361              :         }
    1362              :       else
    1363              :         {
    1364           18 :           destptr = gfc_evaluate_now (destptr, &cond_block);
    1365           18 :           gfc_add_expr_to_block (&cond_block, gfc_call_free (destptr));
    1366           18 :           gfc_add_modify (&cond_block, unshare_expr (dest),
    1367           18 :                           build_zero_cst (TREE_TYPE (dest)));
    1368              :         }
    1369           66 :       else_b = gfc_finish_block (&cond_block);
    1370              : 
    1371           66 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1372              :                               unshare_expr (srcptr), null_pointer_node);
    1373           66 :       gfc_add_expr_to_block (&block,
    1374              :                              build3_loc (input_location, COND_EXPR,
    1375              :                                          void_type_node, cond,
    1376              :                                          then_b, else_b));
    1377              :     }
    1378              :   else
    1379          361 :     gfc_add_expr_to_block (&block, then_b);
    1380              : 
    1381          427 :   return gfc_finish_block (&block);
    1382              : }
    1383              : 
    1384              : static void
    1385           84 : gfc_omp_linear_clause_add_loop (stmtblock_t *block, tree dest, tree src,
    1386              :                                 tree add, tree nelems)
    1387              : {
    1388           84 :   stmtblock_t tmpblock;
    1389           84 :   tree desta, srca, index = gfc_create_var (gfc_array_index_type, "S");
    1390           84 :   nelems = gfc_evaluate_now (nelems, block);
    1391              : 
    1392           84 :   gfc_init_block (&tmpblock);
    1393           84 :   if (TREE_CODE (TREE_TYPE (dest)) == ARRAY_TYPE)
    1394              :     {
    1395           60 :       desta = gfc_build_array_ref (dest, index, NULL);
    1396           60 :       srca = gfc_build_array_ref (src, index, NULL);
    1397              :     }
    1398              :   else
    1399              :     {
    1400           24 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (dest)));
    1401           24 :       tree idx = fold_build2 (MULT_EXPR, sizetype,
    1402              :                               fold_convert (sizetype, index),
    1403              :                               TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dest))));
    1404           24 :       desta = build_fold_indirect_ref (fold_build2 (POINTER_PLUS_EXPR,
    1405              :                                                     TREE_TYPE (dest), dest,
    1406              :                                                     idx));
    1407           24 :       srca = build_fold_indirect_ref (fold_build2 (POINTER_PLUS_EXPR,
    1408              :                                                    TREE_TYPE (src), src,
    1409              :                                                     idx));
    1410              :     }
    1411           84 :   gfc_add_modify (&tmpblock, desta,
    1412           84 :                   fold_build2 (PLUS_EXPR, TREE_TYPE (desta),
    1413              :                                srca, add));
    1414              : 
    1415           84 :   gfc_loopinfo loop;
    1416           84 :   gfc_init_loopinfo (&loop);
    1417           84 :   loop.dimen = 1;
    1418           84 :   loop.from[0] = gfc_index_zero_node;
    1419           84 :   loop.loopvar[0] = index;
    1420           84 :   loop.to[0] = nelems;
    1421           84 :   gfc_trans_scalarizing_loops (&loop, &tmpblock);
    1422           84 :   gfc_add_block_to_block (block, &loop.pre);
    1423           84 : }
    1424              : 
    1425              : /* Build and return code for a constructor of DEST that initializes
    1426              :    it to SRC plus ADD (ADD is scalar integer).  */
    1427              : 
    1428              : tree
    1429          108 : gfc_omp_clause_linear_ctor (tree clause, tree dest, tree src, tree add)
    1430              : {
    1431          108 :   tree type = TREE_TYPE (dest), ptr, size, nelems = NULL_TREE;
    1432          108 :   stmtblock_t block;
    1433              : 
    1434          108 :   gcc_assert (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_LINEAR);
    1435              : 
    1436          108 :   gfc_start_block (&block);
    1437          108 :   add = gfc_evaluate_now (add, &block);
    1438              : 
    1439          108 :   if ((! GFC_DESCRIPTOR_TYPE_P (type)
    1440           24 :        || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
    1441          192 :       && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
    1442           24 :           || !POINTER_TYPE_P (type)))
    1443              :     {
    1444           60 :       bool compute_nelts = false;
    1445           60 :       gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
    1446           60 :       if (!TYPE_DOMAIN (type)
    1447           60 :           || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE
    1448           60 :           || TYPE_MIN_VALUE (TYPE_DOMAIN (type)) == error_mark_node
    1449          120 :           || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == error_mark_node)
    1450              :         compute_nelts = true;
    1451           60 :       else if (VAR_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
    1452              :         {
    1453           48 :           tree a = DECL_ATTRIBUTES (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
    1454           48 :           if (lookup_attribute ("omp dummy var", a))
    1455              :             compute_nelts = true;
    1456              :         }
    1457              :       if (compute_nelts)
    1458              :         {
    1459           48 :           nelems = fold_build2 (EXACT_DIV_EXPR, sizetype,
    1460              :                                 TYPE_SIZE_UNIT (type),
    1461              :                                 TYPE_SIZE_UNIT (TREE_TYPE (type)));
    1462           48 :           nelems = size_binop (MINUS_EXPR, nelems, size_one_node);
    1463              :         }
    1464              :       else
    1465           12 :         nelems = array_type_nelts_minus_one (type);
    1466           60 :       nelems = fold_convert (gfc_array_index_type, nelems);
    1467              : 
    1468           60 :       gfc_omp_linear_clause_add_loop (&block, dest, src, add, nelems);
    1469           60 :       return gfc_finish_block (&block);
    1470              :     }
    1471              : 
    1472              :   /* Allocatable arrays in LINEAR clauses need to be allocated
    1473              :      and copied from SRC.  */
    1474           48 :   gfc_add_modify (&block, dest, src);
    1475           48 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1476              :     {
    1477           24 :       tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
    1478           24 :       size = gfc_conv_descriptor_ubound_get (dest, rank);
    1479           24 :       size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    1480              :                               size,
    1481              :                               gfc_conv_descriptor_lbound_get (dest, rank));
    1482           24 :       size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    1483              :                               size, gfc_index_one_node);
    1484           24 :       if (GFC_TYPE_ARRAY_RANK (type) > 1)
    1485            0 :         size = fold_build2_loc (input_location, MULT_EXPR,
    1486              :                                 gfc_array_index_type, size,
    1487              :                                 gfc_conv_descriptor_stride_get (dest, rank));
    1488           24 :       tree esize = fold_convert (gfc_array_index_type,
    1489              :                                  TYPE_SIZE_UNIT (gfc_get_element_type (type)));
    1490           24 :       nelems = gfc_evaluate_now (unshare_expr (size), &block);
    1491           24 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    1492              :                               nelems, unshare_expr (esize));
    1493           24 :       size = gfc_evaluate_now (fold_convert (size_type_node, size),
    1494              :                                &block);
    1495           24 :       nelems = fold_build2_loc (input_location, MINUS_EXPR,
    1496              :                                 gfc_array_index_type, nelems,
    1497              :                                 gfc_index_one_node);
    1498              :     }
    1499              :   else
    1500           24 :     size = fold_convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (type)));
    1501           48 :   ptr = gfc_create_var (pvoid_type_node, NULL);
    1502           48 :   gfc_allocate_using_malloc (&block, ptr, size, NULL_TREE);
    1503           48 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1504              :     {
    1505           24 :       gfc_conv_descriptor_data_set (&block, unshare_expr (dest), ptr);
    1506           24 :       tree etype = gfc_get_element_type (type);
    1507           24 :       ptr = fold_convert (build_pointer_type (etype), ptr);
    1508           24 :       tree srcptr = gfc_conv_descriptor_data_get (unshare_expr (src));
    1509           24 :       srcptr = fold_convert (build_pointer_type (etype), srcptr);
    1510           24 :       gfc_omp_linear_clause_add_loop (&block, ptr, srcptr, add, nelems);
    1511              :     }
    1512              :   else
    1513              :     {
    1514           24 :       gfc_add_modify (&block, unshare_expr (dest),
    1515           24 :                       fold_convert (TREE_TYPE (dest), ptr));
    1516           24 :       ptr = fold_convert (TREE_TYPE (dest), ptr);
    1517           24 :       tree dstm = build_fold_indirect_ref (ptr);
    1518           24 :       tree srcm = build_fold_indirect_ref (unshare_expr (src));
    1519           24 :       gfc_add_modify (&block, dstm,
    1520           24 :                       fold_build2 (PLUS_EXPR, TREE_TYPE (add), srcm, add));
    1521              :     }
    1522           48 :   return gfc_finish_block (&block);
    1523              : }
    1524              : 
    1525              : /* Build and return code destructing DECL.  Return NULL if nothing
    1526              :    to be done.  */
    1527              : 
    1528              : tree
    1529        33788 : gfc_omp_clause_dtor (tree clause, tree decl)
    1530              : {
    1531        33788 :   tree type = TREE_TYPE (decl), tem;
    1532        33788 :   tree decl_type = TREE_TYPE (OMP_CLAUSE_DECL (clause));
    1533        33788 :   tree orig_decl = OMP_CLAUSE_DECL (clause);
    1534              : 
    1535        33788 :   if (DECL_ARTIFICIAL (orig_decl)
    1536        12860 :       && DECL_LANG_SPECIFIC (orig_decl)
    1537        34150 :       && GFC_DECL_SAVED_DESCRIPTOR (orig_decl))
    1538              :     {
    1539          346 :       orig_decl = GFC_DECL_SAVED_DESCRIPTOR (orig_decl);
    1540          346 :       decl_type = TREE_TYPE (orig_decl);
    1541              :     }
    1542              : 
    1543              :   /* Only pointer association was privatized; cf. gfc_omp_clause_copy_ctor.
    1544              :      Scalar class pointers must not finalize or free their targets here.  */
    1545        33788 :   if (DECL_P (orig_decl)
    1546        33788 :       && (GFC_DECL_ASSOCIATE_VAR_P (orig_decl)
    1547        33761 :           || GFC_DECL_GET_SCALAR_POINTER (orig_decl)
    1548        33695 :           || gfc_class_container_kind (decl_type) == 'p'))
    1549              :     return NULL_TREE;
    1550        33683 :   if (gfc_omp_body_local_private_p (clause))
    1551              :     return NULL_TREE;
    1552              :   /* Only gfc_omp_clause_copy_ctor gives a nonallocatable CLASS copy data of
    1553              :      its own.  */
    1554        33680 :   if (gfc_class_container_kind (decl_type) == 't'
    1555           26 :       && OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_FIRSTPRIVATE
    1556        33694 :       && OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_LINEAR)
    1557              :     return NULL_TREE;
    1558        33666 :   if (gfc_is_polymorphic_nonptr (decl_type))
    1559              :     {
    1560           37 :       if (POINTER_TYPE_P (decl_type))
    1561           24 :         decl_type = TREE_TYPE (decl_type);
    1562           37 :       decl_type = TREE_TYPE (TYPE_FIELDS (decl_type));
    1563           37 :       if (GFC_DESCRIPTOR_TYPE_P (decl_type) || GFC_ARRAY_TYPE_P (decl_type))
    1564            0 :         fatal_error (input_location,
    1565              :                      "Sorry, polymorphic arrays not yet supported for "
    1566              :                      "firstprivate");
    1567           37 :       stmtblock_t block, cond_block;
    1568           37 :       gfc_start_block (&block);
    1569           37 :       gfc_init_block (&cond_block);
    1570           37 :       tree final = gfc_class_vtab_final_get (decl);
    1571           37 :       tree size = fold_convert (size_type_node, gfc_class_vtab_size_get (decl));
    1572           37 :       gfc_se se;
    1573           37 :       gfc_init_se (&se, NULL);
    1574           37 :       symbol_attribute attr = {};
    1575           37 :       tree data = gfc_class_data_get (decl);
    1576           37 :       tree desc = gfc_conv_scalar_to_descriptor (&se, data, attr);
    1577              : 
    1578              :       /* Call class->_vpt->_finalize + free.  */
    1579           37 :       tree call = build_fold_indirect_ref (final);
    1580           37 :       call = build_call_expr_loc (input_location, call, 3,
    1581              :                                   gfc_build_addr_expr (NULL, desc),
    1582              :                                   size, boolean_false_node);
    1583           37 :       gfc_add_block_to_block (&cond_block, &se.pre);
    1584           37 :       gfc_add_expr_to_block (&cond_block, fold_convert (void_type_node, call));
    1585           37 :       gfc_add_block_to_block (&cond_block, &se.post);
    1586              :       /* Create: if (_vtab && _final) <cond_block>  */
    1587           37 :       tree cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    1588              :                                    gfc_class_vptr_get (decl),
    1589              :                                    null_pointer_node);
    1590           37 :       tree cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    1591              :                                    final, null_pointer_node);
    1592           37 :       cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    1593              :                               boolean_type_node, cond, cond2);
    1594           37 :       gfc_add_expr_to_block (&block, build3_loc (input_location, COND_EXPR,
    1595              :                                  void_type_node, cond,
    1596              :                                  gfc_finish_block (&cond_block), NULL_TREE));
    1597           37 :       call = builtin_decl_explicit (BUILT_IN_FREE);
    1598           37 :       call = build_call_expr_loc (input_location, call, 1, data);
    1599           37 :       gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
    1600           37 :       return gfc_finish_block (&block);
    1601              :     }
    1602              : 
    1603        33629 :   if ((! GFC_DESCRIPTOR_TYPE_P (type)
    1604          454 :        || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
    1605        66868 :       && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
    1606          332 :           || !POINTER_TYPE_P (type)))
    1607              :     {
    1608        32909 :       if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
    1609          142 :         return gfc_walk_alloc_comps (decl, NULL_TREE,
    1610           71 :                                      OMP_CLAUSE_DECL (clause),
    1611           71 :                                      WALK_ALLOC_COMPS_DTOR);
    1612              :       return NULL_TREE;
    1613              :     }
    1614              : 
    1615          720 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1616              :     {
    1617              :       /* Allocatable arrays in FIRSTPRIVATE/LASTPRIVATE etc. clauses need
    1618              :          to be deallocated if they were allocated.  */
    1619          390 :       tem = gfc_conv_descriptor_data_get (decl);
    1620          390 :       tem = gfc_deallocate_with_status (tem, NULL_TREE, NULL_TREE, NULL_TREE,
    1621              :                                         NULL_TREE, true, NULL,
    1622              :                                         GFC_CAF_COARRAY_NOCOARRAY);
    1623              :     }
    1624              :   else
    1625          330 :     tem = gfc_call_free (decl);
    1626          720 :   tem = gfc_omp_unshare_expr (tem);
    1627              : 
    1628          720 :   if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
    1629              :     {
    1630           86 :       stmtblock_t block;
    1631           86 :       tree then_b;
    1632              : 
    1633           86 :       gfc_init_block (&block);
    1634          172 :       gfc_add_expr_to_block (&block,
    1635              :                              gfc_walk_alloc_comps (decl, NULL_TREE,
    1636           86 :                                                    OMP_CLAUSE_DECL (clause),
    1637              :                                                    WALK_ALLOC_COMPS_DTOR));
    1638           86 :       gfc_add_expr_to_block (&block, tem);
    1639           86 :       then_b = gfc_finish_block (&block);
    1640              : 
    1641           86 :       tem = fold_convert (pvoid_type_node,
    1642              :                           GFC_DESCRIPTOR_TYPE_P (type)
    1643              :                           ? gfc_conv_descriptor_data_get (decl) : decl);
    1644           86 :       tem = unshare_expr (tem);
    1645           86 :       tree cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1646              :                                    tem, null_pointer_node);
    1647           86 :       tem = build3_loc (input_location, COND_EXPR, void_type_node, cond,
    1648              :                         then_b, build_empty_stmt (input_location));
    1649              :     }
    1650              :   return tem;
    1651              : }
    1652              : 
    1653              : /* Build a conditional expression in BLOCK.  If COND_VAL is not
    1654              :    null, then the block THEN_B is executed, otherwise ELSE_VAL
    1655              :    is assigned to VAL.  */
    1656              : 
    1657              : static void
    1658         1026 : gfc_build_cond_assign (stmtblock_t *block, tree val, tree cond_val,
    1659              :                        tree then_b, tree else_val)
    1660              : {
    1661         1026 :   stmtblock_t cond_block;
    1662         1026 :   tree else_b = NULL_TREE;
    1663         1026 :   tree val_ty = TREE_TYPE (val);
    1664              : 
    1665         1026 :   if (else_val)
    1666              :     {
    1667         1026 :       gfc_init_block (&cond_block);
    1668         1026 :       gfc_add_modify (&cond_block, val, fold_convert (val_ty, else_val));
    1669         1026 :       else_b = gfc_finish_block (&cond_block);
    1670              :     }
    1671         1026 :   gfc_add_expr_to_block (block,
    1672              :                          build3_loc (input_location, COND_EXPR, void_type_node,
    1673              :                                      cond_val, then_b, else_b));
    1674         1026 : }
    1675              : 
    1676              : /* Build a conditional expression in BLOCK, returning a temporary
    1677              :    variable containing the result.  If COND_VAL is not null, then
    1678              :    THEN_VAL will be assigned to the variable, otherwise ELSE_VAL
    1679              :    is assigned.
    1680              :  */
    1681              : 
    1682              : static tree
    1683         1025 : gfc_build_cond_assign_expr (stmtblock_t *block, tree cond_val,
    1684              :                             tree then_val, tree else_val)
    1685              : {
    1686         1025 :   tree val;
    1687         1025 :   tree val_ty = TREE_TYPE (then_val);
    1688         1025 :   stmtblock_t cond_block;
    1689              : 
    1690         1025 :   val = create_tmp_var (val_ty);
    1691              : 
    1692         1025 :   gfc_init_block (&cond_block);
    1693         1025 :   gfc_add_modify (&cond_block, val, then_val);
    1694         1025 :   tree then_b = gfc_finish_block (&cond_block);
    1695              : 
    1696         1025 :   gfc_build_cond_assign (block, val, cond_val, then_b, else_val);
    1697              : 
    1698         1025 :   return val;
    1699              : }
    1700              : 
    1701              : void
    1702        30379 : gfc_omp_finish_clause (tree c, gimple_seq *pre_p, bool openacc)
    1703              : {
    1704        30379 :   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
    1705              :     return;
    1706              : 
    1707         7254 :   tree decl = OMP_CLAUSE_DECL (c);
    1708         7254 :   location_t loc = OMP_CLAUSE_LOCATION (c);
    1709              : 
    1710              :   /* Assumed-size arrays can't be mapped implicitly, they have to be
    1711              :      mapped explicitly using array sections.  */
    1712         7254 :   if (TREE_CODE (decl) == PARM_DECL
    1713         1041 :       && GFC_ARRAY_TYPE_P (TREE_TYPE (decl))
    1714          371 :       && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (decl)) == GFC_ARRAY_UNKNOWN
    1715         7625 :       && GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (decl),
    1716              :                                 GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)) - 1)
    1717              :          == NULL)
    1718              :     {
    1719            1 :       error_at (OMP_CLAUSE_LOCATION (c),
    1720              :                 "implicit mapping of assumed size array %qD", decl);
    1721            1 :       return;
    1722              :     }
    1723              : 
    1724         7253 :   tree c2 = NULL_TREE, c3 = NULL_TREE, c4 = NULL_TREE;
    1725         7253 :   tree present = gfc_omp_check_optional_argument (decl, true);
    1726         7253 :   tree orig_decl = NULL_TREE;
    1727         7253 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
    1728              :     {
    1729         1282 :       if (!gfc_omp_privatize_by_reference (decl)
    1730          156 :           && !GFC_DECL_GET_SCALAR_POINTER (decl)
    1731           93 :           && !GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
    1732            3 :           && !GFC_DECL_CRAY_POINTEE (decl)
    1733         1285 :           && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
    1734              :         return;
    1735         1279 :       orig_decl = decl;
    1736              : 
    1737         1279 :       c4 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
    1738         1279 :       OMP_CLAUSE_SET_MAP_KIND (c4, GOMP_MAP_POINTER);
    1739         1279 :       OMP_CLAUSE_DECL (c4) = decl;
    1740         1279 :       OMP_CLAUSE_SIZE (c4) = size_int (0);
    1741         1279 :       decl = build_fold_indirect_ref (decl);
    1742         1279 :       if (present
    1743         1279 :           && (GFC_DECL_GET_SCALAR_POINTER (orig_decl)
    1744          257 :               || GFC_DECL_GET_SCALAR_ALLOCATABLE (orig_decl)))
    1745              :         {
    1746           67 :           c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
    1747           67 :           OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
    1748           67 :           OMP_CLAUSE_DECL (c2) = unshare_expr (decl);
    1749           67 :           OMP_CLAUSE_SIZE (c2) = size_int (0);
    1750              : 
    1751           67 :           stmtblock_t block;
    1752           67 :           gfc_start_block (&block);
    1753           67 :           tree ptr = gfc_build_cond_assign_expr (&block, present,
    1754              :                                                  unshare_expr (decl),
    1755              :                                                  null_pointer_node);
    1756           67 :           gimplify_and_add (gfc_finish_block (&block), pre_p);
    1757           67 :           ptr = build_fold_indirect_ref (ptr);
    1758           67 :           OMP_CLAUSE_DECL (c) = ptr;
    1759           67 :           OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (ptr));
    1760              :         }
    1761              :       else
    1762              :         {
    1763         1212 :           OMP_CLAUSE_DECL (c) = decl;
    1764         1212 :           OMP_CLAUSE_SIZE (c) = NULL_TREE;
    1765              :         }
    1766         1279 :       if (TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE
    1767         1279 :           && (GFC_DECL_GET_SCALAR_POINTER (orig_decl)
    1768          391 :               || GFC_DECL_GET_SCALAR_ALLOCATABLE (orig_decl)))
    1769              :         {
    1770           67 :           c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
    1771           67 :           OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
    1772           67 :           OMP_CLAUSE_DECL (c3) = decl;
    1773           67 :           OMP_CLAUSE_SIZE (c3) = size_int (0);
    1774           67 :           decl = build_fold_indirect_ref (decl);
    1775           67 :           OMP_CLAUSE_DECL (c) = unshare_expr (decl);
    1776              :         }
    1777              :     }
    1778         7250 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
    1779              :     {
    1780         1726 :       stmtblock_t block;
    1781         1726 :       gfc_start_block (&block);
    1782         1726 :       tree type = TREE_TYPE (decl);
    1783         1726 :       tree ptr = gfc_conv_descriptor_data_get (decl);
    1784              : 
    1785              :       /* OpenMP: automatically map pointer targets with the pointer;
    1786              :          hence, always update the descriptor/pointer itself.
    1787              :          NOTE: This also remaps the pointer for allocatable arrays with
    1788              :          'target' attribute which also don't have the 'restrict' qualifier.  */
    1789         1726 :       bool always_modifier = false;
    1790              : 
    1791         1726 :       if (!openacc
    1792         1726 :           && !TYPE_QUALS (TREE_TYPE (ptr)).has (TYPE_QUAL_RESTRICT))
    1793          206 :         always_modifier = true;
    1794              : 
    1795         1726 :       if (present)
    1796           56 :         ptr = gfc_build_cond_assign_expr (&block, present, ptr,
    1797              :                                           null_pointer_node);
    1798         1726 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
    1799         1726 :       ptr = build_fold_indirect_ref (ptr);
    1800         1726 :       OMP_CLAUSE_DECL (c) = ptr;
    1801         1726 :       c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
    1802         1726 :       OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_TO_PSET);
    1803         1726 :       if (present)
    1804              :         {
    1805           56 :           ptr = create_tmp_var (TREE_TYPE (TREE_OPERAND (decl, 0)));
    1806           56 :           gfc_add_modify (&block, ptr, TREE_OPERAND (decl, 0));
    1807              : 
    1808           56 :           OMP_CLAUSE_DECL (c2) = build_fold_indirect_ref (ptr);
    1809              :         }
    1810              :       else
    1811         1670 :         OMP_CLAUSE_DECL (c2) = decl;
    1812         1726 :       OMP_CLAUSE_SIZE (c2) = TYPE_SIZE_UNIT (type);
    1813         1726 :       c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
    1814         3246 :       OMP_CLAUSE_SET_MAP_KIND (c3, always_modifier ? GOMP_MAP_ALWAYS_POINTER
    1815              :                                                    : GOMP_MAP_POINTER);
    1816         1726 :       if (present)
    1817              :         {
    1818           56 :           ptr = gfc_conv_descriptor_data_get (unshare_expr (decl));
    1819           56 :           ptr = gfc_build_addr_expr (NULL, ptr);
    1820           56 :           ptr = gfc_build_cond_assign_expr (&block, present,
    1821              :                                             ptr, null_pointer_node);
    1822           56 :           ptr = build_fold_indirect_ref (ptr);
    1823           56 :           OMP_CLAUSE_DECL (c3) = ptr;
    1824              :         }
    1825              :       else
    1826         1670 :         OMP_CLAUSE_DECL (c3) = gfc_conv_descriptor_data_get (decl);
    1827         1726 :       OMP_CLAUSE_SIZE (c3) = size_int (0);
    1828         1726 :       tree size = create_tmp_var (gfc_array_index_type);
    1829         1726 :       tree elemsz = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    1830         1726 :       elemsz = fold_convert (gfc_array_index_type, elemsz);
    1831              : 
    1832         1726 :       if (orig_decl == NULL_TREE)
    1833         1492 :         orig_decl = decl;
    1834         1726 :       if (!openacc
    1835         1726 :           && gfc_has_alloc_comps (type, orig_decl, true))
    1836              :         {
    1837              :           /* Save array descriptor for use in gfc_omp_deep_mapping{,_p,_cnt};
    1838              :              force evaluate to ensure that it is not gimplified + is a decl.  */
    1839            3 :           gfc_allocate_lang_decl (size);
    1840            3 :           GFC_DECL_SAVED_DESCRIPTOR (size) = orig_decl;
    1841              :         }
    1842         1726 :       enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (type);
    1843         1726 :       if (akind == GFC_ARRAY_ALLOCATABLE
    1844              :           || akind == GFC_ARRAY_POINTER
    1845         1726 :           || akind == GFC_ARRAY_POINTER_CONT
    1846         1726 :           || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
    1847              :           || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
    1848            1 :           || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
    1849              :         {
    1850         1725 :           stmtblock_t cond_block;
    1851         1725 :           tree tem, then_b, else_b, zero, cond;
    1852              : 
    1853         1725 :           int rank = ((akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
    1854              :                        || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
    1855         1725 :                        || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
    1856         1725 :                       ? -1 : GFC_TYPE_ARRAY_RANK (type));
    1857         1725 :           gfc_init_block (&cond_block);
    1858         1725 :           tem = gfc_full_array_size (&cond_block, unshare_expr (decl), rank);
    1859         1725 :           gfc_add_modify (&cond_block, size, tem);
    1860         1725 :           gfc_add_modify (&cond_block, size,
    1861              :                           fold_build2 (MULT_EXPR, gfc_array_index_type,
    1862              :                                        size, elemsz));
    1863         1725 :           then_b = gfc_finish_block (&cond_block);
    1864         1725 :           gfc_init_block (&cond_block);
    1865         1725 :           zero = build_int_cst (gfc_array_index_type, 0);
    1866         1725 :           gfc_add_modify (&cond_block, size, zero);
    1867         1725 :           else_b = gfc_finish_block (&cond_block);
    1868         1725 :           tem = gfc_conv_descriptor_data_get (unshare_expr (decl));
    1869         1725 :           tem = fold_convert (pvoid_type_node, tem);
    1870         1725 :           cond = fold_build2_loc (loc, NE_EXPR,
    1871              :                                   boolean_type_node, tem, null_pointer_node);
    1872         1725 :           if (present)
    1873              :             {
    1874           55 :               cond = fold_build2_loc (loc, TRUTH_ANDIF_EXPR,
    1875              :                                       boolean_type_node, present, cond);
    1876              :             }
    1877         1725 :           gfc_add_expr_to_block (&block, build3_loc (loc, COND_EXPR,
    1878              :                                                      void_type_node, cond,
    1879              :                                                      then_b, else_b));
    1880         1725 :         }
    1881            1 :       else if (present)
    1882              :         {
    1883            1 :           stmtblock_t cond_block;
    1884            1 :           tree then_b;
    1885              : 
    1886            1 :           int rank = ((akind == GFC_ARRAY_ASSUMED_RANK
    1887            1 :                        || akind == GFC_ARRAY_ASSUMED_RANK_CONT)
    1888            1 :                       ? -1 : GFC_TYPE_ARRAY_RANK (type));
    1889            1 :           gfc_init_block (&cond_block);
    1890            1 :           gfc_add_modify (&cond_block, size,
    1891              :                           gfc_full_array_size (&cond_block, unshare_expr (decl),
    1892              :                                                rank));
    1893            1 :           gfc_add_modify (&cond_block, size,
    1894              :                           fold_build2 (MULT_EXPR, gfc_array_index_type,
    1895              :                                        size, elemsz));
    1896            1 :           then_b = gfc_finish_block (&cond_block);
    1897              : 
    1898            1 :           gfc_build_cond_assign (&block, size, present, then_b,
    1899              :                                  build_int_cst (gfc_array_index_type, 0));
    1900              :         }
    1901              :       else
    1902              :         {
    1903            0 :           int rank = ((akind == GFC_ARRAY_ASSUMED_RANK
    1904            0 :                        || akind == GFC_ARRAY_ASSUMED_RANK_CONT)
    1905            0 :                       ? -1 : GFC_TYPE_ARRAY_RANK (type));
    1906            0 :           gfc_add_modify (&block, size,
    1907              :                           gfc_full_array_size (&block, unshare_expr (decl),
    1908              :                                                rank));
    1909            0 :           gfc_add_modify (&block, size,
    1910              :                           fold_build2 (MULT_EXPR, gfc_array_index_type,
    1911              :                                        size, elemsz));
    1912              :         }
    1913         1726 :       OMP_CLAUSE_SIZE (c) = size;
    1914         1726 :       tree stmt = gfc_finish_block (&block);
    1915         1726 :       gimplify_and_add (stmt, pre_p);
    1916              :     }
    1917              :   else
    1918              :     {
    1919         5524 :       if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
    1920              :         {
    1921         1190 :           if (DECL_P (decl))
    1922          212 :             OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
    1923              :           else
    1924              :             {
    1925          978 :               tree type = TREE_TYPE (decl);
    1926          978 :               tree size = TYPE_SIZE_UNIT (type);
    1927              :               /* For variable-length character types, TYPE_SIZE_UNIT is a
    1928              :                  SAVE_EXPR.  Gimplifying the SAVE_EXPR (here or elsewhere)
    1929              :                  resolves it in place, embedding a gimple temporary that
    1930              :                  later causes an ICE in remap_type during inlining because
    1931              :                  the temporary is not in scope (PR101760, PR102314).
    1932              :                  Compute the size from the array domain and element size
    1933              :                  to decouple completely from the type's SAVE_EXPRs.  */
    1934          978 :               if (size
    1935          978 :                   && TREE_CODE (type) == ARRAY_TYPE
    1936          536 :                   && TYPE_DOMAIN (type)
    1937          536 :                   && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
    1938         1514 :                   && !TREE_CONSTANT (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
    1939              :                 {
    1940          316 :                   tree len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    1941          316 :                   tree lb = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
    1942          316 :                   tree eltsz = TYPE_SIZE_UNIT (TREE_TYPE (type));
    1943          316 :                   len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len, lb);
    1944          316 :                   len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len,
    1945              :                                      build_one_cst (TREE_TYPE (len)));
    1946          316 :                   size = fold_build2 (MULT_EXPR, sizetype,
    1947              :                                       fold_convert (sizetype, len),
    1948              :                                       fold_convert (sizetype, eltsz));
    1949              :                 }
    1950          978 :               OMP_CLAUSE_SIZE (c) = size;
    1951              :             }
    1952              :         }
    1953              : 
    1954         5524 :       tree type = TREE_TYPE (decl);
    1955         5524 :       if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
    1956            0 :         type = TREE_TYPE (type);
    1957         5524 :       if (!openacc
    1958         5524 :           && orig_decl != NULL_TREE
    1959         5524 :           && gfc_has_alloc_comps (type, orig_decl, true))
    1960              :         {
    1961              :           /* Save array descriptor for use in gfc_omp_deep_mapping{,_p,_cnt};
    1962              :              force evaluate to ensure that it is not gimplified + is a decl.  */
    1963           19 :           tree size = create_tmp_var (TREE_TYPE (OMP_CLAUSE_SIZE (c)));
    1964           19 :           gfc_allocate_lang_decl (size);
    1965           19 :           GFC_DECL_SAVED_DESCRIPTOR (size) = orig_decl;
    1966           19 :           gimplify_assign (size, OMP_CLAUSE_SIZE (c), pre_p);
    1967           19 :           OMP_CLAUSE_SIZE (c) = size;
    1968              :         }
    1969              :     }
    1970         7250 :   tree last = c;
    1971         7250 :   if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
    1972              :                      NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
    1973            0 :     OMP_CLAUSE_SIZE (c) = size_int (0);
    1974         7250 :   if (c2)
    1975              :     {
    1976         1793 :       OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (last);
    1977         1793 :       OMP_CLAUSE_CHAIN (last) = c2;
    1978         1793 :       last = c2;
    1979              :     }
    1980         7250 :   if (c3)
    1981              :     {
    1982         1793 :       OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (last);
    1983         1793 :       OMP_CLAUSE_CHAIN (last) = c3;
    1984         1793 :       last = c3;
    1985              :     }
    1986         7250 :   if (c4)
    1987              :     {
    1988         1279 :       OMP_CLAUSE_CHAIN (c4) = OMP_CLAUSE_CHAIN (last);
    1989         1279 :       OMP_CLAUSE_CHAIN (last) = c4;
    1990              :     }
    1991              : }
    1992              : 
    1993              : 
    1994              : /* map(<flag>: data [len: <size>])
    1995              :    map(attach: &data [bias: <bias>])
    1996              :    offset += 2; offset_data += 2 */
    1997              : static void
    1998          646 : gfc_omp_deep_mapping_map (tree data, tree size, unsigned HOST_WIDE_INT tkind,
    1999              :                           location_t loc, tree data_array, tree sizes_array,
    2000              :                           tree kinds_array, tree offset_data, tree offset,
    2001              :                           gimple_seq *seq, const gimple *ctx)
    2002              : {
    2003          646 :   tree one = build_int_cst (size_type_node, 1);
    2004              : 
    2005          646 :   STRIP_NOPS (data);
    2006          646 :   if (!POINTER_TYPE_P (TREE_TYPE (data)))
    2007              :     {
    2008          205 :       gcc_assert (TREE_CODE (data) == INDIRECT_REF);
    2009          205 :       data = TREE_OPERAND (data, 0);
    2010              :     }
    2011              : 
    2012              :   /* data_array[offset_data] = data; */
    2013          646 :   tree tmp = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (data_array)),
    2014              :                      unshare_expr (data_array), offset_data,
    2015              :                      NULL_TREE, NULL_TREE);
    2016          646 :   gimplify_assign (tmp, data, seq);
    2017              : 
    2018              :   /* offset_data++ */
    2019          646 :   tmp = build2_loc (loc, PLUS_EXPR, size_type_node, offset_data, one);
    2020          646 :   gimplify_assign (offset_data, tmp, seq);
    2021              : 
    2022              :   /* data_array[offset_data] = &data; */
    2023          646 :   tmp = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (data_array)),
    2024              :                 unshare_expr (data_array),
    2025              :                 offset_data, NULL_TREE, NULL_TREE);
    2026          646 :   gimplify_assign (tmp, build_fold_addr_expr (data), seq);
    2027              : 
    2028              :   /* offset_data++ */
    2029          646 :   tmp = build2_loc (loc, PLUS_EXPR, size_type_node, offset_data, one);
    2030          646 :   gimplify_assign (offset_data, tmp, seq);
    2031              : 
    2032              :   /* sizes_array[offset] = size */
    2033          646 :   tmp = build2_loc (loc, MULT_EXPR, size_type_node,
    2034          646 :                     TYPE_SIZE_UNIT (size_type_node), offset);
    2035          646 :   tmp = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (sizes_array),
    2036              :                     sizes_array, tmp);
    2037          646 :   gimple_seq seq2 = NULL;
    2038          646 :   tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
    2039          646 :   gimple_seq_add_seq (seq, seq2);
    2040          646 :   tmp = build_fold_indirect_ref_loc (loc, tmp);
    2041          646 :   gimplify_assign (tmp, size, seq);
    2042              : 
    2043              :   /* FIXME: tkind |= talign << talign_shift; */
    2044              :   /* kinds_array[offset] = tkind. */
    2045          646 :   tmp = build2_loc (loc, MULT_EXPR, size_type_node,
    2046          646 :                     TYPE_SIZE_UNIT (short_unsigned_type_node), offset);
    2047          646 :   tmp = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (kinds_array),
    2048              :                     kinds_array, tmp);
    2049          646 :   seq2 = NULL;
    2050          646 :   tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
    2051          646 :   gimple_seq_add_seq (seq, seq2);
    2052          646 :   tmp = build_fold_indirect_ref_loc (loc, tmp);
    2053          646 :   gimplify_assign (tmp, build_int_cst (short_unsigned_type_node, tkind), seq);
    2054              : 
    2055              :   /* offset++ */
    2056          646 :   tmp = build2_loc (loc, PLUS_EXPR, size_type_node, offset, one);
    2057          646 :   gimplify_assign (offset, tmp, seq);
    2058              : 
    2059              :   /* sizes_array[offset] = bias (= 0).  */
    2060          646 :   tmp = build2_loc (loc, MULT_EXPR, size_type_node,
    2061          646 :                     TYPE_SIZE_UNIT (size_type_node), offset);
    2062          646 :   tmp = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (sizes_array),
    2063              :                     sizes_array, tmp);
    2064          646 :   seq2 = NULL;
    2065          646 :   tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
    2066          646 :   gimple_seq_add_seq (seq, seq2);
    2067          646 :   tmp = build_fold_indirect_ref_loc (loc, tmp);
    2068          646 :   gimplify_assign (tmp, build_zero_cst (size_type_node), seq);
    2069              : 
    2070          646 :   gcc_assert (gimple_code (ctx) == GIMPLE_OMP_TARGET);
    2071          646 :   tkind = (gimple_omp_target_kind (ctx) == GF_OMP_TARGET_KIND_EXIT_DATA
    2072          646 :            ? GOMP_MAP_DETACH : GOMP_MAP_ATTACH);
    2073              : 
    2074              :   /* kinds_array[offset] = tkind. */
    2075          646 :   tmp = build2_loc (loc, MULT_EXPR, size_type_node,
    2076          646 :                     TYPE_SIZE_UNIT (short_unsigned_type_node), offset);
    2077          646 :   tmp = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (kinds_array),
    2078              :                     kinds_array, tmp);
    2079          646 :   seq2 = NULL;
    2080          646 :   tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
    2081          646 :   gimple_seq_add_seq (seq, seq2);
    2082          646 :   tmp = build_fold_indirect_ref_loc (loc, tmp);
    2083          646 :   gimplify_assign (tmp, build_int_cst (short_unsigned_type_node, tkind), seq);
    2084              : 
    2085              :   /* offset++ */
    2086          646 :   tmp = build2_loc (loc, PLUS_EXPR, size_type_node, offset, one);
    2087          646 :   gimplify_assign (offset, tmp, seq);
    2088          646 : }
    2089              : 
    2090              : static void gfc_omp_deep_mapping_item (bool, bool, bool, location_t, tree,
    2091              :                                        tree *, unsigned HOST_WIDE_INT, tree,
    2092              :                                        tree, tree, tree, tree, tree,
    2093              :                                        gimple_seq *, const gimple *, bool *);
    2094              : 
    2095              : /* Map allocatable components.  */
    2096              : static void
    2097          928 : gfc_omp_deep_mapping_comps (bool is_cnt, location_t loc, tree decl,
    2098              :                             tree *token, unsigned HOST_WIDE_INT tkind,
    2099              :                             tree data_array, tree sizes_array, tree kinds_array,
    2100              :                             tree offset_data, tree offset, tree num,
    2101              :                             gimple_seq *seq, const gimple *ctx,
    2102              :                             bool *poly_warned)
    2103              : {
    2104          928 :   tree type = TREE_TYPE (decl);
    2105          928 :   if (TREE_CODE (type) != RECORD_TYPE)
    2106              :     return;
    2107         2566 :   for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    2108              :     {
    2109         1642 :       type = TREE_TYPE (field);
    2110         1642 :       if (gfc_is_polymorphic_nonptr (type)
    2111         1440 :           || GFC_DECL_GET_SCALAR_ALLOCATABLE (field)
    2112         2804 :           || (GFC_DESCRIPTOR_TYPE_P (type)
    2113          772 :               && GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE))
    2114              :         {
    2115         1252 :           tree tmp = fold_build3_loc (loc, COMPONENT_REF, TREE_TYPE (field),
    2116              :                                       decl, field, NULL_TREE);
    2117         1252 :           gfc_omp_deep_mapping_item (is_cnt, true, true, loc, tmp, token,
    2118              :                                      tkind, data_array, sizes_array,
    2119              :                                      kinds_array, offset_data, offset, num,
    2120              :                                      seq, ctx, poly_warned);
    2121              :         }
    2122          390 :       else if (GFC_DECL_GET_SCALAR_POINTER (field)
    2123          390 :                || GFC_DESCRIPTOR_TYPE_P (type))
    2124            0 :         continue;
    2125          390 :       else if (gfc_has_alloc_comps (TREE_TYPE (field), field, false))
    2126              :         {
    2127          104 :           tree tmp = fold_build3_loc (loc, COMPONENT_REF, TREE_TYPE (field),
    2128              :                                       decl, field, NULL_TREE);
    2129          104 :           if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
    2130           40 :             gfc_omp_deep_mapping_item (is_cnt, false, false, loc, tmp,
    2131              :                                        token, tkind, data_array, sizes_array,
    2132              :                                        kinds_array, offset_data, offset, num,
    2133              :                                        seq, ctx, poly_warned);
    2134              :           else
    2135           64 :             gfc_omp_deep_mapping_comps (is_cnt, loc, tmp, token, tkind,
    2136              :                                         data_array, sizes_array, kinds_array,
    2137              :                                         offset_data, offset, num, seq, ctx,
    2138              :                                         poly_warned);
    2139              :         }
    2140              :     }
    2141              : }
    2142              : 
    2143              : static void
    2144          945 : gfc_omp_gen_simple_loop (tree var, tree begin, tree end, enum tree_code cond,
    2145              :                          tree step, location_t loc, gimple_seq *seq1,
    2146              :                          gimple_seq *seq2)
    2147              : {
    2148          945 :   tree tmp;
    2149              : 
    2150              :   /* var = begin. */
    2151          945 :   gimplify_assign (var, begin, seq1);
    2152              : 
    2153              :   /* Loop: for (var = begin; var <cond> end; var += step).  */
    2154          945 :   tree label_loop = create_artificial_label (loc);
    2155          945 :   tree label_cond = create_artificial_label (loc);
    2156              : 
    2157          945 :   gimplify_and_add (fold_build1_loc (loc, GOTO_EXPR, void_type_node,
    2158              :                                      label_cond), seq1);
    2159          945 :   gimple_seq_add_stmt (seq1, gimple_build_label (label_loop));
    2160              : 
    2161              :   /* Everything above is seq1; place loop body here.  */
    2162              : 
    2163              :   /* End of loop body -> put into seq2.  */
    2164          945 :   tmp = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (var), var, step);
    2165          945 :   gimplify_assign (var, tmp, seq2);
    2166          945 :   gimple_seq_add_stmt (seq2, gimple_build_label (label_cond));
    2167          945 :   tmp = fold_build2_loc (loc, cond, boolean_type_node, var, end);
    2168          945 :   tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
    2169              :                   build_empty_stmt (loc));
    2170          945 :   gimplify_and_add (tmp, seq2);
    2171          945 : }
    2172              : 
    2173              : /* Return size variable with the size of an array.  */
    2174              : static tree
    2175          605 : gfc_omp_get_array_size (location_t loc, tree desc, gimple_seq *seq)
    2176              : {
    2177          605 :   tree tmp;
    2178          605 :   gimple_seq seq1 = NULL, seq2 = NULL;
    2179          605 :   tree size = build_decl (loc, VAR_DECL, create_tmp_var_name ("size"),
    2180              :                           size_type_node);
    2181          605 :   tree extent = build_decl (loc, VAR_DECL, create_tmp_var_name ("extent"),
    2182              :                             gfc_array_index_type);
    2183          605 :   tree idx = build_decl (loc, VAR_DECL, create_tmp_var_name ("idx"),
    2184              :                          gfc_array_dim_rank_type);
    2185              : 
    2186          605 :   tree begin = gfc_rank_cst[0];
    2187          605 :   tree end;
    2188          605 :   if (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (desc)) == GFC_ARRAY_ASSUMED_SHAPE_CONT
    2189          605 :       || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (desc)) == GFC_ARRAY_ASSUMED_SHAPE)
    2190            8 :     end = gfc_conv_descriptor_rank_get (desc);
    2191              :   else
    2192          597 :     end = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))];
    2193          605 :   tree step = gfc_rank_cst[1];
    2194              : 
    2195              :   /* size = 0
    2196              :      for (idx = 0; idx < rank; idx++)
    2197              :        extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1
    2198              :        if (extent < 0) extent = 0
    2199              :          size *= extent.  */
    2200          605 :   gimplify_assign (size, build_int_cst (size_type_node, 1), seq);
    2201              : 
    2202          605 :   gfc_omp_gen_simple_loop (idx, begin, end, LT_EXPR, step, loc, &seq1, &seq2);
    2203          605 :   gimple_seq_add_seq (seq, seq1);
    2204              : 
    2205          605 :   tmp = fold_build2_loc (loc, MINUS_EXPR, gfc_array_index_type,
    2206              :                          gfc_conv_descriptor_ubound_get (desc, idx),
    2207              :                          gfc_conv_descriptor_lbound_get (desc, idx));
    2208          605 :   tmp = fold_build2_loc (loc, PLUS_EXPR, gfc_array_index_type,
    2209              :                          tmp, gfc_index_one_node);
    2210          605 :   gimplify_assign (extent, tmp, seq);
    2211          605 :   tmp = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
    2212              :                          extent, gfc_index_zero_node);
    2213          605 :   tmp = build3_v (COND_EXPR, tmp,
    2214              :                   fold_build2_loc (loc, MODIFY_EXPR,
    2215              :                                    gfc_array_index_type,
    2216              :                                    extent, gfc_index_zero_node),
    2217              :                   build_empty_stmt (loc));
    2218          605 :   gimplify_and_add (tmp, seq);
    2219              :   /* size *= extent.  */
    2220          605 :   gimplify_assign (size, fold_build2_loc (loc, MULT_EXPR, size_type_node, size,
    2221              :                                           fold_convert (size_type_node,
    2222              :                                                         extent)), seq);
    2223          605 :   gimple_seq_add_seq (seq, seq2);
    2224          605 :   return size;
    2225              : }
    2226              : 
    2227              : /* Generate loop to access every array element; takes addr of first element
    2228              :    (decl's data comp); returns loop code in seq1 + seq2
    2229              :    and the pointer to the element as return value.  */
    2230              : static tree
    2231          340 : gfc_omp_elmental_loop (location_t loc, tree decl, tree size, tree elem_len,
    2232              :                        gimple_seq *seq1, gimple_seq *seq2)
    2233              : {
    2234          340 :   tree idx = build_decl (loc, VAR_DECL, create_tmp_var_name ("idx"),
    2235              :                          size_type_node);
    2236          340 :   tree begin = build_zero_cst (size_type_node);
    2237          340 :   tree end = size;
    2238          340 :   tree step = build_int_cst (size_type_node, 1);
    2239          340 :   tree ptr;
    2240              : 
    2241          340 :   gfc_omp_gen_simple_loop (idx, begin, end, LT_EXPR, step, loc, seq1, seq2);
    2242              : 
    2243          340 :   tree type = TREE_TYPE (decl);
    2244          340 :   if (POINTER_TYPE_P (type))
    2245              :     {
    2246          296 :       type = TREE_TYPE (type);
    2247          296 :       gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
    2248          296 :       decl = fold_convert (build_pointer_type (TREE_TYPE (type)), decl);
    2249              :     }
    2250              :   else
    2251              :     {
    2252           44 :       gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
    2253           44 :       decl = build_fold_addr_expr_loc (loc, decl);
    2254              :     }
    2255          340 :   decl = fold_convert (build_pointer_type (TREE_TYPE (type)), decl);
    2256          340 :   tree tmp = build2_loc (loc, MULT_EXPR, size_type_node, idx,
    2257              :                          fold_convert (size_type_node, elem_len));
    2258          340 :   ptr = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (decl), decl, tmp);
    2259          340 :   gimple_seq seq3 = NULL;
    2260          340 :   ptr = force_gimple_operand (ptr, &seq3, true, NULL_TREE);
    2261          340 :   gimple_seq_add_seq (seq1, seq3);
    2262              : 
    2263          340 :   return ptr;
    2264              : }
    2265              : 
    2266              : 
    2267              : /* If do_copy, copy data pointer and vptr (if applicable) as well.
    2268              :    Otherwise, only handle allocatable components.
    2269              :    do_copy == false can happen only with nonpolymorphic arguments
    2270              :    to a copy clause.
    2271              :    if (is_cnt) token ... offset is ignored and num is used, otherwise
    2272              :    num is NULL_TREE and unused.  */
    2273              : 
    2274              : static void
    2275         1700 : gfc_omp_deep_mapping_item (bool is_cnt, bool do_copy, bool do_alloc_check,
    2276              :                            location_t loc, tree decl, tree *token,
    2277              :                            unsigned HOST_WIDE_INT tkind, tree data_array,
    2278              :                            tree sizes_array, tree kinds_array, tree offset_data,
    2279              :                            tree offset, tree num, gimple_seq *seq,
    2280              :                            const gimple *ctx, bool *poly_warned)
    2281              : {
    2282         1700 :   tree tmp;
    2283         1700 :   tree type = TREE_TYPE (decl);
    2284         1700 :   if (POINTER_TYPE_P (type))
    2285          416 :     type = TREE_TYPE (type);
    2286         1700 :   tree end_label = NULL_TREE;
    2287         1700 :   tree size = NULL_TREE, elem_len = NULL_TREE;
    2288              : 
    2289         1700 :   bool poly = gfc_is_polymorphic_nonptr (type);
    2290         1700 :   if (poly && is_cnt && !*poly_warned)
    2291              :     {
    2292           41 :       if (gfc_is_unlimited_polymorphic_nonptr (type))
    2293            2 :         error_at (loc,
    2294              :                   "Mapping of unlimited polymorphic list item %qD is "
    2295              :                   "unspecified behavior and unsupported", decl);
    2296              : 
    2297              :       else
    2298           39 :         warning_at (loc, OPT_Wopenmp,
    2299              :                     "Mapping of polymorphic list item %qD is "
    2300              :                     "unspecified behavior", decl);
    2301           41 :       *poly_warned = true;
    2302              :     }
    2303         1700 :   if (do_alloc_check)
    2304              :     {
    2305         1430 :       tree then_label = create_artificial_label (loc);
    2306         1430 :       end_label = create_artificial_label (loc);
    2307         1430 :       tmp = decl;
    2308         1430 :       if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE
    2309         1430 :           || (POINTER_TYPE_P (TREE_TYPE (tmp))
    2310          396 :               && (POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))
    2311          396 :                   || GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (tmp))))))
    2312            8 :         tmp = build_fold_indirect_ref_loc (loc, tmp);
    2313         1430 :       if (poly)
    2314          242 :         tmp = gfc_class_data_get (tmp);
    2315         1430 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    2316          906 :         tmp = gfc_conv_descriptor_data_get (tmp);
    2317         1430 :       gimple_seq seq2 = NULL;
    2318         1430 :       tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
    2319         1430 :       gimple_seq_add_seq (seq, seq2);
    2320              : 
    2321         1430 :       gimple_seq_add_stmt (seq,
    2322         1430 :                            gimple_build_cond (NE_EXPR, tmp, null_pointer_node,
    2323              :                                               then_label, end_label));
    2324         1430 :       gimple_seq_add_stmt (seq, gimple_build_label (then_label));
    2325              :     }
    2326         1700 :   tree class_decl = decl;
    2327         1700 :   if (poly)
    2328              :     {
    2329          242 :       decl = gfc_class_data_get (decl);
    2330          242 :       type = TREE_TYPE (decl);
    2331              :     }
    2332         1700 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
    2333              :     {
    2334          548 :       decl = build_fold_indirect_ref (decl);
    2335          548 :       type = TREE_TYPE (decl);
    2336              :     }
    2337              : 
    2338         1700 :   if (is_cnt && do_copy)
    2339              :     {
    2340          646 :       tree tmp = fold_build2_loc (loc, PLUS_EXPR, size_type_node,
    2341              :                                   num, build_int_cst (size_type_node, 1));
    2342          646 :       gimplify_assign (num, tmp, seq);
    2343              :     }
    2344         1054 :   else if (do_copy)
    2345              :     {
    2346              :       /* copy data pointer  */
    2347          646 :       tree bytesize;
    2348          646 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
    2349              :         {
    2350              :           /* TODO: Optimization: Shouldn't this be an expr. const, except for
    2351              :              deferred-length strings. (Cf. also below).  */
    2352          441 :           elem_len = (poly ? gfc_class_vtab_size_get (class_decl)
    2353          386 :                            : gfc_conv_descriptor_elem_len_get (decl));
    2354          882 :           tmp = (POINTER_TYPE_P (TREE_TYPE (decl))
    2355          441 :                  ? build_fold_indirect_ref (decl) : decl);
    2356          441 :           size = gfc_omp_get_array_size (loc, tmp, seq);
    2357          441 :           bytesize = fold_build2_loc (loc, MULT_EXPR, size_type_node,
    2358              :                                       fold_convert (size_type_node, size),
    2359              :                                       fold_convert (size_type_node, elem_len));
    2360          441 :           tmp = gfc_conv_descriptor_data_get (decl);
    2361              :         }
    2362          205 :       else if (poly)
    2363              :         {
    2364           66 :           tmp = decl;
    2365           66 :           bytesize = fold_convert (size_type_node,
    2366              :                                    gfc_class_vtab_size_get (class_decl));
    2367              :         }
    2368              :       else
    2369              :         {
    2370          139 :           tmp = decl;
    2371          139 :           bytesize = TYPE_SIZE_UNIT (TREE_TYPE (decl));
    2372              :         }
    2373          646 :       unsigned HOST_WIDE_INT tkind2 = tkind;
    2374          646 :       if (!is_cnt
    2375          646 :           && (tkind == GOMP_MAP_ALLOC
    2376          618 :               || (tkind == GOMP_MAP_FROM
    2377           60 :                   && (gimple_omp_target_kind (ctx)
    2378              :                       != GF_OMP_TARGET_KIND_EXIT_DATA)))
    2379          690 :           && gfc_omp_replace_alloc_by_to_mapping (TREE_TYPE (decl), decl, true))
    2380           12 :         tkind2 = tkind == GOMP_MAP_ALLOC ? GOMP_MAP_TO : GOMP_MAP_TOFROM;
    2381              : 
    2382          646 :       gfc_omp_deep_mapping_map (tmp, bytesize, tkind2, loc, data_array,
    2383              :                                 sizes_array, kinds_array, offset_data,
    2384              :                                 offset, seq, ctx);
    2385              :     }
    2386              : 
    2387         1700 :   tmp = decl;
    2388         1700 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
    2389            0 :     while (TREE_CODE (tmp) == COMPONENT_REF || TREE_CODE (tmp) == ARRAY_REF)
    2390            0 :       tmp = TREE_OPERAND (tmp, TREE_CODE (tmp) == COMPONENT_REF ? 1 : 0);
    2391         1700 :   if (poly || gfc_has_alloc_comps (type, tmp, true))
    2392              :     {
    2393          864 :       gimple_seq seq2 = NULL;
    2394          864 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
    2395              :         {
    2396          296 :           if (elem_len == NULL_TREE)
    2397              :             {
    2398          164 :               elem_len = gfc_conv_descriptor_elem_len_get (decl);
    2399          164 :               size = fold_convert (size_type_node,
    2400              :                                    gfc_omp_get_array_size (loc, decl, seq));
    2401              :             }
    2402          296 :           decl = gfc_conv_descriptor_data_get (decl);
    2403          296 :           decl = gfc_omp_elmental_loop (loc, decl, size, elem_len, seq, &seq2);
    2404          296 :           decl = build_fold_indirect_ref_loc (loc, decl);
    2405              :         }
    2406          568 :       else if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
    2407              :         {
    2408           44 :           type = TREE_TYPE (tmp);
    2409              :           /* FIXME: PR95868 - for var%str of deferred length, elem_len == 0;
    2410              :              len is stored as var%_str_length, but not in GFC_DECL_STRING_LEN
    2411              :              nor in TYPE_SIZE_UNIT as expression. */
    2412           44 :           elem_len = TYPE_SIZE_UNIT (TREE_TYPE (type));
    2413           44 :           size = fold_convert (size_type_node, GFC_TYPE_ARRAY_SIZE (type));
    2414           44 :           decl = gfc_omp_elmental_loop (loc, decl, size, elem_len, seq, &seq2);
    2415           44 :           decl = build_fold_indirect_ref_loc (loc, decl);
    2416              :         }
    2417          524 :       else if (POINTER_TYPE_P (TREE_TYPE (decl)))
    2418            0 :         decl = build_fold_indirect_ref (decl);
    2419              : 
    2420          864 :       gfc_omp_deep_mapping_comps (is_cnt, loc, decl, token, tkind,
    2421              :                                   data_array, sizes_array, kinds_array,
    2422              :                                   offset_data, offset, num, seq, ctx,
    2423              :                                   poly_warned);
    2424          864 :       gimple_seq_add_seq (seq, seq2);
    2425              :     }
    2426         1700 :   if (end_label)
    2427         1430 :     gimple_seq_add_stmt (seq, gimple_build_label (end_label));
    2428         1700 : }
    2429              : 
    2430              : 
    2431              : /* Which map types to check/handle for deep mapping.  */
    2432              : static bool
    2433        43494 : gfc_omp_deep_map_kind_p (tree clause)
    2434              : {
    2435        43494 :   switch (OMP_CLAUSE_CODE (clause))
    2436              :     {
    2437        39524 :     case OMP_CLAUSE_MAP:
    2438        39524 :       break;
    2439              :     case OMP_CLAUSE_FIRSTPRIVATE:
    2440              :     case OMP_CLAUSE_TO:
    2441              :     case OMP_CLAUSE_FROM:
    2442              :       return true;
    2443            0 :     default:
    2444            0 :       gcc_unreachable ();
    2445              :     }
    2446              : 
    2447        39524 :   switch (OMP_CLAUSE_MAP_KIND (clause))
    2448              :     {
    2449              :     case GOMP_MAP_TO:
    2450              :     case GOMP_MAP_FROM:
    2451              :     case GOMP_MAP_TOFROM:
    2452              :     case GOMP_MAP_ALWAYS_TO:
    2453              :     case GOMP_MAP_ALWAYS_FROM:
    2454              :     case GOMP_MAP_ALWAYS_TOFROM:
    2455              :     case GOMP_MAP_ALWAYS_PRESENT_FROM:
    2456              :     case GOMP_MAP_ALWAYS_PRESENT_TO:
    2457              :     case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
    2458              :     case GOMP_MAP_FIRSTPRIVATE:
    2459              :     case GOMP_MAP_ALLOC:
    2460              :       return true;
    2461              :     case GOMP_MAP_POINTER:
    2462              :     case GOMP_MAP_TO_PSET:
    2463              :     case GOMP_MAP_FORCE_PRESENT:
    2464              :     case GOMP_MAP_DELETE:
    2465              :     case GOMP_MAP_FORCE_DEVICEPTR:
    2466              :     case GOMP_MAP_DEVICE_RESIDENT:
    2467              :     case GOMP_MAP_LINK:
    2468              :     case GOMP_MAP_IF_PRESENT:
    2469              :     case GOMP_MAP_PRESENT_ALLOC:
    2470              :     case GOMP_MAP_PRESENT_FROM:
    2471              :     case GOMP_MAP_PRESENT_TO:
    2472              :     case GOMP_MAP_PRESENT_TOFROM:
    2473              :     case GOMP_MAP_FIRSTPRIVATE_INT:
    2474              :     case GOMP_MAP_USE_DEVICE_PTR:
    2475              :     case GOMP_MAP_ZERO_LEN_ARRAY_SECTION:
    2476              :     case GOMP_MAP_FORCE_ALLOC:
    2477              :     case GOMP_MAP_FORCE_TO:
    2478              :     case GOMP_MAP_FORCE_FROM:
    2479              :     case GOMP_MAP_FORCE_TOFROM:
    2480              :     case GOMP_MAP_USE_DEVICE_PTR_IF_PRESENT:
    2481              :     case GOMP_MAP_STRUCT:
    2482              :     case GOMP_MAP_STRUCT_UNORD:
    2483              :     case GOMP_MAP_ALWAYS_POINTER:
    2484              :     case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
    2485              :     case GOMP_MAP_DELETE_ZERO_LEN_ARRAY_SECTION:
    2486              :     case GOMP_MAP_RELEASE:
    2487              :     case GOMP_MAP_ATTACH:
    2488              :     case GOMP_MAP_DETACH:
    2489              :     case GOMP_MAP_FORCE_DETACH:
    2490              :     case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
    2491              :     case GOMP_MAP_FIRSTPRIVATE_POINTER:
    2492              :     case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
    2493              :     case GOMP_MAP_ATTACH_DETACH:
    2494              :       break;
    2495            0 :     default:
    2496            0 :       gcc_unreachable ();
    2497              :     }
    2498              :   return false;
    2499              : }
    2500              : 
    2501              : /* Three OpenMP deep-mapping lang hooks: gfc_omp_deep_mapping{_p,_cnt,}.  */
    2502              : 
    2503              : /* Common check for gfc_omp_deep_mapping_p and gfc_omp_deep_mapping_do. */
    2504              : 
    2505              : static tree
    2506        95166 : gfc_omp_deep_mapping_int_p (const gimple *ctx, tree clause)
    2507              : {
    2508        95166 :   if (is_gimple_omp_oacc (ctx) || !gfc_omp_deep_map_kind_p (clause))
    2509              :     return NULL_TREE;
    2510        23578 :   tree decl = OMP_CLAUSE_DECL (clause);
    2511        23578 :   if (OMP_CLAUSE_SIZE (clause) != NULL_TREE
    2512        23492 :       && DECL_P (OMP_CLAUSE_SIZE (clause))
    2513         6603 :       && DECL_LANG_SPECIFIC (OMP_CLAUSE_SIZE (clause))
    2514        23880 :       && GFC_DECL_SAVED_DESCRIPTOR (OMP_CLAUSE_SIZE (clause)))
    2515              :     /* Saved decl. */
    2516          302 :     decl = GFC_DECL_SAVED_DESCRIPTOR (OMP_CLAUSE_SIZE (clause));
    2517        23276 :   else if (TREE_CODE (decl) == MEM_REF || TREE_CODE (decl) == INDIRECT_REF)
    2518              :     /* The following can happen for, e.g., class(t) :: var(..)  */
    2519        12518 :     decl = TREE_OPERAND (decl, 0);
    2520        23578 :   if (TREE_CODE (decl) == INDIRECT_REF)
    2521              :     /* The following can happen for, e.g., class(t) :: var(..)  */
    2522          132 :     decl = TREE_OPERAND (decl, 0);
    2523        23578 :   if (DECL_P (decl)
    2524        14689 :       && DECL_LANG_SPECIFIC (decl)
    2525        25734 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
    2526           74 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
    2527              :   /* Handle map(to: var.desc) map([to/from/tofrom:] var.desc.data)
    2528              :      to get proper map kind by skipping to the next item. */
    2529        23578 :   tree tmp = OMP_CLAUSE_CHAIN (clause);
    2530        23578 :   if (tmp != NULL_TREE
    2531        17630 :       && OMP_CLAUSE_CODE (tmp) == OMP_CLAUSE_CODE (clause)
    2532        15899 :       && OMP_CLAUSE_SIZE (tmp) != NULL_TREE
    2533        15899 :       && DECL_P (OMP_CLAUSE_SIZE (tmp))
    2534         1493 :       && DECL_LANG_SPECIFIC (OMP_CLAUSE_SIZE (tmp))
    2535        23653 :       && GFC_DECL_SAVED_DESCRIPTOR (OMP_CLAUSE_SIZE (tmp)) == decl)
    2536              :     return NULL_TREE;
    2537        23578 :   if (DECL_P (decl)
    2538        14689 :       && DECL_LANG_SPECIFIC (decl)
    2539        25689 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
    2540           29 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
    2541        23578 :   tree type = TREE_TYPE (decl);
    2542        23578 :   if (POINTER_TYPE_P (type))
    2543        13452 :     type = TREE_TYPE (type);
    2544        23578 :   if (POINTER_TYPE_P (type))
    2545          140 :     type = TREE_TYPE (type);
    2546        23578 :   tmp = decl;
    2547        25340 :   while (TREE_CODE (tmp) == COMPONENT_REF || TREE_CODE (tmp) == ARRAY_REF)
    2548         2423 :     tmp = TREE_OPERAND (tmp, TREE_CODE (tmp) == COMPONENT_REF ? 1 : 0);
    2549        23578 :   if (!gfc_is_polymorphic_nonptr (type)
    2550        23578 :       && !gfc_has_alloc_comps (type, tmp, true))
    2551        22989 :     return NULL_TREE;
    2552              :   return decl;
    2553              : }
    2554              : 
    2555              : /* Return true if there is any deep mapping required, even if the number of
    2556              :    mappings is known at compile time.  Deep mapping is required if the passed
    2557              :    CLAUSE is a map clause and its OMP_CLAUSE_DECL refers to a derived-type with
    2558              :    allocatable components. CTX is the statement that contains the CLAUSE.  */
    2559              : 
    2560              : bool
    2561        46355 : gfc_omp_deep_mapping_p (const gimple *ctx, tree clause)
    2562              : {
    2563        46355 :   tree decl = gfc_omp_deep_mapping_int_p (ctx, clause);
    2564        46355 :   if (decl == NULL_TREE)
    2565        46210 :     return false;
    2566              :   return true;
    2567              : }
    2568              : 
    2569              : /* Handle gfc_omp_deep_mapping{,_cnt} */
    2570              : static tree
    2571        48787 : gfc_omp_deep_mapping_do (bool is_cnt, const gimple *ctx, tree clause,
    2572              :                          unsigned HOST_WIDE_INT tkind, tree data, tree sizes,
    2573              :                          tree kinds, tree offset_data, tree offset,
    2574              :                          gimple_seq *seq)
    2575              : {
    2576        48787 :   tree num = NULL_TREE;
    2577        48787 :   location_t loc = OMP_CLAUSE_LOCATION (clause);
    2578        48787 :   tree decl = gfc_omp_deep_mapping_int_p (ctx, clause);
    2579        48787 :   bool poly_warned = false;
    2580        48787 :   if (decl == NULL_TREE)
    2581              :     return NULL_TREE;
    2582              :   /* Handle: map(alloc:dt%cmp [len: ptr_size]) map(tofrom: D.0123...),
    2583              :      where GFC_DECL_SAVED_DESCRIPTOR(D.0123) is the same (here: dt%cmp).  */
    2584          420 :   if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
    2585          420 :       && (OMP_CLAUSE_MAP_KIND (clause) == GOMP_MAP_ALLOC
    2586          376 :           || OMP_CLAUSE_MAP_KIND (clause) == GOMP_MAP_PRESENT_ALLOC))
    2587              :     {
    2588              :       tree c = clause;
    2589           84 :       while ((c = OMP_CLAUSE_CHAIN (c)) != NULL_TREE)
    2590              :         {
    2591           60 :           if (!gfc_omp_deep_map_kind_p (c))
    2592           36 :             continue;
    2593           24 :           tree d = gfc_omp_deep_mapping_int_p (ctx, c);
    2594           24 :           if (d != NULL_TREE && operand_equal_p (decl, d, 0))
    2595              :             return NULL_TREE;
    2596              :         }
    2597              :     }
    2598          408 :   tree type = TREE_TYPE (decl);
    2599          408 :   if (POINTER_TYPE_P (type))
    2600          138 :     type = TREE_TYPE (type);
    2601          408 :   if (POINTER_TYPE_P (type))
    2602            8 :     type = TREE_TYPE (type);
    2603          408 :   bool poly = gfc_is_polymorphic_nonptr (type);
    2604              : 
    2605          408 :   if (is_cnt)
    2606              :     {
    2607          204 :       num = build_decl (loc, VAR_DECL,
    2608              :                         create_tmp_var_name ("n_deepmap"), size_type_node);
    2609          204 :       tree tmp = fold_build2_loc (loc, MODIFY_EXPR, size_type_node, num,
    2610              :                                   build_int_cst (size_type_node, 0));
    2611          204 :       gimple_add_tmp_var (num);
    2612          204 :       gimplify_and_add (tmp, seq);
    2613              :     }
    2614              :   else
    2615          204 :     gcc_assert (short_unsigned_type_node == TREE_TYPE (TREE_TYPE (kinds)));
    2616              : 
    2617          408 :   bool do_copy = poly;
    2618          408 :   bool do_alloc_check = false;
    2619          408 :   tree token = NULL_TREE;
    2620          408 :   tree tmp = decl;
    2621          408 :   if (poly)
    2622              :     {
    2623           40 :       tmp = TYPE_FIELDS (type);
    2624           40 :       type = TREE_TYPE (tmp);
    2625              :     }
    2626              :   else
    2627          420 :     while (TREE_CODE (tmp) == COMPONENT_REF || TREE_CODE (tmp) == ARRAY_REF)
    2628           72 :       tmp = TREE_OPERAND (tmp, TREE_CODE (tmp) == COMPONENT_REF ? 1 : 0);
    2629          408 :   if (TREE_CODE (tmp) == MEM_REF)
    2630           16 :     tmp = TREE_OPERAND (tmp, 0);
    2631          408 :   if (TREE_CODE (tmp) == SSA_NAME)
    2632              :     {
    2633           16 :       gimple *def_stmt = SSA_NAME_DEF_STMT (tmp);
    2634           16 :       if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
    2635              :         {
    2636           16 :           tmp = gimple_assign_rhs1 (def_stmt);
    2637           16 :           if (poly)
    2638              :             {
    2639            0 :               tmp = TYPE_FIELDS (type);
    2640            0 :               type = TREE_TYPE (tmp);
    2641              :             }
    2642              :           else
    2643           32 :             while (TREE_CODE (tmp) == COMPONENT_REF
    2644           32 :                    || TREE_CODE (tmp) == ARRAY_REF)
    2645           16 :               tmp = TREE_OPERAND (tmp,
    2646              :                                   TREE_CODE (tmp) == COMPONENT_REF ? 1 : 0);
    2647              :         }
    2648              :     }
    2649              :   /* If the clause argument is nonallocatable, skip is-allocate check. */
    2650          408 :   if (GFC_DECL_GET_SCALAR_ALLOCATABLE (tmp)
    2651          280 :       || GFC_DECL_GET_SCALAR_POINTER (tmp)
    2652          422 :       || (GFC_DESCRIPTOR_TYPE_P (type)
    2653           42 :           && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
    2654           24 :               || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
    2655            8 :               || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)))
    2656              :     do_alloc_check = true;
    2657              : 
    2658          408 :   if (!is_cnt
    2659          204 :       && OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
    2660          200 :       && (tkind == GOMP_MAP_ALLOC
    2661          188 :           || (tkind == GOMP_MAP_FROM
    2662           21 :               && (gimple_omp_target_kind (ctx)
    2663              :                   != GF_OMP_TARGET_KIND_EXIT_DATA)))
    2664          444 :       && (poly || gfc_omp_replace_alloc_by_to_mapping (type, tmp, true)))
    2665           24 :     OMP_CLAUSE_SET_MAP_KIND (clause, tkind == GOMP_MAP_ALLOC ? GOMP_MAP_TO
    2666              :                                                              : GOMP_MAP_TOFROM);
    2667              : 
    2668              :   /* TODO: For map(a(:)), we know it is present & allocated.  */
    2669              : 
    2670          408 :   tree present = (DECL_P (decl) ? gfc_omp_check_optional_argument (decl, true)
    2671              :                                 : NULL_TREE);
    2672          694 :   if (POINTER_TYPE_P (TREE_TYPE (decl))
    2673          424 :       && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
    2674            8 :     decl = build_fold_indirect_ref (decl);
    2675          408 :   if (present)
    2676              :     {
    2677           16 :       tree then_label = create_artificial_label (loc);
    2678           16 :       tree end_label = create_artificial_label (loc);
    2679           16 :       gimple_seq seq2 = NULL;
    2680           16 :       tmp = force_gimple_operand (present, &seq2, true, NULL_TREE);
    2681           16 :       gimple_seq_add_seq (seq, seq2);
    2682           16 :       gimple_seq_add_stmt (seq,
    2683           16 :                            gimple_build_cond_from_tree (present,
    2684              :                                                         then_label, end_label));
    2685           16 :       gimple_seq_add_stmt (seq, gimple_build_label (then_label));
    2686           16 :       gfc_omp_deep_mapping_item (is_cnt, do_copy, do_alloc_check, loc, decl,
    2687              :                                  &token, tkind, data, sizes, kinds,
    2688              :                                  offset_data, offset, num, seq, ctx,
    2689              :                                  &poly_warned);
    2690           16 :       gimple_seq_add_stmt (seq, gimple_build_label (end_label));
    2691              :     }
    2692              :   else
    2693          392 :     gfc_omp_deep_mapping_item (is_cnt, do_copy, do_alloc_check, loc, decl,
    2694              :                                &token, tkind, data, sizes, kinds, offset_data,
    2695              :                                offset, num, seq, ctx, &poly_warned);
    2696              :   /* Multiply by 2 as there are two mappings: data + pointer assign.  */
    2697          408 :   if (is_cnt)
    2698          204 :     gimplify_assign (num,
    2699              :                      fold_build2_loc (loc, MULT_EXPR,
    2700              :                                       size_type_node, num,
    2701              :                                       build_int_cst (size_type_node, 2)), seq);
    2702              :   return num;
    2703              : }
    2704              : 
    2705              : /* Returns NULL_TREE if known that no deep mapping is required for the passed
    2706              :    'map' CLAUSE, otherwise returns a size_type expression with the number of
    2707              :    required data-mapping operations, which may be zero.  Deep mapping is
    2708              :    required for allocatable components of derived types; the number of mapping
    2709              :    operations depends on the allocation status, array sizes and the dynamic
    2710              :    type.  CTX is the gimple statement that contains the map CLAUSE; the
    2711              :    gimple code used for counting is added to SEQ.  */
    2712              : 
    2713              : tree
    2714        48356 : gfc_omp_deep_mapping_cnt (const gimple *ctx, tree clause, gimple_seq *seq)
    2715              : {
    2716        48356 :   return gfc_omp_deep_mapping_do (true, ctx, clause, 0, NULL_TREE, NULL_TREE,
    2717        48356 :                                   NULL_TREE, NULL_TREE, NULL_TREE, seq);
    2718              : }
    2719              : 
    2720              : /* Handle the deep mapping for the passed map CLAUSE that is part of
    2721              :    the gimple statement CTX by walking all allocated allocatable components
    2722              :    and its allocatable components to add additional data-mapping operations.
    2723              :    TKIND is the map-type/kind to be used. The generated code is added to
    2724              :    SEQ – and the actual struct-field address used for mapping, the map size,
    2725              :    and kind value to the arrays DATA, SIZES, and KINDS, respectively.
    2726              :    OFFSET_DATA and OFFSET are size-type variables; the map operations are
    2727              :    added at array index OFFSET_DATA for DATA and at array index OFFSET for
    2728              :    SIZES/KINDS, incrementing the offsets after each assignment.  */
    2729              : 
    2730              : void
    2731          431 : gfc_omp_deep_mapping (const gimple *ctx, tree clause,
    2732              :                       unsigned HOST_WIDE_INT tkind, tree data,
    2733              :                       tree sizes, tree kinds, tree offset_data, tree offset,
    2734              :                       gimple_seq *seq)
    2735              : {
    2736          431 :   (void) gfc_omp_deep_mapping_do (false, ctx, clause, tkind, data, sizes, kinds,
    2737              :                                   offset_data, offset, seq);
    2738          431 : }
    2739              : 
    2740              : /* Return true if DECL is a scalar variable (for the purpose of
    2741              :    implicit firstprivatization/mapping). Only if 'ptr_alloc_ok.'
    2742              :    is true, allocatables and pointers are permitted. */
    2743              : 
    2744              : bool
    2745         4064 : gfc_omp_scalar_p (tree decl, bool ptr_alloc_ok)
    2746              : {
    2747         4064 :   tree type = TREE_TYPE (decl);
    2748              : 
    2749         4064 :   if (gfc_omp_privatize_by_reference (decl))
    2750         1842 :     type = TREE_TYPE (type);
    2751              : 
    2752         4064 :   if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
    2753         4064 :       || GFC_DECL_GET_SCALAR_POINTER (decl))
    2754              :     {
    2755          185 :       if (!ptr_alloc_ok)
    2756              :         return false;
    2757           20 :       if (POINTER_TYPE_P (type) && !VOID_TYPE_P (TREE_TYPE (type)))
    2758           20 :         type = TREE_TYPE (type);
    2759              :       /* Special case: pointer/allocatable dummy argument to an ENTRY
    2760              :          function, but only if not optional - as the optional case is
    2761              :          already handled by 'gfc_omp_privatize_by_reference' returning 'true'. */
    2762           20 :       if (POINTER_TYPE_P (type) && !VOID_TYPE_P (TREE_TYPE (type)))
    2763            4 :         type = TREE_TYPE (type);
    2764              :     }
    2765         3899 :   if (GFC_ARRAY_TYPE_P (type) || GFC_CLASS_TYPE_P (type))
    2766              :     return false;
    2767         3452 :   if ((TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == INTEGER_TYPE)
    2768         6202 :       && TYPE_STRING_FLAG (type))
    2769              :     return false;
    2770         3452 :   if (INTEGRAL_TYPE_P (type)
    2771         3452 :       || SCALAR_FLOAT_TYPE_P (type)
    2772         3452 :       || COMPLEX_FLOAT_TYPE_P (type))
    2773         3020 :     return true;
    2774              :   return false;
    2775              : }
    2776              : 
    2777              : 
    2778              : /* Return true if DECL is a scalar with target attribute but does not have the
    2779              :    allocatable (or pointer) attribute (for the purpose of implicit mapping).  */
    2780              : 
    2781              : bool
    2782         3949 : gfc_omp_scalar_target_p (tree decl)
    2783              : {
    2784         3949 :   return (DECL_P (decl) && GFC_DECL_GET_SCALAR_TARGET (decl)
    2785         4049 :           && gfc_omp_scalar_p (decl, false));
    2786              : }
    2787              : 
    2788              : 
    2789              : /* Return true if DECL's DECL_VALUE_EXPR (if any) should be
    2790              :    disregarded in OpenMP construct, because it is going to be
    2791              :    remapped during OpenMP lowering.  SHARED is true if DECL
    2792              :    is going to be shared, false if it is going to be privatized.  */
    2793              : 
    2794              : bool
    2795      1729450 : gfc_omp_disregard_value_expr (tree decl, bool shared)
    2796              : {
    2797      1729450 :   if (GFC_DECL_COMMON_OR_EQUIV (decl)
    2798      1729450 :       && DECL_HAS_VALUE_EXPR_P (decl))
    2799              :     {
    2800         2958 :       tree value = DECL_VALUE_EXPR (decl);
    2801              : 
    2802         2958 :       if (TREE_CODE (value) == COMPONENT_REF
    2803         2958 :           && VAR_P (TREE_OPERAND (value, 0))
    2804         5916 :           && GFC_DECL_COMMON_OR_EQUIV (TREE_OPERAND (value, 0)))
    2805              :         {
    2806              :           /* If variable in COMMON or EQUIVALENCE is privatized, return
    2807              :              true, as just that variable is supposed to be privatized,
    2808              :              not the whole COMMON or whole EQUIVALENCE.
    2809              :              For shared variables in COMMON or EQUIVALENCE, let them be
    2810              :              gimplified to DECL_VALUE_EXPR, so that for multiple shared vars
    2811              :              from the same COMMON or EQUIVALENCE just one sharing of the
    2812              :              whole COMMON or EQUIVALENCE is enough.  */
    2813         2958 :           return ! shared;
    2814              :         }
    2815              :     }
    2816              : 
    2817      1726492 :   if (GFC_DECL_RESULT (decl) && DECL_HAS_VALUE_EXPR_P (decl))
    2818          334 :     return ! shared;
    2819              : 
    2820              :   return false;
    2821              : }
    2822              : 
    2823              : /* Return true if DECL that is shared iff SHARED is true should
    2824              :    be put into OMP_CLAUSE_PRIVATE with OMP_CLAUSE_PRIVATE_DEBUG
    2825              :    flag set.  */
    2826              : 
    2827              : bool
    2828        40718 : gfc_omp_private_debug_clause (tree decl, bool shared)
    2829              : {
    2830        40718 :   if (GFC_DECL_CRAY_POINTEE (decl))
    2831              :     return true;
    2832              : 
    2833        40682 :   if (GFC_DECL_COMMON_OR_EQUIV (decl)
    2834        40682 :       && DECL_HAS_VALUE_EXPR_P (decl))
    2835              :     {
    2836          326 :       tree value = DECL_VALUE_EXPR (decl);
    2837              : 
    2838          326 :       if (TREE_CODE (value) == COMPONENT_REF
    2839          326 :           && VAR_P (TREE_OPERAND (value, 0))
    2840          652 :           && GFC_DECL_COMMON_OR_EQUIV (TREE_OPERAND (value, 0)))
    2841          326 :         return shared;
    2842              :     }
    2843              : 
    2844              :   return false;
    2845              : }
    2846              : 
    2847              : /* Register language specific type size variables as potentially OpenMP
    2848              :    firstprivate variables.  */
    2849              : 
    2850              : void
    2851        22763 : gfc_omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
    2852              : {
    2853        22763 :   if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
    2854              :     {
    2855         4037 :       int r;
    2856              : 
    2857         4037 :       gcc_assert (TYPE_LANG_SPECIFIC (type) != NULL);
    2858         9294 :       for (r = 0; r < GFC_TYPE_ARRAY_RANK (type); r++)
    2859              :         {
    2860         5257 :           omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_LBOUND (type, r));
    2861         5257 :           omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_UBOUND (type, r));
    2862         5257 :           omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_STRIDE (type, r));
    2863              :         }
    2864         4037 :       omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_SIZE (type));
    2865         4037 :       omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_OFFSET (type));
    2866              :     }
    2867        22763 : }
    2868              : 
    2869              : 
    2870              : static inline tree
    2871        78108 : gfc_trans_add_clause (tree node, tree tail)
    2872              : {
    2873        78108 :   OMP_CLAUSE_CHAIN (node) = tail;
    2874        78108 :   return node;
    2875              : }
    2876              : 
    2877              : static tree
    2878        44406 : gfc_trans_omp_variable (gfc_symbol *sym, bool declare_simd)
    2879              : {
    2880        44406 :   if (declare_simd)
    2881              :     {
    2882          183 :       int cnt = 0;
    2883          183 :       gfc_symbol *proc_sym;
    2884          183 :       gfc_formal_arglist *f;
    2885              : 
    2886          183 :       gcc_assert (sym->attr.dummy);
    2887          183 :       proc_sym = sym->ns->proc_name;
    2888          183 :       if (proc_sym->attr.entry_master)
    2889            0 :         ++cnt;
    2890          183 :       if (gfc_return_by_reference (proc_sym))
    2891              :         {
    2892            0 :           ++cnt;
    2893            0 :           if (proc_sym->ts.type == BT_CHARACTER)
    2894            0 :             ++cnt;
    2895              :         }
    2896          351 :       for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
    2897          351 :         if (f->sym == sym)
    2898              :           break;
    2899          168 :         else if (f->sym)
    2900          168 :           ++cnt;
    2901          183 :       gcc_assert (f);
    2902          183 :       return build_int_cst (integer_type_node, cnt);
    2903              :     }
    2904              : 
    2905        44223 :   tree t = gfc_get_symbol_decl (sym);
    2906        44223 :   tree parent_decl;
    2907        44223 :   int parent_flag;
    2908        44223 :   bool return_value;
    2909        44223 :   bool alternate_entry;
    2910        44223 :   bool entry_master;
    2911              : 
    2912        44223 :   return_value = sym->attr.function && sym->result == sym;
    2913          167 :   alternate_entry = sym->attr.function && sym->attr.entry
    2914        44257 :                     && sym->result == sym;
    2915        88446 :   entry_master = sym->attr.result
    2916          172 :                  && sym->ns->proc_name->attr.entry_master
    2917        44235 :                  && !gfc_return_by_reference (sym->ns->proc_name);
    2918        44223 :   parent_decl = current_function_decl
    2919        44223 :                 ? DECL_CONTEXT (current_function_decl) : NULL_TREE;
    2920              : 
    2921        44223 :   if ((t == parent_decl && return_value)
    2922        44216 :        || (sym->ns && sym->ns->proc_name
    2923        44216 :            && sym->ns->proc_name->backend_decl == parent_decl
    2924         2374 :            && (alternate_entry || entry_master)))
    2925              :     parent_flag = 1;
    2926              :   else
    2927        44214 :     parent_flag = 0;
    2928              : 
    2929              :   /* Special case for assigning the return value of a function.
    2930              :      Self recursive functions must have an explicit return value.  */
    2931        44223 :   if (return_value && (t == current_function_decl || parent_flag))
    2932           97 :     t = gfc_get_fake_result_decl (sym, parent_flag);
    2933              : 
    2934              :   /* Similarly for alternate entry points.  */
    2935        44126 :   else if (alternate_entry
    2936           32 :            && (sym->ns->proc_name->backend_decl == current_function_decl
    2937            0 :                || parent_flag))
    2938              :     {
    2939           32 :       gfc_entry_list *el = NULL;
    2940              : 
    2941           51 :       for (el = sym->ns->entries; el; el = el->next)
    2942           51 :         if (sym == el->sym)
    2943              :           {
    2944           32 :             t = gfc_get_fake_result_decl (sym, parent_flag);
    2945           32 :             break;
    2946              :           }
    2947              :     }
    2948              : 
    2949        44094 :   else if (entry_master
    2950           12 :            && (sym->ns->proc_name->backend_decl == current_function_decl
    2951            0 :                || parent_flag))
    2952           12 :     t = gfc_get_fake_result_decl (sym, parent_flag);
    2953              : 
    2954              :   return t;
    2955              : }
    2956              : 
    2957              : static tree
    2958        11835 : gfc_trans_omp_variable_list (enum omp_clause_code code,
    2959              :                              gfc_omp_namelist *namelist, tree list,
    2960              :                              bool declare_simd)
    2961              : {
    2962              :   /* PARAMETER (named constants) are excluded as OpenACC 3.4 permits them now
    2963              :      as 'var' but permits compilers to ignore them.  In expressions, it should
    2964              :      have been replaced by the value (and this function should not be called
    2965              :      anyway) and for var-using clauses, they should just be skipped.  */
    2966        30561 :   for (; namelist != NULL; namelist = namelist->next)
    2967        18726 :     if ((namelist->sym->attr.referenced || declare_simd)
    2968        18726 :         && namelist->sym->attr.flavor != FL_PARAMETER)
    2969              :       {
    2970        18721 :         tree t = gfc_trans_omp_variable (namelist->sym, declare_simd);
    2971        18721 :         if (t != error_mark_node)
    2972              :           {
    2973        18721 :             tree node;
    2974        18721 :             node = build_omp_clause (input_location, code);
    2975        18721 :             OMP_CLAUSE_DECL (node) = t;
    2976        18721 :             list = gfc_trans_add_clause (node, list);
    2977              : 
    2978        18721 :             if (code == OMP_CLAUSE_LASTPRIVATE
    2979         2903 :                 && namelist->u.lastprivate_conditional)
    2980          120 :               OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (node) = 1;
    2981              :           }
    2982              :       }
    2983        11835 :   return list;
    2984              : }
    2985              : 
    2986              : struct omp_udr_find_orig_data
    2987              : {
    2988              :   gfc_omp_udr *omp_udr;
    2989              :   bool omp_orig_seen;
    2990              : };
    2991              : 
    2992              : static int
    2993          678 : omp_udr_find_orig (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
    2994              :                    void *data)
    2995              : {
    2996          678 :   struct omp_udr_find_orig_data *cd = (struct omp_udr_find_orig_data *) data;
    2997          678 :   if ((*e)->expr_type == EXPR_VARIABLE
    2998          366 :       && (*e)->symtree->n.sym == cd->omp_udr->omp_orig)
    2999           72 :     cd->omp_orig_seen = true;
    3000              : 
    3001          678 :   return 0;
    3002              : }
    3003              : 
    3004              : static void
    3005          687 : gfc_trans_omp_array_reduction_or_udr (tree c, gfc_omp_namelist *n, locus where)
    3006              : {
    3007          687 :   gfc_symbol *sym = n->sym;
    3008          687 :   gfc_symtree *root1 = NULL, *root2 = NULL, *root3 = NULL, *root4 = NULL;
    3009          687 :   gfc_symtree *symtree1, *symtree2, *symtree3, *symtree4 = NULL;
    3010          687 :   gfc_symbol init_val_sym, outer_sym, intrinsic_sym;
    3011          687 :   gfc_symbol omp_var_copy[4];
    3012          687 :   gfc_expr *e1, *e2, *e3, *e4;
    3013          687 :   gfc_ref *ref;
    3014          687 :   tree decl, backend_decl, stmt, type, outer_decl;
    3015          687 :   locus old_loc = gfc_current_locus;
    3016          687 :   const char *iname;
    3017          687 :   bool t;
    3018          687 :   gfc_omp_udr *udr = n->u2.udr ? n->u2.udr->udr : NULL;
    3019          687 :   gfc_namespace *old_ns = gfc_current_ns;
    3020              : 
    3021          687 :   if (gfc_current_ns->proc_name
    3022          687 :       && gfc_current_ns->proc_name->ns != gfc_current_ns)
    3023           42 :     gfc_current_ns = gfc_current_ns->proc_name->ns;
    3024              : 
    3025          687 :   decl = OMP_CLAUSE_DECL (c);
    3026          687 :   gfc_current_locus = where;
    3027          687 :   type = TREE_TYPE (decl);
    3028          687 :   outer_decl = create_tmp_var_raw (type);
    3029          687 :   if (TREE_CODE (decl) == PARM_DECL
    3030           31 :       && TREE_CODE (type) == REFERENCE_TYPE
    3031           12 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type))
    3032          699 :       && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (type)) == GFC_ARRAY_ALLOCATABLE)
    3033              :     {
    3034           12 :       decl = build_fold_indirect_ref (decl);
    3035           12 :       type = TREE_TYPE (type);
    3036              :     }
    3037              : 
    3038              :   /* Create a fake symbol for init value.  */
    3039          687 :   memset (&init_val_sym, 0, sizeof (init_val_sym));
    3040          687 :   init_val_sym.ns = sym->ns;
    3041          687 :   init_val_sym.name = sym->name;
    3042          687 :   init_val_sym.ts = sym->ts;
    3043          687 :   init_val_sym.attr.referenced = 1;
    3044          687 :   init_val_sym.declared_at = where;
    3045          687 :   init_val_sym.attr.flavor = FL_VARIABLE;
    3046          687 :   if (OMP_CLAUSE_REDUCTION_CODE (c) != ERROR_MARK)
    3047          284 :     backend_decl = omp_reduction_init (c, gfc_sym_type (&init_val_sym));
    3048          403 :   else if (udr->initializer_ns)
    3049              :     backend_decl = NULL;
    3050              :   else
    3051          133 :     switch (sym->ts.type)
    3052              :       {
    3053           16 :       case BT_LOGICAL:
    3054           16 :       case BT_INTEGER:
    3055           16 :       case BT_REAL:
    3056           16 :       case BT_COMPLEX:
    3057           16 :         backend_decl = build_zero_cst (gfc_sym_type (&init_val_sym));
    3058           16 :         break;
    3059              :       default:
    3060              :         backend_decl = NULL_TREE;
    3061              :         break;
    3062              :       }
    3063          687 :   init_val_sym.backend_decl = backend_decl;
    3064              : 
    3065              :   /* Create a fake symbol for the outer array reference.  */
    3066          687 :   outer_sym = *sym;
    3067          687 :   if (sym->as)
    3068          426 :     outer_sym.as = gfc_copy_array_spec (sym->as);
    3069          687 :   outer_sym.attr.dummy = 0;
    3070          687 :   outer_sym.attr.result = 0;
    3071          687 :   outer_sym.attr.flavor = FL_VARIABLE;
    3072          687 :   outer_sym.backend_decl = outer_decl;
    3073          687 :   if (decl != OMP_CLAUSE_DECL (c))
    3074           12 :     outer_sym.backend_decl = build_fold_indirect_ref (outer_decl);
    3075              : 
    3076              :   /* Create fake symtrees for it.  */
    3077          687 :   symtree1 = gfc_new_symtree (&root1, sym->name);
    3078          687 :   symtree1->n.sym = sym;
    3079          687 :   gcc_assert (symtree1 == root1);
    3080              : 
    3081          687 :   symtree2 = gfc_new_symtree (&root2, sym->name);
    3082          687 :   symtree2->n.sym = &init_val_sym;
    3083          687 :   gcc_assert (symtree2 == root2);
    3084              : 
    3085          687 :   symtree3 = gfc_new_symtree (&root3, sym->name);
    3086          687 :   symtree3->n.sym = &outer_sym;
    3087          687 :   gcc_assert (symtree3 == root3);
    3088              : 
    3089          687 :   memset (omp_var_copy, 0, sizeof omp_var_copy);
    3090          687 :   if (udr)
    3091              :     {
    3092          403 :       omp_var_copy[0] = *udr->omp_out;
    3093          403 :       omp_var_copy[1] = *udr->omp_in;
    3094          403 :       *udr->omp_out = outer_sym;
    3095          403 :       *udr->omp_in = *sym;
    3096          403 :       if (udr->initializer_ns)
    3097              :         {
    3098          270 :           omp_var_copy[2] = *udr->omp_priv;
    3099          270 :           omp_var_copy[3] = *udr->omp_orig;
    3100          270 :           *udr->omp_priv = *sym;
    3101          270 :           *udr->omp_orig = outer_sym;
    3102              :         }
    3103              :     }
    3104              : 
    3105              :   /* Create expressions.  */
    3106          687 :   e1 = gfc_get_expr ();
    3107          687 :   e1->expr_type = EXPR_VARIABLE;
    3108          687 :   e1->where = where;
    3109          687 :   e1->symtree = symtree1;
    3110          687 :   e1->ts = sym->ts;
    3111          687 :   if (sym->attr.dimension)
    3112              :     {
    3113          426 :       e1->ref = ref = gfc_get_ref ();
    3114          426 :       ref->type = REF_ARRAY;
    3115          426 :       ref->u.ar.where = where;
    3116          426 :       ref->u.ar.as = sym->as;
    3117          426 :       ref->u.ar.type = AR_FULL;
    3118          426 :       ref->u.ar.dimen = 0;
    3119              :     }
    3120          687 :   t = gfc_resolve_expr (e1);
    3121          687 :   gcc_assert (t);
    3122              : 
    3123          687 :   e2 = NULL;
    3124          687 :   if (backend_decl != NULL_TREE)
    3125              :     {
    3126          300 :       e2 = gfc_get_expr ();
    3127          300 :       e2->expr_type = EXPR_VARIABLE;
    3128          300 :       e2->where = where;
    3129          300 :       e2->symtree = symtree2;
    3130          300 :       e2->ts = sym->ts;
    3131          300 :       t = gfc_resolve_expr (e2);
    3132          300 :       gcc_assert (t);
    3133              :     }
    3134          387 :   else if (udr->initializer_ns == NULL)
    3135              :     {
    3136          117 :       gcc_assert (sym->ts.type == BT_DERIVED);
    3137          117 :       e2 = gfc_default_initializer (&sym->ts);
    3138          117 :       gcc_assert (e2);
    3139          117 :       t = gfc_resolve_expr (e2);
    3140          117 :       gcc_assert (t);
    3141              :     }
    3142          270 :   else if (n->u2.udr->initializer->op == EXEC_ASSIGN)
    3143              :     {
    3144          204 :       e2 = gfc_copy_expr (n->u2.udr->initializer->expr2);
    3145          204 :       t = gfc_resolve_expr (e2);
    3146          204 :       gcc_assert (t);
    3147              :     }
    3148          687 :   if (udr && udr->initializer_ns)
    3149              :     {
    3150          270 :       struct omp_udr_find_orig_data cd;
    3151          270 :       cd.omp_udr = udr;
    3152          270 :       cd.omp_orig_seen = false;
    3153          270 :       gfc_code_walker (&n->u2.udr->initializer,
    3154              :                        gfc_dummy_code_callback, omp_udr_find_orig, &cd);
    3155          270 :       if (cd.omp_orig_seen)
    3156           72 :         OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
    3157              :     }
    3158              : 
    3159          687 :   e3 = gfc_copy_expr (e1);
    3160          687 :   e3->symtree = symtree3;
    3161          687 :   t = gfc_resolve_expr (e3);
    3162          687 :   gcc_assert (t);
    3163              : 
    3164          687 :   iname = NULL;
    3165          687 :   e4 = NULL;
    3166          687 :   switch (OMP_CLAUSE_REDUCTION_CODE (c))
    3167              :     {
    3168          160 :     case PLUS_EXPR:
    3169          160 :     case MINUS_EXPR:
    3170          160 :       e4 = gfc_add (e3, e1);
    3171          160 :       break;
    3172           26 :     case MULT_EXPR:
    3173           26 :       e4 = gfc_multiply (e3, e1);
    3174           26 :       break;
    3175            6 :     case TRUTH_ANDIF_EXPR:
    3176            6 :       e4 = gfc_and (e3, e1);
    3177            6 :       break;
    3178            6 :     case TRUTH_ORIF_EXPR:
    3179            6 :       e4 = gfc_or (e3, e1);
    3180            6 :       break;
    3181            6 :     case EQ_EXPR:
    3182            6 :       e4 = gfc_eqv (e3, e1);
    3183            6 :       break;
    3184            6 :     case NE_EXPR:
    3185            6 :       e4 = gfc_neqv (e3, e1);
    3186            6 :       break;
    3187              :     case MIN_EXPR:
    3188              :       iname = "min";
    3189              :       break;
    3190              :     case MAX_EXPR:
    3191              :       iname = "max";
    3192              :       break;
    3193              :     case BIT_AND_EXPR:
    3194              :       iname = "iand";
    3195              :       break;
    3196              :     case BIT_IOR_EXPR:
    3197              :       iname = "ior";
    3198              :       break;
    3199              :     case BIT_XOR_EXPR:
    3200              :       iname = "ieor";
    3201              :       break;
    3202          403 :     case ERROR_MARK:
    3203          403 :       if (n->u2.udr->combiner->op == EXEC_ASSIGN)
    3204              :         {
    3205          337 :           gfc_free_expr (e3);
    3206          337 :           e3 = gfc_copy_expr (n->u2.udr->combiner->expr1);
    3207          337 :           e4 = gfc_copy_expr (n->u2.udr->combiner->expr2);
    3208          337 :           t = gfc_resolve_expr (e3);
    3209          337 :           gcc_assert (t);
    3210          337 :           t = gfc_resolve_expr (e4);
    3211          337 :           gcc_assert (t);
    3212              :         }
    3213              :       break;
    3214            0 :     default:
    3215            0 :       gcc_unreachable ();
    3216              :     }
    3217          210 :   if (iname != NULL)
    3218              :     {
    3219           74 :       memset (&intrinsic_sym, 0, sizeof (intrinsic_sym));
    3220           74 :       intrinsic_sym.ns = sym->ns;
    3221           74 :       intrinsic_sym.name = iname;
    3222           74 :       intrinsic_sym.ts = sym->ts;
    3223           74 :       intrinsic_sym.attr.referenced = 1;
    3224           74 :       intrinsic_sym.attr.intrinsic = 1;
    3225           74 :       intrinsic_sym.attr.function = 1;
    3226           74 :       intrinsic_sym.attr.implicit_type = 1;
    3227           74 :       intrinsic_sym.result = &intrinsic_sym;
    3228           74 :       intrinsic_sym.declared_at = where;
    3229              : 
    3230           74 :       symtree4 = gfc_new_symtree (&root4, iname);
    3231           74 :       symtree4->n.sym = &intrinsic_sym;
    3232           74 :       gcc_assert (symtree4 == root4);
    3233              : 
    3234           74 :       e4 = gfc_get_expr ();
    3235           74 :       e4->expr_type = EXPR_FUNCTION;
    3236           74 :       e4->where = where;
    3237           74 :       e4->symtree = symtree4;
    3238           74 :       e4->value.function.actual = gfc_get_actual_arglist ();
    3239           74 :       e4->value.function.actual->expr = e3;
    3240           74 :       e4->value.function.actual->next = gfc_get_actual_arglist ();
    3241           74 :       e4->value.function.actual->next->expr = e1;
    3242              :     }
    3243          687 :   if (OMP_CLAUSE_REDUCTION_CODE (c) != ERROR_MARK)
    3244              :     {
    3245              :       /* e1 and e3 have been stored as arguments of e4, avoid sharing.  */
    3246          284 :       e1 = gfc_copy_expr (e1);
    3247          284 :       e3 = gfc_copy_expr (e3);
    3248          284 :       t = gfc_resolve_expr (e4);
    3249          284 :       gcc_assert (t);
    3250              :     }
    3251              : 
    3252              :   /* Create the init statement list.  */
    3253          687 :   pushlevel ();
    3254          687 :   if (e2)
    3255          621 :     stmt = gfc_trans_assignment (e1, e2, false, false);
    3256              :   else
    3257           66 :     stmt = gfc_trans_call (n->u2.udr->initializer, false,
    3258              :                            NULL_TREE, NULL_TREE, false);
    3259          687 :   if (TREE_CODE (stmt) != BIND_EXPR)
    3260          198 :     stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    3261              :   else
    3262          489 :     poplevel (0, 0);
    3263          687 :   OMP_CLAUSE_REDUCTION_INIT (c) = stmt;
    3264              : 
    3265              :   /* Create the merge statement list.  */
    3266          687 :   pushlevel ();
    3267          687 :   if (e4)
    3268          621 :     stmt = gfc_trans_assignment (e3, e4, false, true);
    3269              :   else
    3270           66 :     stmt = gfc_trans_call (n->u2.udr->combiner, false,
    3271              :                            NULL_TREE, NULL_TREE, false);
    3272          687 :   if (TREE_CODE (stmt) != BIND_EXPR)
    3273          237 :     stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    3274              :   else
    3275          450 :     poplevel (0, 0);
    3276          687 :   OMP_CLAUSE_REDUCTION_MERGE (c) = stmt;
    3277              : 
    3278              :   /* And stick the placeholder VAR_DECL into the clause as well.  */
    3279          687 :   OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = outer_decl;
    3280              : 
    3281          687 :   gfc_current_locus = old_loc;
    3282              : 
    3283          687 :   gfc_free_expr (e1);
    3284          687 :   if (e2)
    3285          621 :     gfc_free_expr (e2);
    3286          687 :   gfc_free_expr (e3);
    3287          687 :   if (e4)
    3288          621 :     gfc_free_expr (e4);
    3289          687 :   free (symtree1);
    3290          687 :   free (symtree2);
    3291          687 :   free (symtree3);
    3292          687 :   free (symtree4);
    3293          687 :   if (outer_sym.as)
    3294          426 :     gfc_free_array_spec (outer_sym.as);
    3295              : 
    3296          687 :   if (udr)
    3297              :     {
    3298          403 :       *udr->omp_out = omp_var_copy[0];
    3299          403 :       *udr->omp_in = omp_var_copy[1];
    3300          403 :       if (udr->initializer_ns)
    3301              :         {
    3302          270 :           *udr->omp_priv = omp_var_copy[2];
    3303          270 :           *udr->omp_orig = omp_var_copy[3];
    3304              :         }
    3305              :     }
    3306              : 
    3307          687 :   gfc_current_ns = old_ns;
    3308          687 : }
    3309              : 
    3310              : static tree
    3311         3856 : gfc_trans_omp_reduction_list (int kind, gfc_omp_namelist *namelist, tree list,
    3312              :                               locus where, bool mark_addressable)
    3313              : {
    3314         3856 :   omp_clause_code clause = OMP_CLAUSE_REDUCTION;
    3315         3856 :   switch (kind)
    3316              :     {
    3317              :     case OMP_LIST_REDUCTION:
    3318              :     case OMP_LIST_REDUCTION_INSCAN:
    3319              :     case OMP_LIST_REDUCTION_TASK:
    3320              :       break;
    3321              :     case OMP_LIST_IN_REDUCTION:
    3322              :       clause = OMP_CLAUSE_IN_REDUCTION;
    3323              :       break;
    3324              :     case OMP_LIST_TASK_REDUCTION:
    3325              :       clause = OMP_CLAUSE_TASK_REDUCTION;
    3326              :       break;
    3327            0 :     default:
    3328            0 :       gcc_unreachable ();
    3329              :     }
    3330         8656 :   for (; namelist != NULL; namelist = namelist->next)
    3331         4800 :     if (namelist->sym->attr.referenced)
    3332              :       {
    3333         4800 :         tree t = gfc_trans_omp_variable (namelist->sym, false);
    3334         4800 :         if (t != error_mark_node)
    3335              :           {
    3336         4800 :             tree node = build_omp_clause (gfc_get_location (&namelist->where),
    3337              :                                           clause);
    3338         4800 :             OMP_CLAUSE_DECL (node) = t;
    3339         4800 :             if (mark_addressable)
    3340           38 :               TREE_ADDRESSABLE (t) = 1;
    3341         4800 :             if (kind == OMP_LIST_REDUCTION_INSCAN)
    3342           20 :               OMP_CLAUSE_REDUCTION_INSCAN (node) = 1;
    3343         4800 :             if (kind == OMP_LIST_REDUCTION_TASK)
    3344           92 :               OMP_CLAUSE_REDUCTION_TASK (node) = 1;
    3345         4800 :             switch (namelist->u.reduction_op)
    3346              :               {
    3347         2350 :               case OMP_REDUCTION_PLUS:
    3348         2350 :                 OMP_CLAUSE_REDUCTION_CODE (node) = PLUS_EXPR;
    3349         2350 :                 break;
    3350          198 :               case OMP_REDUCTION_MINUS:
    3351          198 :                 OMP_CLAUSE_REDUCTION_CODE (node) = MINUS_EXPR;
    3352          198 :                 break;
    3353          254 :               case OMP_REDUCTION_TIMES:
    3354          254 :                 OMP_CLAUSE_REDUCTION_CODE (node) = MULT_EXPR;
    3355          254 :                 break;
    3356           92 :               case OMP_REDUCTION_AND:
    3357           92 :                 OMP_CLAUSE_REDUCTION_CODE (node) = TRUTH_ANDIF_EXPR;
    3358           92 :                 break;
    3359          785 :               case OMP_REDUCTION_OR:
    3360          785 :                 OMP_CLAUSE_REDUCTION_CODE (node) = TRUTH_ORIF_EXPR;
    3361          785 :                 break;
    3362           86 :               case OMP_REDUCTION_EQV:
    3363           86 :                 OMP_CLAUSE_REDUCTION_CODE (node) = EQ_EXPR;
    3364           86 :                 break;
    3365           86 :               case OMP_REDUCTION_NEQV:
    3366           86 :                 OMP_CLAUSE_REDUCTION_CODE (node) = NE_EXPR;
    3367           86 :                 break;
    3368          218 :               case OMP_REDUCTION_MAX:
    3369          218 :                 OMP_CLAUSE_REDUCTION_CODE (node) = MAX_EXPR;
    3370          218 :                 break;
    3371          201 :               case OMP_REDUCTION_MIN:
    3372          201 :                 OMP_CLAUSE_REDUCTION_CODE (node) = MIN_EXPR;
    3373          201 :                 break;
    3374           40 :               case OMP_REDUCTION_IAND:
    3375           40 :                 OMP_CLAUSE_REDUCTION_CODE (node) = BIT_AND_EXPR;
    3376           40 :                 break;
    3377           49 :               case OMP_REDUCTION_IOR:
    3378           49 :                 OMP_CLAUSE_REDUCTION_CODE (node) = BIT_IOR_EXPR;
    3379           49 :                 break;
    3380           38 :               case OMP_REDUCTION_IEOR:
    3381           38 :                 OMP_CLAUSE_REDUCTION_CODE (node) = BIT_XOR_EXPR;
    3382           38 :                 break;
    3383          403 :               case OMP_REDUCTION_USER:
    3384          403 :                 OMP_CLAUSE_REDUCTION_CODE (node) = ERROR_MARK;
    3385          403 :                 break;
    3386            0 :               default:
    3387            0 :                 gcc_unreachable ();
    3388              :               }
    3389         4800 :             if (namelist->sym->attr.dimension
    3390         4374 :                 || namelist->u.reduction_op == OMP_REDUCTION_USER
    3391         4127 :                 || namelist->sym->attr.allocatable)
    3392          687 :               gfc_trans_omp_array_reduction_or_udr (node, namelist, where);
    3393         4800 :             list = gfc_trans_add_clause (node, list);
    3394              :           }
    3395              :       }
    3396         3856 :   return list;
    3397              : }
    3398              : 
    3399              : static inline tree
    3400         4996 : gfc_convert_expr_to_tree (stmtblock_t *block, gfc_expr *expr)
    3401              : {
    3402         4996 :   gfc_se se;
    3403         4996 :   tree result;
    3404              : 
    3405         4996 :   gfc_init_se (&se, NULL );
    3406         4996 :   gfc_conv_expr (&se, expr);
    3407         4996 :   gfc_add_block_to_block (block, &se.pre);
    3408         4996 :   result = gfc_evaluate_now (se.expr, block);
    3409         4996 :   gfc_add_block_to_block (block, &se.post);
    3410              : 
    3411         4996 :   return result;
    3412              : }
    3413              : 
    3414              : static vec<tree, va_heap, vl_embed> *doacross_steps;
    3415              : 
    3416              : 
    3417              : /* Map an array section or array element.
    3418              :    BLOCK will hold any output statements generated; if there are iterators,
    3419              :      it's a block for the current iterator group.
    3420              :    OP is the construct containing the map clause.
    3421              :    N is the entry that appears in the clause namelist.  It may contain iterator
    3422              :      variables.
    3423              :    DECL is the base object associated with the namelist entry.  It can be an
    3424              :      array descriptor, a bare array, or pointer to an array.
    3425              :    ELEMENT is true for an array element, false for an array section.
    3426              :    OPENMP is true for OpenMP, false for OpenACC.
    3427              :    PTR_KIND is the map operation.
    3428              :    NODE is an input operand representing the map clause.
    3429              :    NODE2, NODE3, and NODE4 are output operands that will hold new map clauses
    3430              :      generated by this function.  Not all of them are always needed.  NODE2
    3431              :      is for an array descriptor object, NODE3 is for its data array, NODE4
    3432              :      is for a pointer mapping.
    3433              :    ITERATOR is a list of active iterator descriptors, chained through
    3434              :      TREE_CHAIN.  */
    3435              : 
    3436              : static void
    3437         4116 : gfc_trans_omp_array_section (stmtblock_t *block, gfc_exec_op op,
    3438              :                              gfc_omp_namelist *n, tree decl, bool element,
    3439              :                              bool openmp, gomp_map_kind ptr_kind, tree &node,
    3440              :                              tree &node2, tree &node3, tree &node4,
    3441              :                              tree iterator)
    3442              : {
    3443         4116 :   gfc_se se;
    3444              :   /* PTR is the array expression from n->expr.  If iterators are this
    3445              :      involved expression can involve iterator variables.  BASE points to the
    3446              :      base array object obtained from DECL.  */
    3447         4116 :   tree ptr, base;
    3448         4116 :   tree elemsz = NULL_TREE;
    3449              : 
    3450         4116 :   gfc_init_se (&se, NULL);
    3451         4116 :   if (element)
    3452              :     {
    3453          185 :       gfc_conv_expr_reference (&se, n->expr);
    3454          185 :       gfc_add_block_to_block (block, &se.pre);
    3455          185 :       ptr = se.expr;
    3456              :     }
    3457              :   else
    3458              :     {
    3459         3931 :       gfc_conv_expr_descriptor (&se, n->expr);
    3460         3931 :       ptr = gfc_conv_array_data (se.expr);
    3461              :     }
    3462         4116 :   if (n->expr->ts.type == BT_CHARACTER && n->expr->ts.deferred)
    3463              :     {
    3464            0 :       gcc_assert (se.string_length);
    3465            0 :       tree len = gfc_evaluate_now (se.string_length, block);
    3466            0 :       elemsz = gfc_get_char_type (n->expr->ts.kind);
    3467            0 :       elemsz = TYPE_SIZE_UNIT (elemsz);
    3468            0 :       elemsz = fold_build2 (MULT_EXPR, size_type_node,
    3469              :                             fold_convert (size_type_node, len), elemsz);
    3470              :     }
    3471         4116 :   if (element)
    3472              :     {
    3473          185 :       if (!elemsz)
    3474          185 :         elemsz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ptr)));
    3475          185 :       OMP_CLAUSE_SIZE (node) = elemsz;
    3476              :     }
    3477              :   else
    3478              :     {
    3479         3931 :       tree type = TREE_TYPE (se.expr);
    3480         3931 :       gfc_add_block_to_block (block, &se.pre);
    3481         3931 :       OMP_CLAUSE_SIZE (node) = gfc_full_array_size (block, se.expr,
    3482         3931 :                                                     GFC_TYPE_ARRAY_RANK (type));
    3483         3931 :       if (!elemsz)
    3484         3931 :         elemsz = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    3485         3931 :       elemsz = fold_convert (gfc_array_index_type, elemsz);
    3486         3931 :       OMP_CLAUSE_SIZE (node) = fold_build2 (MULT_EXPR, gfc_array_index_type,
    3487              :                                             OMP_CLAUSE_SIZE (node), elemsz);
    3488         3931 :       if (n->expr->ts.type == BT_DERIVED
    3489           21 :           && n->expr->ts.u.derived->attr.alloc_comp)
    3490              :         {
    3491              :           /* Save array descriptor for use in gfc_omp_deep_mapping{,_p,_cnt};
    3492              :              force evaluate to ensure that it is not gimplified + is a decl.  */
    3493           15 :           tree tmp = OMP_CLAUSE_SIZE (node);
    3494           15 :           tree var = gfc_create_var (TREE_TYPE (tmp), NULL);
    3495           15 :           gfc_add_modify_loc (input_location, block, var, tmp);
    3496           15 :           OMP_CLAUSE_SIZE (node) = var;
    3497           15 :           gfc_allocate_lang_decl (var);
    3498           15 :           GFC_DECL_SAVED_DESCRIPTOR (var) = se.expr;
    3499              :         }
    3500              :     }
    3501         4116 :   gcc_assert (se.post.head == NULL_TREE);
    3502         4116 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
    3503         4116 :   OMP_CLAUSE_DECL (node) = build_fold_indirect_ref (ptr);
    3504         4116 :   ptr = fold_convert (ptrdiff_type_node, ptr);
    3505              : 
    3506         7946 :   if (POINTER_TYPE_P (TREE_TYPE (decl))
    3507          365 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))
    3508           79 :       && ptr_kind == GOMP_MAP_POINTER
    3509           79 :       && op != EXEC_OMP_TARGET_EXIT_DATA
    3510           79 :       && OMP_CLAUSE_MAP_KIND (node) != GOMP_MAP_RELEASE
    3511         4195 :       && OMP_CLAUSE_MAP_KIND (node) != GOMP_MAP_DELETE)
    3512              : 
    3513              :     {
    3514              :       /* NODE4 is a newly-generated map clause for the pointer.  */
    3515           79 :       node4 = build_omp_clause (input_location,
    3516              :                                 OMP_CLAUSE_MAP);
    3517           79 :       OMP_CLAUSE_SET_MAP_KIND (node4, GOMP_MAP_POINTER);
    3518           79 :       OMP_CLAUSE_DECL (node4) = decl;
    3519           79 :       OMP_CLAUSE_SIZE (node4) = size_int (0);
    3520              :       /* Make DECL be the descriptor rather than the pointer to it.  */
    3521           79 :       decl = build_fold_indirect_ref (decl);
    3522              :     }
    3523         4037 :   else if (ptr_kind == GOMP_MAP_ALWAYS_POINTER
    3524            0 :            && n->expr->ts.type == BT_CHARACTER
    3525            0 :            && n->expr->ts.deferred)
    3526              :     {
    3527            0 :       gomp_map_kind map_kind;
    3528            0 :       if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DELETE)
    3529            0 :         map_kind = OMP_CLAUSE_MAP_KIND (node);
    3530            0 :       else if (op == EXEC_OMP_TARGET_EXIT_DATA
    3531            0 :                || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_RELEASE)
    3532              :         map_kind = GOMP_MAP_RELEASE;
    3533              :       else
    3534              :         map_kind = GOMP_MAP_TO;
    3535            0 :       gcc_assert (se.string_length);
    3536            0 :       node4 = build_omp_clause (input_location, OMP_CLAUSE_MAP);
    3537            0 :       OMP_CLAUSE_SET_MAP_KIND (node4, map_kind);
    3538            0 :       OMP_CLAUSE_DECL (node4) = se.string_length;
    3539            0 :       OMP_CLAUSE_SIZE (node4) = TYPE_SIZE_UNIT (gfc_charlen_type_node);
    3540              :     }
    3541         4116 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
    3542              :     {
    3543         2779 :       tree type = TREE_TYPE (decl);
    3544         2779 :       base = gfc_conv_descriptor_data_get (decl);
    3545              :       /* NODE2 is a newly-generated map clause for the array descriptor DECL.  */
    3546         2779 :       node2 = build_omp_clause (input_location, OMP_CLAUSE_MAP);
    3547         2779 :       OMP_CLAUSE_DECL (node2) = decl;
    3548         2779 :       OMP_CLAUSE_SIZE (node2) = TYPE_SIZE_UNIT (type);
    3549         2779 :       if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DELETE
    3550         2778 :           || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_RELEASE
    3551         2569 :           || op == EXEC_OMP_TARGET_EXIT_DATA
    3552         5348 :           || op == EXEC_OACC_EXIT_DATA)
    3553              :         {
    3554          392 :           gomp_map_kind map_kind
    3555          392 :             = OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DELETE ? GOMP_MAP_DELETE
    3556          391 :                                                             : GOMP_MAP_RELEASE;
    3557          392 :           OMP_CLAUSE_SET_MAP_KIND (node2, map_kind);
    3558          392 :           OMP_CLAUSE_RELEASE_DESCRIPTOR (node2) = 1;
    3559              :         }
    3560              :       else
    3561         2387 :         OMP_CLAUSE_SET_MAP_KIND (node2, GOMP_MAP_TO_PSET);
    3562              :       /* NODE3 is a newly-generated map clause for the array data.  */
    3563         2779 :       node3 = build_omp_clause (input_location, OMP_CLAUSE_MAP);
    3564         2779 :       OMP_CLAUSE_SET_MAP_KIND (node3, ptr_kind);
    3565         2779 :       OMP_CLAUSE_DECL (node3) = gfc_conv_descriptor_data_get (decl);
    3566              :       /* This purposely does not include GOMP_MAP_ALWAYS_POINTER.  The extra
    3567              :          cast prevents gimplify.cc from recognising it as being part of the
    3568              :          struct - and adding an 'alloc: for the 'desc.data' pointer, which
    3569              :          would break as the 'desc' (the descriptor) is also mapped
    3570              :          (see node4 above).  */
    3571         2779 :       if (ptr_kind == GOMP_MAP_ATTACH_DETACH && !openmp)
    3572          141 :         STRIP_NOPS (OMP_CLAUSE_DECL (node3));
    3573              :     }
    3574              :   else  /* DECL is bare array or pointer to an array.  */
    3575              :     {
    3576         1337 :       if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
    3577              :         {
    3578         1051 :           tree offset;
    3579         1051 :           base = build_fold_addr_expr (decl);
    3580         1051 :           offset = fold_build2 (MINUS_EXPR, ptrdiff_type_node, ptr,
    3581              :                                 fold_convert (ptrdiff_type_node, base));
    3582         1051 :           offset = build2 (TRUNC_DIV_EXPR, ptrdiff_type_node,
    3583              :                            offset, fold_convert (ptrdiff_type_node, elemsz));
    3584         1051 :           offset = build4_loc (input_location, ARRAY_REF,
    3585         1051 :                                TREE_TYPE (TREE_TYPE (decl)),
    3586              :                                decl, offset, NULL_TREE, NULL_TREE);
    3587         1051 :           OMP_CLAUSE_DECL (node) = offset;
    3588              : 
    3589         1051 :           if (ptr_kind == GOMP_MAP_ATTACH_DETACH && openmp)
    3590          145 :             return;
    3591              :         }
    3592              :       else  /* DECL is a pointer.  */
    3593              :         {
    3594          286 :           gcc_assert (POINTER_TYPE_P (TREE_TYPE (decl)));
    3595              :           base = decl;
    3596              :         }
    3597         1192 :       node3 = build_omp_clause (input_location,
    3598              :                                 OMP_CLAUSE_MAP);
    3599         1192 :       OMP_CLAUSE_SET_MAP_KIND (node3, ptr_kind);
    3600         1192 :       OMP_CLAUSE_DECL (node3) = decl;
    3601              :     }
    3602              : 
    3603              :   /* FIXME: This is a broken hack.  The ptr expression is based on the
    3604              :      namelist entry and can contain references to iterator variables, which
    3605              :      are not yet set to their initial values when ptr is used.  This
    3606              :      tries to replace instances of the iterator values with the initial values
    3607              :      in ptr explicitly.  It's broken because the expansion of ptr can also
    3608              :      add statements to the iterator block that also contain references to
    3609              :      the uninitialized variables, and substituting those similarly breaks
    3610              :      other things.  */
    3611         3971 :   base = fold_convert (ptrdiff_type_node, base);
    3612         7993 :   for (tree it = iterator; it; it = TREE_CHAIN (it))
    3613              :     {
    3614           51 :       ptr = simplify_replace_tree (ptr, OMP_ITERATOR_VAR (it),
    3615           51 :                                    OMP_ITERATOR_BEGIN (it));
    3616           51 :       base = simplify_replace_tree (base, OMP_ITERATOR_VAR (it),
    3617           51 :                                     OMP_ITERATOR_BEGIN (it));
    3618              :     }
    3619              : 
    3620              :   /* The OMP_CLAUSE_SIZE field for the array data map clause node3
    3621              :      contains the initial offset of ptr from base, not the size.  */
    3622         3971 :   OMP_CLAUSE_SIZE (node3) = fold_build2 (MINUS_EXPR, ptrdiff_type_node,
    3623              :                                          ptr, base);
    3624              : }
    3625              : 
    3626              : /* Construct a list of omp_iterator objects for the iterators specified
    3627              :    in NS.  Initialization statements for the start/end/step expressions
    3628              :    are added to OUTER_BLOCK.  BLOCK is the tree block that contains the
    3629              :    iterator variable bindings.  */
    3630              : static tree
    3631           94 : handle_iterator (gfc_namespace *ns, stmtblock_t *outer_block, tree block)
    3632              : {
    3633           94 :   tree list = NULL_TREE;
    3634          203 :   for (gfc_symbol *sym = ns->omp_affinity_iterators; sym; sym = sym->tlink)
    3635              :     {
    3636          109 :       gfc_constructor *c;
    3637          109 :       gfc_se se;
    3638              : 
    3639          109 :       tree last = make_omp_iterator ();
    3640          109 :       tree iter_var = gfc_get_symbol_decl (sym);
    3641          109 :       tree type = TREE_TYPE (iter_var);
    3642          109 :       OMP_ITERATOR_VAR (last) = iter_var;
    3643          109 :       DECL_CHAIN (iter_var) = BLOCK_VARS (block);
    3644          109 :       BLOCK_VARS (block) = iter_var;
    3645              : 
    3646              :       /* begin */
    3647          109 :       c = gfc_constructor_first (sym->value->value.constructor);
    3648          109 :       gfc_init_se (&se, NULL);
    3649          109 :       gfc_conv_expr (&se, c->expr);
    3650          109 :       gfc_add_block_to_block (outer_block, &se.pre);
    3651          109 :       gfc_add_block_to_block (outer_block, &se.post);
    3652          109 :       OMP_ITERATOR_BEGIN (last) = fold_convert (type,
    3653              :                                                 gfc_evaluate_now (se.expr,
    3654              :                                                                   outer_block));
    3655              :       /* end */
    3656          109 :       c = gfc_constructor_next (c);
    3657          109 :       gfc_init_se (&se, NULL);
    3658          109 :       gfc_conv_expr (&se, c->expr);
    3659          109 :       gfc_add_block_to_block (outer_block, &se.pre);
    3660          109 :       gfc_add_block_to_block (outer_block, &se.post);
    3661          109 :       OMP_ITERATOR_END (last) = fold_convert (type,
    3662              :                                               gfc_evaluate_now (se.expr,
    3663              :                                                                 outer_block));
    3664              :       /* step */
    3665          109 :       c = gfc_constructor_next (c);
    3666          109 :       tree step;
    3667          109 :       if (c)
    3668              :         {
    3669            5 :           gfc_init_se (&se, NULL);
    3670            5 :           gfc_conv_expr (&se, c->expr);
    3671            5 :           gfc_add_block_to_block (outer_block, &se.pre);
    3672            5 :           gfc_add_block_to_block (outer_block, &se.post);
    3673            5 :           gfc_conv_expr (&se, c->expr);
    3674            5 :           step = fold_convert (type,
    3675              :                                gfc_evaluate_now (se.expr,
    3676              :                                                  outer_block));
    3677              :         }
    3678              :       else
    3679          104 :         step = build_int_cst (type, 1);
    3680          109 :       OMP_ITERATOR_STEP (last) = step;
    3681              :       /* orig_step */
    3682          109 :       OMP_ITERATOR_ORIG_STEP (last) = save_expr (step);
    3683          109 :       TREE_CHAIN (last) = list;
    3684          109 :       list = last;
    3685              :     }
    3686           94 :   return list;
    3687              : }
    3688              : 
    3689              : /* Start an iterator group for the iterators in NS.  OUTER_BLOCK is the
    3690              :    statement block to hold side-effects from evaluating the iterator
    3691              :    start/end/step expressions, and ITER_BLOCK is set to a newly initialized
    3692              :    block for the scope of the iterators.  TREE_BLOCK is set to a new
    3693              :    tree BLOCK node to hold the iterator variables.  Returns the list of
    3694              :    omp_iterator objects, as per handle_iterator above.  */
    3695              : static tree
    3696           94 : start_iterator_group (gfc_namespace *ns, stmtblock_t *outer_block,
    3697              :                       stmtblock_t *iter_block, tree &tree_block)
    3698              : {
    3699           94 :   gfc_init_block (iter_block);
    3700           94 :   tree_block = make_node (BLOCK);
    3701           94 :   TREE_USED (tree_block) = 1;
    3702           94 :   BLOCK_VARS (tree_block) = NULL_TREE;
    3703           94 :   return handle_iterator (ns, outer_block, tree_block);
    3704              : }
    3705              : 
    3706              : /* Finish an iterator group.  LIST identifies the namelist type for the
    3707              :    clause.  ITER_BLOCK is the statement block within the scope of the
    3708              :    iterators, and TREE_BLOCK is the block for the iterator variables.
    3709              :    ITERATOR is the list of omp_iterator objects as previously returned by
    3710              :    start_iterator_group.  OMP_CLAUSES is the list of clauses the iterators
    3711              :    apply to, ending at PREV_CLAUSES.  */
    3712              : static void
    3713           94 : finish_iterator_group (int list, stmtblock_t *iter_block, tree tree_block,
    3714              :                        tree iterator, tree omp_clauses, tree prev_clauses)
    3715              : {
    3716           94 :   gcc_assert (iterator);
    3717           94 :   BLOCK_SUBBLOCKS (tree_block) = gfc_finish_block (iter_block);
    3718           94 :   OMP_ITERATOR_BLOCK (iterator) = tree_block;
    3719          276 :   for (tree c = omp_clauses; c != prev_clauses; c = OMP_CLAUSE_CHAIN (c))
    3720          182 :     switch (list)
    3721              :       {
    3722           56 :       case OMP_LIST_AFFINITY:
    3723           56 :       case OMP_LIST_DEPEND:
    3724           56 :         OMP_CLAUSE_DECL (c) = build_tree_list (iterator, OMP_CLAUSE_DECL (c));
    3725           56 :         break;
    3726          112 :       case OMP_LIST_MAP:
    3727          112 :         if (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
    3728          112 :             && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
    3729          108 :           OMP_CLAUSE_ITERATORS (c) = iterator;
    3730              :         break;
    3731           14 :       case OMP_LIST_TO:
    3732           14 :       case OMP_LIST_FROM:
    3733           14 :       case OMP_LIST_CACHE:
    3734           14 :         OMP_CLAUSE_ITERATORS (c) = iterator;
    3735           14 :         break;
    3736            0 :       default:
    3737            0 :         gcc_unreachable ();
    3738              :       }
    3739           94 : }
    3740              : 
    3741              : /* To alleviate quadratic behaviour in checking each entry of a
    3742              :    gfc_omp_namelist against every other entry, we build a hashtable indexed by
    3743              :    gfc_symbol pointer, which we can use in the usual case that a map
    3744              :    expression has a symbol as its root term.  Return a namelist based on the
    3745              :    root symbol used by N, building a new table in SYM_ROOTED_NL using the
    3746              :    gfc_omp_namelist N2 (all clauses) if we haven't done so already.  */
    3747              : 
    3748              : static gfc_omp_namelist *
    3749          934 : get_symbol_rooted_namelist (hash_map<gfc_symbol *,
    3750              :                                      gfc_omp_namelist *> *&sym_rooted_nl,
    3751              :                             gfc_omp_namelist *n,
    3752              :                             gfc_omp_namelist *n2, bool *sym_based)
    3753              : {
    3754              :   /* Early-out if we have a NULL clause list (e.g. for OpenACC).  */
    3755          934 :   if (!n2)
    3756              :     return NULL;
    3757              : 
    3758          897 :   gfc_symbol *use_sym = NULL;
    3759              : 
    3760              :   /* We're only interested in cases where we have an expression, e.g. a
    3761              :      component access.  */
    3762          897 :   if (n->expr && n->expr->expr_type == EXPR_VARIABLE && n->expr->symtree)
    3763          897 :     use_sym = n->expr->symtree->n.sym;
    3764              : 
    3765          897 :   *sym_based = false;
    3766              : 
    3767          897 :   if (!use_sym)
    3768              :     return n2;
    3769              : 
    3770          897 :   if (!sym_rooted_nl)
    3771              :     {
    3772          388 :       sym_rooted_nl = new hash_map<gfc_symbol *, gfc_omp_namelist *> ();
    3773              : 
    3774         1715 :       for (; n2 != NULL; n2 = n2->next)
    3775              :         {
    3776         1327 :           if (!n2->expr
    3777         1326 :               || n2->expr->expr_type != EXPR_VARIABLE
    3778         1326 :               || !n2->expr->symtree)
    3779            1 :             continue;
    3780              : 
    3781         1326 :           gfc_omp_namelist *nl_copy = gfc_get_omp_namelist ();
    3782         1326 :           memcpy (nl_copy, n2, sizeof *nl_copy);
    3783         1326 :           nl_copy->u2.duplicate_of = n2;
    3784         1326 :           nl_copy->next = NULL;
    3785              : 
    3786         1326 :           gfc_symbol *idx_sym = n2->expr->symtree->n.sym;
    3787              : 
    3788         1326 :           bool existed;
    3789         1326 :           gfc_omp_namelist *&entry
    3790         1326 :             = sym_rooted_nl->get_or_insert (idx_sym, &existed);
    3791         1326 :           if (existed)
    3792          881 :             nl_copy->next = entry;
    3793         1326 :           entry = nl_copy;
    3794              :         }
    3795              :     }
    3796              : 
    3797          897 :   gfc_omp_namelist **n2_sym = sym_rooted_nl->get (use_sym);
    3798              : 
    3799          897 :   if (n2_sym)
    3800              :     {
    3801          897 :       *sym_based = true;
    3802          897 :       return *n2_sym;
    3803              :     }
    3804              : 
    3805              :   return NULL;
    3806              : }
    3807              : 
    3808              : /* Helper function for gfc_trans_omp_clauses.  Adjust existing and create new
    3809              :    map nodes for derived-type component array descriptors. Return true if the
    3810              :    mapping has to be dropped.  */
    3811              : 
    3812              : static bool
    3813         1191 : gfc_map_array_descriptor (
    3814              :   tree &node, tree &node2, tree &node3, tree &node4, tree descr, bool openacc,
    3815              :   location_t map_loc, stmtblock_t *block, gfc_exec_op op, gfc_omp_namelist *n,
    3816              :   hash_map<gfc_symbol *, gfc_omp_namelist *> *&sym_rooted_nl, gfc_se se,
    3817              :   gfc_omp_clauses *clauses, bool mid_desc_p)
    3818              : {
    3819         1191 :   tree type = TREE_TYPE (descr);
    3820         1191 :   tree ptr = gfc_conv_descriptor_data_get (descr);
    3821         1191 :   ptr = build_fold_indirect_ref (ptr);
    3822         1191 :   OMP_CLAUSE_DECL (node) = ptr;
    3823         1191 :   int rank = GFC_TYPE_ARRAY_RANK (type);
    3824         1191 :   OMP_CLAUSE_SIZE (node) = gfc_full_array_size (block, descr, rank);
    3825         1191 :   tree elemsz = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    3826              : 
    3827         1191 :   gomp_map_kind map_kind = OMP_CLAUSE_MAP_KIND (node);
    3828         1191 :   if (GOMP_MAP_COPY_TO_P (map_kind) || map_kind == GOMP_MAP_ALLOC)
    3829              :     {
    3830          842 :       if (mid_desc_p)
    3831              :         {
    3832              :           /* For an intermediate descriptor, the pointee (i.e. the actual array
    3833              :              content) is mapped in a separate set of nodes. This ALLOC is only
    3834              :              emitted to comply with the group layout expected by the gimplifier.
    3835              :             */
    3836           89 :           OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALLOC);
    3837           89 :           OMP_CLAUSE_SIZE (node) = size_zero_node;
    3838           89 :           OMP_CLAUSE_MAP_GIMPLE_ONLY (node) = 1;
    3839              :         }
    3840              :       else
    3841          753 :         map_kind
    3842         1250 :           = ((GOMP_MAP_ALWAYS_P (map_kind) || gfc_expr_attr (n->expr).pointer)
    3843          753 :                ? GOMP_MAP_ALWAYS_TO
    3844              :                : GOMP_MAP_TO);
    3845              :     }
    3846          349 :   else if (n->u.map.op == OMP_MAP_RELEASE || n->u.map.op == OMP_MAP_DELETE)
    3847              :     ;
    3848          344 :   else if (op == EXEC_OMP_TARGET_EXIT_DATA || op == EXEC_OACC_EXIT_DATA)
    3849              :     map_kind = GOMP_MAP_RELEASE;
    3850           31 :   else if (mid_desc_p)
    3851              :     {
    3852              :       /* For an intermediate descriptor, the pointee (i.e. the actual array
    3853              :          content) is mapped in a separate set of nodes. This ALLOC is only
    3854              :          emitted to comply with the group layout expected by the gimplifier.  */
    3855            1 :       OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALLOC);
    3856            1 :       OMP_CLAUSE_SIZE (node) = size_zero_node;
    3857            1 :       OMP_CLAUSE_MAP_GIMPLE_ONLY (node) = 1;
    3858              :     }
    3859              :   else
    3860              :     map_kind = GOMP_MAP_ALLOC;
    3861              : 
    3862         1191 :   if (!openacc && n->expr->ts.type == BT_CHARACTER && n->expr->ts.deferred)
    3863              :     {
    3864           42 :       gcc_assert (se.string_length);
    3865           42 :       tree len = fold_convert (size_type_node, se.string_length);
    3866           42 :       elemsz = gfc_get_char_type (n->expr->ts.kind);
    3867           42 :       elemsz = TYPE_SIZE_UNIT (elemsz);
    3868           42 :       elemsz = fold_build2 (MULT_EXPR, size_type_node, len, elemsz);
    3869           42 :       node4 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    3870           42 :       OMP_CLAUSE_SET_MAP_KIND (node4, map_kind);
    3871           42 :       OMP_CLAUSE_DECL (node4) = se.string_length;
    3872           42 :       OMP_CLAUSE_SIZE (node4) = TYPE_SIZE_UNIT (gfc_charlen_type_node);
    3873              :     }
    3874         1191 :   elemsz = fold_convert (gfc_array_index_type, elemsz);
    3875         1191 :   OMP_CLAUSE_SIZE (node) = fold_build2 (MULT_EXPR, gfc_array_index_type,
    3876              :                                         OMP_CLAUSE_SIZE (node), elemsz);
    3877              : 
    3878         1191 :   node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    3879         1191 :   if (map_kind == GOMP_MAP_RELEASE || map_kind == GOMP_MAP_DELETE)
    3880              :     {
    3881          318 :       OMP_CLAUSE_SET_MAP_KIND (node2, map_kind);
    3882          318 :       OMP_CLAUSE_RELEASE_DESCRIPTOR (node2) = 1;
    3883              :     }
    3884              :   else
    3885          873 :     OMP_CLAUSE_SET_MAP_KIND (node2, GOMP_MAP_TO_PSET);
    3886         1191 :   OMP_CLAUSE_DECL (node2) = descr;
    3887         1191 :   OMP_CLAUSE_SIZE (node2) = TYPE_SIZE_UNIT (type);
    3888              : 
    3889         1191 :   if (!openacc)
    3890              :     {
    3891         1051 :       if (n->expr->ts.type == BT_DERIVED
    3892           18 :           && n->expr->ts.u.derived->attr.alloc_comp)
    3893              :         {
    3894              :           /* Save array descriptor for use
    3895              :              in gfc_omp_deep_mapping{,_p,_cnt}; force
    3896              :              evaluate to ensure that it is
    3897              :              not gimplified + is a decl.  */
    3898           12 :           tree tmp = OMP_CLAUSE_SIZE (node);
    3899           12 :           tree var = gfc_create_var (TREE_TYPE (tmp), NULL);
    3900           12 :           gfc_add_modify_loc (map_loc, block, var, tmp);
    3901           12 :           OMP_CLAUSE_SIZE (node) = var;
    3902           12 :           gfc_allocate_lang_decl (var);
    3903           12 :           GFC_DECL_SAVED_DESCRIPTOR (var) = descr;
    3904              :         }
    3905              : 
    3906              :       /* If we don't have a mapping of a smaller part
    3907              :           of the array -- or we can't prove that we do
    3908              :           statically -- set this flag.  If there is a
    3909              :           mapping of a smaller part of the array after
    3910              :           all, this will turn into a no-op at
    3911              :           runtime.  */
    3912         1051 :       OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (node) = 1;
    3913              : 
    3914         1051 :       bool drop_mapping = false;
    3915              : 
    3916         1051 :       if (!mid_desc_p)
    3917              :         {
    3918          879 :           gfc_omp_namelist *n2 = clauses->lists[OMP_LIST_MAP];
    3919              : 
    3920          879 :           bool sym_based;
    3921          879 :           n2 = get_symbol_rooted_namelist (sym_rooted_nl, n, n2, &sym_based);
    3922              : 
    3923         4058 :           for (; n2 != NULL; n2 = n2->next)
    3924              :             {
    3925         2586 :               if ((!sym_based && n == n2)
    3926         2586 :                   || (sym_based && n == n2->u2.duplicate_of) || !n2->expr)
    3927          617 :                 continue;
    3928              : 
    3929         1969 :               if (!gfc_omp_expr_prefix_same (n->expr, n2->expr))
    3930         1683 :                 continue;
    3931              : 
    3932          286 :               gfc_ref *ref1 = n->expr->ref;
    3933          286 :               gfc_ref *ref2 = n2->expr->ref;
    3934              : 
    3935              :               /* We know ref1 and ref2 overlap.  We're
    3936              :                  interested in whether ref2 describes a
    3937              :                  smaller part of the array than ref1, which
    3938              :                  we already know refers to the full
    3939              :                  array.  */
    3940              : 
    3941          644 :               while (ref1->next && ref2->next)
    3942              :                 {
    3943              :                   ref1 = ref1->next;
    3944              :                   ref2 = ref2->next;
    3945              :                 }
    3946              : 
    3947          286 :               if (ref2->next
    3948          286 :                   || (ref2->type == REF_ARRAY
    3949          286 :                       && (ref2->u.ar.type == AR_ELEMENT
    3950          286 :                           || (ref2->u.ar.type == AR_SECTION))))
    3951              :                 {
    3952              :                   drop_mapping = true;
    3953              :                   break;
    3954              :                 }
    3955              :             }
    3956          879 :           if (drop_mapping)
    3957          286 :             return true;
    3958              :         }
    3959              :     }
    3960              : 
    3961          905 :   if (mid_desc_p && GOMP_MAP_COPY_FROM_P (OMP_CLAUSE_MAP_KIND (node)))
    3962           82 :     node = NULL_TREE;
    3963              : 
    3964          905 :   node3 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    3965          905 :   OMP_CLAUSE_SET_MAP_KIND (node3, GOMP_MAP_ATTACH_DETACH);
    3966          905 :   OMP_CLAUSE_DECL (node3) = gfc_conv_descriptor_data_get (descr);
    3967              :   /* Similar to gfc_trans_omp_array_section (details
    3968              :      there), we add/keep the cast for OpenMP to prevent
    3969              :      that an 'alloc:' gets added for node3 ('desc.data')
    3970              :      as that is part of the whole descriptor (node3).
    3971              :      TODO: Remove once the ME handles this properly.  */
    3972          905 :   if (!openacc)
    3973          765 :     OMP_CLAUSE_DECL (node3) = fold_convert (TREE_TYPE (TREE_OPERAND (ptr, 0)),
    3974              :                                             OMP_CLAUSE_DECL (node3));
    3975              :   else
    3976          140 :     STRIP_NOPS (OMP_CLAUSE_DECL (node3));
    3977          905 :   OMP_CLAUSE_SIZE (node3) = size_zero_node;
    3978          905 :   if (mid_desc_p)
    3979          172 :     OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (node3) = 1;
    3980              : 
    3981              :   return false;
    3982              : }
    3983              : 
    3984              : static tree
    3985        32910 : gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
    3986              :                        locus where, bool declare_simd = false,
    3987              :                        bool openacc = false, gfc_exec_op op = EXEC_NOP)
    3988              : {
    3989        32910 :   tree omp_clauses = NULL_TREE, prev_clauses, chunk_size, c;
    3990        32910 :   tree iterator = NULL_TREE;
    3991        32910 :   tree tree_block = NULL_TREE;
    3992        32910 :   stmtblock_t iter_block;
    3993        32910 :   int list, ifc;
    3994        32910 :   enum omp_clause_code clause_code;
    3995        32910 :   gfc_omp_namelist *prev = NULL;
    3996        32910 :   gfc_se se;
    3997        32910 :   vec<gfc_symbol *> descriptors = vNULL;
    3998              : 
    3999        32910 :   if (clauses == NULL)
    4000              :     return NULL_TREE;
    4001              : 
    4002        32910 :   hash_map<gfc_symbol *, gfc_omp_namelist *> *sym_rooted_nl = NULL;
    4003              : 
    4004      1316400 :   for (list = 0; list < OMP_LIST_NUM; list++)
    4005              :     {
    4006      1283490 :       gfc_omp_namelist *n = clauses->lists[list];
    4007              : 
    4008      1283490 :       if (n == NULL)
    4009      1255103 :         continue;
    4010        28387 :       switch (list)
    4011              :         {
    4012         3856 :         case OMP_LIST_REDUCTION:
    4013         3856 :         case OMP_LIST_REDUCTION_INSCAN:
    4014         3856 :         case OMP_LIST_REDUCTION_TASK:
    4015         3856 :         case OMP_LIST_IN_REDUCTION:
    4016         3856 :         case OMP_LIST_TASK_REDUCTION:
    4017              :           /* An OpenACC async clause indicates the need to set reduction
    4018              :              arguments addressable, to allow asynchronous copy-out.  */
    4019         3856 :           omp_clauses = gfc_trans_omp_reduction_list (list, n, omp_clauses,
    4020         3856 :                                                       where, clauses->async);
    4021         3856 :           break;
    4022         6499 :         case OMP_LIST_PRIVATE:
    4023         6499 :           clause_code = OMP_CLAUSE_PRIVATE;
    4024         6499 :           goto add_clause;
    4025         1086 :         case OMP_LIST_SHARED:
    4026         1086 :           clause_code = OMP_CLAUSE_SHARED;
    4027         1086 :           goto add_clause;
    4028         1115 :         case OMP_LIST_FIRSTPRIVATE:
    4029         1115 :           clause_code = OMP_CLAUSE_FIRSTPRIVATE;
    4030         1115 :           goto add_clause;
    4031         1698 :         case OMP_LIST_LASTPRIVATE:
    4032         1698 :           clause_code = OMP_CLAUSE_LASTPRIVATE;
    4033         1698 :           goto add_clause;
    4034           96 :         case OMP_LIST_COPYIN:
    4035           96 :           clause_code = OMP_CLAUSE_COPYIN;
    4036           96 :           goto add_clause;
    4037           74 :         case OMP_LIST_COPYPRIVATE:
    4038           74 :           clause_code = OMP_CLAUSE_COPYPRIVATE;
    4039           74 :           goto add_clause;
    4040           61 :         case OMP_LIST_UNIFORM:
    4041           61 :           clause_code = OMP_CLAUSE_UNIFORM;
    4042           61 :           goto add_clause;
    4043           51 :         case OMP_LIST_USE_DEVICE:
    4044           51 :         case OMP_LIST_USE_DEVICE_PTR:
    4045           51 :           clause_code = OMP_CLAUSE_USE_DEVICE_PTR;
    4046           51 :           goto add_clause;
    4047          922 :         case OMP_LIST_USE_DEVICE_ADDR:
    4048          922 :           clause_code = OMP_CLAUSE_USE_DEVICE_ADDR;
    4049          922 :           goto add_clause;
    4050           43 :         case OMP_LIST_IS_DEVICE_PTR:
    4051           43 :           clause_code = OMP_CLAUSE_IS_DEVICE_PTR;
    4052           43 :           goto add_clause;
    4053          113 :         case OMP_LIST_HAS_DEVICE_ADDR:
    4054          113 :           clause_code = OMP_CLAUSE_HAS_DEVICE_ADDR;
    4055          113 :           goto add_clause;
    4056            2 :         case OMP_LIST_NONTEMPORAL:
    4057            2 :           clause_code = OMP_CLAUSE_NONTEMPORAL;
    4058            2 :           goto add_clause;
    4059            9 :         case OMP_LIST_SCAN_IN:
    4060            9 :           clause_code = OMP_CLAUSE_INCLUSIVE;
    4061            9 :           goto add_clause;
    4062            7 :         case OMP_LIST_SCAN_EX:
    4063            7 :           clause_code = OMP_CLAUSE_EXCLUSIVE;
    4064            7 :           goto add_clause;
    4065            4 :         case OMP_LIST_USE:
    4066            4 :           clause_code = OMP_CLAUSE_USE;
    4067            4 :           goto add_clause;
    4068           55 :         case OMP_LIST_INTEROP:
    4069           55 :           clause_code = OMP_CLAUSE_INTEROP;
    4070           55 :           goto add_clause;
    4071              : 
    4072        11835 :         add_clause:
    4073        11835 :           omp_clauses
    4074        11835 :             = gfc_trans_omp_variable_list (clause_code, n, omp_clauses,
    4075              :                                            declare_simd);
    4076        11835 :           break;
    4077              : 
    4078              :         case OMP_LIST_DESTROY:
    4079           12 :           for (; n != NULL; n = n->next)
    4080            9 :             if (n->sym->attr.referenced)
    4081              :               {
    4082            9 :                 tree t = gfc_trans_omp_variable (n->sym, declare_simd);
    4083            9 :                 if (t != error_mark_node)
    4084              :                   {
    4085            9 :                     tree node
    4086            9 :                       = build_omp_clause (input_location, OMP_CLAUSE_DESTROY);
    4087            9 :                     OMP_CLAUSE_DECL (node) = t;
    4088            9 :                     TREE_ADDRESSABLE (OMP_CLAUSE_DECL (node)) = 1;
    4089            9 :                     omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    4090              :                   }
    4091              :               }
    4092              :           break;
    4093              : 
    4094              :         case OMP_LIST_INIT:
    4095              :           {
    4096              :             tree pref_type = NULL_TREE;
    4097              :             const char *last = NULL;
    4098           32 :             for (; n != NULL; n = n->next)
    4099           26 :               if (n->sym->attr.referenced)
    4100              :                 {
    4101           26 :                   tree t = gfc_trans_omp_variable (n->sym, false);
    4102           26 :                   if (t == error_mark_node)
    4103            0 :                     continue;
    4104           26 :                   tree node = build_omp_clause (input_location,
    4105              :                                                 OMP_CLAUSE_INIT);
    4106           26 :                   OMP_CLAUSE_DECL (node) = t;
    4107           26 :                   TREE_ADDRESSABLE (OMP_CLAUSE_DECL (node)) = 1;
    4108           26 :                   if (n->u.init.target)
    4109           19 :                     OMP_CLAUSE_INIT_TARGET (node) = 1;
    4110           26 :                   if (n->u.init.targetsync)
    4111           10 :                     OMP_CLAUSE_INIT_TARGETSYNC (node) = 1;
    4112           26 :                   if (last != n->u2.init_interop)
    4113              :                     {
    4114            6 :                       last = n->u2.init_interop;
    4115            6 :                       if (n->u2.init_interop == NULL)
    4116              :                         pref_type = NULL_TREE;
    4117              :                       else
    4118              :                         {
    4119            5 :                           pref_type = build_string (n->u.init.len,
    4120              :                                                     n->u2.init_interop);
    4121            5 :                           TREE_TYPE (pref_type)
    4122           10 :                             = build_array_type_nelts (unsigned_char_type_node,
    4123            5 :                                                       n->u.init.len);
    4124              :                         }
    4125              :                     }
    4126           26 :                   OMP_CLAUSE_INIT_PREFER_TYPE (node) = pref_type;
    4127           26 :                   omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    4128              :                 }
    4129              :             break;
    4130              :           }
    4131              : 
    4132              :         case OMP_LIST_ALIGNED:
    4133          256 :           for (; n != NULL; n = n->next)
    4134          149 :             if (n->sym->attr.referenced || declare_simd)
    4135              :               {
    4136          149 :                 tree t = gfc_trans_omp_variable (n->sym, declare_simd);
    4137          149 :                 if (t != error_mark_node)
    4138              :                   {
    4139          149 :                     tree node = build_omp_clause (input_location,
    4140              :                                                   OMP_CLAUSE_ALIGNED);
    4141          149 :                     OMP_CLAUSE_DECL (node) = t;
    4142          149 :                     if (n->expr)
    4143              :                       {
    4144          148 :                         tree alignment_var;
    4145              : 
    4146          148 :                         if (declare_simd)
    4147            5 :                           alignment_var = gfc_conv_constant_to_tree (n->expr);
    4148              :                         else
    4149              :                           {
    4150          143 :                             gfc_init_se (&se, NULL);
    4151          143 :                             gfc_conv_expr (&se, n->expr);
    4152          143 :                             gfc_add_block_to_block (block, &se.pre);
    4153          143 :                             alignment_var = gfc_evaluate_now (se.expr, block);
    4154          143 :                             gfc_add_block_to_block (block, &se.post);
    4155              :                           }
    4156          148 :                         OMP_CLAUSE_ALIGNED_ALIGNMENT (node) = alignment_var;
    4157              :                       }
    4158          149 :                     omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    4159              :                   }
    4160              :               }
    4161              :           break;
    4162              :         case OMP_LIST_ALLOCATE:
    4163              :           {
    4164              :             tree allocator_ = NULL_TREE;
    4165              :             gfc_expr *alloc_expr = NULL;
    4166          679 :             for (; n != NULL; n = n->next)
    4167          429 :               if (n->sym->attr.referenced)
    4168              :                 {
    4169          429 :                   tree t = gfc_trans_omp_variable (n->sym, false);
    4170          429 :                   if (t != error_mark_node)
    4171              :                     {
    4172          429 :                       tree node = build_omp_clause (input_location,
    4173              :                                                     OMP_CLAUSE_ALLOCATE);
    4174          429 :                       OMP_CLAUSE_DECL (node) = t;
    4175          429 :                       if (n->u2.allocator)
    4176              :                         {
    4177          294 :                           if (alloc_expr != n->u2.allocator)
    4178              :                             {
    4179          170 :                               gfc_init_se (&se, NULL);
    4180          170 :                               gfc_conv_expr (&se, n->u2.allocator);
    4181          170 :                               gfc_add_block_to_block (block, &se.pre);
    4182          170 :                               t = se.expr;
    4183          170 :                               if (DECL_P (t) && se.post.head == NULL_TREE)
    4184           22 :                                 allocator_ = (POINTER_TYPE_P (TREE_TYPE (t))
    4185           11 :                                               ? build_fold_indirect_ref (t): t);
    4186              :                               else
    4187          159 :                                 allocator_ = gfc_evaluate_now (t, block);
    4188          170 :                               gfc_add_block_to_block (block, &se.post);
    4189              :                             }
    4190          294 :                           OMP_CLAUSE_ALLOCATE_ALLOCATOR (node) = allocator_;
    4191              :                         }
    4192          429 :                       alloc_expr = n->u2.allocator;
    4193          429 :                       if (n->u.align)
    4194              :                         {
    4195           51 :                           tree align_;
    4196           51 :                           gfc_init_se (&se, NULL);
    4197           51 :                           gfc_conv_expr (&se, n->u.align);
    4198           51 :                           gcc_assert (CONSTANT_CLASS_P (se.expr)
    4199              :                                       && se.pre.head == NULL
    4200              :                                       && se.post.head == NULL);
    4201           51 :                           align_ = se.expr;
    4202           51 :                           OMP_CLAUSE_ALLOCATE_ALIGN (node) = align_;
    4203              :                         }
    4204          429 :                       omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    4205              :                     }
    4206              :                 }
    4207              :               else
    4208            0 :                 alloc_expr = n->u2.allocator;
    4209              :             }
    4210              :           break;
    4211              :         case OMP_LIST_LINEAR:
    4212              :           {
    4213              :             gfc_expr *last_step_expr = NULL;
    4214              :             tree last_step = NULL_TREE;
    4215              :             bool last_step_parm = false;
    4216              : 
    4217         1290 :             for (; n != NULL; n = n->next)
    4218              :               {
    4219          796 :                 if (n->expr)
    4220              :                   {
    4221          777 :                     last_step_expr = n->expr;
    4222          777 :                     last_step = NULL_TREE;
    4223          777 :                     last_step_parm = false;
    4224              :                   }
    4225          796 :                 if (n->sym->attr.referenced || declare_simd)
    4226              :                   {
    4227          796 :                     tree t = gfc_trans_omp_variable (n->sym, declare_simd);
    4228          796 :                     if (t != error_mark_node)
    4229              :                       {
    4230          796 :                         tree node = build_omp_clause (input_location,
    4231              :                                                       OMP_CLAUSE_LINEAR);
    4232          796 :                         OMP_CLAUSE_DECL (node) = t;
    4233          796 :                         omp_clause_linear_kind kind;
    4234          796 :                         switch (n->u.linear.op)
    4235              :                           {
    4236              :                           case OMP_LINEAR_DEFAULT:
    4237              :                             kind = OMP_CLAUSE_LINEAR_DEFAULT;
    4238              :                             break;
    4239              :                           case OMP_LINEAR_REF:
    4240              :                             kind = OMP_CLAUSE_LINEAR_REF;
    4241              :                             break;
    4242              :                           case OMP_LINEAR_VAL:
    4243              :                             kind = OMP_CLAUSE_LINEAR_VAL;
    4244              :                             break;
    4245              :                           case OMP_LINEAR_UVAL:
    4246              :                             kind = OMP_CLAUSE_LINEAR_UVAL;
    4247              :                             break;
    4248            0 :                           default:
    4249            0 :                             gcc_unreachable ();
    4250              :                           }
    4251          796 :                         OMP_CLAUSE_LINEAR_KIND (node) = kind;
    4252          796 :                         OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (node)
    4253          796 :                           = n->u.linear.old_modifier;
    4254          796 :                         if (last_step_expr && last_step == NULL_TREE)
    4255              :                           {
    4256          777 :                             if (!declare_simd)
    4257              :                               {
    4258          695 :                                 gfc_init_se (&se, NULL);
    4259          695 :                                 gfc_conv_expr (&se, last_step_expr);
    4260          695 :                                 gfc_add_block_to_block (block, &se.pre);
    4261          695 :                                 last_step = gfc_evaluate_now (se.expr, block);
    4262          695 :                                 gfc_add_block_to_block (block, &se.post);
    4263              :                               }
    4264           82 :                             else if (last_step_expr->expr_type == EXPR_VARIABLE)
    4265              :                               {
    4266            2 :                                 gfc_symbol *s = last_step_expr->symtree->n.sym;
    4267            2 :                                 last_step = gfc_trans_omp_variable (s, true);
    4268            2 :                                 last_step_parm = true;
    4269              :                               }
    4270              :                             else
    4271           80 :                               last_step
    4272           80 :                                 = gfc_conv_constant_to_tree (last_step_expr);
    4273              :                           }
    4274          796 :                         if (last_step_parm)
    4275              :                           {
    4276            2 :                             OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (node) = 1;
    4277            2 :                             OMP_CLAUSE_LINEAR_STEP (node) = last_step;
    4278              :                           }
    4279              :                         else
    4280              :                           {
    4281          794 :                             if (kind == OMP_CLAUSE_LINEAR_REF)
    4282              :                               {
    4283           34 :                                 tree type;
    4284           34 :                                 if (n->sym->attr.flavor == FL_PROCEDURE)
    4285              :                                   {
    4286            0 :                                     type = gfc_get_function_type (n->sym);
    4287            0 :                                     type = build_pointer_type (type);
    4288              :                                   }
    4289              :                                 else
    4290           34 :                                   type = gfc_sym_type (n->sym);
    4291           34 :                                 if (POINTER_TYPE_P (type))
    4292           34 :                                   type = TREE_TYPE (type);
    4293              :                                 /* Otherwise to be determined what exactly
    4294              :                                    should be done.  */
    4295           34 :                                 tree t = fold_convert (sizetype, last_step);
    4296           34 :                                 t = size_binop (MULT_EXPR, t,
    4297              :                                                 TYPE_SIZE_UNIT (type));
    4298           34 :                                 OMP_CLAUSE_LINEAR_STEP (node) = t;
    4299              :                               }
    4300              :                             else
    4301              :                               {
    4302          760 :                                 tree type
    4303          760 :                                   = gfc_typenode_for_spec (&n->sym->ts);
    4304          760 :                                 OMP_CLAUSE_LINEAR_STEP (node)
    4305         1520 :                                   = fold_convert (type, last_step);
    4306              :                               }
    4307              :                           }
    4308          796 :                         if (n->sym->attr.dimension || n->sym->attr.allocatable)
    4309          222 :                           OMP_CLAUSE_LINEAR_ARRAY (node) = 1;
    4310          796 :                         omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    4311              :                       }
    4312              :                   }
    4313              :               }
    4314              :           }
    4315              :           break;
    4316              :         case OMP_LIST_AFFINITY:
    4317              :         case OMP_LIST_DEPEND:
    4318              :           iterator = NULL_TREE;
    4319              :           prev = NULL;
    4320              :           prev_clauses = omp_clauses;
    4321         1582 :           for (; n != NULL; n = n->next)
    4322              :             {
    4323          857 :               if (iterator && prev->u2.ns != n->u2.ns)
    4324              :                 {
    4325           12 :                   finish_iterator_group (list, &iter_block, tree_block,
    4326              :                                          iterator, omp_clauses, prev_clauses);
    4327           12 :                   prev_clauses = omp_clauses;
    4328           12 :                   iterator = NULL_TREE;
    4329              :                 }
    4330          857 :               if (n->u2.ns && (!prev || prev->u2.ns != n->u2.ns))
    4331              : 
    4332           46 :                 iterator = start_iterator_group (n->u2.ns, block, &iter_block,
    4333              :                                                  tree_block);
    4334          857 :               if (!iterator)
    4335          802 :                 gfc_init_block (&iter_block);
    4336          857 :               prev = n;
    4337          857 :               if (list == OMP_LIST_DEPEND
    4338          831 :                   && (n->u.depend_doacross_op == OMP_DOACROSS_SINK_FIRST
    4339          831 :                       || n->u.depend_doacross_op == OMP_DEPEND_SINK_FIRST))
    4340              :                 {
    4341          228 :                   tree vec = NULL_TREE;
    4342          228 :                   unsigned int i;
    4343          228 :                   bool is_depend
    4344              :                     = n->u.depend_doacross_op == OMP_DEPEND_SINK_FIRST;
    4345          228 :                   for (i = 0; ; i++)
    4346              :                     {
    4347         1219 :                       tree addend = integer_zero_node, t;
    4348         1219 :                       bool neg = false;
    4349         1219 :                       if (n->sym && n->expr)
    4350              :                         {
    4351          558 :                           addend = gfc_conv_constant_to_tree (n->expr);
    4352          558 :                           if (TREE_CODE (addend) == INTEGER_CST
    4353          558 :                               && tree_int_cst_sgn (addend) == -1)
    4354              :                             {
    4355          407 :                               neg = true;
    4356          407 :                               addend = const_unop (NEGATE_EXPR,
    4357          407 :                                                    TREE_TYPE (addend), addend);
    4358              :                             }
    4359              :                         }
    4360              : 
    4361         1219 :                       if (n->sym == NULL)
    4362            0 :                         t = null_pointer_node;  /* "omp_cur_iteration - 1".  */
    4363              :                       else
    4364         1219 :                         t = gfc_trans_omp_variable (n->sym, false);
    4365         1219 :                       if (t != error_mark_node)
    4366              :                         {
    4367         1219 :                           if (i < vec_safe_length (doacross_steps)
    4368          426 :                               && !integer_zerop (addend)
    4369          630 :                               && (*doacross_steps)[i])
    4370              :                             {
    4371          204 :                               tree step = (*doacross_steps)[i];
    4372          204 :                               addend = fold_convert (TREE_TYPE (step), addend);
    4373          204 :                               addend = build2 (TRUNC_DIV_EXPR,
    4374          204 :                                                TREE_TYPE (step), addend, step);
    4375              :                             }
    4376         1219 :                           vec = tree_cons (addend, t, vec);
    4377         1219 :                           if (neg)
    4378          407 :                             OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
    4379              :                         }
    4380         1219 :                       if (n->next == NULL
    4381         1057 :                           || n->next->u.depend_doacross_op != OMP_DOACROSS_SINK)
    4382              :                         break;
    4383          991 :                       n = n->next;
    4384          991 :                     }
    4385          228 :                   if (vec == NULL_TREE)
    4386            0 :                     continue;
    4387              : 
    4388          228 :                   tree node = build_omp_clause (input_location,
    4389              :                                                 OMP_CLAUSE_DOACROSS);
    4390          228 :                   OMP_CLAUSE_DOACROSS_KIND (node) = OMP_CLAUSE_DOACROSS_SINK;
    4391          228 :                   OMP_CLAUSE_DOACROSS_DEPEND (node) = is_depend;
    4392          228 :                   OMP_CLAUSE_DECL (node) = nreverse (vec);
    4393          228 :                   omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    4394          228 :                   continue;
    4395          228 :                 }
    4396              : 
    4397          629 :               if (n->sym && !n->sym->attr.referenced)
    4398            0 :                 continue;
    4399              : 
    4400          655 :               tree node = build_omp_clause (input_location,
    4401              :                                             list == OMP_LIST_DEPEND
    4402              :                                             ? OMP_CLAUSE_DEPEND
    4403              :                                             : OMP_CLAUSE_AFFINITY);
    4404          629 :               if (n->sym == NULL)  /* omp_all_memory  */
    4405            9 :                 OMP_CLAUSE_DECL (node) = null_pointer_node;
    4406          620 :               else if (n->expr == NULL || n->expr->ref->u.ar.type == AR_FULL)
    4407              :                 {
    4408          404 :                   tree decl = gfc_trans_omp_variable (n->sym, false);
    4409          404 :                   if (gfc_omp_privatize_by_reference (decl))
    4410           62 :                     decl = build_fold_indirect_ref (decl);
    4411          404 :                   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
    4412              :                     {
    4413           23 :                       decl = gfc_conv_descriptor_data_get (decl);
    4414           23 :                       gcc_assert (POINTER_TYPE_P (TREE_TYPE (decl)));
    4415           23 :                       decl = build_fold_indirect_ref (decl);
    4416              :                     }
    4417          381 :                   else if (n->sym->attr.allocatable || n->sym->attr.pointer)
    4418           22 :                     decl = build_fold_indirect_ref (decl);
    4419          359 :                   else if (DECL_P (decl))
    4420          326 :                     TREE_ADDRESSABLE (decl) = 1;
    4421          404 :                   OMP_CLAUSE_DECL (node) = decl;
    4422          404 :                 }
    4423              :               else
    4424              :                 {
    4425          216 :                   tree ptr;
    4426          216 :                   gfc_init_se (&se, NULL);
    4427              :                   /* The first ref can be an element selection on the base
    4428              :                      object while the full expression still denotes an array,
    4429              :                      e.g. x(j)%a.  Pick the lowering path from the overall
    4430              :                      expression rank, not from the first REF_ARRAY.  */
    4431          216 :                   if (n->expr->rank == 0)
    4432              :                     {
    4433          135 :                       gfc_conv_expr_reference (&se, n->expr);
    4434          135 :                       ptr = se.expr;
    4435              :                     }
    4436              :                   else
    4437              :                     {
    4438           81 :                       gfc_conv_expr_descriptor (&se, n->expr);
    4439           81 :                       ptr = gfc_conv_array_data (se.expr);
    4440              :                     }
    4441          216 :                   gfc_add_block_to_block (&iter_block, &se.pre);
    4442          216 :                   gfc_add_block_to_block (&iter_block, &se.post);
    4443          216 :                   gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
    4444          216 :                   OMP_CLAUSE_DECL (node) = build_fold_indirect_ref (ptr);
    4445              :                 }
    4446          629 :               if (list == OMP_LIST_DEPEND)
    4447          603 :                 switch (n->u.depend_doacross_op)
    4448              :                   {
    4449          228 :                   case OMP_DEPEND_IN:
    4450          228 :                     OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_IN;
    4451          228 :                     break;
    4452          258 :                   case OMP_DEPEND_OUT:
    4453          258 :                     OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_OUT;
    4454          258 :                     break;
    4455           55 :                   case OMP_DEPEND_INOUT:
    4456           55 :                     OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_INOUT;
    4457           55 :                     break;
    4458            9 :                   case OMP_DEPEND_INOUTSET:
    4459            9 :                     OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_INOUTSET;
    4460            9 :                     break;
    4461           15 :                   case OMP_DEPEND_MUTEXINOUTSET:
    4462           15 :                     OMP_CLAUSE_DEPEND_KIND (node)
    4463           15 :                       = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
    4464           15 :                     break;
    4465           38 :                   case OMP_DEPEND_DEPOBJ:
    4466           38 :                     OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_DEPOBJ;
    4467           38 :                     break;
    4468            0 :                   default:
    4469            0 :                     gcc_unreachable ();
    4470              :                   }
    4471          629 :               if (!iterator)
    4472          574 :                 gfc_add_block_to_block (block, &iter_block);
    4473          629 :               omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    4474              :             }
    4475          725 :           if (iterator)
    4476           34 :             finish_iterator_group (list, &iter_block, tree_block,
    4477              :                                    iterator, omp_clauses, prev_clauses);
    4478              :           break;
    4479              :         case OMP_LIST_MAP:
    4480              :           iterator = NULL_TREE;
    4481              :           prev = NULL;
    4482              :           prev_clauses = omp_clauses;
    4483        25297 :           for (; n != NULL; n = n->next)
    4484              :             {
    4485        16070 :               if (!openacc)
    4486              :                 {
    4487         7486 :                   if (n->u3.udm)
    4488            8 :                     gfc_error ("Sorry, declared mapper %qs, used for %qs at %L, "
    4489              :                                "is not yet supported",
    4490            8 :                                n->u3.udm->requested_mapper_id[0] != '\0'
    4491              :                                ? n->u3.udm->requested_mapper_id : "default",
    4492            8 :                                n->sym->name, &n->where);
    4493              : 
    4494              :                   // Remove duplicates
    4495         7486 :                   bool skip = false;
    4496        17234 :                   for (gfc_omp_namelist *n2 = n->next; n2 != NULL;
    4497         9748 :                        n2 = n2->next)
    4498              :                     {
    4499        10017 :                       if (n2->sym == n->sym
    4500        10017 :                           && gfc_dep_compare_expr (n2->expr, n->expr) == 0)
    4501              :                         {
    4502          315 :                           if (n2->u.map.op == n->u.map.op)
    4503              :                             {
    4504              :                               skip = true;
    4505              :                               break;
    4506              :                             }
    4507          297 :                           else if ((n2->u.map.op & ~OMP_MAP_TOFROM)
    4508          297 :                                    == (n->u.map.op & ~OMP_MAP_TOFROM))
    4509              :                             {
    4510          251 :                               n2->u.map.op = (enum gfc_omp_map_op) (
    4511          251 :                                 n->u.map.op | n2->u.map.op);
    4512          251 :                               skip = true;
    4513          251 :                               break;
    4514              :                             }
    4515              :                         }
    4516              :                     }
    4517         7486 :                   if (skip)
    4518          564 :                     continue;
    4519              :                 }
    4520              : 
    4521        15801 :               if (!n->sym->attr.referenced
    4522        15801 :                   || n->sym->attr.flavor == FL_PARAMETER)
    4523            9 :                 continue;
    4524              : 
    4525        15792 :               if (iterator && prev->u2.ns != n->u2.ns)
    4526              :                 {
    4527           25 :                   finish_iterator_group (list, &iter_block, tree_block,
    4528              :                                          iterator, omp_clauses, prev_clauses);
    4529           25 :                   prev_clauses = omp_clauses;
    4530           25 :                   iterator = NULL_TREE;
    4531              :                 }
    4532        15792 :               if (n->u2.ns && (!prev || prev->u2.ns != n->u2.ns))
    4533              :                 {
    4534           37 :                   iterator = start_iterator_group (n->u2.ns, block,
    4535              :                                                    &iter_block, tree_block);
    4536           37 :                   prev_clauses = omp_clauses;
    4537              :                 }
    4538        15792 :               if (!iterator)
    4539        15753 :                 gfc_init_block (&iter_block);
    4540        15792 :               prev = n;
    4541              : 
    4542        15792 :               location_t map_loc = gfc_get_location (&n->where);
    4543        15792 :               bool always_modifier = false;
    4544        15792 :               tree node = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    4545        15792 :               tree node2 = NULL_TREE;
    4546        15792 :               tree node3 = NULL_TREE;
    4547        15792 :               tree node4 = NULL_TREE;
    4548        15792 :               tree node5 = NULL_TREE;
    4549              : 
    4550              :               /* OpenMP: automatically map pointer targets with the pointer;
    4551              :                  hence, always update the descriptor/pointer itself.  */
    4552        15792 :               if (!openacc
    4553        15792 :                   && ((n->expr == NULL && n->sym->attr.pointer)
    4554        15792 :                       || (n->expr && gfc_expr_attr (n->expr).pointer)))
    4555              :                 always_modifier = true;
    4556              : 
    4557        15792 :               if (n->u.map.readonly)
    4558           22 :                 OMP_CLAUSE_MAP_READONLY (node) = 1;
    4559              : 
    4560        15792 :               switch (n->u.map.op)
    4561              :                 {
    4562         1098 :                 case OMP_MAP_ALLOC:
    4563         1098 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALLOC);
    4564         1098 :                   break;
    4565           64 :                 case OMP_MAP_IF_PRESENT:
    4566           64 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_IF_PRESENT);
    4567           64 :                   break;
    4568           66 :                 case OMP_MAP_ATTACH:
    4569           66 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ATTACH);
    4570           66 :                   break;
    4571         4363 :                 case OMP_MAP_TO:
    4572         4363 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_TO);
    4573         4363 :                   break;
    4574         3113 :                 case OMP_MAP_FROM:
    4575         3113 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FROM);
    4576         3113 :                   break;
    4577         4569 :                 case OMP_MAP_TOFROM:
    4578         4569 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_TOFROM);
    4579         4569 :                   break;
    4580           35 :                 case OMP_MAP_ALWAYS_TO:
    4581           35 :                   always_modifier = true;
    4582           35 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_TO);
    4583           35 :                   break;
    4584           17 :                 case OMP_MAP_ALWAYS_FROM:
    4585           17 :                   always_modifier = true;
    4586           17 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_FROM);
    4587           17 :                   break;
    4588          172 :                 case OMP_MAP_ALWAYS_TOFROM:
    4589          172 :                   always_modifier = true;
    4590          172 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_TOFROM);
    4591          172 :                   break;
    4592           15 :                 case OMP_MAP_PRESENT_ALLOC:
    4593           15 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_PRESENT_ALLOC);
    4594           15 :                   break;
    4595           14 :                 case OMP_MAP_PRESENT_TO:
    4596           14 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_PRESENT_TO);
    4597           14 :                   break;
    4598            5 :                 case OMP_MAP_PRESENT_FROM:
    4599            5 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_PRESENT_FROM);
    4600            5 :                   break;
    4601            3 :                 case OMP_MAP_PRESENT_TOFROM:
    4602            3 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_PRESENT_TOFROM);
    4603            3 :                   break;
    4604           10 :                 case OMP_MAP_ALWAYS_PRESENT_TO:
    4605           10 :                   always_modifier = true;
    4606           10 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_PRESENT_TO);
    4607           10 :                   break;
    4608            4 :                 case OMP_MAP_ALWAYS_PRESENT_FROM:
    4609            4 :                   always_modifier = true;
    4610            4 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_PRESENT_FROM);
    4611            4 :                   break;
    4612            2 :                 case OMP_MAP_ALWAYS_PRESENT_TOFROM:
    4613            2 :                   always_modifier = true;
    4614            2 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_PRESENT_TOFROM);
    4615            2 :                   break;
    4616          457 :                 case OMP_MAP_RELEASE:
    4617          457 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_RELEASE);
    4618          457 :                   break;
    4619           80 :                 case OMP_MAP_DELETE:
    4620           80 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_DELETE);
    4621           80 :                   break;
    4622           44 :                 case OMP_MAP_DETACH:
    4623           44 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_DETACH);
    4624           44 :                   break;
    4625           64 :                 case OMP_MAP_FORCE_ALLOC:
    4626           64 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_ALLOC);
    4627           64 :                   break;
    4628          465 :                 case OMP_MAP_FORCE_TO:
    4629          465 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_TO);
    4630          465 :                   break;
    4631          577 :                 case OMP_MAP_FORCE_FROM:
    4632          577 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_FROM);
    4633          577 :                   break;
    4634            0 :                 case OMP_MAP_FORCE_TOFROM:
    4635            0 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_TOFROM);
    4636            0 :                   break;
    4637          545 :                 case OMP_MAP_FORCE_PRESENT:
    4638          545 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_PRESENT);
    4639          545 :                   break;
    4640           10 :                 case OMP_MAP_FORCE_DEVICEPTR:
    4641           10 :                   OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_DEVICEPTR);
    4642           10 :                   break;
    4643            0 :                 default:
    4644            0 :                   gcc_unreachable ();
    4645              :                 }
    4646              : 
    4647        15792 :               tree decl = gfc_trans_omp_variable (n->sym, false);
    4648        15792 :               if (DECL_P (decl))
    4649        15792 :                 TREE_ADDRESSABLE (decl) = 1;
    4650              : 
    4651        15792 :               gfc_ref *lastref = NULL;
    4652              : 
    4653        15792 :               if (n->expr)
    4654        15022 :                 for (gfc_ref *ref = n->expr->ref; ref; ref = ref->next)
    4655         9215 :                   if (ref->type == REF_COMPONENT || ref->type == REF_ARRAY)
    4656         9215 :                     lastref = ref;
    4657              : 
    4658         5807 :               bool allocatable = false, pointer = false;
    4659              : 
    4660         5807 :               if (lastref && lastref->type == REF_COMPONENT)
    4661              :                 {
    4662          457 :                   gfc_component *c = lastref->u.c.component;
    4663              : 
    4664          457 :                   if (c->ts.type == BT_CLASS)
    4665              :                     {
    4666           24 :                       pointer = CLASS_DATA (c)->attr.class_pointer;
    4667           24 :                       allocatable = CLASS_DATA (c)->attr.allocatable;
    4668              :                     }
    4669              :                   else
    4670              :                     {
    4671          433 :                       pointer = c->attr.pointer;
    4672          433 :                       allocatable = c->attr.allocatable;
    4673              :                     }
    4674              :                 }
    4675              : 
    4676        15792 :               if (n->expr == NULL
    4677         5807 :                   || (n->expr->ref->type == REF_ARRAY
    4678         3677 :                       && n->expr->ref->u.ar.type == AR_FULL))
    4679              :                 {
    4680         9985 :                   gomp_map_kind map_kind;
    4681         9985 :                   tree type = TREE_TYPE (decl);
    4682         9985 :                   if (n->sym->ts.type == BT_CHARACTER
    4683          218 :                       && n->sym->ts.deferred
    4684           92 :                       && (n->sym->attr.omp_declare_target
    4685           84 :                           || n->sym->attr.omp_declare_target_link
    4686           84 :                           || n->sym->attr.omp_declare_target_local)
    4687            8 :                       && (always_modifier || n->sym->attr.pointer)
    4688            8 :                       && op != EXEC_OMP_TARGET_EXIT_DATA
    4689            4 :                       && n->u.map.op != OMP_MAP_DELETE
    4690            4 :                       && n->u.map.op != OMP_MAP_RELEASE)
    4691              :                     {
    4692            4 :                       gcc_assert (n->sym->ts.u.cl->backend_decl);
    4693            4 :                       node5 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    4694            4 :                       OMP_CLAUSE_SET_MAP_KIND (node5, GOMP_MAP_ALWAYS_TO);
    4695            4 :                       OMP_CLAUSE_DECL (node5) = n->sym->ts.u.cl->backend_decl;
    4696            4 :                       OMP_CLAUSE_SIZE (node5)
    4697            8 :                         = TYPE_SIZE_UNIT (gfc_charlen_type_node);
    4698              :                     }
    4699              : 
    4700         9985 :                   tree present = gfc_omp_check_optional_argument (decl, true);
    4701         9985 :                   if (openacc && n->sym->ts.type == BT_CLASS)
    4702              :                     {
    4703           60 :                       if (n->sym->attr.optional)
    4704            0 :                         sorry_at (gfc_get_location (&n->where),
    4705              :                                   "optional class parameter");
    4706           60 :                       tree ptr = gfc_class_data_get (decl);
    4707           60 :                       ptr = build_fold_indirect_ref (ptr);
    4708           60 :                       OMP_CLAUSE_DECL (node) = ptr;
    4709           60 :                       OMP_CLAUSE_SIZE (node) = gfc_class_vtab_size_get (decl);
    4710           60 :                       node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    4711           60 :                       OMP_CLAUSE_SET_MAP_KIND (node2, GOMP_MAP_ATTACH_DETACH);
    4712           60 :                       OMP_CLAUSE_DECL (node2) = gfc_class_data_get (decl);
    4713           60 :                       OMP_CLAUSE_SIZE (node2) = size_int (0);
    4714           60 :                       goto finalize_map_clause;
    4715              :                     }
    4716         9925 :                   else if (POINTER_TYPE_P (type)
    4717         9925 :                            && (gfc_omp_privatize_by_reference (decl)
    4718          510 :                                || GFC_DECL_GET_SCALAR_POINTER (decl)
    4719          325 :                                || GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
    4720           86 :                                || GFC_DECL_CRAY_POINTEE (decl)
    4721           86 :                                || GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type))
    4722           86 :                                || (n->sym->ts.type == BT_DERIVED
    4723            8 :                                    && (n->sym->ts.u.derived->ts.f90_type
    4724              :                                        != BT_VOID))))
    4725              :                     {
    4726         3448 :                       tree orig_decl = decl;
    4727         3448 :                       bool bare_attach_detach
    4728              :                         = (openacc
    4729         1252 :                            && (n->u.map.op == OMP_MAP_ATTACH
    4730         1252 :                                || n->u.map.op == OMP_MAP_DETACH)
    4731            4 :                            && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
    4732         3452 :                            && !(POINTER_TYPE_P (TREE_TYPE (decl))
    4733            4 :                                 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE
    4734         3448 :                                                           (TREE_TYPE (decl)))));
    4735              : 
    4736              :                       /* For nonallocatable, nonpointer arrays, a temporary
    4737              :                          variable is generated, but this one is only defined if
    4738              :                          the variable is present; hence, we now set it to NULL
    4739              :                          to avoid accessing undefined variables.  We cannot use
    4740              :                          a temporary variable here as otherwise the replacement
    4741              :                          of the variables in omp-low.cc will not work.  */
    4742         3448 :                       if (present && GFC_ARRAY_TYPE_P (type))
    4743              :                         {
    4744          284 :                           tree tmp = fold_build2_loc (input_location,
    4745              :                                                       MODIFY_EXPR,
    4746              :                                                       void_type_node, decl,
    4747              :                                                       null_pointer_node);
    4748          284 :                           tree cond = fold_build1_loc (input_location,
    4749              :                                                        TRUTH_NOT_EXPR,
    4750              :                                                        boolean_type_node,
    4751              :                                                        present);
    4752          284 :                           gfc_add_expr_to_block (&iter_block,
    4753              :                                                  build3_loc (input_location,
    4754              :                                                              COND_EXPR,
    4755              :                                                              void_type_node,
    4756              :                                                              cond, tmp,
    4757              :                                                              NULL_TREE));
    4758              :                         }
    4759              :                       /* Bare OpenACC attach/detach on scalar pointer-like
    4760              :                          variables wants a single attach operation on the
    4761              :                          pointer itself, not a standalone pointer-mapping
    4762              :                          node.  Component and descriptor cases have dedicated
    4763              :                          handling below; this covers the plain scalar path.  */
    4764         3448 :                       if (bare_attach_detach)
    4765              :                         {
    4766            4 :                           decl = build_fold_indirect_ref (decl);
    4767            4 :                           OMP_CLAUSE_DECL (node) = build_fold_addr_expr (decl);
    4768            4 :                           OMP_CLAUSE_SIZE (node) = size_zero_node;
    4769            4 :                           goto finalize_map_clause;
    4770              :                         }
    4771              :                       /* For descriptor types, the unmapping happens below.  */
    4772         3444 :                       if (op != EXEC_OMP_TARGET_EXIT_DATA
    4773         3444 :                           || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
    4774              :                         {
    4775         3444 :                           enum gomp_map_kind gmk = GOMP_MAP_POINTER;
    4776         3444 :                           if (op == EXEC_OMP_TARGET_EXIT_DATA
    4777           73 :                               && n->u.map.op == OMP_MAP_DELETE)
    4778              :                             gmk = GOMP_MAP_DELETE;
    4779           62 :                           else if (op == EXEC_OMP_TARGET_EXIT_DATA)
    4780           62 :                             gmk = GOMP_MAP_RELEASE;
    4781         3444 :                           tree size;
    4782         3444 :                           if (gmk == GOMP_MAP_RELEASE || gmk == GOMP_MAP_DELETE)
    4783           73 :                             size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
    4784              :                           else
    4785         3371 :                             size = size_int (0);
    4786         3444 :                           node4 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    4787         3444 :                           OMP_CLAUSE_SET_MAP_KIND (node4, gmk);
    4788         3444 :                           OMP_CLAUSE_DECL (node4) = decl;
    4789         3444 :                           OMP_CLAUSE_SIZE (node4) = size;
    4790              :                         }
    4791         3444 :                       decl = build_fold_indirect_ref (decl);
    4792         3444 :                       if ((TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE
    4793         2216 :                            || gfc_omp_is_optional_argument (orig_decl))
    4794         4496 :                           && (GFC_DECL_GET_SCALAR_POINTER (orig_decl)
    4795         2110 :                               || GFC_DECL_GET_SCALAR_ALLOCATABLE (orig_decl)))
    4796              :                         {
    4797          408 :                           enum gomp_map_kind gmk;
    4798          408 :                           if (op == EXEC_OMP_TARGET_EXIT_DATA
    4799            8 :                               && n->u.map.op == OMP_MAP_DELETE)
    4800              :                             gmk = GOMP_MAP_DELETE;
    4801            6 :                           else if (op == EXEC_OMP_TARGET_EXIT_DATA)
    4802              :                             gmk = GOMP_MAP_RELEASE;
    4803              :                           else
    4804              :                             gmk = GOMP_MAP_POINTER;
    4805          408 :                           tree size;
    4806          408 :                           if (gmk == GOMP_MAP_RELEASE || gmk == GOMP_MAP_DELETE)
    4807            8 :                             size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
    4808              :                           else
    4809          400 :                             size = size_int (0);
    4810          408 :                           node3 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    4811          408 :                           OMP_CLAUSE_SET_MAP_KIND (node3, gmk);
    4812          408 :                           OMP_CLAUSE_DECL (node3) = decl;
    4813          408 :                           OMP_CLAUSE_SIZE (node3) = size;
    4814          408 :                           decl = build_fold_indirect_ref (decl);
    4815              :                         }
    4816              :                     }
    4817         9921 :                   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
    4818              :                     {
    4819         1423 :                       tree type = TREE_TYPE (decl);
    4820         1423 :                       tree ptr = gfc_conv_descriptor_data_get (decl);
    4821         1423 :                       if (present)
    4822          309 :                         ptr = gfc_build_cond_assign_expr (&iter_block,
    4823              :                                                           present, ptr,
    4824              :                                                           null_pointer_node);
    4825         1423 :                       gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
    4826         1423 :                       ptr = build_fold_indirect_ref (ptr);
    4827         1423 :                       OMP_CLAUSE_DECL (node) = ptr;
    4828         1423 :                       node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    4829         1423 :                       OMP_CLAUSE_DECL (node2) = decl;
    4830         1423 :                       OMP_CLAUSE_SIZE (node2) = TYPE_SIZE_UNIT (type);
    4831         1423 :                       if (n->u.map.op == OMP_MAP_DELETE)
    4832              :                         map_kind = GOMP_MAP_DELETE;
    4833         1396 :                       else if (op == EXEC_OMP_TARGET_EXIT_DATA
    4834         1333 :                                || n->u.map.op == OMP_MAP_RELEASE)
    4835              :                         map_kind = GOMP_MAP_RELEASE;
    4836              :                       else
    4837         1423 :                         map_kind = GOMP_MAP_TO_PSET;
    4838         1423 :                       OMP_CLAUSE_SET_MAP_KIND (node2, map_kind);
    4839              : 
    4840         1423 :                       if (op != EXEC_OMP_TARGET_EXIT_DATA
    4841         1333 :                           && n->u.map.op != OMP_MAP_DELETE
    4842         1333 :                           && n->u.map.op != OMP_MAP_RELEASE)
    4843              :                         {
    4844         1285 :                           node3 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    4845         1285 :                           if (present)
    4846              :                             {
    4847          309 :                               ptr = gfc_conv_descriptor_data_get (decl);
    4848          309 :                               ptr = gfc_build_addr_expr (NULL, ptr);
    4849          309 :                               ptr = gfc_build_cond_assign_expr (
    4850              :                                 &iter_block, present, ptr, null_pointer_node);
    4851          309 :                               ptr = build_fold_indirect_ref (ptr);
    4852          309 :                               OMP_CLAUSE_DECL (node3) = ptr;
    4853              :                             }
    4854              :                           else
    4855          976 :                             OMP_CLAUSE_DECL (node3)
    4856         1952 :                               = gfc_conv_descriptor_data_get (decl);
    4857         1285 :                           OMP_CLAUSE_SIZE (node3) = size_int (0);
    4858              : 
    4859         1285 :                           if (n->u.map.op == OMP_MAP_ATTACH)
    4860              :                             {
    4861              :                               /* Standalone attach clauses used with arrays with
    4862              :                                  descriptors must copy the descriptor to the
    4863              :                                  target, else they won't have anything to
    4864              :                                  perform the attachment onto (see OpenACC 2.6,
    4865              :                                  "2.6.3. Data Structures with Pointers").  */
    4866            9 :                               OMP_CLAUSE_SET_MAP_KIND (node3, GOMP_MAP_ATTACH);
    4867              :                               /* We don't want to map PTR at all in this case,
    4868              :                                  so delete its node and shuffle the others
    4869              :                                  down.  */
    4870            9 :                               node = node2;
    4871            9 :                               node2 = node3;
    4872            9 :                               node3 = NULL;
    4873            9 :                               goto finalize_map_clause;
    4874              :                             }
    4875         1276 :                           else if (n->u.map.op == OMP_MAP_DETACH)
    4876              :                             {
    4877            4 :                               OMP_CLAUSE_SET_MAP_KIND (node3, GOMP_MAP_DETACH);
    4878              :                               /* Similarly to above, we don't want to unmap PTR
    4879              :                                  here.  */
    4880            4 :                               node = node2;
    4881            4 :                               node2 = node3;
    4882            4 :                               node3 = NULL;
    4883            4 :                               goto finalize_map_clause;
    4884              :                             }
    4885              :                           else
    4886         2074 :                             OMP_CLAUSE_SET_MAP_KIND (node3,
    4887              :                                                      always_modifier
    4888              :                                                      ? GOMP_MAP_ALWAYS_POINTER
    4889              :                                                      : GOMP_MAP_POINTER);
    4890              :                         }
    4891              : 
    4892              :                       /* We have to check for n->sym->attr.dimension because
    4893              :                          of scalar coarrays.  */
    4894         1410 :                       if ((n->sym->attr.pointer || n->sym->attr.allocatable)
    4895         1410 :                           && n->sym->attr.dimension)
    4896              :                         {
    4897         1410 :                           stmtblock_t cond_block;
    4898         1410 :                           tree size
    4899         1410 :                             = gfc_create_var (gfc_array_index_type, NULL);
    4900         1410 :                           tree tem, then_b, else_b, zero, cond;
    4901              : 
    4902         1410 :                           gfc_init_block (&cond_block);
    4903         1410 :                           tem
    4904         2820 :                             = gfc_full_array_size (&cond_block, decl,
    4905         1410 :                                                    GFC_TYPE_ARRAY_RANK (type));
    4906         1410 :                           tree elemsz;
    4907         1410 :                           if (n->sym->ts.type == BT_CHARACTER
    4908           52 :                               && n->sym->ts.deferred)
    4909              :                             {
    4910           44 :                               tree len = n->sym->ts.u.cl->backend_decl;
    4911           44 :                               len = fold_convert (size_type_node, len);
    4912           44 :                               elemsz = gfc_get_char_type (n->sym->ts.kind);
    4913           44 :                               elemsz = TYPE_SIZE_UNIT (elemsz);
    4914           44 :                               elemsz = fold_build2 (MULT_EXPR, size_type_node,
    4915              :                                                     len, elemsz);
    4916           44 :                             }
    4917              :                           else
    4918         1366 :                             elemsz
    4919         1366 :                               = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    4920         1410 :                           elemsz = fold_convert (gfc_array_index_type, elemsz);
    4921         1410 :                           tem = fold_build2 (MULT_EXPR, gfc_array_index_type,
    4922              :                                              tem, elemsz);
    4923         1410 :                           gfc_add_modify (&cond_block, size, tem);
    4924         1410 :                           then_b = gfc_finish_block (&cond_block);
    4925         1410 :                           gfc_init_block (&cond_block);
    4926         1410 :                           zero = build_int_cst (gfc_array_index_type, 0);
    4927         1410 :                           gfc_add_modify (&cond_block, size, zero);
    4928         1410 :                           else_b = gfc_finish_block (&cond_block);
    4929         1410 :                           tem = gfc_conv_descriptor_data_get (decl);
    4930         1410 :                           tem = fold_convert (pvoid_type_node, tem);
    4931         1410 :                           cond = fold_build2_loc (input_location, NE_EXPR,
    4932              :                                                   boolean_type_node,
    4933              :                                                   tem, null_pointer_node);
    4934         1410 :                           if (present)
    4935          309 :                             cond = fold_build2_loc (input_location,
    4936              :                                                     TRUTH_ANDIF_EXPR,
    4937              :                                                     boolean_type_node,
    4938              :                                                     present, cond);
    4939         1410 :                           gfc_add_expr_to_block (&iter_block,
    4940              :                                                  build3_loc (input_location,
    4941              :                                                              COND_EXPR,
    4942              :                                                              void_type_node,
    4943              :                                                              cond, then_b,
    4944              :                                                              else_b));
    4945         1410 :                           OMP_CLAUSE_SIZE (node) = size;
    4946         1410 :                         }
    4947            0 :                       else if (n->sym->attr.dimension)
    4948              :                         {
    4949            0 :                           stmtblock_t cond_block;
    4950            0 :                           gfc_init_block (&cond_block);
    4951            0 :                           tree size = gfc_full_array_size (&cond_block, decl,
    4952            0 :                                         GFC_TYPE_ARRAY_RANK (type));
    4953            0 :                           tree elemsz
    4954            0 :                             = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    4955            0 :                           elemsz = fold_convert (gfc_array_index_type, elemsz);
    4956            0 :                           size = fold_build2 (MULT_EXPR, gfc_array_index_type,
    4957              :                                               size, elemsz);
    4958            0 :                           size = gfc_evaluate_now (size, &cond_block);
    4959            0 :                           if (present)
    4960              :                             {
    4961            0 :                               tree var = gfc_create_var (gfc_array_index_type,
    4962              :                                                          NULL);
    4963            0 :                               gfc_add_modify (&cond_block, var, size);
    4964            0 :                               tree cond_body = gfc_finish_block (&cond_block);
    4965            0 :                               tree cond = build3_loc (input_location, COND_EXPR,
    4966              :                                                       void_type_node, present,
    4967              :                                                       cond_body, NULL_TREE);
    4968            0 :                               gfc_add_expr_to_block (&iter_block, cond);
    4969            0 :                               OMP_CLAUSE_SIZE (node) = var;
    4970              :                             }
    4971              :                           else
    4972              :                             {
    4973            0 :                               gfc_add_block_to_block (&iter_block, &cond_block);
    4974            0 :                               OMP_CLAUSE_SIZE (node) = size;
    4975              :                             }
    4976              :                         }
    4977              :                     }
    4978         8498 :                   else if (present
    4979          845 :                            && INDIRECT_REF_P (decl)
    4980         9241 :                            && INDIRECT_REF_P (TREE_OPERAND (decl, 0)))
    4981              :                     {
    4982              :                       /* A single indirectref is handled by the middle end.  */
    4983          228 :                       gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
    4984          228 :                       tree tmp = TREE_OPERAND (decl, 0);
    4985          228 :                       tmp = gfc_build_cond_assign_expr (&iter_block,
    4986              :                                                         present, tmp,
    4987              :                                                         null_pointer_node);
    4988          228 :                       OMP_CLAUSE_DECL (node) = build_fold_indirect_ref (tmp);
    4989              :                     }
    4990              :                   else
    4991         8270 :                     OMP_CLAUSE_DECL (node) = decl;
    4992              : 
    4993         9908 :                   if (!n->sym->attr.dimension
    4994         6171 :                       && n->sym->ts.type == BT_CHARACTER
    4995          144 :                       && n->sym->ts.deferred)
    4996              :                     {
    4997           48 :                       if (!DECL_P (decl))
    4998              :                         {
    4999           48 :                           gcc_assert (TREE_CODE (decl) == INDIRECT_REF);
    5000           48 :                           decl = TREE_OPERAND (decl, 0);
    5001              :                         }
    5002           48 :                       tree cond = fold_build2_loc (input_location, NE_EXPR,
    5003              :                                                    boolean_type_node,
    5004              :                                                    decl, null_pointer_node);
    5005           48 :                       if (present)
    5006            2 :                         cond = fold_build2_loc (input_location,
    5007              :                                                 TRUTH_ANDIF_EXPR,
    5008              :                                                 boolean_type_node,
    5009              :                                                 present, cond);
    5010           48 :                       tree len = n->sym->ts.u.cl->backend_decl;
    5011           48 :                       len = fold_convert (size_type_node, len);
    5012           48 :                       tree size = gfc_get_char_type (n->sym->ts.kind);
    5013           48 :                       size = TYPE_SIZE_UNIT (size);
    5014           48 :                       size = fold_build2 (MULT_EXPR, size_type_node, len, size);
    5015           48 :                       size = build3_loc (input_location,
    5016              :                                                          COND_EXPR,
    5017              :                                                          size_type_node,
    5018              :                                                          cond, size,
    5019              :                                                          size_zero_node);
    5020           48 :                       size = gfc_evaluate_now (size, &iter_block);
    5021           48 :                       OMP_CLAUSE_SIZE (node) = size;
    5022              :                     }
    5023         9908 :                   if ((TREE_CODE (decl) != PARM_DECL
    5024          186 :                        || DECL_ARTIFICIAL (OMP_CLAUSE_DECL (node)))
    5025         9722 :                       && n->sym->ts.type == BT_DERIVED
    5026        10454 :                       && n->sym->ts.u.derived->attr.alloc_comp)
    5027              :                     {
    5028              :                       /* Save array descriptor for use in
    5029              :                          gfc_omp_deep_mapping{,_p,_cnt}; force evaluate
    5030              :                          to ensure that it is not gimplified + is a decl.  */
    5031          213 :                       tree tmp = OMP_CLAUSE_SIZE (node);
    5032          213 :                       if (tmp == NULL_TREE)
    5033          230 :                         tmp = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
    5034           46 :                                             : TYPE_SIZE_UNIT (TREE_TYPE (decl));
    5035          213 :                       tree var = gfc_create_var (TREE_TYPE (tmp), NULL);
    5036          213 :                       gfc_add_modify_loc (input_location, &iter_block,
    5037              :                                           var, tmp);
    5038          213 :                       OMP_CLAUSE_SIZE (node) = var;
    5039          213 :                       gfc_allocate_lang_decl (var);
    5040          213 :                       if (TREE_CODE (decl) == INDIRECT_REF)
    5041           48 :                         decl = TREE_OPERAND (decl, 0);
    5042          213 :                       if (TREE_CODE (decl) == INDIRECT_REF)
    5043            2 :                         decl = TREE_OPERAND (decl, 0);
    5044          213 :                       if (DECL_LANG_SPECIFIC (decl)
    5045          213 :                           && GFC_DECL_SAVED_DESCRIPTOR (decl))
    5046            6 :                         GFC_DECL_SAVED_DESCRIPTOR (var)
    5047            2 :                           = GFC_DECL_SAVED_DESCRIPTOR (decl);
    5048              :                       else
    5049          211 :                         GFC_DECL_SAVED_DESCRIPTOR (var) = decl;
    5050              :                     }
    5051              :                 }
    5052         5807 :               else if (n->expr
    5053         5807 :                        && n->expr->expr_type == EXPR_VARIABLE
    5054         5807 :                        && n->expr->ref->type == REF_ARRAY
    5055         3677 :                        && !n->expr->ref->next)
    5056              :                 {
    5057              :                   /* An array element or array section which is not part of a
    5058              :                      derived type, etc.  */
    5059         3389 :                   bool element = n->expr->ref->u.ar.type == AR_ELEMENT;
    5060         3389 :                   tree type = TREE_TYPE (decl);
    5061         3389 :                   gomp_map_kind k = GOMP_MAP_POINTER;
    5062         3389 :                   if (!openacc
    5063          538 :                       && !GFC_DESCRIPTOR_TYPE_P (type)
    5064         3842 :                       && !(POINTER_TYPE_P (type)
    5065          281 :                            && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type))))
    5066              :                     k = GOMP_MAP_FIRSTPRIVATE_POINTER;
    5067         3389 :                   gfc_trans_omp_array_section (&iter_block, op, n, decl,
    5068              :                                                element, !openacc, k,
    5069              :                                                node, node2, node3, node4,
    5070              :                                                iterator);
    5071         3389 :                 }
    5072         2418 :               else if (n->expr
    5073         2418 :                        && n->expr->expr_type == EXPR_VARIABLE
    5074         2418 :                        && (n->expr->ref->type == REF_COMPONENT
    5075              :                            || n->expr->ref->type == REF_ARRAY)
    5076         2418 :                        && lastref
    5077         2418 :                        && lastref->type == REF_COMPONENT
    5078          457 :                        && lastref->u.c.component->ts.type != BT_CLASS
    5079          433 :                        && lastref->u.c.component->ts.type != BT_DERIVED
    5080          340 :                        && !lastref->u.c.component->attr.dimension)
    5081              :                 {
    5082              :                   /* Derived type access with last component being a scalar.  */
    5083          340 :                   gfc_init_se (&se, NULL);
    5084              : 
    5085          340 :                   gfc_conv_expr (&se, n->expr);
    5086          340 :                   gfc_add_block_to_block (&iter_block, &se.pre);
    5087              :                   /* For BT_CHARACTER a pointer is returned.  */
    5088          340 :                   OMP_CLAUSE_DECL (node)
    5089          586 :                     = POINTER_TYPE_P (TREE_TYPE (se.expr))
    5090          340 :                       ? build_fold_indirect_ref (se.expr) : se.expr;
    5091          340 :                   gfc_add_block_to_block (&iter_block, &se.post);
    5092          340 :                   if (pointer || allocatable)
    5093              :                     {
    5094              :                       /* If it's a bare attach/detach clause, we just want
    5095              :                          to perform a single attach/detach operation, of the
    5096              :                          pointer itself, not of the pointed-to object.  */
    5097          161 :                       if (openacc
    5098           68 :                           && (n->u.map.op == OMP_MAP_ATTACH
    5099           50 :                               || n->u.map.op == OMP_MAP_DETACH))
    5100              :                         {
    5101           36 :                           OMP_CLAUSE_DECL (node)
    5102           36 :                             = build_fold_addr_expr (OMP_CLAUSE_DECL (node));
    5103           36 :                           OMP_CLAUSE_SIZE (node) = size_zero_node;
    5104           36 :                           goto finalize_map_clause;
    5105              :                         }
    5106              : 
    5107          125 :                       node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    5108          125 :                       OMP_CLAUSE_SET_MAP_KIND (node2, GOMP_MAP_ATTACH_DETACH);
    5109          125 :                       OMP_CLAUSE_DECL (node2)
    5110          180 :                         = POINTER_TYPE_P (TREE_TYPE (se.expr))
    5111          125 :                           ? se.expr
    5112           55 :                           : gfc_build_addr_expr (NULL, se.expr);
    5113          125 :                       OMP_CLAUSE_SIZE (node2) = size_int (0);
    5114          125 :                       if (!openacc
    5115           93 :                           && n->expr->ts.type == BT_CHARACTER
    5116           54 :                           && n->expr->ts.deferred)
    5117              :                         {
    5118           54 :                           gcc_assert (se.string_length);
    5119           54 :                           tree tmp
    5120           54 :                             = gfc_get_char_type (n->expr->ts.kind);
    5121           54 :                           OMP_CLAUSE_SIZE (node)
    5122           54 :                             = fold_build2 (MULT_EXPR, size_type_node,
    5123              :                                            fold_convert (size_type_node,
    5124              :                                                se.string_length),
    5125              :                                            TYPE_SIZE_UNIT (tmp));
    5126           54 :                           gomp_map_kind kind;
    5127           54 :                           if (n->u.map.op == OMP_MAP_DELETE)
    5128              :                             kind = GOMP_MAP_DELETE;
    5129           54 :                           else if (op == EXEC_OMP_TARGET_EXIT_DATA)
    5130              :                             kind = GOMP_MAP_RELEASE;
    5131              :                           else
    5132           48 :                             kind = GOMP_MAP_TO;
    5133           54 :                           node3 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    5134           54 :                           OMP_CLAUSE_SET_MAP_KIND (node3, kind);
    5135           54 :                           OMP_CLAUSE_DECL (node3) = se.string_length;
    5136           54 :                           OMP_CLAUSE_SIZE (node3)
    5137          108 :                             = TYPE_SIZE_UNIT (gfc_charlen_type_node);
    5138              :                         }
    5139           93 :                       if (!openacc
    5140           93 :                           && n->expr->ts.type == BT_DERIVED
    5141            0 :                           && n->expr->ts.u.derived->attr.alloc_comp)
    5142              :                         {
    5143              :                           /* Save array descriptor for use in
    5144              :                              gfc_omp_deep_mapping{,_p,_cnt}; force evaluate
    5145              :                              to ensure that it is not gimplified + is a decl.  */
    5146            0 :                           tree tmp = OMP_CLAUSE_SIZE (node);
    5147            0 :                           if (tmp == NULL_TREE)
    5148            0 :                             tmp = (DECL_P (se.expr)
    5149            0 :                                    ? DECL_SIZE_UNIT (se.expr)
    5150            0 :                                    : TYPE_SIZE_UNIT (TREE_TYPE (se.expr)));
    5151            0 :                           tree var = gfc_create_var (TREE_TYPE (tmp), NULL);
    5152            0 :                           gfc_add_modify_loc (input_location, &iter_block,
    5153              :                                               var, tmp);
    5154            0 :                           OMP_CLAUSE_SIZE (node) = var;
    5155            0 :                           gfc_allocate_lang_decl (var);
    5156            0 :                           if (TREE_CODE (se.expr) == INDIRECT_REF)
    5157            0 :                             se.expr = TREE_OPERAND (se.expr, 0);
    5158            0 :                           if (DECL_LANG_SPECIFIC (se.expr)
    5159            0 :                               && GFC_DECL_SAVED_DESCRIPTOR (se.expr))
    5160            0 :                             GFC_DECL_SAVED_DESCRIPTOR (var)
    5161            0 :                               = GFC_DECL_SAVED_DESCRIPTOR (se.expr);
    5162              :                           else
    5163            0 :                             GFC_DECL_SAVED_DESCRIPTOR (var) = se.expr;
    5164              :                         }
    5165              :                     }
    5166              :                 }
    5167         2078 :               else if (n->expr
    5168         2078 :                        && n->expr->expr_type == EXPR_VARIABLE
    5169         2078 :                        && (n->expr->ref->type == REF_COMPONENT
    5170              :                            || n->expr->ref->type == REF_ARRAY))
    5171              :                 {
    5172         2078 :                   gfc_init_se (&se, NULL);
    5173         2078 :                   se.expr = gfc_maybe_dereference_var (n->sym, decl);
    5174         2078 :                   vec<tree> mid_descr = vNULL;
    5175         2078 :                   vec<gfc_ref *> midref = vNULL;
    5176              : 
    5177         7543 :                   for (gfc_ref *ref = n->expr->ref; ref; ref = ref->next)
    5178              :                     {
    5179         5465 :                       if (ref->type == REF_COMPONENT)
    5180              :                         {
    5181         2739 :                           if (ref->u.c.sym->attr.extension)
    5182           91 :                             conv_parent_component_references (&se, ref);
    5183              : 
    5184         2739 :                           gfc_conv_component_ref (&se, ref);
    5185         2739 :                           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)))
    5186              :                             {
    5187         1999 :                               mid_descr.safe_push (se.expr);
    5188         1999 :                               midref.safe_push (ref);
    5189              :                             }
    5190              :                         }
    5191         2726 :                       else if (ref->type == REF_ARRAY)
    5192              :                         {
    5193         2726 :                           if (ref->u.ar.type == AR_ELEMENT && ref->next)
    5194          765 :                             gfc_conv_array_ref (&se, &ref->u.ar, n->expr,
    5195          765 :                                                 &n->expr->where);
    5196              :                           else
    5197         1961 :                             gcc_assert (!ref->next);
    5198              :                         }
    5199              :                       else
    5200            0 :                         sorry_at (gfc_get_location (&n->where),
    5201              :                                   "unhandled expression type");
    5202              :                     }
    5203              : 
    5204         2078 :                   tree inner = se.expr;
    5205              : 
    5206              :                   /* Last component is a derived type or class pointer.  */
    5207         2078 :                   if (lastref->type == REF_COMPONENT
    5208          117 :                       && (lastref->u.c.component->ts.type == BT_DERIVED
    5209           24 :                           || lastref->u.c.component->ts.type == BT_CLASS))
    5210              :                     {
    5211          117 :                       if (pointer || allocatable)
    5212              :                         {
    5213              :                           /* If it's a bare attach/detach clause, we just want
    5214              :                              to perform a single attach/detach operation, of the
    5215              :                              pointer itself, not of the pointed-to object.  */
    5216           67 :                           if (openacc
    5217           49 :                               && (n->u.map.op == OMP_MAP_ATTACH
    5218           43 :                                   || n->u.map.op == OMP_MAP_DETACH))
    5219              :                             {
    5220           12 :                               OMP_CLAUSE_DECL (node)
    5221           12 :                                 = build_fold_addr_expr (inner);
    5222           12 :                               OMP_CLAUSE_SIZE (node) = size_zero_node;
    5223           18 :                               goto finalize_map_clause;
    5224              :                             }
    5225              : 
    5226           18 :                           gfc_omp_namelist *n2
    5227              :                             = openacc ? NULL : clauses->lists[OMP_LIST_MAP];
    5228              : 
    5229           55 :                           bool sym_based;
    5230           55 :                           n2 = get_symbol_rooted_namelist (sym_rooted_nl, n,
    5231              :                                                            n2, &sym_based);
    5232              : 
    5233              :                           /* If the last reference is a pointer to a derived
    5234              :                              type ("foo%dt_ptr"), check if any subcomponents
    5235              :                              of the same derived type member are being mapped
    5236              :                              elsewhere in the clause list ("foo%dt_ptr%x",
    5237              :                              etc.).  If we have such subcomponent mappings,
    5238              :                              we only create an ALLOC node for the pointer
    5239              :                              itself, and inhibit mapping the whole derived
    5240              :                              type.  */
    5241              : 
    5242          158 :                           for (; n2 != NULL; n2 = n2->next)
    5243              :                             {
    5244           54 :                               if ((!sym_based && n == n2)
    5245           54 :                                   || (sym_based && n == n2->u2.duplicate_of)
    5246           42 :                                   || !n2->expr)
    5247           12 :                                 continue;
    5248              : 
    5249           42 :                               if (!gfc_omp_expr_prefix_same (n->expr,
    5250              :                                                              n2->expr))
    5251           36 :                                 continue;
    5252              : 
    5253            6 :                               gfc_ref *ref1 = n->expr->ref;
    5254            6 :                               gfc_ref *ref2 = n2->expr->ref;
    5255              : 
    5256            6 :                               while (ref1->next && ref2->next)
    5257              :                                 {
    5258              :                                   ref1 = ref1->next;
    5259              :                                   ref2 = ref2->next;
    5260              :                                 }
    5261              : 
    5262            6 :                               if (ref2->next)
    5263              :                                 {
    5264            6 :                                   inner = build_fold_addr_expr (inner);
    5265            6 :                                   OMP_CLAUSE_SET_MAP_KIND (node,
    5266              :                                                            GOMP_MAP_ALLOC);
    5267            6 :                                   OMP_CLAUSE_DECL (node) = inner;
    5268            6 :                                   OMP_CLAUSE_SIZE (node)
    5269            6 :                                     = TYPE_SIZE_UNIT (TREE_TYPE (inner));
    5270            6 :                                   goto finalize_map_clause;
    5271              :                                 }
    5272              :                             }
    5273              : 
    5274           49 :                           tree data, size;
    5275              : 
    5276           49 :                           if (lastref->u.c.component->ts.type == BT_CLASS)
    5277              :                             {
    5278           24 :                               data = gfc_class_data_get (inner);
    5279           24 :                               gcc_assert (POINTER_TYPE_P (TREE_TYPE (data)));
    5280           24 :                               data = build_fold_indirect_ref (data);
    5281           24 :                               size = gfc_class_vtab_size_get (inner);
    5282              :                             }
    5283              :                           else  /* BT_DERIVED.  */
    5284              :                             {
    5285           25 :                               data = inner;
    5286           25 :                               size = TYPE_SIZE_UNIT (TREE_TYPE (inner));
    5287              :                             }
    5288              : 
    5289           49 :                           OMP_CLAUSE_DECL (node) = data;
    5290           49 :                           OMP_CLAUSE_SIZE (node) = size;
    5291           49 :                           node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
    5292           49 :                           OMP_CLAUSE_SET_MAP_KIND (node2,
    5293              :                                                    GOMP_MAP_ATTACH_DETACH);
    5294           49 :                           OMP_CLAUSE_DECL (node2) = build_fold_addr_expr (data);
    5295           49 :                           OMP_CLAUSE_SIZE (node2) = size_int (0);
    5296              :                         }
    5297              :                       else
    5298              :                         {
    5299           50 :                           OMP_CLAUSE_DECL (node) = inner;
    5300           50 :                           OMP_CLAUSE_SIZE (node)
    5301          100 :                             = TYPE_SIZE_UNIT (TREE_TYPE (inner));
    5302              :                         }
    5303           99 :                       if (!openacc
    5304           15 :                           && n->expr->ts.type == BT_DERIVED
    5305           15 :                           && n->expr->ts.u.derived->attr.alloc_comp)
    5306              :                         {
    5307              :                           /* Save array descriptor for use in
    5308              :                              gfc_omp_deep_mapping{,_p,_cnt}; force evaluate
    5309              :                              to ensure that it is not gimplified + is a decl.  */
    5310            8 :                           tree tmp = OMP_CLAUSE_SIZE (node);
    5311            8 :                           tree var = gfc_create_var (TREE_TYPE (tmp), NULL);
    5312            8 :                           gfc_add_modify_loc (input_location, &iter_block,
    5313              :                                               var, tmp);
    5314            8 :                           OMP_CLAUSE_SIZE (node) = var;
    5315            8 :                           gfc_allocate_lang_decl (var);
    5316            8 :                           if (TREE_CODE (inner) == INDIRECT_REF)
    5317            6 :                             inner = TREE_OPERAND (inner, 0);
    5318            8 :                           GFC_DECL_SAVED_DESCRIPTOR (var) = inner;
    5319              :                         }
    5320              :                     }
    5321         1961 :                   else if (lastref->type == REF_ARRAY
    5322         1961 :                            && lastref->u.ar.type == AR_FULL)
    5323              :                     {
    5324              :                       /* Bare attach and detach clauses don't want any
    5325              :                          additional nodes.  */
    5326         1234 :                       if ((n->u.map.op == OMP_MAP_ATTACH
    5327         1203 :                            || n->u.map.op == OMP_MAP_DETACH)
    5328         1248 :                           && (POINTER_TYPE_P (TREE_TYPE (inner))
    5329           45 :                               || GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (inner))))
    5330              :                         {
    5331           45 :                           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (inner)))
    5332              :                             {
    5333           45 :                               tree ptr = gfc_conv_descriptor_data_get (inner);
    5334           45 :                               OMP_CLAUSE_DECL (node) = ptr;
    5335              :                             }
    5336              :                           else
    5337            0 :                             OMP_CLAUSE_DECL (node) = inner;
    5338           45 :                           OMP_CLAUSE_SIZE (node) = size_zero_node;
    5339           45 :                           goto finalize_map_clause;
    5340              :                         }
    5341              : 
    5342         1189 :                       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (inner)))
    5343              :                         {
    5344         1019 :                           bool drop_mapping = gfc_map_array_descriptor (
    5345              :                             node, node2, node3, node4, inner, openacc, map_loc,
    5346              :                             &iter_block, op, n, sym_rooted_nl, se, clauses,
    5347              :                             false);
    5348         1019 :                           if (drop_mapping)
    5349          286 :                             continue;
    5350              :                         }
    5351              :                       else
    5352          170 :                         OMP_CLAUSE_DECL (node) = inner;
    5353              :                     }
    5354          727 :                   else if (lastref->type == REF_ARRAY)
    5355              :                     {
    5356              :                       /* An array element or section.  */
    5357          727 :                       bool element = lastref->u.ar.type == AR_ELEMENT;
    5358          727 :                       gomp_map_kind kind = GOMP_MAP_ATTACH_DETACH;
    5359          727 :                       gfc_trans_omp_array_section (&iter_block, op, n, inner,
    5360              :                                                    element, !openacc, kind,
    5361              :                                                    node, node2, node3, node4,
    5362              :                                                    iterator);
    5363              :                     }
    5364              :                   else
    5365            0 :                     gcc_unreachable ();
    5366              : 
    5367              :                   /* Map intermediate array descriptors.  */
    5368         1645 :                   if (!openacc && !mid_descr.is_empty ())
    5369         2321 :                     for (size_t i = 0; i < mid_descr.length (); i++)
    5370         1333 :                       if (mid_descr[i] != inner
    5371         1333 :                           && !descriptors.contains (midref[i]->u.c.sym))
    5372              :                         {
    5373          172 :                           descriptors.safe_push (midref[i]->u.c.sym);
    5374          172 :                           tree node1 = copy_node (node);
    5375          172 :                           tree node2 = NULL_TREE;
    5376          172 :                           tree node3 = NULL_TREE;
    5377          172 :                           tree node4 = NULL_TREE;
    5378          344 :                           gfc_map_array_descriptor (node1, node2, node3, node4,
    5379          172 :                                                     mid_descr[i], openacc,
    5380              :                                                     map_loc, &iter_block,
    5381              :                                                     op, n,
    5382              :                                                     sym_rooted_nl, se, clauses,
    5383              :                                                     true);
    5384              : 
    5385          172 :                           if (node1 != NULL_TREE)
    5386           90 :                             omp_clauses
    5387           90 :                               = gfc_trans_add_clause (node1, omp_clauses);
    5388          172 :                           if (node2 != NULL_TREE)
    5389          172 :                             omp_clauses
    5390          172 :                               = gfc_trans_add_clause (node2, omp_clauses);
    5391          172 :                           if (node3 != NULL_TREE)
    5392          172 :                             omp_clauses
    5393          172 :                               = gfc_trans_add_clause (node3, omp_clauses);
    5394          172 :                           if (node4 != NULL_TREE)
    5395            0 :                             omp_clauses
    5396            0 :                               = gfc_trans_add_clause (node4, omp_clauses);
    5397              :                         }
    5398         1729 :                 }
    5399              :               else
    5400            0 :                 sorry_at (gfc_get_location (&n->where), "unhandled expression");
    5401              : 
    5402        15506 :               finalize_map_clause:
    5403              : 
    5404        15506 :               if (!iterator)
    5405        15467 :                 gfc_add_block_to_block (block, &iter_block);
    5406              : 
    5407        15506 :               omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    5408        15506 :               if (node2)
    5409         5169 :                 omp_clauses = gfc_trans_add_clause (node2, omp_clauses);
    5410        15506 :               if (node3)
    5411         6438 :                 omp_clauses = gfc_trans_add_clause (node3, omp_clauses);
    5412        15506 :               if (node4)
    5413         3565 :                 omp_clauses = gfc_trans_add_clause (node4, omp_clauses);
    5414        15506 :               if (node5)
    5415            4 :                 omp_clauses = gfc_trans_add_clause (node5, omp_clauses);
    5416              :             }
    5417         9227 :           if (iterator)
    5418           12 :             finish_iterator_group (list, &iter_block, tree_block,
    5419              :                                    iterator, omp_clauses, prev_clauses);
    5420              :           break;
    5421              :         case OMP_LIST_TO:
    5422              :         case OMP_LIST_FROM:
    5423              :         case OMP_LIST_CACHE:
    5424              :           iterator = NULL_TREE;
    5425              :           prev = NULL;
    5426              :           prev_clauses = omp_clauses;
    5427         3924 :           for (; n != NULL; n = n->next)
    5428              :             {
    5429         2069 :               if (!n->sym->attr.referenced
    5430            0 :                   && n->sym->attr.flavor != FL_PARAMETER)
    5431            0 :                 continue;
    5432              : 
    5433         2069 :               if (iterator && prev->u2.ns != n->u2.ns)
    5434              :                 {
    5435            0 :                   finish_iterator_group (list, &iter_block, tree_block,
    5436              :                                          iterator, omp_clauses, prev_clauses);
    5437            0 :                   prev_clauses = omp_clauses;
    5438            0 :                   iterator = NULL_TREE;
    5439              :                 }
    5440         2069 :               if (n->u2.ns && (!prev || prev->u2.ns != n->u2.ns))
    5441              :                 {
    5442              :                   /* Start a new iterator group.  */
    5443           11 :                   iterator = start_iterator_group (n->u2.ns, block,
    5444              :                                                    &iter_block, tree_block);
    5445           11 :                   prev_clauses = omp_clauses;
    5446              :                 }
    5447         2069 :               if (!iterator)
    5448         2055 :                 gfc_init_block (&iter_block);
    5449         2069 :               prev = n;
    5450              : 
    5451         2069 :               switch (list)
    5452              :                 {
    5453              :                 case OMP_LIST_TO:
    5454              :                   clause_code = OMP_CLAUSE_TO;
    5455              :                   break;
    5456         1125 :                 case OMP_LIST_FROM:
    5457         1125 :                   clause_code = OMP_CLAUSE_FROM;
    5458         1125 :                   break;
    5459           84 :                 case OMP_LIST_CACHE:
    5460           84 :                   clause_code = OMP_CLAUSE__CACHE_;
    5461           84 :                   break;
    5462              :                 default:
    5463              :                   gcc_unreachable ();
    5464              :                 }
    5465         2069 :               tree node = build_omp_clause (gfc_get_location (&n->where),
    5466              :                                             clause_code);
    5467         2069 :               if (n->expr == NULL
    5468          141 :                   || (n->expr->ref->type == REF_ARRAY
    5469          129 :                       && n->expr->ref->u.ar.type == AR_FULL
    5470            0 :                       && n->expr->ref->next == NULL))
    5471              :                 {
    5472         1928 :                   tree decl = gfc_trans_omp_variable (n->sym, false);
    5473         1928 :                   if (gfc_omp_privatize_by_reference (decl))
    5474              :                     {
    5475         1047 :                       if (gfc_omp_is_allocatable_or_ptr (decl))
    5476          240 :                         decl = build_fold_indirect_ref (decl);
    5477         1047 :                       decl = build_fold_indirect_ref (decl);
    5478              :                     }
    5479          881 :                   else if (DECL_P (decl))
    5480          881 :                     TREE_ADDRESSABLE (decl) = 1;
    5481         1928 :                   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
    5482              :                     {
    5483          597 :                       tree type = TREE_TYPE (decl);
    5484          597 :                       tree ptr = gfc_conv_descriptor_data_get (decl);
    5485          597 :                       gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
    5486          597 :                       ptr = build_fold_indirect_ref (ptr);
    5487          597 :                       OMP_CLAUSE_DECL (node) = ptr;
    5488          597 :                       OMP_CLAUSE_SIZE (node)
    5489          597 :                         = gfc_full_array_size (&iter_block, decl,
    5490          597 :                                                GFC_TYPE_ARRAY_RANK (type));
    5491          597 :                       tree elemsz
    5492          597 :                         = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    5493          597 :                       elemsz = fold_convert (gfc_array_index_type, elemsz);
    5494         1194 :                       OMP_CLAUSE_SIZE (node)
    5495         1194 :                         = fold_build2 (MULT_EXPR, gfc_array_index_type,
    5496              :                                        OMP_CLAUSE_SIZE (node), elemsz);
    5497              :                     }
    5498              :                   else
    5499              :                     {
    5500         1331 :                       OMP_CLAUSE_DECL (node) = decl;
    5501         1331 :                       if (gfc_omp_is_allocatable_or_ptr (decl))
    5502          120 :                         OMP_CLAUSE_SIZE (node)
    5503          240 :                                 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
    5504              :                     }
    5505              :                 }
    5506              :               else
    5507              :                 {
    5508          141 :                   tree ptr;
    5509          141 :                   gfc_init_se (&se, NULL);
    5510          141 :                   if (n->expr->rank == 0)
    5511              :                     {
    5512            9 :                       gfc_conv_expr_reference (&se, n->expr);
    5513            9 :                       ptr = se.expr;
    5514            9 :                       gfc_add_block_to_block (&iter_block, &se.pre);
    5515            9 :                       OMP_CLAUSE_SIZE (node)
    5516           18 :                         = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ptr)));
    5517              :                     }
    5518              :                   else
    5519              :                     {
    5520          132 :                       gfc_conv_expr_descriptor (&se, n->expr);
    5521          132 :                       ptr = gfc_conv_array_data (se.expr);
    5522          132 :                       tree type = TREE_TYPE (se.expr);
    5523          132 :                       gfc_add_block_to_block (&iter_block, &se.pre);
    5524          132 :                       OMP_CLAUSE_SIZE (node)
    5525          132 :                         = gfc_full_array_size (&iter_block, se.expr,
    5526          132 :                                                GFC_TYPE_ARRAY_RANK (type));
    5527          132 :                       tree elemsz
    5528          132 :                         = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    5529          132 :                       elemsz = fold_convert (gfc_array_index_type, elemsz);
    5530          264 :                       OMP_CLAUSE_SIZE (node)
    5531          264 :                         = fold_build2 (MULT_EXPR, gfc_array_index_type,
    5532              :                                        OMP_CLAUSE_SIZE (node), elemsz);
    5533              :                     }
    5534          141 :                   gfc_add_block_to_block (&iter_block, &se.post);
    5535          141 :                   gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
    5536          141 :                   OMP_CLAUSE_DECL (node) = build_fold_indirect_ref (ptr);
    5537              :                 }
    5538         2069 :               if (n->u.present_modifier)
    5539            5 :                 OMP_CLAUSE_MOTION_PRESENT (node) = 1;
    5540         2069 :               if (list == OMP_LIST_CACHE && n->u.map.readonly)
    5541           16 :                 OMP_CLAUSE__CACHE__READONLY (node) = 1;
    5542         2069 :               if (!iterator)
    5543         2055 :                 gfc_add_block_to_block (block, &iter_block);
    5544         2069 :               omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    5545              :             }
    5546         1855 :           if (iterator)
    5547           11 :             finish_iterator_group (list, &iter_block, tree_block,
    5548              :                                    iterator, omp_clauses, prev_clauses);
    5549              :           break;
    5550              :         case OMP_LIST_USES_ALLOCATORS:
    5551           90 :           for (; n != NULL; n = n->next)
    5552              :             {
    5553           61 :               if (!n->sym->attr.referenced)
    5554           43 :                 continue;
    5555           18 :               tree node = build_omp_clause (input_location,
    5556              :                                             OMP_CLAUSE_USES_ALLOCATORS);
    5557           18 :               tree t;
    5558           18 :               if (n->sym->attr.flavor == FL_VARIABLE)
    5559           16 :                 t = gfc_get_symbol_decl (n->sym);
    5560              :               else
    5561              :                 {
    5562            2 :                   t = gfc_conv_mpz_to_tree (n->sym->value->value.integer,
    5563              :                                             n->sym->ts.kind);
    5564            2 :                   t = fold_convert (ptr_type_node, t);
    5565              :                 }
    5566           18 :               OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR(node) = t;
    5567           18 :               if (n->u.memspace_sym)
    5568              :                 {
    5569            5 :                   gcc_checking_assert (n->u.memspace_sym->attr.flavor
    5570              :                                        == FL_PARAMETER
    5571              :                                        && !n->u.memspace_sym->attr.dimension);
    5572            5 :                   n->u.memspace_sym->attr.referenced = true;
    5573            5 :                   gfc_init_se (&se, NULL);
    5574            5 :                   gfc_conv_expr (&se, n->u.memspace_sym->value);
    5575            5 :                   OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (node) = se.expr;
    5576              :                 }
    5577           18 :               if (n->u2.traits_sym)
    5578              :                 {
    5579            9 :                   n->u2.traits_sym->attr.referenced = true;
    5580            9 :                   OMP_CLAUSE_USES_ALLOCATORS_TRAITS (node)
    5581           18 :                     = gfc_get_symbol_decl (n->u2.traits_sym);
    5582              :                 }
    5583           18 :               omp_clauses = gfc_trans_add_clause (node, omp_clauses);
    5584              :             }
    5585              :           break;
    5586              :         default:
    5587              :           break;
    5588              :         }
    5589              :     }
    5590              : 
    5591              :   /* Free hashmap if we built it.  */
    5592        32910 :   if (sym_rooted_nl)
    5593              :     {
    5594          388 :       typedef hash_map<gfc_symbol *, gfc_omp_namelist *>::iterator hti;
    5595          833 :       for (hti it = sym_rooted_nl->begin (); it != sym_rooted_nl->end (); ++it)
    5596              :         {
    5597          445 :           gfc_omp_namelist *&nl = (*it).second;
    5598         1771 :           while (nl)
    5599              :             {
    5600         1326 :               gfc_omp_namelist *next = nl->next;
    5601         1326 :               free (nl);
    5602         1326 :               nl = next;
    5603              :             }
    5604              :         }
    5605          388 :       delete sym_rooted_nl;
    5606              :     }
    5607              : 
    5608        32910 :   if (clauses->if_expr)
    5609              :     {
    5610         1120 :       tree if_var;
    5611              : 
    5612         1120 :       gfc_init_se (&se, NULL);
    5613         1120 :       gfc_conv_expr (&se, clauses->if_expr);
    5614         1120 :       gfc_add_block_to_block (block, &se.pre);
    5615         1120 :       if_var = gfc_evaluate_now (se.expr, block);
    5616         1120 :       gfc_add_block_to_block (block, &se.post);
    5617              : 
    5618         1120 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_IF);
    5619         1120 :       OMP_CLAUSE_IF_MODIFIER (c) = ERROR_MARK;
    5620         1120 :       OMP_CLAUSE_IF_EXPR (c) = if_var;
    5621         1120 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5622              :     }
    5623              : 
    5624       362010 :   for (ifc = 0; ifc < OMP_IF_LAST; ifc++)
    5625       329100 :     if (clauses->if_exprs[ifc])
    5626              :       {
    5627          123 :         tree if_var;
    5628              : 
    5629          123 :         gfc_init_se (&se, NULL);
    5630          123 :         gfc_conv_expr (&se, clauses->if_exprs[ifc]);
    5631          123 :         gfc_add_block_to_block (block, &se.pre);
    5632          123 :         if_var = gfc_evaluate_now (se.expr, block);
    5633          123 :         gfc_add_block_to_block (block, &se.post);
    5634              : 
    5635          123 :         c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_IF);
    5636          123 :         switch (ifc)
    5637              :           {
    5638            0 :           case OMP_IF_CANCEL:
    5639            0 :             OMP_CLAUSE_IF_MODIFIER (c) = VOID_CST;
    5640            0 :             break;
    5641           40 :           case OMP_IF_PARALLEL:
    5642           40 :             OMP_CLAUSE_IF_MODIFIER (c) = OMP_PARALLEL;
    5643           40 :             break;
    5644           39 :           case OMP_IF_SIMD:
    5645           39 :             OMP_CLAUSE_IF_MODIFIER (c) = OMP_SIMD;
    5646           39 :             break;
    5647            1 :           case OMP_IF_TASK:
    5648            1 :             OMP_CLAUSE_IF_MODIFIER (c) = OMP_TASK;
    5649            1 :             break;
    5650           23 :           case OMP_IF_TASKLOOP:
    5651           23 :             OMP_CLAUSE_IF_MODIFIER (c) = OMP_TASKLOOP;
    5652           23 :             break;
    5653           16 :           case OMP_IF_TARGET:
    5654           16 :             OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET;
    5655           16 :             break;
    5656            1 :           case OMP_IF_TARGET_DATA:
    5657            1 :             OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET_DATA;
    5658            1 :             break;
    5659            1 :           case OMP_IF_TARGET_UPDATE:
    5660            1 :             OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET_UPDATE;
    5661            1 :             break;
    5662            1 :           case OMP_IF_TARGET_ENTER_DATA:
    5663            1 :             OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET_ENTER_DATA;
    5664            1 :             break;
    5665            1 :           case OMP_IF_TARGET_EXIT_DATA:
    5666            1 :             OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET_EXIT_DATA;
    5667            1 :             break;
    5668              :           default:
    5669              :             gcc_unreachable ();
    5670              :           }
    5671          123 :         OMP_CLAUSE_IF_EXPR (c) = if_var;
    5672          123 :         omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5673              :       }
    5674              : 
    5675        32910 :   if (clauses->self_expr)
    5676              :     {
    5677          159 :       tree self_var;
    5678              : 
    5679          159 :       gfc_init_se (&se, NULL);
    5680          159 :       gfc_conv_expr (&se, clauses->self_expr);
    5681          159 :       gfc_add_block_to_block (block, &se.pre);
    5682          159 :       self_var = gfc_evaluate_now (se.expr, block);
    5683          159 :       gfc_add_block_to_block (block, &se.post);
    5684              : 
    5685          159 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SELF);
    5686          159 :       OMP_CLAUSE_SELF_EXPR (c) = self_var;
    5687          159 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5688              :     }
    5689              : 
    5690        32910 :   if (clauses->final_expr)
    5691              :     {
    5692           64 :       tree final_var;
    5693              : 
    5694           64 :       gfc_init_se (&se, NULL);
    5695           64 :       gfc_conv_expr (&se, clauses->final_expr);
    5696           64 :       gfc_add_block_to_block (block, &se.pre);
    5697           64 :       final_var = gfc_evaluate_now (se.expr, block);
    5698           64 :       gfc_add_block_to_block (block, &se.post);
    5699              : 
    5700           64 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FINAL);
    5701           64 :       OMP_CLAUSE_FINAL_EXPR (c) = final_var;
    5702           64 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5703              :     }
    5704              : 
    5705        32910 :   if (clauses->message || clauses->severity != OMP_SEVERITY_UNSET)
    5706              :     {
    5707           60 :       tree message = NULL_TREE;
    5708           60 :       tree len = NULL_TREE;
    5709              : 
    5710           60 :       if (clauses->message)
    5711              :         {
    5712           57 :           gfc_init_se (&se, NULL);
    5713           57 :           gfc_conv_expr (&se, clauses->message);
    5714           57 :           gfc_add_block_to_block (block, &se.pre);
    5715           57 :           message = se.expr;
    5716           57 :           len = se.string_length;
    5717           57 :           if (!DECL_P (se.expr))
    5718           57 :             message = gfc_evaluate_now (message, block);
    5719           57 :           gfc_add_block_to_block (block, &se.post);
    5720              : 
    5721           57 :           if (!POINTER_TYPE_P (TREE_TYPE (message)))
    5722              :             /* To ensure an ARRAY_TYPE is not passed as such.  */
    5723           57 :             message = gfc_build_addr_expr (NULL, message);
    5724              :         }
    5725              : 
    5726           60 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_MESSAGE);
    5727           60 :       OMP_CLAUSE_MESSAGE_EXPR (c) = message;
    5728           60 :       OMP_CLAUSE_MESSAGE_LEN (c) = len;
    5729           60 :       if (clauses->severity == OMP_SEVERITY_WARNING)
    5730           16 :         OMP_CLAUSE_MESSAGE_SEVERITY_WARN (c) = 1;
    5731           60 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5732              :     }
    5733              : 
    5734        32910 :   if (clauses->novariants)
    5735              :     {
    5736            8 :       tree novariants_var;
    5737              : 
    5738            8 :       gfc_init_se (&se, NULL);
    5739            8 :       gfc_conv_expr (&se, clauses->novariants);
    5740            8 :       gfc_add_block_to_block (block, &se.pre);
    5741            8 :       novariants_var = gfc_evaluate_now (se.expr, block);
    5742            8 :       gfc_add_block_to_block (block, &se.post);
    5743              : 
    5744            8 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOVARIANTS);
    5745            8 :       OMP_CLAUSE_NOVARIANTS_EXPR (c) = novariants_var;
    5746            8 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5747              :     }
    5748              : 
    5749        32910 :   if (clauses->nocontext)
    5750              :     {
    5751            9 :       tree nocontext_var;
    5752              : 
    5753            9 :       gfc_init_se (&se, NULL);
    5754            9 :       gfc_conv_expr (&se, clauses->nocontext);
    5755            9 :       gfc_add_block_to_block (block, &se.pre);
    5756            9 :       nocontext_var = gfc_evaluate_now (se.expr, block);
    5757            9 :       gfc_add_block_to_block (block, &se.post);
    5758              : 
    5759            9 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOCONTEXT);
    5760            9 :       OMP_CLAUSE_NOCONTEXT_EXPR (c) = nocontext_var;
    5761            9 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5762              :     }
    5763              : 
    5764        32910 :   if (clauses->num_threads_list)
    5765              :     {
    5766              :       tree num_threads = NULL_TREE;
    5767         1987 :       for (gfc_expr_list *el = clauses->num_threads_list; el; el = el->next)
    5768         1006 :         num_threads = tree_cons (NULL_TREE,
    5769         1006 :                                  gfc_convert_expr_to_tree (block, el->expr),
    5770              :                                  num_threads);
    5771          981 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_THREADS);
    5772          981 :       OMP_CLAUSE_NUM_THREADS_EXPR (c) = nreverse (num_threads);
    5773          981 :       OMP_CLAUSE_NUM_THREADS_STRICT (c) = clauses->num_threads_strict;
    5774          981 :       OMP_CLAUSE_NUM_THREADS_DIMS (c) = clauses->num_threads_dims;
    5775          981 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5776              :     }
    5777              : 
    5778        32910 :   if (clauses->device_type != OMP_DEVICE_TYPE_UNSET)
    5779              :     {
    5780            3 :       enum omp_clause_device_type_kind type;
    5781            3 :       switch (clauses->device_type)
    5782              :         {
    5783              :         case OMP_DEVICE_TYPE_HOST:
    5784              :           type = OMP_CLAUSE_DEVICE_TYPE_HOST;
    5785              :           break;
    5786              :         case OMP_DEVICE_TYPE_NOHOST:
    5787              :           type = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
    5788              :           break;
    5789              :         case OMP_DEVICE_TYPE_ANY:
    5790              :           type = OMP_CLAUSE_DEVICE_TYPE_ANY;
    5791              :           break;
    5792            0 :         case OMP_DEVICE_TYPE_UNSET:
    5793            0 :         default:
    5794            0 :           gcc_unreachable ();
    5795              :         }
    5796            3 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DEVICE_TYPE);
    5797            3 :       OMP_CLAUSE_DEVICE_TYPE_KIND (c) = type;
    5798            3 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5799              :     }
    5800              : 
    5801        32910 :   if (clauses->dyn_groupprivate)
    5802              :     {
    5803           38 :       gfc_init_se (&se, NULL);
    5804           38 :       gfc_conv_expr (&se, clauses->dyn_groupprivate);
    5805           38 :       gfc_add_block_to_block (block, &se.pre);
    5806           38 :       tree expr = (CONSTANT_CLASS_P (se.expr) || DECL_P (se.expr)
    5807           38 :                    ? se.expr : gfc_evaluate_now (se.expr, block));
    5808           38 :       gfc_add_block_to_block (block, &se.post);
    5809              : 
    5810           38 :       enum omp_clause_fallback_kind kind = OMP_CLAUSE_FALLBACK_UNSPECIFIED;
    5811           38 :       switch (clauses->fallback)
    5812              :         {
    5813              :         case OMP_FALLBACK_ABORT:
    5814              :           kind = OMP_CLAUSE_FALLBACK_ABORT;
    5815              :           break;
    5816              :         case OMP_FALLBACK_DEFAULT_MEM:
    5817              :           kind = OMP_CLAUSE_FALLBACK_DEFAULT_MEM;
    5818              :           break;
    5819              :         case OMP_FALLBACK_NULL:
    5820              :           kind = OMP_CLAUSE_FALLBACK_NULL;
    5821              :           break;
    5822              :         case OMP_FALLBACK_NONE:
    5823              :           break;
    5824              :         }
    5825           38 :       location_t loc = gfc_get_location (&clauses->dyn_groupprivate->where);
    5826           38 :       c = build_omp_clause (loc, OMP_CLAUSE_DYN_GROUPPRIVATE);
    5827           38 :       OMP_CLAUSE_DYN_GROUPPRIVATE_KIND (c) = kind;
    5828           38 :       OMP_CLAUSE_DYN_GROUPPRIVATE_EXPR (c) = expr;
    5829           38 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5830              :     }
    5831              : 
    5832        32910 :   chunk_size = NULL_TREE;
    5833        32910 :   if (clauses->chunk_size)
    5834              :     {
    5835          493 :       gfc_init_se (&se, NULL);
    5836          493 :       gfc_conv_expr (&se, clauses->chunk_size);
    5837          493 :       gfc_add_block_to_block (block, &se.pre);
    5838          493 :       chunk_size = gfc_evaluate_now (se.expr, block);
    5839          493 :       gfc_add_block_to_block (block, &se.post);
    5840              :     }
    5841              : 
    5842        32910 :   if (clauses->sched_kind != OMP_SCHED_NONE)
    5843              :     {
    5844          782 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SCHEDULE);
    5845          782 :       OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = chunk_size;
    5846          782 :       switch (clauses->sched_kind)
    5847              :         {
    5848          407 :         case OMP_SCHED_STATIC:
    5849          407 :           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
    5850          407 :           break;
    5851          159 :         case OMP_SCHED_DYNAMIC:
    5852          159 :           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
    5853          159 :           break;
    5854          125 :         case OMP_SCHED_GUIDED:
    5855          125 :           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
    5856          125 :           break;
    5857           84 :         case OMP_SCHED_RUNTIME:
    5858           84 :           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
    5859           84 :           break;
    5860            7 :         case OMP_SCHED_AUTO:
    5861            7 :           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
    5862            7 :           break;
    5863            0 :         default:
    5864            0 :           gcc_unreachable ();
    5865              :         }
    5866          782 :       if (clauses->sched_monotonic)
    5867           54 :         OMP_CLAUSE_SCHEDULE_KIND (c)
    5868           27 :           = (omp_clause_schedule_kind) (OMP_CLAUSE_SCHEDULE_KIND (c)
    5869              :                                         | OMP_CLAUSE_SCHEDULE_MONOTONIC);
    5870          755 :       else if (clauses->sched_nonmonotonic)
    5871           46 :         OMP_CLAUSE_SCHEDULE_KIND (c)
    5872           23 :           = (omp_clause_schedule_kind) (OMP_CLAUSE_SCHEDULE_KIND (c)
    5873              :                                         | OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
    5874          782 :       if (clauses->sched_simd)
    5875           17 :         OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
    5876          782 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5877              :     }
    5878              : 
    5879        32910 :   if (clauses->default_sharing != OMP_DEFAULT_UNKNOWN)
    5880              :     {
    5881         1093 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DEFAULT);
    5882         1093 :       switch (clauses->default_sharing)
    5883              :         {
    5884          683 :         case OMP_DEFAULT_NONE:
    5885          683 :           OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_NONE;
    5886          683 :           break;
    5887          183 :         case OMP_DEFAULT_SHARED:
    5888          183 :           OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_SHARED;
    5889          183 :           break;
    5890           24 :         case OMP_DEFAULT_PRIVATE:
    5891           24 :           OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_PRIVATE;
    5892           24 :           break;
    5893            8 :         case OMP_DEFAULT_FIRSTPRIVATE:
    5894            8 :           OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
    5895            8 :           break;
    5896          195 :         case OMP_DEFAULT_PRESENT:
    5897          195 :           OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_PRESENT;
    5898          195 :           break;
    5899            0 :         default:
    5900            0 :           gcc_unreachable ();
    5901              :         }
    5902         1093 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5903              :     }
    5904              : 
    5905        32910 :   if (clauses->nowait)
    5906              :     {
    5907         2166 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOWAIT);
    5908         2166 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5909              :     }
    5910              : 
    5911        32910 :   if (clauses->full)
    5912              :     {
    5913           48 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FULL);
    5914           48 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5915              :     }
    5916              : 
    5917        32910 :   if (clauses->partial)
    5918              :     {
    5919          260 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_PARTIAL);
    5920          260 :       OMP_CLAUSE_PARTIAL_EXPR (c)
    5921          520 :         = (clauses->partial > 0
    5922          260 :            ? build_int_cst (integer_type_node, clauses->partial)
    5923              :            : NULL_TREE);
    5924          260 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5925              :     }
    5926              : 
    5927        32910 :   if (clauses->sizes_list)
    5928              :     {
    5929              :       tree list = NULL_TREE;
    5930          348 :       for (gfc_expr_list *el = clauses->sizes_list; el; el = el->next)
    5931          226 :         list = tree_cons (NULL_TREE, gfc_convert_expr_to_tree (block, el->expr),
    5932              :                           list);
    5933              : 
    5934          122 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SIZES);
    5935          122 :       OMP_CLAUSE_SIZES_LIST (c) = nreverse (list);
    5936          122 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5937              :     }
    5938              : 
    5939        32910 :   if (clauses->ordered)
    5940              :     {
    5941          315 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_ORDERED);
    5942          315 :       OMP_CLAUSE_ORDERED_EXPR (c)
    5943          315 :         = clauses->orderedc ? build_int_cst (integer_type_node,
    5944          134 :                                              clauses->orderedc) : NULL_TREE;
    5945          315 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5946              :     }
    5947              : 
    5948        32910 :   if (clauses->order_concurrent)
    5949              :     {
    5950          303 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_ORDER);
    5951          303 :       OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = clauses->order_unconstrained;
    5952          303 :       OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = clauses->order_reproducible;
    5953          303 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5954              :     }
    5955              : 
    5956        32910 :   if (clauses->untied)
    5957              :     {
    5958          142 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_UNTIED);
    5959          142 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5960              :     }
    5961              : 
    5962        32910 :   if (clauses->mergeable)
    5963              :     {
    5964           32 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_MERGEABLE);
    5965           32 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5966              :     }
    5967              : 
    5968        32910 :   if (clauses->collapse)
    5969              :     {
    5970         1843 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_COLLAPSE);
    5971         1843 :       OMP_CLAUSE_COLLAPSE_EXPR (c)
    5972         1843 :         = build_int_cst (integer_type_node, clauses->collapse);
    5973         1843 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5974              :     }
    5975              : 
    5976        32910 :   if (clauses->inbranch)
    5977              :     {
    5978           18 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_INBRANCH);
    5979           18 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5980              :     }
    5981              : 
    5982        32910 :   if (clauses->notinbranch)
    5983              :     {
    5984           25 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOTINBRANCH);
    5985           25 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5986              :     }
    5987              : 
    5988        32910 :   switch (clauses->cancel)
    5989              :     {
    5990              :     case OMP_CANCEL_UNKNOWN:
    5991              :       break;
    5992            0 :     case OMP_CANCEL_PARALLEL:
    5993            0 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_PARALLEL);
    5994            0 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5995            0 :       break;
    5996            0 :     case OMP_CANCEL_SECTIONS:
    5997            0 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SECTIONS);
    5998            0 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    5999            0 :       break;
    6000            0 :     case OMP_CANCEL_DO:
    6001            0 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FOR);
    6002            0 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6003            0 :       break;
    6004            0 :     case OMP_CANCEL_TASKGROUP:
    6005            0 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_TASKGROUP);
    6006            0 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6007            0 :       break;
    6008              :     }
    6009              : 
    6010        32910 :   if (clauses->proc_bind != OMP_PROC_BIND_UNKNOWN)
    6011              :     {
    6012           64 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_PROC_BIND);
    6013           64 :       switch (clauses->proc_bind)
    6014              :         {
    6015            1 :         case OMP_PROC_BIND_PRIMARY:
    6016            1 :           OMP_CLAUSE_PROC_BIND_KIND (c) = OMP_CLAUSE_PROC_BIND_PRIMARY;
    6017            1 :           break;
    6018            9 :         case OMP_PROC_BIND_MASTER:
    6019            9 :           OMP_CLAUSE_PROC_BIND_KIND (c) = OMP_CLAUSE_PROC_BIND_MASTER;
    6020            9 :           break;
    6021           53 :         case OMP_PROC_BIND_SPREAD:
    6022           53 :           OMP_CLAUSE_PROC_BIND_KIND (c) = OMP_CLAUSE_PROC_BIND_SPREAD;
    6023           53 :           break;
    6024            1 :         case OMP_PROC_BIND_CLOSE:
    6025            1 :           OMP_CLAUSE_PROC_BIND_KIND (c) = OMP_CLAUSE_PROC_BIND_CLOSE;
    6026            1 :           break;
    6027            0 :         default:
    6028            0 :           gcc_unreachable ();
    6029              :         }
    6030           64 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6031              :     }
    6032              : 
    6033        32910 :   if (clauses->safelen_expr)
    6034              :     {
    6035           90 :       tree safelen_var;
    6036              : 
    6037           90 :       gfc_init_se (&se, NULL);
    6038           90 :       gfc_conv_expr (&se, clauses->safelen_expr);
    6039           90 :       gfc_add_block_to_block (block, &se.pre);
    6040           90 :       safelen_var = gfc_evaluate_now (se.expr, block);
    6041           90 :       gfc_add_block_to_block (block, &se.post);
    6042              : 
    6043           90 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SAFELEN);
    6044           90 :       OMP_CLAUSE_SAFELEN_EXPR (c) = safelen_var;
    6045           90 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6046              :     }
    6047              : 
    6048        32910 :   if (clauses->simdlen_expr)
    6049              :     {
    6050          136 :       c = build_omp_clause (gfc_get_location (&clauses->simdlen_expr->where),
    6051              :                             OMP_CLAUSE_SIMDLEN);
    6052          136 :       OMP_CLAUSE_SIMDLEN_EXPR (c)
    6053          136 :         = gfc_conv_constant_to_tree (clauses->simdlen_expr);
    6054          136 :       if (clauses->simdlen_type.type != BT_UNKNOWN)
    6055              :         {
    6056           20 :           gfc_typespec ts = {};
    6057           20 :           ts.type = clauses->simdlen_type.type;
    6058           20 :           ts.kind = clauses->simdlen_type.kind;
    6059           20 :           OMP_CLAUSE_SIMDLEN_TYPE (c)
    6060           40 :                 = gfc_typenode_for_spec (&ts, 0);
    6061              :         }
    6062          136 :       if (clauses->simdlen_divisor)
    6063           13 :         OMP_CLAUSE_SIMDLEN_DIVISOR (c)
    6064           26 :           = gfc_conv_constant_to_tree (clauses->simdlen_divisor);
    6065          136 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6066              : 
    6067          136 :       if (declare_simd && OMP_CLAUSE_SIMDLEN_TYPE (c))
    6068              :         /* By contrast, for 'simd', the sorry is in the middle end.  */
    6069           11 :         sorry_at (OMP_CLAUSE_LOCATION (c),
    6070              :                   "%<simdlen%> clause with %<scaled%> modifier");
    6071              :     }
    6072              : 
    6073        32910 :   if (clauses->num_teams_list)
    6074              :     {
    6075              :       tree num_teams = NULL_TREE;
    6076          124 :       for (gfc_expr_list *el = clauses->num_teams_list; el; el = el->next)
    6077           77 :         num_teams = tree_cons (NULL_TREE,
    6078           77 :                                gfc_convert_expr_to_tree (block, el->expr),
    6079              :                                num_teams);
    6080           47 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_TEAMS);
    6081           47 :       if (clauses->num_teams_dims)
    6082              :         {
    6083           10 :           OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = NULL_TREE;
    6084           10 :           OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = nreverse (num_teams);
    6085           10 :           OMP_CLAUSE_NUM_TEAMS_DIMS (c) = clauses->num_teams_dims;
    6086              :         }
    6087              :       else
    6088              :         {
    6089           37 :           OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = TREE_VALUE (num_teams);
    6090           37 :           if (TREE_CHAIN(num_teams))
    6091           18 :             OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
    6092           36 :               = TREE_VALUE (TREE_CHAIN (num_teams));
    6093              :         }
    6094           47 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6095              :     }
    6096              : 
    6097        32910 :   if (clauses->device)
    6098              :     {
    6099          301 :       tree device;
    6100              : 
    6101          301 :       gfc_init_se (&se, NULL);
    6102          301 :       gfc_conv_expr (&se, clauses->device);
    6103          301 :       gfc_add_block_to_block (block, &se.pre);
    6104          301 :       device = gfc_evaluate_now (se.expr, block);
    6105          301 :       gfc_add_block_to_block (block, &se.post);
    6106              : 
    6107          301 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DEVICE);
    6108          301 :       OMP_CLAUSE_DEVICE_ID (c) = device;
    6109              : 
    6110          301 :       if (clauses->ancestor)
    6111           39 :         OMP_CLAUSE_DEVICE_ANCESTOR (c) = 1;
    6112              : 
    6113          301 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6114              :     }
    6115              : 
    6116        32910 :   if (clauses->thread_limit_list)
    6117              :     {
    6118              :       tree thread_limit = NULL_TREE;
    6119          128 :       for (gfc_expr_list *el = clauses->thread_limit_list; el; el = el->next)
    6120           76 :         thread_limit = tree_cons (NULL_TREE,
    6121           76 :                                   gfc_convert_expr_to_tree (block, el->expr),
    6122              :                                   thread_limit);
    6123           52 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_THREAD_LIMIT);
    6124           52 :       OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = nreverse (thread_limit);
    6125           52 :       OMP_CLAUSE_THREAD_LIMIT_STRICT (c) = clauses->thread_limit_strict;
    6126           52 :       OMP_CLAUSE_THREAD_LIMIT_DIMS (c) = clauses->thread_limit_dims;
    6127           52 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6128              :     }
    6129              : 
    6130        32910 :   chunk_size = NULL_TREE;
    6131        32910 :   if (clauses->dist_chunk_size)
    6132              :     {
    6133           81 :       gfc_init_se (&se, NULL);
    6134           81 :       gfc_conv_expr (&se, clauses->dist_chunk_size);
    6135           81 :       gfc_add_block_to_block (block, &se.pre);
    6136           81 :       chunk_size = gfc_evaluate_now (se.expr, block);
    6137           81 :       gfc_add_block_to_block (block, &se.post);
    6138              :     }
    6139              : 
    6140        32910 :   if (clauses->dist_sched_kind != OMP_SCHED_NONE)
    6141              :     {
    6142           94 :       c = build_omp_clause (gfc_get_location (&where),
    6143              :                             OMP_CLAUSE_DIST_SCHEDULE);
    6144           94 :       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = chunk_size;
    6145           94 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6146              :     }
    6147              : 
    6148        32910 :   if (clauses->grainsize)
    6149              :     {
    6150           33 :       tree grainsize;
    6151              : 
    6152           33 :       gfc_init_se (&se, NULL);
    6153           33 :       gfc_conv_expr (&se, clauses->grainsize);
    6154           33 :       gfc_add_block_to_block (block, &se.pre);
    6155           33 :       grainsize = gfc_evaluate_now (se.expr, block);
    6156           33 :       gfc_add_block_to_block (block, &se.post);
    6157              : 
    6158           33 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_GRAINSIZE);
    6159           33 :       OMP_CLAUSE_GRAINSIZE_EXPR (c) = grainsize;
    6160           33 :       if (clauses->grainsize_strict)
    6161            1 :         OMP_CLAUSE_GRAINSIZE_STRICT (c) = 1;
    6162           33 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6163              :     }
    6164              : 
    6165        32910 :   if (clauses->num_tasks)
    6166              :     {
    6167           25 :       tree num_tasks;
    6168              : 
    6169           25 :       gfc_init_se (&se, NULL);
    6170           25 :       gfc_conv_expr (&se, clauses->num_tasks);
    6171           25 :       gfc_add_block_to_block (block, &se.pre);
    6172           25 :       num_tasks = gfc_evaluate_now (se.expr, block);
    6173           25 :       gfc_add_block_to_block (block, &se.post);
    6174              : 
    6175           25 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_TASKS);
    6176           25 :       OMP_CLAUSE_NUM_TASKS_EXPR (c) = num_tasks;
    6177           25 :       if (clauses->num_tasks_strict)
    6178            1 :         OMP_CLAUSE_NUM_TASKS_STRICT (c) = 1;
    6179           25 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6180              :     }
    6181              : 
    6182        32910 :   if (clauses->priority)
    6183              :     {
    6184           34 :       tree priority;
    6185              : 
    6186           34 :       gfc_init_se (&se, NULL);
    6187           34 :       gfc_conv_expr (&se, clauses->priority);
    6188           34 :       gfc_add_block_to_block (block, &se.pre);
    6189           34 :       priority = gfc_evaluate_now (se.expr, block);
    6190           34 :       gfc_add_block_to_block (block, &se.post);
    6191              : 
    6192           34 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_PRIORITY);
    6193           34 :       OMP_CLAUSE_PRIORITY_EXPR (c) = priority;
    6194           34 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6195              :     }
    6196              : 
    6197        32910 :   if (clauses->detach)
    6198              :     {
    6199          116 :       tree detach;
    6200              : 
    6201          116 :       gfc_init_se (&se, NULL);
    6202          116 :       gfc_conv_expr (&se, clauses->detach);
    6203          116 :       gfc_add_block_to_block (block, &se.pre);
    6204          116 :       detach = se.expr;
    6205          116 :       gfc_add_block_to_block (block, &se.post);
    6206              : 
    6207          116 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DETACH);
    6208          116 :       TREE_ADDRESSABLE (detach) = 1;
    6209          116 :       OMP_CLAUSE_DECL (c) = detach;
    6210          116 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6211              :     }
    6212              : 
    6213        32910 :   if (clauses->filter)
    6214              :     {
    6215           31 :       tree filter;
    6216              : 
    6217           31 :       gfc_init_se (&se, NULL);
    6218           31 :       gfc_conv_expr (&se, clauses->filter);
    6219           31 :       gfc_add_block_to_block (block, &se.pre);
    6220           31 :       filter = gfc_evaluate_now (se.expr, block);
    6221           31 :       gfc_add_block_to_block (block, &se.post);
    6222              : 
    6223           31 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FILTER);
    6224           31 :       OMP_CLAUSE_FILTER_EXPR (c) = filter;
    6225           31 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6226              :     }
    6227              : 
    6228        32910 :   if (clauses->hint)
    6229              :     {
    6230           13 :       tree hint;
    6231              : 
    6232           13 :       gfc_init_se (&se, NULL);
    6233           13 :       gfc_conv_expr (&se, clauses->hint);
    6234           13 :       gfc_add_block_to_block (block, &se.pre);
    6235           13 :       hint = gfc_evaluate_now (se.expr, block);
    6236           13 :       gfc_add_block_to_block (block, &se.post);
    6237              : 
    6238           13 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_HINT);
    6239           13 :       OMP_CLAUSE_HINT_EXPR (c) = hint;
    6240           13 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6241              :     }
    6242              : 
    6243        32910 :   if (clauses->simd)
    6244              :     {
    6245           24 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SIMD);
    6246           24 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6247              :     }
    6248        32910 :   if (clauses->threads)
    6249              :     {
    6250           13 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_THREADS);
    6251           13 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6252              :     }
    6253        32910 :   if (clauses->nogroup)
    6254              :     {
    6255           13 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOGROUP);
    6256           13 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6257              :     }
    6258              : 
    6259       230370 :   for (int i = 0; i < OMP_DEFAULTMAP_CAT_NUM; i++)
    6260              :     {
    6261       197460 :       if (clauses->defaultmap[i] == OMP_DEFAULTMAP_UNSET)
    6262       197290 :        continue;
    6263          170 :       enum omp_clause_defaultmap_kind behavior, category;
    6264          170 :       switch ((gfc_omp_defaultmap_category) i)
    6265              :         {
    6266              :         case OMP_DEFAULTMAP_CAT_UNCATEGORIZED:
    6267              :           category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
    6268              :           break;
    6269              :         case OMP_DEFAULTMAP_CAT_ALL:
    6270              :           category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL;
    6271              :           break;
    6272              :         case OMP_DEFAULTMAP_CAT_SCALAR:
    6273              :           category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
    6274              :           break;
    6275              :         case OMP_DEFAULTMAP_CAT_AGGREGATE:
    6276              :           category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
    6277              :           break;
    6278              :         case OMP_DEFAULTMAP_CAT_ALLOCATABLE:
    6279              :           category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE;
    6280              :           break;
    6281              :         case OMP_DEFAULTMAP_CAT_POINTER:
    6282              :           category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
    6283              :           break;
    6284            0 :         default: gcc_unreachable ();
    6285              :         }
    6286          170 :       switch (clauses->defaultmap[i])
    6287              :         {
    6288              :         case OMP_DEFAULTMAP_ALLOC:
    6289              :           behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
    6290              :           break;
    6291              :         case OMP_DEFAULTMAP_TO: behavior = OMP_CLAUSE_DEFAULTMAP_TO; break;
    6292              :         case OMP_DEFAULTMAP_FROM: behavior = OMP_CLAUSE_DEFAULTMAP_FROM; break;
    6293              :         case OMP_DEFAULTMAP_TOFROM:
    6294              :           behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
    6295              :           break;
    6296              :         case OMP_DEFAULTMAP_FIRSTPRIVATE:
    6297              :           behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
    6298              :           break;
    6299              :         case OMP_DEFAULTMAP_PRESENT:
    6300              :           behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
    6301              :           break;
    6302              :         case OMP_DEFAULTMAP_NONE: behavior = OMP_CLAUSE_DEFAULTMAP_NONE; break;
    6303              :         case OMP_DEFAULTMAP_DEFAULT:
    6304              :           behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
    6305              :           break;
    6306            0 :         default: gcc_unreachable ();
    6307              :         }
    6308          170 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DEFAULTMAP);
    6309          170 :       OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
    6310          170 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6311              :     }
    6312              : 
    6313        32910 :   if (clauses->doacross_source)
    6314              :     {
    6315          132 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DOACROSS);
    6316          132 :       OMP_CLAUSE_DOACROSS_KIND (c) = OMP_CLAUSE_DOACROSS_SOURCE;
    6317          132 :       OMP_CLAUSE_DOACROSS_DEPEND (c) = clauses->depend_source;
    6318          132 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6319              :     }
    6320              : 
    6321        32910 :   if (clauses->async)
    6322              :     {
    6323          549 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_ASYNC);
    6324          549 :       if (clauses->async_expr)
    6325          549 :         OMP_CLAUSE_ASYNC_EXPR (c)
    6326         1098 :           = gfc_convert_expr_to_tree (block, clauses->async_expr);
    6327              :       else
    6328            0 :         OMP_CLAUSE_ASYNC_EXPR (c) = NULL;
    6329          549 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6330              :     }
    6331        32910 :   if (clauses->seq)
    6332              :     {
    6333          140 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SEQ);
    6334          140 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6335              :     }
    6336        32910 :   if (clauses->par_auto)
    6337              :     {
    6338           62 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_AUTO);
    6339           62 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6340              :     }
    6341        32910 :   if (clauses->if_present)
    6342              :     {
    6343           23 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_IF_PRESENT);
    6344           23 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6345              :     }
    6346        32910 :   if (clauses->finalize)
    6347              :     {
    6348           23 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FINALIZE);
    6349           23 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6350              :     }
    6351        32910 :   if (clauses->independent)
    6352              :     {
    6353          239 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_INDEPENDENT);
    6354          239 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6355              :     }
    6356        32910 :   if (clauses->wait_list)
    6357              :     {
    6358              :       gfc_expr_list *el;
    6359              : 
    6360          317 :       for (el = clauses->wait_list; el; el = el->next)
    6361              :         {
    6362          172 :           c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_WAIT);
    6363          172 :           OMP_CLAUSE_DECL (c) = gfc_convert_expr_to_tree (block, el->expr);
    6364          172 :           OMP_CLAUSE_CHAIN (c) = omp_clauses;
    6365          172 :           omp_clauses = c;
    6366              :         }
    6367              :     }
    6368        32910 :   if (clauses->num_gangs_expr)
    6369              :     {
    6370          666 :       tree num_gangs_var
    6371          666 :         = gfc_convert_expr_to_tree (block, clauses->num_gangs_expr);
    6372          666 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_GANGS);
    6373          666 :       OMP_CLAUSE_NUM_GANGS_EXPR (c) = num_gangs_var;
    6374          666 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6375              :     }
    6376        32910 :   if (clauses->num_workers_expr)
    6377              :     {
    6378          583 :       tree num_workers_var
    6379          583 :         = gfc_convert_expr_to_tree (block, clauses->num_workers_expr);
    6380          583 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_WORKERS);
    6381          583 :       OMP_CLAUSE_NUM_WORKERS_EXPR (c) = num_workers_var;
    6382          583 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6383              :     }
    6384        32910 :   if (clauses->vector_length_expr)
    6385              :     {
    6386          553 :       tree vector_length_var
    6387          553 :         = gfc_convert_expr_to_tree (block, clauses->vector_length_expr);
    6388          553 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_VECTOR_LENGTH);
    6389          553 :       OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = vector_length_var;
    6390          553 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6391              :     }
    6392        32910 :   if (clauses->tile_list)
    6393              :     {
    6394              :       tree list = NULL_TREE;
    6395          174 :       for (gfc_expr_list *el = clauses->tile_list; el; el = el->next)
    6396          114 :         list = tree_cons (NULL_TREE, gfc_convert_expr_to_tree (block, el->expr),
    6397              :                           list);
    6398              : 
    6399           60 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_TILE);
    6400           60 :       OMP_CLAUSE_TILE_LIST (c) = nreverse (list);
    6401           60 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6402              :     }
    6403        32910 :   if (clauses->vector)
    6404              :     {
    6405          835 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_VECTOR);
    6406          835 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6407              : 
    6408          835 :       if (clauses->vector_expr)
    6409              :         {
    6410          119 :           tree vector_var
    6411          119 :             = gfc_convert_expr_to_tree (block, clauses->vector_expr);
    6412          119 :           OMP_CLAUSE_VECTOR_EXPR (c) = vector_var;
    6413              : 
    6414              :           /* TODO: We're not capturing location information for individual
    6415              :              clauses.  However, if we have an expression attached to the
    6416              :              clause, that one provides better location information.  */
    6417          238 :           OMP_CLAUSE_LOCATION (c)
    6418          119 :             = gfc_get_location (&clauses->vector_expr->where);
    6419              :         }
    6420              :     }
    6421        32910 :   if (clauses->worker)
    6422              :     {
    6423          730 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_WORKER);
    6424          730 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6425              : 
    6426          730 :       if (clauses->worker_expr)
    6427              :         {
    6428           89 :           tree worker_var
    6429           89 :             = gfc_convert_expr_to_tree (block, clauses->worker_expr);
    6430           89 :           OMP_CLAUSE_WORKER_EXPR (c) = worker_var;
    6431              : 
    6432              :           /* TODO: We're not capturing location information for individual
    6433              :              clauses.  However, if we have an expression attached to the
    6434              :              clause, that one provides better location information.  */
    6435          178 :           OMP_CLAUSE_LOCATION (c)
    6436           89 :             = gfc_get_location (&clauses->worker_expr->where);
    6437              :         }
    6438              :     }
    6439        32910 :   if (clauses->gang)
    6440              :     {
    6441         1011 :       tree arg;
    6442         1011 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_GANG);
    6443         1011 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6444              : 
    6445         1011 :       if (clauses->gang_num_expr)
    6446              :         {
    6447          101 :           arg = gfc_convert_expr_to_tree (block, clauses->gang_num_expr);
    6448          101 :           OMP_CLAUSE_GANG_EXPR (c) = arg;
    6449              : 
    6450              :           /* TODO: We're not capturing location information for individual
    6451              :              clauses.  However, if we have an expression attached to the
    6452              :              clause, that one provides better location information.  */
    6453          202 :           OMP_CLAUSE_LOCATION (c)
    6454          101 :             = gfc_get_location (&clauses->gang_num_expr->where);
    6455              :         }
    6456              : 
    6457         1011 :       if (clauses->gang_static)
    6458              :         {
    6459           15 :           arg = clauses->gang_static_expr
    6460          104 :             ? gfc_convert_expr_to_tree (block, clauses->gang_static_expr)
    6461              :             : integer_minus_one_node;
    6462          104 :           OMP_CLAUSE_GANG_STATIC_EXPR (c) = arg;
    6463              :         }
    6464              :     }
    6465        32910 :   if (clauses->bind != OMP_BIND_UNSET)
    6466              :     {
    6467           31 :       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_BIND);
    6468           31 :       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
    6469           31 :       switch (clauses->bind)
    6470              :         {
    6471           10 :         case OMP_BIND_TEAMS:
    6472           10 :           OMP_CLAUSE_BIND_KIND (c) = OMP_CLAUSE_BIND_TEAMS;
    6473           10 :           break;
    6474           15 :         case OMP_BIND_PARALLEL:
    6475           15 :           OMP_CLAUSE_BIND_KIND (c) = OMP_CLAUSE_BIND_PARALLEL;
    6476           15 :           break;
    6477            6 :         case OMP_BIND_THREAD:
    6478            6 :           OMP_CLAUSE_BIND_KIND (c) = OMP_CLAUSE_BIND_THREAD;
    6479            6 :           break;
    6480            0 :         default:
    6481            0 :           gcc_unreachable ();
    6482              :         }
    6483              :     }
    6484              :   /* OpenACC 'nohost' clauses cannot appear here.  */
    6485        32910 :   gcc_checking_assert (!clauses->nohost);
    6486              : 
    6487              :   /* OpenACC 'device_num' and 'device_type' clauses cannot appear here.  */
    6488        32910 :   gcc_checking_assert (!clauses->device_num_expr
    6489              :                        && !clauses->oacc_device_type_present);
    6490              : 
    6491        32910 :   return nreverse (omp_clauses);
    6492              : }
    6493              : 
    6494              : /* Like gfc_trans_code, but force creation of a BIND_EXPR around it.  */
    6495              : 
    6496              : static tree
    6497        21696 : gfc_trans_omp_code (gfc_code *code, bool force_empty)
    6498              : {
    6499        21696 :   tree stmt;
    6500              : 
    6501        21696 :   pushlevel ();
    6502        21696 :   stmt = gfc_trans_code (code);
    6503        21696 :   if (TREE_CODE (stmt) != BIND_EXPR)
    6504              :     {
    6505        19253 :       if (!IS_EMPTY_STMT (stmt) || force_empty)
    6506              :         {
    6507        19163 :           tree block = poplevel (1, 0);
    6508        19163 :           stmt = build3_v (BIND_EXPR, NULL, stmt, block);
    6509              :         }
    6510              :       else
    6511           90 :         poplevel (0, 0);
    6512              :     }
    6513              :   else
    6514         2443 :     poplevel (0, 0);
    6515        21696 :   return stmt;
    6516              : }
    6517              : 
    6518              : /* Translate OpenACC 'parallel', 'kernels', 'serial', 'data', 'host_data'
    6519              :    construct. */
    6520              : 
    6521              : static tree
    6522         4189 : gfc_trans_oacc_construct (gfc_code *code)
    6523              : {
    6524         4189 :   stmtblock_t block;
    6525         4189 :   tree stmt, oacc_clauses;
    6526         4189 :   enum tree_code construct_code;
    6527              : 
    6528         4189 :   switch (code->op)
    6529              :     {
    6530              :       case EXEC_OACC_PARALLEL:
    6531              :         construct_code = OACC_PARALLEL;
    6532              :         break;
    6533              :       case EXEC_OACC_KERNELS:
    6534              :         construct_code = OACC_KERNELS;
    6535              :         break;
    6536              :       case EXEC_OACC_SERIAL:
    6537              :         construct_code = OACC_SERIAL;
    6538              :         break;
    6539              :       case EXEC_OACC_DATA:
    6540              :         construct_code = OACC_DATA;
    6541              :         break;
    6542              :       case EXEC_OACC_HOST_DATA:
    6543              :         construct_code = OACC_HOST_DATA;
    6544              :         break;
    6545            0 :       default:
    6546            0 :         gcc_unreachable ();
    6547              :     }
    6548              : 
    6549         4189 :   gfc_start_block (&block);
    6550         4189 :   oacc_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    6551              :                                         code->loc, false, true);
    6552         4189 :   pushlevel ();
    6553         4189 :   stmt = gfc_trans_omp_code (code->block->next, true);
    6554         4189 :   stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    6555         4189 :   stmt = build2_loc (gfc_get_location (&code->loc), construct_code,
    6556              :                      void_type_node, stmt, oacc_clauses);
    6557         4189 :   gfc_add_expr_to_block (&block, stmt);
    6558         4189 :   return gfc_finish_block (&block);
    6559              : }
    6560              : 
    6561              : /* update, enter_data, exit_data, cache, init, set, shutdown.  */
    6562              : static tree
    6563         2498 : gfc_trans_oacc_executable_directive (gfc_code *code)
    6564              : {
    6565         2498 :   stmtblock_t block;
    6566         2498 :   tree stmt, oacc_clauses;
    6567         2498 :   enum tree_code construct_code;
    6568              : 
    6569         2498 :   switch (code->op)
    6570              :     {
    6571              :       case EXEC_OACC_UPDATE:
    6572              :         construct_code = OACC_UPDATE;
    6573              :         break;
    6574          787 :       case EXEC_OACC_ENTER_DATA:
    6575          787 :         construct_code = OACC_ENTER_DATA;
    6576          787 :         break;
    6577          575 :       case EXEC_OACC_EXIT_DATA:
    6578          575 :         construct_code = OACC_EXIT_DATA;
    6579          575 :         break;
    6580           76 :       case EXEC_OACC_CACHE:
    6581           76 :         construct_code = OACC_CACHE;
    6582           76 :         break;
    6583          368 :       case EXEC_OACC_INIT:
    6584          368 :       case EXEC_OACC_SHUTDOWN:
    6585          368 :       case EXEC_OACC_SET:
    6586          368 :         goto builtin_oacc_exec_directive;
    6587            0 :       default:
    6588            0 :         gcc_unreachable ();
    6589              :     }
    6590              : 
    6591         2130 :   gfc_start_block (&block);
    6592         2130 :   oacc_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    6593              :                                         code->loc, false, true, code->op);
    6594         2130 :   stmt = build1_loc (input_location, construct_code, void_type_node,
    6595              :                      oacc_clauses);
    6596         2130 :   gfc_add_expr_to_block (&block, stmt);
    6597         2130 :   return gfc_finish_block (&block);
    6598              : 
    6599          368 : builtin_oacc_exec_directive:
    6600              : 
    6601          368 :   enum built_in_function builtin_code;
    6602              : 
    6603          368 :   switch (code->op)
    6604              :   {
    6605              :     case EXEC_OACC_INIT:
    6606              :       builtin_code = BUILT_IN_GOACC_INIT;
    6607              :       break;
    6608          121 :     case EXEC_OACC_SHUTDOWN:
    6609          121 :       builtin_code = BUILT_IN_GOACC_SHUTDOWN;
    6610          121 :       break;
    6611          119 :     case EXEC_OACC_SET:
    6612          119 :       builtin_code = BUILT_IN_GOACC_SET_DEVICE;
    6613          119 :       break;
    6614              :     default:
    6615              :       gcc_unreachable ();
    6616              :   }
    6617              : 
    6618          368 :   location_t loc = input_location;
    6619          368 :   gfc_omp_clauses *clauses = code->ext.omp_clauses;
    6620              : 
    6621          368 :   gfc_start_block (&block);
    6622              : 
    6623          368 :   tree n_device;
    6624          368 :   if (clauses->device_num_expr)
    6625          105 :     n_device = gfc_convert_expr_to_tree (&block, clauses->device_num_expr);
    6626              :   else
    6627              :     /* no 'device_num' clause specified by
    6628              :        the user, we don't modify the value of ICV
    6629              :        'acc-current-device-num-var' or we do not
    6630              :        take any action in init and shutdown directive
    6631              :        using -1 value.  */
    6632          263 :     n_device = build_int_cst (integer_type_node, -1);
    6633              : 
    6634              :   /* GOMP_DEVICE_NONE is used to make the operation
    6635              :      in all the devices.
    6636              : 
    6637              :      GOMP_DEVICE_DEFAULT is used in set directive
    6638              :      to do nothing if the clause do not appear.  */
    6639          368 :   int device_type = code->op == EXEC_OACC_SET ?
    6640              :                     GOMP_DEVICE_DEFAULT       :
    6641              :                     GOMP_DEVICE_NONE;
    6642          368 :   if (clauses->oacc_device_type_present)
    6643          324 :     device_type = clauses->oacc_device_type;
    6644              : 
    6645          368 :   tree d_type = build_int_cst (integer_type_node, device_type);
    6646              : 
    6647          368 :   stmt = builtin_decl_explicit (builtin_code);
    6648              : 
    6649          368 :   stmt = build_call_expr_loc (loc, stmt, 2, n_device, d_type);
    6650              : 
    6651          368 :   if (clauses->if_expr)
    6652          108 :     stmt = build3_loc (input_location, COND_EXPR, void_type_node,
    6653              :                        gfc_convert_expr_to_tree (&block, clauses->if_expr),
    6654              :                        stmt, NULL_TREE);
    6655              : 
    6656          368 :   gfc_add_expr_to_block (&block, stmt);
    6657              : 
    6658          368 :   return gfc_finish_block (&block);
    6659              : }
    6660              : 
    6661              : static tree
    6662          173 : gfc_trans_oacc_wait_directive (gfc_code *code)
    6663              : {
    6664          173 :   stmtblock_t block;
    6665          173 :   tree stmt, t;
    6666          173 :   vec<tree, va_gc> *args;
    6667          173 :   int nparms = 0;
    6668          173 :   gfc_expr_list *el;
    6669          173 :   gfc_omp_clauses *clauses = code->ext.omp_clauses;
    6670          173 :   location_t loc = input_location;
    6671              : 
    6672          303 :   for (el = clauses->wait_list; el; el = el->next)
    6673          130 :     nparms++;
    6674              : 
    6675          173 :   vec_alloc (args, nparms + 2);
    6676          173 :   stmt = builtin_decl_explicit (BUILT_IN_GOACC_WAIT);
    6677              : 
    6678          173 :   gfc_start_block (&block);
    6679              : 
    6680          173 :   if (clauses->async_expr)
    6681            3 :     t = gfc_convert_expr_to_tree (&block, clauses->async_expr);
    6682              :   else
    6683          170 :     t = build_int_cst (integer_type_node, -2);
    6684              : 
    6685          173 :   args->quick_push (t);
    6686          173 :   args->quick_push (build_int_cst (integer_type_node, nparms));
    6687              : 
    6688          303 :   for (el = clauses->wait_list; el; el = el->next)
    6689          130 :     args->quick_push (gfc_convert_expr_to_tree (&block, el->expr));
    6690              : 
    6691          173 :   stmt = build_call_expr_loc_vec (loc, stmt, args);
    6692          173 :   if (clauses->if_expr)
    6693            6 :     stmt = build3_loc (input_location, COND_EXPR, void_type_node,
    6694              :                        gfc_convert_expr_to_tree (&block, clauses->if_expr),
    6695              :                        stmt, NULL_TREE);
    6696          173 :   gfc_add_expr_to_block (&block, stmt);
    6697              : 
    6698          173 :   vec_free (args);
    6699              : 
    6700          173 :   return gfc_finish_block (&block);
    6701              : }
    6702              : 
    6703              : static tree gfc_trans_omp_sections (gfc_code *, gfc_omp_clauses *);
    6704              : static tree gfc_trans_omp_workshare (gfc_code *, gfc_omp_clauses *);
    6705              : 
    6706              : static tree
    6707           35 : gfc_trans_omp_allocators (gfc_code *code)
    6708              : {
    6709           35 :   static bool warned = false;
    6710           35 :   gfc_omp_namelist *omp_allocate
    6711           35 :     = code->ext.omp_clauses->lists[OMP_LIST_ALLOCATE];
    6712           35 :   if (!flag_openmp_allocators && !warned)
    6713              :     {
    6714            3 :       omp_allocate = NULL;
    6715            3 :       gfc_error ("%<!$OMP %s%> at %L requires %<-fopenmp-allocators%>",
    6716            3 :                  code->op == EXEC_OMP_ALLOCATE ? "ALLOCATE" : "ALLOCATORS",
    6717              :                  &code->loc);
    6718            3 :       warning (0, "All files that might deallocate such a variable must be "
    6719              :                   "compiled with %<-fopenmp-allocators%>");
    6720            3 :       inform (UNKNOWN_LOCATION,
    6721              :               "This includes explicit DEALLOCATE, reallocation on intrinsic "
    6722              :               "assignment, INTENT(OUT) for allocatable dummy arguments, and "
    6723              :               "reallocation of allocatable components allocated with an "
    6724              :               "OpenMP allocator");
    6725            3 :       warned = true;
    6726              :     }
    6727           35 :   return gfc_trans_allocate (code->block->next, omp_allocate);
    6728              : }
    6729              : 
    6730              : static tree
    6731           12 : gfc_trans_omp_assume (gfc_code *code)
    6732              : {
    6733           12 :   stmtblock_t block;
    6734           12 :   gfc_init_block (&block);
    6735           12 :   gfc_omp_assumptions *assume = code->ext.omp_clauses->assume;
    6736           12 :   if (assume)
    6737           23 :     for (gfc_expr_list *el = assume->holds; el; el = el->next)
    6738              :       {
    6739           11 :         location_t loc = gfc_get_location (&el->expr->where);
    6740           11 :         gfc_se se;
    6741           11 :         gfc_init_se (&se, NULL);
    6742           11 :         gfc_conv_expr (&se, el->expr);
    6743           11 :         tree t;
    6744           11 :         if (se.pre.head == NULL_TREE && se.post.head == NULL_TREE)
    6745           10 :           t = se.expr;
    6746              :         else
    6747              :           {
    6748            1 :             tree var = create_tmp_var_raw (boolean_type_node);
    6749            1 :             DECL_CONTEXT (var) = current_function_decl;
    6750            1 :             stmtblock_t block2;
    6751            1 :             gfc_init_block (&block2);
    6752            1 :             gfc_add_block_to_block (&block2, &se.pre);
    6753            1 :             gfc_add_modify_loc (loc, &block2, var,
    6754              :                                 fold_convert_loc (loc, boolean_type_node,
    6755              :                                                   se.expr));
    6756            1 :             gfc_add_block_to_block (&block2, &se.post);
    6757            1 :             t = gfc_finish_block (&block2);
    6758            1 :             t = build4 (TARGET_EXPR, boolean_type_node, var, t, NULL, NULL);
    6759              :           }
    6760           11 :         t = build_call_expr_internal_loc (loc, IFN_ASSUME,
    6761              :                                           void_type_node, 1, t);
    6762           11 :         gfc_add_expr_to_block (&block, t);
    6763              :       }
    6764           12 :   gfc_add_expr_to_block (&block, gfc_trans_omp_code (code->block->next, true));
    6765           12 :   return gfc_finish_block (&block);
    6766              : }
    6767              : 
    6768              : static tree
    6769         2607 : gfc_trans_omp_atomic (gfc_code *code)
    6770              : {
    6771         2607 :   gfc_code *atomic_code = code->block;
    6772         2607 :   gfc_se lse;
    6773         2607 :   gfc_se rse;
    6774         2607 :   gfc_se vse;
    6775         2607 :   gfc_expr *expr1, *expr2, *e, *capture_expr1 = NULL, *capture_expr2 = NULL;
    6776         2607 :   gfc_symbol *var;
    6777         2607 :   stmtblock_t block;
    6778         2607 :   tree lhsaddr, type, rhs, x, compare = NULL_TREE, comp_tgt = NULL_TREE;
    6779         2607 :   enum tree_code op = ERROR_MARK;
    6780         2607 :   enum tree_code aop = OMP_ATOMIC;
    6781         2607 :   bool var_on_left = false, else_branch = false;
    6782         2607 :   enum omp_memory_order mo, fail_mo;
    6783         2607 :   switch (atomic_code->ext.omp_clauses->memorder)
    6784              :     {
    6785              :     case OMP_MEMORDER_UNSET: mo = OMP_MEMORY_ORDER_UNSPECIFIED; break;
    6786              :     case OMP_MEMORDER_ACQ_REL: mo = OMP_MEMORY_ORDER_ACQ_REL; break;
    6787              :     case OMP_MEMORDER_ACQUIRE: mo = OMP_MEMORY_ORDER_ACQUIRE; break;
    6788              :     case OMP_MEMORDER_RELAXED: mo = OMP_MEMORY_ORDER_RELAXED; break;
    6789              :     case OMP_MEMORDER_RELEASE: mo = OMP_MEMORY_ORDER_RELEASE; break;
    6790              :     case OMP_MEMORDER_SEQ_CST: mo = OMP_MEMORY_ORDER_SEQ_CST; break;
    6791            0 :     default: gcc_unreachable ();
    6792              :     }
    6793         2607 :   switch (atomic_code->ext.omp_clauses->fail)
    6794              :     {
    6795              :     case OMP_MEMORDER_UNSET: fail_mo = OMP_FAIL_MEMORY_ORDER_UNSPECIFIED; break;
    6796           14 :     case OMP_MEMORDER_ACQUIRE: fail_mo = OMP_FAIL_MEMORY_ORDER_ACQUIRE; break;
    6797           26 :     case OMP_MEMORDER_RELAXED: fail_mo = OMP_FAIL_MEMORY_ORDER_RELAXED; break;
    6798            2 :     case OMP_MEMORDER_SEQ_CST: fail_mo = OMP_FAIL_MEMORY_ORDER_SEQ_CST; break;
    6799            0 :     default: gcc_unreachable ();
    6800              :     }
    6801         2607 :   mo = (omp_memory_order) (mo | fail_mo);
    6802              : 
    6803         2607 :   code = code->block->next;
    6804         2607 :   if (atomic_code->ext.omp_clauses->compare)
    6805              :     {
    6806          145 :       gfc_expr *comp_expr;
    6807          145 :       if (code->op == EXEC_IF)
    6808              :         {
    6809          126 :           comp_expr = code->block->expr1;
    6810          126 :           gcc_assert (code->block->next->op == EXEC_ASSIGN);
    6811          126 :           expr1 = code->block->next->expr1;
    6812          126 :           expr2 = code->block->next->expr2;
    6813          126 :           if (code->block->block)
    6814              :             {
    6815           64 :               gcc_assert (atomic_code->ext.omp_clauses->capture
    6816              :                           && code->block->block->next->op == EXEC_ASSIGN);
    6817           64 :               else_branch = true;
    6818           64 :               aop = OMP_ATOMIC_CAPTURE_OLD;
    6819           64 :               capture_expr1 = code->block->block->next->expr1;
    6820           64 :               capture_expr2 = code->block->block->next->expr2;
    6821              :             }
    6822           62 :           else if (atomic_code->ext.omp_clauses->capture)
    6823              :             {
    6824           19 :               gcc_assert (code->next->op == EXEC_ASSIGN);
    6825           19 :               aop = OMP_ATOMIC_CAPTURE_NEW;
    6826           19 :               capture_expr1 = code->next->expr1;
    6827           19 :               capture_expr2 = code->next->expr2;
    6828              :             }
    6829              :         }
    6830              :       else
    6831              :         {
    6832           19 :           gcc_assert (atomic_code->ext.omp_clauses->capture
    6833              :                       && code->op == EXEC_ASSIGN
    6834              :                       && code->next->op == EXEC_IF);
    6835           19 :           aop = OMP_ATOMIC_CAPTURE_OLD;
    6836           19 :           capture_expr1 = code->expr1;
    6837           19 :           capture_expr2 = code->expr2;
    6838           19 :           expr1 = code->next->block->next->expr1;
    6839           19 :           expr2 = code->next->block->next->expr2;
    6840           19 :           comp_expr = code->next->block->expr1;
    6841              :         }
    6842          145 :       gfc_init_se (&lse, NULL);
    6843          145 :       gfc_conv_expr (&lse, comp_expr->value.op.op2);
    6844          145 :       gfc_add_block_to_block (&block, &lse.pre);
    6845          145 :       compare = lse.expr;
    6846          145 :       var = expr1->symtree->n.sym;
    6847              :     }
    6848              :   else
    6849              :     {
    6850         2462 :       gcc_assert (code->op == EXEC_ASSIGN);
    6851         2462 :       expr1 = code->expr1;
    6852         2462 :       expr2 = code->expr2;
    6853         2462 :       if (atomic_code->ext.omp_clauses->capture
    6854          463 :           && (expr2->expr_type == EXPR_VARIABLE
    6855          245 :               || (expr2->expr_type == EXPR_FUNCTION
    6856          113 :                   && expr2->value.function.isym
    6857          113 :                   && expr2->value.function.isym->id == GFC_ISYM_CONVERSION
    6858           41 :                   && (expr2->value.function.actual->expr->expr_type
    6859              :                       == EXPR_VARIABLE))))
    6860              :         {
    6861          235 :           capture_expr1 = expr1;
    6862          235 :           capture_expr2 = expr2;
    6863          235 :           expr1 = code->next->expr1;
    6864          235 :           expr2 = code->next->expr2;
    6865          235 :           aop = OMP_ATOMIC_CAPTURE_OLD;
    6866              :         }
    6867         2227 :       else if (atomic_code->ext.omp_clauses->capture)
    6868              :         {
    6869          228 :           aop = OMP_ATOMIC_CAPTURE_NEW;
    6870          228 :           capture_expr1 = code->next->expr1;
    6871          228 :           capture_expr2 = code->next->expr2;
    6872              :         }
    6873         2462 :       var = expr1->symtree->n.sym;
    6874              :     }
    6875              : 
    6876         2607 :   gfc_init_se (&lse, NULL);
    6877         2607 :   gfc_init_se (&rse, NULL);
    6878         2607 :   gfc_init_se (&vse, NULL);
    6879         2607 :   gfc_start_block (&block);
    6880              : 
    6881         2607 :   if (((atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_MASK)
    6882              :        != GFC_OMP_ATOMIC_WRITE)
    6883         2198 :       && expr2->expr_type == EXPR_FUNCTION
    6884          472 :       && expr2->value.function.isym
    6885          472 :       && expr2->value.function.isym->id == GFC_ISYM_CONVERSION)
    6886          139 :     expr2 = expr2->value.function.actual->expr;
    6887              : 
    6888         2607 :   if ((atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_MASK)
    6889              :       == GFC_OMP_ATOMIC_READ)
    6890              :     {
    6891          497 :       gfc_conv_expr (&vse, expr1);
    6892          497 :       gfc_add_block_to_block (&block, &vse.pre);
    6893              : 
    6894          497 :       gfc_conv_expr (&lse, expr2);
    6895          497 :       gfc_add_block_to_block (&block, &lse.pre);
    6896          497 :       type = TREE_TYPE (lse.expr);
    6897          497 :       lhsaddr = gfc_build_addr_expr (NULL, lse.expr);
    6898              : 
    6899          497 :       x = build1 (OMP_ATOMIC_READ, type, lhsaddr);
    6900          497 :       OMP_ATOMIC_MEMORY_ORDER (x) = mo;
    6901          497 :       x = convert (TREE_TYPE (vse.expr), x);
    6902          497 :       gfc_add_modify (&block, vse.expr, x);
    6903              : 
    6904          497 :       gfc_add_block_to_block (&block, &lse.pre);
    6905          497 :       gfc_add_block_to_block (&block, &rse.pre);
    6906              : 
    6907          497 :       return gfc_finish_block (&block);
    6908              :     }
    6909              : 
    6910         2110 :   if (capture_expr2
    6911          565 :       && capture_expr2->expr_type == EXPR_FUNCTION
    6912           21 :       && capture_expr2->value.function.isym
    6913           21 :       && capture_expr2->value.function.isym->id == GFC_ISYM_CONVERSION)
    6914           21 :     capture_expr2 = capture_expr2->value.function.actual->expr;
    6915          565 :   gcc_assert (!capture_expr2 || capture_expr2->expr_type == EXPR_VARIABLE);
    6916              : 
    6917         2110 :   if (aop == OMP_ATOMIC_CAPTURE_OLD)
    6918              :     {
    6919          318 :       gfc_conv_expr (&vse, capture_expr1);
    6920          318 :       gfc_add_block_to_block (&block, &vse.pre);
    6921          318 :       gfc_conv_expr (&lse, capture_expr2);
    6922          318 :       gfc_add_block_to_block (&block, &lse.pre);
    6923          318 :       gfc_init_se (&lse, NULL);
    6924              :     }
    6925              : 
    6926         2110 :   gfc_conv_expr (&lse, expr1);
    6927         2110 :   gfc_add_block_to_block (&block, &lse.pre);
    6928         2110 :   type = TREE_TYPE (lse.expr);
    6929         2110 :   lhsaddr = gfc_build_addr_expr (NULL, lse.expr);
    6930              : 
    6931         2110 :   if (((atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_MASK)
    6932              :        == GFC_OMP_ATOMIC_WRITE)
    6933         1701 :       || (atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_SWAP)
    6934         1679 :       || compare)
    6935              :     {
    6936          576 :       gfc_conv_expr (&rse, expr2);
    6937          576 :       gfc_add_block_to_block (&block, &rse.pre);
    6938              :     }
    6939         1534 :   else if (expr2->expr_type == EXPR_OP)
    6940              :     {
    6941         1188 :       gfc_expr *e;
    6942         1188 :       switch (expr2->value.op.op)
    6943              :         {
    6944              :         case INTRINSIC_PLUS:
    6945              :           op = PLUS_EXPR;
    6946              :           break;
    6947           91 :         case INTRINSIC_TIMES:
    6948           91 :           op = MULT_EXPR;
    6949           91 :           break;
    6950          113 :         case INTRINSIC_MINUS:
    6951          113 :           op = MINUS_EXPR;
    6952          113 :           break;
    6953           91 :         case INTRINSIC_DIVIDE:
    6954           91 :           if (expr2->ts.type == BT_INTEGER)
    6955              :             op = TRUNC_DIV_EXPR;
    6956              :           else
    6957           74 :             op = RDIV_EXPR;
    6958              :           break;
    6959           43 :         case INTRINSIC_AND:
    6960           43 :           op = TRUTH_ANDIF_EXPR;
    6961           43 :           break;
    6962           49 :         case INTRINSIC_OR:
    6963           49 :           op = TRUTH_ORIF_EXPR;
    6964           49 :           break;
    6965           43 :         case INTRINSIC_EQV:
    6966           43 :           op = EQ_EXPR;
    6967           43 :           break;
    6968           43 :         case INTRINSIC_NEQV:
    6969           43 :           op = NE_EXPR;
    6970           43 :           break;
    6971            0 :         default:
    6972            0 :           gcc_unreachable ();
    6973              :         }
    6974         1188 :       e = expr2->value.op.op1;
    6975         1188 :       if (e->expr_type == EXPR_FUNCTION
    6976           48 :           && e->value.function.isym
    6977           48 :           && e->value.function.isym->id == GFC_ISYM_CONVERSION)
    6978           48 :         e = e->value.function.actual->expr;
    6979         1188 :       if (e->expr_type == EXPR_VARIABLE
    6980          929 :           && e->symtree != NULL
    6981          929 :           && e->symtree->n.sym == var)
    6982              :         {
    6983          914 :           expr2 = expr2->value.op.op2;
    6984          914 :           var_on_left = true;
    6985              :         }
    6986              :       else
    6987              :         {
    6988          274 :           e = expr2->value.op.op2;
    6989          274 :           if (e->expr_type == EXPR_FUNCTION
    6990           48 :               && e->value.function.isym
    6991           48 :               && e->value.function.isym->id == GFC_ISYM_CONVERSION)
    6992           48 :             e = e->value.function.actual->expr;
    6993          274 :           gcc_assert (e->expr_type == EXPR_VARIABLE
    6994              :                       && e->symtree != NULL
    6995              :                       && e->symtree->n.sym == var);
    6996              :           expr2 = expr2->value.op.op1;
    6997              :           var_on_left = false;
    6998              :         }
    6999         1188 :       gfc_conv_expr (&rse, expr2);
    7000         1188 :       gfc_add_block_to_block (&block, &rse.pre);
    7001              :     }
    7002              :   else
    7003              :     {
    7004          346 :       gcc_assert (expr2->expr_type == EXPR_FUNCTION);
    7005          346 :       switch (expr2->value.function.isym->id)
    7006              :         {
    7007              :         case GFC_ISYM_MIN:
    7008              :           op = MIN_EXPR;
    7009              :           break;
    7010          114 :         case GFC_ISYM_MAX:
    7011          114 :           op = MAX_EXPR;
    7012          114 :           break;
    7013           47 :         case GFC_ISYM_IAND:
    7014           47 :           op = BIT_AND_EXPR;
    7015           47 :           break;
    7016           49 :         case GFC_ISYM_IOR:
    7017           49 :           op = BIT_IOR_EXPR;
    7018           49 :           break;
    7019           45 :         case GFC_ISYM_IEOR:
    7020           45 :           op = BIT_XOR_EXPR;
    7021           45 :           break;
    7022            0 :         default:
    7023            0 :           gcc_unreachable ();
    7024              :         }
    7025          346 :       e = expr2->value.function.actual->expr;
    7026          346 :       if (e->expr_type == EXPR_FUNCTION
    7027           13 :           && e->value.function.isym
    7028           13 :           && e->value.function.isym->id == GFC_ISYM_CONVERSION)
    7029           13 :         e = e->value.function.actual->expr;
    7030          346 :       gcc_assert (e->expr_type == EXPR_VARIABLE
    7031              :                   && e->symtree != NULL
    7032              :                   && e->symtree->n.sym == var);
    7033              : 
    7034          346 :       gfc_conv_expr (&rse, expr2->value.function.actual->next->expr);
    7035          346 :       gfc_add_block_to_block (&block, &rse.pre);
    7036          346 :       if (expr2->value.function.actual->next->next != NULL)
    7037              :         {
    7038           26 :           tree accum = gfc_create_var (TREE_TYPE (rse.expr), NULL);
    7039           26 :           gfc_actual_arglist *arg;
    7040              : 
    7041           26 :           gfc_add_modify (&block, accum, rse.expr);
    7042           64 :           for (arg = expr2->value.function.actual->next->next; arg;
    7043           38 :                arg = arg->next)
    7044              :             {
    7045           38 :               gfc_init_block (&rse.pre);
    7046           38 :               gfc_conv_expr (&rse, arg->expr);
    7047           38 :               gfc_add_block_to_block (&block, &rse.pre);
    7048           38 :               x = fold_build2_loc (input_location, op, TREE_TYPE (accum),
    7049              :                                    accum, rse.expr);
    7050           38 :               gfc_add_modify (&block, accum, x);
    7051              :             }
    7052              : 
    7053           26 :           rse.expr = accum;
    7054              :         }
    7055              : 
    7056          346 :       expr2 = expr2->value.function.actual->next->expr;
    7057              :     }
    7058              : 
    7059         2110 :   lhsaddr = save_expr (lhsaddr);
    7060         2110 :   if (TREE_CODE (lhsaddr) != SAVE_EXPR
    7061         2110 :       && (TREE_CODE (lhsaddr) != ADDR_EXPR
    7062         1650 :           || !VAR_P (TREE_OPERAND (lhsaddr, 0))))
    7063              :     {
    7064              :       /* Make sure LHS is simple enough so that goa_lhs_expr_p can recognize
    7065              :          it even after unsharing function body.  */
    7066           44 :       tree var = create_tmp_var_raw (TREE_TYPE (lhsaddr));
    7067           44 :       DECL_CONTEXT (var) = current_function_decl;
    7068           44 :       lhsaddr = build4 (TARGET_EXPR, TREE_TYPE (lhsaddr), var, lhsaddr,
    7069              :                         NULL_TREE, NULL_TREE);
    7070              :     }
    7071              : 
    7072         2110 :   if (compare)
    7073              :     {
    7074          145 :       tree var = create_tmp_var_raw (TREE_TYPE (lhsaddr));
    7075          145 :       DECL_CONTEXT (var) = current_function_decl;
    7076          145 :       lhsaddr = build4 (TARGET_EXPR, TREE_TYPE (lhsaddr), var, lhsaddr, NULL,
    7077              :                         NULL);
    7078          145 :       lse.expr = build_fold_indirect_ref_loc (input_location, lhsaddr);
    7079          145 :       compare = convert (TREE_TYPE (lse.expr), compare);
    7080          145 :       compare = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    7081              :                                  lse.expr, compare);
    7082              :     }
    7083              : 
    7084         2110 :   if (expr2->expr_type == EXPR_VARIABLE || compare)
    7085          461 :     rhs = rse.expr;
    7086              :   else
    7087         1649 :     rhs = gfc_evaluate_now (rse.expr, &block);
    7088              : 
    7089         2110 :   if (((atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_MASK)
    7090              :        == GFC_OMP_ATOMIC_WRITE)
    7091         1701 :       || (atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_SWAP)
    7092         1679 :       || compare)
    7093              :     x = rhs;
    7094              :   else
    7095              :     {
    7096         1534 :       x = convert (TREE_TYPE (rhs),
    7097              :                    build_fold_indirect_ref_loc (input_location, lhsaddr));
    7098         1534 :       if (var_on_left)
    7099          914 :         x = fold_build2_loc (input_location, op, TREE_TYPE (rhs), x, rhs);
    7100              :       else
    7101          620 :         x = fold_build2_loc (input_location, op, TREE_TYPE (rhs), rhs, x);
    7102              :     }
    7103              : 
    7104         2110 :   if (TREE_CODE (TREE_TYPE (rhs)) == COMPLEX_TYPE
    7105         2110 :       && TREE_CODE (type) != COMPLEX_TYPE)
    7106            0 :     x = fold_build1_loc (input_location, REALPART_EXPR,
    7107            0 :                          TREE_TYPE (TREE_TYPE (rhs)), x);
    7108              : 
    7109         2110 :   gfc_add_block_to_block (&block, &lse.pre);
    7110         2110 :   gfc_add_block_to_block (&block, &rse.pre);
    7111              : 
    7112         2110 :   if (aop == OMP_ATOMIC_CAPTURE_NEW)
    7113              :     {
    7114          247 :       gfc_conv_expr (&vse, capture_expr1);
    7115          247 :       gfc_add_block_to_block (&block, &vse.pre);
    7116          247 :       gfc_add_block_to_block (&block, &lse.pre);
    7117              :     }
    7118              : 
    7119         2110 :   if (compare && else_branch)
    7120              :     {
    7121           64 :       tree var2 = create_tmp_var_raw (boolean_type_node);
    7122           64 :       DECL_CONTEXT (var2) = current_function_decl;
    7123           64 :       comp_tgt = build4 (TARGET_EXPR, boolean_type_node, var2,
    7124              :                          boolean_false_node, NULL, NULL);
    7125           64 :       compare = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (var2),
    7126              :                                  var2, compare);
    7127           64 :       TREE_OPERAND (compare, 0) = comp_tgt;
    7128           64 :       compare = omit_one_operand_loc (input_location, boolean_type_node,
    7129              :                                       compare, comp_tgt);
    7130              :     }
    7131              : 
    7132         2110 :   if (compare)
    7133          145 :     x = build3_loc (input_location, COND_EXPR, type, compare,
    7134              :                     convert (type, x), lse.expr);
    7135              : 
    7136         2110 :   if (aop == OMP_ATOMIC)
    7137              :     {
    7138         1545 :       x = build2_v (OMP_ATOMIC, lhsaddr, convert (type, x));
    7139         1545 :       OMP_ATOMIC_MEMORY_ORDER (x) = mo;
    7140         1545 :       OMP_ATOMIC_WEAK (x) = atomic_code->ext.omp_clauses->weak;
    7141         1545 :       gfc_add_expr_to_block (&block, x);
    7142              :     }
    7143              :   else
    7144              :     {
    7145          565 :       x = build2 (aop, type, lhsaddr, convert (type, x));
    7146          565 :       OMP_ATOMIC_MEMORY_ORDER (x) = mo;
    7147          565 :       OMP_ATOMIC_WEAK (x) = atomic_code->ext.omp_clauses->weak;
    7148          565 :       if (compare && else_branch)
    7149              :         {
    7150           64 :           tree vtmp = create_tmp_var_raw (TREE_TYPE (x));
    7151           64 :           DECL_CONTEXT (vtmp) = current_function_decl;
    7152           64 :           x = fold_build2_loc (input_location, MODIFY_EXPR,
    7153           64 :                                TREE_TYPE (vtmp), vtmp, x);
    7154           64 :           vtmp = build4 (TARGET_EXPR, TREE_TYPE (vtmp), vtmp,
    7155           64 :                          build_zero_cst (TREE_TYPE (vtmp)), NULL, NULL);
    7156           64 :           TREE_OPERAND (x, 0) = vtmp;
    7157           64 :           tree x2 = convert (TREE_TYPE (vse.expr), vtmp);
    7158           64 :           x2 = fold_build2_loc (input_location, MODIFY_EXPR,
    7159           64 :                                TREE_TYPE (vse.expr), vse.expr, x2);
    7160           64 :           x2 = build3_loc (input_location, COND_EXPR, void_type_node, comp_tgt,
    7161              :                            void_node, x2);
    7162           64 :           x = omit_one_operand_loc (input_location, TREE_TYPE (x2), x2, x);
    7163           64 :           gfc_add_expr_to_block (&block, x);
    7164              :         }
    7165              :       else
    7166              :         {
    7167          501 :           x = convert (TREE_TYPE (vse.expr), x);
    7168          501 :           gfc_add_modify (&block, vse.expr, x);
    7169              :         }
    7170              :     }
    7171              : 
    7172         2110 :   return gfc_finish_block (&block);
    7173              : }
    7174              : 
    7175              : static tree
    7176          604 : gfc_trans_omp_barrier (void)
    7177              : {
    7178          604 :   tree decl = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
    7179          604 :   return build_call_expr_loc (input_location, decl, 1,
    7180              :                               build_int_cst (integer_type_node,
    7181          604 :                                              GOMP_BARRIER_EXPLICIT));
    7182              : }
    7183              : 
    7184              : static tree
    7185          315 : gfc_trans_omp_cancel (gfc_code *code)
    7186              : {
    7187          315 :   int mask = 0;
    7188          315 :   tree ifc = boolean_true_node;
    7189          315 :   stmtblock_t block;
    7190          315 :   switch (code->ext.omp_clauses->cancel)
    7191              :     {
    7192              :     case OMP_CANCEL_PARALLEL: mask = 1; break;
    7193              :     case OMP_CANCEL_DO: mask = 2; break;
    7194              :     case OMP_CANCEL_SECTIONS: mask = 4; break;
    7195              :     case OMP_CANCEL_TASKGROUP: mask = 8; break;
    7196            0 :     default: gcc_unreachable ();
    7197              :     }
    7198          315 :   gfc_start_block (&block);
    7199          315 :   if (code->ext.omp_clauses->if_expr
    7200          224 :       || code->ext.omp_clauses->if_exprs[OMP_IF_CANCEL])
    7201              :     {
    7202           99 :       gfc_se se;
    7203           99 :       tree if_var;
    7204              : 
    7205           99 :       gcc_assert ((code->ext.omp_clauses->if_expr == NULL)
    7206              :                   ^ (code->ext.omp_clauses->if_exprs[OMP_IF_CANCEL] == NULL));
    7207           99 :       gfc_init_se (&se, NULL);
    7208           99 :       gfc_conv_expr (&se, code->ext.omp_clauses->if_expr != NULL
    7209              :                           ? code->ext.omp_clauses->if_expr
    7210              :                           : code->ext.omp_clauses->if_exprs[OMP_IF_CANCEL]);
    7211           99 :       gfc_add_block_to_block (&block, &se.pre);
    7212           99 :       if_var = gfc_evaluate_now (se.expr, &block);
    7213           99 :       gfc_add_block_to_block (&block, &se.post);
    7214           99 :       tree type = TREE_TYPE (if_var);
    7215           99 :       ifc = fold_build2_loc (input_location, NE_EXPR,
    7216              :                              boolean_type_node, if_var,
    7217              :                              build_zero_cst (type));
    7218              :     }
    7219          315 :   tree decl = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
    7220          315 :   tree c_bool_type = TREE_TYPE (TREE_TYPE (decl));
    7221          315 :   ifc = fold_convert (c_bool_type, ifc);
    7222          315 :   gfc_add_expr_to_block (&block,
    7223              :                          build_call_expr_loc (input_location, decl, 2,
    7224              :                                               build_int_cst (integer_type_node,
    7225          315 :                                                              mask), ifc));
    7226          315 :   return gfc_finish_block (&block);
    7227              : }
    7228              : 
    7229              : static tree
    7230          175 : gfc_trans_omp_cancellation_point (gfc_code *code)
    7231              : {
    7232          175 :   int mask = 0;
    7233          175 :   switch (code->ext.omp_clauses->cancel)
    7234              :     {
    7235              :     case OMP_CANCEL_PARALLEL: mask = 1; break;
    7236              :     case OMP_CANCEL_DO: mask = 2; break;
    7237              :     case OMP_CANCEL_SECTIONS: mask = 4; break;
    7238              :     case OMP_CANCEL_TASKGROUP: mask = 8; break;
    7239            0 :     default: gcc_unreachable ();
    7240              :     }
    7241          175 :   tree decl = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
    7242          175 :   return build_call_expr_loc (input_location, decl, 1,
    7243          350 :                               build_int_cst (integer_type_node, mask));
    7244              : }
    7245              : 
    7246              : static tree
    7247          149 : gfc_trans_omp_critical (gfc_code *code)
    7248              : {
    7249          149 :   stmtblock_t block;
    7250          149 :   tree stmt, name = NULL_TREE;
    7251          149 :   if (code->ext.omp_clauses->critical_name != NULL)
    7252           40 :     name = get_identifier (code->ext.omp_clauses->critical_name);
    7253          149 :   gfc_start_block (&block);
    7254          149 :   stmt = make_node (OMP_CRITICAL);
    7255          149 :   SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
    7256          149 :   TREE_TYPE (stmt) = void_type_node;
    7257          149 :   OMP_CRITICAL_BODY (stmt) = gfc_trans_code (code->block->next);
    7258          149 :   OMP_CRITICAL_NAME (stmt) = name;
    7259          149 :   OMP_CRITICAL_CLAUSES (stmt) = gfc_trans_omp_clauses (&block,
    7260              :                                                        code->ext.omp_clauses,
    7261              :                                                        code->loc);
    7262          149 :   gfc_add_expr_to_block (&block, stmt);
    7263          149 :   return gfc_finish_block (&block);
    7264              : }
    7265              : 
    7266              : typedef struct dovar_init_d {
    7267              :   gfc_symbol *sym;
    7268              :   tree var;
    7269              :   tree init;
    7270              :   bool non_unit_iter;
    7271              : } dovar_init;
    7272              : 
    7273              : static bool
    7274         2884 : gfc_nonrect_loop_expr (stmtblock_t *pblock, gfc_se *sep, int loop_n,
    7275              :                        gfc_code *code, gfc_expr *expr, vec<dovar_init> *inits,
    7276              :                        int simple, gfc_expr *curr_loop_var)
    7277              : {
    7278         2884 :   int i;
    7279         4771 :   for (i = 0; i < loop_n; i++)
    7280              :     {
    7281         2441 :       gcc_assert (code->ext.iterator->var->expr_type == EXPR_VARIABLE);
    7282         2441 :       if (gfc_find_sym_in_expr (code->ext.iterator->var->symtree->n.sym, expr))
    7283              :         break;
    7284         1887 :       code = code->block->next;
    7285              :     }
    7286         2884 :   if (i >= loop_n)
    7287              :     return false;
    7288              : 
    7289              :   /* Canonical format: TREE_VEC with [var, multiplier, offset].  */
    7290          554 :   gfc_symbol *var = code->ext.iterator->var->symtree->n.sym;
    7291              : 
    7292          554 :   tree tree_var = NULL_TREE;
    7293          554 :   tree a1 = integer_one_node;
    7294          554 :   tree a2 = integer_zero_node;
    7295              : 
    7296          554 :   if (!simple)
    7297              :     {
    7298              :       /* FIXME: Handle non-const iter steps, cf. PR fortran/110735.  */
    7299            6 :       sorry_at (gfc_get_location (&curr_loop_var->where),
    7300              :                 "non-rectangular loop nest with non-constant step for %qs",
    7301            3 :                 curr_loop_var->symtree->n.sym->name);
    7302            3 :       return false;
    7303              :     }
    7304              : 
    7305              :   dovar_init *di;
    7306              :   unsigned ix;
    7307          551 :   FOR_EACH_VEC_ELT (*inits, ix, di)
    7308           18 :     if (di->sym == var)
    7309              :       {
    7310           18 :         if (!di->non_unit_iter)
    7311              :           {
    7312           16 :             tree_var = di->init;
    7313           16 :             gcc_assert (DECL_P (tree_var));
    7314              :             break;
    7315              :           }
    7316              :         else
    7317              :           {
    7318              :             /* FIXME: Handle non-const iter steps, cf. PR fortran/110735.  */
    7319            2 :             sorry_at (gfc_get_location (&code->loc),
    7320              :                       "non-rectangular loop nest with non-constant step "
    7321              :                       "for %qs", var->name);
    7322            2 :             inform (gfc_get_location (&expr->where), "Used here");
    7323            2 :             return false;
    7324              :           }
    7325              :       }
    7326          533 :   if (tree_var == NULL_TREE)
    7327          533 :     tree_var = var->backend_decl;
    7328              : 
    7329          549 :   if (expr->expr_type == EXPR_VARIABLE)
    7330           54 :     gcc_assert (expr->symtree->n.sym == var);
    7331          495 :   else if (expr->expr_type != EXPR_OP
    7332          495 :            || (expr->value.op.op != INTRINSIC_TIMES
    7333          479 :                && expr->value.op.op != INTRINSIC_PLUS
    7334          359 :                && expr->value.op.op != INTRINSIC_MINUS))
    7335            0 :     gcc_unreachable ();
    7336              :   else
    7337              :     {
    7338          495 :       gfc_se se;
    7339          495 :       gfc_expr *et = NULL, *eo = NULL, *e = expr;
    7340          495 :       if (expr->value.op.op != INTRINSIC_TIMES)
    7341              :         {
    7342          479 :           if (gfc_find_sym_in_expr (var, expr->value.op.op1))
    7343              :             {
    7344          431 :               e = expr->value.op.op1;
    7345          431 :               eo = expr->value.op.op2;
    7346              :             }
    7347              :           else
    7348              :             {
    7349           48 :               eo = expr->value.op.op1;
    7350           48 :               e = expr->value.op.op2;
    7351              :             }
    7352              :         }
    7353          495 :       if (e->value.op.op == INTRINSIC_TIMES)
    7354              :         {
    7355           91 :           if (e->value.op.op1->expr_type == EXPR_VARIABLE
    7356           91 :               && e->value.op.op1->symtree->n.sym == var)
    7357           51 :             et = e->value.op.op2;
    7358              :           else
    7359              :             {
    7360           40 :               et = e->value.op.op1;
    7361           40 :               gcc_assert (e->value.op.op2->expr_type == EXPR_VARIABLE
    7362              :                           && e->value.op.op2->symtree->n.sym == var);
    7363              :             }
    7364              :         }
    7365              :       else
    7366          404 :         gcc_assert (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == var);
    7367           91 :       if (et != NULL)
    7368              :         {
    7369           91 :           gfc_init_se (&se, NULL);
    7370           91 :           gfc_conv_expr_val (&se, et);
    7371           91 :           gfc_add_block_to_block (pblock, &se.pre);
    7372           91 :           a1 = se.expr;
    7373              :         }
    7374          495 :       if (eo != NULL)
    7375              :         {
    7376          479 :           gfc_init_se (&se, NULL);
    7377          479 :           gfc_conv_expr_val (&se, eo);
    7378          479 :           gfc_add_block_to_block (pblock, &se.pre);
    7379          479 :           a2 = se.expr;
    7380          479 :           if (expr->value.op.op == INTRINSIC_MINUS && expr->value.op.op2 == eo)
    7381              :             /* outer-var - a2.  */
    7382          335 :             a2 = fold_build1 (NEGATE_EXPR, TREE_TYPE (a2), a2);
    7383          144 :           else if (expr->value.op.op == INTRINSIC_MINUS)
    7384              :             /* a2 - outer-var.  */
    7385           24 :             a1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (a1), a1);
    7386              :         }
    7387          495 :       a1 = DECL_P (a1) ? a1 : gfc_evaluate_now (a1, pblock);
    7388          495 :       a2 = DECL_P (a2) ? a2 : gfc_evaluate_now (a2, pblock);
    7389              :     }
    7390              : 
    7391          549 :   gfc_init_se (sep, NULL);
    7392          549 :   sep->expr = make_tree_vec (3);
    7393          549 :   TREE_VEC_ELT (sep->expr, 0) = tree_var;
    7394          549 :   TREE_VEC_ELT (sep->expr, 1) = fold_convert (TREE_TYPE (tree_var), a1);
    7395          549 :   TREE_VEC_ELT (sep->expr, 2) = fold_convert (TREE_TYPE (tree_var), a2);
    7396              : 
    7397          549 :   return true;
    7398              : }
    7399              : 
    7400              : int
    7401          715 : gfc_expr_list_len (gfc_expr_list *list)
    7402              : {
    7403          715 :   unsigned len = 0;
    7404         2106 :   for (; list; list = list->next)
    7405         1391 :     len++;
    7406              : 
    7407          715 :   return len;
    7408              : }
    7409              : 
    7410              : static tree
    7411         9762 : gfc_trans_omp_do (gfc_code *code, gfc_exec_op op, stmtblock_t *pblock,
    7412              :                   gfc_omp_clauses *do_clauses, tree par_clauses)
    7413              : {
    7414         9762 :   gfc_se se;
    7415         9762 :   tree dovar, stmt, from, to, step, type, init, cond, incr, orig_decls;
    7416         9762 :   tree local_dovar = NULL_TREE, cycle_label, tmp, omp_clauses;
    7417         9762 :   stmtblock_t block;
    7418         9762 :   stmtblock_t body;
    7419         9762 :   gfc_omp_clauses *clauses = code->ext.omp_clauses;
    7420         9762 :   int i, collapse = clauses->collapse;
    7421         9762 :   vec<dovar_init> inits = vNULL;
    7422         9762 :   dovar_init *di;
    7423         9762 :   unsigned ix;
    7424         9762 :   vec<tree, va_heap, vl_embed> *saved_doacross_steps = doacross_steps;
    7425        19524 :   gfc_expr_list *oacc_tile
    7426         9762 :     = do_clauses ? do_clauses->tile_list : clauses->tile_list;
    7427         9762 :   gfc_expr_list *sizes
    7428              :     = do_clauses ? do_clauses->sizes_list : clauses->sizes_list;
    7429         9762 :   gfc_code *orig_code = code;
    7430              : 
    7431              :   /* Both collapsed and tiled loops are lowered the same way.  In
    7432              :      OpenACC, those clauses are not compatible, so prioritize the tile
    7433              :      clause, if present.  */
    7434         9762 :   if (oacc_tile)
    7435           60 :     collapse = gfc_expr_list_len (oacc_tile);
    7436         9702 :   else if (sizes)
    7437          122 :     collapse = gfc_expr_list_len (sizes);
    7438              : 
    7439         9762 :   doacross_steps = NULL;
    7440         9762 :   if (clauses->orderedc)
    7441          134 :     collapse = clauses->orderedc;
    7442         9762 :   if (collapse <= 0)
    7443              :     collapse = 1;
    7444              : 
    7445         9762 :   code = code->block->next;
    7446              : 
    7447         9762 :   init = make_tree_vec (collapse);
    7448         9762 :   cond = make_tree_vec (collapse);
    7449         9762 :   incr = make_tree_vec (collapse);
    7450         9762 :   orig_decls = clauses->ordered ? make_tree_vec (collapse) : NULL_TREE;
    7451              : 
    7452         9762 :   if (pblock == NULL)
    7453              :     {
    7454         6142 :       gfc_start_block (&block);
    7455         6142 :       pblock = &block;
    7456              :     }
    7457              : 
    7458              :   /* simd schedule modifier is only useful for composite do simd and other
    7459              :      constructs including that, where gfc_trans_omp_do is only called
    7460              :      on the simd construct and DO's clauses are translated elsewhere.  */
    7461         9762 :   do_clauses->sched_simd = false;
    7462              : 
    7463         9762 :   omp_clauses = gfc_trans_omp_clauses (pblock, do_clauses, code->loc);
    7464              : 
    7465        31903 :   for (i = 0; i < collapse; i++)
    7466              :     {
    7467        12379 :       int simple = 0;
    7468        12379 :       int dovar_found = 0;
    7469        12379 :       tree dovar_decl;
    7470              : 
    7471        12379 :       if (code->op == EXEC_OMP_TILE || code->op == EXEC_OMP_UNROLL)
    7472              :         {
    7473          321 :           TREE_VEC_ELT (init, i) = NULL_TREE;
    7474          321 :           TREE_VEC_ELT (cond, i) = NULL_TREE;
    7475          321 :           TREE_VEC_ELT (incr, i) = NULL_TREE;
    7476          321 :           TREE_VEC_ELT (incr, i) = NULL_TREE;
    7477          321 :           if (orig_decls)
    7478            2 :             TREE_VEC_ELT (orig_decls, i) = NULL_TREE;
    7479          321 :           continue;
    7480              :         }
    7481        12058 :       gcc_assert (code->op == EXEC_DO);
    7482        12058 :       if (clauses)
    7483              :         {
    7484        12058 :           gfc_omp_namelist *n = NULL;
    7485        12058 :           if (op == EXEC_OMP_SIMD && collapse == 1)
    7486         1015 :             for (n = clauses->lists[OMP_LIST_LINEAR];
    7487         1315 :                  n != NULL; n = n->next)
    7488          443 :               if (code->ext.iterator->var->symtree->n.sym == n->sym)
    7489              :                 {
    7490              :                   dovar_found = 3;
    7491              :                   break;
    7492              :                 }
    7493        12058 :           if (n == NULL && op != EXEC_OMP_DISTRIBUTE)
    7494        11816 :             for (n = clauses->lists[OMP_LIST_LASTPRIVATE];
    7495        13912 :                  n != NULL; n = n->next)
    7496         3477 :               if (code->ext.iterator->var->symtree->n.sym == n->sym)
    7497              :                 {
    7498              :                   dovar_found = 2;
    7499              :                   break;
    7500              :                 }
    7501        12058 :           if (n == NULL)
    7502        11797 :             for (n = clauses->lists[OMP_LIST_PRIVATE]; n != NULL; n = n->next)
    7503         7006 :               if (code->ext.iterator->var->symtree->n.sym == n->sym)
    7504              :                 {
    7505              :                   dovar_found = 1;
    7506              :                   break;
    7507              :                 }
    7508              :         }
    7509              : 
    7510              :       /* Evaluate all the expressions in the iterator.  */
    7511        12058 :       gfc_init_se (&se, NULL);
    7512        12058 :       gfc_conv_expr_lhs (&se, code->ext.iterator->var);
    7513        12058 :       gfc_add_block_to_block (pblock, &se.pre);
    7514        12058 :       local_dovar = dovar_decl = dovar = se.expr;
    7515        12058 :       type = TREE_TYPE (dovar);
    7516        12058 :       gcc_assert (TREE_CODE (type) == INTEGER_TYPE);
    7517              : 
    7518        12058 :       gfc_init_se (&se, NULL);
    7519        12058 :       gfc_conv_expr_val (&se, code->ext.iterator->step);
    7520        12058 :       gfc_add_block_to_block (pblock, &se.pre);
    7521        12058 :       step = gfc_evaluate_now (se.expr, pblock);
    7522              : 
    7523        12058 :       if (TREE_CODE (step) == INTEGER_CST)
    7524        11397 :         simple = tree_int_cst_sgn (step);
    7525              : 
    7526        12058 :       gfc_init_se (&se, NULL);
    7527        12058 :       if (!clauses->non_rectangular
    7528        13500 :           || !gfc_nonrect_loop_expr (pblock, &se, i, orig_code->block->next,
    7529              :                                      code->ext.iterator->start, &inits, simple,
    7530         1442 :                                      code->ext.iterator->var))
    7531              :         {
    7532        11742 :           gfc_conv_expr_val (&se, code->ext.iterator->start);
    7533        11742 :           gfc_add_block_to_block (pblock, &se.pre);
    7534        11742 :           if (!DECL_P (se.expr))
    7535        11296 :             se.expr = gfc_evaluate_now (se.expr, pblock);
    7536              :         }
    7537        12058 :       from = se.expr;
    7538              : 
    7539        12058 :       gfc_init_se (&se, NULL);
    7540        12058 :       if (!clauses->non_rectangular
    7541        13500 :           || !gfc_nonrect_loop_expr (pblock, &se, i, orig_code->block->next,
    7542              :                                      code->ext.iterator->end, &inits, simple,
    7543         1442 :                                      code->ext.iterator->var))
    7544              :         {
    7545        11825 :           gfc_conv_expr_val (&se, code->ext.iterator->end);
    7546        11825 :           gfc_add_block_to_block (pblock, &se.pre);
    7547        11825 :           if (!DECL_P (se.expr))
    7548        10517 :             se.expr = gfc_evaluate_now (se.expr, pblock);
    7549              :         }
    7550        12058 :       to = se.expr;
    7551              : 
    7552        12058 :       if (!DECL_P (dovar))
    7553          131 :         dovar_decl
    7554          131 :           = gfc_trans_omp_variable (code->ext.iterator->var->symtree->n.sym,
    7555              :                                     false);
    7556        12058 :       if (simple && !DECL_P (dovar))
    7557              :         {
    7558           85 :           const char *name = code->ext.iterator->var->symtree->n.sym->name;
    7559           85 :           local_dovar = gfc_create_var (type, name);
    7560           85 :           dovar_init e = {code->ext.iterator->var->symtree->n.sym,
    7561           85 :                           dovar, local_dovar, false};
    7562           85 :           inits.safe_push (e);
    7563              :         }
    7564              :       /* Loop body.  */
    7565        11397 :       if (simple)
    7566              :         {
    7567        11397 :           TREE_VEC_ELT (init, i) = build2_v (MODIFY_EXPR, local_dovar, from);
    7568              :           /* The condition should not be folded.  */
    7569        11951 :           TREE_VEC_ELT (cond, i) = build2_loc (input_location, simple > 0
    7570              :                                                ? LE_EXPR : GE_EXPR,
    7571              :                                                logical_type_node, local_dovar,
    7572              :                                                to);
    7573        11397 :           TREE_VEC_ELT (incr, i) = fold_build2_loc (input_location, PLUS_EXPR,
    7574              :                                                     type, local_dovar, step);
    7575        11397 :           TREE_VEC_ELT (incr, i) = fold_build2_loc (input_location,
    7576              :                                                     MODIFY_EXPR,
    7577              :                                                     type, local_dovar,
    7578        11397 :                                                     TREE_VEC_ELT (incr, i));
    7579        11397 :           if (orig_decls && !clauses->orderedc)
    7580              :             orig_decls = NULL;
    7581          383 :           else if (orig_decls)
    7582          383 :             TREE_VEC_ELT (orig_decls, i) = dovar_decl;
    7583              :         }
    7584              :       else
    7585              :         {
    7586              :           /* STEP is not 1 or -1.  Use:
    7587              :              for (count = 0; count < (to + step - from) / step; count++)
    7588              :                {
    7589              :                  dovar = from + count * step;
    7590              :                  body;
    7591              :                cycle_label:;
    7592              :                }  */
    7593          661 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, type, step, from);
    7594          661 :           tmp = fold_build2_loc (input_location, PLUS_EXPR, type, to, tmp);
    7595          661 :           tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, type, tmp,
    7596              :                                  step);
    7597          661 :           tmp = gfc_evaluate_now (tmp, pblock);
    7598          661 :           local_dovar = gfc_create_var (type, "count");
    7599          661 :           TREE_VEC_ELT (init, i) = build2_v (MODIFY_EXPR, local_dovar,
    7600              :                                              build_int_cst (type, 0));
    7601              :           /* The condition should not be folded.  */
    7602          661 :           TREE_VEC_ELT (cond, i) = build2_loc (input_location, LT_EXPR,
    7603              :                                                logical_type_node,
    7604              :                                                local_dovar, tmp);
    7605          661 :           TREE_VEC_ELT (incr, i) = fold_build2_loc (input_location, PLUS_EXPR,
    7606              :                                                     type, local_dovar,
    7607              :                                                     build_int_cst (type, 1));
    7608          661 :           TREE_VEC_ELT (incr, i) = fold_build2_loc (input_location,
    7609              :                                                     MODIFY_EXPR, type,
    7610              :                                                     local_dovar,
    7611          661 :                                                     TREE_VEC_ELT (incr, i));
    7612              : 
    7613              :           /* Initialize DOVAR.  */
    7614          661 :           tmp = fold_build2_loc (input_location, MULT_EXPR, type, local_dovar,
    7615              :                                  step);
    7616          661 :           tmp = fold_build2_loc (input_location, PLUS_EXPR, type, from, tmp);
    7617          661 :           dovar_init e = {code->ext.iterator->var->symtree->n.sym,
    7618          661 :                           dovar, tmp, true};
    7619          661 :           inits.safe_push (e);
    7620          661 :           if (clauses->orderedc)
    7621              :             {
    7622          192 :               if (doacross_steps == NULL)
    7623           47 :                 vec_safe_grow_cleared (doacross_steps, clauses->orderedc, true);
    7624          192 :               (*doacross_steps)[i] = step;
    7625              :             }
    7626          661 :           if (orig_decls)
    7627          198 :             TREE_VEC_ELT (orig_decls, i) = dovar_decl;
    7628              :         }
    7629              : 
    7630        12058 :       if (dovar_found == 3
    7631        12058 :           && op == EXEC_OMP_SIMD
    7632          143 :           && collapse == 1
    7633          143 :           && local_dovar != dovar)
    7634              :         {
    7635          120 :           for (tmp = omp_clauses; tmp; tmp = OMP_CLAUSE_CHAIN (tmp))
    7636          120 :             if (OMP_CLAUSE_CODE (tmp) == OMP_CLAUSE_LINEAR
    7637          120 :                 && OMP_CLAUSE_DECL (tmp) == dovar)
    7638              :               {
    7639           30 :                 OMP_CLAUSE_LINEAR_NO_COPYIN (tmp) = 1;
    7640           30 :                 break;
    7641              :               }
    7642              :         }
    7643        12058 :       if (!dovar_found && op == EXEC_OMP_SIMD)
    7644              :         {
    7645         1663 :           if (collapse == 1)
    7646              :             {
    7647          862 :               tmp = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
    7648          862 :               OMP_CLAUSE_LINEAR_STEP (tmp) = step;
    7649          862 :               OMP_CLAUSE_LINEAR_NO_COPYIN (tmp) = 1;
    7650          862 :               OMP_CLAUSE_DECL (tmp) = dovar_decl;
    7651          862 :               omp_clauses = gfc_trans_add_clause (tmp, omp_clauses);
    7652          862 :               if (local_dovar != dovar)
    7653              :                 dovar_found = 3;
    7654              :             }
    7655          801 :           else if (local_dovar != dovar
    7656          114 :                    && !code->ext.iterator->var->symtree->n.sym->attr.pointer)
    7657              :             {
    7658              :               /* The body sets DOVAR from the counter, so it is no longer the
    7659              :                  predetermined lastprivate iteration variable.  */
    7660          102 :               tmp = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
    7661          102 :               OMP_CLAUSE_DECL (tmp) = dovar_decl;
    7662          102 :               omp_clauses = gfc_trans_add_clause (tmp, omp_clauses);
    7663          102 :               dovar_found = 2;
    7664              :             }
    7665              :         }
    7666        10395 :       else if (!dovar_found && local_dovar != dovar)
    7667              :         {
    7668          268 :           tmp = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
    7669          268 :           OMP_CLAUSE_DECL (tmp) = dovar_decl;
    7670          268 :           omp_clauses = gfc_trans_add_clause (tmp, omp_clauses);
    7671              :         }
    7672        12026 :       if (dovar_found > 1)
    7673              :         {
    7674         1658 :           tree c = NULL;
    7675              : 
    7676         1658 :           tmp = NULL;
    7677         1658 :           if (local_dovar != dovar)
    7678              :             {
    7679              :               /* If dovar is lastprivate, but different counter is used,
    7680              :                  dovar += step needs to be added to
    7681              :                  OMP_CLAUSE_LASTPRIVATE_STMT, otherwise the copied dovar
    7682              :                  will have the value on entry of the last loop, rather
    7683              :                  than value after iterator increment.  */
    7684          347 :               if (clauses->orderedc)
    7685              :                 {
    7686           60 :                   if (clauses->collapse <= 1 || i >= clauses->collapse)
    7687              :                     tmp = local_dovar;
    7688              :                   else
    7689           36 :                     tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7690              :                                            type, local_dovar,
    7691              :                                            build_one_cst (type));
    7692           60 :                   tmp = fold_build2_loc (input_location, MULT_EXPR, type,
    7693              :                                          tmp, step);
    7694           60 :                   tmp = fold_build2_loc (input_location, PLUS_EXPR, type,
    7695              :                                          from, tmp);
    7696              :                 }
    7697              :               else
    7698          287 :                 tmp = fold_build2_loc (input_location, PLUS_EXPR, type,
    7699              :                                        dovar, step);
    7700          347 :               tmp = fold_build2_loc (input_location, MODIFY_EXPR, type,
    7701              :                                      dovar, tmp);
    7702         1142 :               for (c = omp_clauses; c ; c = OMP_CLAUSE_CHAIN (c))
    7703          717 :                 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7704          717 :                     && OMP_CLAUSE_DECL (c) == dovar_decl)
    7705              :                   {
    7706          207 :                     OMP_CLAUSE_LASTPRIVATE_STMT (c) = tmp;
    7707          207 :                     break;
    7708              :                   }
    7709          510 :                 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
    7710          510 :                          && OMP_CLAUSE_DECL (c) == dovar_decl)
    7711              :                   {
    7712           62 :                     OMP_CLAUSE_LINEAR_STMT (c) = tmp;
    7713           62 :                     break;
    7714              :                   }
    7715              :             }
    7716         1658 :           if (c == NULL && op == EXEC_OMP_DO && par_clauses != NULL)
    7717              :             {
    7718          892 :               for (c = par_clauses; c ; c = OMP_CLAUSE_CHAIN (c))
    7719          892 :                 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7720          892 :                     && OMP_CLAUSE_DECL (c) == dovar_decl)
    7721              :                   {
    7722          406 :                     tree l = build_omp_clause (input_location,
    7723              :                                                OMP_CLAUSE_LASTPRIVATE);
    7724          406 :                     if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
    7725            4 :                       OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (l) = 1;
    7726          406 :                     OMP_CLAUSE_DECL (l) = dovar_decl;
    7727          406 :                     OMP_CLAUSE_CHAIN (l) = omp_clauses;
    7728          406 :                     OMP_CLAUSE_LASTPRIVATE_STMT (l) = tmp;
    7729          406 :                     omp_clauses = l;
    7730          406 :                     OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_SHARED);
    7731          406 :                     break;
    7732              :                   }
    7733              :             }
    7734         1658 :           gcc_assert (local_dovar == dovar || c != NULL);
    7735              :         }
    7736        12058 :       if (local_dovar != dovar)
    7737              :         {
    7738          746 :           if (op != EXEC_OMP_SIMD || dovar_found == 1)
    7739          559 :             tmp = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
    7740          187 :           else if (collapse == 1)
    7741              :             {
    7742           62 :               tmp = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
    7743           62 :               OMP_CLAUSE_LINEAR_STEP (tmp) = build_int_cst (type, 1);
    7744           62 :               OMP_CLAUSE_LINEAR_NO_COPYIN (tmp) = 1;
    7745           62 :               OMP_CLAUSE_LINEAR_NO_COPYOUT (tmp) = 1;
    7746              :             }
    7747              :           else
    7748          125 :             tmp = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
    7749          746 :           OMP_CLAUSE_DECL (tmp) = local_dovar;
    7750          746 :           omp_clauses = gfc_trans_add_clause (tmp, omp_clauses);
    7751              :         }
    7752              : 
    7753        12058 :       if (i + 1 < collapse)
    7754         2589 :         code = code->block->next;
    7755              :     }
    7756              : 
    7757         9762 :   if (pblock != &block)
    7758              :     {
    7759         3620 :       pushlevel ();
    7760         3620 :       gfc_start_block (&block);
    7761              :     }
    7762              : 
    7763         9762 :   gfc_start_block (&body);
    7764              : 
    7765        20270 :   FOR_EACH_VEC_ELT (inits, ix, di)
    7766          746 :     gfc_add_modify (&body, di->var, di->init);
    7767         9762 :   inits.release ();
    7768              : 
    7769              :   /* Cycle statement is implemented with a goto.  Exit statement must not be
    7770              :      present for this loop.  */
    7771         9762 :   cycle_label = gfc_build_label_decl (NULL_TREE);
    7772              : 
    7773              :   /* Put these labels where they can be found later.  */
    7774              : 
    7775         9762 :   code->cycle_label = cycle_label;
    7776         9762 :   code->exit_label = NULL_TREE;
    7777              : 
    7778              :   /* Main loop body.  */
    7779         9762 :   if (clauses->lists[OMP_LIST_REDUCTION_INSCAN])
    7780              :     {
    7781           16 :       gfc_code *code1, *scan, *code2, *tmpcode;
    7782           16 :       code1 = tmpcode = code->block->next;
    7783           16 :       if (tmpcode && tmpcode->op != EXEC_OMP_SCAN)
    7784           18 :         while (tmpcode && tmpcode->next && tmpcode->next->op != EXEC_OMP_SCAN)
    7785              :           tmpcode = tmpcode->next;
    7786           16 :       scan = tmpcode->op == EXEC_OMP_SCAN ? tmpcode : tmpcode->next;
    7787           16 :       if (code1 != scan)
    7788           16 :         tmpcode->next = NULL;
    7789           16 :       code2 = scan->next;
    7790           16 :       gcc_assert (scan->op == EXEC_OMP_SCAN);
    7791           16 :       location_t loc = gfc_get_location (&scan->loc);
    7792              : 
    7793           16 :       tmp = code1 != scan ? gfc_trans_code (code1) : build_empty_stmt (loc);
    7794           16 :       tmp = build2 (OMP_SCAN, void_type_node, tmp, NULL_TREE);
    7795           16 :       SET_EXPR_LOCATION (tmp, loc);
    7796           16 :       gfc_add_expr_to_block (&body, tmp);
    7797           16 :       input_location = loc;
    7798           16 :       tree c = gfc_trans_omp_clauses (&body, scan->ext.omp_clauses, scan->loc);
    7799           16 :       tmp = code2 ? gfc_trans_code (code2) : build_empty_stmt (loc);
    7800           16 :       tmp = build2 (OMP_SCAN, void_type_node, tmp, c);
    7801           16 :       SET_EXPR_LOCATION (tmp, loc);
    7802           16 :       if (code1 != scan)
    7803           16 :         tmpcode->next = scan;
    7804              :     }
    7805         9746 :   else if (code->op == EXEC_OMP_TILE || code->op == EXEC_OMP_UNROLL)
    7806          293 :     tmp = gfc_trans_omp_code (code, true);
    7807              :   else
    7808         9453 :     tmp = gfc_trans_omp_code (code->block->next, true);
    7809         9762 :   gfc_add_expr_to_block (&body, tmp);
    7810              : 
    7811              :   /* Label for cycle statements (if needed).  */
    7812         9762 :   if (TREE_USED (cycle_label))
    7813              :     {
    7814         9762 :       tmp = build1_v (LABEL_EXPR, cycle_label);
    7815         9762 :       gfc_add_expr_to_block (&body, tmp);
    7816              :     }
    7817              : 
    7818              :   /* End of loop body.  */
    7819         9762 :   switch (op)
    7820              :     {
    7821         1641 :     case EXEC_OMP_SIMD: stmt = make_node (OMP_SIMD); break;
    7822         2442 :     case EXEC_OMP_DO: stmt = make_node (OMP_FOR); break;
    7823           85 :     case EXEC_OMP_DISTRIBUTE: stmt = make_node (OMP_DISTRIBUTE); break;
    7824          119 :     case EXEC_OMP_LOOP: stmt = make_node (OMP_LOOP); break;
    7825           95 :     case EXEC_OMP_TASKLOOP: stmt = make_node (OMP_TASKLOOP); break;
    7826         4933 :     case EXEC_OACC_LOOP: stmt = make_node (OACC_LOOP); break;
    7827          122 :     case EXEC_OMP_TILE: stmt = make_node (OMP_TILE); break;
    7828          325 :     case EXEC_OMP_UNROLL: stmt = make_node (OMP_UNROLL); break;
    7829            0 :     default: gcc_unreachable ();
    7830              :     }
    7831              : 
    7832         9762 :   SET_EXPR_LOCATION (stmt, gfc_get_location (&orig_code->loc));
    7833         9762 :   TREE_TYPE (stmt) = void_type_node;
    7834         9762 :   OMP_FOR_BODY (stmt) = gfc_finish_block (&body);
    7835         9762 :   OMP_FOR_CLAUSES (stmt) = omp_clauses;
    7836         9762 :   OMP_FOR_INIT (stmt) = init;
    7837         9762 :   OMP_FOR_COND (stmt) = cond;
    7838         9762 :   OMP_FOR_INCR (stmt) = incr;
    7839         9762 :   if (orig_decls)
    7840          140 :     OMP_FOR_ORIG_DECLS (stmt) = orig_decls;
    7841         9762 :   OMP_FOR_NON_RECTANGULAR (stmt) = clauses->non_rectangular;
    7842         9762 :   gfc_add_expr_to_block (&block, stmt);
    7843              : 
    7844         9762 :   vec_free (doacross_steps);
    7845         9762 :   doacross_steps = saved_doacross_steps;
    7846              : 
    7847         9762 :   return gfc_finish_block (&block);
    7848              : }
    7849              : 
    7850              : /* Translate combined OpenACC 'parallel loop', 'kernels loop', 'serial loop'
    7851              :    construct. */
    7852              : 
    7853              : static tree
    7854         1556 : gfc_trans_oacc_combined_directive (gfc_code *code)
    7855              : {
    7856         1556 :   stmtblock_t block, *pblock = NULL;
    7857         1556 :   gfc_omp_clauses construct_clauses, loop_clauses;
    7858         1556 :   tree stmt, oacc_clauses = NULL_TREE;
    7859         1556 :   enum tree_code construct_code;
    7860         1556 :   location_t loc = input_location;
    7861              : 
    7862         1556 :   switch (code->op)
    7863              :     {
    7864              :       case EXEC_OACC_PARALLEL_LOOP:
    7865              :         construct_code = OACC_PARALLEL;
    7866              :         break;
    7867              :       case EXEC_OACC_KERNELS_LOOP:
    7868              :         construct_code = OACC_KERNELS;
    7869              :         break;
    7870              :       case EXEC_OACC_SERIAL_LOOP:
    7871              :         construct_code = OACC_SERIAL;
    7872              :         break;
    7873            0 :       default:
    7874            0 :         gcc_unreachable ();
    7875              :     }
    7876              : 
    7877         1556 :   gfc_start_block (&block);
    7878              : 
    7879         1556 :   memset (&loop_clauses, 0, sizeof (loop_clauses));
    7880         1556 :   if (code->ext.omp_clauses != NULL)
    7881              :     {
    7882         1556 :       memcpy (&construct_clauses, code->ext.omp_clauses,
    7883              :               sizeof (construct_clauses));
    7884         1556 :       loop_clauses.collapse = construct_clauses.collapse;
    7885         1556 :       loop_clauses.gang = construct_clauses.gang;
    7886         1556 :       loop_clauses.gang_static = construct_clauses.gang_static;
    7887         1556 :       loop_clauses.gang_num_expr = construct_clauses.gang_num_expr;
    7888         1556 :       loop_clauses.gang_static_expr = construct_clauses.gang_static_expr;
    7889         1556 :       loop_clauses.vector = construct_clauses.vector;
    7890         1556 :       loop_clauses.vector_expr = construct_clauses.vector_expr;
    7891         1556 :       loop_clauses.worker = construct_clauses.worker;
    7892         1556 :       loop_clauses.worker_expr = construct_clauses.worker_expr;
    7893         1556 :       loop_clauses.seq = construct_clauses.seq;
    7894         1556 :       loop_clauses.par_auto = construct_clauses.par_auto;
    7895         1556 :       loop_clauses.independent = construct_clauses.independent;
    7896         1556 :       loop_clauses.tile_list = construct_clauses.tile_list;
    7897         1556 :       loop_clauses.lists[OMP_LIST_PRIVATE]
    7898         1556 :         = construct_clauses.lists[OMP_LIST_PRIVATE];
    7899         1556 :       loop_clauses.lists[OMP_LIST_REDUCTION]
    7900         1556 :         = construct_clauses.lists[OMP_LIST_REDUCTION];
    7901         1556 :       construct_clauses.gang = false;
    7902         1556 :       construct_clauses.gang_static = false;
    7903         1556 :       construct_clauses.gang_num_expr = NULL;
    7904         1556 :       construct_clauses.gang_static_expr = NULL;
    7905         1556 :       construct_clauses.vector = false;
    7906         1556 :       construct_clauses.vector_expr = NULL;
    7907         1556 :       construct_clauses.worker = false;
    7908         1556 :       construct_clauses.worker_expr = NULL;
    7909         1556 :       construct_clauses.seq = false;
    7910         1556 :       construct_clauses.par_auto = false;
    7911         1556 :       construct_clauses.independent = false;
    7912         1556 :       construct_clauses.independent = false;
    7913         1556 :       construct_clauses.tile_list = NULL;
    7914         1556 :       construct_clauses.lists[OMP_LIST_PRIVATE] = NULL;
    7915         1556 :       if (construct_code == OACC_KERNELS)
    7916           87 :         construct_clauses.lists[OMP_LIST_REDUCTION] = NULL;
    7917         1556 :       oacc_clauses = gfc_trans_omp_clauses (&block, &construct_clauses,
    7918              :                                             code->loc, false, true);
    7919              :     }
    7920         1556 :   if (!loop_clauses.seq)
    7921              :     pblock = &block;
    7922              :   else
    7923           54 :     pushlevel ();
    7924         1556 :   stmt = gfc_trans_omp_do (code, EXEC_OACC_LOOP, pblock, &loop_clauses, NULL);
    7925         1556 :   protected_set_expr_location (stmt, loc);
    7926         1556 :   if (TREE_CODE (stmt) != BIND_EXPR)
    7927         1556 :     stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    7928              :   else
    7929            0 :     poplevel (0, 0);
    7930         1556 :   stmt = build2_loc (loc, construct_code, void_type_node, stmt, oacc_clauses);
    7931         1556 :   gfc_add_expr_to_block (&block, stmt);
    7932         1556 :   return gfc_finish_block (&block);
    7933              : }
    7934              : 
    7935              : static tree
    7936          111 : gfc_trans_omp_depobj (gfc_code *code)
    7937              : {
    7938          111 :   stmtblock_t block;
    7939          111 :   gfc_se se;
    7940          111 :   gfc_init_se (&se, NULL);
    7941          111 :   gfc_init_block (&block);
    7942          111 :   gfc_conv_expr (&se, code->ext.omp_clauses->depobj);
    7943          111 :   gcc_assert (se.pre.head == NULL && se.post.head == NULL);
    7944          111 :   tree depobj = se.expr;
    7945          111 :   location_t loc = EXPR_LOCATION (depobj);
    7946          111 :   if (!POINTER_TYPE_P (TREE_TYPE (depobj)))
    7947          111 :     depobj = gfc_build_addr_expr (NULL, depobj);
    7948          111 :   depobj = fold_convert (build_pointer_type_for_mode (ptr_type_node,
    7949              :                                                       TYPE_MODE (ptr_type_node),
    7950              :                                                       true), depobj);
    7951          111 :   gfc_omp_namelist *n = code->ext.omp_clauses->lists[OMP_LIST_DEPEND];
    7952          111 :   if (n)
    7953              :     {
    7954           84 :       tree var;
    7955           84 :       if (!n->sym)  /* omp_all_memory.  */
    7956            3 :         var = null_pointer_node;
    7957           81 :       else if (n->expr && n->expr->ref->u.ar.type != AR_FULL)
    7958              :         {
    7959           18 :           gfc_init_se (&se, NULL);
    7960           18 :           if (n->expr->rank == 0)
    7961              :             {
    7962           18 :               gfc_conv_expr_reference (&se, n->expr);
    7963           18 :               var = se.expr;
    7964              :             }
    7965              :           else
    7966              :             {
    7967            0 :               gfc_conv_expr_descriptor (&se, n->expr);
    7968            0 :               var = gfc_conv_array_data (se.expr);
    7969              :             }
    7970           18 :           gfc_add_block_to_block (&block, &se.pre);
    7971           18 :           gfc_add_block_to_block (&block, &se.post);
    7972           18 :           gcc_assert (POINTER_TYPE_P (TREE_TYPE (var)));
    7973              :         }
    7974              :       else
    7975              :         {
    7976           63 :           var = gfc_get_symbol_decl (n->sym);
    7977          101 :           if (POINTER_TYPE_P (TREE_TYPE (var))
    7978           73 :               && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (var))))
    7979            8 :             var = build_fold_indirect_ref (var);
    7980           63 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (var)))
    7981              :             {
    7982           12 :               var = gfc_conv_descriptor_data_get (var);
    7983           12 :               gcc_assert (POINTER_TYPE_P (TREE_TYPE (var)));
    7984              :             }
    7985           51 :           else if ((n->sym->attr.allocatable || n->sym->attr.pointer)
    7986           13 :                    && n->sym->attr.dummy)
    7987            8 :             var = build_fold_indirect_ref (var);
    7988           69 :           else if (!POINTER_TYPE_P (TREE_TYPE (var))
    7989           45 :                    || (n->sym->ts.f90_type == BT_VOID
    7990           12 :                        && !POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (var)))
    7991            8 :                        && !GFC_ARRAY_TYPE_P (TREE_TYPE (TREE_TYPE (var)))))
    7992              :             {
    7993           30 :               TREE_ADDRESSABLE (var) = 1;
    7994           30 :               var = gfc_build_addr_expr (NULL, var);
    7995              :             }
    7996              :         }
    7997           84 :       depobj = save_expr (depobj);
    7998           84 :       tree r = build_fold_indirect_ref_loc (loc, depobj);
    7999           84 :       gfc_add_expr_to_block (&block,
    8000              :                              build2 (MODIFY_EXPR, void_type_node, r, var));
    8001              :     }
    8002              : 
    8003              :   /* Only one may be set. */
    8004          111 :   gcc_assert (((int)(n != NULL) + (int)(code->ext.omp_clauses->destroy)
    8005              :               + (int)(code->ext.omp_clauses->depobj_update != OMP_DEPEND_UNSET))
    8006              :               == 1);
    8007          111 :   int k = -1; /* omp_clauses->destroy */
    8008          111 :   if (!code->ext.omp_clauses->destroy)
    8009           93 :     switch (code->ext.omp_clauses->depobj_update != OMP_DEPEND_UNSET
    8010           93 :             ? code->ext.omp_clauses->depobj_update : n->u.depend_doacross_op)
    8011              :       {
    8012              :       case OMP_DEPEND_IN: k = GOMP_DEPEND_IN; break;
    8013              :       case OMP_DEPEND_OUT: k = GOMP_DEPEND_OUT; break;
    8014              :       case OMP_DEPEND_INOUT: k = GOMP_DEPEND_INOUT; break;
    8015              :       case OMP_DEPEND_INOUTSET: k = GOMP_DEPEND_INOUTSET; break;
    8016              :       case OMP_DEPEND_MUTEXINOUTSET: k = GOMP_DEPEND_MUTEXINOUTSET; break;
    8017            0 :       default: gcc_unreachable ();
    8018              :       }
    8019          111 :   tree t = build_int_cst (ptr_type_node, k);
    8020          111 :   depobj = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (depobj), depobj,
    8021          111 :                        TYPE_SIZE_UNIT (ptr_type_node));
    8022          111 :   depobj = build_fold_indirect_ref_loc (loc, depobj);
    8023          111 :   gfc_add_expr_to_block (&block, build2 (MODIFY_EXPR, void_type_node, depobj, t));
    8024              : 
    8025          111 :   return gfc_finish_block (&block);
    8026              : }
    8027              : 
    8028              : /* Callback for walk_tree to find an OMP dispatch call and wrap it into an
    8029              :  * IFN_GOMP_DISPATCH.  */
    8030              : 
    8031              : static tree
    8032         2066 : replace_omp_dispatch_call (tree *tp, int *, void *decls_p)
    8033              : {
    8034         2066 :   tree t = *tp;
    8035         2066 :   tree decls = (tree) decls_p;
    8036         2066 :   tree orig_fn_decl = TREE_PURPOSE (decls);
    8037         2066 :   tree dup_fn_decl = TREE_VALUE (decls);
    8038         2066 :   if (TREE_CODE (t) == CALL_EXPR)
    8039              :     {
    8040          141 :       if (CALL_EXPR_FN (t) == dup_fn_decl)
    8041            1 :         CALL_EXPR_FN (t) = orig_fn_decl;
    8042          140 :       else if (TREE_CODE (CALL_EXPR_FN (t)) == ADDR_EXPR
    8043          140 :                && TREE_OPERAND (CALL_EXPR_FN (t), 0) == dup_fn_decl)
    8044          127 :         TREE_OPERAND (CALL_EXPR_FN (t), 0) = dup_fn_decl;
    8045              :       else
    8046              :         return NULL_TREE;
    8047          128 :       *tp = build_call_expr_internal_loc (input_location, IFN_GOMP_DISPATCH,
    8048          128 :                                           TREE_TYPE (t), 1, t);
    8049          128 :       return *tp;
    8050              :     }
    8051              : 
    8052              :   return NULL_TREE;
    8053              : }
    8054              : 
    8055              : static tree
    8056          128 : gfc_trans_omp_dispatch (gfc_code *code)
    8057              : {
    8058          128 :   stmtblock_t block;
    8059          128 :   gfc_code *next = code->block->next;
    8060              :   // assume ill-formed "function dispatch structured
    8061              :   // block" have already been rejected by resolve_omp_dispatch
    8062          128 :   gcc_assert (next->op == EXEC_CALL || next->op == EXEC_ASSIGN);
    8063              : 
    8064              :   // Make duplicate decl for dispatch function call to make it easy to spot
    8065              :   // after translation
    8066          128 :   gfc_symbol *orig_fn_sym;
    8067          128 :   gfc_expr *call_expr = next->op == EXEC_CALL ? next->expr1 : next->expr2;
    8068          128 :   if (call_expr != NULL) // function
    8069              :     {
    8070           71 :       if (call_expr->value.function.isym != NULL) // dig into convert intrinsics
    8071            4 :         call_expr = call_expr->value.function.actual->expr;
    8072           71 :       gcc_assert (call_expr->expr_type == EXPR_FUNCTION);
    8073           71 :       orig_fn_sym = call_expr->value.function.esym
    8074           71 :                       ? call_expr->value.function.esym
    8075            0 :                       : call_expr->symtree->n.sym;
    8076              :     }
    8077              :   else // subroutine
    8078              :     {
    8079           57 :       orig_fn_sym = next->resolved_sym;
    8080              :     }
    8081          128 :   if (!orig_fn_sym->backend_decl)
    8082           25 :     gfc_get_symbol_decl (orig_fn_sym);
    8083          128 :   gfc_symbol dup_fn_sym = *orig_fn_sym;
    8084          128 :   dup_fn_sym.backend_decl = copy_node (orig_fn_sym->backend_decl);
    8085          128 :   if (call_expr != NULL)
    8086           71 :     call_expr->value.function.esym = &dup_fn_sym;
    8087              :   else
    8088           57 :     next->resolved_sym = &dup_fn_sym;
    8089              : 
    8090          128 :   tree body = gfc_trans_code (next);
    8091              : 
    8092              :   // Walk the tree to find the duplicate decl, wrap IFN call and replace
    8093              :   // dup decl with original
    8094          128 :   tree fn_decls
    8095          128 :     = build_tree_list (orig_fn_sym->backend_decl, dup_fn_sym.backend_decl);
    8096          128 :   tree dispatch_call
    8097          128 :     = walk_tree (&body, replace_omp_dispatch_call, fn_decls, NULL);
    8098          128 :   gcc_assert (dispatch_call != NULL_TREE);
    8099              : 
    8100          128 :   gfc_start_block (&block);
    8101          128 :   tree omp_clauses
    8102          128 :     = gfc_trans_omp_clauses (&block, code->ext.omp_clauses, code->loc);
    8103              : 
    8104              :   // Extract depend clauses and create taskwait
    8105          128 :   tree depend_clauses = NULL_TREE;
    8106          128 :   tree *depend_clauses_ptr = &depend_clauses;
    8107          333 :   for (tree c = omp_clauses; c; c = OMP_CLAUSE_CHAIN (c))
    8108              :     {
    8109          205 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
    8110              :         {
    8111            8 :           *depend_clauses_ptr = c;
    8112            8 :           depend_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
    8113              :         }
    8114              :     }
    8115          128 :   if (depend_clauses != NULL_TREE)
    8116              :     {
    8117            4 :       tree stmt = make_node (OMP_TASK);
    8118            4 :       TREE_TYPE (stmt) = void_node;
    8119            4 :       OMP_TASK_CLAUSES (stmt) = depend_clauses;
    8120            4 :       OMP_TASK_BODY (stmt) = NULL_TREE;
    8121            4 :       SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
    8122            4 :       gfc_add_expr_to_block (&block, stmt);
    8123              :     }
    8124              : 
    8125          128 :   tree stmt = make_node (OMP_DISPATCH);
    8126          128 :   SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
    8127          128 :   TREE_TYPE (stmt) = void_type_node;
    8128          128 :   OMP_DISPATCH_BODY (stmt) = body;
    8129          128 :   OMP_DISPATCH_CLAUSES (stmt) = omp_clauses;
    8130              : 
    8131          128 :   gfc_add_expr_to_block (&block, stmt);
    8132          128 :   return gfc_finish_block (&block);
    8133              : }
    8134              : 
    8135              : static tree
    8136           31 : gfc_trans_omp_error (gfc_code *code)
    8137              : {
    8138           31 :   stmtblock_t block;
    8139           31 :   gfc_se se;
    8140           31 :   tree len, message;
    8141           31 :   bool fatal = code->ext.omp_clauses->severity == OMP_SEVERITY_FATAL;
    8142           44 :   tree fndecl = builtin_decl_explicit (fatal ? BUILT_IN_GOMP_ERROR
    8143              :                                              : BUILT_IN_GOMP_WARNING);
    8144           31 :   gfc_start_block (&block);
    8145           31 :   gfc_init_se (&se, NULL );
    8146           31 :   if (!code->ext.omp_clauses->message)
    8147              :     {
    8148            3 :       message = null_pointer_node;
    8149            3 :       len = build_int_cst (size_type_node, 0);
    8150              :     }
    8151              :   else
    8152              :     {
    8153           28 :       gfc_conv_expr (&se, code->ext.omp_clauses->message);
    8154           28 :       message = se.expr;
    8155           28 :       if (!POINTER_TYPE_P (TREE_TYPE (message)))
    8156              :         /* To ensure an ARRAY_TYPE is not passed as such.  */
    8157           19 :         message = gfc_build_addr_expr (NULL, message);
    8158           28 :       len = se.string_length;
    8159              :     }
    8160           31 :   gfc_add_block_to_block (&block, &se.pre);
    8161           31 :   gfc_add_expr_to_block (&block, build_call_expr_loc (input_location, fndecl,
    8162              :                                                       2, message, len));
    8163           31 :   gfc_add_block_to_block (&block, &se.post);
    8164           31 :   return gfc_finish_block (&block);
    8165              : }
    8166              : 
    8167              : static tree
    8168           79 : gfc_trans_omp_flush (gfc_code *code)
    8169              : {
    8170           79 :   tree call;
    8171           79 :   if (!code->ext.omp_clauses
    8172           12 :       || code->ext.omp_clauses->memorder == OMP_MEMORDER_UNSET
    8173           12 :       || code->ext.omp_clauses->memorder == OMP_MEMORDER_SEQ_CST)
    8174              :     {
    8175           70 :       call = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
    8176           70 :       call = build_call_expr_loc (input_location, call, 0);
    8177              :     }
    8178              :   else
    8179              :     {
    8180            9 :       enum memmodel mo = MEMMODEL_LAST;
    8181            9 :       switch (code->ext.omp_clauses->memorder)
    8182              :         {
    8183              :         case OMP_MEMORDER_ACQ_REL: mo = MEMMODEL_ACQ_REL; break;
    8184              :         case OMP_MEMORDER_RELEASE: mo = MEMMODEL_RELEASE; break;
    8185              :         case OMP_MEMORDER_ACQUIRE: mo = MEMMODEL_ACQUIRE; break;
    8186            0 :         default: gcc_unreachable (); break;
    8187              :         }
    8188            9 :       call = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
    8189            9 :       call = build_call_expr_loc (input_location, call, 1,
    8190            9 :                                   build_int_cst (integer_type_node, mo));
    8191              :     }
    8192           79 :   return call;
    8193              : }
    8194              : 
    8195              : static tree
    8196          116 : gfc_trans_omp_master (gfc_code *code)
    8197              : {
    8198          116 :   tree stmt = gfc_trans_code (code->block->next);
    8199          116 :   if (IS_EMPTY_STMT (stmt))
    8200              :     return stmt;
    8201          110 :   return build1_v (OMP_MASTER, stmt);
    8202              : }
    8203              : 
    8204              : static tree
    8205           55 : gfc_trans_omp_masked (gfc_code *code, gfc_omp_clauses *clauses)
    8206              : {
    8207           55 :   stmtblock_t block;
    8208           55 :   tree body = gfc_trans_code (code->block->next);
    8209           55 :   if (IS_EMPTY_STMT (body))
    8210              :     return body;
    8211           46 :   if (!clauses)
    8212           39 :     clauses = code->ext.omp_clauses;
    8213           46 :   gfc_start_block (&block);
    8214           46 :   tree omp_clauses = gfc_trans_omp_clauses (&block, clauses, code->loc);
    8215           46 :   tree stmt = make_node (OMP_MASKED);
    8216           46 :   SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
    8217           46 :   TREE_TYPE (stmt) = void_type_node;
    8218           46 :   OMP_MASKED_BODY (stmt) = body;
    8219           46 :   OMP_MASKED_CLAUSES (stmt) = omp_clauses;
    8220           46 :   gfc_add_expr_to_block (&block, stmt);
    8221           46 :   return gfc_finish_block (&block);
    8222              : }
    8223              : 
    8224              : 
    8225              : static tree
    8226          525 : gfc_trans_omp_ordered (gfc_code *code)
    8227              : {
    8228          525 :   if (!flag_openmp)
    8229              :     {
    8230            5 :       if (!code->ext.omp_clauses->simd)
    8231            3 :         return gfc_trans_code (code->block ? code->block->next : NULL);
    8232            2 :       code->ext.omp_clauses->threads = 0;
    8233              :     }
    8234          522 :   tree omp_clauses = gfc_trans_omp_clauses (NULL, code->ext.omp_clauses,
    8235              :                                             code->loc);
    8236          522 :   return build2_loc (input_location, OMP_ORDERED, void_type_node,
    8237          522 :                      code->block ? gfc_trans_code (code->block->next)
    8238          522 :                      : NULL_TREE, omp_clauses);
    8239              : }
    8240              : 
    8241              : static tree
    8242         1944 : gfc_trans_omp_parallel (gfc_code *code)
    8243              : {
    8244         1944 :   stmtblock_t block;
    8245         1944 :   tree stmt, omp_clauses;
    8246              : 
    8247         1944 :   gfc_start_block (&block);
    8248         1944 :   omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    8249              :                                        code->loc);
    8250         1944 :   pushlevel ();
    8251         1944 :   stmt = gfc_trans_omp_code (code->block->next, true);
    8252         1944 :   stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    8253         1944 :   stmt = build2_loc (input_location, OMP_PARALLEL, void_type_node, stmt,
    8254              :                      omp_clauses);
    8255         1944 :   gfc_add_expr_to_block (&block, stmt);
    8256         1944 :   return gfc_finish_block (&block);
    8257              : }
    8258              : 
    8259              : enum
    8260              : {
    8261              :   GFC_OMP_SPLIT_SIMD,
    8262              :   GFC_OMP_SPLIT_DO,
    8263              :   GFC_OMP_SPLIT_PARALLEL,
    8264              :   GFC_OMP_SPLIT_DISTRIBUTE,
    8265              :   GFC_OMP_SPLIT_TEAMS,
    8266              :   GFC_OMP_SPLIT_TARGET,
    8267              :   GFC_OMP_SPLIT_TASKLOOP,
    8268              :   GFC_OMP_SPLIT_MASKED,
    8269              :   GFC_OMP_SPLIT_NUM
    8270              : };
    8271              : 
    8272              : enum
    8273              : {
    8274              :   GFC_OMP_MASK_SIMD = (1 << GFC_OMP_SPLIT_SIMD),
    8275              :   GFC_OMP_MASK_DO = (1 << GFC_OMP_SPLIT_DO),
    8276              :   GFC_OMP_MASK_PARALLEL = (1 << GFC_OMP_SPLIT_PARALLEL),
    8277              :   GFC_OMP_MASK_DISTRIBUTE = (1 << GFC_OMP_SPLIT_DISTRIBUTE),
    8278              :   GFC_OMP_MASK_TEAMS = (1 << GFC_OMP_SPLIT_TEAMS),
    8279              :   GFC_OMP_MASK_TARGET = (1 << GFC_OMP_SPLIT_TARGET),
    8280              :   GFC_OMP_MASK_TASKLOOP = (1 << GFC_OMP_SPLIT_TASKLOOP),
    8281              :   GFC_OMP_MASK_MASKED = (1 << GFC_OMP_SPLIT_MASKED)
    8282              : };
    8283              : 
    8284              : /* If a var is in lastprivate/firstprivate/reduction but not in a
    8285              :    data mapping/sharing clause, add it to 'map(tofrom:' if is_target
    8286              :    and to 'shared' otherwise.  */
    8287              : static void
    8288         2738 : gfc_add_clause_implicitly (gfc_omp_clauses *clauses_out,
    8289              :                            gfc_omp_clauses *clauses_in,
    8290              :                            bool is_target, bool is_parallel_do)
    8291              : {
    8292         2738 :   int clauselist_to_add = is_target ? OMP_LIST_MAP : OMP_LIST_SHARED;
    8293         2738 :   gfc_omp_namelist *tail = NULL;
    8294        16428 :   for (int i = 0; i < 5; ++i)
    8295              :     {
    8296        13690 :       gfc_omp_namelist *n;
    8297        13690 :       switch (i)
    8298              :         {
    8299         2738 :         case 0: n = clauses_in->lists[OMP_LIST_FIRSTPRIVATE]; break;
    8300         2738 :         case 1: n = clauses_in->lists[OMP_LIST_LASTPRIVATE]; break;
    8301         2738 :         case 2: n = clauses_in->lists[OMP_LIST_REDUCTION]; break;
    8302         2738 :         case 3: n = clauses_in->lists[OMP_LIST_REDUCTION_INSCAN]; break;
    8303         2738 :         case 4: n = clauses_in->lists[OMP_LIST_REDUCTION_TASK]; break;
    8304              :         default: gcc_unreachable ();
    8305              :         }
    8306        17039 :       for (; n != NULL; n = n->next)
    8307              :         {
    8308              :           gfc_omp_namelist *n2, **n_firstp = NULL, **n_lastp = NULL;
    8309        20726 :           for (int j = 0; j < 6; ++j)
    8310              :             {
    8311        18251 :               gfc_omp_namelist **n2ref = NULL, *prev2 = NULL;
    8312        18251 :               switch (j)
    8313              :                 {
    8314         3349 :                 case 0:
    8315         3349 :                   n2ref = &clauses_out->lists[clauselist_to_add];
    8316         3349 :                   break;
    8317         3312 :                 case 1:
    8318         3312 :                   n2ref = &clauses_out->lists[OMP_LIST_FIRSTPRIVATE];
    8319         3312 :                   break;
    8320         3312 :                 case 2:
    8321         3312 :                   if (is_target)
    8322          258 :                     n2ref = &clauses_in->lists[OMP_LIST_LASTPRIVATE];
    8323              :                   else
    8324         3054 :                     n2ref = &clauses_out->lists[OMP_LIST_LASTPRIVATE];
    8325              :                   break;
    8326         3312 :                 case 3: n2ref = &clauses_out->lists[OMP_LIST_REDUCTION]; break;
    8327         2483 :                 case 4:
    8328         2483 :                   n2ref = &clauses_out->lists[OMP_LIST_REDUCTION_INSCAN];
    8329         2483 :                   break;
    8330         2483 :                 case 5:
    8331         2483 :                   n2ref = &clauses_out->lists[OMP_LIST_REDUCTION_TASK];
    8332         2483 :                   break;
    8333              :                 default: gcc_unreachable ();
    8334              :                 }
    8335        28525 :               for (n2 = *n2ref; n2 != NULL; prev2 = n2, n2 = n2->next)
    8336        13547 :                 if (n2->sym == n->sym)
    8337              :                   break;
    8338        18251 :               if (n2)
    8339              :                 {
    8340         3273 :                   if (j == 0 /* clauselist_to_add */)
    8341              :                     break;  /* Already present.  */
    8342         3236 :                   if (j == 1 /* OMP_LIST_FIRSTPRIVATE */)
    8343              :                     {
    8344         1132 :                       n_firstp = prev2 ? &prev2->next : n2ref;
    8345         1132 :                       continue;
    8346              :                     }
    8347         2104 :                   if (j == 2 /* OMP_LIST_LASTPRIVATE */)
    8348              :                     {
    8349         1267 :                       n_lastp = prev2 ? &prev2->next : n2ref;
    8350         1267 :                       continue;
    8351              :                     }
    8352              :                   break;
    8353              :                 }
    8354              :             }
    8355         3349 :           if (n_firstp && n_lastp)
    8356              :             {
    8357              :               /* For parallel do, GCC puts firstprivate/lastprivate
    8358              :                  on the parallel.  */
    8359          283 :               if (is_parallel_do)
    8360          280 :                 continue;
    8361            3 :               *n_firstp = (*n_firstp)->next;
    8362            3 :               if (!is_target)
    8363            0 :                 *n_lastp = (*n_lastp)->next;
    8364              :             }
    8365         3066 :           else if (is_target && n_lastp)
    8366              :             ;
    8367         3011 :           else if (n2 || n_firstp || n_lastp)
    8368         2652 :             continue;
    8369          417 :           if (clauses_out->lists[clauselist_to_add]
    8370          305 :               && (clauses_out->lists[clauselist_to_add]
    8371          305 :                   == clauses_in->lists[clauselist_to_add]))
    8372              :             {
    8373              :               gfc_omp_namelist *p = NULL;
    8374          421 :               for (n2 = clauses_in->lists[clauselist_to_add]; n2; n2 = n2->next)
    8375              :                 {
    8376          273 :                   if (p)
    8377              :                     {
    8378          125 :                       p->next = gfc_get_omp_namelist ();
    8379          125 :                       p = p->next;
    8380              :                     }
    8381              :                   else
    8382              :                     {
    8383          148 :                       p = gfc_get_omp_namelist ();
    8384          148 :                       clauses_out->lists[clauselist_to_add] = p;
    8385              :                     }
    8386          273 :                   *p = *n2;
    8387              :                 }
    8388              :             }
    8389          417 :           if (!tail)
    8390              :             {
    8391          288 :               tail = clauses_out->lists[clauselist_to_add];
    8392          413 :               for (; tail && tail->next; tail = tail->next)
    8393              :                 ;
    8394              :             }
    8395          417 :           n2 = gfc_get_omp_namelist ();
    8396          417 :           n2->where = n->where;
    8397          417 :           n2->sym = n->sym;
    8398          417 :           if (is_target)
    8399          120 :             n2->u.map.op = OMP_MAP_TOFROM;
    8400          417 :           if (tail)
    8401              :             {
    8402          305 :               tail->next = n2;
    8403          305 :               tail = n2;
    8404              :             }
    8405              :           else
    8406          112 :             clauses_out->lists[clauselist_to_add] = n2;
    8407              :         }
    8408              :     }
    8409         2738 : }
    8410              : 
    8411              : /* Kind of opposite to above, add firstprivate to CLAUSES_OUT if it is mapped
    8412              :    in CLAUSES_IN's FIRSTPRIVATE list but not its MAP list.  */
    8413              : 
    8414              : static void
    8415          387 : gfc_add_firstprivate_if_unmapped (gfc_omp_clauses *clauses_out,
    8416              :                                   gfc_omp_clauses *clauses_in)
    8417              : {
    8418          387 :   gfc_omp_namelist *n = clauses_in->lists[OMP_LIST_FIRSTPRIVATE];
    8419          387 :   gfc_omp_namelist **tail = NULL;
    8420              : 
    8421          539 :   for (; n != NULL; n = n->next)
    8422              :     {
    8423          152 :       gfc_omp_namelist *n2 = clauses_out->lists[OMP_LIST_MAP];
    8424          194 :       for (; n2 != NULL; n2 = n2->next)
    8425           53 :         if (n->sym == n2->sym)
    8426              :           break;
    8427          152 :       if (n2 == NULL)
    8428              :         {
    8429          141 :           gfc_omp_namelist *dup = gfc_get_omp_namelist ();
    8430          141 :           *dup = *n;
    8431          141 :           dup->next = NULL;
    8432          141 :           if (!tail)
    8433              :             {
    8434           78 :               tail = &clauses_out->lists[OMP_LIST_FIRSTPRIVATE];
    8435           78 :               while (*tail && (*tail)->next)
    8436            0 :                 tail = &(*tail)->next;
    8437              :             }
    8438          141 :           *tail = dup;
    8439          141 :           tail = &(*tail)->next;
    8440              :         }
    8441              :     }
    8442          387 : }
    8443              : 
    8444              : static void
    8445         4672 : gfc_free_split_omp_clauses (gfc_code *code, gfc_omp_clauses *clausesa)
    8446              : {
    8447        42048 :   for (int i = 0; i < GFC_OMP_SPLIT_NUM; ++i)
    8448      1495040 :     for (int j = 0; j < OMP_LIST_NUM; ++j)
    8449      1457664 :       if (clausesa[i].lists[j] && clausesa[i].lists[j] != code->ext.omp_clauses->lists[j])
    8450         1403 :         for (gfc_omp_namelist *n = clausesa[i].lists[j]; n;)
    8451              :           {
    8452          959 :             gfc_omp_namelist *p = n;
    8453          959 :             n = n->next;
    8454          959 :             free (p);
    8455              :           }
    8456         4672 : }
    8457              : 
    8458              : static void
    8459         4672 : gfc_split_omp_clauses (gfc_code *code,
    8460              :                        gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM])
    8461              : {
    8462         4672 :   int mask = 0, innermost = 0;
    8463         4672 :   bool is_loop = false;
    8464         4672 :   memset (clausesa, 0, GFC_OMP_SPLIT_NUM * sizeof (gfc_omp_clauses));
    8465         4672 :   switch (code->op)
    8466              :     {
    8467              :     case EXEC_OMP_DISTRIBUTE:
    8468              :       innermost = GFC_OMP_SPLIT_DISTRIBUTE;
    8469              :       break;
    8470           39 :     case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
    8471           39 :       mask = GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
    8472           39 :       innermost = GFC_OMP_SPLIT_DO;
    8473           39 :       break;
    8474           28 :     case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
    8475           28 :       mask = GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_PARALLEL
    8476              :              | GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
    8477           28 :       innermost = GFC_OMP_SPLIT_SIMD;
    8478           28 :       break;
    8479           47 :     case EXEC_OMP_DISTRIBUTE_SIMD:
    8480           47 :       mask = GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_SIMD;
    8481           47 :       innermost = GFC_OMP_SPLIT_SIMD;
    8482           47 :       break;
    8483            0 :     case EXEC_OMP_DO:
    8484            0 :     case EXEC_OMP_LOOP:
    8485            0 :       innermost = GFC_OMP_SPLIT_DO;
    8486            0 :       break;
    8487          146 :     case EXEC_OMP_DO_SIMD:
    8488          146 :       mask = GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
    8489          146 :       innermost = GFC_OMP_SPLIT_SIMD;
    8490          146 :       break;
    8491            0 :     case EXEC_OMP_PARALLEL:
    8492            0 :       innermost = GFC_OMP_SPLIT_PARALLEL;
    8493            0 :       break;
    8494         1120 :     case EXEC_OMP_PARALLEL_DO:
    8495         1120 :     case EXEC_OMP_PARALLEL_LOOP:
    8496         1120 :       mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
    8497         1120 :       innermost = GFC_OMP_SPLIT_DO;
    8498         1120 :       break;
    8499          332 :     case EXEC_OMP_PARALLEL_DO_SIMD:
    8500          332 :       mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
    8501          332 :       innermost = GFC_OMP_SPLIT_SIMD;
    8502          332 :       break;
    8503           11 :     case EXEC_OMP_PARALLEL_MASKED:
    8504           11 :       mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_MASKED;
    8505           11 :       innermost = GFC_OMP_SPLIT_MASKED;
    8506           11 :       break;
    8507           14 :     case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
    8508           14 :       mask = (GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_MASKED
    8509              :               | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD);
    8510           14 :       innermost = GFC_OMP_SPLIT_TASKLOOP;
    8511           14 :       break;
    8512           20 :     case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
    8513           20 :       mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD;
    8514           20 :       innermost = GFC_OMP_SPLIT_TASKLOOP;
    8515           20 :       break;
    8516           24 :     case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
    8517           24 :       mask = (GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_MASKED
    8518              :               | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD);
    8519           24 :       innermost = GFC_OMP_SPLIT_SIMD;
    8520           24 :       break;
    8521           28 :     case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
    8522           28 :       mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD;
    8523           28 :       innermost = GFC_OMP_SPLIT_SIMD;
    8524           28 :       break;
    8525            0 :     case EXEC_OMP_SIMD:
    8526            0 :       innermost = GFC_OMP_SPLIT_SIMD;
    8527            0 :       break;
    8528         2080 :     case EXEC_OMP_TARGET:
    8529         2080 :       innermost = GFC_OMP_SPLIT_TARGET;
    8530         2080 :       break;
    8531           26 :     case EXEC_OMP_TARGET_PARALLEL:
    8532           26 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_PARALLEL;
    8533           26 :       innermost = GFC_OMP_SPLIT_PARALLEL;
    8534           26 :       break;
    8535           82 :     case EXEC_OMP_TARGET_PARALLEL_DO:
    8536           82 :     case EXEC_OMP_TARGET_PARALLEL_LOOP:
    8537           82 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
    8538           82 :       innermost = GFC_OMP_SPLIT_DO;
    8539           82 :       break;
    8540           17 :     case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
    8541           17 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO
    8542              :              | GFC_OMP_MASK_SIMD;
    8543           17 :       innermost = GFC_OMP_SPLIT_SIMD;
    8544           17 :       break;
    8545           27 :     case EXEC_OMP_TARGET_SIMD:
    8546           27 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_SIMD;
    8547           27 :       innermost = GFC_OMP_SPLIT_SIMD;
    8548           27 :       break;
    8549           79 :     case EXEC_OMP_TARGET_TEAMS:
    8550           79 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS;
    8551           79 :       innermost = GFC_OMP_SPLIT_TEAMS;
    8552           79 :       break;
    8553           15 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
    8554           15 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS
    8555              :              | GFC_OMP_MASK_DISTRIBUTE;
    8556           15 :       innermost = GFC_OMP_SPLIT_DISTRIBUTE;
    8557           15 :       break;
    8558           62 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
    8559           62 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE
    8560              :              | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
    8561           62 :       innermost = GFC_OMP_SPLIT_DO;
    8562           62 :       break;
    8563           48 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    8564           48 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE
    8565              :              | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
    8566           48 :       innermost = GFC_OMP_SPLIT_SIMD;
    8567           48 :       break;
    8568           17 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
    8569           17 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS
    8570              :              | GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_SIMD;
    8571           17 :       innermost = GFC_OMP_SPLIT_SIMD;
    8572           17 :       break;
    8573           14 :     case EXEC_OMP_TARGET_TEAMS_LOOP:
    8574           14 :       mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DO;
    8575           14 :       innermost = GFC_OMP_SPLIT_DO;
    8576           14 :       break;
    8577            8 :     case EXEC_OMP_MASKED_TASKLOOP:
    8578            8 :       mask = GFC_OMP_MASK_MASKED | GFC_OMP_MASK_TASKLOOP;
    8579            8 :       innermost = GFC_OMP_SPLIT_TASKLOOP;
    8580            8 :       break;
    8581            0 :     case EXEC_OMP_MASTER_TASKLOOP:
    8582            0 :     case EXEC_OMP_TASKLOOP:
    8583            0 :       innermost = GFC_OMP_SPLIT_TASKLOOP;
    8584            0 :       break;
    8585           24 :     case EXEC_OMP_MASKED_TASKLOOP_SIMD:
    8586           24 :       mask = GFC_OMP_MASK_MASKED | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD;
    8587           24 :       innermost = GFC_OMP_SPLIT_SIMD;
    8588           24 :       break;
    8589           45 :     case EXEC_OMP_MASTER_TASKLOOP_SIMD:
    8590           45 :     case EXEC_OMP_TASKLOOP_SIMD:
    8591           45 :       mask = GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD;
    8592           45 :       innermost = GFC_OMP_SPLIT_SIMD;
    8593           45 :       break;
    8594          153 :     case EXEC_OMP_TEAMS:
    8595          153 :       innermost = GFC_OMP_SPLIT_TEAMS;
    8596          153 :       break;
    8597           18 :     case EXEC_OMP_TEAMS_DISTRIBUTE:
    8598           18 :       mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE;
    8599           18 :       innermost = GFC_OMP_SPLIT_DISTRIBUTE;
    8600           18 :       break;
    8601           37 :     case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
    8602           37 :       mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE
    8603              :              | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
    8604           37 :       innermost = GFC_OMP_SPLIT_DO;
    8605           37 :       break;
    8606           62 :     case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    8607           62 :       mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE
    8608              :              | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
    8609           62 :       innermost = GFC_OMP_SPLIT_SIMD;
    8610           62 :       break;
    8611           40 :     case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
    8612           40 :       mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_SIMD;
    8613           40 :       innermost = GFC_OMP_SPLIT_SIMD;
    8614           40 :       break;
    8615              :     case EXEC_OMP_TEAMS_LOOP:
    8616              :       mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DO;
    8617              :       innermost = GFC_OMP_SPLIT_DO;
    8618              :       break;
    8619            0 :     default:
    8620            0 :       gcc_unreachable ();
    8621              :     }
    8622         4663 :   if (mask == 0)
    8623              :     {
    8624         2233 :       clausesa[innermost] = *code->ext.omp_clauses;
    8625         2233 :       return;
    8626              :     }
    8627              :   /* Loops are similar to DO but still a bit different.  */
    8628         2439 :   switch (code->op)
    8629              :     {
    8630           59 :     case EXEC_OMP_LOOP:
    8631           59 :     case EXEC_OMP_PARALLEL_LOOP:
    8632           59 :     case EXEC_OMP_TEAMS_LOOP:
    8633           59 :     case EXEC_OMP_TARGET_PARALLEL_LOOP:
    8634           59 :     case EXEC_OMP_TARGET_TEAMS_LOOP:
    8635           59 :       is_loop = true;
    8636         2439 :     default:
    8637         2439 :       break;
    8638              :     }
    8639         2439 :   if (code->ext.omp_clauses != NULL)
    8640              :     {
    8641         2439 :       if (mask & GFC_OMP_MASK_TARGET)
    8642              :         {
    8643              :           /* First the clauses that are unique to some constructs.  */
    8644          387 :           clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_MAP]
    8645          387 :             = code->ext.omp_clauses->lists[OMP_LIST_MAP];
    8646          387 :           clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_IS_DEVICE_PTR]
    8647          387 :             = code->ext.omp_clauses->lists[OMP_LIST_IS_DEVICE_PTR];
    8648          387 :           clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_HAS_DEVICE_ADDR]
    8649          387 :             = code->ext.omp_clauses->lists[OMP_LIST_HAS_DEVICE_ADDR];
    8650          387 :           clausesa[GFC_OMP_SPLIT_TARGET].dyn_groupprivate
    8651          387 :             = code->ext.omp_clauses->dyn_groupprivate;
    8652          387 :           clausesa[GFC_OMP_SPLIT_TARGET].fallback
    8653          387 :             = code->ext.omp_clauses->fallback;
    8654          387 :           clausesa[GFC_OMP_SPLIT_TARGET].device
    8655          387 :             = code->ext.omp_clauses->device;
    8656          387 :           clausesa[GFC_OMP_SPLIT_TARGET].thread_limit_list
    8657          387 :             = code->ext.omp_clauses->thread_limit_list;
    8658          387 :           clausesa[GFC_OMP_SPLIT_TARGET].thread_limit_strict
    8659          387 :             = code->ext.omp_clauses->thread_limit_strict;
    8660          387 :           clausesa[GFC_OMP_SPLIT_TARGET].thread_limit_dims
    8661          387 :             = code->ext.omp_clauses->thread_limit_dims;
    8662          387 :           clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_USES_ALLOCATORS]
    8663          387 :             = code->ext.omp_clauses->lists[OMP_LIST_USES_ALLOCATORS];
    8664         2709 :           for (int i = 0; i < OMP_DEFAULTMAP_CAT_NUM; i++)
    8665         2322 :             clausesa[GFC_OMP_SPLIT_TARGET].defaultmap[i]
    8666         2322 :               = code->ext.omp_clauses->defaultmap[i];
    8667          387 :           clausesa[GFC_OMP_SPLIT_TARGET].if_exprs[OMP_IF_TARGET]
    8668          387 :             = code->ext.omp_clauses->if_exprs[OMP_IF_TARGET];
    8669              :           /* And this is copied to all.  */
    8670          387 :           clausesa[GFC_OMP_SPLIT_TARGET].if_expr
    8671          387 :             = code->ext.omp_clauses->if_expr;
    8672          387 :           clausesa[GFC_OMP_SPLIT_TARGET].nowait
    8673          387 :             = code->ext.omp_clauses->nowait;
    8674          387 :           clausesa[GFC_OMP_SPLIT_TARGET].device_type
    8675          387 :             = code->ext.omp_clauses->device_type;
    8676          387 :           clausesa[GFC_OMP_SPLIT_TARGET].message
    8677          387 :             = code->ext.omp_clauses->message;
    8678              :         }
    8679         2439 :       if (mask & GFC_OMP_MASK_TEAMS)
    8680              :         {
    8681              :           /* First the clauses that are unique to some constructs.  */
    8682          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].num_teams_list
    8683          401 :             = code->ext.omp_clauses->num_teams_list;
    8684          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].num_teams_dims
    8685          401 :             = code->ext.omp_clauses->num_teams_dims;
    8686          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].thread_limit_list
    8687          401 :             = code->ext.omp_clauses->thread_limit_list;
    8688          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].thread_limit_strict
    8689          401 :             = code->ext.omp_clauses->thread_limit_strict;
    8690          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].thread_limit_dims
    8691          401 :             = code->ext.omp_clauses->thread_limit_dims;
    8692          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].dyn_groupprivate
    8693          401 :             = code->ext.omp_clauses->dyn_groupprivate;
    8694          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].fallback
    8695          401 :             = code->ext.omp_clauses->fallback;
    8696              :           /* Shared and default clauses are allowed on parallel, teams
    8697              :              and taskloop.  */
    8698          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].lists[OMP_LIST_SHARED]
    8699          401 :             = code->ext.omp_clauses->lists[OMP_LIST_SHARED];
    8700          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].default_sharing
    8701          401 :             = code->ext.omp_clauses->default_sharing;
    8702              :           /* Message is used on target, teams, and parallel.  */
    8703          401 :           clausesa[GFC_OMP_SPLIT_TEAMS].message
    8704          401 :             = code->ext.omp_clauses->message;
    8705              :         }
    8706         2439 :       if (mask & GFC_OMP_MASK_DISTRIBUTE)
    8707              :         {
    8708              :           /* First the clauses that are unique to some constructs.  */
    8709          413 :           clausesa[GFC_OMP_SPLIT_DISTRIBUTE].dist_sched_kind
    8710          413 :             = code->ext.omp_clauses->dist_sched_kind;
    8711          413 :           clausesa[GFC_OMP_SPLIT_DISTRIBUTE].dist_chunk_size
    8712          413 :             = code->ext.omp_clauses->dist_chunk_size;
    8713              :           /* Duplicate collapse.  */
    8714          413 :           clausesa[GFC_OMP_SPLIT_DISTRIBUTE].collapse
    8715          413 :             = code->ext.omp_clauses->collapse;
    8716          413 :           clausesa[GFC_OMP_SPLIT_DISTRIBUTE].order_concurrent
    8717          413 :             = code->ext.omp_clauses->order_concurrent;
    8718          413 :           clausesa[GFC_OMP_SPLIT_DISTRIBUTE].order_unconstrained
    8719          413 :             = code->ext.omp_clauses->order_unconstrained;
    8720          413 :           clausesa[GFC_OMP_SPLIT_DISTRIBUTE].order_reproducible
    8721          413 :             = code->ext.omp_clauses->order_reproducible;
    8722              :         }
    8723         2439 :       if (mask & GFC_OMP_MASK_PARALLEL)
    8724              :         {
    8725              :           /* First the clauses that are unique to some constructs.  */
    8726         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].lists[OMP_LIST_COPYIN]
    8727         1950 :             = code->ext.omp_clauses->lists[OMP_LIST_COPYIN];
    8728         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].num_threads_list
    8729         1950 :             = code->ext.omp_clauses->num_threads_list;
    8730         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].num_threads_strict
    8731         1950 :             = code->ext.omp_clauses->num_threads_strict;
    8732         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].num_threads_dims
    8733         1950 :             = code->ext.omp_clauses->num_threads_dims;
    8734         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].proc_bind
    8735         1950 :             = code->ext.omp_clauses->proc_bind;
    8736              :           /* Shared and default clauses are allowed on parallel, teams
    8737              :              and taskloop.  */
    8738         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].lists[OMP_LIST_SHARED]
    8739         1950 :             = code->ext.omp_clauses->lists[OMP_LIST_SHARED];
    8740         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].default_sharing
    8741         1950 :             = code->ext.omp_clauses->default_sharing;
    8742         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].if_exprs[OMP_IF_PARALLEL]
    8743         1950 :             = code->ext.omp_clauses->if_exprs[OMP_IF_PARALLEL];
    8744              :           /* And this is copied to all.  */
    8745         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].if_expr
    8746         1950 :             = code->ext.omp_clauses->if_expr;
    8747         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].message
    8748         1950 :             = code->ext.omp_clauses->message;
    8749         1950 :           clausesa[GFC_OMP_SPLIT_PARALLEL].severity
    8750         1950 :             = code->ext.omp_clauses->severity;
    8751              :         }
    8752         2439 :       if (mask & GFC_OMP_MASK_MASKED)
    8753           81 :         clausesa[GFC_OMP_SPLIT_MASKED].filter = code->ext.omp_clauses->filter;
    8754         2439 :       if ((mask & GFC_OMP_MASK_DO) && !is_loop)
    8755              :         {
    8756              :           /* First the clauses that are unique to some constructs.  */
    8757         1937 :           clausesa[GFC_OMP_SPLIT_DO].ordered
    8758         1937 :             = code->ext.omp_clauses->ordered;
    8759         1937 :           clausesa[GFC_OMP_SPLIT_DO].orderedc
    8760         1937 :             = code->ext.omp_clauses->orderedc;
    8761         1937 :           clausesa[GFC_OMP_SPLIT_DO].sched_kind
    8762         1937 :             = code->ext.omp_clauses->sched_kind;
    8763         1937 :           if (innermost == GFC_OMP_SPLIT_SIMD)
    8764          633 :             clausesa[GFC_OMP_SPLIT_DO].sched_simd
    8765          633 :               = code->ext.omp_clauses->sched_simd;
    8766         1937 :           clausesa[GFC_OMP_SPLIT_DO].sched_monotonic
    8767         1937 :             = code->ext.omp_clauses->sched_monotonic;
    8768         1937 :           clausesa[GFC_OMP_SPLIT_DO].sched_nonmonotonic
    8769         1937 :             = code->ext.omp_clauses->sched_nonmonotonic;
    8770         1937 :           clausesa[GFC_OMP_SPLIT_DO].chunk_size
    8771         1937 :             = code->ext.omp_clauses->chunk_size;
    8772         1937 :           clausesa[GFC_OMP_SPLIT_DO].nowait
    8773         1937 :             = code->ext.omp_clauses->nowait;
    8774              :         }
    8775         1996 :       if (mask & GFC_OMP_MASK_DO)
    8776              :         {
    8777         1996 :           clausesa[GFC_OMP_SPLIT_DO].bind
    8778         1996 :             = code->ext.omp_clauses->bind;
    8779              :           /* Duplicate collapse.  */
    8780         1996 :           clausesa[GFC_OMP_SPLIT_DO].collapse
    8781         1996 :             = code->ext.omp_clauses->collapse;
    8782         1996 :           clausesa[GFC_OMP_SPLIT_DO].order_concurrent
    8783         1996 :             = code->ext.omp_clauses->order_concurrent;
    8784         1996 :           clausesa[GFC_OMP_SPLIT_DO].order_unconstrained
    8785         1996 :             = code->ext.omp_clauses->order_unconstrained;
    8786         1996 :           clausesa[GFC_OMP_SPLIT_DO].order_reproducible
    8787         1996 :             = code->ext.omp_clauses->order_reproducible;
    8788              :         }
    8789         2439 :       if (mask & GFC_OMP_MASK_SIMD)
    8790              :         {
    8791          919 :           clausesa[GFC_OMP_SPLIT_SIMD].safelen_expr
    8792          919 :             = code->ext.omp_clauses->safelen_expr;
    8793          919 :           clausesa[GFC_OMP_SPLIT_SIMD].simdlen_expr
    8794          919 :             = code->ext.omp_clauses->simdlen_expr;
    8795          919 :           clausesa[GFC_OMP_SPLIT_SIMD].simdlen_type
    8796          919 :             = code->ext.omp_clauses->simdlen_type;
    8797          919 :           clausesa[GFC_OMP_SPLIT_SIMD].simdlen_divisor
    8798          919 :             = code->ext.omp_clauses->simdlen_divisor;
    8799          919 :           clausesa[GFC_OMP_SPLIT_SIMD].lists[OMP_LIST_ALIGNED]
    8800          919 :             = code->ext.omp_clauses->lists[OMP_LIST_ALIGNED];
    8801              :           /* Duplicate collapse.  */
    8802          919 :           clausesa[GFC_OMP_SPLIT_SIMD].collapse
    8803          919 :             = code->ext.omp_clauses->collapse;
    8804          919 :           clausesa[GFC_OMP_SPLIT_SIMD].if_exprs[OMP_IF_SIMD]
    8805          919 :             = code->ext.omp_clauses->if_exprs[OMP_IF_SIMD];
    8806          919 :           clausesa[GFC_OMP_SPLIT_SIMD].order_concurrent
    8807          919 :             = code->ext.omp_clauses->order_concurrent;
    8808          919 :           clausesa[GFC_OMP_SPLIT_SIMD].order_unconstrained
    8809          919 :             = code->ext.omp_clauses->order_unconstrained;
    8810          919 :           clausesa[GFC_OMP_SPLIT_SIMD].order_reproducible
    8811          919 :             = code->ext.omp_clauses->order_reproducible;
    8812              :           /* And this is copied to all.  */
    8813          919 :           clausesa[GFC_OMP_SPLIT_SIMD].if_expr
    8814          919 :             = code->ext.omp_clauses->if_expr;
    8815              :         }
    8816         2439 :       if (mask & GFC_OMP_MASK_TASKLOOP)
    8817              :         {
    8818              :           /* First the clauses that are unique to some constructs.  */
    8819          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].nogroup
    8820          163 :             = code->ext.omp_clauses->nogroup;
    8821          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].grainsize
    8822          163 :             = code->ext.omp_clauses->grainsize;
    8823          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].grainsize_strict
    8824          163 :             = code->ext.omp_clauses->grainsize_strict;
    8825          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].num_tasks
    8826          163 :             = code->ext.omp_clauses->num_tasks;
    8827          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].num_tasks_strict
    8828          163 :             = code->ext.omp_clauses->num_tasks_strict;
    8829          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].priority
    8830          163 :             = code->ext.omp_clauses->priority;
    8831          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].final_expr
    8832          163 :             = code->ext.omp_clauses->final_expr;
    8833          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].untied
    8834          163 :             = code->ext.omp_clauses->untied;
    8835          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].mergeable
    8836          163 :             = code->ext.omp_clauses->mergeable;
    8837          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].if_exprs[OMP_IF_TASKLOOP]
    8838          163 :             = code->ext.omp_clauses->if_exprs[OMP_IF_TASKLOOP];
    8839              :           /* And this is copied to all.  */
    8840          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].if_expr
    8841          163 :             = code->ext.omp_clauses->if_expr;
    8842              :           /* Shared and default clauses are allowed on parallel, teams
    8843              :              and taskloop.  */
    8844          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[OMP_LIST_SHARED]
    8845          163 :             = code->ext.omp_clauses->lists[OMP_LIST_SHARED];
    8846          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].default_sharing
    8847          163 :             = code->ext.omp_clauses->default_sharing;
    8848              :           /* Duplicate collapse.  */
    8849          163 :           clausesa[GFC_OMP_SPLIT_TASKLOOP].collapse
    8850          163 :             = code->ext.omp_clauses->collapse;
    8851              :         }
    8852              :       /* Private clause is supported on all constructs but master/masked,
    8853              :          it is enough to put it on the innermost one except for master/masked.  For
    8854              :          !$ omp parallel do put it on parallel though,
    8855              :          as that's what we did for OpenMP 3.1.  */
    8856         2439 :       clausesa[((innermost == GFC_OMP_SPLIT_DO && !is_loop)
    8857              :                 || code->op == EXEC_OMP_PARALLEL_MASTER
    8858         1135 :                 || code->op == EXEC_OMP_PARALLEL_MASKED)
    8859         1124 :                ? (int) GFC_OMP_SPLIT_PARALLEL
    8860         3563 :                : innermost].lists[OMP_LIST_PRIVATE]
    8861         2439 :         = code->ext.omp_clauses->lists[OMP_LIST_PRIVATE];
    8862              :       /* Firstprivate clause is supported on all constructs but
    8863              :          simd and masked/master.  Put it on the outermost of those and duplicate
    8864              :          on parallel and teams.  */
    8865         2439 :       if (mask & GFC_OMP_MASK_TARGET)
    8866          387 :         gfc_add_firstprivate_if_unmapped (&clausesa[GFC_OMP_SPLIT_TARGET],
    8867              :                                           code->ext.omp_clauses);
    8868         2439 :       if (mask & GFC_OMP_MASK_TEAMS)
    8869          401 :         clausesa[GFC_OMP_SPLIT_TEAMS].lists[OMP_LIST_FIRSTPRIVATE]
    8870          401 :           = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
    8871         2038 :       else if (mask & GFC_OMP_MASK_DISTRIBUTE)
    8872          114 :         clausesa[GFC_OMP_SPLIT_DISTRIBUTE].lists[OMP_LIST_FIRSTPRIVATE]
    8873          114 :           = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
    8874         2439 :       if (mask & GFC_OMP_MASK_TASKLOOP)
    8875          163 :         clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[OMP_LIST_FIRSTPRIVATE]
    8876          163 :           = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
    8877         2439 :       if ((mask & GFC_OMP_MASK_PARALLEL)
    8878         1950 :           && !(mask & GFC_OMP_MASK_TASKLOOP))
    8879         1864 :         clausesa[GFC_OMP_SPLIT_PARALLEL].lists[OMP_LIST_FIRSTPRIVATE]
    8880         1864 :           = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
    8881          575 :       else if ((mask & GFC_OMP_MASK_DO) && !is_loop)
    8882          146 :         clausesa[GFC_OMP_SPLIT_DO].lists[OMP_LIST_FIRSTPRIVATE]
    8883          146 :           = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
    8884              :       /* Lastprivate is allowed on distribute, do, simd, taskloop and loop.
    8885              :          In parallel do{, simd} we actually want to put it on
    8886              :          parallel rather than do.  */
    8887         2439 :       if (mask & GFC_OMP_MASK_DISTRIBUTE)
    8888          413 :         clausesa[GFC_OMP_SPLIT_DISTRIBUTE].lists[OMP_LIST_LASTPRIVATE]
    8889          413 :           = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
    8890         2439 :       if (mask & GFC_OMP_MASK_TASKLOOP)
    8891          163 :         clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[OMP_LIST_LASTPRIVATE]
    8892          163 :           = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
    8893         2439 :       if ((mask & GFC_OMP_MASK_PARALLEL) && !is_loop
    8894         1914 :           && !(mask & GFC_OMP_MASK_TASKLOOP))
    8895         1828 :         clausesa[GFC_OMP_SPLIT_PARALLEL].lists[OMP_LIST_LASTPRIVATE]
    8896         1828 :           = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
    8897          611 :       else if (mask & GFC_OMP_MASK_DO)
    8898          205 :         clausesa[GFC_OMP_SPLIT_DO].lists[OMP_LIST_LASTPRIVATE]
    8899          205 :           = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
    8900         2439 :       if (mask & GFC_OMP_MASK_SIMD)
    8901          919 :         clausesa[GFC_OMP_SPLIT_SIMD].lists[OMP_LIST_LASTPRIVATE]
    8902          919 :           = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
    8903              :       /* Reduction is allowed on simd, do, parallel, teams, taskloop, and loop.
    8904              :          Duplicate it on all of them, but
    8905              :          - omit on do if parallel is present;
    8906              :          - omit on task and parallel if loop is present;
    8907              :          additionally, inscan applies to do/simd only.  */
    8908         9756 :       for (int i = OMP_LIST_REDUCTION; i <= OMP_LIST_REDUCTION_TASK; i++)
    8909              :         {
    8910         7317 :           if (mask & GFC_OMP_MASK_TASKLOOP
    8911          489 :               && i != OMP_LIST_REDUCTION_INSCAN)
    8912          326 :             clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[i]
    8913          326 :               = code->ext.omp_clauses->lists[i];
    8914         7317 :           if (mask & GFC_OMP_MASK_TEAMS
    8915         1203 :               && i != OMP_LIST_REDUCTION_INSCAN
    8916         1203 :               && !is_loop)
    8917          756 :             clausesa[GFC_OMP_SPLIT_TEAMS].lists[i]
    8918          756 :               = code->ext.omp_clauses->lists[i];
    8919         7317 :           if (mask & GFC_OMP_MASK_PARALLEL
    8920         5850 :               && i != OMP_LIST_REDUCTION_INSCAN
    8921         3900 :               && !(mask & GFC_OMP_MASK_TASKLOOP)
    8922         3728 :               && !is_loop)
    8923         3656 :             clausesa[GFC_OMP_SPLIT_PARALLEL].lists[i]
    8924         3656 :               = code->ext.omp_clauses->lists[i];
    8925         3661 :           else if (mask & GFC_OMP_MASK_DO)
    8926         2406 :             clausesa[GFC_OMP_SPLIT_DO].lists[i]
    8927         2406 :               = code->ext.omp_clauses->lists[i];
    8928         7317 :           if (mask & GFC_OMP_MASK_SIMD)
    8929         2757 :             clausesa[GFC_OMP_SPLIT_SIMD].lists[i]
    8930         2757 :               = code->ext.omp_clauses->lists[i];
    8931              :         }
    8932         2439 :       if (mask & GFC_OMP_MASK_TARGET)
    8933          387 :         clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_IN_REDUCTION]
    8934          387 :           = code->ext.omp_clauses->lists[OMP_LIST_IN_REDUCTION];
    8935         2439 :       if (mask & GFC_OMP_MASK_TASKLOOP)
    8936          163 :         clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[OMP_LIST_IN_REDUCTION]
    8937          163 :           = code->ext.omp_clauses->lists[OMP_LIST_IN_REDUCTION];
    8938              :       /* Linear clause is supported on do and simd,
    8939              :          put it on the innermost one.  */
    8940         2439 :       clausesa[innermost].lists[OMP_LIST_LINEAR]
    8941         2439 :         = code->ext.omp_clauses->lists[OMP_LIST_LINEAR];
    8942              :     }
    8943              :    /* Propagate firstprivate/lastprivate/reduction vars to
    8944              :       shared (parallel, teams) and map-tofrom (target).  */
    8945         2439 :    if (mask & GFC_OMP_MASK_TARGET)
    8946          387 :      gfc_add_clause_implicitly (&clausesa[GFC_OMP_SPLIT_TARGET],
    8947              :                                 code->ext.omp_clauses, true, false);
    8948         2439 :    if ((mask & GFC_OMP_MASK_PARALLEL) && innermost != GFC_OMP_MASK_PARALLEL)
    8949         1950 :      gfc_add_clause_implicitly (&clausesa[GFC_OMP_SPLIT_PARALLEL],
    8950              :                                 code->ext.omp_clauses, false,
    8951         1950 :                                 mask & GFC_OMP_MASK_DO);
    8952         2439 :    if (mask & GFC_OMP_MASK_TEAMS && innermost != GFC_OMP_MASK_TEAMS)
    8953          401 :      gfc_add_clause_implicitly (&clausesa[GFC_OMP_SPLIT_TEAMS],
    8954              :                                 code->ext.omp_clauses, false, false);
    8955         2439 :    if (((mask & (GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO))
    8956              :         == (GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO))
    8957         1827 :        && !is_loop)
    8958         1791 :     clausesa[GFC_OMP_SPLIT_DO].nowait = true;
    8959              : 
    8960              :    /* Distribute allocate clause to do, parallel, distribute, teams, target
    8961              :       and taskloop.  The code below iterates over variables in the
    8962              :       allocate list and checks if that available is also in any
    8963              :       privatization clause on those construct.  If yes, then we add it
    8964              :       to the list of 'allocate'ed variables for that construct.  If a
    8965              :       variable is found in none of them then we issue an error.  */
    8966              : 
    8967         2439 :    if (code->ext.omp_clauses->lists[OMP_LIST_ALLOCATE])
    8968              :      {
    8969              :        gfc_omp_namelist *alloc_nl, *priv_nl;
    8970              :        gfc_omp_namelist *tails[GFC_OMP_SPLIT_NUM];
    8971          104 :        for (alloc_nl = code->ext.omp_clauses->lists[OMP_LIST_ALLOCATE];
    8972          181 :            alloc_nl; alloc_nl = alloc_nl->next)
    8973              :          {
    8974              :            bool found = false;
    8975          728 :            for (int i = GFC_OMP_SPLIT_DO; i <= GFC_OMP_SPLIT_TASKLOOP; i++)
    8976              :              {
    8977              :                gfc_omp_namelist *p;
    8978              :                int list;
    8979        24960 :                for (list = 0; list < OMP_LIST_NUM; list++)
    8980              :                  {
    8981        24336 :                    switch (list)
    8982              :                    {
    8983         5616 :                      case OMP_LIST_PRIVATE:
    8984         5616 :                      case OMP_LIST_FIRSTPRIVATE:
    8985         5616 :                      case OMP_LIST_LASTPRIVATE:
    8986         5616 :                      case OMP_LIST_REDUCTION:
    8987         5616 :                      case OMP_LIST_REDUCTION_INSCAN:
    8988         5616 :                      case OMP_LIST_REDUCTION_TASK:
    8989         5616 :                      case OMP_LIST_IN_REDUCTION:
    8990         5616 :                      case OMP_LIST_TASK_REDUCTION:
    8991         5616 :                      case OMP_LIST_LINEAR:
    8992         6092 :                        for (priv_nl = clausesa[i].lists[list]; priv_nl;
    8993          476 :                             priv_nl = priv_nl->next)
    8994          476 :                          if (alloc_nl->sym == priv_nl->sym)
    8995              :                            {
    8996          131 :                              found = true;
    8997          131 :                              p = gfc_get_omp_namelist ();
    8998          131 :                              p->sym = alloc_nl->sym;
    8999          131 :                              p->expr = alloc_nl->expr;
    9000          131 :                              p->u.align = alloc_nl->u.align;
    9001          131 :                              p->u2.allocator = alloc_nl->u2.allocator;
    9002          131 :                              p->where = alloc_nl->where;
    9003          131 :                              if (clausesa[i].lists[OMP_LIST_ALLOCATE] == NULL)
    9004              :                                {
    9005          109 :                                  clausesa[i].lists[OMP_LIST_ALLOCATE] = p;
    9006          109 :                                  tails[i] = p;
    9007              :                                }
    9008              :                              else
    9009              :                                {
    9010           22 :                                  tails[i]->next = p;
    9011           22 :                                  tails[i] = tails[i]->next;
    9012              :                                }
    9013              :                            }
    9014              :                        break;
    9015              :                      default:
    9016              :                        break;
    9017              :                    }
    9018              :                  }
    9019              :              }
    9020          104 :            if (!found)
    9021            1 :              gfc_error ("%qs specified in %<allocate%> clause at %L but not "
    9022              :                         "in an explicit privatization clause",
    9023            1 :                         alloc_nl->sym->name, &alloc_nl->where);
    9024              :          }
    9025              :      }
    9026              : }
    9027              : 
    9028              : static tree
    9029          633 : gfc_trans_omp_do_simd (gfc_code *code, stmtblock_t *pblock,
    9030              :                        gfc_omp_clauses *clausesa, tree omp_clauses)
    9031              : {
    9032          633 :   stmtblock_t block;
    9033          633 :   gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
    9034          633 :   tree stmt, body, omp_do_clauses = NULL_TREE;
    9035          633 :   bool free_clausesa = false;
    9036              : 
    9037          633 :   if (pblock == NULL)
    9038          478 :     gfc_start_block (&block);
    9039              :   else
    9040          155 :     gfc_init_block (&block);
    9041              : 
    9042          633 :   if (clausesa == NULL)
    9043              :     {
    9044          146 :       clausesa = clausesa_buf;
    9045          146 :       gfc_split_omp_clauses (code, clausesa);
    9046          146 :       free_clausesa = true;
    9047              :     }
    9048          633 :   if (flag_openmp)
    9049          628 :     omp_do_clauses
    9050          628 :       = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_DO], code->loc);
    9051         1111 :   body = gfc_trans_omp_do (code, EXEC_OMP_SIMD, pblock ? pblock : &block,
    9052              :                            &clausesa[GFC_OMP_SPLIT_SIMD], omp_clauses);
    9053          633 :   if (pblock == NULL)
    9054              :     {
    9055          478 :       if (TREE_CODE (body) != BIND_EXPR)
    9056          478 :         body = build3_v (BIND_EXPR, NULL, body, poplevel (1, 0));
    9057              :       else
    9058            0 :         poplevel (0, 0);
    9059              :     }
    9060          155 :   else if (TREE_CODE (body) != BIND_EXPR)
    9061          155 :     body = build3_v (BIND_EXPR, NULL, body, NULL_TREE);
    9062          633 :   if (flag_openmp)
    9063              :     {
    9064          628 :       stmt = make_node (OMP_FOR);
    9065          628 :       SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
    9066          628 :       TREE_TYPE (stmt) = void_type_node;
    9067          628 :       OMP_FOR_BODY (stmt) = body;
    9068          628 :       OMP_FOR_CLAUSES (stmt) = omp_do_clauses;
    9069              :     }
    9070              :   else
    9071              :     stmt = body;
    9072          633 :   gfc_add_expr_to_block (&block, stmt);
    9073          633 :   if (free_clausesa)
    9074          146 :     gfc_free_split_omp_clauses (code, clausesa);
    9075          633 :   return gfc_finish_block (&block);
    9076              : }
    9077              : 
    9078              : static tree
    9079         1340 : gfc_trans_omp_parallel_do (gfc_code *code, bool is_loop, stmtblock_t *pblock,
    9080              :                            gfc_omp_clauses *clausesa)
    9081              : {
    9082         1340 :   stmtblock_t block, *new_pblock = pblock;
    9083         1340 :   gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
    9084         1340 :   tree stmt, omp_clauses = NULL_TREE;
    9085         1340 :   bool free_clausesa = false;
    9086              : 
    9087         1340 :   if (pblock == NULL)
    9088         1120 :     gfc_start_block (&block);
    9089              :   else
    9090          220 :     gfc_init_block (&block);
    9091              : 
    9092         1340 :   if (clausesa == NULL)
    9093              :     {
    9094         1120 :       clausesa = clausesa_buf;
    9095         1120 :       gfc_split_omp_clauses (code, clausesa);
    9096         1120 :       free_clausesa = true;
    9097              :     }
    9098         1340 :   omp_clauses
    9099         1340 :     = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_PARALLEL],
    9100              :                              code->loc);
    9101         1340 :   if (pblock == NULL)
    9102              :     {
    9103         1120 :       if (!clausesa[GFC_OMP_SPLIT_DO].ordered
    9104         1111 :           && clausesa[GFC_OMP_SPLIT_DO].sched_kind != OMP_SCHED_STATIC)
    9105              :         new_pblock = &block;
    9106              :       else
    9107           65 :         pushlevel ();
    9108              :     }
    9109         2644 :   stmt = gfc_trans_omp_do (code, is_loop ? EXEC_OMP_LOOP : EXEC_OMP_DO,
    9110              :                            new_pblock, &clausesa[GFC_OMP_SPLIT_DO],
    9111              :                            omp_clauses);
    9112         1340 :   if (pblock == NULL)
    9113              :     {
    9114         1120 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9115         1104 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9116              :       else
    9117           16 :         poplevel (0, 0);
    9118              :     }
    9119          220 :   else if (TREE_CODE (stmt) != BIND_EXPR)
    9120          220 :     stmt = build3_v (BIND_EXPR, NULL, stmt, NULL_TREE);
    9121         1340 :   stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
    9122              :                      void_type_node, stmt, omp_clauses);
    9123         1340 :   OMP_PARALLEL_COMBINED (stmt) = 1;
    9124         1340 :   gfc_add_expr_to_block (&block, stmt);
    9125         1340 :   if (free_clausesa)
    9126         1120 :     gfc_free_split_omp_clauses (code, clausesa);
    9127         1340 :   return gfc_finish_block (&block);
    9128              : }
    9129              : 
    9130              : static tree
    9131          487 : gfc_trans_omp_parallel_do_simd (gfc_code *code, stmtblock_t *pblock,
    9132              :                                 gfc_omp_clauses *clausesa)
    9133              : {
    9134          487 :   stmtblock_t block;
    9135          487 :   gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
    9136          487 :   tree stmt, omp_clauses = NULL_TREE;
    9137          487 :   bool free_clausesa = false;
    9138              : 
    9139          487 :   if (pblock == NULL)
    9140          332 :     gfc_start_block (&block);
    9141              :   else
    9142          155 :     gfc_init_block (&block);
    9143              : 
    9144          487 :   if (clausesa == NULL)
    9145              :     {
    9146          332 :       clausesa = clausesa_buf;
    9147          332 :       gfc_split_omp_clauses (code, clausesa);
    9148          332 :       free_clausesa = true;
    9149              :     }
    9150          487 :   if (flag_openmp)
    9151          484 :     omp_clauses
    9152          484 :       = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_PARALLEL],
    9153              :                                code->loc);
    9154          487 :   if (pblock == NULL)
    9155          332 :     pushlevel ();
    9156          487 :   stmt = gfc_trans_omp_do_simd (code, pblock, clausesa, omp_clauses);
    9157          487 :   if (pblock == NULL)
    9158              :     {
    9159          332 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9160          227 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9161              :       else
    9162          105 :         poplevel (0, 0);
    9163              :     }
    9164          155 :   else if (TREE_CODE (stmt) != BIND_EXPR)
    9165          155 :     stmt = build3_v (BIND_EXPR, NULL, stmt, NULL_TREE);
    9166          487 :   if (flag_openmp)
    9167              :     {
    9168          484 :       stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
    9169              :                          void_type_node, stmt, omp_clauses);
    9170          484 :       OMP_PARALLEL_COMBINED (stmt) = 1;
    9171              :     }
    9172          487 :   gfc_add_expr_to_block (&block, stmt);
    9173          487 :   if (free_clausesa)
    9174          332 :     gfc_free_split_omp_clauses (code, clausesa);
    9175          487 :   return gfc_finish_block (&block);
    9176              : }
    9177              : 
    9178              : static tree
    9179           54 : gfc_trans_omp_parallel_sections (gfc_code *code)
    9180              : {
    9181           54 :   stmtblock_t block;
    9182           54 :   gfc_omp_clauses section_clauses;
    9183           54 :   tree stmt, omp_clauses;
    9184              : 
    9185           54 :   memset (&section_clauses, 0, sizeof (section_clauses));
    9186           54 :   section_clauses.nowait = true;
    9187              : 
    9188           54 :   gfc_start_block (&block);
    9189           54 :   omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    9190              :                                        code->loc);
    9191           54 :   pushlevel ();
    9192           54 :   stmt = gfc_trans_omp_sections (code, &section_clauses);
    9193           54 :   if (TREE_CODE (stmt) != BIND_EXPR)
    9194           54 :     stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9195              :   else
    9196            0 :     poplevel (0, 0);
    9197           54 :   stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
    9198              :                      void_type_node, stmt, omp_clauses);
    9199           54 :   OMP_PARALLEL_COMBINED (stmt) = 1;
    9200           54 :   gfc_add_expr_to_block (&block, stmt);
    9201           54 :   return gfc_finish_block (&block);
    9202              : }
    9203              : 
    9204              : static tree
    9205           50 : gfc_trans_omp_parallel_workshare (gfc_code *code)
    9206              : {
    9207           50 :   stmtblock_t block;
    9208           50 :   gfc_omp_clauses workshare_clauses;
    9209           50 :   tree stmt, omp_clauses;
    9210              : 
    9211           50 :   memset (&workshare_clauses, 0, sizeof (workshare_clauses));
    9212           50 :   workshare_clauses.nowait = true;
    9213              : 
    9214           50 :   gfc_start_block (&block);
    9215           50 :   omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    9216              :                                        code->loc);
    9217           50 :   pushlevel ();
    9218           50 :   stmt = gfc_trans_omp_workshare (code, &workshare_clauses);
    9219           50 :   stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9220           50 :   stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
    9221              :                      void_type_node, stmt, omp_clauses);
    9222           50 :   OMP_PARALLEL_COMBINED (stmt) = 1;
    9223           50 :   gfc_add_expr_to_block (&block, stmt);
    9224           50 :   return gfc_finish_block (&block);
    9225              : }
    9226              : 
    9227              : static tree
    9228           53 : gfc_trans_omp_scope (gfc_code *code)
    9229              : {
    9230           53 :   stmtblock_t block;
    9231           53 :   tree body = gfc_trans_code (code->block->next);
    9232           53 :   if (IS_EMPTY_STMT (body))
    9233              :     return body;
    9234           51 :   gfc_start_block (&block);
    9235           51 :   tree omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    9236              :                                             code->loc);
    9237           51 :   tree stmt = make_node (OMP_SCOPE);
    9238           51 :   SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
    9239           51 :   TREE_TYPE (stmt) = void_type_node;
    9240           51 :   OMP_SCOPE_BODY (stmt) = body;
    9241           51 :   OMP_SCOPE_CLAUSES (stmt) = omp_clauses;
    9242           51 :   gfc_add_expr_to_block (&block, stmt);
    9243           51 :   return gfc_finish_block (&block);
    9244              : }
    9245              : 
    9246              : static tree
    9247          129 : gfc_trans_omp_sections (gfc_code *code, gfc_omp_clauses *clauses)
    9248              : {
    9249          129 :   stmtblock_t block, body;
    9250          129 :   tree omp_clauses, stmt;
    9251          129 :   bool has_lastprivate = clauses->lists[OMP_LIST_LASTPRIVATE] != NULL;
    9252          129 :   location_t loc = gfc_get_location (&code->loc);
    9253              : 
    9254          129 :   gfc_start_block (&block);
    9255              : 
    9256          129 :   omp_clauses = gfc_trans_omp_clauses (&block, clauses, code->loc);
    9257              : 
    9258          129 :   gfc_init_block (&body);
    9259          499 :   for (code = code->block; code; code = code->block)
    9260              :     {
    9261              :       /* Last section is special because of lastprivate, so even if it
    9262              :          is empty, chain it in.  */
    9263          740 :       stmt = gfc_trans_omp_code (code->next,
    9264           48 :                                  has_lastprivate && code->block == NULL);
    9265          370 :       if (! IS_EMPTY_STMT (stmt))
    9266              :         {
    9267          280 :           stmt = build1_v (OMP_SECTION, stmt);
    9268          280 :           gfc_add_expr_to_block (&body, stmt);
    9269              :         }
    9270              :     }
    9271          129 :   stmt = gfc_finish_block (&body);
    9272              : 
    9273          129 :   stmt = build2_loc (loc, OMP_SECTIONS, void_type_node, stmt, omp_clauses);
    9274          129 :   gfc_add_expr_to_block (&block, stmt);
    9275              : 
    9276          129 :   return gfc_finish_block (&block);
    9277              : }
    9278              : 
    9279              : static tree
    9280          560 : gfc_trans_omp_single (gfc_code *code, gfc_omp_clauses *clauses)
    9281              : {
    9282          560 :   stmtblock_t block;
    9283          560 :   gfc_start_block (&block);
    9284          560 :   tree omp_clauses = gfc_trans_omp_clauses (&block, clauses, code->loc);
    9285          560 :   tree stmt = gfc_trans_omp_code (code->block->next, true);
    9286          560 :   stmt = build2_loc (gfc_get_location (&code->loc), OMP_SINGLE, void_type_node,
    9287              :                      stmt, omp_clauses);
    9288          560 :   gfc_add_expr_to_block (&block, stmt);
    9289          560 :   return gfc_finish_block (&block);
    9290              : }
    9291              : 
    9292              : static tree
    9293         1125 : gfc_trans_omp_task (gfc_code *code)
    9294              : {
    9295         1125 :   stmtblock_t block;
    9296         1125 :   tree stmt, omp_clauses;
    9297              : 
    9298         1125 :   gfc_start_block (&block);
    9299         1125 :   omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    9300              :                                        code->loc);
    9301         1125 :   pushlevel ();
    9302         1125 :   stmt = gfc_trans_omp_code (code->block->next, true);
    9303         1125 :   stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9304         1125 :   stmt = build2_loc (gfc_get_location (&code->loc), OMP_TASK, void_type_node,
    9305              :                      stmt, omp_clauses);
    9306         1125 :   gfc_add_expr_to_block (&block, stmt);
    9307         1125 :   return gfc_finish_block (&block);
    9308              : }
    9309              : 
    9310              : static tree
    9311          181 : gfc_trans_omp_taskgroup (gfc_code *code)
    9312              : {
    9313          181 :   stmtblock_t block;
    9314          181 :   gfc_start_block (&block);
    9315          181 :   tree body = gfc_trans_code (code->block->next);
    9316          181 :   tree stmt = make_node (OMP_TASKGROUP);
    9317          181 :   SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
    9318          181 :   TREE_TYPE (stmt) = void_type_node;
    9319          181 :   OMP_TASKGROUP_BODY (stmt) = body;
    9320          181 :   OMP_TASKGROUP_CLAUSES (stmt) = gfc_trans_omp_clauses (&block,
    9321              :                                                         code->ext.omp_clauses,
    9322              :                                                         code->loc);
    9323          181 :   gfc_add_expr_to_block (&block, stmt);
    9324          181 :   return gfc_finish_block (&block);
    9325              : }
    9326              : 
    9327              : static tree
    9328          148 : gfc_trans_omp_taskwait (gfc_code *code)
    9329              : {
    9330          148 :   if (!code->ext.omp_clauses)
    9331              :     {
    9332          134 :       tree decl = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
    9333          134 :       return build_call_expr_loc (input_location, decl, 0);
    9334              :     }
    9335           14 :   stmtblock_t block;
    9336           14 :   gfc_start_block (&block);
    9337           14 :   tree stmt = make_node (OMP_TASK);
    9338           14 :   SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
    9339           14 :   TREE_TYPE (stmt) = void_type_node;
    9340           14 :   OMP_TASK_BODY (stmt) = NULL_TREE;
    9341           14 :   OMP_TASK_CLAUSES (stmt) = gfc_trans_omp_clauses (&block,
    9342              :                                                    code->ext.omp_clauses,
    9343              :                                                    code->loc);
    9344           14 :   gfc_add_expr_to_block (&block, stmt);
    9345           14 :   return gfc_finish_block (&block);
    9346              : }
    9347              : 
    9348              : static tree
    9349            8 : gfc_trans_omp_taskyield (void)
    9350              : {
    9351            8 :   tree decl = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
    9352            8 :   return build_call_expr_loc (input_location, decl, 0);
    9353              : }
    9354              : 
    9355              : static tree
    9356          380 : gfc_trans_omp_distribute (gfc_code *code, gfc_omp_clauses *clausesa)
    9357              : {
    9358          380 :   stmtblock_t block;
    9359          380 :   gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
    9360          380 :   tree stmt, omp_clauses = NULL_TREE;
    9361          380 :   bool free_clausesa = false;
    9362              : 
    9363          380 :   gfc_start_block (&block);
    9364          380 :   if (clausesa == NULL)
    9365              :     {
    9366          114 :       clausesa = clausesa_buf;
    9367          114 :       gfc_split_omp_clauses (code, clausesa);
    9368          114 :       free_clausesa = true;
    9369              :     }
    9370          380 :   if (flag_openmp)
    9371          380 :     omp_clauses
    9372          380 :       = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_DISTRIBUTE],
    9373              :                                code->loc);
    9374          380 :   switch (code->op)
    9375              :     {
    9376            0 :     case EXEC_OMP_DISTRIBUTE:
    9377            0 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
    9378            0 :     case EXEC_OMP_TEAMS_DISTRIBUTE:
    9379              :       /* This is handled in gfc_trans_omp_do.  */
    9380            0 :       gcc_unreachable ();
    9381          138 :       break;
    9382          138 :     case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
    9383          138 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
    9384          138 :     case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
    9385          138 :       stmt = gfc_trans_omp_parallel_do (code, false, &block, clausesa);
    9386          138 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9387          138 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9388              :       else
    9389            0 :         poplevel (0, 0);
    9390              :       break;
    9391          138 :     case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
    9392          138 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    9393          138 :     case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    9394          138 :       stmt = gfc_trans_omp_parallel_do_simd (code, &block, clausesa);
    9395          138 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9396          138 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9397              :       else
    9398            0 :         poplevel (0, 0);
    9399              :       break;
    9400          104 :     case EXEC_OMP_DISTRIBUTE_SIMD:
    9401          104 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
    9402          104 :     case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
    9403          104 :       stmt = gfc_trans_omp_do (code, EXEC_OMP_SIMD, &block,
    9404              :                                &clausesa[GFC_OMP_SPLIT_SIMD], NULL_TREE);
    9405          104 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9406          104 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9407              :       else
    9408            0 :         poplevel (0, 0);
    9409              :       break;
    9410            0 :     default:
    9411            0 :       gcc_unreachable ();
    9412              :     }
    9413          380 :   if (flag_openmp)
    9414              :     {
    9415          380 :       tree distribute = make_node (OMP_DISTRIBUTE);
    9416          380 :       SET_EXPR_LOCATION (distribute, gfc_get_location (&code->loc));
    9417          380 :       TREE_TYPE (distribute) = void_type_node;
    9418          380 :       OMP_FOR_BODY (distribute) = stmt;
    9419          380 :       OMP_FOR_CLAUSES (distribute) = omp_clauses;
    9420          380 :       stmt = distribute;
    9421              :     }
    9422          380 :   gfc_add_expr_to_block (&block, stmt);
    9423          380 :   if (free_clausesa)
    9424          114 :     gfc_free_split_omp_clauses (code, clausesa);
    9425          380 :   return gfc_finish_block (&block);
    9426              : }
    9427              : 
    9428              : static tree
    9429          554 : gfc_trans_omp_teams (gfc_code *code, gfc_omp_clauses *clausesa,
    9430              :                      tree omp_clauses)
    9431              : {
    9432          554 :   stmtblock_t block;
    9433          554 :   gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
    9434          554 :   tree stmt;
    9435          554 :   bool combined = true, free_clausesa = false;
    9436              : 
    9437          554 :   gfc_start_block (&block);
    9438          554 :   if (clausesa == NULL)
    9439              :     {
    9440          319 :       clausesa = clausesa_buf;
    9441          319 :       gfc_split_omp_clauses (code, clausesa);
    9442          319 :       free_clausesa = true;
    9443              :     }
    9444          554 :   if (flag_openmp)
    9445              :     {
    9446          554 :       omp_clauses
    9447          554 :         = chainon (omp_clauses,
    9448              :                    gfc_trans_omp_clauses (&block,
    9449              :                                           &clausesa[GFC_OMP_SPLIT_TEAMS],
    9450              :                                           code->loc));
    9451          554 :       pushlevel ();
    9452              :     }
    9453          554 :   switch (code->op)
    9454              :     {
    9455          232 :     case EXEC_OMP_TARGET_TEAMS:
    9456          232 :     case EXEC_OMP_TEAMS:
    9457          232 :       stmt = gfc_trans_omp_code (code->block->next, true);
    9458          232 :       combined = false;
    9459          232 :       break;
    9460           33 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
    9461           33 :     case EXEC_OMP_TEAMS_DISTRIBUTE:
    9462           33 :       stmt = gfc_trans_omp_do (code, EXEC_OMP_DISTRIBUTE, NULL,
    9463              :                                &clausesa[GFC_OMP_SPLIT_DISTRIBUTE],
    9464              :                                NULL);
    9465           33 :       break;
    9466           23 :     case EXEC_OMP_TARGET_TEAMS_LOOP:
    9467           23 :     case EXEC_OMP_TEAMS_LOOP:
    9468           23 :       stmt = gfc_trans_omp_do (code, EXEC_OMP_LOOP, NULL,
    9469              :                                &clausesa[GFC_OMP_SPLIT_DO],
    9470              :                                NULL);
    9471           23 :       break;
    9472          266 :     default:
    9473          266 :       stmt = gfc_trans_omp_distribute (code, clausesa);
    9474          266 :       break;
    9475              :     }
    9476          554 :   if (flag_openmp)
    9477              :     {
    9478          554 :       stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9479          554 :       stmt = build2_loc (gfc_get_location (&code->loc), OMP_TEAMS,
    9480              :                          void_type_node, stmt, omp_clauses);
    9481          554 :       if (combined)
    9482          322 :         OMP_TEAMS_COMBINED (stmt) = 1;
    9483              :     }
    9484          554 :   gfc_add_expr_to_block (&block, stmt);
    9485          554 :   if (free_clausesa)
    9486          319 :     gfc_free_split_omp_clauses (code, clausesa);
    9487          554 :   return gfc_finish_block (&block);
    9488              : }
    9489              : 
    9490              : static tree
    9491         2467 : gfc_trans_omp_target (gfc_code *code)
    9492              : {
    9493         2467 :   stmtblock_t block;
    9494         2467 :   gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM];
    9495         2467 :   tree stmt, omp_clauses = NULL_TREE;
    9496              : 
    9497         2467 :   gfc_start_block (&block);
    9498              : 
    9499              :   /* For target + teams, evaluate some clauses on the host; as for those their
    9500              :      expressions must be target consistent, this is also permitted when
    9501              :      nested and not only for combined directives.  */
    9502         2467 :   bool combined_teams_dir = false;
    9503         2467 :   if (flag_openmp)
    9504         2467 :     switch (code->op)
    9505              :       {
    9506              :       case EXEC_OMP_TARGET_TEAMS:
    9507              :       case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
    9508              :       case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
    9509              :       case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    9510              :       case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
    9511              :       case EXEC_OMP_TARGET_TEAMS_LOOP:
    9512              :         combined_teams_dir = true;
    9513              :         break;
    9514              :       default:
    9515         2232 :         combined_teams_dir = false;
    9516              :         break;
    9517              :       }
    9518         2467 :   tree tree_tgt_clauses = NULL_TREE, tree_teams_clauses = NULL_TREE;
    9519         2467 :   gfc_omp_clauses *teams_clauses = NULL;
    9520         2467 :   if (flag_openmp && code->op == EXEC_OMP_TARGET && code->block->next)
    9521              :     {
    9522         2000 :       gfc_code *code2 = (code->block->next->op == EXEC_BLOCK
    9523           96 :                          ? code->block->next->ext.block.ns->code
    9524              :                          : code->block->next);
    9525         2000 :       if (code2)
    9526         1987 :         switch (code2->op)
    9527              :           {
    9528          178 :           case EXEC_OMP_TEAMS:
    9529          178 :           case EXEC_OMP_TEAMS_DISTRIBUTE:
    9530          178 :           case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
    9531          178 :           case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    9532          178 :           case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
    9533          178 :           case EXEC_OMP_TEAMS_LOOP:
    9534          178 :             teams_clauses = code2->ext.omp_clauses;
    9535          178 :             break;
    9536              :           default:
    9537              :             break;
    9538              :           }
    9539              :      }
    9540          178 :   if ((teams_clauses && teams_clauses->num_teams_list)
    9541         2418 :        || (combined_teams_dir && code->ext.omp_clauses->num_teams_list))
    9542              :     {
    9543           65 :       gfc_omp_clauses *num_teams_clauses
    9544           65 :         = teams_clauses ? teams_clauses : code->ext.omp_clauses;
    9545          114 :       tree c, t, num_teams = NULL_TREE;
    9546          114 :       for (gfc_expr_list *el = num_teams_clauses->num_teams_list;
    9547          254 :            el; el = el->next)
    9548              :         {
    9549          140 :           t = gfc_convert_expr_to_tree (&block, el->expr);
    9550          140 :           if (DECL_P (t))
    9551              :             {
    9552           79 :               c = build_omp_clause (gfc_get_location (&el->expr->where),
    9553              :                                     OMP_CLAUSE_FIRSTPRIVATE);
    9554           79 :               OMP_CLAUSE_DECL (c) = t;
    9555           79 :               tree_tgt_clauses = gfc_trans_add_clause (c, tree_tgt_clauses);
    9556              :             }
    9557          140 :           num_teams = tree_cons (NULL_TREE, t, num_teams);
    9558              :         }
    9559          114 :       locus *loc = &num_teams_clauses->num_teams_list->expr->where;
    9560          114 :       c = build_omp_clause (gfc_get_location (loc), OMP_CLAUSE_NUM_TEAMS);
    9561          114 :       if (num_teams_clauses->num_teams_dims)
    9562              :         {
    9563           12 :           OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = NULL_TREE;
    9564           12 :           OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = nreverse (num_teams);
    9565           12 :           OMP_CLAUSE_NUM_TEAMS_DIMS (c) = num_teams_clauses->num_teams_dims;
    9566              :         }
    9567              :       else
    9568              :         {
    9569          102 :           OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = TREE_VALUE (num_teams);
    9570          102 :           if (TREE_CHAIN (num_teams))
    9571           17 :             OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
    9572           34 :               = TREE_VALUE (TREE_CHAIN (num_teams));
    9573              :         }
    9574          114 :       tree_tgt_clauses = gfc_trans_add_clause (c, tree_tgt_clauses);
    9575          114 :       tree_teams_clauses = gfc_trans_add_clause (unshare_expr (c),
    9576              :                                                  tree_teams_clauses);
    9577          114 :       gfc_free_expr_list (num_teams_clauses->num_teams_list);
    9578          114 :       num_teams_clauses->num_teams_list = NULL;
    9579              :     }
    9580         2467 :   if ((teams_clauses && teams_clauses->thread_limit_list)
    9581         2433 :       || (combined_teams_dir && code->ext.omp_clauses->thread_limit_list))
    9582              :     {
    9583              :       /* thread_limit can be put on target and on teams, use the team one as
    9584              :          having higher precedence.  */
    9585           46 :       gfc_omp_clauses *thread_limit_clauses
    9586           34 :         = (teams_clauses && teams_clauses->thread_limit_list
    9587           46 :            ? teams_clauses : code->ext.omp_clauses);
    9588           80 :       tree c, t, thread_limit = NULL_TREE;
    9589           80 :       for (gfc_expr_list *el = thread_limit_clauses->thread_limit_list;
    9590          164 :            el; el = el->next)
    9591              :         {
    9592           84 :           t = gfc_convert_expr_to_tree (&block, el->expr);
    9593           84 :           if (DECL_P (t))
    9594              :             {
    9595           54 :               c = build_omp_clause (gfc_get_location (&el->expr->where),
    9596              :                                     OMP_CLAUSE_FIRSTPRIVATE);
    9597           54 :               OMP_CLAUSE_DECL (c) = t;
    9598           54 :               tree_tgt_clauses = gfc_trans_add_clause (c, tree_tgt_clauses);
    9599              :             }
    9600           84 :           thread_limit = tree_cons (NULL_TREE, t, thread_limit);
    9601              :         }
    9602           80 :       locus *loc = &thread_limit_clauses->thread_limit_list->expr->where;
    9603           80 :       c = build_omp_clause (gfc_get_location (loc), OMP_CLAUSE_THREAD_LIMIT);
    9604           80 :       OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = nreverse (thread_limit);
    9605           80 :       OMP_CLAUSE_THREAD_LIMIT_STRICT (c)
    9606           80 :         = thread_limit_clauses->thread_limit_strict;
    9607           80 :       OMP_CLAUSE_THREAD_LIMIT_DIMS (c)
    9608           80 :         = thread_limit_clauses->thread_limit_dims;
    9609           80 :       tree_tgt_clauses = gfc_trans_add_clause (c, tree_tgt_clauses);
    9610           80 :       tree_teams_clauses = gfc_trans_add_clause (unshare_expr (c),
    9611              :                                                  tree_teams_clauses);
    9612           80 :       gfc_free_expr_list (thread_limit_clauses->thread_limit_list);
    9613           80 :       thread_limit_clauses->thread_limit_list = NULL;
    9614           80 :       gfc_free_expr_list (code->ext.omp_clauses->thread_limit_list);
    9615           80 :       code->ext.omp_clauses->thread_limit_list = NULL;
    9616              :     }
    9617         2467 :   if ((teams_clauses && teams_clauses->dyn_groupprivate)
    9618         2461 :       || (combined_teams_dir && code->ext.omp_clauses->dyn_groupprivate))
    9619              :     {
    9620              :       /* dyn_groupprivate can be put on target and on teams, use the team one as
    9621              :          having higher precedence.  */
    9622            6 :       gfc_omp_clauses *dyn_gp_clauses
    9623            6 :         = (teams_clauses && teams_clauses->dyn_groupprivate
    9624            6 :            ? teams_clauses : code->ext.omp_clauses);
    9625           12 :       gfc_omp_clauses tmp = {};
    9626           12 :       tmp.dyn_groupprivate = dyn_gp_clauses->dyn_groupprivate;
    9627           12 :       tmp.fallback = dyn_gp_clauses->fallback;
    9628           12 :       tree c = gfc_trans_omp_clauses (&block, &tmp, code->loc);
    9629           12 :       if (DECL_P (OMP_CLAUSE_DYN_GROUPPRIVATE_EXPR (c)))
    9630              :         {
    9631            0 :           tree c2;
    9632            0 :           OMP_CLAUSE_DYN_GROUPPRIVATE_EXPR (c)
    9633            0 :             = gfc_evaluate_now (OMP_CLAUSE_DYN_GROUPPRIVATE_EXPR (c), &block);
    9634            0 :           c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
    9635              :                                  OMP_CLAUSE_FIRSTPRIVATE);
    9636            0 :           OMP_CLAUSE_DECL (c2) = OMP_CLAUSE_DYN_GROUPPRIVATE_EXPR (c);
    9637            0 :           tree_tgt_clauses = gfc_trans_add_clause (c2, tree_tgt_clauses);
    9638              :         }
    9639              :       /* There is no need to put the clause also on teams.  */
    9640           12 :       tree_tgt_clauses = gfc_trans_add_clause (c, tree_tgt_clauses);
    9641           12 :       gfc_free_expr (dyn_gp_clauses->dyn_groupprivate);
    9642           12 :       dyn_gp_clauses->dyn_groupprivate = NULL;
    9643           12 :       gfc_free_expr (code->ext.omp_clauses->dyn_groupprivate);
    9644           12 :       code->ext.omp_clauses->dyn_groupprivate = NULL;
    9645              :     }
    9646              :   /* FIXME: also 'if' and 'allocate' are target consistent.  */
    9647              : 
    9648         2467 :   gfc_split_omp_clauses (code, clausesa);
    9649         2467 :   if (flag_openmp)
    9650         2467 :     omp_clauses
    9651         2467 :       = chainon (gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_TARGET],
    9652              :                                         code->loc), tree_tgt_clauses);
    9653         2467 :   switch (code->op)
    9654              :     {
    9655         2080 :     case EXEC_OMP_TARGET:
    9656         2080 :       pushlevel ();
    9657         2080 :       if (tree_teams_clauses)
    9658              :         {
    9659           54 :           gfc_code *code2 = code->block->next;
    9660           54 :           if (code2->op == EXEC_BLOCK)
    9661              :             {
    9662            0 :               gcc_checking_assert (!code2->ext.block.ns->sym_root->left
    9663              :                                    && !code2->ext.block.ns->sym_root->right);
    9664            0 :               code2 = code2->ext.block.ns->code;
    9665              :             }
    9666           54 :           gcc_checking_assert (code2->next == NULL);
    9667          108 :           stmt = gfc_trans_omp_teams (code2,
    9668              :                                       combined_teams_dir ? clausesa : NULL,
    9669              :                                       tree_teams_clauses);
    9670              :         }
    9671              :       else
    9672         2026 :         stmt = gfc_trans_omp_code (code->block->next, true);
    9673         2080 :       stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9674         2080 :       break;
    9675           26 :     case EXEC_OMP_TARGET_PARALLEL:
    9676           26 :       {
    9677           26 :         stmtblock_t iblock;
    9678              : 
    9679           26 :         pushlevel ();
    9680           26 :         gfc_start_block (&iblock);
    9681           26 :         tree inner_clauses
    9682           26 :           = gfc_trans_omp_clauses (&iblock, &clausesa[GFC_OMP_SPLIT_PARALLEL],
    9683              :                                    code->loc);
    9684           26 :         stmt = gfc_trans_omp_code (code->block->next, true);
    9685           26 :         stmt = build2_loc (input_location, OMP_PARALLEL, void_type_node, stmt,
    9686              :                            inner_clauses);
    9687           26 :         gfc_add_expr_to_block (&iblock, stmt);
    9688           26 :         stmt = gfc_finish_block (&iblock);
    9689           26 :         if (TREE_CODE (stmt) != BIND_EXPR)
    9690           23 :           stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9691              :         else
    9692            3 :           poplevel (0, 0);
    9693              :       }
    9694           26 :       break;
    9695           82 :     case EXEC_OMP_TARGET_PARALLEL_DO:
    9696           82 :     case EXEC_OMP_TARGET_PARALLEL_LOOP:
    9697           82 :       stmt = gfc_trans_omp_parallel_do (code,
    9698              :                                         (code->op
    9699              :                                          == EXEC_OMP_TARGET_PARALLEL_LOOP),
    9700              :                                         &block, clausesa);
    9701           82 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9702           82 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9703              :       else
    9704            0 :         poplevel (0, 0);
    9705              :       break;
    9706           17 :     case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
    9707           17 :       stmt = gfc_trans_omp_parallel_do_simd (code, &block, clausesa);
    9708           17 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9709           17 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9710              :       else
    9711            0 :         poplevel (0, 0);
    9712              :       break;
    9713           27 :     case EXEC_OMP_TARGET_SIMD:
    9714           27 :       stmt = gfc_trans_omp_do (code, EXEC_OMP_SIMD, &block,
    9715              :                                &clausesa[GFC_OMP_SPLIT_SIMD], NULL_TREE);
    9716           27 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9717           27 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9718              :       else
    9719            0 :         poplevel (0, 0);
    9720              :       break;
    9721          235 :     default:
    9722          235 :       pushlevel ();
    9723          235 :       stmt = gfc_trans_omp_teams (code, clausesa, tree_teams_clauses);
    9724          235 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9725          235 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9726              :       else
    9727            0 :         poplevel (0, 0);
    9728              :       break;
    9729              :     }
    9730         2467 :   if (flag_openmp)
    9731              :     {
    9732         2467 :       stmt = build2_loc (gfc_get_location (&code->loc), OMP_TARGET,
    9733              :                          void_type_node, stmt, omp_clauses);
    9734         2467 :       if (code->op != EXEC_OMP_TARGET)
    9735          387 :         OMP_TARGET_COMBINED (stmt) = 1;
    9736         2467 :       cfun->has_omp_target = true;
    9737              :     }
    9738         2467 :   gfc_add_expr_to_block (&block, stmt);
    9739         2467 :   gfc_free_split_omp_clauses (code, clausesa);
    9740         2467 :   return gfc_finish_block (&block);
    9741              : }
    9742              : 
    9743              : static tree
    9744           79 : gfc_trans_omp_taskloop (gfc_code *code, gfc_exec_op op)
    9745              : {
    9746           79 :   stmtblock_t block;
    9747           79 :   gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM];
    9748           79 :   tree stmt, omp_clauses = NULL_TREE;
    9749              : 
    9750           79 :   gfc_start_block (&block);
    9751           79 :   gfc_split_omp_clauses (code, clausesa);
    9752           79 :   if (flag_openmp)
    9753           79 :     omp_clauses
    9754           79 :       = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_TASKLOOP],
    9755              :                                code->loc);
    9756           79 :   switch (op)
    9757              :     {
    9758            0 :     case EXEC_OMP_TASKLOOP:
    9759              :       /* This is handled in gfc_trans_omp_do.  */
    9760            0 :       gcc_unreachable ();
    9761           79 :       break;
    9762           79 :     case EXEC_OMP_TASKLOOP_SIMD:
    9763           79 :       stmt = gfc_trans_omp_do (code, EXEC_OMP_SIMD, &block,
    9764              :                                &clausesa[GFC_OMP_SPLIT_SIMD], NULL_TREE);
    9765           79 :       if (TREE_CODE (stmt) != BIND_EXPR)
    9766           79 :         stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9767              :       else
    9768            0 :         poplevel (0, 0);
    9769           79 :       break;
    9770            0 :     default:
    9771            0 :       gcc_unreachable ();
    9772              :     }
    9773           79 :   if (flag_openmp)
    9774              :     {
    9775           79 :       tree taskloop = make_node (OMP_TASKLOOP);
    9776           79 :       SET_EXPR_LOCATION (taskloop, gfc_get_location (&code->loc));
    9777           79 :       TREE_TYPE (taskloop) = void_type_node;
    9778           79 :       OMP_FOR_BODY (taskloop) = stmt;
    9779           79 :       OMP_FOR_CLAUSES (taskloop) = omp_clauses;
    9780           79 :       stmt = taskloop;
    9781              :     }
    9782           79 :   gfc_add_expr_to_block (&block, stmt);
    9783           79 :   gfc_free_split_omp_clauses (code, clausesa);
    9784           79 :   return gfc_finish_block (&block);
    9785              : }
    9786              : 
    9787              : static tree
    9788           84 : gfc_trans_omp_master_masked_taskloop (gfc_code *code, gfc_exec_op op)
    9789              : {
    9790           84 :   gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM];
    9791           84 :   stmtblock_t block;
    9792           84 :   tree stmt;
    9793              : 
    9794           84 :   if (op != EXEC_OMP_MASTER_TASKLOOP_SIMD
    9795           56 :       && code->op != EXEC_OMP_MASTER_TASKLOOP)
    9796           45 :     gfc_split_omp_clauses (code, clausesa);
    9797              : 
    9798           84 :   pushlevel ();
    9799           84 :   if (op == EXEC_OMP_MASKED_TASKLOOP_SIMD
    9800           84 :       || op == EXEC_OMP_MASTER_TASKLOOP_SIMD)
    9801           48 :     stmt = gfc_trans_omp_taskloop (code, EXEC_OMP_TASKLOOP_SIMD);
    9802              :   else
    9803              :     {
    9804           36 :       gcc_assert (op == EXEC_OMP_MASKED_TASKLOOP
    9805              :                   || op == EXEC_OMP_MASTER_TASKLOOP);
    9806           36 :       stmt = gfc_trans_omp_do (code, EXEC_OMP_TASKLOOP, NULL,
    9807           36 :                                code->op != EXEC_OMP_MASTER_TASKLOOP
    9808              :                                ? &clausesa[GFC_OMP_SPLIT_TASKLOOP]
    9809              :                                : code->ext.omp_clauses, NULL);
    9810              :     }
    9811           84 :   if (TREE_CODE (stmt) != BIND_EXPR)
    9812           55 :     stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9813              :   else
    9814           29 :     poplevel (0, 0);
    9815           84 :   gfc_start_block (&block);
    9816           84 :   if (op == EXEC_OMP_MASKED_TASKLOOP || op == EXEC_OMP_MASKED_TASKLOOP_SIMD)
    9817              :     {
    9818           35 :       tree clauses = gfc_trans_omp_clauses (&block,
    9819              :                                             &clausesa[GFC_OMP_SPLIT_MASKED],
    9820              :                                             code->loc);
    9821           35 :       tree msk = make_node (OMP_MASKED);
    9822           35 :       SET_EXPR_LOCATION (msk, gfc_get_location (&code->loc));
    9823           35 :       TREE_TYPE (msk) = void_type_node;
    9824           35 :       OMP_MASKED_BODY (msk) = stmt;
    9825           35 :       OMP_MASKED_CLAUSES (msk) = clauses;
    9826           35 :       OMP_MASKED_COMBINED (msk) = 1;
    9827           35 :       gfc_add_expr_to_block (&block, msk);
    9828              :     }
    9829              :   else
    9830              :     {
    9831           49 :       gcc_assert (op == EXEC_OMP_MASTER_TASKLOOP
    9832              :                   || op == EXEC_OMP_MASTER_TASKLOOP_SIMD);
    9833           49 :       stmt = build1_v (OMP_MASTER, stmt);
    9834           49 :       gfc_add_expr_to_block (&block, stmt);
    9835              :     }
    9836           84 :   if (op != EXEC_OMP_MASTER_TASKLOOP_SIMD
    9837           56 :       && code->op != EXEC_OMP_MASTER_TASKLOOP)
    9838           45 :     gfc_free_split_omp_clauses (code, clausesa);
    9839           84 :   return gfc_finish_block (&block);
    9840              : }
    9841              : 
    9842              : static tree
    9843           61 : gfc_trans_omp_parallel_master_masked (gfc_code *code)
    9844              : {
    9845           61 :   stmtblock_t block;
    9846           61 :   tree stmt, omp_clauses;
    9847           61 :   gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM];
    9848           61 :   bool parallel_combined = false;
    9849              : 
    9850           61 :   if (code->op != EXEC_OMP_PARALLEL_MASTER)
    9851           50 :     gfc_split_omp_clauses (code, clausesa);
    9852              : 
    9853           61 :   gfc_start_block (&block);
    9854           61 :   omp_clauses = gfc_trans_omp_clauses (&block,
    9855           61 :                                        code->op == EXEC_OMP_PARALLEL_MASTER
    9856              :                                        ? code->ext.omp_clauses
    9857              :                                        : &clausesa[GFC_OMP_SPLIT_PARALLEL],
    9858              :                                        code->loc);
    9859           61 :   pushlevel ();
    9860           61 :   if (code->op == EXEC_OMP_PARALLEL_MASTER)
    9861           11 :     stmt = gfc_trans_omp_master (code);
    9862           50 :   else if (code->op == EXEC_OMP_PARALLEL_MASKED)
    9863           11 :     stmt = gfc_trans_omp_masked (code, &clausesa[GFC_OMP_SPLIT_MASKED]);
    9864              :   else
    9865              :     {
    9866           39 :       gfc_exec_op op;
    9867           39 :       switch (code->op)
    9868              :         {
    9869              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
    9870              :           op = EXEC_OMP_MASKED_TASKLOOP;
    9871              :           break;
    9872            8 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
    9873            8 :           op = EXEC_OMP_MASKED_TASKLOOP_SIMD;
    9874            8 :           break;
    9875           10 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
    9876           10 :           op = EXEC_OMP_MASTER_TASKLOOP;
    9877           10 :           break;
    9878           14 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
    9879           14 :           op = EXEC_OMP_MASTER_TASKLOOP_SIMD;
    9880           14 :           break;
    9881            0 :         default:
    9882            0 :           gcc_unreachable ();
    9883              :         }
    9884           39 :       stmt = gfc_trans_omp_master_masked_taskloop (code, op);
    9885           39 :       parallel_combined = true;
    9886              :     }
    9887           61 :   if (TREE_CODE (stmt) != BIND_EXPR)
    9888           48 :     stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
    9889              :   else
    9890           13 :     poplevel (0, 0);
    9891           61 :   stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
    9892              :                      void_type_node, stmt, omp_clauses);
    9893              :   /* masked does have just filter clause, but during gimplification
    9894              :      isn't represented by a gimplification omp context, so for
    9895              :        !$omp parallel masked don't set OMP_PARALLEL_COMBINED,
    9896              :      so that
    9897              :        !$omp parallel masked
    9898              :        !$omp taskloop simd lastprivate (x)
    9899              :      isn't confused with
    9900              :        !$omp parallel masked taskloop simd lastprivate (x)  */
    9901           61 :   if (parallel_combined)
    9902           39 :     OMP_PARALLEL_COMBINED (stmt) = 1;
    9903           61 :   gfc_add_expr_to_block (&block, stmt);
    9904           61 :   if (code->op != EXEC_OMP_PARALLEL_MASTER)
    9905           50 :     gfc_free_split_omp_clauses (code, clausesa);
    9906           61 :   return gfc_finish_block (&block);
    9907              : }
    9908              : 
    9909              : static tree
    9910         1390 : gfc_trans_omp_target_data (gfc_code *code)
    9911              : {
    9912         1390 :   stmtblock_t block;
    9913         1390 :   tree stmt, omp_clauses;
    9914              : 
    9915         1390 :   gfc_start_block (&block);
    9916         1390 :   omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    9917              :                                        code->loc);
    9918         1390 :   stmt = gfc_trans_omp_code (code->block->next, true);
    9919         1390 :   stmt = build2_loc (gfc_get_location (&code->loc), OMP_TARGET_DATA,
    9920              :                      void_type_node, stmt, omp_clauses);
    9921         1390 :   gfc_add_expr_to_block (&block, stmt);
    9922         1390 :   return gfc_finish_block (&block);
    9923              : }
    9924              : 
    9925              : static tree
    9926          440 : gfc_trans_omp_target_enter_data (gfc_code *code)
    9927              : {
    9928          440 :   stmtblock_t block;
    9929          440 :   tree stmt, omp_clauses;
    9930              : 
    9931          440 :   gfc_start_block (&block);
    9932          440 :   omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    9933              :                                        code->loc);
    9934          440 :   stmt = build1_loc (input_location, OMP_TARGET_ENTER_DATA, void_type_node,
    9935              :                      omp_clauses);
    9936          440 :   gfc_add_expr_to_block (&block, stmt);
    9937          440 :   return gfc_finish_block (&block);
    9938              : }
    9939              : 
    9940              : static tree
    9941          362 : gfc_trans_omp_target_exit_data (gfc_code *code)
    9942              : {
    9943          362 :   stmtblock_t block;
    9944          362 :   tree stmt, omp_clauses;
    9945              : 
    9946          362 :   gfc_start_block (&block);
    9947          362 :   omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    9948              :                                        code->loc, false, false, code->op);
    9949          362 :   stmt = build1_loc (input_location, OMP_TARGET_EXIT_DATA, void_type_node,
    9950              :                      omp_clauses);
    9951          362 :   gfc_add_expr_to_block (&block, stmt);
    9952          362 :   return gfc_finish_block (&block);
    9953              : }
    9954              : 
    9955              : static tree
    9956         1777 : gfc_trans_omp_target_update (gfc_code *code)
    9957              : {
    9958         1777 :   stmtblock_t block;
    9959         1777 :   tree stmt, omp_clauses;
    9960              : 
    9961         1777 :   gfc_start_block (&block);
    9962         1777 :   omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
    9963              :                                        code->loc);
    9964         1777 :   stmt = build1_loc (input_location, OMP_TARGET_UPDATE, void_type_node,
    9965              :                      omp_clauses);
    9966         1777 :   gfc_add_expr_to_block (&block, stmt);
    9967         1777 :   return gfc_finish_block (&block);
    9968              : }
    9969              : 
    9970              : static tree
    9971            8 : gfc_trans_openmp_interop (gfc_code *code, gfc_omp_clauses *clauses)
    9972              : {
    9973            8 :   stmtblock_t block;
    9974            8 :   gfc_start_block (&block);
    9975            8 :   tree omp_clauses = gfc_trans_omp_clauses (&block, clauses, code->loc);
    9976            8 :   tree stmt = build1_loc (input_location, OMP_INTEROP, void_type_node,
    9977              :                           omp_clauses);
    9978            8 :   gfc_add_expr_to_block (&block, stmt);
    9979            8 :   return gfc_finish_block (&block);
    9980              : }
    9981              : 
    9982              : static tree
    9983           85 : gfc_trans_omp_workshare (gfc_code *code, gfc_omp_clauses *clauses)
    9984              : {
    9985           85 :   tree res, tmp, stmt;
    9986           85 :   stmtblock_t block, *pblock = NULL;
    9987           85 :   stmtblock_t singleblock;
    9988           85 :   int saved_ompws_flags;
    9989           85 :   bool singleblock_in_progress = false;
    9990              :   /* True if previous gfc_code in workshare construct is not workshared.  */
    9991           85 :   bool prev_singleunit;
    9992           85 :   location_t loc = gfc_get_location (&code->loc);
    9993              : 
    9994           85 :   code = code->block->next;
    9995              : 
    9996           85 :   pushlevel ();
    9997              : 
    9998           85 :   gfc_start_block (&block);
    9999           85 :   pblock = &block;
   10000              : 
   10001           85 :   ompws_flags = OMPWS_WORKSHARE_FLAG;
   10002           85 :   prev_singleunit = false;
   10003              : 
   10004              :   /* Translate statements one by one to trees until we reach
   10005              :      the end of the workshare construct.  Adjacent gfc_codes that
   10006              :      are a single unit of work are clustered and encapsulated in a
   10007              :      single OMP_SINGLE construct.  */
   10008          282 :   for (; code; code = code->next)
   10009              :     {
   10010          197 :       if (code->here != 0)
   10011              :         {
   10012            0 :           res = gfc_trans_label_here (code);
   10013            0 :           gfc_add_expr_to_block (pblock, res);
   10014              :         }
   10015              : 
   10016              :       /* No dependence analysis, use for clauses with wait.
   10017              :          If this is the last gfc_code, use default omp_clauses.  */
   10018          197 :       if (code->next == NULL && clauses->nowait)
   10019           60 :         ompws_flags |= OMPWS_NOWAIT;
   10020              : 
   10021              :       /* By default, every gfc_code is a single unit of work.  */
   10022          197 :       ompws_flags |= OMPWS_CURR_SINGLEUNIT;
   10023          197 :       ompws_flags &= ~(OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY);
   10024              : 
   10025          197 :       switch (code->op)
   10026              :         {
   10027              :         case EXEC_NOP:
   10028              :           res = NULL_TREE;
   10029              :           break;
   10030              : 
   10031          125 :         case EXEC_ASSIGN:
   10032          125 :           res = gfc_trans_assign (code);
   10033          125 :           break;
   10034              : 
   10035            0 :         case EXEC_POINTER_ASSIGN:
   10036            0 :           res = gfc_trans_pointer_assign (code);
   10037            0 :           break;
   10038              : 
   10039            0 :         case EXEC_INIT_ASSIGN:
   10040            0 :           res = gfc_trans_init_assign (code);
   10041            0 :           break;
   10042              : 
   10043           24 :         case EXEC_FORALL:
   10044           24 :           res = gfc_trans_forall (code);
   10045           24 :           break;
   10046              : 
   10047           19 :         case EXEC_WHERE:
   10048           19 :           res = gfc_trans_where (code);
   10049           19 :           break;
   10050              : 
   10051            7 :         case EXEC_OMP_ATOMIC:
   10052            7 :           res = gfc_trans_omp_directive (code);
   10053            7 :           break;
   10054              : 
   10055           17 :         case EXEC_OMP_PARALLEL:
   10056           17 :         case EXEC_OMP_PARALLEL_DO:
   10057           17 :         case EXEC_OMP_PARALLEL_MASTER:
   10058           17 :         case EXEC_OMP_PARALLEL_SECTIONS:
   10059           17 :         case EXEC_OMP_PARALLEL_WORKSHARE:
   10060           17 :         case EXEC_OMP_CRITICAL:
   10061           17 :           saved_ompws_flags = ompws_flags;
   10062           17 :           ompws_flags = 0;
   10063           17 :           res = gfc_trans_omp_directive (code);
   10064           17 :           ompws_flags = saved_ompws_flags;
   10065           17 :           break;
   10066              : 
   10067            5 :         case EXEC_BLOCK:
   10068            5 :           res = gfc_trans_block_construct (code);
   10069            5 :           break;
   10070              : 
   10071            0 :         default:
   10072            0 :           gfc_internal_error ("gfc_trans_omp_workshare(): Bad statement code");
   10073              :         }
   10074              : 
   10075          197 :       input_location = gfc_get_location (&code->loc);
   10076              : 
   10077          197 :       if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
   10078              :         {
   10079          197 :           if (prev_singleunit)
   10080              :             {
   10081           81 :               if (ompws_flags & OMPWS_CURR_SINGLEUNIT)
   10082              :                 /* Add current gfc_code to single block.  */
   10083           53 :                 gfc_add_expr_to_block (&singleblock, res);
   10084              :               else
   10085              :                 {
   10086              :                   /* Finish single block and add it to pblock.  */
   10087           28 :                   tmp = gfc_finish_block (&singleblock);
   10088           28 :                   tmp = build2_loc (loc, OMP_SINGLE,
   10089              :                                     void_type_node, tmp, NULL_TREE);
   10090           28 :                   gfc_add_expr_to_block (pblock, tmp);
   10091              :                   /* Add current gfc_code to pblock.  */
   10092           28 :                   gfc_add_expr_to_block (pblock, res);
   10093           28 :                   singleblock_in_progress = false;
   10094              :                 }
   10095              :             }
   10096              :           else
   10097              :             {
   10098          116 :               if (ompws_flags & OMPWS_CURR_SINGLEUNIT)
   10099              :                 {
   10100              :                   /* Start single block.  */
   10101           79 :                   gfc_init_block (&singleblock);
   10102           79 :                   gfc_add_expr_to_block (&singleblock, res);
   10103           79 :                   singleblock_in_progress = true;
   10104           79 :                   loc = gfc_get_location (&code->loc);
   10105              :                 }
   10106              :               else
   10107              :                 /* Add the new statement to the block.  */
   10108           37 :                 gfc_add_expr_to_block (pblock, res);
   10109              :             }
   10110          197 :           prev_singleunit = (ompws_flags & OMPWS_CURR_SINGLEUNIT) != 0;
   10111              :         }
   10112              :     }
   10113              : 
   10114              :   /* Finish remaining SINGLE block, if we were in the middle of one.  */
   10115           85 :   if (singleblock_in_progress)
   10116              :     {
   10117              :       /* Finish single block and add it to pblock.  */
   10118           51 :       tmp = gfc_finish_block (&singleblock);
   10119           51 :       tmp = build2_loc (loc, OMP_SINGLE, void_type_node, tmp,
   10120           51 :                         clauses->nowait
   10121           28 :                         ? build_omp_clause (input_location, OMP_CLAUSE_NOWAIT)
   10122              :                         : NULL_TREE);
   10123           51 :       gfc_add_expr_to_block (pblock, tmp);
   10124              :     }
   10125              : 
   10126           85 :   stmt = gfc_finish_block (pblock);
   10127           85 :   if (TREE_CODE (stmt) != BIND_EXPR)
   10128              :     {
   10129           52 :       if (!IS_EMPTY_STMT (stmt))
   10130              :         {
   10131           52 :           tree bindblock = poplevel (1, 0);
   10132           52 :           stmt = build3_v (BIND_EXPR, NULL, stmt, bindblock);
   10133              :         }
   10134              :       else
   10135            0 :         poplevel (0, 0);
   10136              :     }
   10137              :   else
   10138           33 :     poplevel (0, 0);
   10139              : 
   10140           85 :   if (IS_EMPTY_STMT (stmt) && !clauses->nowait)
   10141            0 :     stmt = gfc_trans_omp_barrier ();
   10142              : 
   10143           85 :   ompws_flags = 0;
   10144           85 :   return stmt;
   10145              : }
   10146              : 
   10147              : tree
   10148           76 : gfc_trans_oacc_declare (gfc_code *code)
   10149              : {
   10150           76 :   stmtblock_t block;
   10151           76 :   tree stmt, oacc_clauses;
   10152           76 :   enum tree_code construct_code;
   10153              : 
   10154           76 :   construct_code = OACC_DATA;
   10155              : 
   10156           76 :   gfc_start_block (&block);
   10157              : 
   10158           76 :   oacc_clauses = gfc_trans_omp_clauses (&block, code->ext.oacc_declare->clauses,
   10159              :                                         code->loc, false, true);
   10160           76 :   stmt = gfc_trans_omp_code (code->block->next, true);
   10161           76 :   stmt = build2_loc (input_location, construct_code, void_type_node, stmt,
   10162              :                      oacc_clauses);
   10163           76 :   gfc_add_expr_to_block (&block, stmt);
   10164              : 
   10165           76 :   return gfc_finish_block (&block);
   10166              : }
   10167              : 
   10168              : tree
   10169        12412 : gfc_trans_oacc_directive (gfc_code *code)
   10170              : {
   10171        12412 :   switch (code->op)
   10172              :     {
   10173         1556 :     case EXEC_OACC_PARALLEL_LOOP:
   10174         1556 :     case EXEC_OACC_KERNELS_LOOP:
   10175         1556 :     case EXEC_OACC_SERIAL_LOOP:
   10176         1556 :       return gfc_trans_oacc_combined_directive (code);
   10177         4189 :     case EXEC_OACC_PARALLEL:
   10178         4189 :     case EXEC_OACC_KERNELS:
   10179         4189 :     case EXEC_OACC_SERIAL:
   10180         4189 :     case EXEC_OACC_DATA:
   10181         4189 :     case EXEC_OACC_HOST_DATA:
   10182         4189 :       return gfc_trans_oacc_construct (code);
   10183         3377 :     case EXEC_OACC_LOOP:
   10184         3377 :       return gfc_trans_omp_do (code, code->op, NULL, code->ext.omp_clauses,
   10185         3377 :                                NULL);
   10186         2498 :     case EXEC_OACC_UPDATE:
   10187         2498 :     case EXEC_OACC_CACHE:
   10188         2498 :     case EXEC_OACC_ENTER_DATA:
   10189         2498 :     case EXEC_OACC_EXIT_DATA:
   10190         2498 :     case EXEC_OACC_INIT:
   10191         2498 :     case EXEC_OACC_SHUTDOWN:
   10192         2498 :     case EXEC_OACC_SET:
   10193         2498 :       return gfc_trans_oacc_executable_directive (code);
   10194          173 :     case EXEC_OACC_WAIT:
   10195          173 :       return gfc_trans_oacc_wait_directive (code);
   10196          543 :     case EXEC_OACC_ATOMIC:
   10197          543 :       return gfc_trans_omp_atomic (code);
   10198           76 :     case EXEC_OACC_DECLARE:
   10199           76 :       return gfc_trans_oacc_declare (code);
   10200            0 :     default:
   10201            0 :       gcc_unreachable ();
   10202              :     }
   10203              : }
   10204              : 
   10205              : tree
   10206        19816 : gfc_trans_omp_directive (gfc_code *code)
   10207              : {
   10208        19816 :   switch (code->op)
   10209              :     {
   10210           35 :     case EXEC_OMP_ALLOCATE:
   10211           35 :     case EXEC_OMP_ALLOCATORS:
   10212           35 :       return gfc_trans_omp_allocators (code);
   10213           12 :     case EXEC_OMP_ASSUME:
   10214           12 :       return gfc_trans_omp_assume (code);
   10215         2064 :     case EXEC_OMP_ATOMIC:
   10216         2064 :       return gfc_trans_omp_atomic (code);
   10217          604 :     case EXEC_OMP_BARRIER:
   10218          604 :       return gfc_trans_omp_barrier ();
   10219          315 :     case EXEC_OMP_CANCEL:
   10220          315 :       return gfc_trans_omp_cancel (code);
   10221          175 :     case EXEC_OMP_CANCELLATION_POINT:
   10222          175 :       return gfc_trans_omp_cancellation_point (code);
   10223          149 :     case EXEC_OMP_CRITICAL:
   10224          149 :       return gfc_trans_omp_critical (code);
   10225          111 :     case EXEC_OMP_DEPOBJ:
   10226          111 :       return gfc_trans_omp_depobj (code);
   10227         2554 :     case EXEC_OMP_DISTRIBUTE:
   10228         2554 :     case EXEC_OMP_DO:
   10229         2554 :     case EXEC_OMP_LOOP:
   10230         2554 :     case EXEC_OMP_SIMD:
   10231         2554 :     case EXEC_OMP_TASKLOOP:
   10232         2554 :     case EXEC_OMP_TILE:
   10233         2554 :     case EXEC_OMP_UNROLL:
   10234         2554 :       return gfc_trans_omp_do (code, code->op, NULL, code->ext.omp_clauses,
   10235         2554 :                                NULL);
   10236          128 :     case EXEC_OMP_DISPATCH:
   10237          128 :       return gfc_trans_omp_dispatch (code);
   10238          114 :     case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   10239          114 :     case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   10240          114 :     case EXEC_OMP_DISTRIBUTE_SIMD:
   10241          114 :       return gfc_trans_omp_distribute (code, NULL);
   10242          146 :     case EXEC_OMP_DO_SIMD:
   10243          146 :       return gfc_trans_omp_do_simd (code, NULL, NULL, NULL_TREE);
   10244           31 :     case EXEC_OMP_ERROR:
   10245           31 :       return gfc_trans_omp_error (code);
   10246           79 :     case EXEC_OMP_FLUSH:
   10247           79 :       return gfc_trans_omp_flush (code);
   10248           44 :     case EXEC_OMP_MASKED:
   10249           44 :       return gfc_trans_omp_masked (code, NULL);
   10250          105 :     case EXEC_OMP_MASTER:
   10251          105 :       return gfc_trans_omp_master (code);
   10252           45 :     case EXEC_OMP_MASKED_TASKLOOP:
   10253           45 :     case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   10254           45 :     case EXEC_OMP_MASTER_TASKLOOP:
   10255           45 :     case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   10256           45 :       return gfc_trans_omp_master_masked_taskloop (code, code->op);
   10257           94 :     case EXEC_OMP_METADIRECTIVE:
   10258           94 :       return gfc_trans_omp_metadirective (code);
   10259          525 :     case EXEC_OMP_ORDERED:
   10260          525 :       return gfc_trans_omp_ordered (code);
   10261         1944 :     case EXEC_OMP_PARALLEL:
   10262         1944 :       return gfc_trans_omp_parallel (code);
   10263         1096 :     case EXEC_OMP_PARALLEL_DO:
   10264         1096 :       return gfc_trans_omp_parallel_do (code, false, NULL, NULL);
   10265           24 :     case EXEC_OMP_PARALLEL_LOOP:
   10266           24 :       return gfc_trans_omp_parallel_do (code, true, NULL, NULL);
   10267          332 :     case EXEC_OMP_PARALLEL_DO_SIMD:
   10268          332 :       return gfc_trans_omp_parallel_do_simd (code, NULL, NULL);
   10269           61 :     case EXEC_OMP_PARALLEL_MASKED:
   10270           61 :     case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   10271           61 :     case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   10272           61 :     case EXEC_OMP_PARALLEL_MASTER:
   10273           61 :     case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   10274           61 :     case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   10275           61 :       return gfc_trans_omp_parallel_master_masked (code);
   10276           54 :     case EXEC_OMP_PARALLEL_SECTIONS:
   10277           54 :       return gfc_trans_omp_parallel_sections (code);
   10278           50 :     case EXEC_OMP_PARALLEL_WORKSHARE:
   10279           50 :       return gfc_trans_omp_parallel_workshare (code);
   10280           53 :     case EXEC_OMP_SCOPE:
   10281           53 :       return gfc_trans_omp_scope (code);
   10282           75 :     case EXEC_OMP_SECTIONS:
   10283           75 :       return gfc_trans_omp_sections (code, code->ext.omp_clauses);
   10284          560 :     case EXEC_OMP_SINGLE:
   10285          560 :       return gfc_trans_omp_single (code, code->ext.omp_clauses);
   10286         2467 :     case EXEC_OMP_TARGET:
   10287         2467 :     case EXEC_OMP_TARGET_PARALLEL:
   10288         2467 :     case EXEC_OMP_TARGET_PARALLEL_DO:
   10289         2467 :     case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   10290         2467 :     case EXEC_OMP_TARGET_PARALLEL_LOOP:
   10291         2467 :     case EXEC_OMP_TARGET_SIMD:
   10292         2467 :     case EXEC_OMP_TARGET_TEAMS:
   10293         2467 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   10294         2467 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   10295         2467 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   10296         2467 :     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   10297         2467 :     case EXEC_OMP_TARGET_TEAMS_LOOP:
   10298         2467 :       return gfc_trans_omp_target (code);
   10299         1390 :     case EXEC_OMP_TARGET_DATA:
   10300         1390 :       return gfc_trans_omp_target_data (code);
   10301          440 :     case EXEC_OMP_TARGET_ENTER_DATA:
   10302          440 :       return gfc_trans_omp_target_enter_data (code);
   10303          362 :     case EXEC_OMP_TARGET_EXIT_DATA:
   10304          362 :       return gfc_trans_omp_target_exit_data (code);
   10305         1777 :     case EXEC_OMP_TARGET_UPDATE:
   10306         1777 :       return gfc_trans_omp_target_update (code);
   10307         1125 :     case EXEC_OMP_TASK:
   10308         1125 :       return gfc_trans_omp_task (code);
   10309          181 :     case EXEC_OMP_TASKGROUP:
   10310          181 :       return gfc_trans_omp_taskgroup (code);
   10311           31 :     case EXEC_OMP_TASKLOOP_SIMD:
   10312           31 :       return gfc_trans_omp_taskloop (code, code->op);
   10313          148 :     case EXEC_OMP_TASKWAIT:
   10314          148 :       return gfc_trans_omp_taskwait (code);
   10315            8 :     case EXEC_OMP_TASKYIELD:
   10316            8 :       return gfc_trans_omp_taskyield ();
   10317          265 :     case EXEC_OMP_TEAMS:
   10318          265 :     case EXEC_OMP_TEAMS_DISTRIBUTE:
   10319          265 :     case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   10320          265 :     case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   10321          265 :     case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   10322          265 :     case EXEC_OMP_TEAMS_LOOP:
   10323          265 :       return gfc_trans_omp_teams (code, NULL, NULL_TREE);
   10324           35 :     case EXEC_OMP_WORKSHARE:
   10325           35 :       return gfc_trans_omp_workshare (code, code->ext.omp_clauses);
   10326            8 :     case EXEC_OMP_INTEROP:
   10327            8 :       return gfc_trans_openmp_interop (code, code->ext.omp_clauses);
   10328            0 :     default:
   10329            0 :       gcc_unreachable ();
   10330              :     }
   10331              : }
   10332              : 
   10333              : void
   10334          119 : gfc_trans_omp_declare_simd (gfc_namespace *ns)
   10335              : {
   10336          119 :   if (ns->entries)
   10337              :     return;
   10338              : 
   10339          119 :   gfc_omp_declare_simd *ods;
   10340          282 :   for (ods = ns->omp_declare_simd; ods; ods = ods->next)
   10341              :     {
   10342          163 :       tree c = gfc_trans_omp_clauses (NULL, ods->clauses, ods->where, true);
   10343          163 :       tree fndecl = ns->proc_name->backend_decl;
   10344          163 :       if (c != NULL_TREE)
   10345          111 :         c = tree_cons (NULL_TREE, c, NULL_TREE);
   10346          163 :       c = build_tree_list (get_identifier ("omp declare simd"), c);
   10347          163 :       TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
   10348          163 :       DECL_ATTRIBUTES (fndecl) = c;
   10349              :     }
   10350              : }
   10351              : 
   10352              : /* Translate the context selector list GFC_SELECTORS, using WHERE as the
   10353              :    locus for error messages.  */
   10354              : 
   10355              : static tree
   10356          541 : gfc_trans_omp_set_selector (gfc_omp_set_selector *gfc_selectors, locus where)
   10357              : {
   10358          541 :   tree set_selectors = NULL_TREE;
   10359          541 :   gfc_omp_set_selector *oss;
   10360              : 
   10361         1056 :   for (oss = gfc_selectors; oss; oss = oss->next)
   10362              :     {
   10363          515 :       tree selectors = NULL_TREE;
   10364          515 :       gfc_omp_selector *os;
   10365          515 :       enum omp_tss_code set = oss->code;
   10366          515 :       gcc_assert (set != OMP_TRAIT_SET_INVALID);
   10367              : 
   10368         1158 :       for (os = oss->trait_selectors; os; os = os->next)
   10369              :         {
   10370          643 :           tree scoreval = NULL_TREE;
   10371          643 :           tree properties = NULL_TREE;
   10372          643 :           gfc_omp_trait_property *otp;
   10373          643 :           enum omp_ts_code sel = os->code;
   10374              : 
   10375              :           /* Per the spec, "Implementations can ignore specified
   10376              :              selectors that are not those described in this section";
   10377              :              however, we  must record such selectors because they
   10378              :              cause match failures.  */
   10379          643 :           if (sel == OMP_TRAIT_INVALID)
   10380              :             {
   10381            1 :               selectors = make_trait_selector (sel, NULL_TREE, NULL_TREE,
   10382              :                                                selectors);
   10383            1 :               continue;
   10384              :             }
   10385              : 
   10386         1033 :           for (otp = os->properties; otp; otp = otp->next)
   10387              :             {
   10388          391 :               switch (otp->property_kind)
   10389              :                 {
   10390           92 :                 case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
   10391           92 :                 case OMP_TRAIT_PROPERTY_BOOL_EXPR:
   10392           92 :                   {
   10393           92 :                     tree expr = NULL_TREE;
   10394           92 :                     gfc_se se;
   10395           92 :                     gfc_init_se (&se, NULL);
   10396           92 :                     gfc_conv_expr (&se, otp->expr);
   10397           92 :                     expr = se.expr;
   10398           92 :                     properties = make_trait_property (NULL_TREE, expr,
   10399              :                                                       properties);
   10400              :                   }
   10401           92 :                   break;
   10402           23 :                 case OMP_TRAIT_PROPERTY_ID:
   10403           23 :                   properties
   10404           23 :                     = make_trait_property (get_identifier (otp->name),
   10405              :                                            NULL_TREE, properties);
   10406           23 :                   break;
   10407          254 :                 case OMP_TRAIT_PROPERTY_NAME_LIST:
   10408          254 :                   {
   10409          254 :                     tree prop = OMP_TP_NAMELIST_NODE;
   10410          254 :                     tree value = NULL_TREE;
   10411          254 :                     if (otp->is_name)
   10412          167 :                       value = get_identifier (otp->name);
   10413              :                     else
   10414           87 :                       value = gfc_conv_constant_to_tree (otp->expr);
   10415              : 
   10416          254 :                     properties = make_trait_property (prop, value,
   10417              :                                                       properties);
   10418              :                   }
   10419          254 :                   break;
   10420           22 :                 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
   10421           22 :                   properties = gfc_trans_omp_clauses (NULL, otp->clauses,
   10422              :                                                       where, true);
   10423           22 :                   break;
   10424            0 :                 default:
   10425            0 :                   gcc_unreachable ();
   10426              :                 }
   10427              :             }
   10428              : 
   10429          642 :           if (os->score)
   10430              :             {
   10431           51 :               gfc_se se;
   10432           51 :               gfc_init_se (&se, NULL);
   10433           51 :               gfc_conv_expr (&se, os->score);
   10434           51 :               scoreval = se.expr;
   10435              :             }
   10436              : 
   10437          642 :           selectors = make_trait_selector (sel, scoreval,
   10438              :                                            properties, selectors);
   10439              :         }
   10440          515 :       set_selectors = make_trait_set_selector (set, selectors, set_selectors);
   10441              :     }
   10442          541 :   return set_selectors;
   10443              : }
   10444              : 
   10445              : /* If 'ns' points to a formal namespace in an interface, ns->parent == NULL;
   10446              :    hence, parent_ns is used instead.  */
   10447              : 
   10448              : void
   10449        10937 : gfc_trans_omp_declare_variant (gfc_namespace *ns, gfc_namespace *parent_ns)
   10450              : {
   10451        10937 :   tree base_fn_decl = ns->proc_name->backend_decl;
   10452        10937 :   gfc_namespace *search_ns = ns;
   10453        10937 :   gfc_omp_declare_variant *next;
   10454              : 
   10455        10937 :   for (gfc_omp_declare_variant *odv = search_ns->omp_declare_variant;
   10456        29702 :        search_ns; odv = next)
   10457              :     {
   10458              :       /* Look in the parent namespace if there are no more directives in the
   10459              :          current namespace.  */
   10460        18765 :       if (!odv)
   10461              :         {
   10462        18365 :           if (!search_ns->parent && search_ns == ns)
   10463              :             search_ns = parent_ns;
   10464              :           else
   10465        12938 :             search_ns = search_ns->parent;
   10466        18365 :           if (search_ns)
   10467         7428 :             next = search_ns->omp_declare_variant;
   10468        18365 :           continue;
   10469              :         }
   10470              : 
   10471          400 :       next = odv->next;
   10472              : 
   10473          400 :       if (odv->error_p)
   10474           17 :         continue;
   10475              : 
   10476              :       /* Check directive the first time it is encountered.  */
   10477          383 :       bool error_found = true;
   10478              : 
   10479          383 :       if (odv->checked_p)
   10480           43 :         error_found = false;
   10481          383 :       if (odv->base_proc_symtree == NULL)
   10482              :         {
   10483          346 :           if (!search_ns->proc_name->attr.function
   10484          227 :               && !search_ns->proc_name->attr.subroutine)
   10485            1 :             gfc_error ("The base name for %<declare variant%> must be "
   10486              :                        "specified at %L", &odv->where);
   10487              :           else
   10488              :             error_found = false;
   10489              :         }
   10490              :       else
   10491              :         {
   10492           37 :           if (!search_ns->contained
   10493           21 :               && !odv->base_proc_symtree->n.sym->attr.use_assoc
   10494            5 :               && strcmp (odv->base_proc_symtree->name,
   10495            5 :                          ns->proc_name->name))
   10496            1 :             gfc_error ("The base name at %L does not match the name of the "
   10497              :                        "current procedure", &odv->where);
   10498           36 :           else if (odv->base_proc_symtree->n.sym->attr.entry)
   10499            1 :             gfc_error ("The base name at %L must not be an entry name",
   10500              :                         &odv->where);
   10501           35 :           else if (odv->base_proc_symtree->n.sym->attr.generic)
   10502            1 :             gfc_error ("The base name at %L must not be a generic name",
   10503              :                         &odv->where);
   10504           34 :           else if (odv->base_proc_symtree->n.sym->attr.proc_pointer)
   10505            1 :             gfc_error ("The base name at %L must not be a procedure pointer",
   10506              :                         &odv->where);
   10507           33 :           else if (odv->base_proc_symtree->n.sym->attr.implicit_type)
   10508            1 :             gfc_error ("The base procedure at %L must have an explicit "
   10509              :                         "interface", &odv->where);
   10510              :           else
   10511              :             error_found = false;
   10512              :         }
   10513              : 
   10514          383 :       odv->checked_p = true;
   10515          383 :       if (error_found)
   10516              :         {
   10517            6 :           odv->error_p = true;
   10518            6 :           continue;
   10519              :         }
   10520              : 
   10521              :       /* Ignore directives that do not apply to the current procedure.  */
   10522          377 :       if ((odv->base_proc_symtree == NULL && search_ns != ns)
   10523          351 :           || (odv->base_proc_symtree != NULL
   10524           32 :               && !ns->proc_name->attr.use_assoc
   10525           19 :               && strcmp (odv->base_proc_symtree->name, ns->proc_name->name))
   10526          338 :           || (odv->base_proc_symtree != NULL
   10527           19 :               && ns->proc_name->attr.use_assoc
   10528           13 :               && strcmp (odv->base_proc_symtree->n.sym->name,
   10529              :                          ns->proc_name->name)))
   10530           44 :         continue;
   10531              : 
   10532          333 :       tree set_selectors = gfc_trans_omp_set_selector (odv->set_selectors,
   10533              :                                                        odv->where);
   10534          333 :       const char *variant_proc_name = odv->variant_proc_symtree->name;
   10535          333 :       gfc_symbol *variant_proc_sym = odv->variant_proc_symtree->n.sym;
   10536          333 :       if (variant_proc_sym == NULL || variant_proc_sym->attr.implicit_type)
   10537              :         {
   10538           39 :           gfc_symtree *proc_st;
   10539           39 :           gfc_find_sym_tree (variant_proc_name, gfc_current_ns, 1, &proc_st);
   10540           39 :           variant_proc_sym = proc_st ? proc_st->n.sym : NULL;
   10541              :         }
   10542           39 :       if (variant_proc_sym == NULL)
   10543              :         {
   10544            1 :           gfc_error ("Cannot find symbol %qs at %L", variant_proc_name,
   10545              :                                                      &odv->where);
   10546            1 :           continue;
   10547              :         }
   10548          332 :       set_selectors = omp_check_context_selector
   10549          332 :         (gfc_get_location (&odv->where), set_selectors,
   10550              :          OMP_CTX_DECLARE_VARIANT);
   10551          332 :       if (set_selectors != error_mark_node)
   10552              :         {
   10553          312 :           if (!variant_proc_sym->attr.implicit_type
   10554          312 :               && !variant_proc_sym->attr.subroutine
   10555           89 :               && !variant_proc_sym->attr.function)
   10556              :             {
   10557            0 :               gfc_error ("variant %qs at %L is not a function or subroutine",
   10558              :                          variant_proc_name, &odv->where);
   10559            0 :               variant_proc_sym = NULL;
   10560              :             }
   10561          312 :           else if (variant_proc_sym == ns->proc_name)
   10562              :             {
   10563            1 :               gfc_error ("variant %qs at %L is the same as base function",
   10564              :                          variant_proc_name, &odv->where);
   10565            1 :               variant_proc_sym = NULL;
   10566              :             }
   10567          311 :           else if (omp_get_context_selector (set_selectors,
   10568              :                                              OMP_TRAIT_SET_CONSTRUCT,
   10569              :                                              OMP_TRAIT_CONSTRUCT_SIMD)
   10570              :                    == NULL_TREE)
   10571              :             {
   10572          289 :               char err[256];
   10573          289 :               gfc_formal_arglist *last_arg = NULL, *extra_arg = NULL;
   10574          289 :               int nappend_args = 0;
   10575          289 :               if (odv->append_args_list)
   10576              :                 {
   10577           26 :                   gfc_formal_arglist *arg;
   10578           26 :                   int nargs = 0;
   10579           26 :                   for (arg = gfc_sym_get_dummy_args (ns->proc_name);
   10580           56 :                        arg; arg = arg->next)
   10581           30 :                     nargs++;
   10582              : 
   10583           26 :                   last_arg = gfc_sym_get_dummy_args (variant_proc_sym);
   10584           59 :                   for (int i = 1 ; i < nargs && last_arg; i++)
   10585            7 :                     last_arg = last_arg->next;
   10586           26 :                   if (nargs == 0)
   10587              :                     {
   10588            3 :                       extra_arg = last_arg;
   10589            3 :                       last_arg = NULL;
   10590            3 :                       variant_proc_sym->formal = NULL;
   10591              :                     }
   10592           23 :                   else if (last_arg)
   10593              :                     {
   10594           23 :                       extra_arg = last_arg->next;
   10595           23 :                       last_arg->next = NULL;
   10596              :                     }
   10597           76 :                   for (gfc_omp_namelist *n = odv->append_args_list; n != NULL;
   10598           50 :                        n = n->next)
   10599           50 :                     nappend_args++;
   10600              :                 }
   10601          289 :               if (!gfc_compare_interfaces (ns->proc_name, variant_proc_sym,
   10602              :                                            variant_proc_sym->name, 0, 1,
   10603              :                                            err, sizeof (err), NULL, NULL))
   10604              :                 {
   10605            2 :                   gfc_error ("variant %qs and base %qs at %L have "
   10606              :                              "incompatible types: %s",
   10607            2 :                              variant_proc_name, ns->proc_name->name,
   10608              :                              &odv->where, err);
   10609            2 :                   if (nappend_args)
   10610            0 :                     inform (gfc_get_location (&odv->append_args_list->where),
   10611              :                             "%<append_args%> clause implies that %qs has %d "
   10612              :                             "dummy arguments of integer type with "
   10613              :                             "%<omp_interop_kind%> kind", variant_proc_name,
   10614              :                             nappend_args);
   10615              :                   variant_proc_sym = NULL;
   10616              :                 }
   10617          289 :               if (last_arg)
   10618           23 :                 last_arg->next = extra_arg;
   10619          266 :               else if (extra_arg)
   10620            3 :                 variant_proc_sym->formal = extra_arg;
   10621           26 :               locus *loc = (odv->append_args_list
   10622          289 :                             ? &odv->append_args_list->where :  &odv->where);
   10623          289 :               int nextra_arg = 0;
   10624          342 :               for (; extra_arg; extra_arg = extra_arg->next)
   10625              :                 {
   10626           53 :                   nextra_arg++;
   10627           53 :                   if (!variant_proc_sym)
   10628            8 :                     continue;
   10629           45 :                   if (extra_arg->sym->ts.type != BT_INTEGER
   10630           43 :                       || extra_arg->sym->ts.kind != gfc_index_integer_kind
   10631           42 :                       || extra_arg->sym->attr.dimension
   10632           40 :                       || extra_arg->sym->attr.codimension
   10633           39 :                       || extra_arg->sym->attr.pointer
   10634           38 :                       || extra_arg->sym->attr.allocatable
   10635           37 :                       || extra_arg->sym->attr.proc_pointer)
   10636              :                     {
   10637            8 :                       gfc_error ("%qs at %L must be a nonpointer, "
   10638              :                                  "nonallocatable scalar integer dummy argument "
   10639              :                                  "of %<omp_interop_kind%> kind as it utilized "
   10640              :                                  "with the %<append_args%> clause at %L",
   10641              :                                  extra_arg->sym->name,
   10642              :                                  &extra_arg->sym->declared_at, loc);
   10643            8 :                       variant_proc_sym = NULL;
   10644              :                     }
   10645           45 :                   if (extra_arg->sym->attr.optional)
   10646              :                     {
   10647            2 :                       gfc_error ("%qs at %L with OPTIONAL attribute "
   10648              :                                  "not support when utilized with the "
   10649              :                                  "%<append_args%> clause at %L",
   10650              :                                  extra_arg->sym->name,
   10651              :                                  &extra_arg->sym->declared_at, loc);
   10652            2 :                       variant_proc_sym = NULL;
   10653              :                     }
   10654              :                 }
   10655          289 :               if (variant_proc_sym && nappend_args != nextra_arg)
   10656              :                 {
   10657            1 :                   gfc_error ("%qs at %L has %d but requires %d "
   10658              :                              "%<omp_interop_kind%> kind dummy arguments as it "
   10659              :                              "is utilized with the %<append_args%> clause at "
   10660              :                              "%L", variant_proc_sym->name,
   10661              :                              &variant_proc_sym->declared_at, nextra_arg,
   10662              :                              nappend_args, loc);
   10663            1 :                   variant_proc_sym = NULL;
   10664              :                 }
   10665              :             }
   10666          266 :           if ((odv->adjust_args_list != NULL || odv->append_args_list != NULL)
   10667          337 :               && omp_get_context_selector (set_selectors,
   10668              :                                            OMP_TRAIT_SET_CONSTRUCT,
   10669              :                                            OMP_TRAIT_CONSTRUCT_DISPATCH)
   10670              :                    == NULL_TREE)
   10671              :             {
   10672            6 :               gfc_error ("the %qs clause can only be specified if "
   10673              :                          "the %<dispatch%> selector of the construct "
   10674              :                          "selector set appears in the %<match%> clause at %L",
   10675            3 :                          odv->adjust_args_list ? "adjust_args" : "append_args",
   10676              :                          &odv->where);
   10677            3 :               variant_proc_sym = NULL;
   10678              :             }
   10679          312 :           if (variant_proc_sym != NULL)
   10680              :             {
   10681          296 :               gfc_set_sym_referenced (variant_proc_sym);
   10682          296 :               tree construct
   10683          296 :                 = omp_get_context_selector_list (set_selectors,
   10684              :                                                  OMP_TRAIT_SET_CONSTRUCT);
   10685          296 :               omp_mark_declare_variant (gfc_get_location (&odv->where),
   10686              :                                         gfc_get_symbol_decl (variant_proc_sym),
   10687              :                                         construct);
   10688          296 :               if (omp_context_selector_matches (set_selectors,
   10689              :                                                 NULL_TREE, false))
   10690              :                 {
   10691          216 :                   tree need_device_ptr_list = NULL_TREE;
   10692          216 :                   tree need_device_addr_list = NULL_TREE;
   10693          216 :                   tree append_args_tree = NULL_TREE;
   10694          216 :                   tree id = get_identifier ("omp declare variant base");
   10695          216 :                   tree variant = gfc_get_symbol_decl (variant_proc_sym);
   10696          216 :                   DECL_ATTRIBUTES (base_fn_decl)
   10697          216 :                     = tree_cons (id, build_tree_list (variant, set_selectors),
   10698          216 :                                  DECL_ATTRIBUTES (base_fn_decl));
   10699          216 :                   int arg_idx_offset = 0;
   10700          216 :                   if (gfc_return_by_reference (ns->proc_name))
   10701              :                     {
   10702            2 :                       arg_idx_offset++;
   10703            2 :                       if (ns->proc_name->ts.type == BT_CHARACTER)
   10704            2 :                         arg_idx_offset++;
   10705              :                     }
   10706          216 :                   int nargs = 0;
   10707          465 :                   for (gfc_formal_arglist *arg
   10708          216 :                         = gfc_sym_get_dummy_args (ns->proc_name);
   10709          465 :                        arg; arg = arg->next)
   10710          249 :                     nargs++;
   10711          216 :                   if (odv->append_args_list)
   10712              :                     {
   10713           14 :                       int append_arg_no = arg_idx_offset + nargs;
   10714           14 :                       tree last_arg = NULL_TREE;
   10715           14 :                       for (gfc_omp_namelist *n = odv->append_args_list;
   10716           43 :                            n != NULL; n = n->next)
   10717              :                         {
   10718           29 :                           tree pref = NULL_TREE;
   10719           29 :                           if (n->u.init.len)
   10720              :                             {
   10721           22 :                               pref = build_string (n->u.init.len,
   10722           11 :                                                    n->u2.init_interop);
   10723           11 :                               TREE_TYPE (pref) = build_array_type_nelts (
   10724              :                                                    unsigned_char_type_node,
   10725           11 :                                                    n->u.init.len);
   10726              :                             }
   10727              :                           /* Save location, (target + target sync) and
   10728              :                              prefer_type list in a tree list.  */
   10729           29 :                           tree t = build_tree_list (n->u.init.target
   10730              :                                                     ? boolean_true_node
   10731              :                                                     : boolean_false_node,
   10732           29 :                                                     n->u.init.targetsync
   10733              :                                                     ? boolean_true_node
   10734              :                                                     : boolean_false_node);
   10735           29 :                           t = build1_loc (gfc_get_location (&n->where),
   10736              :                                           NOP_EXPR, void_type_node, t);
   10737           29 :                           t = build_tree_list (t, pref);
   10738           29 :                           if (append_args_tree)
   10739              :                             {
   10740           15 :                               TREE_CHAIN (last_arg) = t;
   10741           15 :                               last_arg = t;
   10742              :                             }
   10743              :                           else
   10744              :                             append_args_tree = last_arg = t;
   10745              :                         }
   10746              :                       /* Store as 'purpose' = arg number to be used for inserting
   10747              :                          and 'value' = list of interop items.  */
   10748           14 :                       append_args_tree = build_tree_list (
   10749              :                                            build_int_cst (integer_type_node,
   10750           14 :                                                           append_arg_no),
   10751              :                                            append_args_tree);
   10752              :                     }
   10753          216 :                   vec<gfc_symbol *> adjust_args_list = vNULL;
   10754          216 :                   for (gfc_omp_namelist *arg_list = odv->adjust_args_list;
   10755          326 :                        arg_list != NULL; arg_list = arg_list->next)
   10756              :                     {
   10757          110 :                       int from, to;
   10758          110 :                       if (arg_list->expr == NULL || arg_list->sym)
   10759          204 :                         from = ((arg_list->u.adj_args.omp_num_args_minus
   10760           94 :                                  || arg_list->u.adj_args.omp_num_args_plus)
   10761           94 :                                 ? nargs : 1);
   10762              :                       else
   10763              :                         {
   10764           16 :                           if (arg_list->u.adj_args.omp_num_args_plus)
   10765            0 :                             mpz_add_ui (arg_list->expr->value.integer,
   10766            0 :                                         arg_list->expr->value.integer, nargs);
   10767           16 :                           if (arg_list->u.adj_args.omp_num_args_minus)
   10768            2 :                             mpz_ui_sub (arg_list->expr->value.integer, nargs,
   10769            2 :                                         arg_list->expr->value.integer);
   10770           16 :                           if (mpz_sgn (arg_list->expr->value.integer) <= 0)
   10771              :                             {
   10772            1 :                               gfc_warning (OPT_Wopenmp,
   10773              :                                            "Expected positive argument index "
   10774              :                                            "at %L", &arg_list->where);
   10775            1 :                               from = 1;
   10776              :                             }
   10777              :                           else
   10778           15 :                             from
   10779           15 :                               = (mpz_fits_sint_p (arg_list->expr->value.integer)
   10780           15 :                                  ? mpz_get_si (arg_list->expr->value.integer)
   10781              :                                  : INT_MAX);
   10782           16 :                           if (from > nargs)
   10783            1 :                             gfc_warning (OPT_Wopenmp,
   10784              :                                          "Argument index at %L exceeds number "
   10785              :                                          "of arguments %d", &arg_list->where,
   10786              :                                          nargs);
   10787              :                         }
   10788          110 :                       locus loc = arg_list->where;
   10789          110 :                       if (!arg_list->u.adj_args.range_start)
   10790              :                         to = from;
   10791              :                       else
   10792              :                         {
   10793            6 :                           loc = gfc_get_location_range (&arg_list->where, 0,
   10794              :                                                         &arg_list->where, 0,
   10795            6 :                                                         &arg_list->next->where);
   10796            6 :                           if (arg_list->next->expr == NULL)
   10797              :                             to = nargs;
   10798              :                           else
   10799              :                             {
   10800            4 :                               if (arg_list->next->u.adj_args.omp_num_args_plus)
   10801            0 :                                 mpz_add_ui (arg_list->next->expr->value.integer,
   10802            0 :                                             arg_list->next->expr->value.integer,
   10803              :                                             nargs);
   10804            4 :                               if (arg_list->next->u.adj_args.omp_num_args_minus)
   10805            2 :                                 mpz_ui_sub (arg_list->next->expr->value.integer,
   10806              :                                             nargs,
   10807            2 :                                             arg_list->next->expr->value.integer);
   10808            4 :                               if (mpz_sgn (arg_list->next->expr->value.integer)
   10809              :                                   <= 0)
   10810              :                                 {
   10811            0 :                                   gfc_warning (OPT_Wopenmp,
   10812              :                                                "Expected positive argument "
   10813              :                                                "index at %L", &loc);
   10814            0 :                                   to = 0;
   10815              :                                 }
   10816              :                               else
   10817            4 :                                 to = mpz_get_si (
   10818            4 :                                        arg_list->next->expr->value.integer);
   10819              :                             }
   10820            6 :                           if (from > to && to != 0)
   10821            1 :                             gfc_warning (OPT_Wopenmp,
   10822              :                                          "Upper argument index smaller than "
   10823              :                                          "lower one at %L", &loc);
   10824            6 :                           if (to > nargs)
   10825              :                             to = nargs;
   10826            6 :                           arg_list = arg_list->next;
   10827              :                         }
   10828          110 :                       if (from > nargs)
   10829            1 :                         continue;
   10830              :                       /* Change to zero based index.  */
   10831          109 :                       from--; to--;
   10832          109 :                       gfc_formal_arglist *arg = ns->proc_name->formal;
   10833          109 :                       if (!arg_list->sym && to >= from)
   10834           35 :                         for (int idx = 0; idx < from; idx++)
   10835           18 :                           arg = arg->next;
   10836          223 :                       for (int idx = from; idx <= to; idx++)
   10837              :                         {
   10838          114 :                           if (idx > from)
   10839            6 :                             arg = arg->next;
   10840          114 :                           if (arg_list->sym)
   10841              :                             {
   10842           91 :                               for (arg = ns->proc_name->formal, idx = 0;
   10843          201 :                                    arg != NULL; arg = arg->next, idx++)
   10844          200 :                                 if (arg->sym == arg_list->sym)
   10845              :                                   break;
   10846           91 :                               if (!arg || !arg_list->sym->attr.dummy)
   10847              :                                 {
   10848            1 :                                   gfc_error ("List item %qs at %L, declared at "
   10849              :                                              "%L, is not a dummy argument",
   10850              :                                              arg_list->sym->name, &loc,
   10851              :                                              &arg_list->sym->declared_at);
   10852            1 :                                   continue;
   10853              :                                 }
   10854              :                             }
   10855          113 :                           if (arg_list->u.adj_args.need_ptr
   10856           82 :                               && (arg->sym->ts.f90_type != BT_VOID
   10857           80 :                                   || !arg->sym->ts.u.derived->ts.is_iso_c
   10858           80 :                                   || (arg->sym->ts.u.derived->intmod_sym_id
   10859              :                                       != ISOCBINDING_PTR)
   10860           79 :                                   || arg->sym->attr.dimension))
   10861              :                             {
   10862            6 :                               gfc_error ("Argument %qs at %L to list item in "
   10863              :                                          "%<need_device_ptr%> at %L must be a "
   10864              :                                          "scalar of TYPE(C_PTR)",
   10865              :                                          arg->sym->name,
   10866              :                                          &arg->sym->declared_at, &loc);
   10867            6 :                               if (!arg->sym->attr.value)
   10868            6 :                                 inform (gfc_get_location (&loc),
   10869              :                                         "Consider using %<need_device_addr%> "
   10870              :                                         "instead");
   10871            6 :                               continue;
   10872              :                             }
   10873          107 :                           if (arg_list->u.adj_args.need_addr
   10874           11 :                               && arg->sym->attr.value)
   10875              :                             {
   10876            1 :                               gfc_error ("Argument %qs at %L to list item in "
   10877              :                                          "%<need_device_addr%> at %L must not "
   10878              :                                          "have the VALUE attribute",
   10879              :                                          arg->sym->name,
   10880              :                                          &arg->sym->declared_at, &loc);
   10881            1 :                               continue;
   10882              :                             }
   10883          106 :                           if (adjust_args_list.contains (arg->sym))
   10884              :                             {
   10885            7 :                               gfc_error ("%qs at %L is specified more than "
   10886            7 :                                          "once", arg->sym->name, &loc);
   10887            7 :                               continue;
   10888              :                             }
   10889           99 :                           adjust_args_list.safe_push (arg->sym);
   10890              : 
   10891           99 :                           if (arg_list->u.adj_args.need_addr)
   10892              :                             {
   10893              :                               /* TODO: Has to to support OPTIONAL and array
   10894              :                                  descriptors; should check for CLASS, coarrays?
   10895              :                                  Reject "abc" and 123 as actual arguments (in
   10896              :                                  gimplify.cc or in the FE? Reject noncontiguous
   10897              :                                  actuals?  Cf. also PR C++/118859.
   10898              :                                  Also check array-valued type(c_ptr).  */
   10899            7 :                               static bool warned = false;
   10900            7 :                               if (!warned)
   10901            1 :                                 sorry_at (gfc_get_location (&loc),
   10902              :                                           "%<need_device_addr%> not yet "
   10903              :                                           "supported");
   10904            7 :                               warned = true;
   10905            7 :                               continue;
   10906            7 :                             }
   10907           92 :                           if (arg_list->u.adj_args.need_ptr
   10908              :                               || arg_list->u.adj_args.need_addr)
   10909              :                             {
   10910              :                               // Store 0-based argument index,
   10911              :                               // as in gimplify_call_expr
   10912           74 :                               tree t
   10913           74 :                                 = build_tree_list (
   10914              :                                     NULL_TREE,
   10915              :                                     build_int_cst (integer_type_node,
   10916           74 :                                                    idx + arg_idx_offset));
   10917           74 :                               if (arg_list->u.adj_args.need_ptr)
   10918           74 :                                 need_device_ptr_list
   10919           74 :                                   = chainon (need_device_ptr_list, t);
   10920              :                               else
   10921            0 :                                 need_device_addr_list
   10922            0 :                                   = chainon (need_device_addr_list, t);
   10923              :                             }
   10924              :                         }
   10925              :                     }
   10926          216 :                   tree t = NULL_TREE;
   10927          216 :                   if (need_device_ptr_list
   10928          216 :                       || need_device_addr_list
   10929          180 :                       || append_args_tree)
   10930              :                     {
   10931           50 :                       t = build_tree_list (need_device_ptr_list,
   10932              :                                            need_device_addr_list),
   10933           50 :                       TREE_CHAIN (t) = append_args_tree;
   10934           50 :                       DECL_ATTRIBUTES (variant) = tree_cons (
   10935              :                         get_identifier ("omp declare variant variant args"), t,
   10936           50 :                         DECL_ATTRIBUTES (variant));
   10937              :                     }
   10938              :                 }
   10939              :             }
   10940              :         }
   10941              :     }
   10942        10937 : }
   10943              : 
   10944              : /* Add ptr for tracking as being allocated by GOMP_alloc. */
   10945              : 
   10946              : tree
   10947           29 : gfc_omp_call_add_alloc (tree ptr)
   10948              : {
   10949           29 :   static tree fn = NULL_TREE;
   10950           29 :   if (fn == NULL_TREE)
   10951              :     {
   10952            6 :       fn = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
   10953            6 :       tree att = build_tree_list (NULL_TREE, build_string (4, ". R "));
   10954            6 :       att = tree_cons (get_identifier ("fn spec"), att, TYPE_ATTRIBUTES (fn));
   10955            6 :       fn = build_type_attribute_variant (fn, att);
   10956            6 :       fn = build_fn_decl ("GOMP_add_alloc", fn);
   10957              :     }
   10958           29 :   return build_call_expr_loc (input_location, fn, 1, ptr);
   10959              : }
   10960              : 
   10961              : /* Generated function returns true when it was tracked via GOMP_add_alloc and
   10962              :    removes it from the tracking.  As called just before GOMP_free or omp_realloc
   10963              :    the pointer is or might become invalid, thus, it is always removed. */
   10964              : 
   10965              : tree
   10966           49 : gfc_omp_call_is_alloc (tree ptr)
   10967              : {
   10968           49 :   static tree fn = NULL_TREE;
   10969           49 :   if (fn == NULL_TREE)
   10970              :     {
   10971            6 :       fn = build_function_type_list (boolean_type_node, ptr_type_node,
   10972              :                                      NULL_TREE);
   10973            6 :       tree att = build_tree_list (NULL_TREE, build_string (4, ". R "));
   10974            6 :       att = tree_cons (get_identifier ("fn spec"), att, TYPE_ATTRIBUTES (fn));
   10975            6 :       fn = build_type_attribute_variant (fn, att);
   10976            6 :       fn = build_fn_decl ("GOMP_is_alloc", fn);
   10977              :     }
   10978           49 :   return build_call_expr_loc (input_location, fn, 1, ptr);
   10979              : }
   10980              : 
   10981              : tree
   10982           94 : gfc_trans_omp_metadirective (gfc_code *code)
   10983              : {
   10984           94 :   gfc_omp_variant *variant = code->ext.omp_variants;
   10985              : 
   10986           94 :   tree metadirective_tree = make_node (OMP_METADIRECTIVE);
   10987           94 :   SET_EXPR_LOCATION (metadirective_tree, gfc_get_location (&code->loc));
   10988           94 :   TREE_TYPE (metadirective_tree) = void_type_node;
   10989           94 :   OMP_METADIRECTIVE_VARIANTS (metadirective_tree) = NULL_TREE;
   10990              : 
   10991           94 :   tree tree_body = NULL_TREE;
   10992              : 
   10993          302 :   while (variant)
   10994              :     {
   10995          208 :       tree ctx = gfc_trans_omp_set_selector (variant->selectors,
   10996              :                                              variant->where);
   10997          208 :       ctx = omp_check_context_selector (gfc_get_location (&variant->where),
   10998              :                                         ctx, OMP_CTX_METADIRECTIVE);
   10999          208 :       if (ctx == error_mark_node)
   11000              :         return error_mark_node;
   11001              : 
   11002              :       /* If the selector doesn't match, drop the whole variant.  */
   11003          208 :       if (!omp_context_selector_matches (ctx, NULL_TREE, false))
   11004              :         {
   11005           25 :           variant = variant->next;
   11006           25 :           continue;
   11007              :         }
   11008              : 
   11009          183 :       gfc_code *next_code = variant->code->next;
   11010          183 :       if (next_code && tree_body == NULL_TREE)
   11011           19 :         tree_body = gfc_trans_code (next_code);
   11012              : 
   11013          183 :       if (next_code)
   11014           21 :         variant->code->next = NULL;
   11015          183 :       tree directive = gfc_trans_code (variant->code);
   11016          183 :       if (next_code)
   11017           21 :         variant->code->next = next_code;
   11018              : 
   11019           21 :       tree body = next_code ? tree_body : NULL_TREE;
   11020          183 :       tree omp_variant = make_omp_metadirective_variant (ctx, directive, body);
   11021          366 :       OMP_METADIRECTIVE_VARIANTS (metadirective_tree)
   11022          183 :         = chainon (OMP_METADIRECTIVE_VARIANTS (metadirective_tree),
   11023              :                    omp_variant);
   11024          183 :       variant = variant->next;
   11025              :     }
   11026              : 
   11027              :   /* TODO: Resolve the metadirective here if possible.   */
   11028              : 
   11029              :   return metadirective_tree;
   11030              : }
        

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